func processes() ([]Process, error) { results := make([]Process, 0, 50) mib := []int32{CTLKern, KernProc, KernProcProc, 0} buf, length, err := common.CallSyscall(mib) if err != nil { return results, err } // get kinfo_proc size k := KinfoProc{} procinfoLen := int(unsafe.Sizeof(k)) count := int(length / uint64(procinfoLen)) // parse buf to procs for i := 0; i < count; i++ { b := buf[i*procinfoLen : i*procinfoLen+procinfoLen] k, err := parseKinfoProc(b) if err != nil { continue } p, err := NewProcess(int32(k.KiPid)) if err != nil { continue } copyParams(&k, p) results = append(results, *p) } return results, nil }
func (p *Process) Cmdline() (string, error) { mib := []int32{CTLKern, KernProc, KernProcArgs, p.Pid} buf, _, err := common.CallSyscall(mib) if err != nil { return "", err } ret := strings.FieldsFunc(string(buf), func(r rune) bool { if r == '\u0000' { return true } return false }) return strings.Join(ret, " "), nil }
func (p *Process) getKProc() (*KinfoProc, error) { mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid} buf, length, err := common.CallSyscall(mib) if err != nil { return nil, err } procK := KinfoProc{} if length != uint64(unsafe.Sizeof(procK)) { return nil, err } k, err := parseKinfoProc(buf) if err != nil { return nil, err } return &k, nil }