Пример #1
0
func ListAgentDirs() ([]string, error) {
	agentDirs, err := f.DirsUnder(g.SelfDir)
	if err != nil {
		log.Println("list dirs under", g.SelfDir, "fail", err)
	}
	return agentDirs, err
}
Пример #2
0
func AllProcs() (ps []*Proc, err error) {
	var dirs []string
	dirs, err = file.DirsUnder("/proc")
	if err != nil {
		return
	}

	size := len(dirs)
	if size == 0 {
		return
	}

	for i := 0; i < size; i++ {
		pid, e := strconv.Atoi(dirs[i])
		if e != nil {
			continue
		}

		statusFile := fmt.Sprintf("/proc/%d/status", pid)
		cmdlineFile := fmt.Sprintf("/proc/%d/cmdline", pid)
		if !file.IsExist(statusFile) || !file.IsExist(cmdlineFile) {
			continue
		}

		name, e := ReadName(statusFile)
		if e != nil {
			continue
		}

		cmdlineBytes, e := file.ToBytes(cmdlineFile)
		if e != nil {
			continue
		}

		cmdlineBytesLen := len(cmdlineBytes)
		if cmdlineBytesLen == 0 {
			continue
		}

		noNut := make([]byte, 0, cmdlineBytesLen)

		for j := 0; j < cmdlineBytesLen; j++ {
			if cmdlineBytes[j] != 0 {
				noNut = append(noNut, cmdlineBytes[j])
			}
		}

		p := Proc{Pid: pid, Name: name, Cmdline: string(noNut)}
		ps = append(ps, &p)
	}

	return
}