1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-26 13:48:59 +08:00

[v2][process][linux] port 1112 to v2.

This port includes only code. test is not included.
This commit is contained in:
shirou 2021-08-14 08:22:57 +00:00
parent efc3e519be
commit b27be3812b

View File

@ -82,7 +82,7 @@ func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
if p.name == "" {
if err := p.fillFromStatusWithContext(ctx); err != nil {
if err := p.fillNameWithContext(ctx); err != nil {
return "", err
}
}
@ -517,6 +517,28 @@ func limitToInt(val string) (int32, error) {
}
}
// Get name from /proc/(pid)/comm or /proc/(pid)/status
func (p *Process) fillNameWithContext(ctx context.Context) error {
err := p.fillFromCommWithContext(ctx)
if err == nil && p.name != "" && len(p.name) < 15 {
return nil
}
return p.fillFromStatusWithContext(ctx)
}
// Get name from /proc/(pid)/comm
func (p *Process) fillFromCommWithContext(ctx context.Context) error {
pid := p.Pid
statPath := common.HostProc(strconv.Itoa(int(pid)), "comm")
contents, err := ioutil.ReadFile(statPath)
if err != nil {
return err
}
p.name = strings.TrimSuffix(string(contents), "\n")
return nil
}
// Get num_fds from /proc/(pid)/limits
func (p *Process) fillFromLimitsWithContext(ctx context.Context) ([]RlimitStat, error) {
pid := p.Pid