Пример #1
0
func (self *ProcList) Get() error {
	n := C.proc_listpids(C.PROC_ALL_PIDS, 0, nil, 0)
	if n <= 0 {
		return syscall.EINVAL
	}
	buf := make([]byte, n)
	n = C.proc_listpids(C.PROC_ALL_PIDS, 0, unsafe.Pointer(&buf[0]), n)
	if n <= 0 {
		return syscall.ENOMEM
	}

	var pid int32
	num := int(n) / binary.Size(pid)
	list := make([]int, 0, num)
	bbuf := bytes.NewBuffer(buf)

	for i := 0; i < num; i++ {
		if err := binary.Read(bbuf, binary.LittleEndian, &pid); err != nil {
			return err
		}
		if pid == 0 {
			continue
		}

		list = append(list, int(pid))
	}

	self.List = list

	return nil
}
Пример #2
0
func getAllPids() (pids []uint, harderror error, softerrors []error) {
	var pid C.pid_t
	pidSize := unsafe.Sizeof(pid)
	cpidsSize := pidSize * 1024 * 2
	cpids := C.malloc(C.size_t(cpidsSize))
	defer C.free(cpids)

	bytesUsed, err := C.proc_listpids(C.PROC_ALL_PIDS, 0, cpids, C.int(cpidsSize))
	if err != nil {
		return nil, err, nil
	}

	numberOfPids := uintptr(bytesUsed) / pidSize
	pids = make([]uint, 0, numberOfPids)
	cpidsSlice := *(*[]C.pid_t)(unsafe.Pointer(
		&reflect.SliceHeader{
			Data: uintptr(unsafe.Pointer(cpids)),
			Len:  int(numberOfPids),
			Cap:  int(numberOfPids)}))

	for i, _ := range cpidsSlice {
		if cpidsSlice[i] == 0 {
			continue
		}

		pids = append(pids, uint(cpidsSlice[i]))
	}

	return pids, nil, nil
}
Пример #3
0
// 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
}