2 * Copyright (c) 1980, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)w.c 8.4 (Berkeley) 4/16/94
35 * $FreeBSD: src/usr.bin/w/w.c,v 1.38.2.6 2002/03/12 19:51:51 phantom Exp $
36 * $DragonFly: src/usr.bin/w/w.c,v 1.10 2007/02/18 16:15:24 corecode Exp $
40 * w - print system status (who and what)
42 * This program is similar to the systat command on Tenex/Tops 10/20
46 #include <sys/param.h>
49 #include <sys/sysctl.h>
50 #include <sys/ioctl.h>
51 #include <sys/socket.h>
54 #include <machine/cpu.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
81 #include <arpa/nameser.h>
86 struct timeval boottime;
92 time_t now; /* the current time of day */
94 time_t uptime; /* time of last reboot & elapsed time since */
95 int ttywidth; /* width of tty */
96 int argwidth; /* width of tty */
97 int header = 1; /* true if -h flag: don't print heading */
98 int nflag; /* true if -n flag: don't convert addrs */
99 int dflag; /* true if -d flag: output debug info */
100 int sortidle; /* sort by idle time */
101 int use_ampm; /* use AM/PM time */
102 int use_comma; /* use comma as floats separator */
103 char **sel_users; /* login array of particular users selected */
104 char domain[MAXHOSTNAMELEN];
105 int maxname = 8, maxline = 3, maxhost = 16;
108 * One of these per active utmp entry.
112 char name[UTX_USERSIZE + 1];
113 char line[UTX_LINESIZE + 1];
114 char host[UTX_HOSTSIZE + 1];
117 dev_t tdev; /* dev_t of terminal */
118 time_t idle; /* idle time of terminal in seconds */
119 struct kinfo_proc *kp; /* `most interesting' proc */
120 char *args; /* arg list of interesting process */
121 struct kinfo_proc *dkp; /* debug option proc list */
122 pid_t pid; /* pid or ~0 if not known */
123 } *ep, *ehead = NULL, **nextp = &ehead;
125 #define debugproc(p) *((struct kinfo_proc **)&(p)->kp_spare[0])
127 static void pr_header(time_t *, int);
128 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
129 static struct stat *ttystat(char *, int);
130 static void process(struct entry *);
132 static void usage(int);
133 static int this_is_uptime(const char *s);
135 char *fmt_argv(char **, char *, int); /* ../../bin/ps/fmt.c */
138 main(int argc, char **argv)
140 struct kinfo_proc *kp;
141 struct kinfo_proc *dkp;
144 int ch, i, nentries, nusers, wcmd, longidle, dropgid;
145 char *memf, *nlistf, *p, *x;
152 char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
154 (void)setlocale(LC_ALL, "");
155 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
156 use_comma = (*nl_langinfo(RADIXCHAR) != ',');
158 /* Are we w(1) or uptime(1)? */
159 if (this_is_uptime(argv[0]) == 0) {
168 memf = nlistf = _PATH_DEVNULL;
169 while ((ch = getopt(argc, argv, p)) != -1)
192 case 'f': case 'l': case 's': case 'u': case 'w':
193 warnx("[-flsuw] no longer supported");
202 if (!(_res.options & RES_INIT))
204 _res.retrans = 2; /* resolver timeout to 2 seconds per try */
205 _res.retry = 1; /* only try once.. */
208 * Discard setgid privileges if not the running kernel so that bad
209 * guys can't print interesting stuff from kernel memory.
214 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
215 errx(1, "%s", errbuf);
230 while ((utx = getutxent()) != NULL) {
231 if (utx->ut_type != USER_PROCESS)
240 for (user = sel_users; !usermatch && *user; user++)
241 if (!strncmp(utx->ut_name, *user, UTX_USERSIZE))
247 if ((ep = calloc(1, sizeof(struct entry))) == NULL)
249 (void)memcpy(ep->name, utx->ut_name, sizeof(utx->ut_name));
250 (void)memcpy(ep->line, utx->ut_line, sizeof(utx->ut_line));
251 (void)memcpy(ep->host, utx->ut_host, sizeof(utx->ut_host));
252 ep->name[sizeof(utx->ut_name)] = '\0';
253 ep->line[sizeof(utx->ut_line)] = '\0';
254 ep->host[sizeof(utx->ut_host)] = '\0';
256 /* XXX: Actually we don't support the utx->ut_ss stuff yet */
257 if (!nflag || getnameinfo((struct sockaddr *)&utx->ut_ss,
258 utx->ut_ss.ss_len, ep->host, sizeof(ep->host), NULL, 0,
259 NI_NUMERICHOST) != 0) {
260 (void)memcpy(ep->host, utx->ut_host,
261 sizeof(utx->ut_host));
262 ep->host[sizeof(utx->ut_host)] = '\0';
267 ep->pid = utx->ut_pid;
277 while ((ut = getutent()) != NULL) {
278 if (ut->ut_name[0] == '\0')
286 for (user = sel_users; !usermatch && *user; user++)
287 if (!strncmp(ut->ut_name, *user, UT_NAMESIZE))
293 /* Don't process entries that we have utmpx for */
294 for (ep = ehead; ep != NULL; ep = ep->next) {
295 if (strncmp(ep->line, ut->ut_line,
296 sizeof(ut->ut_line)) == 0)
300 --nusers; /* Duplicate entry */
304 if ((ep = calloc(1, sizeof(struct entry))) == NULL)
306 (void)memcpy(ep->name, ut->ut_name, sizeof(ut->ut_name));
307 (void)memcpy(ep->line, ut->ut_line, sizeof(ut->ut_line));
308 (void)memcpy(ep->host, ut->ut_host, sizeof(ut->ut_host));
309 ep->name[sizeof(ut->ut_name)] = '\0';
310 ep->line[sizeof(ut->ut_line)] = '\0';
311 ep->host[sizeof(ut->ut_host)] = '\0';
312 ep->tv.tv_sec = ut->ut_time;
328 if (header || wcmd == 0) {
329 pr_header(&now, nusers);
335 #define HEADER_USER "USER"
336 #define HEADER_TTY "TTY"
337 #define HEADER_FROM "FROM"
338 #define HEADER_LOGIN_IDLE "LOGIN@ IDLE "
339 #define HEADER_WHAT "WHAT\n"
340 #define WUSED (maxname + maxline + maxhost + \
341 sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */
342 (void)printf("%-*.*s %-*.*s %-*.*s %s",
343 maxname, maxname, HEADER_USER,
344 maxline, maxline, HEADER_TTY,
345 maxhost, maxhost, HEADER_FROM,
346 HEADER_LOGIN_IDLE HEADER_WHAT);
349 if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
350 err(1, "%s", kvm_geterr(kd));
351 for (i = 0; i < nentries; i++, kp++) {
352 if (kp->kp_stat == SIDL || kp->kp_stat == SZOMB)
354 for (ep = ehead; ep != NULL; ep = ep->next) {
355 if (ep->tdev == kp->kp_tdev) {
357 * proc is associated with this terminal
359 if (ep->kp == NULL && kp->kp_pgid == kp->kp_tpgid) {
361 * Proc is 'most interesting'
363 if (proc_compare(ep->kp, kp))
367 * Proc debug option info; add to debug
368 * list using kinfo_proc kp_eproc.e_spare
369 * as next pointer; ptr to ptr avoids the
370 * ptr = long assumption.
376 if (ep->pid != 0 && ep->pid == kp->kp_pid) {
382 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
383 ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
384 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
387 ttywidth = ws.ws_col - 1;
388 argwidth = ttywidth - WUSED;
391 for (ep = ehead; ep != NULL; ep = ep->next) {
392 if (ep->kp == NULL) {
396 ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth),
397 ep->kp->kp_comm, MAXCOMLEN);
398 if (ep->args == NULL)
401 /* sort by idle time */
402 if (sortidle && ehead != NULL) {
403 struct entry *from, *save;
407 while (from != NULL) {
409 (*nextp) && from->idle >= (*nextp)->idle;
410 nextp = &(*nextp)->next)
418 #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
419 else if (ehead != NULL) {
420 struct entry *from = ehead, *save;
423 while (from != NULL) {
425 (*nextp) && strcmp(from->line, (*nextp)->line) > 0;
426 nextp = &(*nextp)->next)
437 if (gethostname(domain, sizeof(domain)) < 0 ||
438 (p = strchr(domain, '.')) == NULL)
441 domain[sizeof(domain) - 1] = '\0';
442 memmove(domain, p, strlen(p) + 1);
446 for (ep = ehead; ep != NULL; ep = ep->next) {
447 char host_buf[UTX_HOSTSIZE + 1];
449 host_buf[UTX_HOSTSIZE] = '\0';
450 strncpy(host_buf, ep->host, maxhost);
451 p = *host_buf ? host_buf : "-";
452 if ((x = strchr(p, ':')) != NULL)
454 if (!nflag && isdigit(*p) && (l = inet_addr(p)) != -1 &&
455 (hp = gethostbyaddr(&l, sizeof(l), AF_INET))) {
456 if (domain[0] != '\0') {
458 p += strlen(hp->h_name);
460 if (p > hp->h_name &&
461 strcasecmp(p, domain) == 0)
466 if (nflag && *p && strcmp(p, "-") &&
467 inet_addr(p) == INADDR_NONE) {
468 hp = gethostbyname(p);
473 memmove(&in, hp->h_addr, sizeof(in));
478 (void)snprintf(buf, sizeof(buf), "%s:%s", p, x);
482 for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
485 ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth),
486 dkp->kp_comm, MAXCOMLEN);
489 (void)printf("\t\t%-9d %s\n",
493 (void)printf("%-*.*s %-*.*s %-*.*s ",
494 maxname, maxname, ep->name,
496 strncmp(ep->line, "tty", 3) &&
497 strncmp(ep->line, "cua", 3) ?
498 ep->line : ep->line + 3,
499 maxhost, maxhost, *p ? p : "-");
500 then = (time_t)ep->tv.tv_sec;
501 pr_attime(&then, &now);
502 longidle = pr_idle(ep->idle);
503 (void)printf("%.*s\n", argwidth - longidle, ep->args);
510 pr_header(time_t *nowp, int nusers)
514 int days, hrs, i, mins, secs;
522 (void)strftime(buf, sizeof(buf) - 1,
523 use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp));
524 buf[sizeof(buf) - 1] = '\0';
525 (void)printf("%s ", buf);
528 * Print how long system has been up.
529 * (Found by looking at "boottime" from the kernel)
532 mib[1] = KERN_BOOTTIME;
533 size = sizeof(boottime);
534 if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
535 boottime.tv_sec != 0) {
536 uptime = now - boottime.tv_sec;
539 days = uptime / 86400;
547 (void)printf(" %d day%s,", days, days > 1 ? "s" : "");
548 if (hrs > 0 && mins > 0)
549 (void)printf(" %2d:%02d,", hrs, mins);
551 (void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
553 (void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
555 (void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
558 /* Print number of users logged in on system */
559 (void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s");
562 * Print 1, 5, and 15 minute load averages.
564 if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
565 (void)printf(", no load average information available\n");
567 (void)printf(", load averages:");
568 for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
569 if (use_comma && i > 0)
571 (void)printf(" %.2f", avenrun[i]);
578 ttystat(char *line, int sz)
580 static struct stat sb;
581 char ttybuf[MAXPATHLEN];
583 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, line);
584 if (stat(ttybuf, &sb)) {
595 (void)fprintf(stderr,
596 "usage: w [-dhin] [-M core] [-N system] [user ...]\n");
598 (void)fprintf(stderr, "usage: uptime\n");
603 this_is_uptime(const char *s)
607 if ((u = strrchr(s, '/')) != NULL)
611 if (strcmp(u, "uptime") == 0)
616 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
618 process(struct entry *ep)
624 if ((max = strlen(ep->name)) > maxname)
626 if ((max = strlen(ep->line)) > maxline)
628 if ((max = strlen(ep->host)) > maxhost)
632 ep->idle = (time_t)-1;
634 if (!(stp = ttystat(ep->line, maxline)))
635 return; /* corrupted record */
637 ep->tdev = stp->st_rdev;
640 * If this is the console device, attempt to ascertain
641 * the true console device dev_t.
647 mib[0] = CTL_MACHDEP;
648 mib[1] = CPU_CONSDEV;
649 size = sizeof(dev_t);
650 (void)sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
654 touched = stp->st_atime;
655 if (touched < ep->tv.tv_sec) {
656 /* tty untouched since before login */
657 touched = ep->tv.tv_sec;
659 if ((ep->idle = now - touched) < 0)