last.c revision 91538
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 91538 2002-03-01 19:46:20Z 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 int snapfound = 0; /* found snapshot entry? */ 95static time_t snaptime; /* if != 0, we will only 96 * report users logged in 97 * at this snapshot time 98 */ 99 100void addarg __P((int, char *)); 101time_t dateconv __P((char *)); 102void doentry __P((struct utmp *)); 103void hostconv __P((char *)); 104void onintr __P((int)); 105void printentry __P((struct utmp *, struct ttytab *)); 106char *ttyconv __P((char *)); 107int want __P((struct utmp *)); 108void usage __P((void)); 109void wtmp __P((void)); 110 111void 112usage(void) 113{ 114 (void)fprintf(stderr, 115"usage: last [-#] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h hostname]\n" 116"\t[-t tty] [-s|w] [user ...]\n"); 117 exit(1); 118} 119 120int 121main(argc, argv) 122 int argc; 123 char *argv[]; 124{ 125 int ch; 126 char *p; 127 128 (void) setlocale(LC_TIME, ""); 129 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 130 131 maxrec = -1; 132 snaptime = 0; 133 while ((ch = getopt(argc, argv, "0123456789d:f:h:st:w")) != -1) 134 switch (ch) { 135 case '0': case '1': case '2': case '3': case '4': 136 case '5': case '6': case '7': case '8': case '9': 137 /* 138 * kludge: last was originally designed to take 139 * a number after a dash. 140 */ 141 if (maxrec == -1) { 142 p = argv[optind - 1]; 143 if (p[0] == '-' && p[1] == ch && !p[2]) 144 maxrec = atol(++p); 145 else 146 maxrec = atol(argv[optind] + 1); 147 if (!maxrec) 148 exit(0); 149 } 150 break; 151 case 'd': 152 snaptime = dateconv(optarg); 153 break; 154 case 'f': 155 file = optarg; 156 break; 157 case 'h': 158 hostconv(optarg); 159 addarg(HOST_TYPE, optarg); 160 break; 161 case 's': 162 sflag++; /* Show delta as seconds */ 163 break; 164 case 't': 165 addarg(TTY_TYPE, ttyconv(optarg)); 166 break; 167 case 'w': 168 width = 8; 169 break; 170 case '?': 171 default: 172 usage(); 173 } 174 175 if (sflag && width == 8) usage(); 176 177 if (argc) { 178 setlinebuf(stdout); 179 for (argv += optind; *argv; ++argv) { 180#define COMPATIBILITY 181#ifdef COMPATIBILITY 182 /* code to allow "last p5" to work */ 183 addarg(TTY_TYPE, ttyconv(*argv)); 184#endif 185 addarg(USER_TYPE, *argv); 186 } 187 } 188 wtmp(); 189 exit(0); 190} 191 192/* 193 * wtmp -- 194 * read through the wtmp file 195 */ 196void 197wtmp() 198{ 199 struct utmp *bp; /* current structure */ 200 struct stat stb; /* stat of file for size */ 201 long bl; 202 int bytes, wfd; 203 char ct[80]; 204 struct tm *tm; 205 time_t t; 206 207 LIST_INIT(&ttylist); 208 209 if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1) 210 err(1, "%s", file); 211 bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf); 212 213 (void)time(&t); 214 buf[0].ut_time = _time_to_int(t); 215 (void)signal(SIGINT, onintr); 216 (void)signal(SIGQUIT, onintr); 217 218 while (--bl >= 0) { 219 if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 || 220 (bytes = read(wfd, buf, sizeof(buf))) == -1) 221 err(1, "%s", file); 222 for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) 223 doentry(bp); 224 } 225 t = _int_to_time(buf[0].ut_time); 226 tm = localtime(&t); 227 (void) strftime(ct, sizeof(ct), "\nwtmp begins %+\n", tm); 228 printf("%s", ct); 229} 230 231/* 232 * doentry -- 233 * process a single wtmp entry 234 */ 235void 236doentry(bp) 237 struct utmp *bp; 238{ 239 struct ttytab *tt, *ttx; /* ttylist 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 return; 269 } 270 /* 271 * if the line is '{' or '|', date got set; see 272 * utmp(5) for more info. 273 */ 274 if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|') && 275 !bp->ut_line[1]) { 276 if (want(bp) && !snaptime) 277 printentry(bp, NULL); 278 return; 279 } 280 /* find associated tty */ 281 LIST_FOREACH(tt, &ttylist, list) 282 if (!strncmp(tt->tty, bp->ut_line, UT_LINESIZE)) 283 break; 284 285 if (tt == NULL) { 286 /* add new one */ 287 tt = malloc(sizeof(struct ttytab)); 288 if (tt == NULL) 289 err(1, "malloc failure"); 290 tt->logout = currentout; 291 strncpy(tt->tty, bp->ut_line, UT_LINESIZE); 292 LIST_INSERT_HEAD(&ttylist, tt, list); 293 } 294 295 /* 296 * print record if not in snapshot mode and wanted 297 * or in snapshot mode and in snapshot range 298 */ 299 if (bp->ut_name[0] && (want(bp) || (bp->ut_time < snaptime && 300 (tt->logout > snaptime || tt->logout < 1)))) { 301 snapfound = 1; 302 /* 303 * when uucp and ftp log in over a network, the entry in 304 * the utmp file is the name plus their process id. See 305 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information. 306 */ 307 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1)) 308 bp->ut_line[3] = '\0'; 309 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1)) 310 bp->ut_line[4] = '\0'; 311 printentry(bp, tt); 312 } 313 tt->logout = bp->ut_time; 314} 315 316/* 317 * printentry -- 318 * output an entry 319 * 320 * If `tt' is non-NULL, use it and `crmsg' to print the logout time or 321 * logout type (crash/shutdown) as appropriate. 322 */ 323void 324printentry(bp, tt) 325 struct utmp *bp; 326 struct ttytab *tt; 327{ 328 char ct[80]; 329 struct tm *tm; 330 time_t delta; /* time difference */ 331 time_t t; 332 333 if (maxrec != -1 && !maxrec--) 334 exit(0); 335 t = _int_to_time(bp->ut_time); 336 tm = localtime(&t); 337 (void) strftime(ct, sizeof(ct), d_first ? "%a %e %b %R" : 338 "%a %b %e %R", tm); 339 printf("%-*.*s %-*.*s %-*.*s %s%c", 340 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, 341 UT_LINESIZE, UT_LINESIZE, bp->ut_line, 342 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, 343 ct, tt == NULL ? '\n' : ' '); 344 if (tt == NULL) 345 return; 346 if (!tt->logout) { 347 puts(" still logged in"); 348 return; 349 } 350 if (tt->logout < 0) { 351 tt->logout = -tt->logout; 352 printf("- %s", crmsg); 353 } else { 354 tm = localtime(&tt->logout); 355 (void) strftime(ct, sizeof(ct), "%R", tm); 356 printf("- %s", ct); 357 } 358 delta = tt->logout - bp->ut_time; 359 if (sflag) { 360 printf(" (%8ld)\n", (long)delta); 361 } else { 362 tm = gmtime(&delta); 363 (void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm); 364 if (delta < 86400) 365 printf(" (%s)\n", ct); 366 else 367 printf(" (%ld+%s)\n", (long)delta / 86400, ct); 368 } 369} 370 371/* 372 * want -- 373 * see if want this entry 374 */ 375int 376want(bp) 377 struct utmp *bp; 378{ 379 ARG *step; 380 381 if (snaptime) 382 return (NO); 383 384 if (!arglist) 385 return (YES); 386 387 for (step = arglist; step; step = step->next) 388 switch(step->type) { 389 case HOST_TYPE: 390 if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE)) 391 return (YES); 392 break; 393 case TTY_TYPE: 394 if (!strncmp(step->name, bp->ut_line, UT_LINESIZE)) 395 return (YES); 396 break; 397 case USER_TYPE: 398 if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE)) 399 return (YES); 400 break; 401 } 402 return (NO); 403} 404 405/* 406 * addarg -- 407 * add an entry to a linked list of arguments 408 */ 409void 410addarg(type, arg) 411 int type; 412 char *arg; 413{ 414 ARG *cur; 415 416 if (!(cur = (ARG *)malloc((u_int)sizeof(ARG)))) 417 err(1, "malloc failure"); 418 cur->next = arglist; 419 cur->type = type; 420 cur->name = arg; 421 arglist = cur; 422} 423 424/* 425 * hostconv -- 426 * convert the hostname to search pattern; if the supplied host name 427 * has a domain attached that is the same as the current domain, rip 428 * off the domain suffix since that's what login(1) does. 429 */ 430void 431hostconv(arg) 432 char *arg; 433{ 434 static int first = 1; 435 static char *hostdot, name[MAXHOSTNAMELEN]; 436 char *argdot; 437 438 if (!(argdot = strchr(arg, '.'))) 439 return; 440 if (first) { 441 first = 0; 442 if (gethostname(name, sizeof(name))) 443 err(1, "gethostname"); 444 hostdot = strchr(name, '.'); 445 } 446 if (hostdot && !strcasecmp(hostdot, argdot)) 447 *argdot = '\0'; 448} 449 450/* 451 * ttyconv -- 452 * convert tty to correct name. 453 */ 454char * 455ttyconv(arg) 456 char *arg; 457{ 458 char *mval; 459 460 /* 461 * kludge -- we assume that all tty's end with 462 * a two character suffix. 463 */ 464 if (strlen(arg) == 2) { 465 /* either 6 for "ttyxx" or 8 for "console" */ 466 if (!(mval = malloc((u_int)8))) 467 err(1, "malloc failure"); 468 if (!strcmp(arg, "co")) 469 (void)strcpy(mval, "console"); 470 else { 471 (void)strcpy(mval, "tty"); 472 (void)strcpy(mval + 3, arg); 473 } 474 return (mval); 475 } 476 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1)) 477 return (arg + 5); 478 return (arg); 479} 480 481/* 482 * dateconv -- 483 * Convert the snapshot time in command line given in the format 484 * [[CC]YY]MMDDhhmm[.SS]] to a time_t. 485 * Derived from atime_arg1() in usr.bin/touch/touch.c 486 */ 487time_t 488dateconv(arg) 489 char *arg; 490{ 491 time_t timet; 492 struct tm *t; 493 int yearset; 494 char *p; 495 496 /* Start with the current time. */ 497 if (time(&timet) < 0) 498 err(1, "time"); 499 if ((t = localtime(&timet)) == NULL) 500 err(1, "localtime"); 501 502 /* [[CC]YY]MMDDhhmm[.SS] */ 503 if ((p = strchr(arg, '.')) == NULL) 504 t->tm_sec = 0; /* Seconds defaults to 0. */ 505 else { 506 if (strlen(p + 1) != 2) 507 goto terr; 508 *p++ = '\0'; 509 t->tm_sec = ATOI2(p); 510 } 511 512 yearset = 0; 513 switch (strlen(arg)) { 514 case 12: /* CCYYMMDDhhmm */ 515 t->tm_year = ATOI2(arg); 516 t->tm_year *= 100; 517 yearset = 1; 518 /* FALLTHOUGH */ 519 case 10: /* YYMMDDhhmm */ 520 if (yearset) { 521 yearset = ATOI2(arg); 522 t->tm_year += yearset; 523 } else { 524 yearset = ATOI2(arg); 525 if (yearset < 69) 526 t->tm_year = yearset + 2000; 527 else 528 t->tm_year = yearset + 1900; 529 } 530 t->tm_year -= 1900; /* Convert to UNIX time. */ 531 /* FALLTHROUGH */ 532 case 8: /* MMDDhhmm */ 533 t->tm_mon = ATOI2(arg); 534 --t->tm_mon; /* Convert from 01-12 to 00-11 */ 535 t->tm_mday = ATOI2(arg); 536 t->tm_hour = ATOI2(arg); 537 t->tm_min = ATOI2(arg); 538 break; 539 case 4: /* hhmm */ 540 t->tm_hour = ATOI2(arg); 541 t->tm_min = ATOI2(arg); 542 break; 543 default: 544 goto terr; 545 } 546 t->tm_isdst = -1; /* Figure out DST. */ 547 timet = mktime(t); 548 if (timet == -1) 549terr: errx(1, 550 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 551 return timet; 552} 553 554 555/* 556 * onintr -- 557 * on interrupt, we inform the user how far we've gotten 558 */ 559void 560onintr(signo) 561 int signo; 562{ 563 char ct[80]; 564 struct tm *tm; 565 time_t t = _int_to_time(buf[0].ut_time); 566 567 tm = localtime(&t); 568 (void) strftime(ct, sizeof(ct), 569 d_first ? "%a %e %b %R" : "%a %b %e %R", 570 tm); 571 printf("\ninterrupted %s\n", ct); 572 if (signo == SIGINT) 573 exit(1); 574 (void)fflush(stdout); /* fix required for rsh */ 575} 576