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
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 static const char copyright[] =
40 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static const char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94";
49 * w - print system status (who and what)
51 * This program is similar to the systat command on Tenex/Tops 10/20
54 #include <sys/param.h>
57 #include <sys/sysctl.h>
60 #include <sys/ioctl.h>
61 #include <sys/socket.h>
64 #include <machine/cpu.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
67 #include <arpa/nameser.h>
92 struct timeval boottime;
96 time_t now; /* the current time of day */
97 int ttywidth; /* width of tty */
98 int argwidth; /* width of tty */
99 int header = 1; /* true if -h flag: don't print heading */
100 int nflag; /* true if -n flag: don't convert addrs */
101 int dflag; /* true if -d flag: output debug info */
102 int sortidle; /* sort by idle time */
103 int use_ampm; /* use AM/PM time */
104 int use_comma; /* use comma as floats separator */
105 char **sel_users; /* login array of particular users selected */
108 * One of these per active utmp entry.
113 dev_t tdev; /* dev_t of terminal */
114 time_t idle; /* idle time of terminal in seconds */
115 struct kinfo_proc *kp; /* `most interesting' proc */
116 char *args; /* arg list of interesting process */
117 struct kinfo_proc *dkp; /* debug option proc list */
118 } *ep, *ehead = NULL, **nextp = &ehead;
120 #define debugproc(p) *((struct kinfo_proc **)&(p)->ki_spare[0])
122 /* W_DISPHOSTSIZE should not be greater than UT_HOSTSIZE */
123 #define W_DISPHOSTSIZE 16
125 static void pr_header(time_t *, int);
126 static struct stat *ttystat(char *, int);
127 static void usage(int);
128 static int this_is_uptime(const char *s);
130 char *fmt_argv(char **, char *, int); /* ../../bin/ps/fmt.c */
133 main(int argc, char *argv[])
135 struct kinfo_proc *kp;
136 struct kinfo_proc *dkp;
140 int ch, i, nentries, nusers, wcmd, longidle, dropgid;
141 const char *memf, *nlistf, *p;
143 char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
144 char fn[MAXHOSTNAMELEN];
147 (void)setlocale(LC_ALL, "");
148 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
149 use_comma = (*nl_langinfo(RADIXCHAR) != ',');
151 /* Are we w(1) or uptime(1)? */
152 if (this_is_uptime(argv[0]) == 0) {
161 memf = nlistf = _PATH_DEVNULL;
162 while ((ch = getopt(argc, argv, p)) != -1)
185 case 'f': case 'l': case 's': case 'u': case 'w':
186 warnx("[-flsuw] no longer supported");
195 if (!(_res.options & RES_INIT))
197 _res.retrans = 2; /* resolver timeout to 2 seconds per try */
198 _res.retry = 1; /* only try once.. */
201 * Discard setgid privileges if not the running kernel so that bad
202 * guys can't print interesting stuff from kernel memory.
207 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
208 errx(1, "%s", errbuf);
211 if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
212 err(1, "%s", _PATH_UTMP);
217 for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
218 if (utmp.ut_name[0] == '\0')
220 if (!(stp = ttystat(utmp.ut_line, UT_LINESIZE)))
221 continue; /* corrupted record */
230 for (user = sel_users; !usermatch && *user; user++)
231 if (!strncmp(utmp.ut_name, *user, UT_NAMESIZE))
236 if ((ep = calloc(1, sizeof(struct entry))) == NULL)
240 memmove(&ep->utmp, &utmp, sizeof(struct utmp));
241 ep->tdev = stp->st_rdev;
244 * If this is the console device, attempt to ascertain
245 * the true console device dev_t.
251 mib[0] = CTL_MACHDEP;
252 mib[1] = CPU_CONSDEV;
253 size = sizeof(dev_t);
254 (void)sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
257 touched = stp->st_atime;
258 if (touched < ep->utmp.ut_time) {
259 /* tty untouched since before login */
260 touched = ep->utmp.ut_time;
262 if ((ep->idle = now - touched) < 0)
267 if (header || wcmd == 0) {
268 pr_header(&now, nusers);
274 #define HEADER_USER "USER"
275 #define HEADER_TTY "TTY"
276 #define HEADER_FROM "FROM"
277 #define HEADER_LOGIN_IDLE "LOGIN@ IDLE "
278 #define HEADER_WHAT "WHAT\n"
279 #define WUSED (UT_NAMESIZE + UT_LINESIZE + W_DISPHOSTSIZE + \
280 sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */
281 (void)printf("%-*.*s %-*.*s %-*.*s %s",
282 UT_NAMESIZE, UT_NAMESIZE, HEADER_USER,
283 UT_LINESIZE, UT_LINESIZE, HEADER_TTY,
284 W_DISPHOSTSIZE, W_DISPHOSTSIZE, HEADER_FROM,
285 HEADER_LOGIN_IDLE HEADER_WHAT);
288 if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
289 err(1, "%s", kvm_geterr(kd));
290 for (i = 0; i < nentries; i++, kp++) {
291 if (kp->ki_stat == SIDL || kp->ki_stat == SZOMB)
293 for (ep = ehead; ep != NULL; ep = ep->next) {
294 if (ep->tdev == kp->ki_tdev) {
296 * proc is associated with this terminal
298 if (ep->kp == NULL && kp->ki_pgid == kp->ki_tpgid) {
300 * Proc is 'most interesting'
302 if (proc_compare(ep->kp, kp))
306 * Proc debug option info; add to debug
307 * list using kinfo_proc ki_spare[0]
308 * as next pointer; ptr to ptr avoids the
309 * ptr = long assumption.
317 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
318 ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
319 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
322 ttywidth = ws.ws_col - 1;
323 argwidth = ttywidth - WUSED;
326 for (ep = ehead; ep != NULL; ep = ep->next) {
327 if (ep->kp == NULL) {
328 ep->args = strdup("-");
331 ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth),
332 ep->kp->ki_comm, MAXCOMLEN);
333 if (ep->args == NULL)
336 /* sort by idle time */
337 if (sortidle && ehead != NULL) {
338 struct entry *from, *save;
342 while (from != NULL) {
344 (*nextp) && from->idle >= (*nextp)->idle;
345 nextp = &(*nextp)->next)
354 for (ep = ehead; ep != NULL; ep = ep->next) {
355 char host_buf[UT_HOSTSIZE + 1];
356 struct sockaddr_storage ss;
357 struct sockaddr *sa = (struct sockaddr *)&ss;
358 struct sockaddr_in *lsin = (struct sockaddr_in *)&ss;
359 struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)&ss;
363 host_buf[UT_HOSTSIZE] = '\0';
364 strncpy(host_buf, ep->utmp.ut_host, UT_HOSTSIZE);
365 p = *host_buf ? host_buf : "-";
366 if ((x_suffix = strrchr(p, ':')) != NULL) {
367 if ((dot = strchr(x_suffix, '.')) != NULL &&
368 strchr(dot+1, '.') == NULL)
374 /* Attempt to change an IP address into a name */
376 memset(&ss, '\0', sizeof(ss));
377 if (inet_pton(AF_INET6, p, &lsin6->sin6_addr) == 1) {
378 lsin6->sin6_len = sizeof(*lsin6);
379 lsin6->sin6_family = AF_INET6;
381 } else if (inet_pton(AF_INET, p, &lsin->sin_addr) == 1) {
382 lsin->sin_len = sizeof(*lsin);
383 lsin->sin_family = AF_INET;
386 if (isaddr && realhostname_sa(fn, sizeof(fn), sa,
387 sa->sa_len) == HOSTNAME_FOUND)
391 (void)snprintf(buf, sizeof(buf), "%s:%s", p, x_suffix);
395 for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
398 ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth),
399 dkp->ki_comm, MAXCOMLEN);
402 (void)printf("\t\t%-9d %s\n",
406 (void)printf("%-*.*s %-*.*s %-*.*s ",
407 UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
408 UT_LINESIZE, UT_LINESIZE,
409 strncmp(ep->utmp.ut_line, "tty", 3) &&
410 strncmp(ep->utmp.ut_line, "cua", 3) ?
411 ep->utmp.ut_line : ep->utmp.ut_line + 3,
412 W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-");
413 t = _time_to_time32(ep->utmp.ut_time);
415 longidle = pr_idle(ep->idle);
416 (void)printf("%.*s\n", argwidth - longidle, ep->args);
423 pr_header(time_t *nowp, int nusers)
427 int days, hrs, i, mins, secs;
435 if (strftime(buf, sizeof(buf),
436 use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)) != 0)
437 (void)printf("%s ", buf);
439 * Print how long system has been up.
440 * (Found by looking getting "boottime" from the kernel)
443 mib[1] = KERN_BOOTTIME;
444 size = sizeof(boottime);
445 if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
446 boottime.tv_sec != 0) {
447 uptime = now - boottime.tv_sec;
450 days = uptime / 86400;
458 (void)printf(" %d day%s,", days, days > 1 ? "s" : "");
459 if (hrs > 0 && mins > 0)
460 (void)printf(" %2d:%02d,", hrs, mins);
462 (void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
464 (void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
466 (void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
469 /* Print number of users logged in to system */
470 (void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s");
473 * Print 1, 5, and 15 minute load averages.
475 if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
476 (void)printf(", no load average information available\n");
478 (void)printf(", load averages:");
479 for (i = 0; i < (int)(sizeof(avenrun) / sizeof(avenrun[0])); i++) {
480 if (use_comma && i > 0)
482 (void)printf(" %.2f", avenrun[i]);
489 ttystat(char *line, int sz)
491 static struct stat sb;
492 char ttybuf[MAXPATHLEN];
494 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, line);
495 if (stat(ttybuf, &sb) == 0) {
505 (void)fprintf(stderr,
506 "usage: w [-dhin] [-M core] [-N system] [user ...]\n");
508 (void)fprintf(stderr, "usage: uptime\n");
513 this_is_uptime(const char *s)
517 if ((u = strrchr(s, '/')) != NULL)
521 if (strcmp(u, "uptime") == 0)