|
|
@ -17,7 +17,17 @@ import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
//FindProcessByName 通过进程名来查询应用信息
|
|
|
|
//FindProcessByName 通过进程名来查询应用信息
|
|
|
|
func FindProcessByName(pname string) (datas []Process, err error) {
|
|
|
|
func FindProcessByName(name string) (datas []Process, err error) {
|
|
|
|
|
|
|
|
return FindProcess(func(pname, exepath, folderpath string) bool {
|
|
|
|
|
|
|
|
if name == pname {
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// FindProcess 通过进程信息来查询应用信息
|
|
|
|
|
|
|
|
func FindProcess(compare func(string, string, string) 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")
|
|
|
@ -31,14 +41,24 @@ func FindProcessByName(pname string) (datas []Process, err error) {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.TrimSpace(name) == pname {
|
|
|
|
|
|
|
|
var tmp Process
|
|
|
|
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")
|
|
|
|
main, err = readAsString("/proc/" + v.Name() + "/status")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data := splitBy(main, ":")
|
|
|
|
data := splitBy(main, ":")
|
|
|
|
tmp.Name = pname
|
|
|
|
|
|
|
|
tmp.Pid, _ = strconv.ParseInt(data["Pid"], 10, 64)
|
|
|
|
tmp.Pid, _ = strconv.ParseInt(data["Pid"], 10, 64)
|
|
|
|
tmp.PPid, _ = strconv.ParseInt(data["PPid"], 10, 64)
|
|
|
|
tmp.PPid, _ = strconv.ParseInt(data["PPid"], 10, 64)
|
|
|
|
tmp.TPid, _ = strconv.ParseInt(data["TracerPid"], 10, 64)
|
|
|
|
tmp.TPid, _ = strconv.ParseInt(data["TracerPid"], 10, 64)
|
|
|
@ -48,6 +68,18 @@ func FindProcessByName(pname string) (datas []Process, err error) {
|
|
|
|
tmp.EUID, _ = strconv.Atoi(uids[1])
|
|
|
|
tmp.EUID, _ = strconv.Atoi(uids[1])
|
|
|
|
tmp.RGID, _ = strconv.Atoi(gids[0])
|
|
|
|
tmp.RGID, _ = strconv.Atoi(gids[0])
|
|
|
|
tmp.EGID, _ = strconv.Atoi(gids[1])
|
|
|
|
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")
|
|
|
|
mainb, err = ioutil.ReadFile("/proc/" + v.Name() + "/cmdline")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
return
|
|
|
@ -56,16 +88,16 @@ func FindProcessByName(pname string) (datas []Process, err error) {
|
|
|
|
for _, v := range args {
|
|
|
|
for _, v := range args {
|
|
|
|
tmp.Args = append(tmp.Args, string(v))
|
|
|
|
tmp.Args = append(tmp.Args, string(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp.LocalPath, err = os.Readlink("/proc/" + v.Name() + "/exe")
|
|
|
|
|
|
|
|
|
|
|
|
mainb, err = ioutil.ReadFile("/proc/" + v.Name() + "/environ")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp.Path = tmp.LocalPath
|
|
|
|
args = bytes.Split(mainb, []byte{0})
|
|
|
|
tmp.LocalPath = filepath.Dir(tmp.LocalPath)
|
|
|
|
for _, v := range args {
|
|
|
|
tmp.ExecPath, err = os.Readlink("/proc/" + v.Name() + "/cwd")
|
|
|
|
tmp.Env = append(tmp.Env, string(v))
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
main, err = readAsString("/proc/" + v.Name() + "/stat")
|
|
|
|
main, err = readAsString("/proc/" + v.Name() + "/stat")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
return
|
|
|
@ -107,6 +139,18 @@ func FindProcessByPid(pid int64) (datas Process, err error) {
|
|
|
|
datas.EUID, _ = strconv.Atoi(uids[1])
|
|
|
|
datas.EUID, _ = strconv.Atoi(uids[1])
|
|
|
|
datas.RGID, _ = strconv.Atoi(gids[0])
|
|
|
|
datas.RGID, _ = strconv.Atoi(gids[0])
|
|
|
|
datas.EGID, _ = strconv.Atoi(gids[1])
|
|
|
|
datas.EGID, _ = strconv.Atoi(gids[1])
|
|
|
|
|
|
|
|
datas.VmPeak, _ = strconv.ParseInt(splitBySpace(data["VmPeak"])[0], 10, 64)
|
|
|
|
|
|
|
|
datas.VmSize, _ = strconv.ParseInt(splitBySpace(data["VmSize"])[0], 10, 64)
|
|
|
|
|
|
|
|
datas.VmHWM, _ = strconv.ParseInt(splitBySpace(data["VmHWM"])[0], 10, 64)
|
|
|
|
|
|
|
|
datas.VmRSS, _ = strconv.ParseInt(splitBySpace(data["VmRSS"])[0], 10, 64)
|
|
|
|
|
|
|
|
datas.VmLck, _ = strconv.ParseInt(splitBySpace(data["VmLck"])[0], 10, 64)
|
|
|
|
|
|
|
|
datas.VmData, _ = strconv.ParseInt(splitBySpace(data["VmData"])[0], 10, 64)
|
|
|
|
|
|
|
|
datas.VmLck *= 1024
|
|
|
|
|
|
|
|
datas.VmData *= 1024
|
|
|
|
|
|
|
|
datas.VmPeak *= 1024
|
|
|
|
|
|
|
|
datas.VmSize *= 1024
|
|
|
|
|
|
|
|
datas.VmHWM *= 1024
|
|
|
|
|
|
|
|
datas.VmRSS *= 1024
|
|
|
|
mainb, err = ioutil.ReadFile("/proc/" + fmt.Sprint(pid) + "/cmdline")
|
|
|
|
mainb, err = ioutil.ReadFile("/proc/" + fmt.Sprint(pid) + "/cmdline")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
return
|
|
|
@ -115,6 +159,16 @@ func FindProcessByPid(pid int64) (datas Process, err error) {
|
|
|
|
for _, v := range args {
|
|
|
|
for _, v := range args {
|
|
|
|
datas.Args = append(datas.Args, string(v))
|
|
|
|
datas.Args = append(datas.Args, string(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mainb, err = ioutil.ReadFile("/proc/" + fmt.Sprint(pid) + "/environ")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
args = bytes.Split(mainb, []byte{0})
|
|
|
|
|
|
|
|
for _, v := range args {
|
|
|
|
|
|
|
|
datas.Env = append(datas.Env, string(v))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
datas.LocalPath, err = os.Readlink("/proc/" + fmt.Sprint(pid) + "/exe")
|
|
|
|
datas.LocalPath, err = os.Readlink("/proc/" + fmt.Sprint(pid) + "/exe")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
return
|