2 * Copyright (c) 1987, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1987, 1993, 1994\n\
33 The Regents of the University of California. All rights reserved.\n";
37 static const char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
59 #include <sys/queue.h>
61 #define NO 0 /* false/no */
62 #define YES 1 /* true/yes */
63 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
66 char *name; /* argument */
70 int type; /* type of arg */
71 struct arg *next; /* linked list pointer */
73 static ARG *arglist; /* head of linked list */
75 static SLIST_HEAD(, idtab) idlist;
78 time_t logout; /* log out time */
79 char id[sizeof ((struct utmpx *)0)->ut_id]; /* identifier */
80 SLIST_ENTRY(idtab) list;
83 static const char *crmsg; /* cause of last reboot */
84 static time_t currentout; /* current logout value */
85 static long maxrec; /* records to display */
86 static const char *file = NULL; /* utx.log file */
87 static int sflag = 0; /* show delta in seconds */
88 static int width = 5; /* show seconds in delta */
89 static int yflag; /* show year */
91 static int snapfound = 0; /* found snapshot entry? */
92 static time_t snaptime; /* if != 0, we will only
93 * report users logged in
94 * at this snapshot time
97 static void addarg(int, char *);
98 static time_t dateconv(char *);
99 static void doentry(struct utmpx *);
100 static void hostconv(char *);
101 static void printentry(struct utmpx *, struct idtab *);
102 static char *ttyconv(char *);
103 static int want(struct utmpx *);
104 static void usage(void);
105 static void wtmp(void);
110 (void)fprintf(stderr,
111 "usage: last [-swy] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h host]\n"
112 " [-n maxrec] [-t tty] [user ...]\n");
117 main(int argc, char *argv[])
122 (void) setlocale(LC_TIME, "");
123 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
127 while ((ch = getopt(argc, argv, "0123456789d:f:h:n:st:wy")) != -1)
129 case '0': case '1': case '2': case '3': case '4':
130 case '5': case '6': case '7': case '8': case '9':
132 * kludge: last was originally designed to take
133 * a number after a dash.
136 p = strchr(argv[optind - 1], ch);
138 p = strchr(argv[optind], ch);
145 snaptime = dateconv(optarg);
152 addarg(HOST_TYPE, optarg);
156 maxrec = strtol(optarg, &p, 10);
157 if (p == optarg || *p != '\0' || errno != 0 ||
159 errx(1, "%s: bad line count", optarg);
162 sflag++; /* Show delta as seconds */
165 addarg(TTY_TYPE, ttyconv(optarg));
178 if (sflag && width == 8) usage();
182 for (argv += optind; *argv; ++argv) {
183 #define COMPATIBILITY
185 /* code to allow "last p5" to work */
186 addarg(TTY_TYPE, ttyconv(*argv));
188 addarg(USER_TYPE, *argv);
197 * read through the utx.log file
202 struct utmpx *buf = NULL;
204 static unsigned int amount = 0;
212 /* Load the last entries from the file. */
213 if (setutxdb(UTXDB_LOG, file) != 0)
215 while ((ut = getutxent()) != NULL) {
216 if (amount % 128 == 0) {
217 buf = realloc(buf, (amount + 128) * sizeof *ut);
221 memcpy(&buf[amount++], ut, sizeof *ut);
222 if (t > ut->ut_tv.tv_sec)
223 t = ut->ut_tv.tv_sec;
227 /* Display them in reverse order. */
229 doentry(&buf[--amount]);
232 (void) strftime(ct, sizeof(ct), "%+", tm);
233 printf("\n%s begins %s\n", ((file == NULL) ? "utx.log" : file), ct);
238 * process a single utx.log entry
241 doentry(struct utmpx *bp)
245 /* the machine stopped */
246 if (bp->ut_type == BOOT_TIME || bp->ut_type == SHUTDOWN_TIME) {
247 /* everybody just logged out */
248 while ((tt = SLIST_FIRST(&idlist)) != NULL) {
249 SLIST_REMOVE_HEAD(&idlist, list);
252 currentout = -bp->ut_tv.tv_sec;
253 crmsg = bp->ut_type != SHUTDOWN_TIME ?
254 "crash" : "shutdown";
256 * if we're in snapshot mode, we want to exit if this
257 * shutdown/reboot appears while we we are tracking the
260 if (snaptime && snapfound)
263 * don't print shutdown/reboot entries unless flagged for
265 if (!snaptime && want(bp))
266 printentry(bp, NULL);
270 if (bp->ut_type == OLD_TIME || bp->ut_type == NEW_TIME) {
271 if (want(bp) && !snaptime)
272 printentry(bp, NULL);
276 if (bp->ut_type != USER_PROCESS && bp->ut_type != DEAD_PROCESS)
279 /* find associated identifier */
280 SLIST_FOREACH(tt, &idlist, list)
281 if (!memcmp(tt->id, bp->ut_id, sizeof bp->ut_id))
286 tt = malloc(sizeof(struct idtab));
288 errx(1, "malloc failure");
289 tt->logout = currentout;
290 memcpy(tt->id, bp->ut_id, sizeof bp->ut_id);
291 SLIST_INSERT_HEAD(&idlist, tt, list);
295 * print record if not in snapshot mode and wanted
296 * or in snapshot mode and in snapshot range
298 if (bp->ut_type == USER_PROCESS && (want(bp) ||
299 (bp->ut_tv.tv_sec < snaptime &&
300 (tt->logout > snaptime || tt->logout < 1)))) {
304 tt->logout = bp->ut_tv.tv_sec;
311 * If `tt' is non-NULL, use it and `crmsg' to print the logout time or
312 * logout type (crash/shutdown) as appropriate.
315 printentry(struct utmpx *bp, struct idtab *tt)
319 time_t delta; /* time difference */
322 if (maxrec != -1 && !maxrec--)
324 t = bp->ut_tv.tv_sec;
326 (void) strftime(ct, sizeof(ct), d_first ?
327 (yflag ? "%a %e %b %Y %R" : "%a %e %b %R") :
328 (yflag ? "%a %b %e %Y %R" : "%a %b %e %R"), tm);
329 switch (bp->ut_type) {
331 printf("%-42s", "boot time");
334 printf("%-42s", "shutdown time");
337 printf("%-42s", "old time");
340 printf("%-42s", "new time");
343 printf("%-10s %-8s %-22.22s",
344 bp->ut_user, bp->ut_line, bp->ut_host);
347 printf(" %s%c", ct, tt == NULL ? '\n' : ' ');
351 puts(" still logged in");
354 if (tt->logout < 0) {
355 tt->logout = -tt->logout;
356 printf("- %s", crmsg);
358 tm = localtime(&tt->logout);
359 (void) strftime(ct, sizeof(ct), "%R", tm);
362 delta = tt->logout - bp->ut_tv.tv_sec;
364 printf(" (%8ld)\n", (long)delta);
367 (void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm);
369 printf(" (%s)\n", ct);
371 printf(" (%ld+%s)\n", (long)delta / 86400, ct);
377 * see if want this entry
380 want(struct utmpx *bp)
390 for (step = arglist; step; step = step->next)
393 if (!strcasecmp(step->name, bp->ut_host))
397 if (!strcmp(step->name, bp->ut_line))
401 if (!strcmp(step->name, bp->ut_user))
410 * add an entry to a linked list of arguments
413 addarg(int type, char *arg)
417 if ((cur = malloc(sizeof(ARG))) == NULL)
418 errx(1, "malloc failure");
427 * convert the hostname to search pattern; if the supplied host name
428 * has a domain attached that is the same as the current domain, rip
429 * off the domain suffix since that's what login(1) does.
434 static int first = 1;
435 static char *hostdot, name[MAXHOSTNAMELEN];
438 if (!(argdot = strchr(arg, '.')))
442 if (gethostname(name, sizeof(name)))
443 err(1, "gethostname");
444 hostdot = strchr(name, '.');
446 if (hostdot && !strcasecmp(hostdot, argdot))
452 * convert tty to correct name.
460 * kludge -- we assume that all tty's end with
461 * a two character suffix.
463 if (strlen(arg) == 2) {
464 /* either 6 for "ttyxx" or 8 for "console" */
465 if ((mval = malloc(8)) == NULL)
466 errx(1, "malloc failure");
467 if (!strcmp(arg, "co"))
468 (void)strcpy(mval, "console");
470 (void)strcpy(mval, "tty");
471 (void)strcpy(mval + 3, arg);
475 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
482 * Convert the snapshot time in command line given in the format
483 * [[CC]YY]MMDDhhmm[.SS]] to a time_t.
484 * Derived from atime_arg1() in usr.bin/touch/touch.c
494 /* Start with the current time. */
495 if (time(&timet) < 0)
497 if ((t = localtime(&timet)) == NULL)
500 /* [[CC]YY]MMDDhhmm[.SS] */
501 if ((p = strchr(arg, '.')) == NULL)
502 t->tm_sec = 0; /* Seconds defaults to 0. */
504 if (strlen(p + 1) != 2)
507 t->tm_sec = ATOI2(p);
511 switch (strlen(arg)) {
512 case 12: /* CCYYMMDDhhmm */
513 t->tm_year = ATOI2(arg);
517 case 10: /* YYMMDDhhmm */
519 yearset = ATOI2(arg);
520 t->tm_year += yearset;
522 yearset = ATOI2(arg);
524 t->tm_year = yearset + 2000;
526 t->tm_year = yearset + 1900;
528 t->tm_year -= 1900; /* Convert to UNIX time. */
530 case 8: /* MMDDhhmm */
531 t->tm_mon = ATOI2(arg);
532 --t->tm_mon; /* Convert from 01-12 to 00-11 */
533 t->tm_mday = ATOI2(arg);
534 t->tm_hour = ATOI2(arg);
535 t->tm_min = ATOI2(arg);
538 t->tm_hour = ATOI2(arg);
539 t->tm_min = ATOI2(arg);
544 t->tm_isdst = -1; /* Figure out DST. */
548 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");