1 /* $NetBSD: who.c,v 1.22 2008/07/21 14:19:28 lukem Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
36 #include <sys/sysctl.h>
37 #include <sys/types.h>
55 #include "utmpentry.h"
57 static void print_boottime(void);
58 static void output_labels(void);
59 static void who_am_i(const char *, int);
60 static void usage(void);
61 static void process(const char *, int);
62 static void eprint(const struct utmpentry
*);
63 static 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
);
65 static void quick(const char *);
67 static int show_term
; /* show term state */
68 static int show_idle
; /* show idle time */
69 static int show_details
; /* show exit status etc. */
70 static int bflag
; /* Show date and time of last reboot */
72 struct ut_type_names
{
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 */
94 main(int argc
, char *argv
[])
96 int c
, only_current_term
, show_labels
, quick_mode
, default_mode
;
99 setlocale(LC_ALL
, "");
101 only_current_term
= show_term
= show_idle
= show_labels
= 0;
102 quick_mode
= default_mode
= 0;
104 while ((c
= getopt(argc
, argv
, "abdHlmpqrsTtuv")) != -1) {
108 show_idle
= show_details
= 1;
112 et
|= (1 << BOOT_TIME
);
117 et
|= (1 << DEAD_PROCESS
);
123 et
|= (1 << LOGIN_PROCESS
);
126 only_current_term
= 1;
129 et
|= (1 << INIT_PROCESS
);
135 et
|= (1 << RUN_LVL
);
144 et
|= (1 << NEW_TIME
);
164 err(EXIT_FAILURE
, "cannot change directory to /dev");
169 only_current_term
= show_term
= show_idle
= 0;
171 if (!quick_mode
&& bflag
)
178 } else if (only_current_term
) {
179 who_am_i(NULL
, show_labels
);
181 process(NULL
, show_labels
);
184 case 1: /* who utmp_file */
187 } else if (only_current_term
) {
188 who_am_i(*argv
, show_labels
);
190 process(*argv
, show_labels
);
193 case 2: /* who am i */
194 who_am_i(NULL
, show_labels
);
207 struct timeval boottime
;
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
));
219 strrstr(const char *str
, const char *pat
)
224 return __DECONST(char *, str
);
228 for (estr
= str
+ strlen(str
); str
< estr
; estr
--)
229 if (strncmp(estr
, pat
, len
) == 0)
230 return __DECONST(char *, estr
);
235 who_am_i(const char *fname
, int show_labels
)
241 struct utmpentry
*ehead
, *ep
;
243 /* search through the utmp and find an entry for this tty */
244 if ((p
= ttyname(STDIN_FILENO
)) != NULL
) {
246 /* strip directory prefixes for ttys */
247 if ((t
= strrstr(p
, "/pts/")) != NULL
||
248 (t
= strrchr(p
, '/')) != NULL
)
251 (void)getutentries(fname
, &ehead
);
252 for (ep
= ehead
; ep
; ep
= ep
->next
)
253 if (strcmp(ep
->line
, p
) == 0) {
263 pw
= getpwuid(getuid());
266 print(pw
? pw
->pw_name
: "?", p
, now
, "", getpid(), 0, 0, 0, 0);
270 process(const char *fname
, int show_labels
)
272 struct utmpentry
*ehead
, *ep
;
273 (void)getutentries(fname
, &ehead
);
276 for (ep
= ehead
; ep
!= NULL
; ep
= ep
->next
)
281 eprint(const struct utmpentry
*ep
)
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
);
288 print(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
)
293 static time_t now
= 0;
295 const char *types
= NULL
;
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
)
307 if (show_term
|| show_idle
) {
311 if (stat(line
, &sb
) == 0) {
312 state
= (sb
.st_mode
& 020) ? '+' : '-';
313 idle
= now
- sb
.st_atime
;
318 (void)printf("%-*.*s ", maxname
, maxname
, name
);
321 (void)printf("%c ", state
);
323 (void)printf("%-*.*s ", maxline
, maxline
, line
);
324 (void)printf("%.12s ", ctime(&t
) + 4);
329 else if (idle
< (24 * 60 * 60))
330 (void)printf("%02ld:%02ld ",
331 (long)(idle
/ (60 * 60)),
332 (long)(idle
% (60 * 60)) / 60);
334 (void)printf(" old ");
336 (void)printf("\t%6d", pid
);
340 (void)printf("\tnew=%c old=%c", term
, xit
);
342 (void)printf("\tterm=%d exit=%d", term
, xit
);
343 (void)printf(" sess=%d", sess
);
344 (void)printf(" type=%s ", types
);
349 (void)printf("\t(%.*s)", maxhost
, host
);
356 (void)printf("%-*.*s ", maxname
, maxname
, "USER");
361 (void)printf("%-*.*s ", maxline
, maxline
, "LINE");
362 (void)printf("WHEN ");
365 (void)printf("IDLE ");
366 (void)printf("\t PID");
368 (void)printf("\tCOMMENT");
375 quick(const char *fname
)
377 struct utmpentry
*ehead
, *ep
;
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)
389 (void)printf("# users = %d\n", num
);
395 (void)fprintf(stderr
, "Usage: %s [-abdHlmqrsTtuv] [file]\n\t%s am i\n",
396 getprogname(), getprogname());