xref: /netbsd-src/usr.bin/who/who.c (revision 301b67f32336906592aa54e2b21570b98e8fd96f)
1 /*	$NetBSD: who.c,v 1.25 2015/11/21 15:01:43 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Michael Fischbein.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38  The Regents of the University of California.  All rights reserved.");
39 #endif /* not lint */
40 
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)who.c	8.1 (Berkeley) 6/6/93";
44 #endif
45 __RCSID("$NetBSD: who.c,v 1.25 2015/11/21 15:01:43 christos Exp $");
46 #endif /* not lint */
47 
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 
51 #include <err.h>
52 #include <locale.h>
53 #include <pwd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58 #include <unistd.h>
59 #ifdef SUPPORT_UTMP
60 #include <utmp.h>
61 #endif
62 #ifdef SUPPORT_UTMPX
63 #include <utmpx.h>
64 #endif
65 
66 #include "utmpentry.h"
67 
68 static void output_labels(void);
69 static void who_am_i(const char *, int);
70 static void usage(void) __dead;
71 static void process(const char *, int);
72 static void eprint(const struct utmpentry *);
73 static void print(const char *, const char *, time_t, const char *, pid_t pid,
74     uint16_t term, uint16_t xit, uint16_t sess, uint16_t type);
75 static void quick(const char *);
76 
77 static int show_term;			/* show term state */
78 static int show_idle;			/* show idle time */
79 static int show_details;		/* show exit status etc. */
80 
81 struct ut_type_names {
82   int type;
83   const char *name;
84 } ut_type_names[] = {
85 #ifdef SUPPORT_UTMPX
86   { EMPTY, "empty" },
87   { RUN_LVL, "run level" },
88   { BOOT_TIME, "boot time" },
89   { OLD_TIME, "old time" },
90   { NEW_TIME, "new time" },
91   { INIT_PROCESS, "init process" },
92   { LOGIN_PROCESS, "login process" },
93   { USER_PROCESS, "user process" },
94   { DEAD_PROCESS, "dead process" },
95 #if defined(_NETBSD_SOURCE)
96   { ACCOUNTING, "accounting" },
97   { SIGNATURE, "signature" },
98   { DOWN_TIME, "down time" },
99 #endif /* _NETBSD_SOURCE */
100 #endif /* SUPPORT_UTMPX */
101   { -1, "unknown" }
102 };
103 
104 int
main(int argc,char * argv[])105 main(int argc, char *argv[])
106 {
107 	int c, only_current_term, show_labels, quick_mode, default_mode;
108 	int et = 0;
109 
110 	setlocale(LC_ALL, "");
111 
112 	only_current_term = show_term = show_idle = show_labels = 0;
113 	quick_mode = default_mode = 0;
114 
115 	while ((c = getopt(argc, argv, "abdHlmpqrsTtuv")) != -1) {
116 		switch (c) {
117 		case 'a':
118 			et = -1;
119 			show_idle = show_details = 1;
120 			break;
121 		case 'b':
122 			et |= (1 << BOOT_TIME);
123 			break;
124 		case 'd':
125 			et |= (1 << DEAD_PROCESS);
126 			break;
127 		case 'H':
128 			show_labels = 1;
129 			break;
130 		case 'l':
131 			et |= (1 << LOGIN_PROCESS);
132 			break;
133 		case 'm':
134 			only_current_term = 1;
135 			break;
136 		case 'p':
137 			et |= (1 << INIT_PROCESS);
138 			break;
139 		case 'q':
140 			quick_mode = 1;
141 			break;
142 		case 'r':
143 			et |= (1 << RUN_LVL);
144 			break;
145 		case 's':
146 			default_mode = 1;
147 			break;
148 		case 'T':
149 			show_term = 1;
150 			break;
151 		case 't':
152 			et |= (1 << NEW_TIME);
153 			break;
154 		case 'u':
155 			show_idle = 1;
156 			break;
157 		case 'v':
158 			show_details = 1;
159 			break;
160 		default:
161 			usage();
162 			/* NOTREACHED */
163 		}
164 	}
165 	argc -= optind;
166 	argv += optind;
167 
168 	if (et != 0)
169 		etype = et;
170 
171 	if (chdir("/dev")) {
172 		err(EXIT_FAILURE, "cannot change directory to /dev");
173 		/* NOTREACHED */
174 	}
175 
176 	if (default_mode)
177 		only_current_term = show_term = show_idle = 0;
178 
179 	switch (argc) {
180 	case 0:					/* who */
181 		if (quick_mode) {
182 			quick(NULL);
183 		} else if (only_current_term) {
184 			who_am_i(NULL, show_labels);
185 		} else {
186 			process(NULL, show_labels);
187 		}
188 		break;
189 	case 1:					/* who utmp_file */
190 		if (quick_mode) {
191 			quick(*argv);
192 		} else if (only_current_term) {
193 			who_am_i(*argv, show_labels);
194 		} else {
195 			process(*argv, show_labels);
196 		}
197 		break;
198 	case 2:					/* who am i */
199 		who_am_i(NULL, show_labels);
200 		break;
201 	default:
202 		usage();
203 		/* NOTREACHED */
204 	}
205 
206 	return 0;
207 }
208 
209 static char *
strrstr(const char * str,const char * pat)210 strrstr(const char *str, const char *pat)
211 {
212 	const char *estr;
213 	size_t len;
214 	if (*pat == '\0')
215 		return __UNCONST(str);
216 
217 	len = strlen(pat);
218 
219 	for (estr = str + strlen(str); str < estr; estr--)
220 		if (strncmp(estr, pat, len) == 0)
221 			return __UNCONST(estr);
222 	return NULL;
223 }
224 
225 static void
who_am_i(const char * fname,int show_labels)226 who_am_i(const char *fname, int show_labels)
227 {
228 	struct passwd *pw;
229 	const char *p;
230 	char *t;
231 	time_t now;
232 	struct utmpentry *ehead, *ep;
233 
234 	/* search through the utmp and find an entry for this tty */
235 	if ((p = ttyname(STDIN_FILENO)) != NULL) {
236 
237 		/* strip directory prefixes for ttys */
238 		if ((t = strrstr(p, "/pts/")) != NULL ||
239 		    (t = strrchr(p, '/')) != NULL)
240 			p = t + 1;
241 
242 		(void)getutentries(fname, &ehead);
243 		for (ep = ehead; ep; ep = ep->next)
244 			if (strcmp(ep->line, p) == 0) {
245 				if (show_labels)
246 					output_labels();
247 				eprint(ep);
248 				return;
249 			}
250 	} else
251 		p = "tty??";
252 
253 	(void)time(&now);
254 	pw = getpwuid(getuid());
255 	if (show_labels)
256 		output_labels();
257 	print(pw ? pw->pw_name : "?", p, now, "", getpid(), 0, 0, 0, 0);
258 }
259 
260 static void
process(const char * fname,int show_labels)261 process(const char *fname, int show_labels)
262 {
263 	struct utmpentry *ehead, *ep;
264 	(void)getutentries(fname, &ehead);
265 	if (show_labels)
266 		output_labels();
267 	for (ep = ehead; ep != NULL; ep = ep->next)
268 		eprint(ep);
269 }
270 
271 static void
eprint(const struct utmpentry * ep)272 eprint(const struct utmpentry *ep)
273 {
274 	print(ep->name, ep->line, (time_t)ep->tv.tv_sec, ep->host, ep->pid,
275 	    ep->term, ep->exit, ep->sess, ep->type);
276 }
277 
278 static void
print(const char * name,const char * line,time_t t,const char * host,pid_t pid,uint16_t term,uint16_t xit,uint16_t sess,uint16_t type)279 print(const char *name, const char *line, time_t t, const char *host,
280     pid_t pid, uint16_t term, uint16_t xit, uint16_t sess, uint16_t type)
281 {
282 	struct stat sb;
283 	char state;
284 	static time_t now = 0;
285 	time_t idle;
286 	const char *types = NULL;
287 	size_t i;
288 	char *tstr;
289 
290 	state = '?';
291 	idle = 0;
292 
293 	for (i = 0; ut_type_names[i].type >= 0; i++) {
294 		types = ut_type_names[i].name;
295 		if (ut_type_names[i].type == type)
296 			break;
297 	}
298 
299 	if (show_term || show_idle) {
300 		if (now == 0)
301 			time(&now);
302 
303 		if (stat(line, &sb) == 0) {
304 			state = (sb.st_mode & 020) ? '+' : '-';
305 			idle = now - sb.st_atime;
306 		}
307 
308 	}
309 
310 	(void)printf("%-*.*s ", (int)maxname, (int)maxname, name);
311 
312 	if (show_term)
313 		(void)printf("%c ", state);
314 
315 	(void)printf("%-*.*s ", (int)maxline, (int)maxline, line);
316 	tstr = ctime(&t);
317 	(void)printf("%.12s ", tstr ? tstr + 4 : "?");
318 
319 	if (show_idle) {
320 		if (idle < 60)
321 			(void)printf("  .   ");
322 		else if (idle < (24 * 60 * 60))
323 			(void)printf("%02ld:%02ld ",
324 				     (long)(idle / (60 * 60)),
325 				     (long)(idle % (60 * 60)) / 60);
326 		else
327 			(void)printf(" old  ");
328 
329 		(void)printf("\t%6d", pid);
330 
331 		if (show_details) {
332 			if (type == RUN_LVL)
333 				(void)printf("\tnew=%c old=%c", term, xit);
334 			else
335 				(void)printf("\tterm=%d exit=%d", term, xit);
336 			(void)printf(" sess=%d", sess);
337 			(void)printf(" type=%s ", types);
338 		}
339 	}
340 
341 	if (*host)
342 		(void)printf("\t(%.*s)", (int)maxhost, host);
343 	(void)putchar('\n');
344 }
345 
346 static void
output_labels(void)347 output_labels(void)
348 {
349 	(void)printf("%-*.*s ", (int)maxname, (int)maxname, "USER");
350 
351 	if (show_term)
352 		(void)printf("S ");
353 
354 	(void)printf("%-*.*s ", (int)maxline, (int)maxline, "LINE");
355 	(void)printf("WHEN         ");
356 
357 	if (show_idle) {
358 		(void)printf("IDLE  ");
359 		(void)printf("\t   PID");
360 
361 		(void)printf("\tCOMMENT");
362 	}
363 
364 	(void)putchar('\n');
365 }
366 
367 static void
quick(const char * fname)368 quick(const char *fname)
369 {
370 	struct utmpentry *ehead, *ep;
371 	int num = 0;
372 
373 	(void)getutentries(fname, &ehead);
374 	for (ep = ehead; ep != NULL; ep = ep->next) {
375 		(void)printf("%-*s ", (int)maxname, ep->name);
376 		if ((++num % 8) == 0)
377 			(void)putchar('\n');
378 	}
379 	if (num % 8)
380 		(void)putchar('\n');
381 
382 	(void)printf("# users = %d\n", num);
383 }
384 
385 static void
usage(void)386 usage(void)
387 {
388 	(void)fprintf(stderr, "Usage: %s [-abdHlmqrsTtuv] [file]\n\t%s am i\n",
389 	    getprogname(), getprogname());
390 	exit(EXIT_FAILURE);
391 }
392