Debian Codegraph @

internal/process/process.go

raw symbols SCIP
1 // Copyright 2018 psgo authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package process
16
17 import (
18 "errors"
19 "fmt"
20 "os"
21 "strconv"
22 "time"
23
24 "github.com/containers/psgo/internal/host"
25 "github.com/containers/psgo/internal/proc"
26 "github.com/moby/sys/user"
27 "golang.org/x/sys/unix"
28 )
29
30 // Process includes process-related from the /proc FS.
31 type Process struct {
32 // PID is the process ID.
33 Pid string
34 // Stat contains data from /proc/$pid/stat.
35 Stat proc.Stat
36 // Status contains data from /proc/$pid/status.
37 Status proc.Status
38 // CmdLine contains data from /proc/$pid/cmdline.
39 CmdLine []string
40 // Label containers data from /proc/$pid/attr/current.
41 Label string
42 // PidNS contains data from /proc/$pid/ns/pid.
43 PidNS string
44 // Huser is the effective host user of a container process.
45 Huser string
46 // Hgroup is the effective host group of a container process.
47 Hgroup string
48 }
49
50 // LookupGID returns the textual group ID, if it can be obtained, or the
51 // decimal representation otherwise.
52 func LookupGID(gid string) (string, error) {
53 gidNum, err := strconv.Atoi(gid)
54 if err != nil {
55 return "", fmt.Errorf("error parsing group ID: %w", err)
56 }
57 g, err := user.LookupGid(gidNum)
58 if err != nil {
59 return gid, nil
60 }
61 return g.Name, nil
62 }
63
64 // LookupUID return the textual user ID, if it can be obtained, or the decimal
65 // representation otherwise.
66 func LookupUID(uid string) (string, error) {
67 uidNum, err := strconv.Atoi(uid)
68 if err != nil {
69 return "", fmt.Errorf("error parsing user ID: %w", err)
70 }
71 u, err := user.LookupUid(uidNum)
72 if err != nil {
73 return uid, nil
74 }
75 return u.Name, nil
76 }
77
78 // New returns a new Process with the specified pid and parses the relevant
79 // data from /proc and /dev.
80 func New(pid string, joinUserNS bool) (*Process, error) {
81 p := Process{Pid: pid}
82
83 if err := p.parseStat(); err != nil {
84 return nil, err
85 }
86 if err := p.parseStatus(joinUserNS); err != nil {
87 return nil, err
88 }
89 if err := p.parseCmdLine(); err != nil {
90 return nil, err
91 }
92 if err := p.parsePIDNamespace(); err != nil {
93 // Ignore permission errors as those occur for some pids when
94 // the caller has limited permissions.
95 if !os.IsPermission(err) {
96 return nil, err
97 }
98 }
99 if err := p.parseLabel(); err != nil {
100 return nil, err
101 }
102
103 return &p, nil
104 }
105
106 // FromPIDs creates a new Process for each pid.
107 func FromPIDs(pids []string, joinUserNS bool) ([]*Process, error) {
108 processes := []*Process{}
109 for _, pid := range pids {
110 p, err := New(pid, joinUserNS)
111 if err != nil {
112 if errors.Is(err, os.ErrNotExist) || errors.Is(err, unix.ESRCH) {
113 // proc parsing is racy
114 // Let's ignore "does not exist" errors
115 continue
116 }
117 return nil, err
118 }
119 processes = append(processes, p)
120 }
121 return processes, nil
122 }
123
124 // parseStat parses /proc/$pid/stat.
125 func (p *Process) parseStat() error {
126 s, err := proc.ParseStat(p.Pid)
127 if err != nil {
128 return err
129 }
130 p.Stat = *s
131 return nil
132 }
133
134 // parseStatus parses /proc/$pid/status.
135 func (p *Process) parseStatus(joinUserNS bool) error {
136 s, err := proc.ParseStatus(p.Pid, joinUserNS)
137 if err != nil {
138 return err
139 }
140 p.Status = *s
141 return nil
142 }
143
144 // parseCmdLine parses /proc/$pid/cmdline.
145 func (p *Process) parseCmdLine() error {
146 s, err := proc.ParseCmdLine(p.Pid)
147 if err != nil {
148 return err
149 }
150 p.CmdLine = s
151 return nil
152 }
153
154 // parsePIDNamespace sets the PID namespace.
155 func (p *Process) parsePIDNamespace() error {
156 pidNS, err := proc.ParsePIDNamespace(p.Pid)
157 if err != nil {
158 return err
159 }
160 p.PidNS = pidNS
161 return nil
162 }
163
164 // parseLabel parses the security label.
165 func (p *Process) parseLabel() error {
166 label, err := proc.ParseAttrCurrent(p.Pid)
167 if err != nil {
168 return err
169 }
170 p.Label = label
171 return nil
172 }
173
174 // SetHostData sets all host-related data fields.
175 func (p *Process) SetHostData() error {
176 var err error
177
178 p.Huser, err = LookupUID(p.Status.Uids[1])
179 if err != nil {
180 return err
181 }
182
183 p.Hgroup, err = LookupGID(p.Status.Gids[1])
184 if err != nil {
185 return err
186 }
187
188 return nil
189 }
190
191 // ElapsedTime returns the time.Duration since process p was created.
192 func (p *Process) ElapsedTime() (time.Duration, error) {
193 startTime, err := p.StartTime()
194 if err != nil {
195 return 0, err
196 }
197 return time.Since(startTime), nil
198 }
199
200 // StarTime returns the time.Time when process p was started.
201 func (p *Process) StartTime() (time.Time, error) {
202 sinceBoot, err := strconv.ParseInt(p.Stat.Starttime, 10, 64)
203 if err != nil {
204 return time.Time{}, err
205 }
206 clockTicks, err := host.ClockTicks()
207 if err != nil {
208 return time.Time{}, err
209 }
210 bootTime, err := host.BootTime()
211 if err != nil {
212 return time.Time{}, err
213 }
214
215 sinceBoot = sinceBoot / clockTicks
216 return time.Unix(sinceBoot+bootTime, 0), nil
217 }
218
219 // CPUTime returns the cumulative CPU time of process p as a time.Duration.
220 func (p *Process) CPUTime() (time.Duration, error) {
221 user, err := strconv.ParseInt(p.Stat.Utime, 10, 64)
222 if err != nil {
223 return 0, err
224 }
225 system, err := strconv.ParseInt(p.Stat.Stime, 10, 64)
226 if err != nil {
227 return 0, err
228 }
229 clockTicks, err := host.ClockTicks()
230 if err != nil {
231 return 0, err
232 }
233 secs := (user + system) / clockTicks
234 cpu := time.Unix(secs, 0)
235 return cpu.Sub(time.Unix(0, 0)), nil
236 }