fix bug while use common user

master
兔子 4 years ago
parent 26da8c8b88
commit bca0d42559

@ -18,8 +18,8 @@ import (
//FindProcessByName 通过进程名来查询应用信息
func FindProcessByName(name string) (datas []Process, err error) {
return FindProcess(func(pname, exepath, folderpath string) bool {
if name == pname {
return FindProcess(func(in Process) bool {
if name == in.Name {
return true
}
return false
@ -27,7 +27,7 @@ func FindProcessByName(name string) (datas []Process, err error) {
}
// 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 mainb []byte
paths, errs := ioutil.ReadDir("/proc")
@ -37,26 +37,19 @@ func FindProcess(compare func(string, string, string) bool) (datas []Process, er
}
for _, v := range paths {
if v.IsDir() && Exists("/proc/"+v.Name()+"/comm") {
name, err = readAsString("/proc/" + v.Name() + "/comm")
if err != nil {
return
}
name, _ = readAsString("/proc/" + v.Name() + "/comm")
var tmp Process
tmp.LocalPath, err = os.Readlink("/proc/" + v.Name() + "/exe")
if err != nil {
return
}
tmp.Path = tmp.LocalPath
tmp.LocalPath = filepath.Dir(tmp.LocalPath)
tmp.ExecPath, err = os.Readlink("/proc/" + v.Name() + "/cwd")
if err != nil {
return
}
tmp.Name = strings.TrimSpace(name)
if compare(tmp.Name, tmp.LocalPath, tmp.ExecPath) {
main, err = readAsString("/proc/" + v.Name() + "/status")
if err != nil {
return
if compare(tmp) {
datas = append(datas, tmp)
continue
}
}
data := splitBy(main, ":")
tmp.Pid, _ = strconv.ParseInt(data["Pid"], 10, 64)
@ -82,7 +75,10 @@ func FindProcess(compare func(string, string, string) bool) (datas []Process, er
tmp.VmRSS *= 1024
mainb, err = ioutil.ReadFile("/proc/" + v.Name() + "/cmdline")
if err != nil {
return
if compare(tmp) {
datas = append(datas, tmp)
continue
}
}
args := bytes.Split(mainb, []byte{0})
for _, v := range args {
@ -91,7 +87,10 @@ func FindProcess(compare func(string, string, string) bool) (datas []Process, er
mainb, err = ioutil.ReadFile("/proc/" + v.Name() + "/environ")
if err != nil {
return
if compare(tmp) {
datas = append(datas, tmp)
continue
}
}
args = bytes.Split(mainb, []byte{0})
for _, v := range args {
@ -100,11 +99,15 @@ func FindProcess(compare func(string, string, string) bool) (datas []Process, er
main, err = readAsString("/proc/" + v.Name() + "/stat")
if err != nil {
return
if compare(tmp) {
datas = append(datas, tmp)
continue
}
}
times := splitBySpace(main)
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)
}
}
@ -121,13 +124,7 @@ func FindProcessByPid(pid int64) (datas Process, err error) {
return
}
name, err = readAsString("/proc/" + fmt.Sprint(pid) + "/comm")
if err != nil {
return
}
main, err = readAsString("/proc/" + fmt.Sprint(pid) + "/status")
if err != nil {
return
}
data := splitBy(main, ":")
datas.Name = strings.TrimSpace(name)
datas.Pid, _ = strconv.ParseInt(data["Pid"], 10, 64)

Loading…
Cancel
Save