Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sigar.ProcEnv for getting a process's environment #62

Merged
merged 1 commit into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Added support to `github.com/gosigar/sys/windows` for querying and enabling
privileges in a process token.
- Added utility code for interfacing with linux NETLINK_INET_DIAG. #60
- Added `ProcEnv` for getting a process's environment variables. #61

### Changed
- Changed several `OpenProcess` calls on Windows to request the lowest possible
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The features vary by operating system.
| LoadAverage | X | X | | X | X |
| Mem | X | X | X | X | X |
| ProcArgs | X | X | X | | X |
| ProcEnv | X | X | | | X |
| ProcExe | X | X | | | X |
| ProcFDUsage | X | | | | X |
| ProcList | X | X | X | | X |
Expand Down
12 changes: 12 additions & 0 deletions sigar_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,18 @@ func (self *ProcArgs) Get(pid int) error {
return err
}

func (self *ProcEnv) Get(pid int) error {
if self.Vars == nil {
self.Vars = map[string]string{}
}

env := func(k, v string) {
self.Vars[k] = v
}

return kern_procargs(pid, nil, nil, env)
}

func (self *ProcExe) Get(pid int) error {
exe := func(arg string) {
self.Name = arg
Expand Down
4 changes: 4 additions & 0 deletions sigar_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ type ProcArgs struct {
List []string
}

type ProcEnv struct {
Vars map[string]string
}

type ProcExe struct {
Name string
Cwd string
Expand Down
11 changes: 11 additions & 0 deletions sigar_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ func TestProcArgs(t *testing.T) {
}
}

func TestProcEnv(t *testing.T) {
env := &ProcEnv{}
if assert.NoError(t, skipNotImplemented(t, env.Get(os.Getpid()), "windows", "openbsd")) {
assert.True(t, len(env.Vars) > 0, "env is empty")

for k, v := range env.Vars {
assert.Equal(t, os.Getenv(k), v)
}
}
}

func TestProcExe(t *testing.T) {
exe := ProcExe{}
if assert.NoError(t, skipNotImplemented(t, exe.Get(os.Getppid()), "windows")) {
Expand Down
28 changes: 28 additions & 0 deletions sigar_linux_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,34 @@ func (self *ProcArgs) Get(pid int) error {
return nil
}

func (self *ProcEnv) Get(pid int) error {
contents, err := readProcFile(pid, "environ")
if err != nil {
return err
}

if self.Vars == nil {
self.Vars = map[string]string{}
}

pairs := bytes.Split(contents, []byte{0})
for _, kv := range pairs {
parts := bytes.SplitN(kv, []byte{'='}, 2)
if len(parts) != 2 {
continue
}

key := string(bytes.TrimSpace(parts[0]))
if key == "" {
continue
}

self.Vars[key] = string(bytes.TrimSpace(parts[1]))
}

return nil
}

func (self *ProcExe) Get(pid int) error {
fields := map[string]*string{
"exe": &self.Name,
Expand Down
4 changes: 4 additions & 0 deletions sigar_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ func (self *ProcArgs) Get(pid int) error {
return nil
}

func (self *ProcEnv) Get(pid int) error {
return ErrNotImplemented{runtime.GOOS}
}

func (self *ProcState) Get(pid int) error {
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions sigar_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (self *FDUsage) Get() error {
return ErrNotImplemented{runtime.GOOS}
}

func (self *ProcEnv) Get(pid int) error {
return ErrNotImplemented{runtime.GOOS}
}

func (self *ProcExe) Get(pid int) error {
return ErrNotImplemented{runtime.GOOS}
}
Expand Down