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