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,26 +37,19 @@ 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 {
return if compare(tmp) {
datas = append(datas, tmp)
continue
}
} }
data := splitBy(main, ":") data := splitBy(main, ":")
tmp.Pid, _ = strconv.ParseInt(data["Pid"], 10, 64) 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 tmp.VmRSS *= 1024
mainb, err = ioutil.ReadFile("/proc/" + v.Name() + "/cmdline") mainb, err = ioutil.ReadFile("/proc/" + v.Name() + "/cmdline")
if err != nil { if err != nil {
return if compare(tmp) {
datas = append(datas, tmp)
continue
}
} }
args := bytes.Split(mainb, []byte{0}) args := bytes.Split(mainb, []byte{0})
for _, v := range args { 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") mainb, err = ioutil.ReadFile("/proc/" + v.Name() + "/environ")
if err != nil { if err != nil {
return if compare(tmp) {
datas = append(datas, tmp)
continue
}
} }
args = bytes.Split(mainb, []byte{0}) args = bytes.Split(mainb, []byte{0})
for _, v := range args { 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") 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) times := splitBySpace(main)
uptime, _ := strconv.ParseInt(strings.TrimSpace(times[21]), 10, 64) 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)) 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