1 /* $NetBSD: w.c,v 1.82 2014/12/22 15:24:14 dennis Exp $ */
4 * Copyright (c) 1980, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)w.c 8.6 (Berkeley) 6/30/94";
42 __RCSID("$NetBSD: w.c,v 1.82 2014/12/22 15:24:14 dennis Exp $");
47 * w - print system status (who and what)
49 * This program is similar to the systat command on Tenex/Tops 10/20
52 #include <sys/param.h>
53 #include <sys/types.h>
56 #include <sys/sysctl.h>
58 #include <sys/ioctl.h>
59 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
89 struct timeval boottime
;
92 time_t now
; /* the current time of day */
93 int ttywidth
; /* width of tty */
94 int argwidth
; /* width of tty left to print process args */
95 int header
= 1; /* true if -h flag: don't print heading */
96 int nflag
; /* true if -n flag: don't convert addrs */
97 int wflag
; /* true if -w flag: wide printout */
98 int sortidle
; /* sort bu idle time */
99 char *sel_user
; /* login of particular user selected */
100 char domain
[MAXHOSTNAMELEN
+ 1];
101 int maxname
= 8, maxline
= 3, maxhost
= 16;
104 * One of these per active utmp entry.
108 char name
[UTX_USERSIZE
+ 1];
109 char line
[UTX_LINESIZE
+ 1];
110 char host
[UTX_HOSTSIZE
+ 1];
113 dev_t tdev
; /* dev_t of terminal */
114 time_t idle
; /* idle time of terminal in seconds */
115 struct kinfo_proc2
*tp
; /* `most interesting' tty proc */
116 struct kinfo_proc2
*pp
; /* pid proc */
117 pid_t pid
; /* pid or ~0 if not known */
118 } *ehead
= NULL
, **nextp
= &ehead
;
120 static void pr_args(struct kinfo_proc2
*);
121 static void pr_header(time_t *, int);
122 static int proc_compare_wrapper(const struct kinfo_proc2
*,
123 const struct kinfo_proc2
*);
124 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
125 static int ttystat(const char *, struct stat
*);
126 static void process(struct entry
*);
128 static void fixhost(struct entry
*ep
);
129 __dead
static void usage(int);
132 main(int argc
, char **argv
)
134 struct kinfo_proc2
*kp
;
136 int ch
, i
, nentries
, nusers
, wcmd
, curtain
, use_sysctl
;
137 char *memf
, *nlistf
, *usrnp
;
147 const char *progname
;
148 char errbuf
[_POSIX2_LINE_MAX
];
150 setprogname(argv
[0]);
152 /* Are we w(1) or uptime(1)? */
153 progname
= getprogname();
154 if (*progname
== '-')
156 if (*progname
== 'u') {
161 options
= "hiM:N:nw";
164 memf
= nlistf
= NULL
;
165 while ((ch
= getopt(argc
, argv
, options
)) != -1)
193 use_sysctl
= (memf
== NULL
&& nlistf
== NULL
);
195 if ((kd
= kvm_openfiles(nlistf
, memf
, NULL
,
196 memf
== NULL
? KVM_NO_FILES
: O_RDONLY
, errbuf
)) == NULL
)
197 errx(1, "%s", errbuf
);
202 len
= sizeof(curtain
);
203 if (sysctlbyname("security.curtain", &curtain
, &len
,
220 while ((utx
= getutxent()) != NULL
) {
221 if (utx
->ut_type
!= USER_PROCESS
)
228 #endif /* !SUPPORT_UTMP */
231 strncmp(utx
->ut_name
, sel_user
, sizeof(utx
->ut_name
)) != 0)
233 if ((ep
= calloc(1, sizeof(struct entry
))) == NULL
)
235 (void)memcpy(ep
->line
, utx
->ut_line
, sizeof(utx
->ut_line
));
236 ep
->line
[sizeof(utx
->ut_line
)] = '\0';
243 (void)memcpy(ep
->name
, utx
->ut_name
, sizeof(utx
->ut_name
));
244 ep
->name
[sizeof(utx
->ut_name
)] = '\0';
245 if (!nflag
|| getnameinfo((struct sockaddr
*)&utx
->ut_ss
,
246 utx
->ut_ss
.ss_len
, ep
->host
, sizeof(ep
->host
), NULL
, 0,
247 NI_NUMERICHOST
) != 0) {
248 (void)memcpy(ep
->host
, utx
->ut_host
,
249 sizeof(utx
->ut_host
));
250 ep
->host
[sizeof(utx
->ut_host
)] = '\0';
255 ep
->pid
= utx
->ut_pid
;
261 while ((ut
= getutent()) != NULL
) {
262 if (ut
->ut_name
[0] == '\0')
266 strncmp(ut
->ut_name
, sel_user
, sizeof(ut
->ut_name
)) != 0)
269 /* Don't process entries that we have utmpx for */
270 for (ep
= ehead
; ep
!= NULL
; ep
= ep
->next
) {
271 if (strncmp(ep
->line
, ut
->ut_line
,
272 sizeof(ut
->ut_line
)) == 0)
283 if ((ep
= calloc(1, sizeof(struct entry
))) == NULL
)
285 (void)memcpy(ep
->name
, ut
->ut_name
, sizeof(ut
->ut_name
));
286 (void)memcpy(ep
->line
, ut
->ut_line
, sizeof(ut
->ut_line
));
287 (void)memcpy(ep
->host
, ut
->ut_host
, sizeof(ut
->ut_host
));
288 ep
->name
[sizeof(ut
->ut_name
)] = '\0';
289 ep
->line
[sizeof(ut
->ut_line
)] = '\0';
290 ep
->host
[sizeof(ut
->ut_host
)] = '\0';
292 ep
->tv
.tv_sec
= ut
->ut_time
;
306 if (header
|| wcmd
== 0) {
307 pr_header(&now
, nusers
);
312 if ((kp
= kvm_getproc2(kd
, KERN_PROC_ALL
, 0,
313 sizeof(struct kinfo_proc2
), &nentries
)) == NULL
)
314 errx(1, "%s", kvm_geterr(kd
));
316 /* Include trailing space because TTY header starts one column early. */
317 for (i
= 0; i
< nentries
; i
++, kp
++) {
319 for (ep
= ehead
; ep
!= NULL
; ep
= ep
->next
) {
320 if (ep
->tdev
!= 0 && ep
->tdev
== kp
->p_tdev
&&
321 kp
->p__pgid
== kp
->p_tpgid
) {
323 * Proc is in foreground of this
326 if (proc_compare_wrapper(ep
->tp
, kp
))
330 if (ep
->pid
!= 0 && ep
->pid
== kp
->p_pid
) {
337 if ((ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) == -1 &&
338 ioctl(STDERR_FILENO
, TIOCGWINSZ
, &ws
) == -1 &&
339 ioctl(STDIN_FILENO
, TIOCGWINSZ
, &ws
) == -1) || ws
.ws_col
== 0)
342 ttywidth
= ws
.ws_col
- 1;
344 if (!wflag
&& maxhost
> (ttywidth
/ 3))
345 maxhost
= ttywidth
/ 3;
347 argwidth
= printf("%-*s TTY %-*s %*s IDLE WHAT\n",
348 maxname
, "USER", maxhost
, "FROM",
349 7 /* "dddhhXm" */, "LOGIN@");
350 argwidth
-= sizeof("WHAT\n") - 1 /* NUL */;
351 argwidth
= ttywidth
- argwidth
;
357 /* sort by idle time */
358 if (sortidle
&& ehead
!= NULL
) {
359 struct entry
*from
= ehead
, *save
;
362 while (from
!= NULL
) {
364 (*nextp
) && from
->idle
>= (*nextp
)->idle
;
365 nextp
= &(*nextp
)->next
)
373 #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
374 else if (ehead
!= NULL
) {
375 struct entry
*from
= ehead
, *save
;
378 while (from
!= NULL
) {
380 (*nextp
) && strcmp(from
->line
, (*nextp
)->line
) > 0;
381 nextp
= &(*nextp
)->next
)
395 rv
= gethostname(domain
, sizeof(domain
));
396 domain
[sizeof(domain
) - 1] = '\0';
397 if (rv
< 0 || (p
= strchr(domain
, '.')) == 0)
400 memmove(domain
, p
, strlen(p
) + 1);
403 for (ep
= ehead
; ep
!= NULL
; ep
= ep
->next
) {
406 else if (ep
->pp
!= NULL
)
408 else if (ep
->pid
!= 0) {
412 warnx("Stale utmp%s entry: %s %s %s",
413 ep
->type
, ep
->name
, ep
->line
, ep
->host
);
417 #if !defined(__minix)
418 usrnp
= (kp
== NULL
) ? ep
->name
: kp
->p_login
;
420 usrnp
= ep
->name
; /* TODO: implement getlogin/setlogin */
421 #endif /* !defined(__minix) */
422 (void)printf("%-*s %-7.7s %-*.*s ",
423 maxname
, usrnp
, ep
->line
,
424 maxhost
, maxhost
, ep
->host
);
425 then
= (time_t)ep
->tv
.tv_sec
;
426 pr_attime(&then
, &now
);
435 pr_args(struct kinfo_proc2
*kp
)
443 argv
= kvm_getargv2(kd
, kp
, (argwidth
< 0) ? 0 : argwidth
);
445 fmt_putc('(', &left
);
446 fmt_puts((char *)kp
->p_comm
, &left
);
447 fmt_putc(')', &left
);
451 fmt_puts(*argv
, &left
);
453 fmt_putc(' ', &left
);
461 pr_header(time_t *nowp
, int nusers
)
473 * SCCS forces the string manipulation below, as it replaces
474 * %, M, and % in a character string with the file name.
476 (void)strftime(buf
, sizeof(buf
), "%l:%" "M%p", localtime(nowp
));
477 buf
[sizeof(buf
) - 1] = '\0';
478 (void)printf("%s ", buf
);
481 * Print how long system has been up.
482 * (Found by looking getting "boottime" from the kernel)
485 mib
[1] = KERN_BOOTTIME
;
486 size
= sizeof(boottime
);
487 if (sysctl(mib
, 2, &boottime
, &size
, NULL
, 0) != -1 &&
488 boottime
.tv_sec
!= 0) {
489 uptime
= now
- boottime
.tv_sec
;
491 if (uptime
> SECSPERMIN
) {
492 days
= uptime
/ SECSPERDAY
;
493 uptime
%= SECSPERDAY
;
494 hrs
= uptime
/ SECSPERHOUR
;
495 uptime
%= SECSPERHOUR
;
496 mins
= uptime
/ SECSPERMIN
;
499 (void)printf(" %d day%s,", days
,
500 days
> 1 ? "s" : "");
501 if (hrs
> 0 && mins
> 0)
502 (void)printf(" %2d:%02d,", hrs
, mins
);
505 (void)printf(" %d hr%s,",
506 hrs
, hrs
> 1 ? "s" : "");
508 (void)printf(" %d min%s,",
509 mins
, mins
> 1 ? "s" : "");
514 /* Print number of users logged in to system */
515 (void)printf(" %d user%s", nusers
, nusers
!= 1 ? "s" : "");
518 * Print 1, 5, and 15 minute load averages.
520 if (getloadavg(avenrun
, sizeof(avenrun
) / sizeof(avenrun
[0])) == -1)
521 (void)printf(", no load average information available\n");
523 (void)printf(", load averages:");
524 for (i
= 0; i
< (sizeof(avenrun
) / sizeof(avenrun
[0])); i
++) {
527 (void)printf(" %.2f", avenrun
[i
]);
533 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
535 ttystat(const char *line
, struct stat
*st
)
537 char ttybuf
[MAXPATHLEN
];
539 (void)snprintf(ttybuf
, sizeof(ttybuf
), "%s%s", _PATH_DEV
, line
);
540 return stat(ttybuf
, st
);
544 process(struct entry
*ep
)
550 if ((max
= strlen(ep
->name
)) > maxname
)
552 if ((max
= strlen(ep
->line
)) > maxline
)
554 if ((max
= strlen(ep
->host
)) > maxhost
)
558 ep
->idle
= (time_t)-1;
562 * Hack to recognize and correctly parse
563 * ut entry made by ftpd. The "tty" used
564 * by ftpd is not a real tty, just identifier in
565 * form ftpPID. Pid parsed from the "tty name"
566 * is used later to match corresponding process.
567 * NB: This is only used for utmp entries. For utmpx,
568 * we already have the pid.
570 if (ep
->pid
== 0 && strncmp(ep
->line
, "ftp", 3) == 0) {
571 ep
->pid
= strtol(ep
->line
+ 3, NULL
, 10);
575 if (ttystat(ep
->line
, &st
) == -1)
578 ep
->tdev
= st
.st_rdev
;
580 * If this is the console device, attempt to ascertain
581 * the true console device dev_t.
588 mib
[1] = KERN_CONSDEV
;
589 size
= sizeof(dev_t
);
590 (void) sysctl(mib
, 2, &ep
->tdev
, &size
, NULL
, 0);
593 touched
= st
.st_atime
;
594 if (touched
< ep
->tv
.tv_sec
) {
595 /* tty untouched since before login */
596 touched
= ep
->tv
.tv_sec
;
598 if ((ep
->idle
= now
- touched
) < 0)
604 proc_compare_wrapper(const struct kinfo_proc2
*p1
,
605 const struct kinfo_proc2
*p2
)
607 struct kinfo_lwp
*l1
, *l2
;
613 l1
= kvm_getlwps(kd
, p1
->p_pid
, 0, sizeof(*l1
), &cnt
);
614 if (l1
== NULL
|| cnt
== 0)
617 l2
= kvm_getlwps(kd
, p2
->p_pid
, 0, sizeof(*l1
), &cnt
);
618 if (l2
== NULL
|| cnt
== 0)
621 return proc_compare(p1
, l1
, p2
, l2
);
625 fixhost(struct entry
*ep
)
627 char host_buf
[sizeof(ep
->host
)];
632 strlcpy(host_buf
, *ep
->host
? ep
->host
: "-", sizeof(host_buf
));
636 * XXX: Historical behavior, ':' in hostname means X display number,
639 for (x
= p
; x
< &host_buf
[sizeof(host_buf
)]; x
++)
640 if (*x
== '\0' || *x
== ':')
642 if (x
== p
+ sizeof(host_buf
) || *x
!= ':')
647 if (!nflag
&& inet_aton(p
, &l
) &&
648 (hp
= gethostbyaddr((char *)&l
, sizeof(l
), AF_INET
))) {
649 if (domain
[0] != '\0') {
651 p
+= strlen(hp
->h_name
);
653 if (p
> hp
->h_name
&&
654 strcasecmp(p
, domain
) == 0)
661 (void)snprintf(ep
->host
, sizeof(ep
->host
), "%s:%s", p
, x
);
664 strlcpy(ep
->host
, p
, sizeof(ep
->host
));
672 (void)fprintf(stderr
,
673 "Usage: %s [-hinw] [-M core] [-N system] [user]\n",
676 (void)fprintf(stderr
, "Usage: %s\n", getprogname());