aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/containers/psgo/ps/ps.go
blob: f8d8d03b5b55004afc90ea379ea1ec5bcdc11102 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
// Package ps is a ps (1) AIX-format compatible golang library extended with
// various descriptors useful for displaying container-related data.
//
// The idea behind the library is to provide an easy to use way of extracting
// process-related data, just as ps (1) does. The problem when using ps (1) is
// that the ps format strings split columns with whitespaces, making the output
// nearly impossible to parse. It also adds some jitter as we have to fork and
// execute ps either in the container or filter the output afterwards, further
// limiting applicability.
//
// Please visit https://github.com/containers/psgo for further details about
// supported format descriptors and to see some usage examples.
package ps

import (
	"fmt"
	"io/ioutil"
	"os"
	"runtime"
	"sort"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/opencontainers/runc/libcontainer/user"
	"github.com/pkg/errors"
	"golang.org/x/sys/unix"
)

// DefaultFormat is the `ps -ef` compatible default format.
const DefaultFormat = "user,pid,ppid,pcpu,etime,tty,time,comm"

var (
	// ErrUnkownDescriptor is returned when an unknown descriptor is parsed.
	ErrUnkownDescriptor = errors.New("unknown descriptor")

	// errNoSuchPID is returned when `/proc/PID` does not exist (anymore).
	errNoSuchPID = errors.New("PID does not exist in /proc")

	// bootTime holds the host's boot time. Singleton to safe some time and
	// energy.
	bootTime int64

	// clockTicks is the value of sysconf(SC_CLK_TCK)
	clockTicks = getClockTicks()

	// ttyDevices is a slice of ttys. Singledton to safe some time and
	// energy.
	ttyDevices []*tty

	descriptors = []aixFormatDescriptor{
		{
			code:   "%C",
			normal: "pcpu",
			header: "%CPU",
			procFn: processPCPU,
		},
		{
			code:   "%G",
			normal: "group",
			header: "GROUP",
			procFn: processGROUP,
		},
		{
			code:   "%P",
			normal: "ppid",
			header: "PPID",
			procFn: processPPID,
		},
		{
			code:   "%U",
			normal: "user",
			header: "USER",
			procFn: processUSER,
		},
		{
			code:   "%a",
			normal: "args",
			header: "COMMAND",
			procFn: processARGS,
		},
		{
			code:   "%c",
			normal: "comm",
			header: "COMMAND",
			procFn: processCOMM,
		},
		{
			code:   "%g",
			normal: "rgroup",
			header: "RGROUP",
			procFn: processRGROUP,
		},
		{
			code:   "%n",
			normal: "nice",
			header: "NI",
			procFn: processNICE,
		},
		{
			code:   "%p",
			normal: "pid",
			header: "PID",
			procFn: processPID,
		},
		{
			code:   "%r",
			normal: "pgid",
			header: "PGID",
			procFn: processPGID,
		},
		{
			code:   "%t",
			normal: "etime",
			header: "ELAPSED",
			procFn: processETIME,
		},
		{
			code:   "%u",
			normal: "ruser",
			header: "RUSER",
			procFn: processRUSER,
		},
		{
			code:   "%x",
			normal: "time",
			header: "TIME",
			procFn: processTIME,
		},
		{
			code:   "%y",
			normal: "tty",
			header: "TTY",
			procFn: processTTY,
		},
		{
			code:   "%z",
			normal: "vsz",
			header: "VSZ",
			procFn: processVSZ,
		},
		{
			normal: "capinh",
			header: "CAPABILITIES",
			procFn: processCAPINH,
		},
		{
			normal: "capprm",
			header: "CAPABILITIES",
			procFn: processCAPPRM,
		},
		{
			normal: "capeff",
			header: "CAPABILITIES",
			procFn: processCAPEFF,
		},
		{
			normal: "capbnd",
			header: "CAPABILITIES",
			procFn: processCAPBND,
		},
		{
			normal: "seccomp",
			header: "SECCOMP",
			procFn: processSECCOMP,
		},
		{
			normal: "label",
			header: "LABEL",
			procFn: processLABEL,
		},
	}
)

// process includes a process ID and the corresponding data from /proc/pid/stat,
// /proc/pid/status and from /prod/pid/cmdline.
type process struct {
	pid     int
	pstat   *stat
	pstatus *status
	cmdline []string
}

