| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: w.c,v 1.75 2011/09/16 15:39:30 joerg Exp $ */ | | 1 | /* $NetBSD: w.c,v 1.76 2011/10/21 02:26:09 christos Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 1980, 1991, 1993, 1994 | | 4 | * Copyright (c) 1980, 1991, 1993, 1994 |
5 | * The Regents of the University of California. All rights reserved. | | 5 | * The Regents of the University of California. All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -29,27 +29,27 @@ | | | @@ -29,27 +29,27 @@ |
29 | * SUCH DAMAGE. | | 29 | * SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #include <sys/cdefs.h> | | 32 | #include <sys/cdefs.h> |
33 | #ifndef lint | | 33 | #ifndef lint |
34 | __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\ | | 34 | __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\ |
35 | The Regents of the University of California. All rights reserved."); | | 35 | The Regents of the University of California. All rights reserved."); |
36 | #endif /* not lint */ | | 36 | #endif /* not lint */ |
37 | | | 37 | |
38 | #ifndef lint | | 38 | #ifndef lint |
39 | #if 0 | | 39 | #if 0 |
40 | static char sccsid[] = "@(#)w.c 8.6 (Berkeley) 6/30/94"; | | 40 | static char sccsid[] = "@(#)w.c 8.6 (Berkeley) 6/30/94"; |
41 | #else | | 41 | #else |
42 | __RCSID("$NetBSD: w.c,v 1.75 2011/09/16 15:39:30 joerg Exp $"); | | 42 | __RCSID("$NetBSD: w.c,v 1.76 2011/10/21 02:26:09 christos Exp $"); |
43 | #endif | | 43 | #endif |
44 | #endif /* not lint */ | | 44 | #endif /* not lint */ |
45 | | | 45 | |
46 | /* | | 46 | /* |
47 | * w - print system status (who and what) | | 47 | * w - print system status (who and what) |
48 | * | | 48 | * |
49 | * This program is similar to the systat command on Tenex/Tops 10/20 | | 49 | * This program is similar to the systat command on Tenex/Tops 10/20 |
50 | * | | 50 | * |
51 | */ | | 51 | */ |
52 | #include <sys/param.h> | | 52 | #include <sys/param.h> |
53 | #include <sys/types.h> | | 53 | #include <sys/types.h> |
54 | #include <sys/time.h> | | 54 | #include <sys/time.h> |
55 | #include <sys/stat.h> | | 55 | #include <sys/stat.h> |
| @@ -110,26 +110,28 @@ struct entry { | | | @@ -110,26 +110,28 @@ struct entry { |
110 | char line[UTX_LINESIZE + 1]; | | 110 | char line[UTX_LINESIZE + 1]; |
111 | char host[UTX_HOSTSIZE + 1]; | | 111 | char host[UTX_HOSTSIZE + 1]; |
112 | char type[2]; | | 112 | char type[2]; |
113 | struct timeval tv; | | 113 | struct timeval tv; |
114 | dev_t tdev; /* dev_t of terminal */ | | 114 | dev_t tdev; /* dev_t of terminal */ |
115 | time_t idle; /* idle time of terminal in seconds */ | | 115 | time_t idle; /* idle time of terminal in seconds */ |
116 | struct kinfo_proc2 *tp; /* `most interesting' tty proc */ | | 116 | struct kinfo_proc2 *tp; /* `most interesting' tty proc */ |
117 | struct kinfo_proc2 *pp; /* pid proc */ | | 117 | struct kinfo_proc2 *pp; /* pid proc */ |
118 | pid_t pid; /* pid or ~0 if not known */ | | 118 | pid_t pid; /* pid or ~0 if not known */ |
119 | } *ehead = NULL, **nextp = &ehead; | | 119 | } *ehead = NULL, **nextp = &ehead; |
120 | | | 120 | |
121 | static void pr_args(struct kinfo_proc2 *); | | 121 | static void pr_args(struct kinfo_proc2 *); |
122 | static void pr_header(time_t *, int); | | 122 | static void pr_header(time_t *, int); |
| | | 123 | static int proc_compare_wrapper(const struct kinfo_proc2 *, |
| | | 124 | const struct kinfo_proc2 *); |
123 | #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX) | | 125 | #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX) |
124 | static int ttystat(const char *, struct stat *); | | 126 | static int ttystat(const char *, struct stat *); |
125 | static void process(struct entry *); | | 127 | static void process(struct entry *); |
126 | #endif | | 128 | #endif |
127 | __dead static void usage(int); | | 129 | __dead static void usage(int); |
128 | | | 130 | |
129 | int | | 131 | int |
130 | main(int argc, char **argv) | | 132 | main(int argc, char **argv) |
131 | { | | 133 | { |
132 | struct kinfo_proc2 *kp; | | 134 | struct kinfo_proc2 *kp; |
133 | struct hostent *hp; | | 135 | struct hostent *hp; |
134 | struct in_addr l; | | 136 | struct in_addr l; |
135 | struct entry *ep; | | 137 | struct entry *ep; |
| @@ -292,37 +294,34 @@ main(int argc, char **argv) | | | @@ -292,37 +294,34 @@ main(int argc, char **argv) |
292 | if (header || wcmd == 0) { | | 294 | if (header || wcmd == 0) { |
293 | pr_header(&now, nusers); | | 295 | pr_header(&now, nusers); |
294 | if (wcmd == 0) | | 296 | if (wcmd == 0) |
295 | exit (0); | | 297 | exit (0); |
296 | } | | 298 | } |
297 | | | 299 | |
298 | if ((kp = kvm_getproc2(kd, KERN_PROC_ALL, 0, | | 300 | if ((kp = kvm_getproc2(kd, KERN_PROC_ALL, 0, |
299 | sizeof(struct kinfo_proc2), &nentries)) == NULL) | | 301 | sizeof(struct kinfo_proc2), &nentries)) == NULL) |
300 | errx(1, "%s", kvm_geterr(kd)); | | 302 | errx(1, "%s", kvm_geterr(kd)); |
301 | | | 303 | |
302 | /* Include trailing space because TTY header starts one column early. */ | | 304 | /* Include trailing space because TTY header starts one column early. */ |
303 | for (i = 0; i < nentries; i++, kp++) { | | 305 | for (i = 0; i < nentries; i++, kp++) { |
304 | | | 306 | |
305 | if (kp->p_stat == SIDL || kp->p_stat == SZOMB) | | | |
306 | continue; | | | |
307 | | | | |
308 | for (ep = ehead; ep != NULL; ep = ep->next) { | | 307 | for (ep = ehead; ep != NULL; ep = ep->next) { |
309 | if (ep->tdev != 0 && ep->tdev == kp->p_tdev && | | 308 | if (ep->tdev != 0 && ep->tdev == kp->p_tdev && |
310 | kp->p__pgid == kp->p_tpgid) { | | 309 | kp->p__pgid == kp->p_tpgid) { |
311 | /* | | 310 | /* |
312 | * Proc is in foreground of this | | 311 | * Proc is in foreground of this |
313 | * terminal | | 312 | * terminal |
314 | */ | | 313 | */ |
315 | if (proc_compare(ep->tp, kp)) | | 314 | if (proc_compare_wrapper(ep->tp, kp)) |
316 | ep->tp = kp; | | 315 | ep->tp = kp; |
317 | break; | | 316 | break; |
318 | } | | 317 | } |
319 | if (ep->pid != 0 && ep->pid == kp->p_pid) { | | 318 | if (ep->pid != 0 && ep->pid == kp->p_pid) { |
320 | ep->pp = kp; | | 319 | ep->pp = kp; |
321 | break; | | 320 | break; |
322 | } | | 321 | } |
323 | } | | 322 | } |
324 | } | | 323 | } |
325 | | | 324 | |
326 | if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 && | | 325 | if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 && |
327 | ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 && | | 326 | ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 && |
328 | ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0) | | 327 | ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0) |
| @@ -608,25 +607,46 @@ process(struct entry *ep) | | | @@ -608,25 +607,46 @@ process(struct entry *ep) |
608 | (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0); | | 607 | (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0); |
609 | } | | 608 | } |
610 | | | 609 | |
611 | touched = st.st_atime; | | 610 | touched = st.st_atime; |
612 | if (touched < ep->tv.tv_sec) { | | 611 | if (touched < ep->tv.tv_sec) { |
613 | /* tty untouched since before login */ | | 612 | /* tty untouched since before login */ |
614 | touched = ep->tv.tv_sec; | | 613 | touched = ep->tv.tv_sec; |
615 | } | | 614 | } |
616 | if ((ep->idle = now - touched) < 0) | | 615 | if ((ep->idle = now - touched) < 0) |
617 | ep->idle = 0; | | 616 | ep->idle = 0; |
618 | } | | 617 | } |
619 | #endif | | 618 | #endif |
620 | | | 619 | |
| | | 620 | static int |
| | | 621 | proc_compare_wrapper(const struct kinfo_proc2 *p1, |
| | | 622 | const struct kinfo_proc2 *p2) |
| | | 623 | { |
| | | 624 | struct kinfo_lwp *l1, *l2; |
| | | 625 | int cnt; |
| | | 626 | |
| | | 627 | if (p1 == NULL) |
| | | 628 | return 1; |
| | | 629 | |
| | | 630 | l1 = kvm_getlwps(kd, p1->p_pid, 0, sizeof(*l1), &cnt); |
| | | 631 | if (l1 == NULL || cnt == 0) |
| | | 632 | return 1; |
| | | 633 | |
| | | 634 | l2 = kvm_getlwps(kd, p2->p_pid, 0, sizeof(*l1), &cnt); |
| | | 635 | if (l2 == NULL || cnt == 0) |
| | | 636 | return 0; |
| | | 637 | |
| | | 638 | return proc_compare(p1, l1, p2, l2); |
| | | 639 | } |
| | | 640 | |
621 | static void | | 641 | static void |
622 | usage(int wcmd) | | 642 | usage(int wcmd) |
623 | { | | 643 | { |
624 | | | 644 | |
625 | if (wcmd) | | 645 | if (wcmd) |
626 | (void)fprintf(stderr, | | 646 | (void)fprintf(stderr, |
627 | "Usage: %s [-hinw] [-M core] [-N system] [user]\n", | | 647 | "Usage: %s [-hinw] [-M core] [-N system] [user]\n", |
628 | getprogname()); | | 648 | getprogname()); |
629 | else | | 649 | else |
630 | (void)fprintf(stderr, "Usage: %s\n", getprogname()); | | 650 | (void)fprintf(stderr, "Usage: %s\n", getprogname()); |
631 | exit(1); | | 651 | exit(1); |
632 | } | | 652 | } |