Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 66d85bb

Browse files
committed
Get rid of cgo dependency
This gets rid of the cgo dependency by using binary package to destruct the psinfo struct (copied from /usr/include/sys/procfs.h).
1 parent 634230a commit 66d85bb

File tree

1 file changed

+84
-22
lines changed

1 file changed

+84
-22
lines changed

process_solaris.go

+84-22
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,95 @@
22

33
package ps
44

5-
// #include <fcntl.h>
6-
// #include <procfs.h>
7-
//
8-
// int read_psinfo(const char *path, psinfo_t *psi) {
9-
// int fh;
10-
// int retval = 0;
11-
// if ((fh = open(path, O_RDONLY)) >= 0) {
12-
// if (read(fh, psi, sizeof(psinfo_t)) == -1) {
13-
// retval = 1;
14-
// }
15-
// close(fh);
16-
// return retval;
17-
// }
18-
// return 2;
19-
// }
20-
import "C"
21-
22-
import "fmt"
5+
import (
6+
"encoding/binary"
7+
"fmt"
8+
"os"
9+
)
10+
11+
type ushort_t uint16
12+
13+
type id_t int32
14+
type pid_t int32
15+
type uid_t int32
16+
type gid_t int32
17+
18+
type dev_t uint64
19+
type size_t uint64
20+
type uintptr_t uint64
21+
22+
type timestruc_t [16]byte
23+
24+
// This is copy from /usr/include/sys/procfs.h
25+
type psinfo_t struct {
26+
Pr_flag int32 /* process flags (DEPRECATED; do not use) */
27+
Pr_nlwp int32 /* number of active lwps in the process */
28+
Pr_pid pid_t /* unique process id */
29+
Pr_ppid pid_t /* process id of parent */
30+
Pr_pgid pid_t /* pid of process group leader */
31+
Pr_sid pid_t /* session id */
32+
Pr_uid uid_t /* real user id */
33+
Pr_euid uid_t /* effective user id */
34+
Pr_gid gid_t /* real group id */
35+
Pr_egid gid_t /* effective group id */
36+
Pr_addr uintptr_t /* address of process */
37+
Pr_size size_t /* size of process image in Kbytes */
38+
Pr_rssize size_t /* resident set size in Kbytes */
39+
Pr_pad1 size_t
40+
Pr_ttydev dev_t /* controlling tty device (or PRNODEV) */
41+
42+
// Guess this following 2 ushort_t values require a padding to properly
43+
// align to the 64bit mark.
44+
Pr_pctcpu ushort_t /* % of recent cpu time used by all lwps */
45+
Pr_pctmem ushort_t /* % of system memory used by process */
46+
Pr_pad64bit [4]byte
47+
48+
Pr_start timestruc_t /* process start time, from the epoch */
49+
Pr_time timestruc_t /* usr+sys cpu time for this process */
50+
Pr_ctime timestruc_t /* usr+sys cpu time for reaped children */
51+
Pr_fname [16]byte /* name of execed file */
52+
Pr_psargs [80]byte /* initial characters of arg list */
53+
Pr_wstat int32 /* if zombie, the wait() status */
54+
Pr_argc int32 /* initial argument count */
55+
Pr_argv uintptr_t /* address of initial argument vector */
56+
Pr_envp uintptr_t /* address of initial environment vector */
57+
Pr_dmodel [1]byte /* data model of the process */
58+
Pr_pad2 [3]byte
59+
Pr_taskid id_t /* task id */
60+
Pr_projid id_t /* project id */
61+
Pr_nzomb int32 /* number of zombie lwps in the process */
62+
Pr_poolid id_t /* pool id */
63+
Pr_zoneid id_t /* zone id */
64+
Pr_contract id_t /* process contract */
65+
Pr_filler int32 /* reserved for future use */
66+
Pr_lwp [128]byte /* information for representative lwp */
67+
}
2368

2469
func (p *UnixProcess) Refresh() error {
25-
var psinfo C.psinfo_t
70+
var psinfo psinfo_t
71+
2672
path := fmt.Sprintf("/proc/%d/psinfo", p.pid)
27-
_, err := C.read_psinfo(C.CString(path), &psinfo)
73+
fh, err := os.Open(path)
2874
if err != nil {
2975
return err
3076
}
31-
p.ppid = int(psinfo.pr_ppid)
32-
p.binary = C.GoString(&psinfo.pr_fname[0])
77+
defer fh.Close()
78+
79+
err = binary.Read(fh, binary.LittleEndian, &psinfo)
80+
if err != nil {
81+
return err
82+
}
83+
84+
p.ppid = int(psinfo.Pr_ppid)
85+
p.binary = toString(psinfo.Pr_fname[:], 16)
3386
return nil
3487
}
88+
89+
func toString(array []byte, len int) string {
90+
for i := 0; i < len; i++ {
91+
if array[i] == 0 {
92+
return string(array[:i])
93+
}
94+
}
95+
return string(array[:])
96+
}

0 commit comments

Comments
 (0)