last.c revision 19223
11590Srgrimes/* 21590Srgrimes * Copyright (c) 1987, 1993, 1994 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes */ 331590Srgrimes 341590Srgrimes#ifndef lint 351590Srgrimesstatic char copyright[] = 361590Srgrimes"@(#) Copyright (c) 1987, 1993, 1994\n\ 371590Srgrimes The Regents of the University of California. All rights reserved.\n"; 381590Srgrimes#endif /* not lint */ 391590Srgrimes 401590Srgrimes#ifndef lint 411590Srgrimesstatic char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94"; 421590Srgrimes#endif /* not lint */ 431590Srgrimes 441590Srgrimes#include <sys/param.h> 451590Srgrimes#include <sys/stat.h> 461590Srgrimes 471590Srgrimes#include <err.h> 481590Srgrimes#include <fcntl.h> 4916438Sache#include <locale.h> 501590Srgrimes#include <paths.h> 511590Srgrimes#include <signal.h> 521590Srgrimes#include <stdio.h> 531590Srgrimes#include <stdlib.h> 541590Srgrimes#include <string.h> 551590Srgrimes#include <time.h> 561590Srgrimes#include <unistd.h> 571590Srgrimes#include <utmp.h> 5811547Sdg#include <sys/queue.h> 591590Srgrimes 601590Srgrimes#define NO 0 /* false/no */ 611590Srgrimes#define YES 1 /* true/yes */ 621590Srgrimes 631590Srgrimesstatic struct utmp buf[1024]; /* utmp read buffer */ 641590Srgrimes 651590Srgrimestypedef struct arg { 661590Srgrimes char *name; /* argument */ 671590Srgrimes#define HOST_TYPE -2 681590Srgrimes#define TTY_TYPE -3 691590Srgrimes#define USER_TYPE -4 701590Srgrimes int type; /* type of arg */ 711590Srgrimes struct arg *next; /* linked list pointer */ 721590Srgrimes} ARG; 731590SrgrimesARG *arglist; /* head of linked list */ 741590Srgrimes 7511547SdgLIST_HEAD(ttylisthead, ttytab) ttylist; 7611547Sdg 7711547Sdgstruct ttytab { 781590Srgrimes long logout; /* log out time */ 791590Srgrimes char tty[UT_LINESIZE + 1]; /* terminal name */ 8011547Sdg LIST_ENTRY(ttytab) list; 8111547Sdg}; 821590Srgrimes 831590Srgrimesstatic long currentout, /* current logout value */ 841590Srgrimes maxrec; /* records to display */ 851590Srgrimesstatic char *file = _PATH_WTMP; /* wtmp file */ 861590Srgrimes 871590Srgrimesvoid addarg __P((int, char *)); 881590Srgrimesvoid hostconv __P((char *)); 891590Srgrimesvoid onintr __P((int)); 901590Srgrimeschar *ttyconv __P((char *)); 9111547Sdgint want __P((struct utmp *)); 921590Srgrimesvoid wtmp __P((void)); 931590Srgrimes 941590Srgrimesint 951590Srgrimesmain(argc, argv) 961590Srgrimes int argc; 971590Srgrimes char *argv[]; 981590Srgrimes{ 991590Srgrimes extern int optind; 1001590Srgrimes extern char *optarg; 1011590Srgrimes int ch; 1021590Srgrimes char *p; 1031590Srgrimes 10416438Sache (void) setlocale(LC_TIME, ""); 10516438Sache 1061590Srgrimes maxrec = -1; 1071590Srgrimes while ((ch = getopt(argc, argv, "0123456789f:h:t:")) != EOF) 1081590Srgrimes switch (ch) { 1091590Srgrimes case '0': case '1': case '2': case '3': case '4': 1101590Srgrimes case '5': case '6': case '7': case '8': case '9': 1111590Srgrimes /* 1121590Srgrimes * kludge: last was originally designed to take 1131590Srgrimes * a number after a dash. 1141590Srgrimes */ 1151590Srgrimes if (maxrec == -1) { 1161590Srgrimes p = argv[optind - 1]; 1171590Srgrimes if (p[0] == '-' && p[1] == ch && !p[2]) 1181590Srgrimes maxrec = atol(++p); 1191590Srgrimes else 1201590Srgrimes maxrec = atol(argv[optind] + 1); 1211590Srgrimes if (!maxrec) 1221590Srgrimes exit(0); 1231590Srgrimes } 1241590Srgrimes break; 1251590Srgrimes case 'f': 1261590Srgrimes file = optarg; 1271590Srgrimes break; 1281590Srgrimes case 'h': 1291590Srgrimes hostconv(optarg); 1301590Srgrimes addarg(HOST_TYPE, optarg); 1311590Srgrimes break; 1321590Srgrimes case 't': 1331590Srgrimes addarg(TTY_TYPE, ttyconv(optarg)); 1341590Srgrimes break; 1351590Srgrimes case '?': 1361590Srgrimes default: 1371590Srgrimes (void)fprintf(stderr, 1381590Srgrimes "usage: last [-#] [-f file] [-t tty] [-h hostname] [user ...]\n"); 1391590Srgrimes exit(1); 1401590Srgrimes } 1411590Srgrimes 1421590Srgrimes if (argc) { 1431590Srgrimes setlinebuf(stdout); 1441590Srgrimes for (argv += optind; *argv; ++argv) { 1451590Srgrimes#define COMPATIBILITY 1461590Srgrimes#ifdef COMPATIBILITY 1471590Srgrimes /* code to allow "last p5" to work */ 1481590Srgrimes addarg(TTY_TYPE, ttyconv(*argv)); 1491590Srgrimes#endif 1501590Srgrimes addarg(USER_TYPE, *argv); 1511590Srgrimes } 1521590Srgrimes } 1531590Srgrimes wtmp(); 1541590Srgrimes exit(0); 1551590Srgrimes} 1561590Srgrimes 1571590Srgrimes/* 1581590Srgrimes * wtmp -- 1591590Srgrimes * read through the wtmp file 1601590Srgrimes */ 1611590Srgrimesvoid 1621590Srgrimeswtmp() 1631590Srgrimes{ 1641590Srgrimes struct utmp *bp; /* current structure */ 16519223Sjoerg struct ttytab *tt, *ttx; /* ttylist entry */ 1661590Srgrimes struct stat stb; /* stat of file for size */ 1671590Srgrimes long bl, delta; /* time difference */ 1681590Srgrimes int bytes, wfd; 16916438Sache char *crmsg; 17016438Sache char ct[80]; 17116438Sache struct tm *tm; 1721590Srgrimes 17311547Sdg LIST_INIT(&ttylist); 17411547Sdg 1751590Srgrimes if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1) 1761590Srgrimes err(1, "%s", file); 1771590Srgrimes bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf); 1781590Srgrimes 1791590Srgrimes (void)time(&buf[0].ut_time); 1801590Srgrimes (void)signal(SIGINT, onintr); 1811590Srgrimes (void)signal(SIGQUIT, onintr); 1821590Srgrimes 1831590Srgrimes while (--bl >= 0) { 1841590Srgrimes if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 || 1851590Srgrimes (bytes = read(wfd, buf, sizeof(buf))) == -1) 1861590Srgrimes err(1, "%s", file); 1871590Srgrimes for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) { 1881590Srgrimes /* 1891590Srgrimes * if the terminal line is '~', the machine stopped. 1901590Srgrimes * see utmp(5) for more info. 1911590Srgrimes */ 1921590Srgrimes if (bp->ut_line[0] == '~' && !bp->ut_line[1]) { 1931590Srgrimes /* everybody just logged out */ 19419223Sjoerg for (tt = ttylist.lh_first; tt;) { 19511547Sdg LIST_REMOVE(tt, list); 19619223Sjoerg ttx = tt; 19719223Sjoerg tt = tt->list.le_next; 19819223Sjoerg free(ttx); 19911547Sdg } 2001590Srgrimes currentout = -bp->ut_time; 2011590Srgrimes crmsg = strncmp(bp->ut_name, "shutdown", 2021590Srgrimes UT_NAMESIZE) ? "crash" : "shutdown"; 20311547Sdg if (want(bp)) { 20416438Sache tm = localtime(&bp->ut_time); 20516438Sache (void) strftime(ct, sizeof(ct), "%c", tm); 20611547Sdg printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s \n", 2071590Srgrimes UT_NAMESIZE, UT_NAMESIZE, 2081590Srgrimes bp->ut_name, UT_LINESIZE, 2091590Srgrimes UT_LINESIZE, bp->ut_line, 2101590Srgrimes UT_HOSTSIZE, UT_HOSTSIZE, 2111590Srgrimes bp->ut_host, ct, ct + 11); 2121590Srgrimes if (maxrec != -1 && !--maxrec) 2131590Srgrimes return; 2141590Srgrimes } 2151590Srgrimes continue; 2161590Srgrimes } 2171590Srgrimes /* 2181590Srgrimes * if the line is '{' or '|', date got set; see 2191590Srgrimes * utmp(5) for more info. 2201590Srgrimes */ 2211590Srgrimes if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|') 2221590Srgrimes && !bp->ut_line[1]) { 22311547Sdg if (want(bp)) { 22416438Sache tm = localtime(&bp->ut_time); 22516438Sache (void) strftime(ct, sizeof(ct), "%c", tm); 22611547Sdg printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s \n", 22711547Sdg UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, 22811547Sdg UT_LINESIZE, UT_LINESIZE, bp->ut_line, 22911547Sdg UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, 23011547Sdg ct, ct + 11); 2311590Srgrimes if (maxrec && !--maxrec) 2321590Srgrimes return; 2331590Srgrimes } 2341590Srgrimes continue; 2351590Srgrimes } 23611547Sdg if (bp->ut_name[0] == '\0' || want(bp)) { 23711547Sdg /* find associated tty */ 23811547Sdg for (tt = ttylist.lh_first; ; tt = tt->list.le_next) { 23911547Sdg if (tt == NULL) { 24011547Sdg /* add new one */ 24111547Sdg tt = malloc(sizeof(struct ttytab)); 24211547Sdg if (tt == NULL) 24311547Sdg err(1, "malloc failure"); 24411547Sdg tt->logout = currentout; 24511547Sdg strncpy(tt->tty, bp->ut_line, UT_LINESIZE); 24611547Sdg LIST_INSERT_HEAD(&ttylist, tt, list); 24711547Sdg break; 24811547Sdg } 24911547Sdg if (!strncmp(tt->tty, bp->ut_line, UT_LINESIZE)) 25011547Sdg break; 2511590Srgrimes } 25211547Sdg if (bp->ut_name[0]) { 25311547Sdg /* 25411547Sdg * when uucp and ftp log in over a network, the entry in 25511547Sdg * the utmp file is the name plus their process id. See 25611547Sdg * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information. 25711547Sdg */ 25811547Sdg if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1)) 25911547Sdg bp->ut_line[3] = '\0'; 26011547Sdg else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1)) 26111547Sdg bp->ut_line[4] = '\0'; 26216438Sache tm = localtime(&bp->ut_time); 26316438Sache (void) strftime(ct, sizeof(ct), "%c", tm); 26411547Sdg printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s ", 26511547Sdg UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, 26611547Sdg UT_LINESIZE, UT_LINESIZE, bp->ut_line, 26711547Sdg UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, 26811547Sdg ct, ct + 11); 26911547Sdg if (!tt->logout) 27011547Sdg puts(" still logged in"); 27111547Sdg else { 27211547Sdg if (tt->logout < 0) { 27311547Sdg tt->logout = -tt->logout; 27411547Sdg printf("- %s", crmsg); 27511547Sdg } 27616438Sache else { 27716438Sache tm = localtime(&tt->logout); 27816438Sache (void) strftime(ct, sizeof(ct), "%c", tm); 27916438Sache printf("- %5.5s", ct + 11); 28016438Sache } 28111547Sdg delta = tt->logout - bp->ut_time; 28216438Sache tm = gmtime(&delta); 28316438Sache (void) strftime(ct, sizeof(ct), "%c", tm); 28411547Sdg if (delta < 86400) 28516438Sache printf(" (%5.5s)\n", ct + 11); 28611547Sdg else 28711547Sdg printf(" (%ld+%5.5s)\n", 28816438Sache delta / 86400, ct + 11); 2891590Srgrimes } 29011547Sdg LIST_REMOVE(tt, list); 29111547Sdg free(tt); 29211547Sdg if (maxrec != -1 && !--maxrec) 29311547Sdg return; 29411547Sdg } else { 29511547Sdg tt->logout = bp->ut_time; 2961590Srgrimes } 2971590Srgrimes } 2981590Srgrimes } 2991590Srgrimes } 30016438Sache tm = localtime(&buf[0].ut_time); 30116438Sache (void) strftime(ct, sizeof(ct), "%c", tm); 3021590Srgrimes printf("\nwtmp begins %10.10s %5.5s \n", ct, ct + 11); 3031590Srgrimes} 3041590Srgrimes 3051590Srgrimes/* 3061590Srgrimes * want -- 3071590Srgrimes * see if want this entry 3081590Srgrimes */ 3091590Srgrimesint 31011547Sdgwant(bp) 3111590Srgrimes struct utmp *bp; 3121590Srgrimes{ 3131590Srgrimes ARG *step; 3141590Srgrimes 3151590Srgrimes if (!arglist) 3161590Srgrimes return (YES); 3171590Srgrimes 3181590Srgrimes for (step = arglist; step; step = step->next) 3191590Srgrimes switch(step->type) { 3201590Srgrimes case HOST_TYPE: 3211590Srgrimes if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE)) 3221590Srgrimes return (YES); 3231590Srgrimes break; 3241590Srgrimes case TTY_TYPE: 3251590Srgrimes if (!strncmp(step->name, bp->ut_line, UT_LINESIZE)) 3261590Srgrimes return (YES); 3271590Srgrimes break; 3281590Srgrimes case USER_TYPE: 3291590Srgrimes if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE)) 3301590Srgrimes return (YES); 3311590Srgrimes break; 3321590Srgrimes } 3331590Srgrimes return (NO); 3341590Srgrimes} 3351590Srgrimes 3361590Srgrimes/* 3371590Srgrimes * addarg -- 3381590Srgrimes * add an entry to a linked list of arguments 3391590Srgrimes */ 3401590Srgrimesvoid 3411590Srgrimesaddarg(type, arg) 3421590Srgrimes int type; 3431590Srgrimes char *arg; 3441590Srgrimes{ 3451590Srgrimes ARG *cur; 3461590Srgrimes 3471590Srgrimes if (!(cur = (ARG *)malloc((u_int)sizeof(ARG)))) 3481590Srgrimes err(1, "malloc failure"); 3491590Srgrimes cur->next = arglist; 3501590Srgrimes cur->type = type; 3511590Srgrimes cur->name = arg; 3521590Srgrimes arglist = cur; 3531590Srgrimes} 3541590Srgrimes 3551590Srgrimes/* 3561590Srgrimes * hostconv -- 3571590Srgrimes * convert the hostname to search pattern; if the supplied host name 3581590Srgrimes * has a domain attached that is the same as the current domain, rip 3591590Srgrimes * off the domain suffix since that's what login(1) does. 3601590Srgrimes */ 3611590Srgrimesvoid 3621590Srgrimeshostconv(arg) 3631590Srgrimes char *arg; 3641590Srgrimes{ 3651590Srgrimes static int first = 1; 3661590Srgrimes static char *hostdot, name[MAXHOSTNAMELEN]; 3671590Srgrimes char *argdot; 3681590Srgrimes 3691590Srgrimes if (!(argdot = strchr(arg, '.'))) 3701590Srgrimes return; 3711590Srgrimes if (first) { 3721590Srgrimes first = 0; 3731590Srgrimes if (gethostname(name, sizeof(name))) 3741590Srgrimes err(1, "gethostname"); 3751590Srgrimes hostdot = strchr(name, '.'); 3761590Srgrimes } 3771590Srgrimes if (hostdot && !strcasecmp(hostdot, argdot)) 3781590Srgrimes *argdot = '\0'; 3791590Srgrimes} 3801590Srgrimes 3811590Srgrimes/* 3821590Srgrimes * ttyconv -- 3831590Srgrimes * convert tty to correct name. 3841590Srgrimes */ 3851590Srgrimeschar * 3861590Srgrimesttyconv(arg) 3871590Srgrimes char *arg; 3881590Srgrimes{ 3891590Srgrimes char *mval; 3901590Srgrimes 3911590Srgrimes /* 3921590Srgrimes * kludge -- we assume that all tty's end with 3931590Srgrimes * a two character suffix. 3941590Srgrimes */ 3951590Srgrimes if (strlen(arg) == 2) { 3961590Srgrimes /* either 6 for "ttyxx" or 8 for "console" */ 3971590Srgrimes if (!(mval = malloc((u_int)8))) 3981590Srgrimes err(1, "malloc failure"); 3991590Srgrimes if (!strcmp(arg, "co")) 4001590Srgrimes (void)strcpy(mval, "console"); 4011590Srgrimes else { 4021590Srgrimes (void)strcpy(mval, "tty"); 4031590Srgrimes (void)strcpy(mval + 3, arg); 4041590Srgrimes } 4051590Srgrimes return (mval); 4061590Srgrimes } 4071590Srgrimes if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1)) 4081590Srgrimes return (arg + 5); 4091590Srgrimes return (arg); 4101590Srgrimes} 4111590Srgrimes 4121590Srgrimes/* 4131590Srgrimes * onintr -- 4141590Srgrimes * on interrupt, we inform the user how far we've gotten 4151590Srgrimes */ 4161590Srgrimesvoid 4171590Srgrimesonintr(signo) 4181590Srgrimes int signo; 4191590Srgrimes{ 42016438Sache char ct[80]; 42116438Sache struct tm *tm; 4221590Srgrimes 42316438Sache tm = localtime(&buf[0].ut_time); 42416438Sache (void) strftime(ct, sizeof(ct), "%c", tm); 4251590Srgrimes printf("\ninterrupted %10.10s %5.5s \n", ct, ct + 11); 4261590Srgrimes if (signo == SIGINT) 4271590Srgrimes exit(1); 4281590Srgrimes (void)fflush(stdout); /* fix required for rsh */ 4291590Srgrimes} 430