last.c revision 74588
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. 3260833Sjake * 3360833Sjake * $FreeBSD: head/usr.bin/last/last.c 74588 2001-03-21 19:08:01Z ache $ 341590Srgrimes */ 351590Srgrimes 361590Srgrimes#ifndef lint 371590Srgrimesstatic char copyright[] = 381590Srgrimes"@(#) Copyright (c) 1987, 1993, 1994\n\ 391590Srgrimes The Regents of the University of California. All rights reserved.\n"; 401590Srgrimes#endif /* not lint */ 411590Srgrimes 421590Srgrimes#ifndef lint 431590Srgrimesstatic char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94"; 441590Srgrimes#endif /* not lint */ 451590Srgrimes 461590Srgrimes#include <sys/param.h> 471590Srgrimes#include <sys/stat.h> 481590Srgrimes 491590Srgrimes#include <err.h> 501590Srgrimes#include <fcntl.h> 5174588Sache#include <langinfo.h> 5216438Sache#include <locale.h> 531590Srgrimes#include <paths.h> 541590Srgrimes#include <signal.h> 551590Srgrimes#include <stdio.h> 561590Srgrimes#include <stdlib.h> 571590Srgrimes#include <string.h> 581590Srgrimes#include <time.h> 591590Srgrimes#include <unistd.h> 601590Srgrimes#include <utmp.h> 6111547Sdg#include <sys/queue.h> 621590Srgrimes 631590Srgrimes#define NO 0 /* false/no */ 641590Srgrimes#define YES 1 /* true/yes */ 651590Srgrimes 661590Srgrimesstatic struct utmp buf[1024]; /* utmp read buffer */ 671590Srgrimes 681590Srgrimestypedef struct arg { 691590Srgrimes char *name; /* argument */ 701590Srgrimes#define HOST_TYPE -2 711590Srgrimes#define TTY_TYPE -3 721590Srgrimes#define USER_TYPE -4 731590Srgrimes int type; /* type of arg */ 741590Srgrimes struct arg *next; /* linked list pointer */ 751590Srgrimes} ARG; 761590SrgrimesARG *arglist; /* head of linked list */ 771590Srgrimes 7860938SjakeLIST_HEAD(ttylisthead, ttytab) ttylist; 7911547Sdg 8011547Sdgstruct ttytab { 8136062Sjb time_t logout; /* log out time */ 821590Srgrimes char tty[UT_LINESIZE + 1]; /* terminal name */ 8360938Sjake LIST_ENTRY(ttytab) list; 8411547Sdg}; 851590Srgrimes 861590Srgrimesstatic long currentout, /* current logout value */ 871590Srgrimes maxrec; /* records to display */ 881590Srgrimesstatic char *file = _PATH_WTMP; /* wtmp file */ 8936434Sdannystatic int sflag = 0; /* show delta in seconds */ 9036434Sdannystatic int width = 5; /* show seconds in delta */ 9174588Sachestatic int d_first; 921590Srgrimes 931590Srgrimesvoid addarg __P((int, char *)); 941590Srgrimesvoid hostconv __P((char *)); 951590Srgrimesvoid onintr __P((int)); 961590Srgrimeschar *ttyconv __P((char *)); 9711547Sdgint want __P((struct utmp *)); 981590Srgrimesvoid wtmp __P((void)); 991590Srgrimes 10036434Sdannyvoid 10136434Sdannyusage(void) 10236434Sdanny{ 10336434Sdanny (void)fprintf(stderr, 10436434Sdanny "usage: last [-#] [-f file] [-h hostname] [-t tty] [-s|w] [user ...]\n"); 10536434Sdanny exit(1); 10636434Sdanny} 10736434Sdanny 1081590Srgrimesint 1091590Srgrimesmain(argc, argv) 1101590Srgrimes int argc; 1111590Srgrimes char *argv[]; 1121590Srgrimes{ 1131590Srgrimes int ch; 1141590Srgrimes char *p; 1151590Srgrimes 11616438Sache (void) setlocale(LC_TIME, ""); 11774588Sache d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 11816438Sache 1191590Srgrimes maxrec = -1; 12036434Sdanny while ((ch = getopt(argc, argv, "0123456789f:h:st:w")) != -1) 1211590Srgrimes switch (ch) { 1221590Srgrimes case '0': case '1': case '2': case '3': case '4': 1231590Srgrimes case '5': case '6': case '7': case '8': case '9': 1241590Srgrimes /* 1251590Srgrimes * kludge: last was originally designed to take 1261590Srgrimes * a number after a dash. 1271590Srgrimes */ 1281590Srgrimes if (maxrec == -1) { 1291590Srgrimes p = argv[optind - 1]; 1301590Srgrimes if (p[0] == '-' && p[1] == ch && !p[2]) 1311590Srgrimes maxrec = atol(++p); 1321590Srgrimes else 1331590Srgrimes maxrec = atol(argv[optind] + 1); 1341590Srgrimes if (!maxrec) 1351590Srgrimes exit(0); 1361590Srgrimes } 1371590Srgrimes break; 1381590Srgrimes case 'f': 1391590Srgrimes file = optarg; 1401590Srgrimes break; 1411590Srgrimes case 'h': 1421590Srgrimes hostconv(optarg); 1431590Srgrimes addarg(HOST_TYPE, optarg); 1441590Srgrimes break; 14536434Sdanny case 's': 14636434Sdanny sflag++; /* Show delta as seconds */ 14736434Sdanny break; 1481590Srgrimes case 't': 1491590Srgrimes addarg(TTY_TYPE, ttyconv(optarg)); 1501590Srgrimes break; 15136434Sdanny case 'w': 15236434Sdanny width = 8; 15336434Sdanny break; 1541590Srgrimes case '?': 1551590Srgrimes default: 15636434Sdanny usage(); 1571590Srgrimes } 1581590Srgrimes 15936434Sdanny if (sflag && width == 8) usage(); 16036434Sdanny 1611590Srgrimes if (argc) { 1621590Srgrimes setlinebuf(stdout); 1631590Srgrimes for (argv += optind; *argv; ++argv) { 1641590Srgrimes#define COMPATIBILITY 1651590Srgrimes#ifdef COMPATIBILITY 1661590Srgrimes /* code to allow "last p5" to work */ 1671590Srgrimes addarg(TTY_TYPE, ttyconv(*argv)); 1681590Srgrimes#endif 1691590Srgrimes addarg(USER_TYPE, *argv); 1701590Srgrimes } 1711590Srgrimes } 1721590Srgrimes wtmp(); 1731590Srgrimes exit(0); 1741590Srgrimes} 1751590Srgrimes 1761590Srgrimes/* 1771590Srgrimes * wtmp -- 1781590Srgrimes * read through the wtmp file 1791590Srgrimes */ 1801590Srgrimesvoid 1811590Srgrimeswtmp() 1821590Srgrimes{ 1831590Srgrimes struct utmp *bp; /* current structure */ 18419223Sjoerg struct ttytab *tt, *ttx; /* ttylist entry */ 1851590Srgrimes struct stat stb; /* stat of file for size */ 18636062Sjb long bl; 18736062Sjb time_t delta; /* time difference */ 1881590Srgrimes int bytes, wfd; 18916438Sache char *crmsg; 19016438Sache char ct[80]; 19116438Sache struct tm *tm; 1921590Srgrimes 19311547Sdg LIST_INIT(&ttylist); 19411547Sdg 1951590Srgrimes if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1) 1961590Srgrimes err(1, "%s", file); 1971590Srgrimes bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf); 1981590Srgrimes 1991590Srgrimes (void)time(&buf[0].ut_time); 2001590Srgrimes (void)signal(SIGINT, onintr); 2011590Srgrimes (void)signal(SIGQUIT, onintr); 2021590Srgrimes 2031590Srgrimes while (--bl >= 0) { 2041590Srgrimes if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 || 2051590Srgrimes (bytes = read(wfd, buf, sizeof(buf))) == -1) 2061590Srgrimes err(1, "%s", file); 2071590Srgrimes for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) { 2081590Srgrimes /* 2091590Srgrimes * if the terminal line is '~', the machine stopped. 2101590Srgrimes * see utmp(5) for more info. 2111590Srgrimes */ 2121590Srgrimes if (bp->ut_line[0] == '~' && !bp->ut_line[1]) { 2131590Srgrimes /* everybody just logged out */ 21470467Sphk for (tt = LIST_FIRST(&ttylist); tt;) { 21511547Sdg LIST_REMOVE(tt, list); 21619223Sjoerg ttx = tt; 21770467Sphk tt = LIST_NEXT(tt, list); 21819223Sjoerg free(ttx); 21911547Sdg } 2201590Srgrimes currentout = -bp->ut_time; 2211590Srgrimes crmsg = strncmp(bp->ut_name, "shutdown", 2221590Srgrimes UT_NAMESIZE) ? "crash" : "shutdown"; 22311547Sdg if (want(bp)) { 22416438Sache tm = localtime(&bp->ut_time); 22574588Sache (void) strftime(ct, sizeof(ct), 22674588Sache d_first ? "%a %e %b %R" : 22774588Sache "%a %b %e %R", 22874588Sache tm); 22974588Sache printf("%-*.*s %-*.*s %-*.*s %s\n", 2301590Srgrimes UT_NAMESIZE, UT_NAMESIZE, 2311590Srgrimes bp->ut_name, UT_LINESIZE, 2321590Srgrimes UT_LINESIZE, bp->ut_line, 2331590Srgrimes UT_HOSTSIZE, UT_HOSTSIZE, 23474588Sache bp->ut_host, ct); 2351590Srgrimes if (maxrec != -1 && !--maxrec) 2361590Srgrimes return; 2371590Srgrimes } 2381590Srgrimes continue; 2391590Srgrimes } 2401590Srgrimes /* 2411590Srgrimes * if the line is '{' or '|', date got set; see 2421590Srgrimes * utmp(5) for more info. 2431590Srgrimes */ 2441590Srgrimes if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|') 2451590Srgrimes && !bp->ut_line[1]) { 24611547Sdg if (want(bp)) { 24716438Sache tm = localtime(&bp->ut_time); 24874588Sache (void) strftime(ct, sizeof(ct), 24974588Sache d_first ? "%a %e %b %R" : 25074588Sache "%a %b %e %R", 25174588Sache tm); 25274588Sache printf("%-*.*s %-*.*s %-*.*s %s\n", 25311547Sdg UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, 25411547Sdg UT_LINESIZE, UT_LINESIZE, bp->ut_line, 25511547Sdg UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, 25674588Sache ct); 2571590Srgrimes if (maxrec && !--maxrec) 2581590Srgrimes return; 2591590Srgrimes } 2601590Srgrimes continue; 2611590Srgrimes } 26211547Sdg if (bp->ut_name[0] == '\0' || want(bp)) { 26311547Sdg /* find associated tty */ 26470467Sphk LIST_FOREACH(tt, &ttylist, list) 26511547Sdg if (!strncmp(tt->tty, bp->ut_line, UT_LINESIZE)) 26611547Sdg break; 26770467Sphk 26870467Sphk if (tt == NULL) { 26970467Sphk /* add new one */ 27070467Sphk tt = malloc(sizeof(struct ttytab)); 27170467Sphk if (tt == NULL) 27270467Sphk err(1, "malloc failure"); 27370467Sphk tt->logout = currentout; 27470467Sphk strncpy(tt->tty, bp->ut_line, UT_LINESIZE); 27570467Sphk LIST_INSERT_HEAD(&ttylist, tt, list); 2761590Srgrimes } 27770467Sphk 27811547Sdg if (bp->ut_name[0]) { 27911547Sdg /* 28011547Sdg * when uucp and ftp log in over a network, the entry in 28111547Sdg * the utmp file is the name plus their process id. See 28211547Sdg * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information. 28311547Sdg */ 28411547Sdg if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1)) 28511547Sdg bp->ut_line[3] = '\0'; 28611547Sdg else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1)) 28711547Sdg bp->ut_line[4] = '\0'; 28816438Sache tm = localtime(&bp->ut_time); 28974588Sache (void) strftime(ct, sizeof(ct), 29074588Sache d_first ? "%a %e %b %R" : 29174588Sache "%a %b %e %R", 29274588Sache tm); 29374588Sache printf("%-*.*s %-*.*s %-*.*s %s ", 29411547Sdg UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, 29511547Sdg UT_LINESIZE, UT_LINESIZE, bp->ut_line, 29611547Sdg UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, 29774588Sache ct); 29811547Sdg if (!tt->logout) 29911547Sdg puts(" still logged in"); 30011547Sdg else { 30111547Sdg if (tt->logout < 0) { 30211547Sdg tt->logout = -tt->logout; 30311547Sdg printf("- %s", crmsg); 30411547Sdg } 30516438Sache else { 30616438Sache tm = localtime(&tt->logout); 30774588Sache (void) strftime(ct, sizeof(ct), "%R", tm); 30874588Sache printf("- %s", ct); 30916438Sache } 31011547Sdg delta = tt->logout - bp->ut_time; 31136434Sdanny if ( sflag ) { 31236434Sdanny printf(" (%8lu)\n", 31336434Sdanny delta); 31436434Sdanny } else { 31536434Sdanny tm = gmtime(&delta); 31674588Sache (void) strftime(ct, sizeof(ct), 31774588Sache width >= 8 ? "%T" : "%R", 31874588Sache tm); 31936434Sdanny if (delta < 86400) 32074588Sache printf(" (%s)\n", ct); 32136434Sdanny else 32274588Sache printf(" (%ld+%s)\n", 32374588Sache delta / 86400, ct); 32436434Sdanny } 3251590Srgrimes } 32611547Sdg LIST_REMOVE(tt, list); 32711547Sdg free(tt); 32811547Sdg if (maxrec != -1 && !--maxrec) 32911547Sdg return; 33011547Sdg } else { 33111547Sdg tt->logout = bp->ut_time; 3321590Srgrimes } 3331590Srgrimes } 3341590Srgrimes } 3351590Srgrimes } 33616438Sache tm = localtime(&buf[0].ut_time); 33735658Ssteve (void) strftime(ct, sizeof(ct), "\nwtmp begins %c\n", tm); 33862871Skris printf("%s", ct); 3391590Srgrimes} 3401590Srgrimes 3411590Srgrimes/* 3421590Srgrimes * want -- 3431590Srgrimes * see if want this entry 3441590Srgrimes */ 3451590Srgrimesint 34611547Sdgwant(bp) 3471590Srgrimes struct utmp *bp; 3481590Srgrimes{ 3491590Srgrimes ARG *step; 3501590Srgrimes 3511590Srgrimes if (!arglist) 3521590Srgrimes return (YES); 3531590Srgrimes 3541590Srgrimes for (step = arglist; step; step = step->next) 3551590Srgrimes switch(step->type) { 3561590Srgrimes case HOST_TYPE: 3571590Srgrimes if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE)) 3581590Srgrimes return (YES); 3591590Srgrimes break; 3601590Srgrimes case TTY_TYPE: 3611590Srgrimes if (!strncmp(step->name, bp->ut_line, UT_LINESIZE)) 3621590Srgrimes return (YES); 3631590Srgrimes break; 3641590Srgrimes case USER_TYPE: 3651590Srgrimes if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE)) 3661590Srgrimes return (YES); 3671590Srgrimes break; 3681590Srgrimes } 3691590Srgrimes return (NO); 3701590Srgrimes} 3711590Srgrimes 3721590Srgrimes/* 3731590Srgrimes * addarg -- 3741590Srgrimes * add an entry to a linked list of arguments 3751590Srgrimes */ 3761590Srgrimesvoid 3771590Srgrimesaddarg(type, arg) 3781590Srgrimes int type; 3791590Srgrimes char *arg; 3801590Srgrimes{ 3811590Srgrimes ARG *cur; 3821590Srgrimes 3831590Srgrimes if (!(cur = (ARG *)malloc((u_int)sizeof(ARG)))) 3841590Srgrimes err(1, "malloc failure"); 3851590Srgrimes cur->next = arglist; 3861590Srgrimes cur->type = type; 3871590Srgrimes cur->name = arg; 3881590Srgrimes arglist = cur; 3891590Srgrimes} 3901590Srgrimes 3911590Srgrimes/* 3921590Srgrimes * hostconv -- 3931590Srgrimes * convert the hostname to search pattern; if the supplied host name 3941590Srgrimes * has a domain attached that is the same as the current domain, rip 3951590Srgrimes * off the domain suffix since that's what login(1) does. 3961590Srgrimes */ 3971590Srgrimesvoid 3981590Srgrimeshostconv(arg) 3991590Srgrimes char *arg; 4001590Srgrimes{ 4011590Srgrimes static int first = 1; 4021590Srgrimes static char *hostdot, name[MAXHOSTNAMELEN]; 4031590Srgrimes char *argdot; 4041590Srgrimes 4051590Srgrimes if (!(argdot = strchr(arg, '.'))) 4061590Srgrimes return; 4071590Srgrimes if (first) { 4081590Srgrimes first = 0; 4091590Srgrimes if (gethostname(name, sizeof(name))) 4101590Srgrimes err(1, "gethostname"); 4111590Srgrimes hostdot = strchr(name, '.'); 4121590Srgrimes } 4131590Srgrimes if (hostdot && !strcasecmp(hostdot, argdot)) 4141590Srgrimes *argdot = '\0'; 4151590Srgrimes} 4161590Srgrimes 4171590Srgrimes/* 4181590Srgrimes * ttyconv -- 4191590Srgrimes * convert tty to correct name. 4201590Srgrimes */ 4211590Srgrimeschar * 4221590Srgrimesttyconv(arg) 4231590Srgrimes char *arg; 4241590Srgrimes{ 4251590Srgrimes char *mval; 4261590Srgrimes 4271590Srgrimes /* 4281590Srgrimes * kludge -- we assume that all tty's end with 4291590Srgrimes * a two character suffix. 4301590Srgrimes */ 4311590Srgrimes if (strlen(arg) == 2) { 4321590Srgrimes /* either 6 for "ttyxx" or 8 for "console" */ 4331590Srgrimes if (!(mval = malloc((u_int)8))) 4341590Srgrimes err(1, "malloc failure"); 4351590Srgrimes if (!strcmp(arg, "co")) 4361590Srgrimes (void)strcpy(mval, "console"); 4371590Srgrimes else { 4381590Srgrimes (void)strcpy(mval, "tty"); 4391590Srgrimes (void)strcpy(mval + 3, arg); 4401590Srgrimes } 4411590Srgrimes return (mval); 4421590Srgrimes } 4431590Srgrimes if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1)) 4441590Srgrimes return (arg + 5); 4451590Srgrimes return (arg); 4461590Srgrimes} 4471590Srgrimes 4481590Srgrimes/* 4491590Srgrimes * onintr -- 4501590Srgrimes * on interrupt, we inform the user how far we've gotten 4511590Srgrimes */ 4521590Srgrimesvoid 4531590Srgrimesonintr(signo) 4541590Srgrimes int signo; 4551590Srgrimes{ 45616438Sache char ct[80]; 45716438Sache struct tm *tm; 4581590Srgrimes 45916438Sache tm = localtime(&buf[0].ut_time); 46074588Sache (void) strftime(ct, sizeof(ct), 46174588Sache d_first ? "%a %e %b %R" : "%a %b %e %R", 46274588Sache tm); 46374588Sache printf("\ninterrupted %s\n", ct); 4641590Srgrimes if (signo == SIGINT) 4651590Srgrimes exit(1); 4661590Srgrimes (void)fflush(stdout); /* fix required for rsh */ 4671590Srgrimes} 468