Home | History | Annotate | Line # | Download | only in last
last.c revision 1.12
      1 /*	$NetBSD: last.c,v 1.12 1998/12/19 17:38:39 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1987, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT(
     39 "@(#) Copyright (c) 1987, 1993, 1994\n\
     40 	The Regents of the University of California.  All rights reserved.\n");
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 #if 0
     45 static char sccsid[] = "@(#)last.c	8.2 (Berkeley) 4/2/94";
     46 #endif
     47 __RCSID("$NetBSD: last.c,v 1.12 1998/12/19 17:38:39 christos Exp $");
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/stat.h>
     52 
     53 #include <err.h>
     54 #include <fcntl.h>
     55 #include <paths.h>
     56 #include <signal.h>
     57 #include <stdio.h>
     58 #include <stdlib.h>
     59 #include <string.h>
     60 #include <time.h>
     61 #include <tzfile.h>
     62 #include <unistd.h>
     63 #include <utmp.h>
     64 
     65 #define	NO	0				/* false/no */
     66 #define	YES	1				/* true/yes */
     67 
     68 static struct utmp	buf[1024];		/* utmp read buffer */
     69 
     70 typedef struct arg {
     71 	char	*name;				/* argument */
     72 #define	HOST_TYPE	-2
     73 #define	TTY_TYPE	-3
     74 #define	USER_TYPE	-4
     75 	int	type;				/* type of arg */
     76 	struct arg	*next;			/* linked list pointer */
     77 } ARG;
     78 ARG	*arglist;				/* head of linked list */
     79 
     80 typedef struct ttytab {
     81 	time_t	logout;				/* log out time */
     82 	char	tty[UT_LINESIZE + 1];		/* terminal name */
     83 	struct ttytab	*next;			/* linked list pointer */
     84 } TTY;
     85 TTY	*ttylist;				/* head of linked list */
     86 
     87 static time_t	currentout;			/* current logout value */
     88 static long	maxrec;				/* records to display */
     89 static char	*file = _PATH_WTMP;		/* wtmp file */
     90 static int	fulltime = 0;                   /* Display seconds? */
     91 
     92 int	 main __P((int, char *[]));
     93 void	 addarg __P((int, char *));
     94 TTY	*addtty __P((char *));
     95 void	 hostconv __P((char *));
     96 void	 onintr __P((int));
     97 char	*ttyconv __P((char *));
     98 int	 want __P((struct utmp *, int));
     99 void	 wtmp __P((void));
    100 
    101 int
    102 main(argc, argv)
    103 	int argc;
    104 	char *argv[];
    105 {
    106 	extern int optind;
    107 	extern char *optarg;
    108 	int ch;
    109 	char *p;
    110 
    111 	maxrec = -1;
    112 	while ((ch = getopt(argc, argv, "0123456789f:h:t:T")) != -1)
    113 		switch (ch) {
    114 		case '0': case '1': case '2': case '3': case '4':
    115 		case '5': case '6': case '7': case '8': case '9':
    116 			/*
    117 			 * kludge: last was originally designed to take
    118 			 * a number after a dash.
    119 			 */
    120 			if (maxrec == -1) {
    121 				p = argv[optind - 1];
    122 				if (p[0] == '-' && p[1] == ch && !p[2])
    123 					maxrec = atol(++p);
    124 				else
    125 					maxrec = atol(argv[optind] + 1);
    126 				if (!maxrec)
    127 					exit(0);
    128 			}
    129 			break;
    130 		case 'f':
    131 			file = optarg;
    132 			break;
    133 		case 'h':
    134 			hostconv(optarg);
    135 			addarg(HOST_TYPE, optarg);
    136 			break;
    137 		case 't':
    138 			addarg(TTY_TYPE, ttyconv(optarg));
    139 			break;
    140 		case 'T':
    141 			fulltime = 1;
    142 			break;
    143 		case '?':
    144 		default:
    145 			(void)fprintf(stderr,
    146 	"usage: last [-#] [-f file] [-t tty] [-h hostname] [-T] [user ...]\n");
    147 			exit(1);
    148 		}
    149 
    150 	if (argc) {
    151 		setlinebuf(stdout);
    152 		for (argv += optind; *argv; ++argv) {
    153 #define	COMPATIBILITY
    154 #ifdef	COMPATIBILITY
    155 			/* code to allow "last p5" to work */
    156 			addarg(TTY_TYPE, ttyconv(*argv));
    157 #endif
    158 			addarg(USER_TYPE, *argv);
    159 		}
    160 	}
    161 	wtmp();
    162 	exit(0);
    163 }
    164 
    165 /*
    166  * wtmp --
    167  *	read through the wtmp file
    168  */
    169 void
    170 wtmp()
    171 {
    172 	struct utmp	*bp;		/* current structure */
    173 	TTY	*T;			/* tty list entry */
    174 	struct stat	stb;		/* stat of file for size */
    175 	time_t	delta;			/* time difference */
    176 	off_t	bl;
    177 	int	timesize;		/* how much of time string to print */
    178 	int	bytes, wfd;
    179 	char	*ct, *crmsg;
    180 
    181 	crmsg = NULL;
    182 
    183 	if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
    184 		err(1, "%s", file);
    185 	bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
    186 
    187 	if (fulltime)
    188 		timesize = 8;	/* HH:MM:SS */
    189 	else
    190 		timesize = 5;	/* HH:MM */
    191 
    192 	(void)time(&buf[0].ut_time);
    193 	(void)signal(SIGINT, onintr);
    194 	(void)signal(SIGQUIT, onintr);
    195 
    196 	while (--bl >= 0) {
    197 		if (lseek(wfd, bl * sizeof(buf), SEEK_SET) == -1 ||
    198 		    (bytes = read(wfd, buf, sizeof(buf))) == -1)
    199 			err(1, "%s", file);
    200 		for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
    201 			/*
    202 			 * if the terminal line is '~', the machine stopped.
    203 			 * see utmp(5) for more info.
    204 			 */
    205 			if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
    206 				/* everybody just logged out */
    207 				for (T = ttylist; T; T = T->next)
    208 					T->logout = -bp->ut_time;
    209 				currentout = -bp->ut_time;
    210 				crmsg = strncmp(bp->ut_name, "shutdown",
    211 				    UT_NAMESIZE) ? "crash" : "shutdown";
    212 				if (want(bp, NO)) {
    213 					ct = ctime(&bp->ut_time);
    214 				printf("%-*.*s  %-*.*s %-*.*s %10.10s %*.*s \n",
    215 					    (int)UT_NAMESIZE, (int)UT_NAMESIZE,
    216 					    bp->ut_name, (int)UT_LINESIZE,
    217 					    (int)UT_LINESIZE, bp->ut_line,
    218 					    (int)UT_HOSTSIZE, (int)UT_HOSTSIZE,
    219 					    bp->ut_host, ct, timesize,
    220 				            timesize, ct + 11);
    221 					if (maxrec != -1 && !--maxrec)
    222 						return;
    223 				}
    224 				continue;
    225 			}
    226 			/*
    227 			 * if the line is '{' or '|', date got set; see
    228 			 * utmp(5) for more info.
    229 			 */
    230 			if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
    231 			    && !bp->ut_line[1]) {
    232 				if (want(bp, NO)) {
    233 					ct = ctime(&bp->ut_time);
    234 				printf("%-*.*s  %-*.*s %-*.*s %10.10s %*.*s \n",
    235 				    (int)UT_NAMESIZE, (int)UT_NAMESIZE,
    236 				    bp->ut_name,
    237 				    (int)UT_LINESIZE, (int)UT_LINESIZE,
    238 				    bp->ut_line,
    239 				    (int)UT_HOSTSIZE, (int)UT_HOSTSIZE,
    240 				    bp->ut_host,
    241 				    ct, timesize, timesize, ct + 11);
    242 					if (maxrec && !--maxrec)
    243 						return;
    244 				}
    245 				continue;
    246 			}
    247 			/* find associated tty */
    248 			for (T = ttylist;; T = T->next) {
    249 				if (!T) {
    250 					/* add new one */
    251 					T = addtty(bp->ut_line);
    252 					break;
    253 				}
    254 				if (!strncmp(T->tty, bp->ut_line, UT_LINESIZE))
    255 					break;
    256 			}
    257 			if (bp->ut_name[0] && want(bp, YES)) {
    258 				ct = ctime(&bp->ut_time);
    259 				printf("%-*.*s  %-*.*s %-*.*s %10.10s %*.*s ",
    260 				(int)UT_NAMESIZE, (int)UT_NAMESIZE, bp->ut_name,
    261 				(int)UT_LINESIZE, (int)UT_LINESIZE, bp->ut_line,
    262 				(int)UT_HOSTSIZE, (int)UT_HOSTSIZE, bp->ut_host,
    263 				ct, timesize, timesize, ct + 11);
    264 				if (!T->logout)
    265 					puts("  still logged in");
    266 				else {
    267 					if (T->logout < 0) {
    268 						T->logout = -T->logout;
    269 						printf("- %s", crmsg);
    270 					}
    271 					else
    272 						printf("- %*.*s",
    273 						    timesize, timesize,
    274 						    ctime(&T->logout)+11);
    275 					delta = T->logout - bp->ut_time;
    276 					if (delta < SECSPERDAY)
    277 						printf("  (%*.*s)\n",
    278 						    timesize, timesize,
    279 						    asctime(gmtime(&delta))+11);
    280 					else
    281 						printf(" (%ld+%*.*s)\n",
    282 						    delta / SECSPERDAY,
    283 						    timesize, timesize,
    284 						    asctime(gmtime(&delta))+11);
    285 				}
    286 				if (maxrec != -1 && !--maxrec)
    287 					return;
    288 			}
    289 			T->logout = bp->ut_time;
    290 		}
    291 	}
    292 	ct = ctime(&buf[0].ut_time);
    293 	printf("\nwtmp begins %10.10s %*.*s \n", ct, timesize, timesize, ct + 11);
    294 }
    295 
    296 /*
    297  * want --
    298  *	see if want this entry
    299  */
    300 int
    301 want(bp, check)
    302 	struct utmp *bp;
    303 	int check;
    304 {
    305 	ARG *step;
    306 
    307 	if (check) {
    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 	}
    318 	if (!arglist)
    319 		return (YES);
    320 
    321 	for (step = arglist; step; step = step->next)
    322 		switch(step->type) {
    323 		case HOST_TYPE:
    324 			if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
    325 				return (YES);
    326 			break;
    327 		case TTY_TYPE:
    328 			if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
    329 				return (YES);
    330 			break;
    331 		case USER_TYPE:
    332 			if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
    333 				return (YES);
    334 			break;
    335 	}
    336 	return (NO);
    337 }
    338 
    339 /*
    340  * addarg --
    341  *	add an entry to a linked list of arguments
    342  */
    343 void
    344 addarg(type, arg)
    345 	int type;
    346 	char *arg;
    347 {
    348 	ARG *cur;
    349 
    350 	if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
    351 		err(1, "malloc failure");
    352 	cur->next = arglist;
    353 	cur->type = type;
    354 	cur->name = arg;
    355 	arglist = cur;
    356 }
    357 
    358 /*
    359  * addtty --
    360  *	add an entry to a linked list of ttys
    361  */
    362 TTY *
    363 addtty(ttyname)
    364 	char *ttyname;
    365 {
    366 	TTY *cur;
    367 
    368 	if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
    369 		err(1, "malloc failure");
    370 	cur->next = ttylist;
    371 	cur->logout = currentout;
    372 	memmove(cur->tty, ttyname, UT_LINESIZE);
    373 	return (ttylist = cur);
    374 }
    375 
    376 /*
    377  * hostconv --
    378  *	convert the hostname to search pattern; if the supplied host name
    379  *	has a domain attached that is the same as the current domain, rip
    380  *	off the domain suffix since that's what login(1) does.
    381  */
    382 void
    383 hostconv(arg)
    384 	char *arg;
    385 {
    386 	static int first = 1;
    387 	static char *hostdot, name[MAXHOSTNAMELEN + 1];
    388 	char *argdot;
    389 
    390 	if (!(argdot = strchr(arg, '.')))
    391 		return;
    392 	if (first) {
    393 		first = 0;
    394 		if (gethostname(name, sizeof(name)))
    395 			err(1, "gethostname");
    396 		name[sizeof(name) - 1] = '\0';
    397 		hostdot = strchr(name, '.');
    398 	}
    399 	if (hostdot && !strcasecmp(hostdot, argdot))
    400 		*argdot = '\0';
    401 }
    402 
    403 /*
    404  * ttyconv --
    405  *	convert tty to correct name.
    406  */
    407 char *
    408 ttyconv(arg)
    409 	char *arg;
    410 {
    411 	char *mval;
    412 
    413 	/*
    414 	 * kludge -- we assume that all tty's end with
    415 	 * a two character suffix.
    416 	 */
    417 	if (strlen(arg) == 2) {
    418 		/* either 6 for "ttyxx" or 8 for "console" */
    419 		if (!(mval = malloc((u_int)8)))
    420 			err(1, "malloc failure");
    421 		if (!strcmp(arg, "co"))
    422 			(void)strcpy(mval, "console");
    423 		else {
    424 			(void)strcpy(mval, "tty");
    425 			(void)strcpy(mval + 3, arg);
    426 		}
    427 		return (mval);
    428 	}
    429 	if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
    430 		return (arg + 5);
    431 	return (arg);
    432 }
    433 
    434 /*
    435  * onintr --
    436  *	on interrupt, we inform the user how far we've gotten
    437  */
    438 void
    439 onintr(signo)
    440 	int signo;
    441 {
    442 	char *ct;
    443 
    444 	ct = ctime(&buf[0].ut_time);
    445 	printf("\ninterrupted %10.10s %8.8s \n", ct, ct + 11);
    446 	if (signo == SIGINT)
    447 		exit(1);
    448 	(void)fflush(stdout);			/* fix required for rsh */
    449 }
    450