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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94";
45 * w - print system status (who and what)
47 * This program is similar to the systat command on Tenex/Tops 10/20
50 #include <sys/param.h>
53 #include <sys/sysctl.h>
56 #include <sys/ioctl.h>
57 #include <sys/socket.h>
60 #include <machine/cpu.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
63 #include <arpa/nameser.h>
88 struct timeval boottime;
92 time_t now; /* the current time of day */
93 int ttywidth; /* width of tty */
94 int argwidth; /* width of tty */
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 dflag; /* true if -d flag: output debug info */
98 int sortidle; /* sort by idle time */
99 int use_ampm; /* use AM/PM time */
100 int use_comma; /* use comma as floats separator */
101 char **sel_users; /* login array of particular users selected */
104 * One of these per active utmp entry.
109 dev_t tdev; /* dev_t of terminal */
110 time_t idle; /* idle time of terminal in seconds */
111 struct kinfo_proc *kp; /* `most interesting' proc */
112 char *args; /* arg list of interesting process */
113 struct kinfo_proc *dkp; /* debug option proc list */
114 } *ep, *ehead = NULL, **nextp = &ehead;
116 #define debugproc(p) *(&((struct kinfo_proc *)p)->ki_udata)
118 #define W_DISPUSERSIZE 10
119 #define W_DISPLINESIZE 8
120 #define W_DISPHOSTSIZE 24
122 static void pr_header(time_t *, int);
123 static struct stat *ttystat(char *);
124 static void usage(int);
125 static int this_is_uptime(const char *s);
127 char *fmt_argv(char **, char *, int); /* ../../bin/ps/fmt.c */
130 main(int argc, char *argv[])
132 struct kinfo_proc *kp;
133 struct kinfo_proc *dkp;
136 int ch, i, nentries, nusers, wcmd, longidle, longattime, dropgid;
137 const char *memf, *nlistf, *p;
139 char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
140 char fn[MAXHOSTNAMELEN];
143 (void)setlocale(LC_ALL, "");
144 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
145 use_comma = (*nl_langinfo(RADIXCHAR) != ',');
147 /* Are we w(1) or uptime(1)? */
148 if (this_is_uptime(argv[0]) == 0) {
157 memf = _PATH_DEVNULL;
159 while ((ch = getopt(argc, argv, p)) != -1)
182 case 'f': case 'l': case 's': case 'u': case 'w':
183 warnx("[-flsuw] no longer supported");
192 if (!(_res.options & RES_INIT))
194 _res.retrans = 2; /* resolver timeout to 2 seconds per try */
195 _res.retry = 1; /* only try once.. */
198 * Discard setgid privileges if not the running kernel so that bad
199 * guys can't print interesting stuff from kernel memory.
204 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
205 errx(1, "%s", errbuf);
213 for (nusers = 0; (utmp = getutxent()) != NULL;) {
214 if (utmp->ut_type != USER_PROCESS)
216 if (!(stp = ttystat(utmp->ut_line)))
217 continue; /* corrupted record */
226 for (user = sel_users; !usermatch && *user; user++)
227 if (!strcmp(utmp->ut_user, *user))
232 if ((ep = calloc(1, sizeof(struct entry))) == NULL)
236 memmove(&ep->utmp, utmp, sizeof *utmp);
237 ep->tdev = stp->st_rdev;
239 * If this is the console device, attempt to ascertain
240 * the true console device dev_t.
245 size = sizeof(dev_t);
246 (void)sysctlbyname("machdep.consdev", &ep->tdev, &size, NULL, 0);
248 touched = stp->st_atime;
249 if (touched < ep->utmp.ut_tv.tv_sec) {
250 /* tty untouched since before login */
251 touched = ep->utmp.ut_tv.tv_sec;
253 if ((ep->idle = now - touched) < 0)
258 if (header || wcmd == 0) {
259 pr_header(&now, nusers);
265 #define HEADER_USER "USER"
266 #define HEADER_TTY "TTY"
267 #define HEADER_FROM "FROM"
268 #define HEADER_LOGIN_IDLE "LOGIN@ IDLE "
269 #define HEADER_WHAT "WHAT\n"
270 #define WUSED (W_DISPUSERSIZE + W_DISPLINESIZE + W_DISPHOSTSIZE + \
271 sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */
272 (void)printf("%-*.*s %-*.*s %-*.*s %s",
273 W_DISPUSERSIZE, W_DISPUSERSIZE, HEADER_USER,
274 W_DISPLINESIZE, W_DISPLINESIZE, HEADER_TTY,
275 W_DISPHOSTSIZE, W_DISPHOSTSIZE, HEADER_FROM,
276 HEADER_LOGIN_IDLE HEADER_WHAT);
279 if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
280 err(1, "%s", kvm_geterr(kd));
281 for (i = 0; i < nentries; i++, kp++) {
282 if (kp->ki_stat == SIDL || kp->ki_stat == SZOMB ||
283 kp->ki_tdev == NODEV)
285 for (ep = ehead; ep != NULL; ep = ep->next) {
286 if (ep->tdev == kp->ki_tdev) {
288 * proc is associated with this terminal
290 if (ep->kp == NULL && kp->ki_pgid == kp->ki_tpgid) {
292 * Proc is 'most interesting'
294 if (proc_compare(ep->kp, kp))
298 * Proc debug option info; add to debug
299 * list using kinfo_proc ki_spare[0]
300 * as next pointer; ptr to ptr avoids the
301 * ptr = long assumption.
309 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
310 ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
311 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
314 ttywidth = ws.ws_col - 1;
315 argwidth = ttywidth - WUSED;
318 for (ep = ehead; ep != NULL; ep = ep->next) {
319 if (ep->kp == NULL) {
320 ep->args = strdup("-");
323 ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth),
324 ep->kp->ki_comm, MAXCOMLEN);
325 if (ep->args == NULL)
328 /* sort by idle time */
329 if (sortidle && ehead != NULL) {
330 struct entry *from, *save;
334 while (from != NULL) {
336 (*nextp) && from->idle >= (*nextp)->idle;
337 nextp = &(*nextp)->next)
346 for (ep = ehead; ep != NULL; ep = ep->next) {
347 struct addrinfo hints, *res;
348 struct sockaddr_storage ss;
349 struct sockaddr *sa = (struct sockaddr *)&ss;
350 struct sockaddr_in *lsin = (struct sockaddr_in *)&ss;
351 struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)&ss;
355 p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
356 if ((x_suffix = strrchr(p, ':')) != NULL) {
357 if ((dot = strchr(x_suffix, '.')) != NULL &&
358 strchr(dot+1, '.') == NULL)
365 memset(&ss, '\0', sizeof(ss));
366 if (inet_pton(AF_INET6, p, &lsin6->sin6_addr) == 1) {
367 lsin6->sin6_len = sizeof(*lsin6);
368 lsin6->sin6_family = AF_INET6;
370 } else if (inet_pton(AF_INET, p, &lsin->sin_addr) == 1) {
371 lsin->sin_len = sizeof(*lsin);
372 lsin->sin_family = AF_INET;
376 /* Attempt to change an IP address into a name */
377 if (isaddr && realhostname_sa(fn, sizeof(fn), sa,
378 sa->sa_len) == HOSTNAME_FOUND)
380 } else if (!isaddr) {
382 * If a host has only one A/AAAA RR, change a
383 * name into an IP address
385 memset(&hints, 0, sizeof(hints));
386 hints.ai_flags = AI_PASSIVE;
387 hints.ai_family = AF_UNSPEC;
388 hints.ai_socktype = SOCK_STREAM;
389 if (getaddrinfo(p, NULL, &hints, &res) == 0) {
390 if (res->ai_next == NULL &&
391 getnameinfo(res->ai_addr, res->ai_addrlen,
392 fn, sizeof(fn), NULL, 0,
393 NI_NUMERICHOST) == 0)
400 (void)snprintf(buf, sizeof(buf), "%s:%s", p, x_suffix);
404 for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
407 ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth),
408 dkp->ki_comm, MAXCOMLEN);
411 (void)printf("\t\t%-9d %s\n",
415 (void)printf("%-*.*s %-*.*s %-*.*s ",
416 W_DISPUSERSIZE, W_DISPUSERSIZE, ep->utmp.ut_user,
417 W_DISPLINESIZE, W_DISPLINESIZE,
419 (strncmp(ep->utmp.ut_line, "tty", 3) &&
420 strncmp(ep->utmp.ut_line, "cua", 3) ?
421 ep->utmp.ut_line : ep->utmp.ut_line + 3) : "-",
422 W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-");
423 t = ep->utmp.ut_tv.tv_sec;
424 longattime = pr_attime(&t, &now);
425 longidle = pr_idle(ep->idle);
426 (void)printf("%.*s\n", argwidth - longidle - longattime,
434 pr_header(time_t *nowp, int nusers)
439 int days, hrs, i, mins, secs;
445 if (strftime(buf, sizeof(buf),
446 use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)) != 0)
447 (void)printf("%s ", buf);
449 * Print how long system has been up.
451 if (clock_gettime(CLOCK_UPTIME, &tp) != -1) {
455 days = uptime / 86400;
463 (void)printf(" %d day%s,", days, days > 1 ? "s" : "");
464 if (hrs > 0 && mins > 0)
465 (void)printf(" %2d:%02d,", hrs, mins);
467 (void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
469 (void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
471 (void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
474 /* Print number of users logged in to system */
475 (void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s");
478 * Print 1, 5, and 15 minute load averages.
480 if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
481 (void)printf(", no load average information available\n");
483 (void)printf(", load averages:");
484 for (i = 0; i < (int)(sizeof(avenrun) / sizeof(avenrun[0])); i++) {
485 if (use_comma && i > 0)
487 (void)printf(" %.2f", avenrun[i]);
496 static struct stat sb;
497 char ttybuf[MAXPATHLEN];
499 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
500 if (stat(ttybuf, &sb) == 0 && S_ISCHR(sb.st_mode)) {
510 (void)fprintf(stderr,
511 "usage: w [-dhin] [-M core] [-N system] [user ...]\n");
513 (void)fprintf(stderr, "usage: uptime\n");
518 this_is_uptime(const char *s)
522 if ((u = strrchr(s, '/')) != NULL)
526 if (strcmp(u, "uptime") == 0)