-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
880 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Mitchell Hashimoto | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,40 @@ | ||
// ps provides an API for finding and listing processes in a platform-agnostic | ||
// way. | ||
// | ||
// NOTE: If you're reading these docs online via GoDocs or some other system, | ||
// you might only see the Unix docs. This project makes heavy use of | ||
// platform-specific implementations. We recommend reading the source if you | ||
// are interested. | ||
package ps | ||
|
||
// Process is the generic interface that is implemented on every platform | ||
// and provides common operations for processes. | ||
type Process interface { | ||
// Pid is the process ID for this process. | ||
Pid() int | ||
|
||
// PPid is the parent process ID for this process. | ||
PPid() int | ||
|
||
// Executable name running this process. This is not a path to the | ||
// executable. | ||
Executable() string | ||
} | ||
|
||
// Processes returns all processes. | ||
// | ||
// This of course will be a point-in-time snapshot of when this method was | ||
// called. Some operating systems don't provide snapshot capability of the | ||
// process table, in which case the process table returned might contain | ||
// ephemeral entities that happened to be running when this was called. | ||
func Processes() ([]Process, error) { | ||
return processes() | ||
} | ||
|
||
// FindProcess looks up a single process by pid. | ||
// | ||
// Process will be nil and error will be nil if a matching process is | ||
// not found. | ||
func FindProcess(pid int) (Process, error) { | ||
return findProcess(pid) | ||
} |
139 changes: 139 additions & 0 deletions
139
third_party/github.com/mitchellh/go-ps/process_darwin.go
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,139 @@ | ||
//go:build darwin | ||
// +build darwin | ||
|
||
package ps | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
type DarwinProcess struct { | ||
pid int | ||
ppid int | ||
binary string | ||
} | ||
|
||
func (p *DarwinProcess) Pid() int { | ||
return p.pid | ||
} | ||
|
||
func (p *DarwinProcess) PPid() int { | ||
return p.ppid | ||
} | ||
|
||
func (p *DarwinProcess) Executable() string { | ||
return p.binary | ||
} | ||
|
||
func findProcess(pid int) (Process, error) { | ||
ps, err := processes() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, p := range ps { | ||
if p.Pid() == pid { | ||
return p, nil | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func processes() ([]Process, error) { | ||
buf, err := darwinSyscall() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
procs := make([]*kinfoProc, 0, 50) | ||
k := 0 | ||
for i := _KINFO_STRUCT_SIZE; i < buf.Len(); i += _KINFO_STRUCT_SIZE { | ||
proc := &kinfoProc{} | ||
err = binary.Read(bytes.NewBuffer(buf.Bytes()[k:i]), binary.LittleEndian, proc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
k = i | ||
procs = append(procs, proc) | ||
} | ||
|
||
darwinProcs := make([]Process, len(procs)) | ||
for i, p := range procs { | ||
darwinProcs[i] = &DarwinProcess{ | ||
pid: int(p.Pid), | ||
ppid: int(p.PPid), | ||
binary: darwinCstring(p.Comm), | ||
} | ||
} | ||
|
||
return darwinProcs, nil | ||
} | ||
|
||
func darwinCstring(s [16]byte) string { | ||
i := 0 | ||
for _, b := range s { | ||
if b != 0 { | ||
i++ | ||
} else { | ||
break | ||
} | ||
} | ||
|
||
return string(s[:i]) | ||
} | ||
|
||
func darwinSyscall() (*bytes.Buffer, error) { | ||
mib := [4]int32{_CTRL_KERN, _KERN_PROC, _KERN_PROC_ALL, 0} | ||
size := uintptr(0) | ||
|
||
_, _, errno := syscall.Syscall6( | ||
syscall.SYS___SYSCTL, | ||
uintptr(unsafe.Pointer(&mib[0])), | ||
4, | ||
0, | ||
uintptr(unsafe.Pointer(&size)), | ||
0, | ||
0) | ||
|
||
if errno != 0 { | ||
return nil, errno | ||
} | ||
|
||
bs := make([]byte, size) | ||
_, _, errno = syscall.Syscall6( | ||
syscall.SYS___SYSCTL, | ||
uintptr(unsafe.Pointer(&mib[0])), | ||
4, | ||
uintptr(unsafe.Pointer(&bs[0])), | ||
uintptr(unsafe.Pointer(&size)), | ||
0, | ||
0) | ||
|
||
if errno != 0 { | ||
return nil, errno | ||
} | ||
|
||
return bytes.NewBuffer(bs[0:size]), nil | ||
} | ||
|
||
const ( | ||
_CTRL_KERN = 1 | ||
_KERN_PROC = 14 | ||
_KERN_PROC_ALL = 0 | ||
_KINFO_STRUCT_SIZE = 648 | ||
) | ||
|
||
type kinfoProc struct { | ||
_ [40]byte | ||
Pid int32 | ||
_ [199]byte | ||
Comm [16]byte | ||
_ [301]byte | ||
PPid int32 | ||
_ [84]byte | ||
} |
12 changes: 12 additions & 0 deletions
12
third_party/github.com/mitchellh/go-ps/process_darwin_test.go
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,12 @@ | ||
//go:build darwin | ||
// +build darwin | ||
|
||
package ps | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestDarwinProcess_impl(t *testing.T) { | ||
var _ Process = new(DarwinProcess) | ||
} |
Oops, something went wrong.