Beispiel #1
0
func (p process) Name() (name string, harderror error, softerrors []error) {
	cname := C.malloc(C.PROC_PIDPATHINFO_MAXSIZE)
	defer C.free(cname)

	_, err := C.proc_pidpath(C.int(p.pid), cname, C.PROC_PIDPATHINFO_MAXSIZE)
	if err != nil {
		harderr := fmt.Errorf("Error while reading name of process %d: %v", p.pid, err)
		return "", harderr, nil
	}

	name, harderror = filepath.EvalSymlinks(C.GoString((*C.char)(cname)))
	return
}
// procTable returns a map of pid to binary path. see
// http://stackoverflow.com/questions/3018054/retrieve-names-of-running-processes
func procTable() map[int]string {
	n := C.proc_listpids(C.PROC_ALL_PIDS, 0, nil, 0)
	pids := make([]C.int, n)
	C.proc_listpids(C.PROC_ALL_PIDS, 0, unsafe.Pointer(&pids[0]), n)

	m := make(map[int]string, len(pids))
	var pathBuf [C.PROC_PIDPATHINFO_MAXSIZE]C.char
	for _, pid := range pids {
		if pid == 0 {
			continue
		}
		C.proc_pidpath(pid, unsafe.Pointer(&pathBuf[0]), C.PROC_PIDPATHINFO_MAXSIZE)
		m[int(pid)] = C.GoString(&pathBuf[0])
	}
	return m
}