print.c revision 1.99 1 /* $NetBSD: print.c,v 1.99 2007/02/17 22:49:57 pavel Exp $ */
2
3 /*
4 * Copyright (c) 2000, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Simon Burge.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1990, 1993, 1994
41 * The Regents of the University of California. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 */
67
68 #include <sys/cdefs.h>
69 #ifndef lint
70 #if 0
71 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
72 #else
73 __RCSID("$NetBSD: print.c,v 1.99 2007/02/17 22:49:57 pavel Exp $");
74 #endif
75 #endif /* not lint */
76
77 #include <sys/param.h>
78 #include <sys/time.h>
79 #include <sys/resource.h>
80 #include <sys/lwp.h>
81 #include <sys/proc.h>
82 #include <sys/stat.h>
83 #include <sys/ucred.h>
84 #include <sys/sysctl.h>
85
86 #include <err.h>
87 #include <grp.h>
88 #include <kvm.h>
89 #include <math.h>
90 #include <nlist.h>
91 #include <pwd.h>
92 #include <stddef.h>
93 #include <stdio.h>
94 #include <stdlib.h>
95 #include <string.h>
96 #include <time.h>
97 #include <tzfile.h>
98 #include <unistd.h>
99
100 #include "ps.h"
101
102 static char *cmdpart(char *);
103 static void printval(void *, VAR *, int);
104 static int titlecmp(char *, char **);
105
106 static void doubleprintorsetwidth(VAR *, double, int, int);
107 static void intprintorsetwidth(VAR *, int, int);
108 static void strprintorsetwidth(VAR *, const char *, int);
109
110 static time_t now;
111
112 #define min(a,b) ((a) <= (b) ? (a) : (b))
113
114 static int
115 iwidth(u_int64_t v)
116 {
117 u_int64_t nlim, lim;
118 int w = 1;
119
120 for (lim = 10; v >= lim; lim = nlim) {
121 nlim = lim * 10;
122 w++;
123 if (nlim < lim)
124 break;
125 }
126 return w;
127 }
128
129 static char *
130 cmdpart(char *arg0)
131 {
132 char *cp;
133
134 return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
135 }
136
137 void
138 printheader(void)
139 {
140 int len;
141 VAR *v;
142 struct varent *vent;
143 static int firsttime = 1;
144 static int noheader = 0;
145
146 /*
147 * If all the columns have user-specified null headers,
148 * don't print the blank header line at all.
149 */
150 if (firsttime) {
151 SIMPLEQ_FOREACH(vent, &displaylist, next) {
152 if (vent->var->header[0])
153 break;
154 }
155 if (vent == NULL) {
156 noheader = 1;
157 firsttime = 0;
158 }
159
160 }
161 if (noheader)
162 return;
163
164 SIMPLEQ_FOREACH(vent, &displaylist, next) {
165 v = vent->var;
166 if (firsttime) {
167 len = strlen(v->header);
168 if (len > v->width)
169 v->width = len;
170 totwidth += v->width + 1; /* +1 for space */
171 }
172 if (v->flag & LJUST) {
173 if (SIMPLEQ_NEXT(vent, next) == NULL) /* last one */
174 (void)printf("%s", v->header);
175 else
176 (void)printf("%-*s", v->width,
177 v->header);
178 } else
179 (void)printf("%*s", v->width, v->header);
180 if (SIMPLEQ_NEXT(vent, next) != NULL)
181 (void)putchar(' ');
182 }
183 (void)putchar('\n');
184 if (firsttime) {
185 firsttime = 0;
186 totwidth--; /* take off last space */
187 }
188 }
189
190 /*
191 * Return 1 if the command name in the argument vector (u-area) does
192 * not match the command name (p_comm)
193 */
194 static int
195 titlecmp(char *name, char **argv)
196 {
197 char *title;
198 int namelen;
199
200
201 /* no argument vector == no match; system processes/threads do that */
202 if (argv == 0 || argv[0] == 0)
203 return (1);
204
205 title = cmdpart(argv[0]);
206
207 /* the basename matches */
208 if (!strcmp(name, title))
209 return (0);
210
211 /* handle login shells, by skipping the leading - */
212 if (title[0] == '-' && !strcmp(name, title + 1))
213 return (0);
214
215 namelen = strlen(name);
216
217 /* handle daemons that report activity as daemonname: activity */
218 if (argv[1] == 0 &&
219 !strncmp(name, title, namelen) &&
220 title[namelen + 0] == ':' &&
221 title[namelen + 1] == ' ')
222 return (0);
223
224 return (1);
225 }
226
227 static void
228 doubleprintorsetwidth(VAR *v, double val, int prec, int mode)
229 {
230 int fmtlen;
231
232 if (mode == WIDTHMODE) {
233 if (val < 0.0 && val < v->longestnd) {
234 fmtlen = (int)log10(-val) + prec + 2;
235 v->longestnd = val;
236 if (fmtlen > v->width)
237 v->width = fmtlen;
238 } else if (val > 0.0 && val > v->longestpd) {
239 fmtlen = (int)log10(val) + prec + 1;
240 v->longestpd = val;
241 if (fmtlen > v->width)
242 v->width = fmtlen;
243 }
244 } else {
245 (void)printf("%*.*f", v->width, prec, val);
246 }
247 }
248
249 static void
250 intprintorsetwidth(VAR *v, int val, int mode)
251 {
252 int fmtlen;
253
254 if (mode == WIDTHMODE) {
255 if (val < 0 && val < v->longestn) {
256 v->longestn = val;
257 fmtlen = iwidth(-val) + 1;
258 if (fmtlen > v->width)
259 v->width = fmtlen;
260 } else if (val > 0 && val > v->longestp) {
261 v->longestp = val;
262 fmtlen = iwidth(val);
263 if (fmtlen > v->width)
264 v->width = fmtlen;
265 }
266 } else
267 (void)printf("%*d", v->width, val);
268 }
269
270 static void
271 strprintorsetwidth(VAR *v, const char *str, int mode)
272 {
273 int len;
274
275 if (mode == WIDTHMODE) {
276 len = strlen(str);
277 if (len > v->width)
278 v->width = len;
279 } else {
280 if (v->flag & LJUST)
281 (void)printf("%-*.*s", v->width, v->width, str);
282 else
283 (void)printf("%*.*s", v->width, v->width, str);
284 }
285 }
286
287 void
288 command(void *arg, VARENT *ve, int mode)
289 {
290 struct kinfo_proc2 *ki;
291 VAR *v;
292 int left;
293 char **argv, **p, *name;
294
295 if (mode == WIDTHMODE)
296 return;
297
298 ki = arg;
299 v = ve->var;
300 if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
301 if (SIMPLEQ_NEXT(ve, next) == NULL) {
302 left = termwidth - (totwidth - v->width);
303 if (left < 1) /* already wrapped, just use std width */
304 left = v->width;
305 } else
306 left = v->width;
307 } else
308 left = -1;
309 if (needenv && kd) {
310 argv = kvm_getenvv2(kd, ki, termwidth);
311 if ((p = argv) != NULL) {
312 while (*p) {
313 fmt_puts(*p, &left);
314 p++;
315 fmt_putc(' ', &left);
316 }
317 }
318 }
319 if (needcomm) {
320 name = ki->p_comm;
321 if (!commandonly) {
322 argv = kvm_getargv2(kd, ki, termwidth);
323 if ((p = argv) != NULL) {
324 while (*p) {
325 fmt_puts(*p, &left);
326 p++;
327 fmt_putc(' ', &left);
328 if (v->flag & ARGV0)
329 break;
330 }
331 if (!(v->flag & ARGV0) &&
332 titlecmp(name, argv)) {
333 /*
334 * append the real command name within
335 * parentheses, if the command name
336 * does not match the one in the
337 * argument vector
338 */
339 fmt_putc('(', &left);
340 fmt_puts(name, &left);
341 fmt_putc(')', &left);
342 }
343 } else {
344 /*
345 * Commands that don't set an argv vector
346 * are printed with square brackets if they
347 * are system commands. Otherwise they are
348 * printed within parentheses.
349 */
350 if (ki->p_flag & P_SYSTEM) {
351 fmt_putc('[', &left);
352 fmt_puts(name, &left);
353 fmt_putc(']', &left);
354 } else {
355 fmt_putc('(', &left);
356 fmt_puts(name, &left);
357 fmt_putc(')', &left);
358 }
359 }
360 } else {
361 fmt_puts(name, &left);
362 }
363 }
364 if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
365 (void)printf("%*s", left, "");
366 }
367
368 void
369 groups(void *arg, VARENT *ve, int mode)
370 {
371 struct kinfo_proc2 *ki;
372 VAR *v;
373 int left, i;
374 char buf[16], *p;
375
376 if (mode == WIDTHMODE)
377 return;
378
379 ki = arg;
380 v = ve->var;
381 if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
382 if (SIMPLEQ_NEXT(ve, next) == NULL) {
383 left = termwidth - (totwidth - v->width);
384 if (left < 1) /* already wrapped, just use std width */
385 left = v->width;
386 } else
387 left = v->width;
388 } else
389 left = -1;
390
391 if (ki->p_ngroups == 0) {
392 fmt_putc('-', &left);
393 return;
394 }
395
396 for (i = 0; i < ki->p_ngroups; i++) {
397 (void)snprintf(buf, sizeof(buf), "%d", ki->p_groups[i]);
398 if (i)
399 fmt_putc(' ', &left);
400 for (p = &buf[0]; *p; p++)
401 fmt_putc(*p, &left);
402 }
403
404 if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
405 (void)printf("%*s", left, "");
406 }
407
408 void
409 groupnames(void *arg, VARENT *ve, int mode)
410 {
411 struct kinfo_proc2 *ki;
412 VAR *v;
413 int left, i;
414 const char *p;
415
416 if (mode == WIDTHMODE)
417 return;
418
419 ki = arg;
420 v = ve->var;
421 if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
422 if (SIMPLEQ_NEXT(ve, next) == NULL) {
423 left = termwidth - (totwidth - v->width);
424 if (left < 1) /* already wrapped, just use std width */
425 left = v->width;
426 } else
427 left = v->width;
428 } else
429 left = -1;
430
431 if (ki->p_ngroups == 0) {
432 fmt_putc('-', &left);
433 return;
434 }
435
436 for (i = 0; i < ki->p_ngroups; i++) {
437 if (i)
438 fmt_putc(' ', &left);
439 for (p = group_from_gid(ki->p_groups[i], 0); *p; p++)
440 fmt_putc(*p, &left);
441 }
442
443 if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
444 (void)printf("%*s", left, "");
445 }
446
447 void
448 ucomm(void *arg, VARENT *ve, int mode)
449 {
450 struct kinfo_proc2 *k;
451 VAR *v;
452
453 k = arg;
454 v = ve->var;
455 strprintorsetwidth(v, k->p_comm, mode);
456 }
457
458 void
459 emul(void *arg, VARENT *ve, int mode)
460 {
461 struct kinfo_proc2 *k;
462 VAR *v;
463
464 k = arg;
465 v = ve->var;
466 strprintorsetwidth(v, k->p_ename, mode);
467 }
468
469 void
470 logname(void *arg, VARENT *ve, int mode)
471 {
472 struct kinfo_proc2 *k;
473 VAR *v;
474
475 k = arg;
476 v = ve->var;
477 strprintorsetwidth(v, k->p_login, mode);
478 }
479
480 void
481 state(void *arg, VARENT *ve, int mode)
482 {
483 struct kinfo_proc2 *k;
484 int flag, is_zombie;
485 char *cp;
486 VAR *v;
487 char buf[16];
488
489 k = arg;
490 is_zombie = 0;
491 v = ve->var;
492 flag = k->p_flag;
493 cp = buf;
494
495 switch (k->p_stat) {
496
497 case LSSTOP:
498 *cp = 'T';
499 break;
500
501 case LSSLEEP:
502 if (flag & L_SINTR) /* interruptable (long) */
503 *cp = k->p_slptime >= maxslp ? 'I' : 'S';
504 else
505 *cp = 'D';
506 break;
507
508 case LSRUN:
509 case LSIDL:
510 case LSONPROC:
511 *cp = 'R';
512 break;
513
514 case LSZOMB:
515 *cp = 'Z';
516 is_zombie = 1;
517 break;
518
519 case LSSUSPENDED:
520 *cp = 'U';
521 break;
522
523 default:
524 *cp = '?';
525 }
526 cp++;
527 if (flag & L_INMEM) {
528 } else
529 *cp++ = 'W';
530 if (k->p_nice < NZERO)
531 *cp++ = '<';
532 else if (k->p_nice > NZERO)
533 *cp++ = 'N';
534 if (flag & P_TRACED)
535 *cp++ = 'X';
536 if (flag & P_SYSTRACE)
537 *cp++ = 'x';
538 if (flag & P_WEXIT && !is_zombie)
539 *cp++ = 'E';
540 if (flag & P_PPWAIT)
541 *cp++ = 'V';
542 if (flag & P_SYSTEM)
543 *cp++ = 'K';
544 /* system process might have this too, don't need to double up */
545 else if (k->p_holdcnt)
546 *cp++ = 'L';
547 if (k->p_eflag & EPROC_SLEADER)
548 *cp++ = 's';
549 if (flag & P_SA)
550 *cp++ = 'a';
551 else if (k->p_nlwps > 1)
552 *cp++ = 'l';
553 if ((flag & P_CONTROLT) && k->p__pgid == k->p_tpgid)
554 *cp++ = '+';
555 *cp = '\0';
556 strprintorsetwidth(v, buf, mode);
557 }
558
559 void
560 lstate(void *arg, VARENT *ve, int mode)
561 {
562 struct kinfo_lwp *k;
563 int flag, is_zombie;
564 char *cp;
565 VAR *v;
566 char buf[16];
567
568 k = arg;
569 is_zombie = 0;
570 v = ve->var;
571 flag = k->l_flag;
572 cp = buf;
573
574 switch (k->l_stat) {
575
576 case LSSTOP:
577 *cp = 'T';
578 break;
579
580 case LSSLEEP:
581 if (flag & L_SINTR) /* interruptible (long) */
582 *cp = k->l_slptime >= maxslp ? 'I' : 'S';
583 else
584 *cp = 'D';
585 break;
586
587 case LSRUN:
588 case LSIDL:
589 case LSONPROC:
590 *cp = 'R';
591 break;
592
593 case LSZOMB:
594 case LSDEAD:
595 *cp = 'Z';
596 is_zombie = 1;
597 break;
598
599 case LSSUSPENDED:
600 *cp = 'U';
601 break;
602
603 default:
604 *cp = '?';
605 }
606 cp++;
607 if (flag & L_INMEM) {
608 } else
609 *cp++ = 'W';
610 if (k->l_holdcnt)
611 *cp++ = 'L';
612 if (flag & L_DETACHED)
613 *cp++ = '-';
614 *cp = '\0';
615 strprintorsetwidth(v, buf, mode);
616 }
617
618 void
619 pnice(void *arg, VARENT *ve, int mode)
620 {
621 struct kinfo_proc2 *k;
622 VAR *v;
623
624 k = arg;
625 v = ve->var;
626 intprintorsetwidth(v, k->p_nice - NZERO, mode);
627 }
628
629 void
630 pri(void *arg, VARENT *ve, int mode)
631 {
632 struct kinfo_lwp *l;
633 VAR *v;
634
635 l = arg;
636 v = ve->var;
637 intprintorsetwidth(v, l->l_priority - PZERO, mode);
638 }
639
640 void
641 uname(void *arg, VARENT *ve, int mode)
642 {
643 struct kinfo_proc2 *k;
644 VAR *v;
645
646 k = arg;
647 v = ve->var;
648 strprintorsetwidth(v, user_from_uid(k->p_uid, 0), mode);
649 }
650
651 void
652 runame(void *arg, VARENT *ve, int mode)
653 {
654 struct kinfo_proc2 *k;
655 VAR *v;
656
657 k = arg;
658 v = ve->var;
659 strprintorsetwidth(v, user_from_uid(k->p_ruid, 0), mode);
660 }
661
662 void
663 svuname(void *arg, VARENT *ve, int mode)
664 {
665 struct kinfo_proc2 *k;
666 VAR *v;
667
668 k = arg;
669 v = ve->var;
670 strprintorsetwidth(v, user_from_uid(k->p_svuid, 0), mode);
671 }
672
673 void
674 gname(void *arg, VARENT *ve, int mode)
675 {
676 struct kinfo_proc2 *k;
677 VAR *v;
678
679 k = arg;
680 v = ve->var;
681 strprintorsetwidth(v, group_from_gid(k->p_gid, 0), mode);
682 }
683
684 void
685 rgname(void *arg, VARENT *ve, int mode)
686 {
687 struct kinfo_proc2 *k;
688 VAR *v;
689
690 k = arg;
691 v = ve->var;
692 strprintorsetwidth(v, group_from_gid(k->p_rgid, 0), mode);
693 }
694
695 void
696 svgname(void *arg, VARENT *ve, int mode)
697 {
698 struct kinfo_proc2 *k;
699 VAR *v;
700
701 k = arg;
702 v = ve->var;
703 strprintorsetwidth(v, group_from_gid(k->p_svgid, 0), mode);
704 }
705
706 void
707 tdev(void *arg, VARENT *ve, int mode)
708 {
709 struct kinfo_proc2 *k;
710 VAR *v;
711 dev_t dev;
712 char buff[16];
713
714 k = arg;
715 v = ve->var;
716 dev = k->p_tdev;
717 if (dev == NODEV) {
718 if (mode == PRINTMODE)
719 (void)printf("%*s", v->width, "?");
720 else
721 if (v->width < 2)
722 v->width = 2;
723 } else {
724 (void)snprintf(buff, sizeof(buff),
725 "%d/%d", major(dev), minor(dev));
726 strprintorsetwidth(v, buff, mode);
727 }
728 }
729
730 void
731 tname(void *arg, VARENT *ve, int mode)
732 {
733 struct kinfo_proc2 *k;
734 VAR *v;
735 dev_t dev;
736 const char *ttname;
737 int noctty;
738
739 k = arg;
740 v = ve->var;
741 dev = k->p_tdev;
742 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
743 if (mode == PRINTMODE)
744 (void)printf("%-*s", v->width, "?");
745 else
746 if (v->width < 2)
747 v->width = 2;
748 } else {
749 noctty = !(k->p_eflag & EPROC_CTTY) ? 1 : 0;
750 if (mode == WIDTHMODE) {
751 int fmtlen;
752
753 fmtlen = strlen(ttname) + noctty;
754 if (v->width < fmtlen)
755 v->width = fmtlen;
756 } else {
757 if (noctty)
758 (void)printf("%-*s-", v->width - 1, ttname);
759 else
760 (void)printf("%-*s", v->width, ttname);
761 }
762 }
763 }
764
765 void
766 longtname(void *arg, VARENT *ve, int mode)
767 {
768 struct kinfo_proc2 *k;
769 VAR *v;
770 dev_t dev;
771 const char *ttname;
772
773 k = arg;
774 v = ve->var;
775 dev = k->p_tdev;
776 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
777 if (mode == PRINTMODE)
778 (void)printf("%-*s", v->width, "?");
779 else
780 if (v->width < 2)
781 v->width = 2;
782 } else {
783 strprintorsetwidth(v, ttname, mode);
784 }
785 }
786
787 void
788 started(void *arg, VARENT *ve, int mode)
789 {
790 struct kinfo_proc2 *k;
791 VAR *v;
792 time_t startt;
793 struct tm *tp;
794 char buf[100], *cp;
795
796 k = arg;
797 v = ve->var;
798 if (!k->p_uvalid) {
799 if (mode == PRINTMODE)
800 (void)printf("%*s", v->width, "-");
801 return;
802 }
803
804 startt = k->p_ustart_sec;
805 tp = localtime(&startt);
806 if (now == 0)
807 (void)time(&now);
808 if (now - k->p_ustart_sec < SECSPERDAY)
809 /* I *hate* SCCS... */
810 (void)strftime(buf, sizeof(buf) - 1, "%l:%" "M%p", tp);
811 else if (now - k->p_ustart_sec < DAYSPERWEEK * SECSPERDAY)
812 /* I *hate* SCCS... */
813 (void)strftime(buf, sizeof(buf) - 1, "%a%" "I%p", tp);
814 else
815 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
816 /* %e and %l can start with a space. */
817 cp = buf;
818 if (*cp == ' ')
819 cp++;
820 strprintorsetwidth(v, cp, mode);
821 }
822
823 void
824 lstarted(void *arg, VARENT *ve, int mode)
825 {
826 struct kinfo_proc2 *k;
827 VAR *v;
828 time_t startt;
829 char buf[100];
830
831 k = arg;
832 v = ve->var;
833 if (!k->p_uvalid) {
834 /*
835 * Minimum width is less than header - we don't
836 * need to check it every time.
837 */
838 if (mode == PRINTMODE)
839 (void)printf("%*s", v->width, "-");
840 return;
841 }
842 startt = k->p_ustart_sec;
843
844 /* assume all times are the same length */
845 if (mode != WIDTHMODE || v->width == 0) {
846 (void)strftime(buf, sizeof(buf) -1, "%c",
847 localtime(&startt));
848 strprintorsetwidth(v, buf, mode);
849 }
850 }
851
852 void
853 elapsed(void *arg, VARENT *ve, int mode)
854 {
855 struct kinfo_proc2 *k;
856 VAR *v;
857 int32_t origseconds, secs, mins, hours, days;
858 int fmtlen, printed_something;
859
860 k = arg;
861 v = ve->var;
862 if (k->p_uvalid == 0) {
863 origseconds = 0;
864 } else {
865 if (now == 0)
866 (void)time(&now);
867 origseconds = now - k->p_ustart_sec;
868 if (origseconds < 0) {
869 /*
870 * Don't try to be fancy if the machine's
871 * clock has been rewound to before the
872 * process "started".
873 */
874 origseconds = 0;
875 }
876 }
877
878 secs = origseconds;
879 mins = secs / SECSPERMIN;
880 secs %= SECSPERMIN;
881 hours = mins / MINSPERHOUR;
882 mins %= MINSPERHOUR;
883 days = hours / HOURSPERDAY;
884 hours %= HOURSPERDAY;
885
886 if (mode == WIDTHMODE) {
887 if (origseconds == 0)
888 /* non-zero so fmtlen is calculated at least once */
889 origseconds = 1;
890
891 if (origseconds > v->longestp) {
892 v->longestp = origseconds;
893
894 if (days > 0) {
895 /* +9 for "-hh:mm:ss" */
896 fmtlen = iwidth(days) + 9;
897 } else if (hours > 0) {
898 /* +6 for "mm:ss" */
899 fmtlen = iwidth(hours) + 6;
900 } else {
901 /* +3 for ":ss" */
902 fmtlen = iwidth(mins) + 3;
903 }
904
905 if (fmtlen > v->width)
906 v->width = fmtlen;
907 }
908 } else {
909 printed_something = 0;
910 fmtlen = v->width;
911
912 if (days > 0) {
913 (void)printf("%*d", fmtlen - 9, days);
914 printed_something = 1;
915 } else if (fmtlen > 9) {
916 (void)printf("%*s", fmtlen - 9, "");
917 }
918 if (fmtlen > 9)
919 fmtlen = 9;
920
921 if (printed_something) {
922 (void)printf("-%.*d", fmtlen - 7, hours);
923 printed_something = 1;
924 } else if (hours > 0) {
925 (void)printf("%*d", fmtlen - 6, hours);
926 printed_something = 1;
927 } else if (fmtlen > 6) {
928 (void)printf("%*s", fmtlen - 6, "");
929 }
930 if (fmtlen > 6)
931 fmtlen = 6;
932
933 /* Don't need to set fmtlen or printed_something any more... */
934 if (printed_something) {
935 (void)printf(":%.*d", fmtlen - 4, mins);
936 } else if (mins > 0) {
937 (void)printf("%*d", fmtlen - 3, mins);
938 } else if (fmtlen > 3) {
939 (void)printf("%*s", fmtlen - 3, "0");
940 }
941
942 (void)printf(":%.2d", secs);
943 }
944 }
945
946 void
947 wchan(void *arg, VARENT *ve, int mode)
948 {
949 struct kinfo_lwp *l;
950 VAR *v;
951 char *buf;
952
953 l = arg;
954 v = ve->var;
955 if (l->l_wchan) {
956 if (l->l_wmesg) {
957 strprintorsetwidth(v, l->l_wmesg, mode);
958 v->width = min(v->width, KI_WMESGLEN);
959 } else {
960 (void)asprintf(&buf, "%-*" PRIx64, v->width,
961 l->l_wchan);
962 if (buf == NULL)
963 err(1, "%s", "");
964 strprintorsetwidth(v, buf, mode);
965 v->width = min(v->width, KI_WMESGLEN);
966 free(buf);
967 }
968 } else {
969 if (mode == PRINTMODE)
970 (void)printf("%-*s", v->width, "-");
971 }
972 }
973
974 #define pgtok(a) (((a)*getpagesize())/1024)
975
976 void
977 vsize(void *arg, VARENT *ve, int mode)
978 {
979 struct kinfo_proc2 *k;
980 VAR *v;
981
982 k = arg;
983 v = ve->var;
984 intprintorsetwidth(v,
985 pgtok(k->p_vm_dsize + k->p_vm_ssize + k->p_vm_tsize), mode);
986 }
987
988 void
989 rssize(void *arg, VARENT *ve, int mode)
990 {
991 struct kinfo_proc2 *k;
992 VAR *v;
993
994 k = arg;
995 v = ve->var;
996 /* XXX don't have info about shared */
997 intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
998 }
999
1000 void
1001 p_rssize(void *arg, VARENT *ve, int mode) /* doesn't account for text */
1002 {
1003 struct kinfo_proc2 *k;
1004 VAR *v;
1005
1006 k = arg;
1007 v = ve->var;
1008 intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
1009 }
1010
1011 void
1012 cputime(void *arg, VARENT *ve, int mode)
1013 {
1014 struct kinfo_proc2 *k;
1015 VAR *v;
1016 int32_t secs;
1017 int32_t psecs; /* "parts" of a second. first micro, then centi */
1018 int fmtlen;
1019
1020 k = arg;
1021 v = ve->var;
1022 if (P_ZOMBIE(k) || k->p_uvalid == 0) {
1023 secs = 0;
1024 psecs = 0;
1025 } else {
1026 /*
1027 * This counts time spent handling interrupts. We could
1028 * fix this, but it is not 100% trivial (and interrupt
1029 * time fractions only work on the sparc anyway). XXX
1030 */
1031 secs = k->p_rtime_sec;
1032 psecs = k->p_rtime_usec;
1033 if (sumrusage) {
1034 secs += k->p_uctime_sec;
1035 psecs += k->p_uctime_usec;
1036 }
1037 /*
1038 * round and scale to 100's
1039 */
1040 psecs = (psecs + 5000) / 10000;
1041 secs += psecs / 100;
1042 psecs = psecs % 100;
1043 }
1044 if (mode == WIDTHMODE) {
1045 /*
1046 * Ugg, this is the only field where a value of 0 is longer
1047 * than the column title.
1048 * Use SECSPERMIN, because secs is divided by that when
1049 * passed to iwidth().
1050 */
1051 if (secs == 0)
1052 secs = SECSPERMIN;
1053
1054 if (secs > v->longestp) {
1055 v->longestp = secs;
1056 /* "+6" for the ":%02ld.%02ld" in the printf() below */
1057 fmtlen = iwidth(secs / SECSPERMIN) + 6;
1058 if (fmtlen > v->width)
1059 v->width = fmtlen;
1060 }
1061 } else {
1062 (void)printf("%*ld:%02ld.%02ld", v->width - 6,
1063 (long)(secs / SECSPERMIN), (long)(secs % SECSPERMIN),
1064 (long)psecs);
1065 }
1066 }
1067
1068 double
1069 getpcpu(k)
1070 const struct kinfo_proc2 *k;
1071 {
1072 static int failure;
1073
1074 if (!nlistread)
1075 failure = (kd) ? donlist() : 1;
1076 if (failure)
1077 return (0.0);
1078
1079 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
1080
1081 /* XXX - I don't like this */
1082 if (k->p_swtime == 0 || (k->p_flag & L_INMEM) == 0 ||
1083 k->p_stat == SZOMB)
1084 return (0.0);
1085 if (rawcpu)
1086 return (100.0 * fxtofl(k->p_pctcpu));
1087 return (100.0 * fxtofl(k->p_pctcpu) /
1088 (1.0 - exp(k->p_swtime * log(ccpu))));
1089 }
1090
1091 void
1092 pcpu(void *arg, VARENT *ve, int mode)
1093 {
1094 struct kinfo_proc2 *k;
1095 VAR *v;
1096
1097 k = arg;
1098 v = ve->var;
1099 doubleprintorsetwidth(v, getpcpu(k), 1, mode);
1100 }
1101
1102 double
1103 getpmem(k)
1104 const struct kinfo_proc2 *k;
1105 {
1106 static int failure;
1107 double fracmem;
1108 int szptudot;
1109
1110 if (!nlistread)
1111 failure = (kd) ? donlist() : 1;
1112 if (failure)
1113 return (0.0);
1114
1115 if ((k->p_flag & L_INMEM) == 0)
1116 return (0.0);
1117 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
1118 szptudot = uspace/getpagesize();
1119 /* XXX don't have info about shared */
1120 fracmem = ((float)k->p_vm_rssize + szptudot)/mempages;
1121 return (100.0 * fracmem);
1122 }
1123
1124 void
1125 pmem(void *arg, VARENT *ve, int mode)
1126 {
1127 struct kinfo_proc2 *k;
1128 VAR *v;
1129
1130 k = arg;
1131 v = ve->var;
1132 doubleprintorsetwidth(v, getpmem(k), 1, mode);
1133 }
1134
1135 void
1136 pagein(void *arg, VARENT *ve, int mode)
1137 {
1138 struct kinfo_proc2 *k;
1139 VAR *v;
1140
1141 k = arg;
1142 v = ve->var;
1143 intprintorsetwidth(v, k->p_uvalid ? k->p_uru_majflt : 0, mode);
1144 }
1145
1146 void
1147 maxrss(void *arg, VARENT *ve, int mode)
1148 {
1149 VAR *v;
1150
1151 v = ve->var;
1152 /* No need to check width! */
1153 if (mode == PRINTMODE)
1154 (void)printf("%*s", v->width, "-");
1155 }
1156
1157 void
1158 tsize(void *arg, VARENT *ve, int mode)
1159 {
1160 struct kinfo_proc2 *k;
1161 VAR *v;
1162
1163 k = arg;
1164 v = ve->var;
1165 intprintorsetwidth(v, pgtok(k->p_vm_tsize), mode);
1166 }
1167
1168 /*
1169 * Generic output routines. Print fields from various prototype
1170 * structures.
1171 */
1172 static void
1173 printval(bp, v, mode)
1174 void *bp;
1175 VAR *v;
1176 int mode;
1177 {
1178 static char ofmt[32] = "%";
1179 int width, vok, fmtlen;
1180 const char *fcp;
1181 char *cp;
1182 int64_t val;
1183 u_int64_t uval;
1184
1185 val = 0; /* XXXGCC -Wuninitialized [hpcarm] */
1186 uval = 0; /* XXXGCC -Wuninitialized [hpcarm] */
1187
1188 /*
1189 * Note that the "INF127" check is nonsensical for types
1190 * that are or can be signed.
1191 */
1192 #define GET(type) (*(type *)bp)
1193 #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
1194
1195 #define VSIGN 1
1196 #define VUNSIGN 2
1197 #define VPTR 3
1198
1199 if (mode == WIDTHMODE) {
1200 vok = 0;
1201 switch (v->type) {
1202 case CHAR:
1203 val = GET(char);
1204 vok = VSIGN;
1205 break;
1206 case UCHAR:
1207 uval = CHK_INF127(GET(u_char));
1208 vok = VUNSIGN;
1209 break;
1210 case SHORT:
1211 val = GET(short);
1212 vok = VSIGN;
1213 break;
1214 case USHORT:
1215 uval = CHK_INF127(GET(u_short));
1216 vok = VUNSIGN;
1217 break;
1218 case INT32:
1219 val = GET(int32_t);
1220 vok = VSIGN;
1221 break;
1222 case INT:
1223 val = GET(int);
1224 vok = VSIGN;
1225 break;
1226 case UINT:
1227 case UINT32:
1228 uval = CHK_INF127(GET(u_int));
1229 vok = VUNSIGN;
1230 break;
1231 case LONG:
1232 val = GET(long);
1233 vok = VSIGN;
1234 break;
1235 case ULONG:
1236 uval = CHK_INF127(GET(u_long));
1237 vok = VUNSIGN;
1238 break;
1239 case KPTR:
1240 uval = GET(u_int64_t);
1241 vok = VPTR;
1242 break;
1243 case KPTR24:
1244 uval = GET(u_int64_t);
1245 uval &= 0xffffff;
1246 vok = VPTR;
1247 break;
1248 case INT64:
1249 val = GET(int64_t);
1250 vok = VSIGN;
1251 break;
1252 case UINT64:
1253 uval = CHK_INF127(GET(u_int64_t));
1254 vok = VUNSIGN;
1255 break;
1256
1257 case SIGLIST:
1258 default:
1259 /* nothing... */;
1260 }
1261 switch (vok) {
1262 case VSIGN:
1263 if (val < 0 && val < v->longestn) {
1264 v->longestn = val;
1265 fmtlen = iwidth(-val) + 1;
1266 if (fmtlen > v->width)
1267 v->width = fmtlen;
1268 } else if (val > 0 && val > v->longestp) {
1269 v->longestp = val;
1270 fmtlen = iwidth(val);
1271 if (fmtlen > v->width)
1272 v->width = fmtlen;
1273 }
1274 return;
1275 case VUNSIGN:
1276 if (uval > v->longestu) {
1277 v->longestu = uval;
1278 v->width = iwidth(uval);
1279 }
1280 return;
1281 case VPTR:
1282 fmtlen = 0;
1283 while (uval > 0) {
1284 uval >>= 4;
1285 fmtlen++;
1286 }
1287 if (fmtlen > v->width)
1288 v->width = fmtlen;
1289 return;
1290 }
1291 }
1292
1293 width = v->width;
1294 cp = ofmt + 1;
1295 fcp = v->fmt;
1296 if (v->flag & LJUST)
1297 *cp++ = '-';
1298 *cp++ = '*';
1299 while ((*cp++ = *fcp++) != '\0')
1300 continue;
1301
1302 switch (v->type) {
1303 case CHAR:
1304 (void)printf(ofmt, width, GET(char));
1305 return;
1306 case UCHAR:
1307 (void)printf(ofmt, width, CHK_INF127(GET(u_char)));
1308 return;
1309 case SHORT:
1310 (void)printf(ofmt, width, GET(short));
1311 return;
1312 case USHORT:
1313 (void)printf(ofmt, width, CHK_INF127(GET(u_short)));
1314 return;
1315 case INT:
1316 (void)printf(ofmt, width, GET(int));
1317 return;
1318 case UINT:
1319 (void)printf(ofmt, width, CHK_INF127(GET(u_int)));
1320 return;
1321 case LONG:
1322 (void)printf(ofmt, width, GET(long));
1323 return;
1324 case ULONG:
1325 (void)printf(ofmt, width, CHK_INF127(GET(u_long)));
1326 return;
1327 case KPTR:
1328 (void)printf(ofmt, width, GET(u_int64_t));
1329 return;
1330 case KPTR24:
1331 (void)printf(ofmt, width, GET(u_int64_t) & 0xffffff);
1332 return;
1333 case INT32:
1334 (void)printf(ofmt, width, GET(int32_t));
1335 return;
1336 case UINT32:
1337 (void)printf(ofmt, width, CHK_INF127(GET(u_int32_t)));
1338 return;
1339 case SIGLIST:
1340 {
1341 sigset_t *s = (sigset_t *)(void *)bp;
1342 size_t i;
1343 #define SIGSETSIZE (sizeof(s->__bits) / sizeof(s->__bits[0]))
1344 char buf[SIGSETSIZE * 8 + 1];
1345
1346 for (i = 0; i < SIGSETSIZE; i++)
1347 (void)snprintf(&buf[i * 8], 9, "%.8x",
1348 s->__bits[(SIGSETSIZE - 1) - i]);
1349
1350 /* Skip leading zeroes */
1351 for (i = 0; buf[i] == '0'; i++)
1352 continue;
1353
1354 if (buf[i] == '\0')
1355 i--;
1356 strprintorsetwidth(v, buf + i, mode);
1357 #undef SIGSETSIZE
1358 }
1359 return;
1360 case INT64:
1361 (void)printf(ofmt, width, GET(int64_t));
1362 return;
1363 case UINT64:
1364 (void)printf(ofmt, width, CHK_INF127(GET(u_int64_t)));
1365 return;
1366 default:
1367 errx(1, "unknown type %d", v->type);
1368 }
1369 #undef GET
1370 #undef CHK_INF127
1371 }
1372
1373 void
1374 pvar(void *arg, VARENT *ve, int mode)
1375 {
1376 VAR *v;
1377
1378 v = ve->var;
1379 if (v->flag & UAREA && !((struct kinfo_proc2 *)arg)->p_uvalid) {
1380 if (mode == PRINTMODE)
1381 (void)printf("%*s", v->width, "-");
1382 return;
1383 }
1384
1385 (void)printval((char *)arg + v->off, v, mode);
1386 }
1387
1388 void
1389 putimeval(void *arg, VARENT *ve, int mode)
1390 {
1391 VAR *v = ve->var;
1392 struct kinfo_proc2 *k = arg;
1393 ulong secs = *(uint32_t *)((char *)arg + v->off);
1394 ulong usec = *(uint32_t *)((char *)arg + v->off + sizeof (uint32_t));
1395 int fmtlen;
1396
1397 if (!k->p_uvalid) {
1398 if (mode == PRINTMODE)
1399 (void)printf("%*s", v->width, "-");
1400 return;
1401 }
1402
1403 if (mode == WIDTHMODE) {
1404 if (secs == 0)
1405 /* non-zero so fmtlen is calculated at least once */
1406 secs = 1;
1407 if (secs > v->longestu) {
1408 v->longestu = secs;
1409 if (secs <= 999)
1410 /* sss.ssssss */
1411 fmtlen = iwidth(secs) + 6 + 1;
1412 else
1413 /* hh:mm:ss.ss */
1414 fmtlen = iwidth((secs + 1) / SECSPERHOUR)
1415 + 2 + 1 + 2 + 1 + 2 + 1;
1416 if (fmtlen > v->width)
1417 v->width = fmtlen;
1418 }
1419 return;
1420 }
1421
1422 if (secs < 999)
1423 (void)printf( "%*lu.%.6lu", v->width - 6 - 1, secs, usec);
1424 else {
1425 uint h, m;
1426 usec += 5000;
1427 if (usec >= 1000000) {
1428 usec -= 1000000;
1429 secs++;
1430 }
1431 m = secs / SECSPERMIN;
1432 secs -= m * SECSPERMIN;
1433 h = m / MINSPERHOUR;
1434 m -= h * MINSPERHOUR;
1435 (void)printf( "%*u:%.2u:%.2lu.%.2lu", v->width - 9, h, m, secs,
1436 usec / 10000u );
1437 }
1438 }
1439