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