Skip to content

Commit dee2c1b

Browse files
committed
libproc: initial implements
Signed-off-by: Koichi Shiraishi <[email protected]>
1 parent d1426a2 commit dee2c1b

14 files changed

+689
-0
lines changed

bsd_info_darwin_amd64.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-FileCopyrightText: 2021 The Go Darwin Authors
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
//go:build ignore
5+
// +build ignore
6+
7+
package libproc
8+
9+
/*
10+
#cgo CFLAGS: -mmacosx-version-min=12.0
11+
12+
#include <sys/proc_info.h>
13+
*/
14+
import "C"
15+
16+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/param.h#L95
17+
const MaxComLen = C.MAXCOMLEN
18+
19+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L63-L86
20+
type ProcBsdinfo C.struct_proc_bsdinfo

darwin_amd64.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// SPDX-FileCopyrightText: 2021 The Go Darwin Authors
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
//go:build ignore
5+
// +build ignore
6+
7+
package libproc
8+
9+
/*
10+
#cgo CFLAGS: -mmacosx-version-min=12.0
11+
12+
#include <stdbool.h>
13+
#include <stdint.h>
14+
15+
#include <libproc.h>
16+
*/
17+
import "C"
18+
19+
const (
20+
ProcPidpathinfoSize = C.PROC_PIDPATHINFO_SIZE
21+
ProcPidpathinfoMaxsize = C.PROC_PIDPATHINFO_MAXSIZE
22+
)
23+
24+
type PID C.pid_t
25+
26+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/libsyscall/wrappers/libproc/libproc.h#L41-L61
27+
const (
28+
/*!
29+
* @define PROC_LISTPIDSPATH_PATH_IS_VOLUME
30+
* @discussion This flag indicates that all processes that hold open
31+
* file references on the volume associated with the specified
32+
* path should be returned.
33+
*/
34+
ProcListpidspathPathIsVolume = C.PROC_LISTPIDSPATH_PATH_IS_VOLUME
35+
36+
/*!
37+
* @define PROC_LISTPIDSPATH_EXCLUDE_EVTONLY
38+
* @discussion This flag indicates that file references that were opened
39+
* with the O_EVTONLY flag should be excluded from the matching
40+
* criteria.
41+
*/
42+
ProcListpidspathExcludeEvtonly = C.PROC_LISTPIDSPATH_EXCLUDE_EVTONLY
43+
)
44+
45+
// ProcType used to specify what type of processes you are interested
46+
// in in other calls, such as `listpids`.
47+
type ProcType uint32
48+
49+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L55-L61
50+
const (
51+
ProcAllPids ProcType = C.PROC_ALL_PIDS
52+
ProcPgrpOnly ProcType = C.PROC_PGRP_ONLY
53+
ProcTtyOnly ProcType = C.PROC_TTY_ONLY
54+
ProcUidOnly ProcType = C.PROC_UID_ONLY
55+
ProcRUIDOnly ProcType = C.PROC_RUID_ONLY
56+
ProcPPIDOnly ProcType = C.PROC_PPID_ONLY
57+
ProcKDBGOnly ProcType = C.PROC_KDBG_ONLY
58+
)
59+
60+
type ProcTaskInfo C.struct_proc_taskinfo

file_info_darwin_amd64.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-FileCopyrightText: 2021 The Go Darwin Authors
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
//go:build ignore
5+
// +build ignore
6+
7+
package libproc
8+
9+
/*
10+
#cgo CFLAGS: -mmacosx-version-min=12.0
11+
12+
#include <sys/proc_info.h>
13+
*/
14+
import "C"
15+
16+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L727
17+
type ProcFDType C.uint32_t
18+
19+
// defns of process file desc type
20+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L714-L723
21+
const (
22+
ProxFDTypeAtalk ProcFDType = C.PROX_FDTYPE_ATALK
23+
ProxFDTypeVnode ProcFDType = C.PROX_FDTYPE_VNODE
24+
ProxFDTypeSocket ProcFDType = C.PROX_FDTYPE_SOCKET
25+
ProxFDTypePshm ProcFDType = C.PROX_FDTYPE_PSHM
26+
ProxFDTypePsem ProcFDType = C.PROX_FDTYPE_PSEM
27+
ProxFDTypeKqueue ProcFDType = C.PROX_FDTYPE_KQUEUE
28+
ProxFDTypePipe ProcFDType = C.PROX_FDTYPE_PIPE
29+
ProxFDTypeFsevents ProcFDType = C.PROX_FDTYPE_FSEVENTS
30+
ProxFDTypeNetpolicy ProcFDType = C.PROX_FDTYPE_NETPOLICY
31+
)

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
module go-darwin.dev/libproc
22