// elapsedTime returns the time.Duration since process p was created.
func (p *process) elapsedTime() (time.Duration, error) {
	sinceBoot, err := strconv.ParseInt(p.pstat.starttime, 10, 64)
	if err != nil {
		return 0, err
	}
	sinceBoot = sinceBoot / clockTicks

	if bootTime == 0 {
		bootTime, err = getBootTime()
		if err != nil {
			return 0, err
		}
	}
	created := time.Unix(sinceBoot+bootTime, 0)
	return (time.Now()).Sub(created), nil
}

// cpuTime returns the cumlative CPU time of process p as a time.Duration.
func (p *process) cpuTime() (time.Duration, error) {
	user, err := strconv.ParseInt(p.pstat.utime, 10, 64)
	if err != nil {
		return 0, err
	}
	system, err := strconv.ParseInt(p.pstat.stime, 10, 64)
	if err != nil {
		return 0, err
	}
	secs := (user + system) / clockTicks
	cpu := time.Unix(secs, 0)
	return cpu.Sub(time.Unix(0, 0)), nil
}

// processes returns a process slice of processes mentioned in /proc.
func processes() ([]*process, error) {
	pids, err := getPIDs()
	if err != nil {
		panic(err)
	}

	processes := []*process{}
	for _, pid := range pids {
		var (
			err error
			p   process
		)
		p.pid = pid
		p.pstat, err = parseStat(fmt.Sprintf("/proc/%d/stat", pid))
		if err != nil {
			if err == errNoSuchPID {
				continue
			}
			return nil, err
		}
		p.pstatus, err = parseStatus(fmt.Sprintf("/proc/%d/status", pid))
		if err != nil {
			if err == errNoSuchPID {
				continue
			}
			return nil, err
		}
		p.cmdline, err = parseCmdline(fmt.Sprintf("/proc/%d/cmdline", pid))
		if err != nil {
			if err == errNoSuchPID {
				continue
			}
			return nil, err
		}
		processes = append(processes, &p)
	}

	return processes, nil
}

// getPIDs extracts and returns all PIDs from /proc.
func getPIDs() ([]int, error) {
	procDir, err := os.Open("/proc/")
	if err != nil {
		return nil, err
	}
	defer procDir.Close()

	// extract string slice of all directories in procDir
	pidDirs, err := procDir.Readdirnames(0)
	if err != nil {
		return nil, err
	}

	// convert pidDirs to int
	pids := []int{}
	for _, pidDir := range pidDirs {
		pid, err := strconv.Atoi(pidDir)
		if err != nil {
			// skip non-numerical entries (e.g., `/proc/softirqs`)
			continue
		}
		pids = append(pids, pid)
	}

	return pids, nil
}

// processFunc is used to map a given aixFormatDescriptor to a corresponding
// function extracting the desired data from a process.
type processFunc func(*process) (string, error)

// aixFormatDescriptor as mentioned in the ps(1) manpage.  A given descriptor
// can either be specified via its code (e.g., "%C") or its normal representation
// (e.g., "pcpu") and will be printed under its corresponding header (e.g, "%CPU").
type aixFormatDescriptor struct {
	code   string
	normal string
	header string
	procFn processFunc
}

// processDescriptors calls each `procFn` of all formatDescriptors on each
// process and returns an array of tab-separated strings.
func processDescriptors(formatDescriptors []aixFormatDescriptor, processes []*process) ([]string, error) {
	data := []string{}
	// create header
	headerArr := []string{}
	for _, desc := range formatDescriptors {
		headerArr = append(headerArr, desc.header)
	}
	data = append(data, strings.Join(headerArr, "\t"))

	// dispatch all descriptor functions on each process
	for _, proc := range processes {
		pData := []string{}
		for _, desc := range formatDescriptors {
			dataStr, err := desc.procFn(proc)
			if err != nil {
				return nil, err
			}
			pData = append(pData, dataStr)
		}
		data = append(data, strings.Join(pData, "\t"))
	}

	return data, nil
}

// ListDescriptors returns a string slice of all supported AIX format
// descriptors in the normal form.
func ListDescriptors() (list []string) {
	for _, d := range descriptors {
		list = append(list, d.normal)
	}
	sort.Strings(list)
	return
}

// JoinNamespaceAndProcessInfo has the same semantics as ProcessInfo but joins
// the mount namespace of the specified pid before extracting data from `/proc`.
func JoinNamespaceAndProcessInfo(pid, format string) ([]string, error) {
	var (
		data    []string
		dataErr error
		wg      sync.WaitGroup
	)

	wg.Add(1)
	go func() {
		defer wg.Done()
		runtime.LockOSThread()

		fd, err := os.Open(fmt.Sprintf("/proc/%s/ns/mnt", pid))
		if err != nil {
			dataErr = err
			return
		}
		defer fd.Close()

		// create a new mountns on the current thread
		if err = unix.Unshare(unix.CLONE_NEWNS); err != nil {
			dataErr = err
			return
		}
		unix.Setns(int(fd.Fd()), unix.CLONE_NEWNS)
		data, dataErr = ProcessInfo(format)
	}()
	wg.Wait()

	return data, dataErr
}

