xref: /freebsd/bin/ps/print.c (revision 13ea0450a9c8742119d36f3bf8f47accdce46e54)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if 0
33 #ifndef lint
34 static char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
35 #endif /* not lint */
36 #endif
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 #include <sys/time.h>
43 #include <sys/resource.h>
44 #include <sys/proc.h>
45 #include <sys/stat.h>
46 
47 #include <sys/mac.h>
48 #include <sys/user.h>
49 #include <sys/sysctl.h>
50 #include <sys/vmmeter.h>
51 
52 #include <err.h>
53 #include <grp.h>
54 #include <jail.h>
55 #include <langinfo.h>
56 #include <locale.h>
57 #include <math.h>
58 #include <nlist.h>
59 #include <pwd.h>
60 #include <stddef.h>
61 #include <stdint.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 #include <vis.h>
67 #include <libxo/xo.h>
68 
69 #include "ps.h"
70 
71 #define	COMMAND_WIDTH	16
72 #define	ARGUMENTS_WIDTH	16
73 
74 #define	ps_pgtok(a)	(((a) * getpagesize()) / 1024)
75 
76 void
77 printheader(void)
78 {
79 	VAR *v;
80 	struct varent *vent;
81 
82 	STAILQ_FOREACH(vent, &varlist, next_ve)
83 		if (*vent->header != '\0')
84 			break;
85 	if (!vent)
86 		return;
87 
88 	STAILQ_FOREACH(vent, &varlist, next_ve) {
89 		v = vent->var;
90 		if (v->flag & LJUST) {
91 			if (STAILQ_NEXT(vent, next_ve) == NULL)	/* last one */
92 				xo_emit("{T:/%s}", vent->header);
93 			else
94 				xo_emit("{T:/%-*s}", v->width, vent->header);
95 		} else
96 			xo_emit("{T:/%*s}", v->width, vent->header);
97 		if (STAILQ_NEXT(vent, next_ve) != NULL)
98 			xo_emit("{P: }");
99 	}
100 	xo_emit("\n");
101 }
102 
103 char *
104 arguments(KINFO *k, VARENT *ve)
105 {
106 	char *vis_args;
107 
108 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
109 		xo_errx(1, "malloc failed");
110 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
111 
112 	if (STAILQ_NEXT(ve, next_ve) != NULL && strlen(vis_args) > ARGUMENTS_WIDTH)
113 		vis_args[ARGUMENTS_WIDTH] = '\0';
114 
115 	return (vis_args);
116 }
117 
118 char *
119 command(KINFO *k, VARENT *ve)
120 {
121 	char *vis_args, *vis_env, *str;
122 
123 	if (cflag) {
124 		/* If it is the last field, then don't pad */
125 		if (STAILQ_NEXT(ve, next_ve) == NULL) {
126 			asprintf(&str, "%s%s%s%s%s",
127 			    k->ki_d.prefix ? k->ki_d.prefix : "",
128 			    k->ki_p->ki_comm,
129 			    (showthreads && k->ki_p->ki_numthreads > 1) ? "/" : "",
130 			    (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_tdname : "",
131 			    (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_moretdname : "");
132 		} else
133 			str = strdup(k->ki_p->ki_comm);
134 
135 		return (str);
136 	}
137 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
138 		xo_errx(1, "malloc failed");
139 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
140 
141 	if (STAILQ_NEXT(ve, next_ve) == NULL) {
142 		/* last field */
143 
144 		if (k->ki_env) {
145 			if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1))
146 			    == NULL)
147 				xo_errx(1, "malloc failed");
148 			strvis(vis_env, k->ki_env,
149 			    VIS_TAB | VIS_NL | VIS_NOSLASH);
150 		} else
151 			vis_env = NULL;
152 
153 		asprintf(&str, "%s%s%s%s",
154 		    k->ki_d.prefix ? k->ki_d.prefix : "",
155 		    vis_env ? vis_env : "",
156 		    vis_env ? " " : "",
157 		    vis_args);
158 
159 		if (vis_env != NULL)
160 			free(vis_env);
161 		free(vis_args);
162 	} else {
163 		/* ki_d.prefix & ki_env aren't shown for interim fields */
164 		str = vis_args;
165 
166 		if (strlen(str) > COMMAND_WIDTH)
167 			str[COMMAND_WIDTH] = '\0';
168 	}
169 
170 	return (str);
171 }
172 
173 char *
174 ucomm(KINFO *k, VARENT *ve)
175 {
176 	char *str;
177 
178 	if (STAILQ_NEXT(ve, next_ve) == NULL) {	/* last field, don't pad */
179 		asprintf(&str, "%s%s%s%s%s",
180 		    k->ki_d.prefix ? k->ki_d.prefix : "",
181 		    k->ki_p->ki_comm,
182 		    (showthreads && k->ki_p->ki_numthreads > 1) ? "/" : "",
183 		    (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_tdname : "",
184 		    (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_moretdname : "");
185 	} else {
186 		if (showthreads && k->ki_p->ki_numthreads > 1)
187 			asprintf(&str, "%s/%s%s", k->ki_p->ki_comm,
188 			    k->ki_p->ki_tdname, k->ki_p->ki_moretdname);
189 		else
190 			str = strdup(k->ki_p->ki_comm);
191 	}
192 	return (str);
193 }
194 
195 char *
196 tdnam(KINFO *k, VARENT *ve __unused)
197 {
198 	char *str;
199 
200 	if (showthreads && k->ki_p->ki_numthreads > 1)
201 		asprintf(&str, "%s%s", k->ki_p->ki_tdname,
202 		    k->ki_p->ki_moretdname);
203 	else
204 		str = strdup("      ");
205 
206 	return (str);
207 }
208 
209 char *
210 logname(KINFO *k, VARENT *ve __unused)
211 {
212 
213 	if (*k->ki_p->ki_login == '\0')
214 		return (NULL);
215 	return (strdup(k->ki_p->ki_login));
216 }
217 
218 char *
219 state(KINFO *k, VARENT *ve __unused)
220 {
221 	long flag, tdflags;
222 	char *cp, *buf;
223 
224 	buf = malloc(16);
225 	if (buf == NULL)
226 		xo_errx(1, "malloc failed");
227 
228 	flag = k->ki_p->ki_flag;
229 	tdflags = k->ki_p->ki_tdflags;	/* XXXKSE */
230 	cp = buf;
231 
232 	switch (k->ki_p->ki_stat) {
233 
234 	case SSTOP:
235 		*cp = 'T';
236 		break;
237 
238 	case SSLEEP:
239 		if (tdflags & TDF_SINTR)	/* interruptable (long) */
240 			*cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
241 		else
242 			*cp = 'D';
243 		break;
244 
245 	case SRUN:
246 	case SIDL:
247 		*cp = 'R';
248 		break;
249 
250 	case SWAIT:
251 		*cp = 'W';
252 		break;
253 
254 	case SLOCK:
255 		*cp = 'L';
256 		break;
257 
258 	case SZOMB:
259 		*cp = 'Z';
260 		break;
261 
262 	default:
263 		*cp = '?';
264 	}
265 	cp++;
266 	if (!(flag & P_INMEM))
267 		*cp++ = 'W';
268 	if (k->ki_p->ki_nice < NZERO || k->ki_p->ki_pri.pri_class == PRI_REALTIME)
269 		*cp++ = '<';
270 	else if (k->ki_p->ki_nice > NZERO || k->ki_p->ki_pri.pri_class == PRI_IDLE)
271 		*cp++ = 'N';
272 	if (flag & P_TRACED)
273 		*cp++ = 'X';
274 	if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
275 		*cp++ = 'E';
276 	if (flag & P_PPWAIT)
277 		*cp++ = 'V';
278 	if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
279 		*cp++ = 'L';
280 	if ((k->ki_p->ki_cr_flags & CRED_FLAG_CAPMODE) != 0)
281 		*cp++ = 'C';
282 	if (k->ki_p->ki_kiflag & KI_SLEADER)
283 		*cp++ = 's';
284 	if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
285 		*cp++ = '+';
286 	if (flag & P_JAILED)
287 		*cp++ = 'J';
288 	*cp = '\0';
289 	return (buf);
290 }
291 
292 #define	scalepri(x)	((x) - PZERO)
293 
294 char *
295 pri(KINFO *k, VARENT *ve __unused)
296 {
297 	char *str;
298 
299 	asprintf(&str, "%d", scalepri(k->ki_p->ki_pri.pri_level));
300 	return (str);
301 }
302 
303 char *
304 upr(KINFO *k, VARENT *ve __unused)
305 {
306 	char *str;
307 
308 	asprintf(&str, "%d", scalepri(k->ki_p->ki_pri.pri_user));
309 	return (str);
310 }
311 #undef scalepri
312 
313 char *
314 uname(KINFO *k, VARENT *ve __unused)
315 {
316 
317 	return (strdup(user_from_uid(k->ki_p->ki_uid, 0)));
318 }
319 
320 char *
321 egroupname(KINFO *k, VARENT *ve __unused)
322 {
323 
324 	return (strdup(group_from_gid(k->ki_p->ki_groups[0], 0)));
325 }
326 
327 char *
328 rgroupname(KINFO *k, VARENT *ve __unused)
329 {
330 
331 	return (strdup(group_from_gid(k->ki_p->ki_rgid, 0)));
332 }
333 
334 char *
335 runame(KINFO *k, VARENT *ve __unused)
336 {
337 
338 	return (strdup(user_from_uid(k->ki_p->ki_ruid, 0)));
339 }
340 
341 char *
342 tdev(KINFO *k, VARENT *ve __unused)
343 {
344 	dev_t dev;
345 	char *str;
346 
347 	dev = k->ki_p->ki_tdev;
348 	if (dev == NODEV)
349 		str = strdup("-");
350 	else
351 		asprintf(&str, "%#jx", (uintmax_t)dev);
352 
353 	return (str);
354 }
355 
356 char *
357 tname(KINFO *k, VARENT *ve __unused)
358 {
359 	dev_t dev;
360 	char *ttname, *str;
361 
362 	dev = k->ki_p->ki_tdev;
363 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
364 		str = strdup("- ");
365 	else {
366 		if (strncmp(ttname, "tty", 3) == 0 ||
367 		    strncmp(ttname, "cua", 3) == 0)
368 			ttname += 3;
369 		if (strncmp(ttname, "pts/", 4) == 0)
370 			ttname += 4;
371 		asprintf(&str, "%s%c", ttname,
372 		    k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
373 	}
374 
375 	return (str);
376 }
377 
378 char *
379 longtname(KINFO *k, VARENT *ve __unused)
380 {
381 	dev_t dev;
382 	const char *ttname;
383 
384 	dev = k->ki_p->ki_tdev;
385 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
386 		ttname = "-";
387 
388 	return (strdup(ttname));
389 }
390 
391 char *
392 started(KINFO *k, VARENT *ve __unused)
393 {
394 	time_t then;
395 	struct tm *tp;
396 	size_t buflen = 100;
397 	char *buf;
398 
399 	if (!k->ki_valid)
400 		return (NULL);
401 
402 	buf = malloc(buflen);
403 	if (buf == NULL)
404 		xo_errx(1, "malloc failed");
405 
406 	then = k->ki_p->ki_start.tv_sec;
407 	tp = localtime(&then);
408 	if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
409 		(void)strftime(buf, buflen, "%H:%M  ", tp);
410 	} else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
411 		(void)strftime(buf, buflen, "%a%H  ", tp);
412 	} else
413 		(void)strftime(buf, buflen, "%e%b%y", tp);
414 	return (buf);
415 }
416 
417 char *
418 lstarted(KINFO *k, VARENT *ve __unused)
419 {
420 	time_t then;
421 	char *buf;
422 	size_t buflen = 100;
423 
424 	if (!k->ki_valid)
425 		return (NULL);
426 
427 	buf = malloc(buflen);
428 	if (buf == NULL)
429 		xo_errx(1, "malloc failed");
430 
431 	then = k->ki_p->ki_start.tv_sec;
432 	(void)strftime(buf, buflen, "%c", localtime(&then));
433 	return (buf);
434 }
435 
436 char *
437 lockname(KINFO *k, VARENT *ve __unused)
438 {
439 	char *str;
440 
441 	if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
442 		if (k->ki_p->ki_lockname[0] != 0)
443 			str = strdup(k->ki_p->ki_lockname);
444 		else
445 			str = strdup("???");
446 	} else
447 		str = NULL;
448 
449 	return (str);
450 }
451 
452 char *
453 wchan(KINFO *k, VARENT *ve __unused)
454 {
455 	char *str;
456 
457 	if (k->ki_p->ki_wchan) {
458 		if (k->ki_p->ki_wmesg[0] != 0)
459 			str = strdup(k->ki_p->ki_wmesg);
460 		else
461 			asprintf(&str, "%lx", (long)k->ki_p->ki_wchan);
462 	} else
463 		str = NULL;
464 
465 	return (str);
466 }
467 
468 char *
469 nwchan(KINFO *k, VARENT *ve __unused)
470 {
471 	char *str;
472 
473 	if (k->ki_p->ki_wchan)
474 		asprintf(&str, "%0lx", (long)k->ki_p->ki_wchan);
475 	else
476 		str = NULL;
477 
478 	return (str);
479 }
480 
481 char *
482 mwchan(KINFO *k, VARENT *ve __unused)
483 {
484 	char *str;
485 
486 	if (k->ki_p->ki_wchan) {
487 		if (k->ki_p->ki_wmesg[0] != 0)
488 			str = strdup(k->ki_p->ki_wmesg);
489 		else
490                         asprintf(&str, "%lx", (long)k->ki_p->ki_wchan);
491 	} else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
492 		if (k->ki_p->ki_lockname[0]) {
493 			str = strdup(k->ki_p->ki_lockname);
494 		} else
495 			str = strdup("???");
496 	} else
497 		str = NULL;
498 
499 	return (str);
500 }
501 
502 char *
503 vsize(KINFO *k, VARENT *ve __unused)
504 {
505 	char *str;
506 
507 	asprintf(&str, "%lu", (u_long)(k->ki_p->ki_size / 1024));
508 	return (str);
509 }
510 
511 static char *
512 printtime(KINFO *k, VARENT *ve __unused, long secs, long psecs)
513 /* psecs is "parts" of a second. first micro, then centi */
514 {
515 	static char decimal_point;
516 	char *str;
517 
518 	if (decimal_point == '\0')
519 		decimal_point = localeconv()->decimal_point[0];
520 	if (!k->ki_valid) {
521 		secs = 0;
522 		psecs = 0;
523 	} else {
524 		/* round and scale to 100's */
525 		psecs = (psecs + 5000) / 10000;
526 		secs += psecs / 100;
527 		psecs = psecs % 100;
528 	}
529 	asprintf(&str, "%ld:%02ld%c%02ld",
530 	    secs / 60, secs % 60, decimal_point, psecs);
531 	return (str);
532 }
533 
534 char *
535 cputime(KINFO *k, VARENT *ve)
536 {
537 	long secs, psecs;
538 
539 	/*
540 	 * This counts time spent handling interrupts.  We could
541 	 * fix this, but it is not 100% trivial (and interrupt
542 	 * time fractions only work on the sparc anyway).	XXX
543 	 */
544 	secs = k->ki_p->ki_runtime / 1000000;
545 	psecs = k->ki_p->ki_runtime % 1000000;
546 	if (sumrusage) {
547 		secs += k->ki_p->ki_childtime.tv_sec;
548 		psecs += k->ki_p->ki_childtime.tv_usec;
549 	}
550 	return (printtime(k, ve, secs, psecs));
551 }
552 
553 char *
554 systime(KINFO *k, VARENT *ve)
555 {
556 	long secs, psecs;
557 
558 	secs = k->ki_p->ki_rusage.ru_stime.tv_sec;
559 	psecs = k->ki_p->ki_rusage.ru_stime.tv_usec;
560 	if (sumrusage) {
561 		secs += k->ki_p->ki_childstime.tv_sec;
562 		psecs += k->ki_p->ki_childstime.tv_usec;
563 	}
564 	return (printtime(k, ve, secs, psecs));
565 }
566 
567 char *
568 usertime(KINFO *k, VARENT *ve)
569 {
570 	long secs, psecs;
571 
572 	secs = k->ki_p->ki_rusage.ru_utime.tv_sec;
573 	psecs = k->ki_p->ki_rusage.ru_utime.tv_usec;
574 	if (sumrusage) {
575 		secs += k->ki_p->ki_childutime.tv_sec;
576 		psecs += k->ki_p->ki_childutime.tv_usec;
577 	}
578 	return (printtime(k, ve, secs, psecs));
579 }
580 
581 char *
582 elapsed(KINFO *k, VARENT *ve __unused)
583 {
584 	time_t val;
585 	int days, hours, mins, secs;
586 	char *str;
587 
588 	if (!k->ki_valid)
589 		return (NULL);
590 	val = now - k->ki_p->ki_start.tv_sec;
591 	days = val / (24 * 60 * 60);
592 	val %= 24 * 60 * 60;
593 	hours = val / (60 * 60);
594 	val %= 60 * 60;
595 	mins = val / 60;
596 	secs = val % 60;
597 	if (days != 0)
598 		asprintf(&str, "%3d-%02d:%02d:%02d", days, hours, mins, secs);
599 	else if (hours != 0)
600 		asprintf(&str, "%02d:%02d:%02d", hours, mins, secs);
601 	else
602 		asprintf(&str, "%02d:%02d", mins, secs);
603 
604 	return (str);
605 }
606 
607 char *
608 elapseds(KINFO *k, VARENT *ve __unused)
609 {
610 	time_t val;
611 	char *str;
612 
613 	if (!k->ki_valid)
614 		return (NULL);
615 	val = now - k->ki_p->ki_start.tv_sec;
616 	asprintf(&str, "%jd", (intmax_t)val);
617 	return (str);
618 }
619 
620 double
621 getpcpu(const KINFO *k)
622 {
623 	static int failure;
624 
625 	if (!nlistread)
626 		failure = donlist();
627 	if (failure)
628 		return (0.0);
629 
630 #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
631 
632 	/* XXX - I don't like this */
633 	if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_flag & P_INMEM) == 0)
634 		return (0.0);
635 	if (rawcpu)
636 		return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
637 	return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
638 		(1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
639 }
640 
641 char *
642 pcpu(KINFO *k, VARENT *ve __unused)
643 {
644 	char *str;
645 
646 	asprintf(&str, "%.1f", getpcpu(k));
647 	return (str);
648 }
649 
650 static double
651 getpmem(KINFO *k)
652 {
653 	static int failure;
654 	double fracmem;
655 
656 	if (!nlistread)
657 		failure = donlist();
658 	if (failure)
659 		return (0.0);
660 
661 	if ((k->ki_p->ki_flag & P_INMEM) == 0)
662 		return (0.0);
663 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
664 	/* XXX don't have info about shared */
665 	fracmem = ((double)k->ki_p->ki_rssize) / mempages;
666 	return (100.0 * fracmem);
667 }
668 
669 char *
670 pmem(KINFO *k, VARENT *ve __unused)
671 {
672 	char *str;
673 
674 	asprintf(&str, "%.1f", getpmem(k));
675 	return (str);
676 }
677 
678 char *
679 pagein(KINFO *k, VARENT *ve __unused)
680 {
681 	char *str;
682 
683 	asprintf(&str, "%ld", k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
684 	return (str);
685 }
686 
687 /* ARGSUSED */
688 char *
689 maxrss(KINFO *k __unused, VARENT *ve __unused)
690 {
691 
692 	/* XXX not yet */
693 	return (NULL);
694 }
695 
696 char *
697 priorityr(KINFO *k, VARENT *ve __unused)
698 {
699 	struct priority *lpri;
700 	char *str;
701 	unsigned class, level;
702 
703 	lpri = &k->ki_p->ki_pri;
704 	class = lpri->pri_class;
705 	level = lpri->pri_level;
706 	switch (class) {
707 	case PRI_ITHD:
708 		asprintf(&str, "intr:%u", level);
709 		break;
710 	case PRI_REALTIME:
711 		asprintf(&str, "real:%u", level);
712 		break;
713 	case PRI_TIMESHARE:
714 		asprintf(&str, "normal");
715 		break;
716 	case PRI_IDLE:
717 		asprintf(&str, "idle:%u", level);
718 		break;
719 	default:
720 		asprintf(&str, "%u:%u", class, level);
721 		break;
722 	}
723 	return (str);
724 }
725 
726 /*
727  * Generic output routines.  Print fields from various prototype
728  * structures.
729  */
730 static char *
731 printval(void *bp, VAR *v)
732 {
733 	static char ofmt[32] = "%";
734 	const char *fcp;
735 	char *cp, *str;
736 
737 	cp = ofmt + 1;
738 	fcp = v->fmt;
739 	while ((*cp++ = *fcp++));
740 
741 #define	CHKINF127(n)	(((n) > 127) && (v->flag & INF127) ? 127 : (n))
742 
743 	switch (v->type) {
744 	case CHAR:
745 		(void)asprintf(&str, ofmt, *(char *)bp);
746 		break;
747 	case UCHAR:
748 		(void)asprintf(&str, ofmt, *(u_char *)bp);
749 		break;
750 	case SHORT:
751 		(void)asprintf(&str, ofmt, *(short *)bp);
752 		break;
753 	case USHORT:
754 		(void)asprintf(&str, ofmt, *(u_short *)bp);
755 		break;
756 	case INT:
757 		(void)asprintf(&str, ofmt, *(int *)bp);
758 		break;
759 	case UINT:
760 		(void)asprintf(&str, ofmt, CHKINF127(*(u_int *)bp));
761 		break;
762 	case LONG:
763 		(void)asprintf(&str, ofmt, *(long *)bp);
764 		break;
765 	case ULONG:
766 		(void)asprintf(&str, ofmt, *(u_long *)bp);
767 		break;
768 	case KPTR:
769 		(void)asprintf(&str, ofmt, *(u_long *)bp);
770 		break;
771 	case PGTOK:
772 		(void)asprintf(&str, ofmt, ps_pgtok(*(u_long *)bp));
773 		break;
774 	}
775 
776 	return (str);
777 }
778 
779 char *
780 kvar(KINFO *k, VARENT *ve)
781 {
782 	VAR *v;
783 
784 	v = ve->var;
785 	return (printval((char *)((char *)k->ki_p + v->off), v));
786 }
787 
788 char *
789 rvar(KINFO *k, VARENT *ve)
790 {
791 	VAR *v;
792 
793 	v = ve->var;
794 	if (!k->ki_valid)
795 		return (NULL);
796 	return (printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v));
797 }
798 
799 char *
800 emulname(KINFO *k, VARENT *ve __unused)
801 {
802 
803 	return (strdup(k->ki_p->ki_emul));
804 }
805 
806 char *
807 label(KINFO *k, VARENT *ve __unused)
808 {
809 	char *string;
810 	mac_t proclabel;
811 	int error;
812 
813 	string = NULL;
814 	if (mac_prepare_process_label(&proclabel) == -1) {
815 		xo_warn("mac_prepare_process_label");
816 		goto out;
817 	}
818 	error = mac_get_pid(k->ki_p->ki_pid, proclabel);
819 	if (error == 0) {
820 		if (mac_to_text(proclabel, &string) == -1)
821 			string = NULL;
822 	}
823 	mac_free(proclabel);
824 out:
825 	return (string);
826 }
827 
828 char *
829 loginclass(KINFO *k, VARENT *ve __unused)
830 {
831 
832 	/*
833 	 * Don't display login class for system processes;
834 	 * login classes are used for resource limits,
835 	 * and limits don't apply to system processes.
836 	 */
837 	if (k->ki_p->ki_flag & P_SYSTEM) {
838 		return (strdup("-"));
839 	}
840 	return (strdup(k->ki_p->ki_loginclass));
841 }
842 
843 char *
844 jailname(KINFO *k, VARENT *ve __unused)
845 {
846 	char *name;
847 
848 	if (k->ki_p->ki_jid == 0)
849 		return (strdup("-"));
850 	name = jail_getname(k->ki_p->ki_jid);
851 	if (name == NULL)
852 		return (strdup("-"));
853 	return (name);
854 }
855