utmpx - Bring in utmpx,wtmpx and lastlogx support
[dragonfly.git] / usr.bin / who / who.c
... / ...
CommitLineData
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#include <sys/sysctl.h>
37#include <sys/types.h>
38#include <sys/stat.h>
39
40#include <err.h>
41#include <locale.h>
42#include <pwd.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <time.h>
47#include <unistd.h>
48#ifdef SUPPORT_UTMP
49#include <utmp.h>
50#endif
51#ifdef SUPPORT_UTMPX
52#include <utmpx.h>
53#endif
54
55#include "utmpentry.h"
56
57static void print_boottime(void);
58static void output_labels(void);
59static void who_am_i(const char *, int);
60static void usage(void);
61static void process(const char *, int);
62static void eprint(const struct utmpentry *);
63static void print(const char *, const char *, time_t, const char *, pid_t pid,
64 uint16_t term, uint16_t xit, uint16_t sess, uint16_t type);
65static void quick(const char *);
66
67static int show_term; /* show term state */
68static int show_idle; /* show idle time */
69static int show_details; /* show exit status etc. */
70static int bflag; /* Show date and time of last reboot */
71
72struct ut_type_names {
73 int type;
74 const char *name;
75} ut_type_names[] = {
76#ifdef SUPPORT_UTMPX
77 { EMPTY, "empty" },
78 { RUN_LVL, "run level" },
79 { BOOT_TIME, "boot time" },
80 { OLD_TIME, "old time" },
81 { NEW_TIME, "new time" },
82 { INIT_PROCESS, "init process" },
83 { LOGIN_PROCESS, "login process" },
84 { USER_PROCESS, "user process" },
85 { DEAD_PROCESS, "dead process" },
86 { ACCOUNTING, "accounting" },
87 { SIGNATURE, "signature" },
88 { DOWN_TIME, "down time" },
89#endif /* SUPPORT_UTMPX */
90 { -1, "unknown" }
91};
92
93int
94main(int argc, char *argv[])
95{
96 int c, only_current_term, show_labels, quick_mode, default_mode;
97 int et = 0;
98
99 setlocale(LC_ALL, "");
100
101 only_current_term = show_term = show_idle = show_labels = 0;
102 quick_mode = default_mode = 0;
103
104 while ((c = getopt(argc, argv, "abdHlmpqrsTtuv")) != -1) {
105 switch (c) {
106 case 'a':
107 et = -1;
108 show_idle = show_details = 1;
109 break;
110 case 'b':
111#if 0
112 et |= (1 << BOOT_TIME);
113#endif
114 bflag = 1;
115 break;
116 case 'd':
117 et |= (1 << DEAD_PROCESS);
118 break;
119 case 'H':
120 show_labels = 1;
121 break;
122 case 'l':
123 et |= (1 << LOGIN_PROCESS);
124 break;
125 case 'm':
126 only_current_term = 1;
127 break;
128 case 'p':
129 et |= (1 << INIT_PROCESS);
130 break;
131 case 'q':
132 quick_mode = 1;
133 break;
134 case 'r':
135 et |= (1 << RUN_LVL);
136 break;
137 case 's':
138 default_mode = 1;
139 break;
140 case 'T':
141 show_term = 1;
142 break;
143 case 't':
144 et |= (1 << NEW_TIME);
145 break;
146 case 'u':
147 show_idle = 1;
148 break;
149 case 'v':
150 show_details = 1;
151 break;
152 default:
153 usage();
154 /* NOTREACHED */
155 }
156 }
157 argc -= optind;
158 argv += optind;
159
160 if (et != 0)
161 etype = et;
162
163 if (chdir("/dev")) {
164 err(EXIT_FAILURE, "cannot change directory to /dev");
165 /* NOTREACHED */
166 }
167
168 if (default_mode)
169 only_current_term = show_term = show_idle = 0;
170
171 if (!quick_mode && bflag)
172 print_boottime();
173
174 switch (argc) {
175 case 0: /* who */
176 if (quick_mode) {
177 quick(NULL);
178 } else if (only_current_term) {
179 who_am_i(NULL, show_labels);
180 } else {
181 process(NULL, show_labels);
182 }
183 break;
184 case 1: /* who utmp_file */
185 if (quick_mode) {
186 quick(*argv);
187 } else if (only_current_term) {
188 who_am_i(*argv, show_labels);
189 } else {
190 process(*argv, show_labels);
191 }
192 break;
193 case 2: /* who am i */
194 who_am_i(NULL, show_labels);
195 break;
196 default:
197 usage();
198 /* NOTREACHED */
199 }
200
201 return 0;
202}
203
204static void
205print_boottime(void)
206{
207 struct timeval boottime;
208 size_t size;
209
210 size = sizeof(boottime);
211 if (sysctlbyname("kern.boottime", &boottime, &size, NULL, 0) != -1 &&
212 boottime.tv_sec != 0) {
213 printf("%s", ctime(&boottime.tv_sec));
214 }
215}
216
217
218static char *
219strrstr(const char *str, const char *pat)
220{
221 const char *estr;
222 size_t len;
223 if (*pat == '\0')
224 return __DECONST(char *, str);
225
226 len = strlen(pat);
227
228 for (estr = str + strlen(str); str < estr; estr--)
229 if (strncmp(estr, pat, len) == 0)
230 return __DECONST(char *, estr);
231 return NULL;
232}
233
234static void
235who_am_i(const char *fname, int show_labels)
236{
237 struct passwd *pw;
238 const char *p;
239 char *t;
240 time_t now;
241 struct utmpentry *ehead, *ep;
242
243 /* search through the utmp and find an entry for this tty */
244 if ((p = ttyname(STDIN_FILENO)) != NULL) {
245
246 /* strip directory prefixes for ttys */
247 if ((t = strrstr(p, "/pts/")) != NULL ||
248 (t = strrchr(p, '/')) != NULL)
249 p = t + 1;
250
251 (void)getutentries(fname, &ehead);
252 for (ep = ehead; ep; ep = ep->next)
253 if (strcmp(ep->line, p) == 0) {
254 if (show_labels)
255 output_labels();
256 eprint(ep);
257 return;
258 }
259 } else
260 p = "tty??";
261
262 (void)time(&now);
263 pw = getpwuid(getuid());
264 if (show_labels)
265 output_labels();
266 print(pw ? pw->pw_name : "?", p, now, "", getpid(), 0, 0, 0, 0);
267}
268
269static void
270process(const char *fname, int show_labels)
271{
272 struct utmpentry *ehead, *ep;
273 (void)getutentries(fname, &ehead);
274 if (show_labels)
275 output_labels();
276 for (ep = ehead; ep != NULL; ep = ep->next)
277 eprint(ep);
278}
279
280static void
281eprint(const struct utmpentry *ep)
282{
283 print(ep->name, ep->line, (time_t)ep->tv.tv_sec, ep->host, ep->pid,
284 ep->term, ep->exit, ep->sess, ep->type);
285}
286
287static void
288print(const char *name, const char *line, time_t t, const char *host,
289 pid_t pid, uint16_t term, uint16_t xit, uint16_t sess, uint16_t type)
290{
291 struct stat sb;
292 char state;
293 static time_t now = 0;
294 time_t idle;
295 const char *types = NULL;
296 size_t i;
297
298 state = '?';
299 idle = 0;
300
301 for (i = 0; ut_type_names[i].type >= 0; i++) {
302 types = ut_type_names[i].name;
303 if (ut_type_names[i].type == type)
304 break;
305 }
306
307 if (show_term || show_idle) {
308 if (now == 0)
309 time(&now);
310
311 if (stat(line, &sb) == 0) {
312 state = (sb.st_mode & 020) ? '+' : '-';
313 idle = now - sb.st_atime;
314 }
315
316 }
317
318 (void)printf("%-*.*s ", maxname, maxname, name);
319
320 if (show_term)
321 (void)printf("%c ", state);
322
323 (void)printf("%-*.*s ", maxline, maxline, line);
324 (void)printf("%.12s ", ctime(&t) + 4);
325
326 if (show_idle) {
327 if (idle < 60)
328 (void)printf(" . ");
329 else if (idle < (24 * 60 * 60))
330 (void)printf("%02ld:%02ld ",
331 (long)(idle / (60 * 60)),
332 (long)(idle % (60 * 60)) / 60);
333 else
334 (void)printf(" old ");
335
336 (void)printf("\t%6d", pid);
337
338 if (show_details) {
339 if (type == RUN_LVL)
340 (void)printf("\tnew=%c old=%c", term, xit);
341 else
342 (void)printf("\tterm=%d exit=%d", term, xit);
343 (void)printf(" sess=%d", sess);
344 (void)printf(" type=%s ", types);
345 }
346 }
347
348 if (*host)
349 (void)printf("\t(%.*s)", maxhost, host);
350 (void)putchar('\n');
351}
352
353static void
354output_labels(void)
355{
356 (void)printf("%-*.*s ", maxname, maxname, "USER");
357
358 if (show_term)
359 (void)printf("S ");
360
361 (void)printf("%-*.*s ", maxline, maxline, "LINE");
362 (void)printf("WHEN ");
363
364 if (show_idle) {
365 (void)printf("IDLE ");
366 (void)printf("\t PID");
367
368 (void)printf("\tCOMMENT");
369 }
370
371 (void)putchar('\n');
372}
373
374static void
375quick(const char *fname)
376{
377 struct utmpentry *ehead, *ep;
378 int num = 0;
379
380 (void)getutentries(fname, &ehead);
381 for (ep = ehead; ep != NULL; ep = ep->next) {
382 (void)printf("%-*s ", maxname, ep->name);
383 if ((++num % 8) == 0)
384 (void)putchar('\n');
385 }
386 if (num % 8)
387 (void)putchar('\n');
388
389 (void)printf("# users = %d\n", num);
390}
391
392static void
393usage(void)
394{
395 (void)fprintf(stderr, "Usage: %s [-abdHlmqrsTtuv] [file]\n\t%s am i\n",
396 getprogname(), getprogname());
397 exit(EXIT_FAILURE);
398}