fix bug while use common user

master
兔子 4 years ago
parent 26da8c8b88
commit bca0d42559

@ -18,8 +18,8 @@ import (
//FindProcessByName 通过进程名来查询应用信息 //FindProcessByName 通过进程名来查询应用信息
func FindProcessByName(name string) (datas []Process, err error) { func FindProcessByName(name string) (datas []Process, err error) {
return FindProcess(func(pname, exepath, folderpath string) bool { return FindProcess(func(in Process) bool {
if name == pname { if name == in.Name {
return true return true
} }
return false return false
@ -27,7 +27,7 @@ func FindProcessByName(name string) (datas []Process, err error) {
} }
// FindProcess 通过进程信息来查询应用信息 // FindProcess 通过进程信息来查询应用信息
func FindProcess(compare func(string, string, string) bool) (datas []Process, err error) { func FindProcess(compare func(Process) bool) (datas []Process, err error) {
var name, main string var name, main string
var mainb []byte var mainb []byte
paths, errs := ioutil.ReadDir("/proc") paths, errs := ioutil.ReadDir("/proc")
@ -37,74 +37,77 @@ func FindProcess(compare func(string, string, string) bool) (datas []Process, er
} }
for _, v := range paths { for _, v := range paths {
if v.IsDir() && Exists("/proc/"+v.Name()+"/comm") { if v.IsDir() && Exists("/proc/"+v.Name()+"/comm") {
name, err = readAsString("/proc/" + v.Name() + "/comm") name, _ = readAsString("/proc/" + v.Name() + "/comm")
if err != nil {
return
}
var tmp Process var tmp Process
tmp.LocalPath, err = os.Readlink("/proc/" + v.Name() + "/exe") tmp.LocalPath, err = os.Readlink("/proc/" + v.Name() + "/exe")
if err != nil {
return
}
tmp.Path = tmp.LocalPath tmp.Path = tmp.LocalPath
tmp.LocalPath = filepath.Dir(tmp.LocalPath) tmp.LocalPath = filepath.Dir(tmp.LocalPath)
tmp.ExecPath, err = os.Readlink("/proc/" + v.Name() + "/cwd") tmp.ExecPath, err = os.Readlink("/proc/" + v.Name() + "/cwd")
if err != nil {
return
}
tmp.Name = strings.TrimSpace(name) tmp.Name = strings.TrimSpace(name)
if compare(tmp.Name, tmp.LocalPath, tmp.ExecPath) { main, err = readAsString("/proc/" + v.Name() + "/status")
main, err = readAsString("/proc/" + v.Name() + "/status") if err != nil {
if err != nil { if compare(tmp) {
return datas = append(datas, tmp)
} continue
data := splitBy(main, ":")
tmp.Pid, _ = strconv.ParseInt(data["Pid"], 10, 64)
tmp.PPid, _ = strconv.ParseInt(data["PPid"], 10, 64)
tmp.TPid, _ = strconv.ParseInt(data["TracerPid"], 10, 64)
uids := splitBySpace(data["Uid"])
gids := splitBySpace(data["Gid"])
tmp.RUID, _ = strconv.Atoi(uids[0])
tmp.EUID, _ = strconv.Atoi(uids[1])
tmp.RGID, _ = strconv.Atoi(gids[0])
tmp.EGID, _ = strconv.Atoi(gids[1])
tmp.VmPeak, _ = strconv.ParseInt(splitBySpace(data["VmPeak"])[0], 10, 64)
tmp.VmSize, _ = strconv.ParseInt(splitBySpace(data["VmSize"])[0], 10, 64)
tmp.VmHWM, _ = strconv.ParseInt(splitBySpace(data["VmHWM"])[0], 10, 64)
tmp.VmRSS, _ = strconv.ParseInt(splitBySpace(data["VmRSS"])[0], 10, 64)
tmp.VmLck, _ = strconv.ParseInt(splitBySpace(data["VmLck"])[0], 10, 64)
tmp.VmData, _ = strconv.ParseInt(splitBySpace(data["VmData"])[0], 10, 64)
tmp.VmLck *= 1024
tmp.VmData *= 1024
tmp.VmPeak *= 1024
tmp.VmSize *= 1024
tmp.VmHWM *= 1024
tmp.VmRSS *= 1024
mainb, err = ioutil.ReadFile("/proc/" + v.Name() + "/cmdline")
if err != nil {
return
} }
args := bytes.Split(mainb, []byte{0}) }
for _, v := range args { data := splitBy(main, ":")
tmp.Args = append(tmp.Args, string(v)) tmp.Pid, _ = strconv.ParseInt(data["Pid"], 10, 64)
tmp.PPid, _ = strconv.ParseInt(data["PPid"], 10, 64)
tmp.TPid, _ = strconv.ParseInt(data["TracerPid"], 10, 64)
uids := splitBySpace(data["Uid"])
gids := splitBySpace(data["Gid"])
tmp.RUID, _ = strconv.Atoi(uids[0])
tmp.EUID, _ = strconv.Atoi(uids[1])
tmp.RGID, _ = strconv.Atoi(gids[0])
tmp.EGID, _ = strconv.Atoi(gids[1])
tmp.VmPeak, _ = strconv.ParseInt(splitBySpace(data["VmPeak"])[0], 10, 64)
tmp.VmSize, _ = strconv.ParseInt(splitBySpace(data["VmSize"])[0], 10, 64)
tmp.VmHWM, _ = strconv.ParseInt(splitBySpace(data["VmHWM"])[0], 10, 64)
tmp.VmRSS, _ = strconv.ParseInt(splitBySpace(data["VmRSS"])[0], 10, 64)
tmp.VmLck, _ = strconv.ParseInt(splitBySpace(data["VmLck"])[0], 10, 64)
tmp.VmData, _ = strconv.ParseInt(splitBySpace(data["VmData"])[0], 10, 64)
tmp.VmLck *= 1024
tmp.VmData *= 1024
tmp.VmPeak *= 1024
tmp.VmSize *= 1024
tmp.VmHWM *= 1024
tmp.VmRSS *= 1024
mainb, err = ioutil.ReadFile("/proc/" + v.Name() + "/cmdline")
if err != nil {
if compare(tmp) {
datas = append(datas, tmp)
continue
} }
}
args := bytes.Split(mainb, []byte{0})
for _, v := range args {
tmp.Args = append(tmp.Args, string(v))
}
mainb, err = ioutil.ReadFile("/proc/" + v.Name() + "/environ") mainb, err = ioutil.ReadFile("/proc/" + v.Name() + "/environ")
if err != nil { if err != nil {
return if compare(tmp) {
} datas = append(datas, tmp)
args = bytes.Split(mainb, []byte{0}) continue
for _, v := range args {
tmp.Env = append(tmp.Env, string(v))
} }
}
args = bytes.Split(mainb, []byte{0})
for _, v := range args {
tmp.Env = append(tmp.Env, string(v))
}
main, err = readAsString("/proc/" + v.Name() + "/stat") main, err = readAsString("/proc/" + v.Name() + "/stat")
if err != nil { if err != nil {
return if compare(tmp) {
datas = append(datas, tmp)
continue
} }
times := splitBySpace(main) }
uptime, _ := strconv.ParseInt(strings.TrimSpace(times[21]), 10, 64) times := splitBySpace(main)
tmp.Uptime = time.Unix(StartTime().Unix()+uptime/100, int64((float64(uptime)/100-float64(uptime/100))*1000000000)) uptime, _ := strconv.ParseInt(strings.TrimSpace(times[21]), 10, 64)
tmp.Uptime = time.Unix(StartTime().Unix()+uptime/100, int64((float64(uptime)/100-float64(uptime/100))*1000000000))
if compare(tmp) {
datas = append(datas, tmp) datas = append(datas, tmp)
} }
} }
@ -121,13 +124,7 @@ func FindProcessByPid(pid int64) (datas Process, err error) {
return return
} }
name, err = readAsString("/proc/" + fmt.Sprint(pid) + "/comm") name, err = readAsString("/proc/" + fmt.Sprint(pid) + "/comm")
if err != nil {
return
}
main, err = readAsString("/proc/" + fmt.Sprint(pid) + "/status") main, err = readAsString("/proc/" + fmt.Sprint(pid) + "/status")
if err != nil {
return
}
data := splitBy(main, ":") data := splitBy(main, ":")
datas.Name = strings.TrimSpace(name) datas.Name = strings.TrimSpace(name)
datas.Pid, _ = strconv.ParseInt(data["Pid"], 10, 64) datas.Pid, _ = strconv.ParseInt(data["Pid"], 10, 64)

Loading…
Cancel
Save