1 /* $NetBSD: who.c,v 1.23 2008/07/24 15:35:41 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.23 2008/07/24 15:35:41 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 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 * 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 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 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 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 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 289 state = '?'; 290 idle = 0; 291 292 for (i = 0; ut_type_names[i].type >= 0; i++) { 293 types = ut_type_names[i].name; 294 if (ut_type_names[i].type == type) 295 break; 296 } 297 298 if (show_term || show_idle) { 299 if (now == 0) 300 time(&now); 301 302 if (stat(line, &sb) == 0) { 303 state = (sb.st_mode & 020) ? '+' : '-'; 304 idle = now - sb.st_atime; 305 } 306 307 } 308 309 (void)printf("%-*.*s ", maxname, maxname, name); 310 311 if (show_term) 312 (void)printf("%c ", state); 313 314 (void)printf("%-*.*s ", maxline, maxline, line); 315 (void)printf("%.12s ", ctime(&t) + 4); 316 317 if (show_idle) { 318 if (idle < 60) 319 (void)printf(" . "); 320 else if (idle < (24 * 60 * 60)) 321 (void)printf("%02ld:%02ld ", 322 (long)(idle / (60 * 60)), 323 (long)(idle % (60 * 60)) / 60); 324 else 325 (void)printf(" old "); 326 327 (void)printf("\t%6d", pid); 328 329 if (show_details) { 330 if (type == RUN_LVL) 331 (void)printf("\tnew=%c old=%c", term, xit); 332 else 333 (void)printf("\tterm=%d exit=%d", term, xit); 334 (void)printf(" sess=%d", sess); 335 (void)printf(" type=%s ", types); 336 } 337 } 338 339 if (*host) 340 (void)printf("\t(%.*s)", maxhost, host); 341 (void)putchar('\n'); 342 } 343 344 static void 345 output_labels(void) 346 { 347 (void)printf("%-*.*s ", maxname, maxname, "USER"); 348 349 if (show_term) 350 (void)printf("S "); 351 352 (void)printf("%-*.*s ", maxline, maxline, "LINE"); 353 (void)printf("WHEN "); 354 355 if (show_idle) { 356 (void)printf("IDLE "); 357 (void)printf("\t PID"); 358 359 (void)printf("\tCOMMENT"); 360 } 361 362 (void)putchar('\n'); 363 } 364 365 static void 366 quick(const char *fname) 367 { 368 struct utmpentry *ehead, *ep; 369 int num = 0; 370 371 (void)getutentries(fname, &ehead); 372 for (ep = ehead; ep != NULL; ep = ep->next) { 373 (void)printf("%-*s ", maxname, ep->name); 374 if ((++num % 8) == 0) 375 (void)putchar('\n'); 376 } 377 if (num % 8) 378 (void)putchar('\n'); 379 380 (void)printf("# users = %d\n", num); 381 } 382 383 static void 384 usage(void) 385 { 386 (void)fprintf(stderr, "Usage: %s [-abdHlmqrsTtuv] [file]\n\t%s am i\n", 387 getprogname(), getprogname()); 388 exit(EXIT_FAILURE); 389 } 390