33
go 1.17
4+
5+
require (
6+
go-darwin.dev/sys v0.0.0-20210627024002-32d42754375c
7+
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22
8+
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
go-darwin.dev/sys v0.0.0-20210627024002-32d42754375c h1:xG0zQnKQMsBPcBl1qe1ubT3qwHEPYpWohgUU94698e4=
2+
go-darwin.dev/sys v0.0.0-20210627024002-32d42754375c/go.mod h1:1KrBulURjz9K38r1eSysg/zBcdiEtaEDNPGbag0u5K0=
3+
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 h1:RqytpXGR1iVNX7psjB3ff8y7sNFinVFvkx1c8SjBkio=
4+
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

kmesg_buffer_darwin_amd64.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-FileCopyrightText: 2021 The Go Darwin Authors
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
//go:build ignore
5+
// +build ignore
6+
7+
package libproc
8+
9+
/*
10+
#cgo CFLAGS: -mmacosx-version-min=12.0
11+
12+
#include <sys/msgbuf.h>
13+
*/
14+
import "C"
15+
16+
const (
17+
MaxMsgBSize = C.MAX_MSG_BSIZE
18+
)

libproc.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// SPDX-FileCopyrightText: 2021 The Go Darwin Authors
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
//go:build darwin && amd64 && gc
5+
// +build darwin,amd64,gc
6+
7+
package libproc
8+
9+
import (
10+
_ "runtime" // for go:linkname
11+
"unsafe"
12+
13+
"golang.org/x/sys/unix"
14+
15+
"go-darwin.dev/sys"
16+
)
17+
18+
// ProcListallpids retursn the list all pids.
19+
func ProcListallpids() (numPids int, err error) {
20+
r1, _, kret := procListallpids()
21+
if sys.KernReturn(kret) != sys.KernSuccess {
22+
return 0, sys.KernErrno(sys.KernReturn(kret))
23+
}
24+
25+
numPids = int(r1 / sys.Sizeof_C_int)
26+
return
27+
}
28+
29+
//go:nosplit
30+
func procListallpids() (r1, r2 uintptr, err unix.Errno) {
31+
return sys.Syscall(libc_proc_listallpids_trampoline_addr, uintptr(0), uintptr(0), uintptr(0))
32+
}
33+
34+
var libc_proc_listallpids_trampoline_addr uintptr
35+
36+
//go:cgo_import_dynamic libc_proc_listallpids proc_listallpids "/usr/lib/libproc.dylib"
37+
38+
// ProcPidpath given a pid, returns the full executable name including directory
39+
// paths and the longer-than-16-chars executable name.
40+
func ProcPidpath(pid PID) (path string, err error) {
41+
buffer := make([]byte, ProcPidpathinfoSize)
42+
_, _, kret := procPidpath(pid, unsafe.Pointer(&buffer[0]), uint32(ProcPidpathinfoMaxsize))
43+
if sys.KernReturn(kret) != sys.KernSuccess {
44+
return "", sys.KernErrno(sys.KernReturn(kret))
45+
}
46+
47+
path = sys.GoString(&buffer[0])
48+
return
49+
}
50+
51+
//go:nosplit
52+
func procPidpath(pid PID, buffer unsafe.Pointer, buffersize uint32) (r1, r2 uintptr, err unix.Errno) {
53+
return sys.Syscall(libc_proc_pidpath_trampoline_addr, uintptr(pid), uintptr(buffer), uintptr(buffersize))
54+
}
55+
56+
var libc_proc_pidpath_trampoline_addr uintptr
57+
58+
//go:cgo_import_dynamic libc_proc_pidpath proc_pidpath "/usr/lib/libproc.dylib"
59+
60+
// $ pushd internal/gen > /dev/null 2>&1; go run main.go -fname /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/libproc.h; popd > /dev/null 2>&1
61+
// TODO(zchee): int proc_listpidspath(uint32_t type, uint32_t typeinfo, const char * path, uint32_t pathflags, void * buffer, int buffersize)
62+
// TODO(zchee): int proc_listpids(uint32_t type, uint32_t typeinfo, void * buffer, int buffersize)
63+
// TODO(zchee): int proc_listpgrppids(pid_t pgrpid, void * buffer, int buffersize)
64+
// TODO(zchee): int proc_listchildpids(pid_t ppid, void * buffer, int buffersize)
65+
// TODO(zchee): int proc_pidinfo(int pid, int flavor, uint64_t arg, void * buffer, int buffersize)
66+
// TODO(zchee): int proc_pidfdinfo(int pid, int fd, int flavor, void * buffer, int buffersize)
67+
// TODO(zchee): int proc_pidfileportinfo(int pid, uint32_t fileport, int flavor, void * buffer, int buffersize)
68+
// TODO(zchee): int proc_name(int pid, void * buffer, uint32_t buffersize)
69+
// TODO(zchee): int proc_regionfilename(int pid, uint64_t address, void * buffer, uint32_t buffersize)
70+
// TODO(zchee): int proc_kmsgbuf(void * buffer, uint32_t buffersize)
71+
// TODO(zchee): int proc_pidpath_audittoken(audit_token_t * audittoken, void * buffer, uint32_t buffersize)
72+
// TODO(zchee): int proc_libversion(int * major, int * minor)
73+
// TODO(zchee): int proc_pid_rusage(int pid, int flavor, rusage_info_t * buffer)
74+
// TODO(zchee): int proc_setpcontrol(const int control)
75+
// TODO(zchee): int proc_setpcontrol(const int control)
76+
// TODO(zchee): int proc_track_dirty(pid_t pid, uint32_t flags)
77+
// TODO(zchee): int proc_set_dirty(pid_t pid, _Bool dirty)
78+
// TODO(zchee): int proc_get_dirty(pid_t pid, uint32_t * flags)
79+
// TODO(zchee): int proc_clear_dirty(pid_t pid, uint32_t flags)
80+
// TODO(zchee): int proc_terminate(pid_t pid, int * sig)
81+
// TODO(zchee): int proc_set_no_smt()
82+
// TODO(zchee): int proc_setthread_no_smt()
83+
// TODO(zchee): int proc_set_csm(uint32_t flags)
84+
// TODO(zchee): int proc_setthread_csm(uint32_t flags)
85+
// TODO(zchee): int proc_udata_info(int pid, int flavor, void * buffer, int buffersize)

libproc.s

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-FileCopyrightText: 2021 The Go Darwin Authors
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
//go:build darwin && amd64 && gc
5+
// +build darwin,amd64,gc
6+
7+
#include "textflag.h"
8+
9+
// int proc_listallpids(void * buffer, int buffersize)
10+
GLOBL ·libc_proc_listallpids_trampoline_addr(SB), RODATA, $8
11+
DATA ·libc_proc_listallpids_trampoline_addr(SB)/8, $libc_proc_listallpids_trampoline<>(SB)
12+
13+
TEXT libc_proc_listallpids_trampoline<>(SB), NOSPLIT, $0-0
14+
JMP libc_proc_listallpids(SB)
15+
16+
// int proc_pidpath(int pid, void * buffer, uint32_t buffersize)
17+
GLOBL ·libc_proc_pidpath_trampoline_addr(SB), RODATA, $8
18+
DATA ·libc_proc_pidpath_trampoline_addr(SB)/8, $libc_proc_pidpath_trampoline<>(SB)
19+
20+
TEXT libc_proc_pidpath_trampoline<>(SB), NOSPLIT, $0-0
21+
JMP libc_proc_pidpath(SB)

net_info_darwin_amd64.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// SPDX-FileCopyrightText: 2021 The Go Darwin Authors
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
//go:build ignore
5+
// +build ignore
6+
7+
// for in4in6_addr
8+
// +godefs map struct_in_addr [4]byte // in_addr
9+
10+
// for in_sockinfo
11+
// +godefs map struct___1 InsiV4 // insi_v4
12+
// +godefs map struct___2 InsiV6 // insi_v6
13+
14+
package libproc
15+
16+
/*
17+
#cgo CFLAGS: -mmacosx-version-min=12.0
18+
19+
#include <stdlib.h>
20+
21+
#include <sys/proc_info.h>
22+
23+
struct insi_v4 {
24+
u_char in4_tos;
25+
};
26+
27+
struct insi_v6 {
28+
uint8_t in6_hlim;
29+
int in6_cksum;
30+
u_short in6_ifindex;
31+
short in6_hops;
32+
};
33+
34+
// local host table entry
35+
union insi_laddr {
36+
struct in4in6_addr ina_46;
37+
struct in6_addr ina_6;
38+
};
39+
*/
40+
import "C"
41+
42+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L614-L617
43+
type SocketFDinfo C.struct_socket_fdinfo
44+
45+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L298-L304
46+
type ProcFileinfo C.struct_proc_fileinfo
47+
48+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L714-L723
49+
const (
50+
SockinfoGeneric = C.SOCKINFO_GENERIC
51+
SockinfoIn = C.SOCKINFO_IN
52+
SockinfoKernCtl = C.SOCKINFO_KERN_CTL
53+
SockinfoKernEvent = C.SOCKINFO_KERN_EVENT
54+
SockinfoNdrv = C.SOCKINFO_NDRV
55+
SockinfoTCP = C.SOCKINFO_TCP
56+
SockinfoUn = C.SOCKINFO_UN
57+
SockinfoVsock = C.SOCKINFO_VSOCK
58+
)
59+
60+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L583-L612
61+
type SocketInfo C.struct_socket_info
62+
63+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L332-L357
64+
type VInfoStat C.struct_vinfo_stat
65+
66+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L562-L570
67+
type SockbufInfo C.struct_sockbuf_info
68+
69+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/socket.h#L612-L619
70+
type SockProto C.struct_sockproto
71+
72+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L414-L417
73+
type In4In6Addr C.struct_in4in6_addr
74+
75+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L419-L447
76+
type InSockinfo C.struct_in_sockinfo
77+
78+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L438-L440
79+
type InsiV4 C.struct_insi_v4
80+
81+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L441-L446
82+
type InsiV6 C.struct_insi_v6
83+
84+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L434-L437
85+
type InsiLaddr C.union_insi_laddr
86+
87+
type TCPSIState C.int
88+
89+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L459-L470
90+
const (
91+
TSI_S_CLOSED TCPSIState = C.TSI_S_CLOSED // closed
92+
TSI_S_LISTEN TCPSIState = C.TSI_S_LISTEN // listening for connection
93+
TSI_S_SYN_SENT TCPSIState = C.TSI_S_SYN_SENT // active, have sent syn
94+
TSI_S_SYN_RECEIVED TCPSIState = C.TSI_S_SYN_RECEIVED // have send and received syn
95+
TSI_S_ESTABLISHED TCPSIState = C.TSI_S_ESTABLISHED // established
96+
TSI_S__CLOSE_WAIT TCPSIState = C.TSI_S__CLOSE_WAIT // rcvd fin, waiting for close
97+
TSI_S_FIN_WAIT_1 TCPSIState = C.TSI_S_FIN_WAIT_1 // have closed, sent fin
98+
TSI_S_CLOSING TCPSIState = C.TSI_S_CLOSING // closed xchd FIN; await FIN ACK
99+
TSI_S_LAST_ACK TCPSIState = C.TSI_S_LAST_ACK // had fin and close; await FIN ACK
100+
TSI_S_FIN_WAIT_2 TCPSIState = C.TSI_S_FIN_WAIT_2 // have closed, fin is acked
101+
TSI_S_TIME_WAIT TCPSIState = C.TSI_S_TIME_WAIT // in 2*msl quiet wait after close
102+
TSI_S_RESERVED TCPSIState = C.TSI_S_RESERVED // pseudo state: reserved
103+
)
104+
105+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L472-L480
106+
type TCPSockinfo C.struct_tcp_sockinfo
107+
108+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L487-L498
109+
type UnSocinfo C.struct_un_sockinfo
110+
111+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L504-L508
112+
type NdrvInfo C.struct_ndrv_info
113+
114+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L514-L518
115+
type KernEventInfo C.struct_kern_event_info
116+
117+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L524-L532
118+
type KernCTLInfo C.struct_kern_ctl_info
119+
120+
// https://github.com/apple-opensource/xnu/blob/7195.101.1/bsd/sys/proc_info.h#L538-L543
121+
type VsockSockinfo C.struct_vsock_sockinfo

0 commit comments

Comments
 (0)