last.c revision 91536
1/* 2 * Copyright (c) 1987, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * $FreeBSD: head/usr.bin/last/last.c 91536 2002-03-01 17:37:06Z iedowse $ 34 */ 35 36#ifndef lint 37static const char copyright[] = 38"@(#) Copyright (c) 1987, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40#endif /* not lint */ 41 42#ifndef lint 43static const char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94"; 44#endif /* not lint */ 45 46#include <sys/param.h> 47#include <sys/stat.h> 48 49#include <err.h> 50#include <fcntl.h> 51#include <langinfo.h> 52#include <locale.h> 53#include <paths.h> 54#include <signal.h> 55#include <stdio.h> 56#include <stdlib.h> 57#include <string.h> 58#include <time.h> 59#include <unistd.h> 60#include <utmp.h> 61#include <sys/queue.h> 62 63#define NO 0 /* false/no */ 64#define YES 1 /* true/yes */ 65#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; 66 67static struct utmp buf[1024]; /* utmp read buffer */ 68 69typedef struct arg { 70 char *name; /* argument */ 71#define HOST_TYPE -2 72#define TTY_TYPE -3 73#define USER_TYPE -4 74 int type; /* type of arg */ 75 struct arg *next; /* linked list pointer */ 76} ARG; 77ARG *arglist; /* head of linked list */ 78 79LIST_HEAD(ttylisthead, ttytab) ttylist; 80 81struct ttytab { 82 time_t logout; /* log out time */ 83 char tty[UT_LINESIZE + 1]; /* terminal name */ 84 LIST_ENTRY(ttytab) list; 85}; 86 87static const char *crmsg; /* cause of last reboot */ 88static long currentout, /* current logout value */ 89 maxrec; /* records to display */ 90static const char *file = _PATH_WTMP; /* wtmp file */ 91static int sflag = 0; /* show delta in seconds */ 92static int width = 5; /* show seconds in delta */ 93static int d_first; 94static time_t snaptime; /* if != 0, we will only 95 * report users logged in 96 * at this snapshot time 97 */ 98 99void addarg __P((int, char *)); 100time_t dateconv __P((char *)); 101void doentry __P((struct utmp *)); 102void hostconv __P((char *)); 103void onintr __P((int)); 104void printentry __P((struct utmp *, struct ttytab *)); 105char *ttyconv __P((char *)); 106int want __P((struct utmp *)); 107void usage __P((void)); 108void wtmp __P((void)); 109 110void 111usage(void) 112{ 113 (void)fprintf(stderr, 114"usage: last [-#] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h hostname]\n" 115"\t[-t tty] [-s|w] [user ...]\n"); 116 exit(1); 117} 118 119int 120main(argc, argv) 121 int argc; 122 char *argv[]; 123{ 124 int ch; 125 char *p; 126 127 (void) setlocale(LC_TIME, ""); 128 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 129 130 maxrec = -1; 131 snaptime = 0; 132 while ((ch = getopt(argc, argv, "0123456789d:f:h:st:w")) != -1) 133 switch (ch) { 134 case '0': case '1': case '2': case '3': case '4': 135 case '5': case '6': case '7': case '8': case '9': 136 /* 137 * kludge: last was originally designed to take 138 * a number after a dash. 139 */ 140 if (maxrec == -1) { 141 p = argv[optind - 1]; 142 if (p[0] == '-' && p[1] == ch && !p[2]) 143 maxrec = atol(++p); 144 else 145 maxrec = atol(argv[optind] + 1); 146 if (!maxrec) 147 exit(0); 148 } 149 break; 150 case 'd': 151 snaptime = dateconv(optarg); 152 break; 153 case 'f': 154 file = optarg; 155 break; 156 case 'h': 157 hostconv(optarg); 158 addarg(HOST_TYPE, optarg); 159 break; 160 case 's': 161 sflag++; /* Show delta as seconds */ 162 break; 163 case 't': 164 addarg(TTY_TYPE, ttyconv(optarg)); 165 break; 166 case 'w': 167 width = 8; 168 break; 169 case '?': 170 default: 171 usage(); 172 } 173 174 if (sflag && width == 8) usage(); 175 176 if (argc) { 177 setlinebuf(stdout); 178 for (argv += optind; *argv; ++argv) { 179#define COMPATIBILITY 180#ifdef COMPATIBILITY 181 /* code to allow "last p5" to work */ 182 addarg(TTY_TYPE, ttyconv(*argv)); 183#endif 184 addarg(USER_TYPE, *argv); 185 } 186 } 187 wtmp(); 188 exit(0); 189} 190 191/* 192 * wtmp -- 193 * read through the wtmp file 194 */ 195void 196wtmp() 197{ 198 struct utmp *bp; /* current structure */ 199 struct stat stb; /* stat of file for size */ 200 long bl; 201 int bytes, wfd; 202 char ct[80]; 203 struct tm *tm; 204 time_t t; 205 206 LIST_INIT(&ttylist); 207 208 if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1) 209 err(1, "%s", file); 210 bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf); 211 212 (void)time(&t); 213 buf[0].ut_time = _time_to_int(t); 214 (void)signal(SIGINT, onintr); 215 (void)signal(SIGQUIT, onintr); 216 217 while (--bl >= 0) { 218 if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 || 219 (bytes = read(wfd, buf, sizeof(buf))) == -1) 220 err(1, "%s", file); 221 for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) 222 doentry(bp); 223 } 224 t = _int_to_time(buf[0].ut_time); 225 tm = localtime(&t); 226 (void) strftime(ct, sizeof(ct), "\nwtmp begins %+\n", tm); 227 printf("%s", ct); 228} 229 230/* 231 * doentry -- 232 * process a single wtmp entry 233 */ 234void 235doentry(bp) 236 struct utmp *bp; 237{ 238 struct ttytab *tt, *ttx; /* ttylist entry */ 239 int snapfound = 0; /* found snapshot entry? */ 240 241 /* 242 * if the terminal line is '~', the machine stopped. 243 * see utmp(5) for more info. 244 */ 245 if (bp->ut_line[0] == '~' && !bp->ut_line[1]) { 246 /* everybody just logged out */ 247 for (tt = LIST_FIRST(&ttylist); tt;) { 248 LIST_REMOVE(tt, list); 249 ttx = tt; 250 tt = LIST_NEXT(tt, list); 251 free(ttx); 252 } 253 currentout = -bp->ut_time; 254 crmsg = strncmp(bp->ut_name, "shutdown", UT_NAMESIZE) ? 255 "crash" : "shutdown"; 256 /* 257 * if we're in snapshot mode, we want to exit if this 258 * shutdown/reboot appears while we we are tracking the 259 * active range 260 */ 261 if (snaptime && snapfound) 262 exit(0); 263 /* 264 * don't print shutdown/reboot entries unless flagged for 265 */ 266 if (!snaptime && want(bp)) { 267 printentry(bp, NULL); 268 if (maxrec != -1 && !--maxrec) 269 exit(0); 270 } 271 return; 272 } 273 /* 274 * if the line is '{' or '|', date got set; see 275 * utmp(5) for more info. 276 */ 277 if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|') && 278 !bp->ut_line[1]) { 279 if (want(bp) && !snaptime) { 280 printentry(bp, NULL); 281 if (maxrec && !--maxrec) 282 exit(0); 283 } 284 return; 285 } 286 /* find associated tty */ 287 LIST_FOREACH(tt, &ttylist, list) 288 if (!strncmp(tt->tty, bp->ut_line, UT_LINESIZE)) 289 break; 290 291 if (tt == NULL) { 292 /* add new one */ 293 tt = malloc(sizeof(struct ttytab)); 294 if (tt == NULL) 295 err(1, "malloc failure"); 296 tt->logout = currentout; 297 strncpy(tt->tty, bp->ut_line, UT_LINESIZE); 298 LIST_INSERT_HEAD(&ttylist, tt, list); 299 } 300 301 /* 302 * print record if not in snapshot mode and wanted 303 * or in snapshot mode and in snapshot range 304 */ 305 if (bp->ut_name[0] && (want(bp) || (bp->ut_time < snaptime && 306 (tt->logout > snaptime || tt->logout < 1)))) { 307 snapfound = 1; 308 /* 309 * when uucp and ftp log in over a network, the entry in 310 * the utmp file is the name plus their process id. See 311 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information. 312 */ 313 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1)) 314 bp->ut_line[3] = '\0'; 315 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1)) 316 bp->ut_line[4] = '\0'; 317 printentry(bp, tt); 318 if (maxrec != -1 && !--maxrec) 319 return; 320 } 321 tt->logout = bp->ut_time; 322} 323 324/* 325 * printentry -- 326 * output an entry 327 * 328 * If `tt' is non-NULL, use it and `crmsg' to print the logout time or 329 * logout type (crash/shutdown) as appropriate. 330 */ 331void 332printentry(bp, tt) 333 struct utmp *bp; 334 struct ttytab *tt; 335{ 336 char ct[80]; 337 struct tm *tm; 338 time_t delta; /* time difference */ 339 time_t t; 340 341 t = _int_to_time(bp->ut_time); 342 tm = localtime(&t); 343 (void) strftime(ct, sizeof(ct), d_first ? "%a %e %b %R" : 344 "%a %b %e %R", tm); 345 printf("%-*.*s %-*.*s %-*.*s %s%c", 346 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, 347 UT_LINESIZE, UT_LINESIZE, bp->ut_line, 348 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, 349 ct, tt == NULL ? '\n' : ' '); 350 if (tt == NULL) 351 return; 352 if (!tt->logout) { 353 puts(" still logged in"); 354 return; 355 } 356 if (tt->logout < 0) { 357 tt->logout = -tt->logout; 358 printf("- %s", crmsg); 359 } else { 360 tm = localtime(&tt->logout); 361 (void) strftime(ct, sizeof(ct), "%R", tm); 362 printf("- %s", ct); 363 } 364 delta = tt->logout - bp->ut_time; 365 if (sflag) { 366 printf(" (%8ld)\n", (long)delta); 367 } else { 368 tm = gmtime(&delta); 369 (void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm); 370 if (delta < 86400) 371 printf(" (%s)\n", ct); 372 else 373 printf(" (%ld+%s)\n", (long)delta / 86400, ct); 374 } 375} 376 377/* 378 * want -- 379 * see if want this entry 380 */ 381int 382want(bp) 383 struct utmp *bp; 384{ 385 ARG *step; 386 387 if (snaptime) 388 return (NO); 389 390 if (!arglist) 391 return (YES); 392 393 for (step = arglist; step; step = step->next) 394 switch(step->type) { 395 case HOST_TYPE: 396 if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE)) 397 return (YES); 398 break; 399 case TTY_TYPE: 400 if (!strncmp(step->name, bp->ut_line, UT_LINESIZE)) 401 return (YES); 402 break; 403 case USER_TYPE: 404 if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE)) 405 return (YES); 406 break; 407 } 408 return (NO); 409} 410 411/* 412 * addarg -- 413 * add an entry to a linked list of arguments 414 */ 415void 416addarg(type, arg) 417 int type; 418 char *arg; 419{ 420 ARG *cur; 421 422 if (!(cur = (ARG *)malloc((u_int)sizeof(ARG)))) 423 err(1, "malloc failure"); 424 cur->next = arglist; 425 cur->type = type; 426 cur->name = arg; 427 arglist = cur; 428} 429 430/* 431 * hostconv -- 432 * convert the hostname to search pattern; if the supplied host name 433 * has a domain attached that is the same as the current domain, rip 434 * off the domain suffix since that's what login(1) does. 435 */ 436void 437hostconv(arg) 438 char *arg; 439{ 440 static int first = 1; 441 static char *hostdot, name[MAXHOSTNAMELEN]; 442 char *argdot; 443 444 if (!(argdot = strchr(arg, '.'))) 445 return; 446 if (first) { 447 first = 0; 448 if (gethostname(name, sizeof(name))) 449 err(1, "gethostname"); 450 hostdot = strchr(name, '.'); 451 } 452 if (hostdot && !strcasecmp(hostdot, argdot)) 453 *argdot = '\0'; 454} 455 456/* 457 * ttyconv -- 458 * convert tty to correct name. 459 */ 460char * 461ttyconv(arg) 462 char *arg; 463{ 464 char *mval; 465 466 /* 467 * kludge -- we assume that all tty's end with 468 * a two character suffix. 469 */ 470 if (strlen(arg) == 2) { 471 /* either 6 for "ttyxx" or 8 for "console" */ 472 if (!(mval = malloc((u_int)8))) 473 err(1, "malloc failure"); 474 if (!strcmp(arg, "co")) 475 (void)strcpy(mval, "console"); 476 else { 477 (void)strcpy(mval, "tty"); 478 (void)strcpy(mval + 3, arg); 479 } 480 return (mval); 481 } 482 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1)) 483 return (arg + 5); 484 return (arg); 485} 486 487/* 488 * dateconv -- 489 * Convert the snapshot time in command line given in the format 490 * [[CC]YY]MMDDhhmm[.SS]] to a time_t. 491 * Derived from atime_arg1() in usr.bin/touch/touch.c 492 */ 493time_t 494dateconv(arg) 495 char *arg; 496{ 497 time_t timet; 498 struct tm *t; 499 int yearset; 500 char *p; 501 502 /* Start with the current time. */ 503 if (time(&timet) < 0) 504 err(1, "time"); 505 if ((t = localtime(&timet)) == NULL) 506 err(1, "localtime"); 507 508 /* [[CC]YY]MMDDhhmm[.SS] */ 509 if ((p = strchr(arg, '.')) == NULL) 510 t->tm_sec = 0; /* Seconds defaults to 0. */ 511 else { 512 if (strlen(p + 1) != 2) 513 goto terr; 514 *p++ = '\0'; 515 t->tm_sec = ATOI2(p); 516 } 517 518 yearset = 0; 519 switch (strlen(arg)) { 520 case 12: /* CCYYMMDDhhmm */ 521 t->tm_year = ATOI2(arg); 522 t->tm_year *= 100; 523 yearset = 1; 524 /* FALLTHOUGH */ 525 case 10: /* YYMMDDhhmm */ 526 if (yearset) { 527 yearset = ATOI2(arg); 528 t->tm_year += yearset; 529 } else { 530 yearset = ATOI2(arg); 531 if (yearset < 69) 532 t->tm_year = yearset + 2000; 533 else 534 t->tm_year = yearset + 1900; 535 } 536 t->tm_year -= 1900; /* Convert to UNIX time. */ 537 /* FALLTHROUGH */ 538 case 8: /* MMDDhhmm */ 539 t->tm_mon = ATOI2(arg); 540 --t->tm_mon; /* Convert from 01-12 to 00-11 */ 541 t->tm_mday = ATOI2(arg); 542 t->tm_hour = ATOI2(arg); 543 t->tm_min = ATOI2(arg); 544 break; 545 case 4: /* hhmm */ 546 t->tm_hour = ATOI2(arg); 547 t->tm_min = ATOI2(arg); 548 break; 549 default: 550 goto terr; 551 } 552 t->tm_isdst = -1; /* Figure out DST. */ 553 timet = mktime(t); 554 if (timet == -1) 555terr: errx(1, 556 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 557 return timet; 558} 559 560 561/* 562 * onintr -- 563 * on interrupt, we inform the user how far we've gotten 564 */ 565void 566onintr(signo) 567 int signo; 568{ 569 char ct[80]; 570 struct tm *tm; 571 time_t t = _int_to_time(buf[0].ut_time); 572 573 tm = localtime(&t); 574 (void) strftime(ct, sizeof(ct), 575 d_first ? "%a %e %b %R" : "%a %b %e %R", 576 tm); 577 printf("\ninterrupted %s\n", ct); 578 if (signo == SIGINT) 579 exit(1); 580 (void)fflush(stdout); /* fix required for rsh */ 581} 582