// ProcessInfo returns the process information of all processes in the current
// mount namespace. The input format must be a comma-separated list of
// supported AIX format descriptors.  If the input string is empty, the
// DefaultFormat is used.
// The return value is an array of tab-separated strings, to easily use the
// output for column-based formatting (e.g., with the `text/tabwriter` package).
func ProcessInfo(format string) ([]string, error) {
	if len(format) == 0 {
		format = DefaultFormat
	}

	formatDescriptors, err := parseDescriptors(format)
	if err != nil {
		return nil, err
	}

	processes, err := processes()
	if err != nil {
		return nil, err
	}

	return processDescriptors(formatDescriptors, processes)
}

// parseDescriptors parses the input string and returns a correspodning array
// of aixFormatDescriptors, which are expected to be separated by commas.
// The input format is "desc1, desc2, ..., desN" where a given descriptor can be
// specified both, in the code and in the normal form.  A concrete example is
// "pid, %C, nice, %a".
func parseDescriptors(input string) ([]aixFormatDescriptor, error) {
	formatDescriptors := []aixFormatDescriptor{}
	for _, s := range strings.Split(input, ",") {
		s = strings.TrimSpace(s)
		found := false
		for _, d := range descriptors {
			if s == d.code || s == d.normal {
				formatDescriptors = append(formatDescriptors, d)
				found = true
			}
		}
		if !found {
			return nil, errors.Wrapf(ErrUnkownDescriptor, "'%s'", s)
		}
	}
	return formatDescriptors, nil
}

// lookupGID returns the textual group ID, if it can be optained, or the
// decimal input representation otherwise.
func lookupGID(gid string) (string, error) {
	gidNum, err := strconv.Atoi(gid)
	if err != nil {
		return "", errors.Wrap(err, "error parsing group ID")
	}
	g, err := user.LookupGid(gidNum)
	if err != nil {
		return gid, nil
	}
	return g.Name, nil
}

// processGROUP returns the effective group ID of the process.  This will be
// the textual group ID, if it can be optained, or a decimal representation
// otherwise.
func processGROUP(p *process) (string, error) {
	return lookupGID(p.pstatus.gids[1])
}

// processRGROUP returns the real group ID of the process.  This will be
// the textual group ID, if it can be optained, or a decimal representation
// otherwise.
func processRGROUP(p *process) (string, error) {
	return lookupGID(p.pstatus.gids[0])
}

// processPPID returns the parent process ID of process p.
func processPPID(p *process) (string, error) {
	return p.pstatus.pPid, nil
}

// lookupUID return the textual user ID, if it can be optained, or the decimal
// input representation otherwise.
func lookupUID(uid string) (string, error) {
	uidNum, err := strconv.Atoi(uid)
	if err != nil {
		return "", errors.Wrap(err, "error parsing user ID")
	}
	u, err := user.LookupUid(uidNum)
	if err != nil {
		return uid, nil
	}
	return u.Name, nil

}

// processUSER returns the effective user name of the process.  This will be
// the textual group ID, if it can be optained, or a decimal representation
// otherwise.
func processUSER(p *process) (string, error) {
	return lookupUID(p.pstatus.uids[1])
}

// processRUSER returns the effective user name of the process.  This will be
// the textual group ID, if it can be optained, or a decimal representation
// otherwise.
func processRUSER(p *process) (string, error) {
	return lookupUID(p.pstatus.uids[0])
}

// processName returns the name of process p in the format "[$name]".
func processName(p *process) (string, error) {
	return fmt.Sprintf("[%s]", p.pstatus.name), nil
}

// processARGS returns the command of p with all its arguments.
func processARGS(p *process) (string, error) {
	args := p.cmdline
	// ps (1) returns "[$name]" if command/args are empty
	if len(args) == 0 {
		return processName(p)
	}
	return strings.Join(args, " "), nil
}

// processCOMM returns the command name (i.e., executable name) of process p.
func processCOMM(p *process) (string, error) {
	args := p.cmdline
	// ps (1) returns "[$name]" if command/args are empty
	if len(args) == 0 {
		return processName(p)
	}
	spl := strings.Split(args[0], "/")
	return spl[len(spl)-1], nil
}

