print.c revision 1.96.2.1 1 /* $NetBSD: print.c,v 1.96.2.1 2007/07/30 12:47:55 liamjfoy Exp $ */
2
3 /*
4 * Copyright (c) 2000 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.96.2.1 2007/07/30 12:47:55 liamjfoy 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 case LSDEAD:
516 *cp = 'Z';
517 is_zombie = 1;
518 break;
519
520 case LSSUSPENDED:
521 *cp = 'U';
522 break;
523
524 default:
525 *cp = '?';
526 }
527 cp++;
528 if (flag & L_INMEM) {
529 } else
530 *cp++ = 'W';
531 if (k->p_nice < NZERO)
532 *cp++ = '<';
533 else if (k->p_nice > NZERO)
534 *cp++ = 'N';
535 if (flag & P_TRACED)
536 *cp++ = 'X';
537 if (flag & P_SYSTRACE)
538 *cp++ = 'x';
539 if (flag & P_WEXIT && !is_zombie)
540 *cp++ = 'E';
541 if (flag & P_PPWAIT)
542 *cp++ = 'V';
543 if (flag & P_SYSTEM)
544 *cp++ = 'K';
545 /* system process might have this too, don't need to double up */
546 else if (k->p_holdcnt)
547 *cp++ = 'L';
548 if (k->p_eflag & EPROC_SLEADER)
549 *cp++ = 's';
550 if (flag & P_SA)
551 *cp++ = 'a';
552 else if (k->p_nlwps > 1)
553 *cp++ = 'l';
554 if ((flag & P_CONTROLT) && k->p__pgid == k->p_tpgid)
555 *cp++ = '+';
556 *cp = '\0';
557 strprintorsetwidth(v, buf, mode);
558 }
559
560 void
561 lstate(void *arg, VARENT *ve, int mode)
562 {
563 struct kinfo_lwp *k;
564 int flag, is_zombie;
565 char *cp;
566 VAR *v;
567 char buf[16];
568
569 k = arg;
570 is_zombie = 0;
571 v = ve->var;
572 flag = k->l_flag;
573 cp = buf;
574
575 switch (k->l_stat) {
576
577 case LSSTOP:
578 *cp = 'T';
579 break;
580
581 case LSSLEEP:
582 if (flag & L_SINTR) /* interruptible (long) */
583 *cp = k->l_slptime >= maxslp ? 'I' : 'S';
584 else
585 *cp = 'D';
586 break;
587
588 case LSRUN:
589 case LSIDL:
590 case LSONPROC:
591 *cp = 'R';
592 break;
593
594 case LSZOMB:
595 case LSDEAD:
596 *cp = 'Z';
597 is_zombie = 1;
598 break;
599
600 case LSSUSPENDED:
601 *cp = 'U';
602 break;
603
604 default:
605 *cp = '?';
606 }
607 cp++;
608 if (flag & L_INMEM) {
609 } else
610 *cp++ = 'W';
611 if (k->l_holdcnt)
612 *cp++ = 'L';
613 if (flag & L_DETACHED)
614 *cp++ = '-';
615 *cp = '\0';
616 strprintorsetwidth(v, buf, mode);
617 }
618
619 void
620 pnice(void *arg, VARENT *ve, int mode)
621 {
622 struct kinfo_proc2 *k;
623 VAR *v;
624
625 k = arg;
626 v = ve->var;
627 intprintorsetwidth(v, k->p_nice - NZERO, mode);
628 }
629
630 void
631 pri(void *arg, VARENT *ve, int mode)
632 {
633 struct kinfo_lwp *l;
634 VAR *v;
635
636 l = arg;
637 v = ve->var;
638 intprintorsetwidth(v, l->l_priority - PZERO, mode);
639 }
640
641 void
642 uname(void *arg, VARENT *ve, int mode)
643 {
644 struct kinfo_proc2 *k;
645 VAR *v;
646
647 k = arg;
648 v = ve->var;
649 strprintorsetwidth(v, user_from_uid(k->p_uid, 0), mode);
650 }
651
652 void
653 runame(void *arg, VARENT *ve, int mode)
654 {
655 struct kinfo_proc2 *k;
656 VAR *v;
657
658 k = arg;
659 v = ve->var;
660 strprintorsetwidth(v, user_from_uid(k->p_ruid, 0), mode);
661 }
662
663 void
664 svuname(void *arg, VARENT *ve, int mode)
665 {
666 struct kinfo_proc2 *k;
667 VAR *v;
668
669 k = arg;
670 v = ve->var;
671 strprintorsetwidth(v, user_from_uid(k->p_svuid, 0), mode);
672 }
673
674 void
675 gname(void *arg, VARENT *ve, int mode)
676 {
677 struct kinfo_proc2 *k;
678 VAR *v;
679
680 k = arg;
681 v = ve->var;
682 strprintorsetwidth(v, group_from_gid(k->p_gid, 0), mode);
683 }
684
685 void
686 rgname(void *arg, VARENT *ve, int mode)
687 {
688 struct kinfo_proc2 *k;
689 VAR *v;
690
691 k = arg;
692 v = ve->var;
693 strprintorsetwidth(v, group_from_gid(k->p_rgid, 0), mode);
694 }
695
696 void
697 svgname(void *arg, VARENT *ve, int mode)
698 {
699 struct kinfo_proc2 *k;
700 VAR *v;
701
702 k = arg;
703 v = ve->var;
704 strprintorsetwidth(v, group_from_gid(k->p_svgid, 0), mode);
705 }
706
707 void
708 tdev(void *arg, VARENT *ve, int mode)
709 {
710 struct kinfo_proc2 *k;
711 VAR *v;
712 dev_t dev;
713 char buff[16];
714
715 k = arg;
716 v = ve->var;
717 dev = k->p_tdev;
718 if (dev == NODEV) {
719 if (mode == PRINTMODE)
720 (void)printf("%*s", v->width, "?");
721 else
722 if (v->width < 2)
723 v->width = 2;
724 } else {
725 (void)snprintf(buff, sizeof(buff),
726 "%d/%d", major(dev), minor(dev));
727 strprintorsetwidth(v, buff, mode);
728 }
729 }
730
731 void
732 tname(void *arg, VARENT *ve, int mode)
733 {
734 struct kinfo_proc2 *k;
735 VAR *v;
736 dev_t dev;
737 const char *ttname;
738 int noctty;
739
740 k = arg;
741 v = ve->var;
742 dev = k->p_tdev;
743 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
744 if (mode == PRINTMODE)
745 (void)printf("%-*s", v->width, "?");
746 else
747 if (v->width < 2)
748 v->width = 2;
749 } else {
750 noctty = !(k->p_eflag & EPROC_CTTY) ? 1 : 0;
751 if (mode == WIDTHMODE) {
752 int fmtlen;
753
754 fmtlen = strlen(ttname) + noctty;
755 if (v->width < fmtlen)
756 v->width = fmtlen;
757 } else {
758 if (noctty)
759 (void)printf("%-*s-", v->width - 1, ttname);
760 else
761 (void)printf("%-*s", v->width, ttname);
762 }
763 }
764 }
765
766 void
767 longtname(void *arg, VARENT *ve, int mode)
768 {
769 struct kinfo_proc2 *k;
770 VAR *v;
771 dev_t dev;
772 const char *ttname;
773
774 k = arg;
775 v = ve->var;
776 dev = k->p_tdev;
777 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
778 if (mode == PRINTMODE)
779 (void)printf("%-*s", v->width, "?");
780 else
781 if (v->width < 2)
782 v->width = 2;
783 } else {
784 strprintorsetwidth(v, ttname, mode);
785 }
786 }
787
788 void
789 started(void *arg, VARENT *ve, int mode)
790 {
791 struct kinfo_proc2 *k;
792 VAR *v;
793 time_t startt;
794 struct tm *tp;
795 char buf[100], *cp;
796
797 k = arg;
798 v = ve->var;
799 if (!k->p_uvalid) {
800 if (mode == PRINTMODE)
801 (void)printf("%*s", v->width, "-");
802 return;
803 }
804
805 startt = k->p_ustart_sec;
806 tp = localtime(&startt);
807 if (now == 0)
808 (void)time(&now);
809 if (now - k->p_ustart_sec < SECSPERDAY)
810 /* I *hate* SCCS... */
811 (void)strftime(buf, sizeof(buf) - 1, "%l:%" "M%p", tp);
812 else if (now - k->p_ustart_sec < DAYSPERWEEK * SECSPERDAY)
813 /* I *hate* SCCS... */
814 (void)strftime(buf, sizeof(buf) - 1, "%a%" "I%p", tp);
815 else
816 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
817 /* %e and %l can start with a space. */
818 cp = buf;
819 if (*cp == ' ')
820 cp++;
821 strprintorsetwidth(v, cp, mode);
822 }
823
824 void
825 lstarted(void *arg, VARENT *ve, int mode)
826 {
827 struct kinfo_proc2 *k;
828 VAR *v;
829 time_t startt;
830 char buf[100];
831
832 k = arg;
833 v = ve->var;
834 if (!k->p_uvalid) {
835 /*
836 * Minimum width is less than header - we don't
837 * need to check it every time.
838 */
839 if (mode == PRINTMODE)
840 (void)printf("%*s", v->width, "-");
841 return;
842 }
843 startt = k->p_ustart_sec;
844
845 /* assume all times are the same length */
846 if (mode != WIDTHMODE || v->width == 0) {
847 (void)strftime(buf, sizeof(buf) -1, "%c",
848 localtime(&startt));
849 strprintorsetwidth(v, buf, mode);
850 }
851 }
852
853 void
854 elapsed(void *arg, VARENT *ve, int mode)
855 {
856 struct kinfo_proc2 *k;
857 VAR *v;
858 int32_t origseconds, secs, mins, hours, days;
859 int fmtlen, printed_something;
860
861 k = arg;
862 v = ve->var;
863 if (k->p_uvalid == 0) {
864 origseconds = 0;
865 } else {
866 if (now == 0)
867 (void)time(&now);
868 origseconds = now - k->p_ustart_sec;
869 if (origseconds < 0) {
870 /*
871 * Don't try to be fancy if the machine's
872 * clock has been rewound to before the
873 * process "started".
874 */
875 origseconds = 0;
876 }
877 }
878
879 secs = origseconds;
880 mins = secs / SECSPERMIN;
881 secs %= SECSPERMIN;
882 hours = mins / MINSPERHOUR;
883 mins %= MINSPERHOUR;
884 days = hours / HOURSPERDAY;
885 hours %= HOURSPERDAY;
886
887 if (mode == WIDTHMODE) {
888 if (origseconds == 0)
889 /* non-zero so fmtlen is calculated at least once */
890 origseconds = 1;
891
892 if (origseconds > v->longestp) {
893 v->longestp = origseconds;
894
895 if (days > 0) {
896 /* +9 for "-hh:mm:ss" */
897 fmtlen = iwidth(days) + 9;
898 } else if (hours > 0) {
899 /* +6 for "mm:ss" */
900 fmtlen = iwidth(hours) + 6;
901 } else {
902 /* +3 for ":ss" */
903 fmtlen = iwidth(mins) + 3;
904 }
905
906 if (fmtlen > v->width)
907 v->width = fmtlen;
908 }
909 } else {
910 printed_something = 0;
911 fmtlen = v->width;
912
913 if (days > 0) {
914 (void)printf("%*d", fmtlen - 9, days);
915 printed_something = 1;
916 } else if (fmtlen > 9) {
917 (void)printf("%*s", fmtlen - 9, "");
918 }
919 if (fmtlen > 9)
920 fmtlen = 9;
921
922 if (printed_something) {
923 (void)printf("-%.*d", fmtlen - 7, hours);
924 printed_something = 1;
925 } else if (hours > 0) {
926 (void)printf("%*d", fmtlen - 6, hours);
927 printed_something = 1;
928 } else if (fmtlen > 6) {
929 (void)printf("%*s", fmtlen - 6, "");
930 }
931 if (fmtlen > 6)
932 fmtlen = 6;
933
934 /* Don't need to set fmtlen or printed_something any more... */
935 if (printed_something) {
936 (void)printf(":%.*d", fmtlen - 4, mins);
937 } else if (mins > 0) {
938 (void)printf("%*d", fmtlen - 3, mins);
939 } else if (fmtlen > 3) {
940 (void)printf("%*s", fmtlen - 3, "0");
941 }
942
943 (void)printf(":%.2d", secs);
944 }
945 }
946
947 void
948 wchan(void *arg, VARENT *ve, int mode)
949 {
950 struct kinfo_lwp *l;
951 VAR *v;
952 char *buf;
953
954 l = arg;
955 v = ve->var;
956 if (l->l_wchan) {
957 if (l->l_wmesg) {
958 strprintorsetwidth(v, l->l_wmesg, mode);
959 v->width = min(v->width, KI_WMESGLEN);
960 } else {
961 (void)asprintf(&buf, "%-*" PRIx64, v->width,
962 l->l_wchan);
963 if (buf == NULL)
964 err(1, "%s", "");
965 strprintorsetwidth(v, buf, mode);
966 v->width = min(v->width, KI_WMESGLEN);
967 free(buf);
968 }
969 } else {
970 if (mode == PRINTMODE)
971 (void)printf("%-*s", v->width, "-");
972 }
973 }
974
975 #define pgtok(a) (((a)*getpagesize())/1024)
976
977 void
978 vsize(void *arg, VARENT *ve, int mode)
979 {
980 struct kinfo_proc2 *k;
981 VAR *v;
982
983 k = arg;
984 v = ve->var;
985 intprintorsetwidth(v,
986 pgtok(k->p_vm_dsize + k->p_vm_ssize + k->p_vm_tsize), mode);
987 }
988
989 void
990 rssize(void *arg, VARENT *ve, int mode)
991 {
992 struct kinfo_proc2 *k;
993 VAR *v;
994
995 k = arg;
996 v = ve->var;
997 /* XXX don't have info about shared */
998 intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
999 }
1000
1001 void
1002 p_rssize(void *arg, VARENT *ve, int mode) /* doesn't account for text */
1003 {
1004 struct kinfo_proc2 *k;
1005 VAR *v;
1006
1007 k = arg;
1008 v = ve->var;
1009 intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
1010 }
1011
1012 void
1013 cputime(void *arg, VARENT *ve, int mode)
1014 {
1015 struct kinfo_proc2 *k;
1016 VAR *v;
1017 int32_t secs;
1018 int32_t psecs; /* "parts" of a second. first micro, then centi */
1019 int fmtlen;
1020
1021 k = arg;
1022 v = ve->var;
1023
1024 /*
1025 * This counts time spent handling interrupts. We could
1026 * fix this, but it is not 100% trivial (and interrupt
1027 * time fractions only work on the sparc anyway). XXX
1028 */
1029 secs = k->p_rtime_sec;
1030 psecs = k->p_rtime_usec;
1031 if (sumrusage) {
1032 secs += k->p_uctime_sec;
1033 psecs += k->p_uctime_usec;
1034 }
1035 /*
1036 * round and scale to 100's
1037 */
1038 psecs = (psecs + 5000) / 10000;
1039 secs += psecs / 100;
1040 psecs = psecs % 100;
1041
1042 if (mode == WIDTHMODE) {
1043 /*
1044 * Ugg, this is the only field where a value of 0 is longer
1045 * than the column title.
1046 * Use SECSPERMIN, because secs is divided by that when
1047 * passed to iwidth().
1048 */
1049 if (secs == 0)
1050 secs = SECSPERMIN;
1051
1052 if (secs > v->longestp) {
1053 v->longestp = secs;
1054 /* "+6" for the ":%02ld.%02ld" in the printf() below */
1055 fmtlen = iwidth(secs / SECSPERMIN) + 6;
1056 if (fmtlen > v->width)
1057 v->width = fmtlen;
1058 }
1059 } else {
1060 (void)printf("%*ld:%02ld.%02ld", v->width - 6,
1061 (long)(secs / SECSPERMIN), (long)(secs % SECSPERMIN),
1062 (long)psecs);
1063 }
1064 }
1065
1066 double
1067 getpcpu(k)
1068 const struct kinfo_proc2 *k;
1069 {
1070 static int failure;
1071
1072 if (!nlistread)
1073 failure = (kd) ? donlist() : 1;
1074 if (failure)
1075 return (0.0);
1076
1077 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
1078
1079 /* XXX - I don't like this */
1080 if (k->p_swtime == 0 || (k->p_flag & L_INMEM) == 0 ||
1081 k->p_realstat == SZOMB)
1082 return (0.0);
1083 if (rawcpu)
1084 return (100.0 * fxtofl(k->p_pctcpu));
1085 return (100.0 * fxtofl(k->p_pctcpu) /
1086 (1.0 - exp(k->p_swtime * log(ccpu))));
1087 }
1088
1089 void
1090 pcpu(void *arg, VARENT *ve, int mode)
1091 {
1092 struct kinfo_proc2 *k;
1093 VAR *v;
1094
1095 k = arg;
1096 v = ve->var;
1097 doubleprintorsetwidth(v, getpcpu(k), 1, mode);
1098 }
1099
1100 double
1101 getpmem(k)
1102 const struct kinfo_proc2 *k;
1103 {
1104 static int failure;
1105 double fracmem;
1106 int szptudot;
1107
1108 if (!nlistread)
1109 failure = (kd) ? donlist() : 1;
1110 if (failure)
1111 return (0.0);
1112
1113 if ((k->p_flag & L_INMEM) == 0)
1114 return (0.0);
1115 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
1116 szptudot = uspace/getpagesize();
1117 /* XXX don't have info about shared */
1118 fracmem = ((float)k->p_vm_rssize + szptudot)/mempages;
1119 return (100.0 * fracmem);
1120 }
1121
1122 void
1123 pmem(void *arg, VARENT *ve, int mode)
1124 {
1125 struct kinfo_proc2 *k;
1126 VAR *v;
1127
1128 k = arg;
1129 v = ve->var;
1130 doubleprintorsetwidth(v, getpmem(k), 1, mode);
1131 }
1132
1133 void
1134 pagein(void *arg, VARENT *ve, int mode)
1135 {
1136 struct kinfo_proc2 *k;
1137 VAR *v;
1138
1139 k = arg;
1140 v = ve->var;
1141 intprintorsetwidth(v, k->p_uvalid ? k->p_uru_majflt : 0, mode);
1142 }
1143
1144 void
1145 maxrss(void *arg, VARENT *ve, int mode)
1146 {
1147 VAR *v;
1148
1149 v = ve->var;
1150 /* No need to check width! */
1151 if (mode == PRINTMODE)
1152 (void)printf("%*s", v->width, "-");
1153 }
1154
1155 void
1156 tsize(void *arg, VARENT *ve, int mode)
1157 {
1158 struct kinfo_proc2 *k;
1159 VAR *v;
1160
1161 k = arg;
1162 v = ve->var;
1163 intprintorsetwidth(v, pgtok(k->p_vm_tsize), mode);
1164 }
1165
1166 /*
1167 * Generic output routines. Print fields from various prototype
1168 * structures.
1169 */
1170 static void
1171 printval(bp, v, mode)
1172 void *bp;
1173 VAR *v;
1174 int mode;
1175 {
1176 static char ofmt[32] = "%";
1177 int width, vok, fmtlen;
1178 const char *fcp;
1179 char *cp;
1180 int64_t val;
1181 u_int64_t uval;
1182
1183 val = 0; /* XXXGCC -Wuninitialized [hpcarm] */
1184 uval = 0; /* XXXGCC -Wuninitialized [hpcarm] */
1185
1186 /*
1187 * Note that the "INF127" check is nonsensical for types
1188 * that are or can be signed.
1189 */
1190 #define GET(type) (*(type *)bp)
1191 #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
1192
1193 #define VSIGN 1
1194 #define VUNSIGN 2
1195 #define VPTR 3
1196
1197 if (mode == WIDTHMODE) {
1198 vok = 0;
1199 switch (v->type) {
1200 case CHAR:
1201 val = GET(char);
1202 vok = VSIGN;
1203 break;
1204 case UCHAR:
1205 uval = CHK_INF127(GET(u_char));
1206 vok = VUNSIGN;
1207 break;
1208 case SHORT:
1209 val = GET(short);
1210 vok = VSIGN;
1211 break;
1212 case USHORT:
1213 uval = CHK_INF127(GET(u_short));
1214 vok = VUNSIGN;
1215 break;
1216 case INT32:
1217 val = GET(int32_t);
1218 vok = VSIGN;
1219 break;
1220 case INT:
1221 val = GET(int);
1222 vok = VSIGN;
1223 break;
1224 case UINT:
1225 case UINT32:
1226 uval = CHK_INF127(GET(u_int));
1227 vok = VUNSIGN;
1228 break;
1229 case LONG:
1230 val = GET(long);
1231 vok = VSIGN;
1232 break;
1233 case ULONG:
1234 uval = CHK_INF127(GET(u_long));
1235 vok = VUNSIGN;
1236 break;
1237 case KPTR:
1238 uval = GET(u_int64_t);
1239 vok = VPTR;
1240 break;
1241 case KPTR24:
1242 uval = GET(u_int64_t);
1243 uval &= 0xffffff;
1244 vok = VPTR;
1245 break;
1246 case INT64:
1247 val = GET(int64_t);
1248 vok = VSIGN;
1249 break;
1250 case UINT64:
1251 uval = CHK_INF127(GET(u_int64_t));
1252 vok = VUNSIGN;
1253 break;
1254
1255 case SIGLIST:
1256 default:
1257 /* nothing... */;
1258 }
1259 switch (vok) {
1260 case VSIGN:
1261 if (val < 0 && val < v->longestn) {
1262 v->longestn = val;
1263 fmtlen = iwidth(-val) + 1;
1264 if (fmtlen > v->width)
1265 v->width = fmtlen;
1266 } else if (val > 0 && val > v->longestp) {
1267 v->longestp = val;
1268 fmtlen = iwidth(val);
1269 if (fmtlen > v->width)
1270 v->width = fmtlen;
1271 }
1272 return;
1273 case VUNSIGN:
1274 if (uval > v->longestu) {
1275 v->longestu = uval;
1276 v->width = iwidth(uval);
1277 }
1278 return;
1279 case VPTR:
1280 fmtlen = 0;
1281 while (uval > 0) {
1282 uval >>= 4;
1283 fmtlen++;
1284 }
1285 if (fmtlen > v->width)
1286 v->width = fmtlen;
1287 return;
1288 }
1289 }
1290
1291 width = v->width;
1292 cp = ofmt + 1;
1293 fcp = v->fmt;
1294 if (v->flag & LJUST)
1295 *cp++ = '-';
1296 *cp++ = '*';
1297 while ((*cp++ = *fcp++) != '\0')
1298 continue;
1299
1300 switch (v->type) {
1301 case CHAR:
1302 (void)printf(ofmt, width, GET(char));
1303 return;
1304 case UCHAR:
1305 (void)printf(ofmt, width, CHK_INF127(GET(u_char)));
1306 return;
1307 case SHORT:
1308 (void)printf(ofmt, width, GET(short));
1309 return;
1310 case USHORT:
1311 (void)printf(ofmt, width, CHK_INF127(GET(u_short)));
1312 return;
1313 case INT:
1314 (void)printf(ofmt, width, GET(int));
1315 return;
1316 case UINT:
1317 (void)printf(ofmt, width, CHK_INF127(GET(u_int)));
1318 return;
1319 case LONG:
1320 (void)printf(ofmt, width, GET(long));
1321 return;
1322 case ULONG:
1323 (void)printf(ofmt, width, CHK_INF127(GET(u_long)));
1324 return;
1325 case KPTR:
1326 (void)printf(ofmt, width, GET(u_int64_t));
1327 return;
1328 case KPTR24:
1329 (void)printf(ofmt, width, GET(u_int64_t) & 0xffffff);
1330 return;
1331 case INT32:
1332 (void)printf(ofmt, width, GET(int32_t));
1333 return;
1334 case UINT32:
1335 (void)printf(ofmt, width, CHK_INF127(GET(u_int32_t)));
1336 return;
1337 case SIGLIST:
1338 {
1339 sigset_t *s = (sigset_t *)(void *)bp;
1340 size_t i;
1341 #define SIGSETSIZE (sizeof(s->__bits) / sizeof(s->__bits[0]))
1342 char buf[SIGSETSIZE * 8 + 1];
1343
1344 for (i = 0; i < SIGSETSIZE; i++)
1345 (void)snprintf(&buf[i * 8], 9, "%.8x",
1346 s->__bits[(SIGSETSIZE - 1) - i]);
1347
1348 /* Skip leading zeroes */
1349 for (i = 0; buf[i] == '0'; i++)
1350 continue;
1351
1352 if (buf[i] == '\0')
1353 i--;
1354 strprintorsetwidth(v, buf + i, mode);
1355 #undef SIGSETSIZE
1356 }
1357 return;
1358 case INT64:
1359 (void)printf(ofmt, width, GET(int64_t));
1360 return;
1361 case UINT64:
1362 (void)printf(ofmt, width, CHK_INF127(GET(u_int64_t)));
1363 return;
1364 default:
1365 errx(1, "unknown type %d", v->type);
1366 }
1367 #undef GET
1368 #undef CHK_INF127
1369 }
1370
1371 void
1372 pvar(void *arg, VARENT *ve, int mode)
1373 {
1374 VAR *v;
1375
1376 v = ve->var;
1377 if (v->flag & UAREA && !((struct kinfo_proc2 *)arg)->p_uvalid) {
1378 if (mode == PRINTMODE)
1379 (void)printf("%*s", v->width, "-");
1380 return;
1381 }
1382
1383 (void)printval((char *)arg + v->off, v, mode);
1384 }
1385
1386 void
1387 putimeval(void *arg, VARENT *ve, int mode)
1388 {
1389 VAR *v = ve->var;
1390 struct kinfo_proc2 *k = arg;
1391 ulong secs = *(uint32_t *)((char *)arg + v->off);
1392 ulong usec = *(uint32_t *)((char *)arg + v->off + sizeof (uint32_t));
1393 int fmtlen;
1394
1395 if (!k->p_uvalid) {
1396 if (mode == PRINTMODE)
1397 (void)printf("%*s", v->width, "-");
1398 return;
1399 }
1400
1401 if (mode == WIDTHMODE) {
1402 if (secs == 0)
1403 /* non-zero so fmtlen is calculated at least once */
1404 secs = 1;
1405 if (secs > v->longestu) {
1406 v->longestu = secs;
1407 if (secs <= 999)
1408 /* sss.ssssss */
1409 fmtlen = iwidth(secs) + 6 + 1;
1410 else
1411 /* hh:mm:ss.ss */
1412 fmtlen = iwidth((secs + 1) / SECSPERHOUR)
1413 + 2 + 1 + 2 + 1 + 2 + 1;
1414 if (fmtlen > v->width)
1415 v->width = fmtlen;
1416 }
1417 return;
1418 }
1419
1420 if (secs < 999)
1421 (void)printf( "%*lu.%.6lu", v->width - 6 - 1, secs, usec);
1422 else {
1423 uint h, m;
1424 usec += 5000;
1425 if (usec >= 1000000) {
1426 usec -= 1000000;
1427 secs++;
1428 }
1429 m = secs / SECSPERMIN;
1430 secs -= m * SECSPERMIN;
1431 h = m / MINSPERHOUR;
1432 m -= h * MINSPERHOUR;
1433 (void)printf( "%*u:%.2u:%.2lu.%.2lu", v->width - 9, h, m, secs,
1434 usec / 10000u );
1435 }
1436 }
1437