2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1987, 1993, 1994\n\
35 The Regents of the University of California. All rights reserved.\n";
39 static const char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/capsicum.h>
46 #include <sys/queue.h>
49 #include <capsicum_helpers.h>
65 #define NO 0 /* false/no */
66 #define YES 1 /* true/yes */
67 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
70 char *name; /* argument */
71 #define REBOOT_TYPE -1
75 int type; /* type of arg */
76 struct arg *next; /* linked list pointer */
78 static ARG *arglist; /* head of linked list */
80 static SLIST_HEAD(, idtab) idlist;
83 time_t logout; /* log out time */
84 char id[sizeof ((struct utmpx *)0)->ut_id]; /* identifier */
85 SLIST_ENTRY(idtab) list;
88 static const char *crmsg; /* cause of last reboot */
89 static time_t currentout; /* current logout value */
90 static long maxrec; /* records to display */
91 static const char *file = NULL; /* utx.log file */
92 static int sflag = 0; /* show delta in seconds */
93 static int width = 5; /* show seconds in delta */
94 static int yflag; /* show year */
96 static int snapfound = 0; /* found snapshot entry? */
97 static time_t snaptime; /* if != 0, we will only
98 * report users logged in
99 * at this snapshot time
102 static void addarg(int, char *);
103 static time_t dateconv(char *);
104 static void doentry(struct utmpx *);
105 static void hostconv(char *);
106 static void printentry(struct utmpx *, struct idtab *);
107 static char *ttyconv(char *);
108 static int want(struct utmpx *);
109 static void usage(void);
110 static void wtmp(void);
115 (void)fprintf(stderr,
116 "usage: last [-swy] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h host]\n"
117 " [-n maxrec] [-t tty] [user ...]\n");
122 main(int argc, char *argv[])
127 (void) setlocale(LC_TIME, "");
128 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
132 while ((ch = getopt(argc, argv, "0123456789d:f:h:n:st:wy")) != -1)
134 case '0': case '1': case '2': case '3': case '4':
135 case '5': case '6': case '7': case '8': case '9':
137 * kludge: last was originally designed to take
138 * a number after a dash.
141 p = strchr(argv[optind - 1], ch);
143 p = strchr(argv[optind], ch);
150 snaptime = dateconv(optarg);
157 addarg(HOST_TYPE, optarg);
161 maxrec = strtol(optarg, &p, 10);
162 if (p == optarg || *p != '\0' || errno != 0 ||
164 errx(1, "%s: bad line count", optarg);
167 sflag++; /* Show delta as seconds */
170 addarg(TTY_TYPE, ttyconv(optarg));
183 if (caph_limit_stdio() < 0)
184 err(1, "can't limit stdio rights");
186 caph_cache_catpages();
189 /* Cache UTX database. */
190 if (setutxdb(UTXDB_LOG, file) != 0)
191 err(1, "%s", file != NULL ? file : "(default utx db)");
193 if (cap_enter() < 0 && errno != ENOSYS)
196 if (sflag && width == 8) usage();
200 for (argv += optind; *argv; ++argv) {
201 if (strcmp(*argv, "reboot") == 0)
202 addarg(REBOOT_TYPE, *argv);
203 #define COMPATIBILITY
205 /* code to allow "last p5" to work */
206 addarg(TTY_TYPE, ttyconv(*argv));
208 addarg(USER_TYPE, *argv);
217 * read through the utx.log file
222 struct utmpx *buf = NULL;
224 static unsigned int amount = 0;
232 /* Load the last entries from the file. */
233 while ((ut = getutxent()) != NULL) {
234 if (amount % 128 == 0) {
235 buf = realloc(buf, (amount + 128) * sizeof *ut);
239 memcpy(&buf[amount++], ut, sizeof *ut);
240 if (t > ut->ut_tv.tv_sec)
241 t = ut->ut_tv.tv_sec;
245 /* Display them in reverse order. */
247 doentry(&buf[--amount]);
250 (void) strftime(ct, sizeof(ct), "%+", tm);
251 printf("\n%s begins %s\n", ((file == NULL) ? "utx.log" : file), ct);
256 * process a single utx.log entry
259 doentry(struct utmpx *bp)
263 /* the machine stopped */
264 if (bp->ut_type == BOOT_TIME || bp->ut_type == SHUTDOWN_TIME) {
265 /* everybody just logged out */
266 while ((tt = SLIST_FIRST(&idlist)) != NULL) {
267 SLIST_REMOVE_HEAD(&idlist, list);
270 currentout = -bp->ut_tv.tv_sec;
271 crmsg = bp->ut_type != SHUTDOWN_TIME ?
272 "crash" : "shutdown";
274 * if we're in snapshot mode, we want to exit if this
275 * shutdown/reboot appears while we we are tracking the
278 if (snaptime && snapfound)
281 * don't print shutdown/reboot entries unless flagged for
283 if (!snaptime && want(bp))
284 printentry(bp, NULL);
288 if (bp->ut_type == OLD_TIME || bp->ut_type == NEW_TIME) {
289 if (want(bp) && !snaptime)
290 printentry(bp, NULL);
294 if (bp->ut_type != USER_PROCESS && bp->ut_type != DEAD_PROCESS)
297 /* find associated identifier */
298 SLIST_FOREACH(tt, &idlist, list)
299 if (!memcmp(tt->id, bp->ut_id, sizeof bp->ut_id))
304 tt = malloc(sizeof(struct idtab));
306 errx(1, "malloc failure");
307 tt->logout = currentout;
308 memcpy(tt->id, bp->ut_id, sizeof bp->ut_id);
309 SLIST_INSERT_HEAD(&idlist, tt, list);
313 * print record if not in snapshot mode and wanted
314 * or in snapshot mode and in snapshot range
316 if (bp->ut_type == USER_PROCESS && (want(bp) ||
317 (bp->ut_tv.tv_sec < snaptime &&
318 (tt->logout > snaptime || tt->logout < 1)))) {
322 tt->logout = bp->ut_tv.tv_sec;
329 * If `tt' is non-NULL, use it and `crmsg' to print the logout time or
330 * logout type (crash/shutdown) as appropriate.
333 printentry(struct utmpx *bp, struct idtab *tt)
337 time_t delta; /* time difference */
340 if (maxrec != -1 && !maxrec--)
342 t = bp->ut_tv.tv_sec;
344 (void) strftime(ct, sizeof(ct), d_first ?
345 (yflag ? "%a %e %b %Y %R" : "%a %e %b %R") :
346 (yflag ? "%a %b %e %Y %R" : "%a %b %e %R"), tm);
347 switch (bp->ut_type) {
349 printf("%-42s", "boot time");
352 printf("%-42s", "shutdown time");
355 printf("%-42s", "old time");
358 printf("%-42s", "new time");
361 printf("%-10s %-8s %-22.22s",
362 bp->ut_user, bp->ut_line, bp->ut_host);
365 printf(" %s%c", ct, tt == NULL ? '\n' : ' ');
369 puts(" still logged in");
372 if (tt->logout < 0) {
373 tt->logout = -tt->logout;
374 printf("- %s", crmsg);
376 tm = localtime(&tt->logout);
377 (void) strftime(ct, sizeof(ct), "%R", tm);
380 delta = tt->logout - bp->ut_tv.tv_sec;
382 printf(" (%8ld)\n", (long)delta);
385 (void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm);
387 printf(" (%s)\n", ct);
389 printf(" (%ld+%s)\n", (long)delta / 86400, ct);
395 * see if want this entry
398 want(struct utmpx *bp)
408 for (step = arglist; step; step = step->next)
411 if (bp->ut_type == BOOT_TIME ||
412 bp->ut_type == SHUTDOWN_TIME)
416 if (!strcasecmp(step->name, bp->ut_host))
420 if (!strcmp(step->name, bp->ut_line))
424 if (!strcmp(step->name, bp->ut_user))
433 * add an entry to a linked list of arguments
436 addarg(int type, char *arg)
440 if ((cur = malloc(sizeof(ARG))) == NULL)
441 errx(1, "malloc failure");
450 * convert the hostname to search pattern; if the supplied host name
451 * has a domain attached that is the same as the current domain, rip
452 * off the domain suffix since that's what login(1) does.
457 static int first = 1;
458 static char *hostdot, name[MAXHOSTNAMELEN];
461 if (!(argdot = strchr(arg, '.')))
465 if (gethostname(name, sizeof(name)))
466 err(1, "gethostname");
467 hostdot = strchr(name, '.');
469 if (hostdot && !strcasecmp(hostdot, argdot))
475 * convert tty to correct name.
483 * kludge -- we assume that all tty's end with
484 * a two character suffix.
486 if (strlen(arg) == 2) {
487 /* either 6 for "ttyxx" or 8 for "console" */
488 if ((mval = malloc(8)) == NULL)
489 errx(1, "malloc failure");
490 if (!strcmp(arg, "co"))
491 (void)strcpy(mval, "console");
493 (void)strcpy(mval, "tty");
494 (void)strcpy(mval + 3, arg);
498 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
505 * Convert the snapshot time in command line given in the format
506 * [[CC]YY]MMDDhhmm[.SS]] to a time_t.
507 * Derived from atime_arg1() in usr.bin/touch/touch.c
517 /* Start with the current time. */
518 if (time(&timet) < 0)
520 if ((t = localtime(&timet)) == NULL)
523 /* [[CC]YY]MMDDhhmm[.SS] */
524 if ((p = strchr(arg, '.')) == NULL)
525 t->tm_sec = 0; /* Seconds defaults to 0. */
527 if (strlen(p + 1) != 2)
530 t->tm_sec = ATOI2(p);
534 switch (strlen(arg)) {
535 case 12: /* CCYYMMDDhhmm */
536 t->tm_year = ATOI2(arg);
540 case 10: /* YYMMDDhhmm */
542 yearset = ATOI2(arg);
543 t->tm_year += yearset;
545 yearset = ATOI2(arg);
547 t->tm_year = yearset + 2000;
549 t->tm_year = yearset + 1900;
551 t->tm_year -= 1900; /* Convert to UNIX time. */
553 case 8: /* MMDDhhmm */
554 t->tm_mon = ATOI2(arg);
555 --t->tm_mon; /* Convert from 01-12 to 00-11 */
556 t->tm_mday = ATOI2(arg);
557 t->tm_hour = ATOI2(arg);
558 t->tm_min = ATOI2(arg);
561 t->tm_hour = ATOI2(arg);
562 t->tm_min = ATOI2(arg);
567 t->tm_isdst = -1; /* Figure out DST. */
571 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");