// processNICE returns the nice value of process p.
func processNICE(p *process) (string, error) {
	return p.pstat.nice, nil
}

// processPID returns the process ID of process p.
func processPID(p *process) (string, error) {
	return p.pstatus.pid, nil
}

// processPGID returns the process group ID of process p.
func processPGID(p *process) (string, error) {
	return p.pstat.pgrp, nil
}

// processPCPU returns how many percent of the CPU time process p uses as
// a three digit float as string.
func processPCPU(p *process) (string, error) {
	elapsed, err := p.elapsedTime()
	if err != nil {
		return "", err
	}
	cpu, err := p.cpuTime()
	if err != nil {
		return "", err
	}
	pcpu := 100 * cpu.Seconds() / elapsed.Seconds()

	return strconv.FormatFloat(pcpu, 'f', 3, 64), nil
}

// processETIME returns the elapsed time since the process was started.
func processETIME(p *process) (string, error) {
	elapsed, err := p.elapsedTime()
	if err != nil {
		return "", nil
	}
	return fmt.Sprintf("%v", elapsed), nil
}

// processTIME returns the cumulative CPU time of process p.
func processTIME(p *process) (string, error) {
	cpu, err := p.cpuTime()
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("%v", cpu), nil
}

// processTTY returns the controlling tty (terminal) of process p.
func processTTY(p *process) (string, error) {
	ttyNr, err := strconv.ParseUint(p.pstat.ttyNr, 10, 64)
	if err != nil {
		return "", nil
	}

	maj, min := ttyNrToDev(ttyNr)
	t, err := findTTY(maj, min)
	if err != nil {
		return "", err
	}

	ttyS := "?"
	if t != nil {
		ttyS = strings.TrimPrefix(t.device, "/dev/")
	}
	return ttyS, nil
}

// processVSZ returns the virtual memory size of process p in KiB (1024-byte
// units).
func processVSZ(p *process) (string, error) {
	vmsize, err := strconv.Atoi(p.pstat.vsize)
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("%d", vmsize/1024), nil
}

// parseCAP parses cap (a string bit mask) and returns the associated set of
// capabilities.  If all capabilties are set, "full" is returned.  If no
// capability is enabled, "none" is returned.
func parseCAP(cap string) (string, error) {
	mask, err := strconv.ParseUint(cap, 16, 64)
	if err != nil {
		return "", err
	}
	if mask == fullCAPs {
		return "full", nil
	}
	caps := maskToCaps(mask)
	if len(caps) == 0 {
		return "none", nil
	}
	sort.Strings(caps)
	return strings.Join(caps, ","), nil
}

// processCAPINH returns the set of inheritable capabilties associated with
// process p.  If all capabilties are set, "full" is returned.  If no
// capability is enabled, "none" is returned.
func processCAPINH(p *process) (string, error) {
	return parseCAP(p.pstatus.capInh)
}

// processCAPPRM returns the set of permitted capabilties associated with
// process p.  If all capabilties are set, "full" is returned.  If no
// capability is enabled, "none" is returned.
func processCAPPRM(p *process) (string, error) {
	return parseCAP(p.pstatus.capPrm)
}

// processCAPEFF returns the set of effective capabilties associated with
// process p.  If all capabilties are set, "full" is returned.  If no
// capability is enabled, "none" is returned.
func processCAPEFF(p *process) (string, error) {
	return parseCAP(p.pstatus.capEff)
}

// processCAPBND returns the set of bounding capabilties associated with
// process p.  If all capabilties are set, "full" is returned.  If no
// capability is enabled, "none" is returned.
func processCAPBND(p *process) (string, error) {
	return parseCAP(p.pstatus.capBnd)
}

// processSECCOMP returns the seccomp mode of the process (i.e., disabled,
// strict or filter) or "?" if /proc/$pid/status.seccomp has a unknown value.
func processSECCOMP(p *process) (string, error) {
	switch p.pstatus.seccomp {
	case "0":
		return "disabled", nil
	case "1":
		return "strict", nil
	case "2":
		return "filter", nil
	default:
		return "?", nil
	}
}

// processLABEL returns the process label of process p.
func processLABEL(p *process) (string, error) {
	data, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/attr/current", p.pid))
	if err != nil {
		if os.IsNotExist(err) {
			// make sure the pid does not exist,
			// could be system does not support labeling.
			if _, err2 := os.Stat(fmt.Sprintf("/proc/%d", p.pid)); err2 != nil {
				return "", errNoSuchPID
			}
		}
		return "", err
	}
	return strings.Trim(string(data), "\x00"), nil
}