xref: /dragonfly/usr.bin/w/w.c (revision cecb9aae)
1 /*-
2  * Copyright (c) 1980, 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  *
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 $
37  */
38 
39 /*
40  * w - print system status (who and what)
41  *
42  * This program is similar to the systat command on Tenex/Tops 10/20
43  *
44  */
45 #include <sys/user.h>
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <sys/stat.h>
49 #include <sys/sysctl.h>
50 #include <sys/ioctl.h>
51 #include <sys/socket.h>
52 #include <sys/tty.h>
53 
54 #include <machine/cpu.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 
58 #include <ctype.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <kvm.h>
63 #include <limits.h>
64 #include <langinfo.h>
65 #include <locale.h>
66 #include <netdb.h>
67 #include <nlist.h>
68 #include <paths.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <unistd.h>
73 #ifdef SUPPORT_UTMP
74 #include <utmp.h>
75 #endif
76 #ifdef SUPPORT_UTMPX
77 #include <utmpx.h>
78 #endif
79 #include <vis.h>
80 
81 #include <arpa/nameser.h>
82 #include <resolv.h>
83 
84 #include "extern.h"
85 
86 struct timeval	boottime;
87 #ifdef SUPPORT_UTMP
88 struct utmp	utmp;
89 #endif
90 struct winsize	ws;
91 kvm_t	       *kd;
92 time_t		now;		/* the current time of day */
93 time_t		then;
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;
106 
107 /*
108  * One of these per active utmp entry.
109  */
110 struct	entry {
111 	struct	entry *next;
112 	char name[UTX_USERSIZE + 1];
113 	char line[UTX_LINESIZE + 1];
114 	char host[UTX_HOSTSIZE + 1];
115 	char type[2];
116 	struct timeval tv;
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;
124 
125 #define debugproc(p) *((struct kinfo_proc **)&(p)->kp_spare[0])
126 
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 *);
131 #endif
132 static void		 usage(int);
133 static int		 this_is_uptime(const char *s);
134 
135 char *fmt_argv(char **, char *, int);	/* ../../bin/ps/fmt.c */
136 
137 int
138 main(int argc, char **argv)
139 {
140 	struct kinfo_proc *kp;
141 	struct kinfo_proc *dkp;
142 	struct hostent *hp;
143 	in_addr_t l;
144 	int ch, i, nentries, nusers, wcmd, longidle, dropgid;
145 	char *memf, *nlistf, *p, *x;
146 #ifdef SUPPORT_UTMP
147 	struct utmp *ut;
148 #endif
149 #ifdef SUPPORT_UTMPX
150 	struct utmpx *utx;
151 #endif
152 	char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
153 
154 	(void)setlocale(LC_ALL, "");
155 	use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
156 	use_comma = (*nl_langinfo(RADIXCHAR) != ',');
157 
158 	/* Are we w(1) or uptime(1)? */
159 	if (this_is_uptime(argv[0]) == 0) {
160 		wcmd = 0;
161 		p = "";
162 	} else {
163 		wcmd = 1;
164 		p = "dhiflM:N:nsuw";
165 	}
166 
167 	dropgid = 0;
168 	memf = nlistf = _PATH_DEVNULL;
169 	while ((ch = getopt(argc, argv, p)) != -1)
170 		switch (ch) {
171 		case 'd':
172 			dflag = 1;
173 			break;
174 		case 'h':
175 			header = 0;
176 			break;
177 		case 'i':
178 			sortidle = 1;
179 			break;
180 		case 'M':
181 			header = 0;
182 			memf = optarg;
183 			dropgid = 1;
184 			break;
185 		case 'N':
186 			nlistf = optarg;
187 			dropgid = 1;
188 			break;
189 		case 'n':
190 			nflag = 1;
191 			break;
192 		case 'f': case 'l': case 's': case 'u': case 'w':
193 			warnx("[-flsuw] no longer supported");
194 			/* FALLTHROUGH */
195 		case '?':
196 		default:
197 			usage(wcmd);
198 		}
199 	argc -= optind;
200 	argv += optind;
201 
202 	if (!(_res.options & RES_INIT))
203 		res_init();
204 	_res.retrans = 2;	/* resolver timeout to 2 seconds per try */
205 	_res.retry = 1;		/* only try once.. */
206 
207 	/*
208 	 * Discard setgid privileges if not the running kernel so that bad
209 	 * guys can't print interesting stuff from kernel memory.
210 	 */
211 	if (dropgid)
212 		setgid(getgid());
213 
214 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
215 		errx(1, "%s", errbuf);
216 
217 	(void)time(&now);
218 #ifdef SUPPORT_UTMPX
219 	setutxent();
220 #endif
221 #ifdef SUPPORT_UTMP
222 	setutent();
223 #endif
224 
225 	if (*argv)
226 		sel_users = argv;
227 
228 	nusers = 0;
229 #ifdef SUPPORT_UTMPX
230 	while ((utx = getutxent()) != NULL) {
231 		if (utx->ut_type != USER_PROCESS)
232 			continue;
233 		++nusers;
234 
235 		if (sel_users) {
236 			int usermatch;
237 			char **user;
238 
239 			usermatch = 0;
240 			for (user = sel_users; !usermatch && *user; user++)
241 				if (!strncmp(utx->ut_name, *user, UTX_USERSIZE))
242 					usermatch = 1;
243 			if (!usermatch)
244 				continue;
245 		}
246 
247 		if ((ep = calloc(1, sizeof(struct entry))) == NULL)
248 			err(1, 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';
255 #if 1
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';
263 		}
264 #endif
265 		ep->type[0] = 'x';
266 		ep->tv = utx->ut_tv;
267 		ep->pid = utx->ut_pid;
268 
269 		*nextp = ep;
270 		nextp = &(ep->next);
271 		if (wcmd != 0)
272 			process(ep);
273 	}
274 #endif
275 
276 #ifdef SUPPORT_UTMP
277 	while ((ut = getutent()) != NULL) {
278 		if (ut->ut_name[0] == '\0')
279 			continue;
280 		++nusers;
281 		if (sel_users) {
282 			int usermatch;
283 			char **user;
284 
285 			usermatch = 0;
286 			for (user = sel_users; !usermatch && *user; user++)
287 				if (!strncmp(ut->ut_name, *user, UT_NAMESIZE))
288 					usermatch = 1;
289 			if (!usermatch)
290 				continue;
291 		}
292 
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)
297 				break;
298 		}
299 		if (ep != NULL) {
300 			--nusers; /* Duplicate entry */
301 			continue;
302 		}
303 
304 		if ((ep = calloc(1, sizeof(struct entry))) == NULL)
305 			err(1, 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;
313 
314 		*nextp = ep;
315 		nextp = &(ep->next);
316 		if (wcmd != 0)
317 			process(ep);
318 	}
319 #endif
320 
321 #ifdef SUPPORT_UTMPX
322 	endutxent();
323 #endif
324 #ifdef SUPPORT_UTMP
325 	endutent();
326 #endif
327 
328 	if (header || wcmd == 0) {
329 		pr_header(&now, nusers);
330 		if (wcmd == 0) {
331 			(void)kvm_close(kd);
332 			exit(0);
333 		}
334 
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);
347 	}
348 
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)
353 			continue;
354 		for (ep = ehead; ep != NULL; ep = ep->next) {
355 			if (ep->tdev == kp->kp_tdev) {
356 				/*
357 				 * proc is associated with this terminal
358 				 */
359 				if (ep->kp == NULL && kp->kp_pgid == kp->kp_tpgid) {
360 					/*
361 					 * Proc is 'most interesting'
362 					 */
363 					if (proc_compare(ep->kp, kp))
364 						ep->kp = kp;
365 				}
366 				/*
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.
371 				 */
372 				dkp = ep->dkp;
373 				ep->dkp = kp;
374 				debugproc(kp) = dkp;
375 			}
376 			if (ep->pid != 0 && ep->pid == kp->kp_pid) {
377 				ep->kp = kp;
378 				break;
379 			}
380 		}
381 	}
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)
385 	       ttywidth = 79;
386         else
387 	       ttywidth = ws.ws_col - 1;
388 	argwidth = ttywidth - WUSED;
389 	if (argwidth < 4)
390 		argwidth = 8;
391 	for (ep = ehead; ep != NULL; ep = ep->next) {
392 		if (ep->kp == NULL) {
393 			ep->args = "-";
394 			continue;
395 		}
396 		ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth),
397 		    ep->kp->kp_comm, MAXCOMLEN);
398 		if (ep->args == NULL)
399 			err(1, NULL);
400 	}
401 	/* sort by idle time */
402 	if (sortidle && ehead != NULL) {
403 		struct entry *from, *save;
404 
405 		from = ehead;
406 		ehead = NULL;
407 		while (from != NULL) {
408 			for (nextp = &ehead;
409 			    (*nextp) && from->idle >= (*nextp)->idle;
410 			    nextp = &(*nextp)->next)
411 				continue;
412 			save = from;
413 			from = from->next;
414 			save->next = *nextp;
415 			*nextp = save;
416 		}
417 	}
418 #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
419 	else if (ehead != NULL) {
420 		struct entry *from = ehead, *save;
421 
422 		ehead = NULL;
423 		while (from != NULL) {
424 			for (nextp = &ehead;
425 			    (*nextp) && strcmp(from->line, (*nextp)->line) > 0;
426 			    nextp = &(*nextp)->next)
427 				continue;
428 			save = from;
429 			from = from->next;
430 			save->next = *nextp;
431 			*nextp = save;
432 		}
433 	}
434 #endif
435 
436 	if (!nflag) {
437 		if (gethostname(domain, sizeof(domain)) < 0 ||
438 		    (p = strchr(domain, '.')) == NULL)
439 			domain[0] = '\0';
440 		else {
441 			domain[sizeof(domain) - 1] = '\0';
442 			memmove(domain, p, strlen(p) + 1);
443 		}
444 	}
445 
446 	for (ep = ehead; ep != NULL; ep = ep->next) {
447 		char host_buf[UTX_HOSTSIZE + 1];
448 
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)
453 			*x++ = '\0';
454 		if (!nflag && isdigit(*p) && (l = inet_addr(p)) != -1 &&
455 		    (hp = gethostbyaddr(&l, sizeof(l), AF_INET))) {
456 			if (domain[0] != '\0') {
457 				p = hp->h_name;
458 				p += strlen(hp->h_name);
459 				p -= strlen(domain);
460 				if (p > hp->h_name &&
461 				    strcasecmp(p, domain) == 0)
462 					*p = '\0';
463 			}
464 			p = hp->h_name;
465 		}
466 		if (nflag && *p && strcmp(p, "-") &&
467 		    inet_addr(p) == INADDR_NONE) {
468 			hp = gethostbyname(p);
469 
470 			if (hp != NULL) {
471 				struct in_addr in;
472 
473 				memmove(&in, hp->h_addr, sizeof(in));
474 				p = inet_ntoa(in);
475 			}
476 		}
477 		if (x) {
478 			(void)snprintf(buf, sizeof(buf), "%s:%s", p, x);
479 			p = buf;
480 		}
481 		if (dflag) {
482 			for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
483 				char *ptr;
484 
485 				ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth),
486 				    dkp->kp_comm, MAXCOMLEN);
487 				if (ptr == NULL)
488 					ptr = "-";
489 				(void)printf("\t\t%-9d %s\n",
490 				    dkp->kp_pid, ptr);
491 			}
492 		}
493 		(void)printf("%-*.*s %-*.*s %-*.*s ",
494 		    maxname, maxname, ep->name,
495 		    maxline, maxline,
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);
504 	}
505 	(void)kvm_close(kd);
506 	exit(0);
507 }
508 
509 static void
510 pr_header(time_t *nowp, int nusers)
511 {
512 	double avenrun[3];
513 	time_t uptime;
514 	int days, hrs, i, mins, secs;
515 	int mib[2];
516 	size_t size;
517 	char buf[256];
518 
519 	/*
520 	 * Print time of day.
521 	 */
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);
526 
527 	/*
528 	 * Print how long system has been up.
529 	 * (Found by looking at "boottime" from the kernel)
530 	 */
531 	mib[0] = CTL_KERN;
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;
537 		if (uptime > 60)
538 			uptime += 30;
539 		days = uptime / 86400;
540 		uptime %= 86400;
541 		hrs = uptime / 3600;
542 		uptime %= 3600;
543 		mins = uptime / 60;
544 		secs = uptime % 60;
545 		(void)printf(" up");
546 		if (days > 0)
547 			(void)printf(" %d day%s,", days, days > 1 ? "s" : "");
548 		if (hrs > 0 && mins > 0)
549 			(void)printf(" %2d:%02d,", hrs, mins);
550 		else if (hrs > 0)
551 			(void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
552 		else if (mins > 0)
553 			(void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
554 		else
555 			(void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
556 	}
557 
558 	/* Print number of users logged in on system */
559 	(void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s");
560 
561 	/*
562 	 * Print 1, 5, and 15 minute load averages.
563 	 */
564 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
565 		(void)printf(", no load average information available\n");
566 	else {
567 		(void)printf(", load averages:");
568 		for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
569 			if (use_comma && i > 0)
570 				(void)printf(",");
571 			(void)printf(" %.2f", avenrun[i]);
572 		}
573 		(void)printf("\n");
574 	}
575 }
576 
577 static struct stat *
578 ttystat(char *line, int sz)
579 {
580 	static struct stat sb;
581 	char ttybuf[MAXPATHLEN];
582 
583 	(void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, line);
584 	if (stat(ttybuf, &sb)) {
585 		warn("%s", ttybuf);
586 		return (NULL);
587 	}
588 	return (&sb);
589 }
590 
591 static void
592 usage(int wcmd)
593 {
594 	if (wcmd)
595 		(void)fprintf(stderr,
596 		    "usage: w [-dhin] [-M core] [-N system] [user ...]\n");
597 	else
598 		(void)fprintf(stderr, "usage: uptime\n");
599 	exit(1);
600 }
601 
602 static int
603 this_is_uptime(const char *s)
604 {
605 	const char *u;
606 
607 	if ((u = strrchr(s, '/')) != NULL)
608 		++u;
609 	else
610 		u = s;
611 	if (strcmp(u, "uptime") == 0)
612 		return (0);
613 	return (-1);
614 }
615 
616 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
617 static void
618 process(struct entry *ep)
619 {
620 	struct stat *stp;
621 	time_t touched;
622 	int max;
623 
624 	if ((max = strlen(ep->name)) > maxname)
625 		maxname = max;
626 	if ((max = strlen(ep->line)) > maxline)
627 		maxline = max;
628 	if ((max = strlen(ep->host)) > maxhost)
629 		maxhost = max;
630 
631 	ep->tdev = 0;
632 	ep->idle = (time_t)-1;
633 
634 	if (!(stp = ttystat(ep->line, maxline)))
635 		return;	/* corrupted record */
636 
637 	ep->tdev = stp->st_rdev;
638 #ifdef CPU_CONSDEV
639 	/*
640 	 * If this is the console device, attempt to ascertain
641 	 * the true console device dev_t.
642 	 */
643 	if (ep->tdev == 0) {
644 		int mib[2];
645 		size_t size;
646 
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);
651 	}
652 #endif
653 
654 	touched = stp->st_atime;
655 	if (touched < ep->tv.tv_sec) {
656 		/* tty untouched since before login */
657 		touched = ep->tv.tv_sec;
658 	}
659 	if ((ep->idle = now - touched) < 0)
660 		ep->idle = 0;
661 }
662 #endif
663