GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/ps/print.c Lines: 263 307 85.7 %
Date: 2016-12-06 Branches: 137 208 65.9 %

Line Branch Exec Source
1
/*	$OpenBSD: print.c,v 1.67 2016/04/25 19:12:07 tedu Exp $	*/
2
/*	$NetBSD: print.c,v 1.27 1995/09/29 21:58:12 cgd Exp $	*/
3
4
/*-
5
 * Copyright (c) 1990, 1993, 1994
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. Neither the name of the University nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32
33
#include <sys/param.h>	/* MAXCOMLEN PZERO NODEV */
34
#include <sys/types.h>
35
#include <sys/proc.h>
36
#include <sys/stat.h>
37
38
#include <sys/sysctl.h>
39
40
#include <err.h>
41
#include <grp.h>
42
#include <kvm.h>
43
#include <math.h>
44
#include <nlist.h>
45
#include <stddef.h>
46
#include <stdio.h>
47
#include <stdlib.h>
48
#include <string.h>
49
#include <unistd.h>
50
#include <limits.h>
51
#include <pwd.h>
52
53
#include "ps.h"
54
55
extern kvm_t *kd;
56
extern int needenv, needcomm, neednlist, commandonly;
57
58
int mbswprint(const char *, int, int);  /* utf8.c */
59
60
static char *cmdpart(char *);
61
62
#define	min(a,b)	((a) < (b) ? (a) : (b))
63
64
static char *
65
cmdpart(char *arg0)
66
32
{
67
	char *cp;
68
69
32
	return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
70
}
71
72
void
73
printheader(void)
74
117
{
75
	VAR *v;
76
	struct varent *vent;
77
78
117
	if (!needheader)
79
		return;
80
491
	for (vent = vhead; vent; vent = vent->next) {
81
374
		v = vent->var;
82
374
		if (v->flag & LJUST) {
83
181
			if (vent->next == NULL)	/* last one */
84
51
				(void)printf("%s", v->header);
85
			else
86
130
				(void)printf("%-*s", v->width, v->header);
87
		} else
88
193
			(void)printf("%*s", v->width, v->header);
89
374
		if (vent->next != NULL)
90
257
			(void)putchar(' ');
91
	}
92
117
	(void)putchar('\n');
93
}
94
95
void
96
command(const struct kinfo_proc *kp, VARENT *ve)
97
133
{
98
	VAR *v;
99
133
	int left, wantspace = 0;
100
	char **argv, **p;
101
102
	/*
103
	 * Determine the available number of display columns.
104
	 * Always decrement and check after writing.
105
	 * No check is needed before mbswprint()
106
	 * and after writing the last data, though.
107
	 */
108
109
133
	v = ve->var;
110

133
	if (ve->next != NULL || termwidth != UNLIMITED) {
111
129
		if (ve->next == NULL) {
112
29
			left = termwidth - (totwidth - v->width);
113
29
			if (left < 1) /* already wrapped, just use std width */
114
7
				left = v->width;
115
		} else
116
100
			left = v->width;
117
	} else
118
4
		left = INT_MAX;
119
120

133
	if (needenv && kd != NULL) {
121
46
		argv = kvm_getenvv(kd, kp, termwidth);
122
46
		if ((p = argv) != NULL) {
123
91
			while (*p) {
124
54
				if (wantspace) {
125
8
					putchar(' ');
126
8
					left--;
127
				}
128
54
				left -= mbswprint(*p, left, 0);
129
54
				if (left == 0)
130
9
					return;
131
45
				p++;
132
45
				wantspace = 1;
133
			}
134
		}
135
	} else
136
87
		argv = NULL;
137
138
124
	if (needcomm) {
139
124
		if (!commandonly) {
140
98
			if (kd != NULL) {
141
98
				argv = kvm_getargv(kd, kp, termwidth);
142
98
				if ((p = argv) != NULL) {
143
146
					while (*p) {
144
114
						if (wantspace) {
145
44
							putchar(' ');
146
44
							left--;
147
						}
148
114
						left -= mbswprint(*p, left, 0);
149
114
						if (left == 0)
150
38
							return;
151
76
						p++;
152
76
						wantspace = 1;
153
					}
154
				}
155
			}
156

60
			if (argv == NULL || argv[0] == '\0' ||
157
			    strcmp(cmdpart(argv[0]), kp->p_comm)) {
158
32
				if (wantspace) {
159
32
					putchar(' ');
160
32
					if (--left == 0)
161
1
						return;
162
				}
163
31
				putchar('(');
164
31
				left--;
165
31
				left -= mbswprint(kp->p_comm, left, 0);
166
31
				if (left == 0)
167
22
					return;
168
9
				putchar(')');
169
9
				left--;
170
			}
171
		} else {
172
26
			if (wantspace) {
173
9
				putchar(' ');
174
9
				left--;
175
			}
176
26
			left -= mbswprint(kp->p_comm, left, 0);
177
		}
178
	}
179
63
	if (ve->next != NULL)
180
172
		while (left-- > 0)
181
132
			putchar(' ');
182
}
183
184
void
185
ucomm(const struct kinfo_proc *kp, VARENT *ve)
186
32
{
187
32
	mbswprint(kp->p_comm, ve->var->width, ve->next != NULL);
188
32
}
189
190
void
191
curwd(const struct kinfo_proc *kp, VARENT *ve)
192
52
{
193
52
	int name[] = { CTL_KERN, KERN_PROC_CWD, kp->p_pid };
194
	char path[PATH_MAX];
195
52
	size_t pathlen = sizeof path;
196
197

52
	if (!kvm_sysctl_only || sysctl(name, 3, path, &pathlen, NULL, 0) != 0)
198
		*path = '\0';
199
200
52
	mbswprint(path, ve->var->width, ve->next != NULL);
201
52
}
202
203
void
204
logname(const struct kinfo_proc *kp, VARENT *ve)
205
38
{
206
	VAR *v;
207
208
38
	v = ve->var;
209
38
	if (kp->p_login[0]) {
210
38
		int n = min(v->width, LOGIN_NAME_MAX);
211
38
		mbswprint(kp->p_login, n, ve->next != NULL);
212
38
		if (ve->next != NULL)
213
36
			while (n++ < v->width)
214
				putchar(' ');
215
	} else
216
		(void)printf("%-*s", v->width, "-");
217
38
}
218
219
#define pgtok(a)	(((unsigned long long)(a)*getpagesize())/1024)
220
221
void
222
printstate(const struct kinfo_proc *kp, VARENT *ve)
223
32
{
224
	int flag;
225
32
	char *cp, state = '\0';
226
	VAR *v;
227
	char buf[16];
228
229
32
	v = ve->var;
230
32
	flag = kp->p_flag;
231
32
	cp = buf;
232
233

32
	switch (kp->p_stat) {
234
235
	case SSTOP:
236
		*cp = 'T';
237
		break;
238
239
	case SSLEEP:
240
30
		if (flag & P_SINTR)	/* interruptible (long) */
241
30
			*cp = kp->p_slptime >= maxslp ? 'I' : 'S';
242
		else
243
			*cp = 'D';
244
		break;
245
246
	case SRUN:
247
	case SIDL:
248
	case SONPROC:
249
2
		state = *cp = 'R';
250
2
		break;
251
252
	case SDEAD:
253
		*cp = 'Z';
254
		break;
255
256
	default:
257
		*cp = '?';
258
	}
259
32
	cp++;
260
261
32
	if (kp->p_nice < NZERO)
262
		*cp++ = '<';
263
32
	else if (kp->p_nice > NZERO)
264
		*cp++ = 'N';
265
32
	if (kp->p_psflags & PS_TRACED)
266
		*cp++ = 'X';
267
32
	if ((kp->p_psflags & (PS_EXITING | PS_ZOMBIE)) == PS_EXITING)
268
		*cp++ = 'E';
269
32
	if (kp->p_psflags & PS_ISPWAIT)
270
		*cp++ = 'V';
271
32
	if (flag & P_SYSTEM)
272
		*cp++ = 'K';
273

32
	if ((flag & P_SYSTEM) == 0 &&
274
	    kp->p_rlim_rss_cur / 1024 < pgtok(kp->p_vm_rssize))
275
		*cp++ = '>';
276
32
	if (kp->p_eflag & EPROC_SLEADER)
277
14
		*cp++ = 's';
278

32
	if ((kp->p_psflags & PS_CONTROLT) && kp->p__pgid == kp->p_tpgid)
279
28
		*cp++ = '+';
280
32
	if (kp->p_psflags & PS_PLEDGE)
281
32
		*cp++ = 'p';
282
32
	*cp = '\0';
283
284

32
	if (state == 'R' && kp->p_cpuid != KI_NOCPU) {
285
		char pbuf[16];
286
287
		snprintf(pbuf, sizeof pbuf, "/%llu", kp->p_cpuid);
288
		*++cp = '\0';
289
		strlcat(buf, pbuf, sizeof buf);
290
		cp = buf + strlen(buf);
291
	}
292
293
32
	(void)printf("%-*s", v->width, buf);
294
32
}
295
296
void
297
pri(const struct kinfo_proc *kp, VARENT *ve)
298
32
{
299
	VAR *v;
300
301
32
	v = ve->var;
302
32
	(void)printf("%*d", v->width, kp->p_priority - PZERO);
303
32
}
304
305
void
306
pnice(const struct kinfo_proc *kp, VARENT *ve)
307
32
{
308
	VAR *v;
309
32
	v = ve->var;
310
32
	(void)printf("%*d", v->width, kp->p_nice - NZERO);
311
32
}
312
313
void
314
euname(const struct kinfo_proc *kp, VARENT *ve)
315
38
{
316
38
	mbswprint(user_from_uid(kp->p_uid, 0), ve->var->width,
317
	    ve->next != NULL);
318
38
}
319
320
void
321
runame(const struct kinfo_proc *kp, VARENT *ve)
322
38
{
323
38
	mbswprint(user_from_uid(kp->p_ruid, 0), ve->var->width,
324
	    ve->next != NULL);
325
38
}
326
327
void
328
gname(const struct kinfo_proc *kp, VARENT *ve)
329
38
{
330
38
	mbswprint(group_from_gid(kp->p_gid, 0), ve->var->width,
331
	    ve->next != NULL);
332
38
}
333
334
void
335
rgname(const struct kinfo_proc *kp, VARENT *ve)
336
38
{
337
38
	mbswprint(group_from_gid(kp->p_rgid, 0), ve->var->width,
338
	    ve->next != NULL);
339
38
}
340
341
void
342
tdev(const struct kinfo_proc *kp, VARENT *ve)
343
32
{
344
	VAR *v;
345
	dev_t dev;
346
	char buff[16];
347
348
32
	v = ve->var;
349
32
	dev = kp->p_tdev;
350
32
	if (dev == NODEV)
351
		(void)printf("%*s", v->width, "??");
352
	else {
353
32
		(void)snprintf(buff, sizeof(buff),
354
		    "%d/%d", major(dev), minor(dev));
355
32
		(void)printf("%*s", v->width, buff);
356
	}
357
32
}
358
359
void
360
tname(const struct kinfo_proc *kp, VARENT *ve)
361
32
{
362
	VAR *v;
363
	dev_t dev;
364
	char *ttname;
365
366
32
	v = ve->var;
367
32
	dev = kp->p_tdev;
368

32
	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
369
		(void)printf("%-*s", v->width, "??");
370
	else {
371
32
		if (strncmp(ttname, "tty", 3) == 0)
372
32
			ttname += 3;
373
32
		(void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
374
			kp->p_eflag & EPROC_CTTY ? ' ' : '-');
375
	}
376
32
}
377
378
void
379
longtname(const struct kinfo_proc *kp, VARENT *ve)
380
32
{
381
	VAR *v;
382
	dev_t dev;
383
	char *ttname;
384
385
32
	v = ve->var;
386
32
	dev = kp->p_tdev;
387

32
	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
388
		(void)printf("%-*s", v->width, "??");
389
	else
390
32
		(void)printf("%-*s", v->width, ttname);
391
32
}
392
393
void
394
started(const struct kinfo_proc *kp, VARENT *ve)
395
32
{
396
	VAR *v;
397
	static time_t now;
398
	time_t startt;
399
	struct tm *tp;
400
	char buf[100];
401
402
32
	v = ve->var;
403
32
	if (!kp->p_uvalid) {
404
		(void)printf("%-*s", v->width, "-");
405
		return;
406
	}
407
408
#define SECSPERHOUR	(60 * 60)
409
#define SECSPERDAY	(24 * 60 * 60)
410
411
32
	startt = kp->p_ustart_sec;
412
32
	tp = localtime(&startt);
413
32
	if (!now)
414
2
		(void)time(&now);
415
32
	if (now - kp->p_ustart_sec < 12 * SECSPERHOUR) {
416
32
		(void)strftime(buf, sizeof(buf) - 1, "%l:%M%p", tp);
417
	} else if (now - kp->p_ustart_sec < 7 * SECSPERDAY) {
418
		(void)strftime(buf, sizeof(buf) - 1, "%a%I%p", tp);
419
	} else
420
		(void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
421
32
	(void)printf("%-*s", v->width, buf);
422
}
423
424
void
425
lstarted(const struct kinfo_proc *kp, VARENT *ve)
426
32
{
427
	VAR *v;
428
	time_t startt;
429
	char buf[100];
430
431
32
	v = ve->var;
432
32
	if (!kp->p_uvalid) {
433
		(void)printf("%-*s", v->width, "-");
434
		return;
435
	}
436
32
	startt = kp->p_ustart_sec;
437
32
	(void)strftime(buf, sizeof(buf) -1, "%c",
438
	    localtime(&startt));
439
32
	(void)printf("%-*s", v->width, buf);
440
}
441
442
void
443
wchan(const struct kinfo_proc *kp, VARENT *ve)
444
32
{
445
	VAR *v;
446
447
32
	v = ve->var;
448
32
	if (kp->p_wmesg[0]) {
449
30
		(void)printf("%-*s", (int)v->width, kp->p_wmesg);
450
	} else
451
2
		(void)printf("%-*s", v->width, "-");
452
32
}
453
454
void
455
vsize(const struct kinfo_proc *kp, VARENT *ve)
456
32
{
457
	VAR *v;
458
459
32
	v = ve->var;
460
32
	(void)printf("%*llu", v->width,
461
	    pgtok(kp->p_vm_dsize + kp->p_vm_ssize + kp->p_vm_tsize));
462
32
}
463
464
void
465
rssize(const struct kinfo_proc *kp, VARENT *ve)
466
32
{
467
	VAR *v;
468
469
32
	v = ve->var;
470
	/* XXX don't have info about shared */
471
32
	(void)printf("%*llu", v->width, (kp->p_flag & P_SYSTEM) ? 0 :
472
	    pgtok(kp->p_vm_rssize));
473
32
}
474
475
void
476
p_rssize(const struct kinfo_proc *kp, VARENT *ve)
477
32
{
478
	VAR *v;
479
480
32
	v = ve->var;
481
32
	(void)printf("%*llu", v->width, (kp->p_flag & P_SYSTEM) ? 0 :
482
	    pgtok(kp->p_vm_rssize));
483
32
}
484
485
void
486
cputime(const struct kinfo_proc *kp, VARENT *ve)
487
32
{
488
	VAR *v;
489
	long secs;
490
	long psecs;	/* "parts" of a second. first micro, then centi */
491
	char obuff[128];
492
493
32
	v = ve->var;
494

32
	if (kp->p_stat == SDEAD || !kp->p_uvalid) {
495
		secs = 0;
496
		psecs = 0;
497
	} else {
498
		/*
499
		 * This counts time spent handling interrupts.  We could
500
		 * fix this, but it is not 100% trivial (and interrupt
501
		 * time fractions only work on the sparc anyway).	XXX
502
		 */
503
32
		secs = kp->p_rtime_sec;
504
32
		psecs = kp->p_rtime_usec;
505
32
		if (sumrusage) {
506
			secs += kp->p_uctime_sec;
507
			psecs += kp->p_uctime_usec;
508
		}
509
		/*
510
		 * round and scale to 100's
511
		 */
512
32
		psecs = (psecs + 5000) / 10000;
513
32
		secs += psecs / 100;
514
32
		psecs = psecs % 100;
515
	}
516
32
	(void)snprintf(obuff, sizeof(obuff),
517
	    "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
518
32
	(void)printf("%*s", v->width, obuff);
519
32
}
520
521
double
522
getpcpu(const struct kinfo_proc *kp)
523
32
{
524
32
	if (fscale == 0)
525
		return (0.0);
526
527
#define	fxtofl(fixpt)	((double)(fixpt) / fscale)
528
529
32
	return (100.0 * fxtofl(kp->p_pctcpu));
530
}
531
532
void
533
pcpu(const struct kinfo_proc *kp, VARENT *ve)
534
32
{
535
	VAR *v;
536
537
32
	v = ve->var;
538
32
	(void)printf("%*.1f", v->width, getpcpu(kp));
539
32
}
540
541
double
542
getpmem(const struct kinfo_proc *kp)
543
32
{
544
	double fracmem;
545
546
32
	if (mempages == 0)
547
		return (0.0);
548
549
32
	if (kp->p_flag & P_SYSTEM)
550
		return (0.0);
551
	/* XXX don't have info about shared */
552
32
	fracmem = ((float)kp->p_vm_rssize)/mempages;
553
32
	return (100.0 * fracmem);
554
}
555
556
void
557
pmem(const struct kinfo_proc *kp, VARENT *ve)
558
32
{
559
	VAR *v;
560
561
32
	v = ve->var;
562
32
	(void)printf("%*.1f", v->width, getpmem(kp));
563
32
}
564
565
void
566
pagein(const struct kinfo_proc *kp, VARENT *ve)
567
32
{
568
	VAR *v;
569
570
32
	v = ve->var;
571
32
	(void)printf("%*llu", v->width,
572
	    kp->p_uvalid ? kp->p_uru_majflt : 0);
573
32
}
574
575
void
576
maxrss(const struct kinfo_proc *kp, VARENT *ve)
577
32
{
578
	VAR *v;
579
580
32
	v = ve->var;
581
32
	(void)printf("%*llu", v->width, kp->p_rlim_rss_cur / 1024);
582
32
}
583
584
void
585
tsize(const struct kinfo_proc *kp, VARENT *ve)
586
32
{
587
	VAR *v;
588
589
32
	v = ve->var;
590
32
	(void)printf("%*llu", v->width, pgtok(kp->p_vm_tsize));
591
32
}
592
593
void
594
dsize(const struct kinfo_proc *kp, VARENT *ve)
595
32
{
596
	VAR *v;
597
598
32
	v = ve->var;
599
32
	(void)printf("%*llu", v->width, pgtok(kp->p_vm_dsize));
600
32
}
601
602
void
603
ssize(const struct kinfo_proc *kp, VARENT *ve)
604
32
{
605
	VAR *v;
606
607
32
	v = ve->var;
608
32
	(void)printf("%*llu", v->width, pgtok(kp->p_vm_ssize));
609
32
}
610
611
/*
612
 * Generic output routines.  Print fields from various prototype
613
 * structures.
614
 */
615
static void
616
printval(char *bp, VAR *v)
617
1515
{
618
	char ofmt[32];
619
620
1515
	snprintf(ofmt, sizeof(ofmt), "%%%s*%s", (v->flag & LJUST) ? "-" : "",
621
	    v->fmt);
622
623
	/*
624
	 * Note that the "INF127" check is nonsensical for types
625
	 * that are or can be signed.
626
	 */
627
#define	GET(type)		(*(type *)bp)
628
#define	CHK_INF127(n)		(((n) > 127) && (v->flag & INF127) ? 127 : (n))
629
630


1515
	switch (v->type) {
631
	case INT8:
632
		(void)printf(ofmt, v->width, GET(int8_t));
633
		break;
634
	case UINT8:
635

32
		(void)printf(ofmt, v->width, CHK_INF127(GET(u_int8_t)));
636
32
		break;
637
	case INT16:
638
32
		(void)printf(ofmt, v->width, GET(int16_t));
639
32
		break;
640
	case UINT16:
641

64
		(void)printf(ofmt, v->width, CHK_INF127(GET(u_int16_t)));
642
64
		break;
643
	case INT32:
644
395
		(void)printf(ofmt, v->width, GET(int32_t));
645
395
		break;
646
	case UINT32:
647

384
		(void)printf(ofmt, v->width, CHK_INF127(GET(u_int32_t)));
648
384
		break;
649
	case INT64:
650
		(void)printf(ofmt, v->width, GET(int64_t));
651
		break;
652
	case UINT64:
653

608
		(void)printf(ofmt, v->width, CHK_INF127(GET(u_int64_t)));
654
608
		break;
655
	default:
656
		errx(1, "unknown type %d", v->type);
657
	}
658
#undef GET
659
#undef CHK_INF127
660
1515
}
661
662
void
663
pvar(const struct kinfo_proc *kp, VARENT *ve)
664
1515
{
665
	VAR *v;
666
667
1515
	v = ve->var;
668

1515
	if ((v->flag & USER) && !kp->p_uvalid)
669
		(void)printf("%*s", v->width, "-");
670
	else
671
1515
		printval((char *)kp + v->off, v);
672
1515
}
673
674
void
675
emulname(const struct kinfo_proc *kp, VARENT *ve)
676
32
{
677
	VAR *v;
678
679
32
	v = ve->var;
680
681
32
	(void)printf("%-*s", (int)v->width, kp->p_emul);
682
32
}