2 * Copyright (c) 1987, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2018 Philip Paeps
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1987, 1993, 1994\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static const char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
60 #include <sys/queue.h>
64 #define NO 0 /* false/no */
65 #define YES 1 /* true/yes */
66 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
69 char *name; /* argument */
70 #define REBOOT_TYPE -1
74 int type; /* type of arg */
75 struct arg *next; /* linked list pointer */
77 static ARG *arglist; /* head of linked list */
79 static SLIST_HEAD(, idtab) idlist;
82 time_t logout; /* log out time */
83 char id[sizeof ((struct utmpx *)0)->ut_id]; /* identifier */
84 SLIST_ENTRY(idtab) list;
87 static const char *crmsg; /* cause of last reboot */
88 static time_t currentout; /* current logout value */
89 static long maxrec; /* records to display */
90 static const char *file = NULL; /* utx.log file */
91 static int sflag = 0; /* show delta in seconds */
92 static int width = 5; /* show seconds in delta */
93 static int yflag; /* show year */
95 static int snapfound = 0; /* found snapshot entry? */
96 static time_t snaptime; /* if != 0, we will only
97 * report users logged in
98 * at this snapshot time
101 static void addarg(int, char *);
102 static time_t dateconv(char *);
103 static void doentry(struct utmpx *);
104 static void hostconv(char *);
105 static void printentry(struct utmpx *, struct idtab *);
106 static char *ttyconv(char *);
107 static int want(struct utmpx *);
108 static void usage(void);
109 static void wtmp(void);
115 "usage: last [-swy] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h host]\n"
116 " [-n maxrec] [-t tty] [user ...]\n");
121 main(int argc, char *argv[])
126 (void) setlocale(LC_TIME, "");
127 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
129 argc = xo_parse_args(argc, argv);
132 atexit(xo_finish_atexit);
136 while ((ch = getopt(argc, argv, "0123456789d:f:h:n:st:wy")) != -1)
138 case '0': case '1': case '2': case '3': case '4':
139 case '5': case '6': case '7': case '8': case '9':
141 * kludge: last was originally designed to take
142 * a number after a dash.
145 p = strchr(argv[optind - 1], ch);
147 p = strchr(argv[optind], ch);
154 snaptime = dateconv(optarg);
161 addarg(HOST_TYPE, optarg);
165 maxrec = strtol(optarg, &p, 10);
166 if (p == optarg || *p != '\0' || errno != 0 ||
168 xo_errx(1, "%s: bad line count", optarg);
171 sflag++; /* Show delta as seconds */
174 addarg(TTY_TYPE, ttyconv(optarg));
187 if (sflag && width == 8) usage();
191 for (argv += optind; *argv; ++argv) {
192 if (strcmp(*argv, "reboot") == 0)
193 addarg(REBOOT_TYPE, *argv);
194 #define COMPATIBILITY
196 /* code to allow "last p5" to work */
197 addarg(TTY_TYPE, ttyconv(*argv));
199 addarg(USER_TYPE, *argv);
208 * read through the utx.log file
213 struct utmpx *buf = NULL;
215 static unsigned int amount = 0;
223 xo_open_container("last-information");
225 /* Load the last entries from the file. */
226 if (setutxdb(UTXDB_LOG, file) != 0)
227 xo_err(1, "%s", file);
228 while ((ut = getutxent()) != NULL) {
229 if (amount % 128 == 0) {
230 buf = realloc(buf, (amount + 128) * sizeof *ut);
232 xo_err(1, "realloc");
234 memcpy(&buf[amount++], ut, sizeof *ut);
235 if (t > ut->ut_tv.tv_sec)
236 t = ut->ut_tv.tv_sec;
240 /* Display them in reverse order. */
241 xo_open_list("last");
243 doentry(&buf[--amount]);
244 xo_close_list("last");
247 (void) strftime(ct, sizeof(ct), "%+", tm);
248 xo_emit("\n{:utxdb/%s}", (file == NULL) ? "utx.log" : file);
249 xo_attr("seconds", "%lu", (unsigned long) t);
250 xo_emit(" begins {:begins/%s}\n", ct);
251 xo_close_container("last-information");
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 xo_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 xo_open_instance("last");
343 t = bp->ut_tv.tv_sec;
345 (void) strftime(ct, sizeof(ct), d_first ?
346 (yflag ? "%a %e %b %Y %R" : "%a %e %b %R") :
347 (yflag ? "%a %b %e %Y %R" : "%a %b %e %R"), tm);
348 switch (bp->ut_type) {
350 xo_emit("{:user/%-42s/%s}", "boot time");
353 xo_emit("{:user/%-42s/%s}", "shutdown time");
356 xo_emit("{:user/%-42s/%s}", "old time");
359 xo_emit("{:user/%-42s/%s}", "new time");
362 xo_emit("{:user/%-10s/%s} {:tty/%-8s/%s} {:from/%-22.22s/%s}",
363 bp->ut_user, bp->ut_line, bp->ut_host);
366 xo_attr("seconds", "%lu", (unsigned long)t);
367 xo_emit(" {:login-time/%s%c/%s}", ct, tt == NULL ? '\n' : ' ');
371 xo_emit(" {:logout-time/still logged in}\n");
374 if (tt->logout < 0) {
375 tt->logout = -tt->logout;
376 xo_emit("- {:logout-reason/%s}", crmsg);
378 tm = localtime(&tt->logout);
379 (void) strftime(ct, sizeof(ct), "%R", tm);
380 xo_attr("seconds", "%lu", (unsigned long)tt->logout);
381 xo_emit("- {:logout-time/%s}", ct);
383 delta = tt->logout - bp->ut_tv.tv_sec;
384 xo_attr("seconds", "%ld", (long)delta);
386 xo_emit(" ({:session-length/%8ld})\n", (long)delta);
389 (void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm);
391 xo_emit(" ({:session-length/%s})\n", ct);
393 xo_emit(" ({:session-length/%ld+%s})\n",
394 (long)delta / 86400, ct);
398 xo_close_instance("last");
403 * see if want this entry
406 want(struct utmpx *bp)
416 for (step = arglist; step; step = step->next)
419 if (bp->ut_type == BOOT_TIME ||
420 bp->ut_type == SHUTDOWN_TIME)
424 if (!strcasecmp(step->name, bp->ut_host))
428 if (!strcmp(step->name, bp->ut_line))
432 if (!strcmp(step->name, bp->ut_user))
441 * add an entry to a linked list of arguments
444 addarg(int type, char *arg)
448 if ((cur = malloc(sizeof(ARG))) == NULL)
449 xo_errx(1, "malloc failure");
458 * convert the hostname to search pattern; if the supplied host name
459 * has a domain attached that is the same as the current domain, rip
460 * off the domain suffix since that's what login(1) does.
465 static int first = 1;
466 static char *hostdot, name[MAXHOSTNAMELEN];
469 if (!(argdot = strchr(arg, '.')))
473 if (gethostname(name, sizeof(name)))
474 xo_err(1, "gethostname");
475 hostdot = strchr(name, '.');
477 if (hostdot && !strcasecmp(hostdot, argdot))
483 * convert tty to correct name.
491 * kludge -- we assume that all tty's end with
492 * a two character suffix.
494 if (strlen(arg) == 2) {
495 /* either 6 for "ttyxx" or 8 for "console" */
496 if ((mval = malloc(8)) == NULL)
497 xo_errx(1, "malloc failure");
498 if (!strcmp(arg, "co"))
499 (void)strcpy(mval, "console");
501 (void)strcpy(mval, "tty");
502 (void)strcpy(mval + 3, arg);
506 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
513 * Convert the snapshot time in command line given in the format
514 * [[CC]YY]MMDDhhmm[.SS]] to a time_t.
515 * Derived from atime_arg1() in usr.bin/touch/touch.c
525 /* Start with the current time. */
526 if (time(&timet) < 0)
528 if ((t = localtime(&timet)) == NULL)
529 xo_err(1, "localtime");
531 /* [[CC]YY]MMDDhhmm[.SS] */
532 if ((p = strchr(arg, '.')) == NULL)
533 t->tm_sec = 0; /* Seconds defaults to 0. */
535 if (strlen(p + 1) != 2)
538 t->tm_sec = ATOI2(p);
542 switch (strlen(arg)) {
543 case 12: /* CCYYMMDDhhmm */
544 t->tm_year = ATOI2(arg);
548 case 10: /* YYMMDDhhmm */
550 yearset = ATOI2(arg);
551 t->tm_year += yearset;
553 yearset = ATOI2(arg);
555 t->tm_year = yearset + 2000;
557 t->tm_year = yearset + 1900;
559 t->tm_year -= 1900; /* Convert to UNIX time. */
561 case 8: /* MMDDhhmm */
562 t->tm_mon = ATOI2(arg);
563 --t->tm_mon; /* Convert from 01-12 to 00-11 */
564 t->tm_mday = ATOI2(arg);
565 t->tm_hour = ATOI2(arg);
566 t->tm_min = ATOI2(arg);
569 t->tm_hour = ATOI2(arg);
570 t->tm_min = ATOI2(arg);
575 t->tm_isdst = -1; /* Figure out DST. */
579 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");