2 * last(1) from sysvinit project, merged into util-linux in Aug 2013.
4 * Copyright (C) 1991-2004 Miquel van Smoorenburg.
5 * Copyright (C) 2013 Ondrej Oprala <ooprala@redhat.com>
6 * Karel Zak <kzak@redhat.com>
8 * Re-implementation of the 'last' command, this time for Linux. Yes I know
9 * there is BSD last, but I just felt like writing this. No thanks :-). Also,
10 * this version gives lots more info (especially with -x)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <sys/types.h>
40 #include <netinet/in.h>
42 #include <arpa/inet.h>
48 #include "pathnames.h"
50 #include "closestream.h"
51 #include "carefulputc.h"
53 #include "timeutils.h"
54 #include "monotonic.h"
55 #include "fileutils.h"
62 # define SHUTDOWN_TIME 254
65 #ifndef LAST_LOGIN_LEN
66 # define LAST_LOGIN_LEN 8
69 #ifndef LAST_DOMAIN_LEN
70 # define LAST_DOMAIN_LEN 16
73 #ifndef LAST_TIMESTAMP_LEN
74 # define LAST_TIMESTAMP_LEN 32
77 #define UCHUNKSIZE 16384 /* How much we read at once. */
80 unsigned int lastb
:1, /* Is this command 'lastb' */
81 extended
:1, /* Lots of info */
82 showhost
:1, /* Show hostname */
83 altlist
:1, /* Hostname at the end */
84 usedns
:1, /* Use DNS to lookup the hostname */
85 useip
:1; /* Print IP address in number format */
87 unsigned int name_len
; /* Number of login name characters to print */
88 unsigned int domain_len
; /* Number of domain name characters to print */
89 unsigned int maxrecs
; /* Maximum number of records to list */
91 char **show
; /* Match search list */
93 struct timeval boot_time
; /* system boot time */
94 time_t since
; /* at what time to start displaying the file */
95 time_t until
; /* at what time to stop displaying the file */
96 time_t present
; /* who where present at time_t */
97 unsigned int time_fmt
; /* time format */
98 char separator
; /* output separator */
101 /* Double linked list of struct utmp's */
104 struct utmplist
*next
;
105 struct utmplist
*prev
;
108 /* Types of listing */
110 R_CRASH
= 1, /* No logout record, system boot in between */
111 R_DOWN
, /* System brought down in decent way */
112 R_NORMAL
, /* Normal */
113 R_NOW
, /* Still logged in */
114 R_REBOOT
, /* Reboot record. */
115 R_REBOOT_CRASH
, /* Reboot record without matching shutdown */
116 R_PHANTOM
, /* No logout record but session is stale. */
117 R_TIMECHANGE
/* NEW_TIME or OLD_TIME */
121 LAST_TIMEFTM_NONE
= 0,
124 LAST_TIMEFTM_ISO8601
,
126 LAST_TIMEFTM_HHMM
, /* non-public */
129 struct last_timefmt
{
131 int in_len
; /* log-in */
133 int out_len
; /* log-out */
137 static struct last_timefmt timefmts
[] = {
138 [LAST_TIMEFTM_NONE
] = { .name
= "notime" },
139 [LAST_TIMEFTM_SHORT
] = {
143 .in_fmt
= LAST_TIMEFTM_CTIME
,
144 .out_fmt
= LAST_TIMEFTM_HHMM
146 [LAST_TIMEFTM_CTIME
] = {
150 .in_fmt
= LAST_TIMEFTM_CTIME
,
151 .out_fmt
= LAST_TIMEFTM_CTIME
153 [LAST_TIMEFTM_ISO8601
] = {
157 .in_fmt
= LAST_TIMEFTM_ISO8601
,
158 .out_fmt
= LAST_TIMEFTM_ISO8601
162 /* Global variables */
163 static unsigned int recsdone
; /* Number of records listed */
164 static time_t lastdate
; /* Last date we've seen */
165 static time_t currentdate
; /* date when we started processing the file */
168 /* --time-format=option parser */
169 static int which_time_format(const char *s
)
173 for (i
= 0; i
< ARRAY_SIZE(timefmts
); i
++) {
174 if (strcmp(timefmts
[i
].name
, s
) == 0)
177 errx(EXIT_FAILURE
, _("unknown time format: %s"), s
);
182 * Read one utmp entry, return in new format.
183 * Automatically reposition file pointer.
185 static int uread(FILE *fp
, struct utmpx
*u
, int *quit
, const char *filename
)
188 static char buf
[UCHUNKSIZE
];
194 if (quit
== NULL
&& u
!= NULL
) {
198 return fread(u
, sizeof(struct utmpx
), 1, fp
);
203 * Initialize and position.
205 utsize
= sizeof(struct utmpx
);
206 fseeko(fp
, 0, SEEK_END
);
210 o
= ((fpos
- 1) / UCHUNKSIZE
) * UCHUNKSIZE
;
211 if (fseeko(fp
, o
, SEEK_SET
) < 0) {
212 warn(_("seek on %s failed"), filename
);
215 bpos
= (int)(fpos
- o
);
216 if (fread(buf
, bpos
, 1, fp
) != 1) {
217 warn(_("cannot read %s"), filename
);
225 * Read one struct. From the buffer if possible.
229 memcpy(u
, buf
+ bpos
, sizeof(struct utmpx
));
234 * Oops we went "below" the buffer. We should be able to
235 * seek back UCHUNKSIZE bytes.
242 * Copy whatever is left in the buffer.
244 memcpy(tmp
+ (-bpos
), buf
, utsize
+ bpos
);
245 if (fseeko(fp
, fpos
, SEEK_SET
) < 0) {
246 warn(_("seek on %s failed"), filename
);
251 * Read another UCHUNKSIZE bytes.
253 if (fread(buf
, UCHUNKSIZE
, 1, fp
) != 1) {
254 warn(_("cannot read %s"), filename
);
259 * The end of the UCHUNKSIZE byte buffer should be the first
260 * few bytes of the current struct utmp.
262 memcpy(tmp
, buf
+ UCHUNKSIZE
+ bpos
, -bpos
);
265 memcpy(u
, tmp
, sizeof(struct utmpx
));
274 static void int_handler(int sig
__attribute__((unused
)))
276 ul_sig_err(EXIT_FAILURE
, "Interrupted");
282 static void quit_handler(int sig
__attribute__((unused
)))
284 ul_sig_warn("Interrupted");
285 signal(SIGQUIT
, quit_handler
);
290 * Lookup a host with DNS.
292 static int dns_lookup(char *result
, int size
, int useip
, int32_t *a
)
294 struct sockaddr_in sin
;
295 struct sockaddr_in6 sin6
;
300 flags
= useip
? NI_NUMERICHOST
: 0;
304 * 1. If last 3 4bytes are 0, must be IPv4
305 * 2. If IPv6 in IPv4, handle as IPv4
306 * 3. Anything else is IPv6
310 if (a
[0] == 0 && a
[1] == 0 && a
[2] == (int32_t)htonl (0xffff))
313 if (mapped
|| (a
[1] == 0 && a
[2] == 0 && a
[3] == 0)) {
315 sin
.sin_family
= AF_INET
;
317 sin
.sin_addr
.s_addr
= mapped
? a
[3] : a
[0];
318 sa
= (struct sockaddr
*)&sin
;
322 memset(&sin6
, 0, sizeof(sin6
));
323 sin6
.sin6_family
= AF_INET6
;
325 memcpy(sin6
.sin6_addr
.s6_addr
, a
, 16);
326 sa
= (struct sockaddr
*)&sin6
;
327 salen
= sizeof(sin6
);
330 return getnameinfo(sa
, salen
, result
, size
, NULL
, 0, flags
);
333 static int time_formatter(int fmt
, char *dst
, size_t dlen
, time_t *when
)
338 case LAST_TIMEFTM_NONE
:
341 case LAST_TIMEFTM_HHMM
:
345 localtime_r(when
, &tm
);
346 if (!snprintf(dst
, dlen
, "%02d:%02d", tm
.tm_hour
, tm
.tm_min
))
350 case LAST_TIMEFTM_CTIME
:
352 char buf
[CTIME_BUFSIZ
];
355 snprintf(dst
, dlen
, "%s", buf
);
356 ret
= rtrim_whitespace((unsigned char *) dst
);
359 case LAST_TIMEFTM_ISO8601
:
360 ret
= strtime_iso(when
, ISO_TIMESTAMP_T
, dst
, dlen
);
369 * Remove trailing spaces from a string.
371 static void trim_trailing_spaces(char *s
)
377 while (p
> s
&& isspace(*--p
))
386 * Show one line of information on screen
388 static int list(const struct last_control
*ctl
, struct utmpx
*p
, time_t logout_time
, int what
)
390 time_t secs
, utmp_time
;
391 char logintime
[LAST_TIMESTAMP_LEN
];
392 char logouttime
[LAST_TIMESTAMP_LEN
];
393 char length
[LAST_TIMESTAMP_LEN
];
395 char utline
[sizeof(p
->ut_line
) + 1];
397 int mins
, hours
, days
;
399 struct last_timefmt
*fmt
;
402 * uucp and ftp have special-type entries
404 mem2strcpy(utline
, p
->ut_line
, sizeof(p
->ut_line
), sizeof(utline
));
405 if (strncmp(utline
, "ftp", 3) == 0 && isdigit(utline
[3]))
407 if (strncmp(utline
, "uucp", 4) == 0 && isdigit(utline
[4]))
411 * Is this something we want to show?
415 for (walk
= ctl
->show
; *walk
; walk
++) {
416 if (strncmp(p
->ut_user
, *walk
, sizeof(p
->ut_user
)) == 0 ||
417 strcmp(utline
, *walk
) == 0 ||
418 (strncmp(utline
, "tty", 3) == 0 &&
419 strcmp(utline
+ 3, *walk
) == 0)) break;
421 if (*walk
== NULL
) return 0;
427 fmt
= &timefmts
[ctl
->time_fmt
];
429 utmp_time
= p
->ut_tv
.tv_sec
;
432 if (ctl
->present
< utmp_time
)
434 if (0 < logout_time
&& logout_time
< ctl
->present
)
439 if (time_formatter(fmt
->in_fmt
, logintime
,
440 sizeof(logintime
), &utmp_time
) < 0)
441 errx(EXIT_FAILURE
, _("preallocation size exceeded"));
444 secs
= logout_time
- utmp_time
; /* Under strange circumstances, secs < 0 can happen */
445 mins
= (secs
/ 60) % 60;
446 hours
= (secs
/ 3600) % 24;
449 strcpy(logouttime
, "- ");
450 if (time_formatter(fmt
->out_fmt
, logouttime
+ 2,
451 sizeof(logouttime
) - 2, &logout_time
) < 0)
452 errx(EXIT_FAILURE
, _("preallocation size exceeded"));
454 if (logout_time
== currentdate
) {
455 if (ctl
->time_fmt
> LAST_TIMEFTM_SHORT
) {
456 snprintf(logouttime
, sizeof(logouttime
), " still running");
459 snprintf(logouttime
, sizeof(logouttime
), " still");
460 snprintf(length
, sizeof(length
), "running");
463 snprintf(length
, sizeof(length
), "(%d+%02d:%02d)", days
, abs(hours
), abs(mins
)); /* hours and mins always shown as positive (w/o minus sign!) even if secs < 0 */
465 snprintf(length
, sizeof(length
), " (%02d:%02d)", hours
, abs(mins
)); /* mins always shown as positive (w/o minus sign!) even if secs < 0 */
466 } else if (secs
>= 0) {
467 snprintf(length
, sizeof(length
), " (%02d:%02d)", hours
, mins
);
469 snprintf(length
, sizeof(length
), " (-00:%02d)", abs(mins
)); /* mins always shown as positive (w/o minus sign!) even if secs < 0 */
475 snprintf(logouttime
, sizeof(logouttime
), "- crash");
478 snprintf(logouttime
, sizeof(logouttime
), "- down ");
481 if (ctl
->time_fmt
> LAST_TIMEFTM_SHORT
) {
482 snprintf(logouttime
, sizeof(logouttime
), " still logged in");
485 snprintf(logouttime
, sizeof(logouttime
), " still");
486 snprintf(length
, sizeof(length
), "logged in");
490 if (ctl
->time_fmt
> LAST_TIMEFTM_SHORT
) {
491 snprintf(logouttime
, sizeof(logouttime
), " gone - no logout");
493 } else if (ctl
->time_fmt
== LAST_TIMEFTM_SHORT
) {
494 snprintf(logouttime
, sizeof(logouttime
), " gone");
495 snprintf(length
, sizeof(length
), "- no logout");
498 snprintf(length
, sizeof(length
), "no logout");
513 * Look up host with DNS if needed.
516 if (ctl
->usedns
|| ctl
->useip
)
517 r
= dns_lookup(domain
, sizeof(domain
), ctl
->useip
, (int32_t*)p
->ut_addr_v6
);
519 mem2strcpy(domain
, p
->ut_host
, sizeof(p
->ut_host
), sizeof(domain
));
523 len
= snprintf(final
, sizeof(final
),
524 "%-8.*s%c%-12.12s%c%-16.*s%c%-*.*s%c%-*.*s%c%s\n",
525 ctl
->name_len
, p
->ut_user
, ctl
->separator
, utline
, ctl
->separator
,
526 ctl
->domain_len
, domain
, ctl
->separator
,
527 fmt
->in_len
, fmt
->in_len
, logintime
, ctl
->separator
, fmt
->out_len
, fmt
->out_len
,
528 logouttime
, ctl
->separator
, length
);
530 len
= snprintf(final
, sizeof(final
),
531 "%-8.*s%c%-12.12s%c%-*.*s%c%-*.*s%c%-12.12s%c%s\n",
532 ctl
->name_len
, p
->ut_user
, ctl
->separator
, utline
, ctl
->separator
,
533 fmt
->in_len
, fmt
->in_len
, logintime
, ctl
->separator
, fmt
->out_len
, fmt
->out_len
,
534 logouttime
, ctl
->separator
, length
, ctl
->separator
, domain
);
537 len
= snprintf(final
, sizeof(final
),
538 "%-8.*s%c%-12.12s%c%-*.*s%c%-*.*s%c%s\n",
539 ctl
->name_len
, p
->ut_user
, ctl
->separator
, utline
, ctl
->separator
,
540 fmt
->in_len
, fmt
->in_len
, logintime
, ctl
->separator
, fmt
->out_len
, fmt
->out_len
,
541 logouttime
, ctl
->separator
, length
);
543 #if defined(__GLIBC__)
544 # if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0)
545 final
[sizeof(final
)-1] = '\0';
549 trim_trailing_spaces(final
);
551 * Print out "final" string safely.
553 fputs_careful(final
, stdout
, '*', false, 0);
555 if (len
< 0 || (size_t)len
>= sizeof(final
))
559 if (ctl
->maxrecs
&& ctl
->maxrecs
<= recsdone
)
566 static void __attribute__((__noreturn__
)) usage(const struct last_control
*ctl
)
569 fputs(USAGE_HEADER
, out
);
571 " %s [options] [<username>...] [<tty>...]\n"), program_invocation_short_name
);
573 fputs(USAGE_SEPARATOR
, out
);
574 fputs(_("Show a listing of last logged in users.\n"), out
);
576 fputs(USAGE_OPTIONS
, out
);
577 fputs(_(" -<number> how many lines to show\n"), out
);
578 fputs(_(" -a, --hostlast display hostnames in the last column\n"), out
);
579 fputs(_(" -d, --dns translate the IP number back into a hostname\n"), out
);
581 _(" -f, --file <file> use a specific file instead of %s\n"), ctl
->lastb
? _PATH_BTMP
: _PATH_WTMP
);
582 fputs(_(" -F, --fulltimes print full login and logout times and dates\n"), out
);
583 fputs(_(" -i, --ip display IP numbers in numbers-and-dots notation\n"), out
);
584 fputs(_(" -n, --limit <number> how many lines to show\n"), out
);
585 fputs(_(" -R, --nohostname don't display the hostname field\n"), out
);
586 fputs(_(" -s, --since <time> display the lines since the specified time\n"), out
);
587 fputs(_(" -t, --until <time> display the lines until the specified time\n"), out
);
588 fputs(_(" -T, --tab-separated use tabs as delimiters\n"), out
);
589 fputs(_(" -p, --present <time> display who were present at the specified time\n"), out
);
590 fputs(_(" -w, --fullnames display full user and domain names\n"), out
);
591 fputs(_(" -x, --system display system shutdown entries and run level changes\n"), out
);
592 fputs(_(" --time-format <format> show timestamps in the specified <format>:\n"
593 " notime|short|full|iso\n"), out
);
595 fputs(USAGE_SEPARATOR
, out
);
596 fprintf(out
, USAGE_HELP_OPTIONS(22));
597 fprintf(out
, USAGE_MAN_TAIL("last(1)"));
599 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
603 static int is_phantom(const struct last_control
*ctl
, struct utmpx
*ut
)
606 char path
[sizeof(ut
->ut_line
) + 16];
607 char user
[sizeof(ut
->ut_user
) + 1];
610 if (ut
->ut_tv
.tv_sec
< ctl
->boot_time
.tv_sec
)
613 mem2strcpy(user
, ut
->ut_user
, sizeof(ut
->ut_user
), sizeof(user
));
617 snprintf(path
, sizeof(path
), "/proc/%u/loginuid", ut
->ut_pid
);
618 if (access(path
, R_OK
) == 0) {
619 unsigned int loginuid
;
622 if (!(f
= fopen(path
, "r")))
624 if (fscanf(f
, "%u", &loginuid
) != 1)
627 if (!ret
&& pw
->pw_uid
!= loginuid
)
631 char utline
[sizeof(ut
->ut_line
) + 1];
633 mem2strcpy(utline
, ut
->ut_line
, sizeof(ut
->ut_line
), sizeof(utline
));
635 snprintf(path
, sizeof(path
), "/dev/%s", utline
);
638 if (pw
->pw_uid
!= st
.st_uid
)
644 static void process_wtmp_file(const struct last_control
*ctl
,
645 const char *filename
)
647 FILE *fp
; /* File pointer of wtmp file */
649 struct utmpx ut
; /* Current utmp entry */
650 struct utmplist
*ulist
= NULL
; /* All entries */
651 struct utmplist
*p
; /* Pointer into utmplist */
652 struct utmplist
*next
; /* Pointer into utmplist */
654 time_t lastboot
= 0; /* Last boottime */
655 time_t lastrch
= 0; /* Last run level change */
656 time_t lastdown
; /* Last downtime */
657 time_t begintime
; /* When wtmp begins */
658 int whydown
= 0; /* Why we went down: crash or shutdown */
660 int c
, x
; /* Scratch */
661 struct stat st
; /* To stat the [uw]tmp file */
662 int quit
= 0; /* Flag */
663 int down
= 0; /* Down flag */
668 lastdown
= 1596001948;
673 lastdate
= currentdate
= lastrch
= lastdown
;
677 * Install signal handlers
679 signal(SIGINT
, int_handler
);
680 signal(SIGQUIT
, quit_handler
);
686 if ((fp
= fopen(filename
, "r")) == NULL
)
687 err(EXIT_FAILURE
, _("cannot open %s"), filename
);
690 * Optimize the buffer size.
692 setvbuf(fp
, NULL
, _IOFBF
, UCHUNKSIZE
);
695 * Read first structure to capture the time field
697 if (uread(fp
, &ut
, NULL
, filename
) == 1)
698 begintime
= ut
.ut_tv
.tv_sec
;
700 if (fstat(fileno(fp
), &st
) != 0)
701 err(EXIT_FAILURE
, _("stat of %s failed"), filename
);
702 begintime
= st
.st_ctime
;
707 * Go to end of file minus one structure
708 * and/or initialize utmp reading code.
710 uread(fp
, NULL
, NULL
, filename
);
713 * Read struct after struct backwards from the file.
717 if (uread(fp
, &ut
, &quit
, filename
) != 1)
720 if (ctl
->since
&& ut
.ut_tv
.tv_sec
< ctl
->since
)
723 if (ctl
->until
&& ctl
->until
< ut
.ut_tv
.tv_sec
)
726 lastdate
= ut
.ut_tv
.tv_sec
;
729 quit
= list(ctl
, &ut
, ut
.ut_tv
.tv_sec
, R_NORMAL
);
734 * Set ut_type to the correct type.
736 if (strncmp(ut
.ut_line
, "~", 1) == 0) {
737 if (strncmp(ut
.ut_user
, "shutdown", 8) == 0)
738 ut
.ut_type
= SHUTDOWN_TIME
;
739 else if (strncmp(ut
.ut_user
, "reboot", 6) == 0)
740 ut
.ut_type
= BOOT_TIME
;
741 else if (strncmp(ut
.ut_user
, "runlevel", 8) == 0)
742 ut
.ut_type
= RUN_LVL
;
746 * For stupid old applications that don't fill in
750 if (ut
.ut_type
!= DEAD_PROCESS
&&
751 ut
.ut_user
[0] && ut
.ut_line
[0] &&
752 strncmp(ut
.ut_user
, "LOGIN", 5) != 0)
753 ut
.ut_type
= USER_PROCESS
;
755 * Even worse, applications that write ghost
756 * entries: ut_type set to USER_PROCESS but
759 if (ut
.ut_user
[0] == 0)
760 ut
.ut_type
= DEAD_PROCESS
;
765 if (strncmp(ut
.ut_user
, "date", 4) == 0) {
766 if (ut
.ut_line
[0] == '|')
767 ut
.ut_type
= OLD_TIME
;
768 if (ut
.ut_line
[0] == '{')
769 ut
.ut_type
= NEW_TIME
;
773 switch (ut
.ut_type
) {
776 strcpy(ut
.ut_line
, "system down");
777 quit
= list(ctl
, &ut
, lastboot
, R_NORMAL
);
779 lastdown
= lastrch
= ut
.ut_tv
.tv_sec
;
786 ut
.ut_type
== NEW_TIME
? "new time" :
788 quit
= list(ctl
, &ut
, lastdown
, R_TIMECHANGE
);
792 strcpy(ut
.ut_line
, "system boot");
793 if (lastdown
> lastboot
&& lastdown
!= currentdate
)
794 quit
= list(ctl
, &ut
, lastboot
, R_REBOOT_CRASH
);
796 quit
= list(ctl
, &ut
, lastdown
, R_REBOOT
);
797 lastboot
= ut
.ut_tv
.tv_sec
;
803 snprintf(ut
.ut_line
, sizeof(ut
.ut_line
), "(to lvl %c)", x
);
804 quit
= list(ctl
, &ut
, lastrch
, R_NORMAL
);
806 if (x
== '0' || x
== '6') {
807 lastdown
= ut
.ut_tv
.tv_sec
;
809 ut
.ut_type
= SHUTDOWN_TIME
;
811 lastrch
= ut
.ut_tv
.tv_sec
;
816 * This was a login - show the first matching
817 * logout record and delete all records with
821 for (p
= ulist
; p
; p
= next
) {
823 if (strncmp(p
->ut
.ut_line
, ut
.ut_line
,
824 sizeof(ut
.ut_line
)) == 0) {
827 quit
= list(ctl
, &ut
, p
->ut
.ut_tv
.tv_sec
, R_NORMAL
);
831 p
->next
->prev
= p
->prev
;
833 p
->prev
->next
= p
->next
;
840 * Not found? Then crashed, down, still
841 * logged in, or missing logout record.
846 /* Is process still alive? */
847 if (is_phantom(ctl
, &ut
))
851 quit
= list(ctl
, &ut
, lastboot
, c
);
857 * Just store the data if it is
858 * interesting enough.
860 if (ut
.ut_line
[0] == 0)
862 p
= xmalloc(sizeof(struct utmplist
));
863 memcpy(&p
->ut
, &ut
, sizeof(struct utmpx
));
877 /* ignored ut_types */
881 warnx("unrecognized ut_type: %d", ut
.ut_type
);
885 * If we saw a shutdown/reboot record we can remove
886 * the entire current ulist.
889 lastboot
= ut
.ut_tv
.tv_sec
;
890 whydown
= (ut
.ut_type
== SHUTDOWN_TIME
) ? R_DOWN
: R_CRASH
;
891 for (p
= ulist
; p
; p
= next
) {
900 if (ctl
->time_fmt
!= LAST_TIMEFTM_NONE
) {
901 struct last_timefmt
*fmt
;
902 char timestr
[LAST_TIMESTAMP_LEN
];
903 char *tmp
= xstrdup(filename
);
905 fmt
= &timefmts
[ctl
->time_fmt
];
906 if (time_formatter(fmt
->in_fmt
, timestr
,
907 sizeof(timestr
), &begintime
) < 0)
908 errx(EXIT_FAILURE
, _("preallocation size exceeded"));
909 printf(_("\n%s begins %s\n"), basename(tmp
), timestr
);
915 for (p
= ulist
; p
; p
= next
) {
924 int LLVMFuzzerTestOneInput(const uint8_t *data
, size_t size
) {
925 struct last_control ctl
= {
927 .name_len
= LAST_LOGIN_LEN
,
928 .time_fmt
= LAST_TIMEFTM_SHORT
,
929 .domain_len
= LAST_DOMAIN_LEN
,
931 .tv_sec
= 1595978419,
935 char name
[] = "/tmp/test-last-fuzz.XXXXXX";
938 fd
= mkstemp_cloexec(name
);
940 err(EXIT_FAILURE
, "mkstemp() failed");
941 if (write_all(fd
, data
, size
) != 0)
942 err(EXIT_FAILURE
, "write() failed");
944 process_wtmp_file(&ctl
, name
);
952 int main(int argc
, char **argv
)
954 struct last_control ctl
= {
956 .name_len
= LAST_LOGIN_LEN
,
957 .time_fmt
= LAST_TIMEFTM_SHORT
,
958 .domain_len
= LAST_DOMAIN_LEN
961 size_t i
, nfiles
= 0;
966 OPT_TIME_FORMAT
= CHAR_MAX
+ 1
968 static const struct option long_opts
[] = {
969 { "limit", required_argument
, NULL
, 'n' },
970 { "help", no_argument
, NULL
, 'h' },
971 { "file", required_argument
, NULL
, 'f' },
972 { "nohostname", no_argument
, NULL
, 'R' },
973 { "version", no_argument
, NULL
, 'V' },
974 { "hostlast", no_argument
, NULL
, 'a' },
975 { "since", required_argument
, NULL
, 's' },
976 { "until", required_argument
, NULL
, 't' },
977 { "present", required_argument
, NULL
, 'p' },
978 { "system", no_argument
, NULL
, 'x' },
979 { "dns", no_argument
, NULL
, 'd' },
980 { "ip", no_argument
, NULL
, 'i' },
981 { "fulltimes", no_argument
, NULL
, 'F' },
982 { "fullnames", no_argument
, NULL
, 'w' },
983 { "tab-separated", no_argument
, NULL
, 'T' },
984 { "time-format", required_argument
, NULL
, OPT_TIME_FORMAT
},
987 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
988 { 'F', OPT_TIME_FORMAT
}, /* fulltime, time-format */
991 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
993 setlocale(LC_ALL
, "");
994 bindtextdomain(PACKAGE
, LOCALEDIR
);
996 close_stdout_atexit();
998 * Which file do we want to read?
1000 ctl
.lastb
= strcmp(program_invocation_short_name
, "lastb") == 0 ? 1 : 0;
1001 ctl
.separator
= ' ';
1002 while ((c
= getopt_long(argc
, argv
,
1003 "hVf:n:RxadFit:p:s:T0123456789w", long_opts
, NULL
)) != -1) {
1005 err_exclusive_options(c
, long_opts
, excl
, excl_st
);
1012 print_version(EXIT_SUCCESS
);
1020 ctl
.maxrecs
= strtos32_or_err(optarg
, _("failed to parse number"));
1024 files
= xmalloc(sizeof(char *) * argc
);
1025 files
[nfiles
++] = xstrdup(optarg
);
1037 ctl
.time_fmt
= LAST_TIMEFTM_CTIME
;
1040 if (parse_timestamp(optarg
, &p
) < 0)
1041 errx(EXIT_FAILURE
, _("invalid time value \"%s\""), optarg
);
1042 ctl
.present
= (time_t) (p
/ 1000000);
1045 if (parse_timestamp(optarg
, &p
) < 0)
1046 errx(EXIT_FAILURE
, _("invalid time value \"%s\""), optarg
);
1047 ctl
.since
= (time_t) (p
/ 1000000);
1050 if (parse_timestamp(optarg
, &p
) < 0)
1051 errx(EXIT_FAILURE
, _("invalid time value \"%s\""), optarg
);
1052 ctl
.until
= (time_t) (p
/ 1000000);
1055 if (ctl
.name_len
< sizeof_member(struct utmpx
, ut_user
))
1056 ctl
.name_len
= sizeof_member(struct utmpx
, ut_user
);
1057 if (ctl
.domain_len
< sizeof_member(struct utmpx
, ut_host
))
1058 ctl
.domain_len
= sizeof_member(struct utmpx
, ut_host
);
1060 case '0': case '1': case '2': case '3': case '4':
1061 case '5': case '6': case '7': case '8': case '9':
1062 ctl
.maxrecs
= 10 * ctl
.maxrecs
+ c
- '0';
1064 case OPT_TIME_FORMAT
:
1065 ctl
.time_fmt
= which_time_format(optarg
);
1068 ctl
.separator
= '\t';
1071 errtryhelp(EXIT_FAILURE
);
1076 ctl
.show
= argv
+ optind
;
1079 files
= xmalloc(sizeof(char *));
1080 files
[nfiles
++] = xstrdup(ctl
.lastb
? _PATH_BTMP
: _PATH_WTMP
);
1083 for (i
= 0; i
< nfiles
; i
++) {
1084 get_boot_time(&ctl
.boot_time
);
1085 process_wtmp_file(&ctl
, files
[i
]);
1089 return EXIT_SUCCESS
;