1 /* $NetBSD: w.c,v 1.73 2008/07/21 14:19:27 lukem 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.73 2008/07/21 14:19:27 lukem 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>
59 #include <sys/ioctl.h>
60 #include <sys/socket.h>
62 #include <netinet/in.h>
63 #include <arpa/inet.h>
90 struct timeval boottime
;
93 time_t now
; /* the current time of day */
94 int ttywidth
; /* width of tty */
95 int argwidth
; /* width of tty left to print process args */
96 int header
= 1; /* true if -h flag: don't print heading */
97 int nflag
; /* true if -n flag: don't convert addrs */
98 int wflag
; /* true if -w flag: wide printout */
99 int sortidle
; /* sort bu idle time */
100 char *sel_user
; /* login of particular user selected */
101 char domain
[MAXHOSTNAMELEN
+ 1];
102 int maxname
= 8, maxline
= 3, maxhost
= 16;
105 * One of these per active utmp entry.
109 char name
[UTX_USERSIZE
+ 1];
110 char line
[UTX_LINESIZE
+ 1];
111 char host
[UTX_HOSTSIZE
+ 1];
114 dev_t tdev
; /* dev_t of terminal */
115 time_t idle
; /* idle time of terminal in seconds */
116 struct kinfo_proc2
*tp
; /* `most interesting' tty proc */
117 struct kinfo_proc2
*pp
; /* pid proc */
118 pid_t pid
; /* pid or ~0 if not known */
119 } *ehead
= NULL
, **nextp
= &ehead
;
121 static void pr_args(struct kinfo_proc2
*);
122 static void pr_header(time_t *, int);
123 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
124 static int ttystat(const char *, struct stat
*);
125 static void process(struct entry
*);
127 static void usage(int);
129 int main(int, char **);
132 main(int argc
, char **argv
)
134 struct kinfo_proc2
*kp
;
138 int ch
, i
, nentries
, nusers
, wcmd
, curtain
, use_sysctl
;
139 char *memf
, *nlistf
, *p
, *x
, *usrnp
;
149 const char *progname
;
150 char buf
[MAXHOSTNAMELEN
], errbuf
[_POSIX2_LINE_MAX
];
152 setprogname(argv
[0]);
154 /* Are we w(1) or uptime(1)? */
155 progname
= getprogname();
156 if (*progname
== '-')
158 if (*progname
== 'u') {
163 options
= "hiM:N:nw";
166 memf
= nlistf
= NULL
;
167 while ((ch
= getopt(argc
, argv
, options
)) != -1)
195 use_sysctl
= (memf
== NULL
&& nlistf
== NULL
);
197 if ((kd
= kvm_openfiles(nlistf
, memf
, NULL
,
198 memf
== NULL
? KVM_NO_FILES
: O_RDONLY
, errbuf
)) == NULL
)
199 errx(1, "%s", errbuf
);
204 len
= sizeof(curtain
);
205 if (sysctlbyname("security.curtain", &curtain
, &len
,
222 while ((utx
= getutxent()) != NULL
) {
223 if (utx
->ut_type
!= USER_PROCESS
)
227 strncmp(utx
->ut_name
, sel_user
, sizeof(utx
->ut_name
)) != 0)
229 if ((ep
= calloc(1, sizeof(struct entry
))) == NULL
)
231 (void)memcpy(ep
->name
, utx
->ut_name
, sizeof(utx
->ut_name
));
232 (void)memcpy(ep
->line
, utx
->ut_line
, sizeof(utx
->ut_line
));
233 ep
->name
[sizeof(utx
->ut_name
)] = '\0';
234 ep
->line
[sizeof(utx
->ut_line
)] = '\0';
235 if (!nflag
|| getnameinfo((struct sockaddr
*)&utx
->ut_ss
,
236 utx
->ut_ss
.ss_len
, ep
->host
, sizeof(ep
->host
), NULL
, 0,
237 NI_NUMERICHOST
) != 0) {
238 (void)memcpy(ep
->host
, utx
->ut_host
,
239 sizeof(utx
->ut_host
));
240 ep
->host
[sizeof(utx
->ut_host
)] = '\0';
244 ep
->pid
= utx
->ut_pid
;
253 while ((ut
= getutent()) != NULL
) {
254 if (ut
->ut_name
[0] == '\0')
258 strncmp(ut
->ut_name
, sel_user
, sizeof(ut
->ut_name
)) != 0)
261 /* Don't process entries that we have utmpx for */
262 for (ep
= ehead
; ep
!= NULL
; ep
= ep
->next
) {
263 if (strncmp(ep
->line
, ut
->ut_line
,
264 sizeof(ut
->ut_line
)) == 0)
271 if ((ep
= calloc(1, sizeof(struct entry
))) == NULL
)
273 (void)memcpy(ep
->name
, ut
->ut_name
, sizeof(ut
->ut_name
));
274 (void)memcpy(ep
->line
, ut
->ut_line
, sizeof(ut
->ut_line
));
275 (void)memcpy(ep
->host
, ut
->ut_host
, sizeof(ut
->ut_host
));
276 ep
->name
[sizeof(ut
->ut_name
)] = '\0';
277 ep
->line
[sizeof(ut
->ut_line
)] = '\0';
278 ep
->host
[sizeof(ut
->ut_host
)] = '\0';
279 ep
->tv
.tv_sec
= ut
->ut_time
;
294 if (header
|| wcmd
== 0) {
295 pr_header(&now
, nusers
);
300 if ((kp
= kvm_getproc2(kd
, KERN_PROC_ALL
, 0,
301 sizeof(struct kinfo_proc2
), &nentries
)) == NULL
)
302 errx(1, "%s", kvm_geterr(kd
));
304 /* Include trailing space because TTY header starts one column early. */
305 for (i
= 0; i
< nentries
; i
++, kp
++) {
307 if (kp
->p_stat
== SIDL
|| kp
->p_stat
== SZOMB
)
310 for (ep
= ehead
; ep
!= NULL
; ep
= ep
->next
) {
311 if (ep
->tdev
!= 0 && ep
->tdev
== kp
->p_tdev
&&
312 kp
->p__pgid
== kp
->p_tpgid
) {
314 * Proc is in foreground of this
317 if (proc_compare(ep
->tp
, kp
))
321 if (ep
->pid
!= 0 && ep
->pid
== kp
->p_pid
) {
328 if ((ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) == -1 &&
329 ioctl(STDERR_FILENO
, TIOCGWINSZ
, &ws
) == -1 &&
330 ioctl(STDIN_FILENO
, TIOCGWINSZ
, &ws
) == -1) || ws
.ws_col
== 0)
333 ttywidth
= ws
.ws_col
- 1;
335 if (!wflag
&& maxhost
> (ttywidth
/ 3))
336 maxhost
= ttywidth
/ 3;
338 argwidth
= printf("%-*s TTY %-*s %*s IDLE WHAT\n",
339 maxname
, "USER", maxhost
, "FROM",
340 7 /* "dddhhXm" */, "LOGIN@");
341 argwidth
-= sizeof("WHAT\n") - 1 /* NUL */;
342 argwidth
= ttywidth
- argwidth
;
348 /* sort by idle time */
349 if (sortidle
&& ehead
!= NULL
) {
350 struct entry
*from
= ehead
, *save
;
353 while (from
!= NULL
) {
355 (*nextp
) && from
->idle
>= (*nextp
)->idle
;
356 nextp
= &(*nextp
)->next
)
364 #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
365 else if (ehead
!= NULL
) {
366 struct entry
*from
= ehead
, *save
;
369 while (from
!= NULL
) {
371 (*nextp
) && strcmp(from
->line
, (*nextp
)->line
) > 0;
372 nextp
= &(*nextp
)->next
)
385 rv
= gethostname(domain
, sizeof(domain
));
386 domain
[sizeof(domain
) - 1] = '\0';
387 if (rv
< 0 || (p
= strchr(domain
, '.')) == 0)
390 memmove(domain
, p
, strlen(p
) + 1);
393 for (ep
= ehead
; ep
!= NULL
; ep
= ep
->next
) {
394 char host_buf
[MAXHOSTNAMELEN
+ 1];
396 strlcpy(host_buf
, *ep
->host
? ep
->host
: "-", sizeof(host_buf
));
399 for (x
= p
; x
< p
+ MAXHOSTNAMELEN
; x
++)
400 if (*x
== '\0' || *x
== ':')
402 if (x
== p
+ MAXHOSTNAMELEN
|| *x
!= ':')
407 if (!nflag
&& inet_aton(p
, &l
) &&
408 (hp
= gethostbyaddr((char *)&l
, sizeof(l
), AF_INET
))) {
409 if (domain
[0] != '\0') {
411 p
+= strlen(hp
->h_name
);
413 if (p
> hp
->h_name
&&
414 strcasecmp(p
, domain
) == 0)
420 (void)snprintf(buf
, sizeof(buf
), "%s:%s", p
, x
);
426 else if (ep
->pp
!= NULL
)
428 else if (ep
->pid
!= 0) {
432 warnx("Stale utmp%s entry: %s %s %s",
433 ep
->type
, ep
->name
, ep
->line
, ep
->host
);
437 usrnp
= (kp
== NULL
) ? ep
->name
: kp
->p_login
;
438 (void)printf("%-*s %-7.7s %-*.*s ",
439 maxname
, usrnp
, ep
->line
,
440 maxhost
, maxhost
, *p
? p
: "-");
441 then
= (time_t)ep
->tv
.tv_sec
;
442 pr_attime(&then
, &now
);
451 pr_args(struct kinfo_proc2
*kp
)
459 argv
= kvm_getargv2(kd
, kp
, (argwidth
< 0) ? 0 : argwidth
);
461 if (kp
->p_comm
== 0) {
464 fmt_putc('(', &left
);
465 fmt_puts((char *)kp
->p_comm
, &left
);
466 fmt_putc(')', &left
);
471 fmt_puts(*argv
, &left
);
473 fmt_putc(' ', &left
);
481 pr_header(time_t *nowp
, int nusers
)
493 * SCCS forces the string manipulation below, as it replaces
494 * %, M, and % in a character string with the file name.
496 (void)strftime(buf
, sizeof(buf
), "%l:%" "M%p", localtime(nowp
));
497 buf
[sizeof(buf
) - 1] = '\0';
498 (void)printf("%s ", buf
);
501 * Print how long system has been up.
502 * (Found by looking getting "boottime" from the kernel)
505 mib
[1] = KERN_BOOTTIME
;
506 size
= sizeof(boottime
);
507 if (sysctl(mib
, 2, &boottime
, &size
, NULL
, 0) != -1 &&
508 boottime
.tv_sec
!= 0) {
509 uptime
= now
- boottime
.tv_sec
;
511 if (uptime
> SECSPERMIN
) {
512 days
= uptime
/ SECSPERDAY
;
513 uptime
%= SECSPERDAY
;
514 hrs
= uptime
/ SECSPERHOUR
;
515 uptime
%= SECSPERHOUR
;
516 mins
= uptime
/ SECSPERMIN
;
519 (void)printf(" %d day%s,", days
,
520 days
> 1 ? "s" : "");
521 if (hrs
> 0 && mins
> 0)
522 (void)printf(" %2d:%02d,", hrs
, mins
);
525 (void)printf(" %d hr%s,",
526 hrs
, hrs
> 1 ? "s" : "");
528 (void)printf(" %d min%s,",
529 mins
, mins
> 1 ? "s" : "");
534 /* Print number of users logged in to system */
535 (void)printf(" %d user%s", nusers
, nusers
!= 1 ? "s" : "");
538 * Print 1, 5, and 15 minute load averages.
540 if (getloadavg(avenrun
, sizeof(avenrun
) / sizeof(avenrun
[0])) == -1)
541 (void)printf(", no load average information available\n");
543 (void)printf(", load averages:");
544 for (i
= 0; i
< (sizeof(avenrun
) / sizeof(avenrun
[0])); i
++) {
547 (void)printf(" %.2f", avenrun
[i
]);
553 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
555 ttystat(const char *line
, struct stat
*st
)
557 char ttybuf
[MAXPATHLEN
];
559 (void)snprintf(ttybuf
, sizeof(ttybuf
), "%s%s", _PATH_DEV
, line
);
560 return stat(ttybuf
, st
);
564 process(struct entry
*ep
)
570 if ((max
= strlen(ep
->name
)) > maxname
)
572 if ((max
= strlen(ep
->line
)) > maxline
)
574 if ((max
= strlen(ep
->host
)) > maxhost
)
578 ep
->idle
= (time_t)-1;
582 * Hack to recognize and correctly parse
583 * ut entry made by ftpd. The "tty" used
584 * by ftpd is not a real tty, just identifier in
585 * form ftpPID. Pid parsed from the "tty name"
586 * is used later to match corresponding process.
587 * NB: This is only used for utmp entries. For utmpx,
588 * we already have the pid.
590 if (ep
->pid
== 0 && strncmp(ep
->line
, "ftp", 3) == 0) {
591 ep
->pid
= strtol(ep
->line
+ 3, NULL
, 10);
595 if (ttystat(ep
->line
, &st
) == -1)
598 ep
->tdev
= st
.st_rdev
;
600 * If this is the console device, attempt to ascertain
601 * the true console device dev_t.
608 mib
[1] = KERN_CONSDEV
;
609 size
= sizeof(dev_t
);
610 (void) sysctl(mib
, 2, &ep
->tdev
, &size
, NULL
, 0);
613 touched
= st
.st_atime
;
614 if (touched
< ep
->tv
.tv_sec
) {
615 /* tty untouched since before login */
616 touched
= ep
->tv
.tv_sec
;
618 if ((ep
->idle
= now
- touched
) < 0)
628 (void)fprintf(stderr
,
629 "Usage: %s [-hinw] [-M core] [-N system] [user]\n",
632 (void)fprintf(stderr
, "Usage: %s\n", getprogname());