forked from mitchellh/go-ps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mitchellh#6 from keybase/taruti/cgo-only-on-osx
Separate cgo into darwin specific package
- Loading branch information
Showing
7 changed files
with
144 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package darwincgo |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// +build darwin | ||
|
||
package darwincgo | ||
|
||
/* | ||
#include <stdio.h> | ||
#include <errno.h> | ||
#include <libproc.h> | ||
extern int darwinProcesses(); | ||
extern void darwinProcessPaths(); | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"path/filepath" | ||
"sync" | ||
) | ||
|
||
// This lock is what verifies that C calling back into Go is only | ||
// modifying data once at a time. | ||
var darwinLock sync.Mutex | ||
var darwinProcsByPID map[int]*DarwinProcess | ||
|
||
// DarwinProcess is process definition for OS X | ||
type DarwinProcess struct { | ||
pid int | ||
ppid int | ||
path string | ||
} | ||
|
||
// Pid returns process id | ||
func (p *DarwinProcess) Pid() int { | ||
return p.pid | ||
} | ||
|
||
// PPid returns parent process id | ||
func (p *DarwinProcess) PPid() int { | ||
return p.ppid | ||
} | ||
|
||
// Executable returns process executable name | ||
func (p *DarwinProcess) Executable() string { | ||
path, _ := p.Path() | ||
return filepath.Base(path) | ||
} | ||
|
||
// Path returns path to process executable | ||
func (p *DarwinProcess) Path() (string, error) { | ||
return p.path, nil | ||
} | ||
|
||
//export goDarwinAppendProc | ||
func goDarwinAppendProc(pid C.pid_t, ppid C.pid_t, comm *C.char) { | ||
proc := &DarwinProcess{ | ||
pid: int(pid), | ||
ppid: int(ppid), | ||
} | ||
darwinProcsByPID[proc.pid] = proc | ||
} | ||
|
||
//export goDarwinSetPath | ||
func goDarwinSetPath(pid C.pid_t, comm *C.char) { | ||
if proc, ok := darwinProcsByPID[int(pid)]; ok && proc != nil { | ||
proc.path = C.GoString(comm) | ||
} | ||
} | ||
|
||
// ProcessMap returns a map of processes for the main library package. | ||
func ProcessMap() (map[int]*DarwinProcess, error) { | ||
darwinLock.Lock() | ||
defer darwinLock.Unlock() | ||
darwinProcsByPID = make(map[int]*DarwinProcess) | ||
|
||
// To ignore deadcode warnings for exported functions | ||
_ = goDarwinAppendProc | ||
_ = goDarwinSetPath | ||
|
||
// TODO: Investigate why darwinProcesses returns error even if process list | ||
// succeeds | ||
C.darwinProcesses() | ||
C.darwinProcessPaths() | ||
|
||
return darwinProcsByPID, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters