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.2 2003/06/17 04:29:33 dillon Exp $
40 * w - print system status (who and what)
42 * This program is similar to the systat command on Tenex/Tops 10/20
45 #include <sys/param.h>
48 #include <sys/sysctl.h>
51 #include <sys/ioctl.h>
52 #include <sys/socket.h>
55 #include <machine/cpu.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
77 #include <arpa/nameser.h>
82 struct timeval boottime;
86 time_t now; /* the current time of day */
87 time_t uptime; /* time of last reboot & elapsed time since */
88 int ttywidth; /* width of tty */
89 int argwidth; /* width of tty */
90 int header = 1; /* true if -h flag: don't print heading */
91 int nflag; /* true if -n flag: don't convert addrs */
92 int dflag; /* true if -d flag: output debug info */
93 int sortidle; /* sort by idle time */
94 int use_ampm; /* use AM/PM time */
95 int use_comma; /* use comma as floats separator */
96 char **sel_users; /* login array of particular users selected */
97 char domain[MAXHOSTNAMELEN];
100 * One of these per active utmp entry.
105 dev_t tdev; /* dev_t of terminal */
106 time_t idle; /* idle time of terminal in seconds */
107 struct kinfo_proc *kp; /* `most interesting' proc */
108 char *args; /* arg list of interesting process */
109 struct kinfo_proc *dkp; /* debug option proc list */
110 } *ep, *ehead = NULL, **nextp = &ehead;
112 #define debugproc(p) *((struct kinfo_proc **)&(p)->kp_eproc.e_spare[0])
114 static void pr_header __P((time_t *, int));
115 static struct stat *ttystat __P((char *, int));
116 static void usage __P((int));
117 static int this_is_uptime __P((const char *s));
119 char *fmt_argv __P((char **, char *, int)); /* ../../bin/ps/fmt.c */
126 struct kinfo_proc *kp;
127 struct kinfo_proc *dkp;
133 int ch, i, nentries, nusers, wcmd, longidle, dropgid;
134 char *memf, *nlistf, *p, *x;
135 char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
137 (void)setlocale(LC_ALL, "");
138 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
139 use_comma = (*nl_langinfo(RADIXCHAR) != ',');
141 /* Are we w(1) or uptime(1)? */
142 if (this_is_uptime(argv[0]) == 0) {
151 memf = nlistf = _PATH_DEVNULL;
152 while ((ch = getopt(argc, argv, p)) != -1)
175 case 'f': case 'l': case 's': case 'u': case 'w':
176 warnx("[-flsuw] no longer supported");
185 if (!(_res.options & RES_INIT))
187 _res.retrans = 2; /* resolver timeout to 2 seconds per try */
188 _res.retry = 1; /* only try once.. */
191 * Discard setgid privileges if not the running kernel so that bad
192 * guys can't print interesting stuff from kernel memory.
197 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
198 errx(1, "%s", errbuf);
201 if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
202 err(1, "%s", _PATH_UTMP);
207 for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
208 if (utmp.ut_name[0] == '\0')
210 if (!(stp = ttystat(utmp.ut_line, UT_LINESIZE)))
211 continue; /* corrupted record */
220 for (user = sel_users; !usermatch && *user; user++)
221 if (!strncmp(utmp.ut_name, *user, UT_NAMESIZE))
226 if ((ep = calloc(1, sizeof(struct entry))) == NULL)
230 memmove(&ep->utmp, &utmp, sizeof(struct utmp));
231 ep->tdev = stp->st_rdev;
234 * If this is the console device, attempt to ascertain
235 * the true console device dev_t.
241 mib[0] = CTL_MACHDEP;
242 mib[1] = CPU_CONSDEV;
243 size = sizeof(dev_t);
244 (void)sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
247 touched = stp->st_atime;
248 if (touched < ep->utmp.ut_time) {
249 /* tty untouched since before login */
250 touched = ep->utmp.ut_time;
252 if ((ep->idle = now - touched) < 0)
257 if (header || wcmd == 0) {
258 pr_header(&now, nusers);
264 #define HEADER_USER "USER"
265 #define HEADER_TTY "TTY"
266 #define HEADER_FROM "FROM"
267 #define HEADER_LOGIN_IDLE "LOGIN@ IDLE "
268 #define HEADER_WHAT "WHAT\n"
269 #define WUSED (UT_NAMESIZE + UT_LINESIZE + UT_HOSTSIZE + \
270 sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */
271 (void)printf("%-*.*s %-*.*s %-*.*s %s",
272 UT_NAMESIZE, UT_NAMESIZE, HEADER_USER,
273 UT_LINESIZE, UT_LINESIZE, HEADER_TTY,
274 UT_HOSTSIZE, UT_HOSTSIZE, HEADER_FROM,
275 HEADER_LOGIN_IDLE HEADER_WHAT);
278 if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
279 err(1, "%s", kvm_geterr(kd));
280 for (i = 0; i < nentries; i++, kp++) {
281 struct proc *pr = &kp->kp_proc;
284 if (pr->p_stat == SIDL || pr->p_stat == SZOMB)
287 for (ep = ehead; ep != NULL; ep = ep->next) {
288 if (ep->tdev == e->e_tdev) {
290 * proc is associated with this terminal
292 if (ep->kp == NULL && e->e_pgid == e->e_tpgid) {
294 * Proc is 'most interesting'
296 if (proc_compare(&ep->kp->kp_proc, pr))
300 * Proc debug option info; add to debug
301 * list using kinfo_proc kp_eproc.e_spare
302 * as next pointer; ptr to ptr avoids the
303 * ptr = long assumption.
311 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
312 ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
313 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
316 ttywidth = ws.ws_col - 1;
317 argwidth = ttywidth - WUSED;
320 for (ep = ehead; ep != NULL; ep = ep->next) {
321 if (ep->kp == NULL) {
325 ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth),
326 ep->kp->kp_proc.p_comm, MAXCOMLEN);
327 if (ep->args == NULL)
330 /* sort by idle time */
331 if (sortidle && ehead != NULL) {
332 struct entry *from, *save;
336 while (from != NULL) {
338 (*nextp) && from->idle >= (*nextp)->idle;
339 nextp = &(*nextp)->next)
349 if (gethostname(domain, sizeof(domain) - 1) < 0 ||
350 (p = strchr(domain, '.')) == NULL)
353 domain[sizeof(domain) - 1] = '\0';
354 memmove(domain, p, strlen(p) + 1);
358 for (ep = ehead; ep != NULL; ep = ep->next) {
359 char host_buf[UT_HOSTSIZE + 1];
361 host_buf[UT_HOSTSIZE] = '\0';
362 strncpy(host_buf, ep->utmp.ut_host, UT_HOSTSIZE);
363 p = *host_buf ? host_buf : "-";
364 if ((x = strchr(p, ':')) != NULL)
366 if (!nflag && isdigit(*p) && (l = inet_addr(p)) != -1 &&
367 (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
368 if (domain[0] != '\0') {
370 p += strlen(hp->h_name);
372 if (p > hp->h_name &&
373 strcasecmp(p, domain) == 0)
378 if (nflag && *p && strcmp(p, "-") &&
379 inet_addr(p) == INADDR_NONE) {
380 hp = gethostbyname(p);
385 memmove(&in, hp->h_addr, sizeof(in));
390 (void)snprintf(buf, sizeof(buf), "%s:%s", p, x);
394 for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
397 ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth),
398 dkp->kp_proc.p_comm, MAXCOMLEN);
401 (void)printf("\t\t%-9d %s\n",
402 dkp->kp_proc.p_pid, ptr);
405 (void)printf("%-*.*s %-*.*s %-*.*s ",
406 UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
407 UT_LINESIZE, UT_LINESIZE,
408 strncmp(ep->utmp.ut_line, "tty", 3) &&
409 strncmp(ep->utmp.ut_line, "cua", 3) ?
410 ep->utmp.ut_line : ep->utmp.ut_line + 3,
411 UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-");
412 pr_attime(&ep->utmp.ut_time, &now);
413 longidle = pr_idle(ep->idle);
414 (void)printf("%.*s\n", argwidth - longidle, ep->args);
421 pr_header(nowp, nusers)
427 int days, hrs, i, mins, secs;
435 (void)strftime(buf, sizeof(buf) - 1,
436 use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp));
437 buf[sizeof(buf) - 1] = '\0';
438 (void)printf("%s ", buf);
441 * Print how long system has been up.
442 * (Found by looking getting "boottime" from the kernel)
445 mib[1] = KERN_BOOTTIME;
446 size = sizeof(boottime);
447 if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
448 boottime.tv_sec != 0) {
449 uptime = now - boottime.tv_sec;
452 days = uptime / 86400;
460 (void)printf(" %d day%s,", days, days > 1 ? "s" : "");
461 if (hrs > 0 && mins > 0)
462 (void)printf(" %2d:%02d,", hrs, mins);
464 (void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
466 (void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
468 (void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
471 /* Print number of users logged in to system */
472 (void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s");
475 * Print 1, 5, and 15 minute load averages.
477 if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
478 (void)printf(", no load average information available\n");
480 (void)printf(", load averages:");
481 for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
482 if (use_comma && i > 0)
484 (void)printf(" %.2f", avenrun[i]);
495 static struct stat sb;
496 char ttybuf[MAXPATHLEN];
498 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, line);
499 if (stat(ttybuf, &sb)) {
511 (void)fprintf(stderr,
512 "usage: w [-dhin] [-M core] [-N system] [user ...]\n");
514 (void)fprintf(stderr, "usage: uptime\n");
524 if ((u = strrchr(s, '/')) != NULL)
528 if (strcmp(u, "uptime") == 0)