who.c revision 1.22 1 /* $NetBSD: who.c,v 1.22 2008/07/21 14:19:28 lukem 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.22 2008/07/21 14:19:28 lukem 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
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 void
210 who_am_i(const char *fname, int show_labels)
211 {
212 struct passwd *pw;
213 const char *p;
214 char *t;
215 time_t now;
216 struct utmpentry *ehead, *ep;
217
218 /* search through the utmp and find an entry for this tty */
219 if ((p = ttyname(STDIN_FILENO)) != NULL) {
220
221 /* strip any directory component */
222 if ((t = strrchr(p, '/')) != NULL)
223 p = t + 1;
224
225 (void)getutentries(fname, &ehead);
226 for (ep = ehead; ep; ep = ep->next)
227 if (strcmp(ep->line, p) == 0) {
228 if (show_labels)
229 output_labels();
230 eprint(ep);
231 return;
232 }
233 } else
234 p = "tty??";
235
236 (void)time(&now);
237 pw = getpwuid(getuid());
238 if (show_labels)
239 output_labels();
240 print(pw ? pw->pw_name : "?", p, now, "", getpid(), 0, 0, 0, 0);
241 }
242
243 static void
244 process(const char *fname, int show_labels)
245 {
246 struct utmpentry *ehead, *ep;
247 (void)getutentries(fname, &ehead);
248 if (show_labels)
249 output_labels();
250 for (ep = ehead; ep != NULL; ep = ep->next)
251 eprint(ep);
252 }
253
254 static void
255 eprint(const struct utmpentry *ep)
256 {
257 print(ep->name, ep->line, (time_t)ep->tv.tv_sec, ep->host, ep->pid,
258 ep->term, ep->exit, ep->sess, ep->type);
259 }
260
261 static void
262 print(const char *name, const char *line, time_t t, const char *host,
263 pid_t pid, uint16_t term, uint16_t xit, uint16_t sess, uint16_t type)
264 {
265 struct stat sb;
266 char state;
267 static time_t now = 0;
268 time_t idle;
269 const char *types = NULL;
270 size_t i;
271
272 state = '?';
273 idle = 0;
274
275 for (i = 0; ut_type_names[i].type >= 0; i++) {
276 types = ut_type_names[i].name;
277 if (ut_type_names[i].type == type)
278 break;
279 }
280
281 if (show_term || show_idle) {
282 if (now == 0)
283 time(&now);
284
285 if (stat(line, &sb) == 0) {
286 state = (sb.st_mode & 020) ? '+' : '-';
287 idle = now - sb.st_atime;
288 }
289
290 }
291
292 (void)printf("%-*.*s ", maxname, maxname, name);
293
294 if (show_term)
295 (void)printf("%c ", state);
296
297 (void)printf("%-*.*s ", maxline, maxline, line);
298 (void)printf("%.12s ", ctime(&t) + 4);
299
300 if (show_idle) {
301 if (idle < 60)
302 (void)printf(" . ");
303 else if (idle < (24 * 60 * 60))
304 (void)printf("%02ld:%02ld ",
305 (long)(idle / (60 * 60)),
306 (long)(idle % (60 * 60)) / 60);
307 else
308 (void)printf(" old ");
309
310 (void)printf("\t%6d", pid);
311
312 if (show_details) {
313 if (type == RUN_LVL)
314 (void)printf("\tnew=%c old=%c", term, xit);
315 else
316 (void)printf("\tterm=%d exit=%d", term, xit);
317 (void)printf(" sess=%d", sess);
318 (void)printf(" type=%s ", types);
319 }
320 }
321
322 if (*host)
323 (void)printf("\t(%.*s)", maxhost, host);
324 (void)putchar('\n');
325 }
326
327 static void
328 output_labels(void)
329 {
330 (void)printf("%-*.*s ", maxname, maxname, "USER");
331
332 if (show_term)
333 (void)printf("S ");
334
335 (void)printf("%-*.*s ", maxline, maxline, "LINE");
336 (void)printf("WHEN ");
337
338 if (show_idle) {
339 (void)printf("IDLE ");
340 (void)printf("\t PID");
341
342 (void)printf("\tCOMMENT");
343 }
344
345 (void)putchar('\n');
346 }
347
348 static void
349 quick(const char *fname)
350 {
351 struct utmpentry *ehead, *ep;
352 int num = 0;
353
354 (void)getutentries(fname, &ehead);
355 for (ep = ehead; ep != NULL; ep = ep->next) {
356 (void)printf("%-*s ", maxname, ep->name);
357 if ((++num % 8) == 0)
358 (void)putchar('\n');
359 }
360 if (num % 8)
361 (void)putchar('\n');
362
363 (void)printf("# users = %d\n", num);
364 }
365
366 static void
367 usage(void)
368 {
369 (void)fprintf(stderr, "Usage: %s [-abdHlmqrsTtuv] [file]\n\t%s am i\n",
370 getprogname(), getprogname());
371 exit(EXIT_FAILURE);
372 }
373