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 */
100 /* Double linked list of struct utmp's */
103 struct utmplist
*next
;
104 struct utmplist
*prev
;
107 /* Types of listing */
109 R_CRASH
= 1, /* No logout record, system boot in between */
110 R_DOWN
, /* System brought down in decent way */
111 R_NORMAL
, /* Normal */
112 R_NOW
, /* Still logged in */
113 R_REBOOT
, /* Reboot record. */
114 R_PHANTOM
, /* No logout record but session is stale. */
115 R_TIMECHANGE
/* NEW_TIME or OLD_TIME */
119 LAST_TIMEFTM_NONE
= 0,
122 LAST_TIMEFTM_ISO8601
,
124 LAST_TIMEFTM_HHMM
, /* non-public */
127 struct last_timefmt
{
129 int in_len
; /* log-in */
131 int out_len
; /* log-out */
135 static struct last_timefmt timefmts
[] = {
136 [LAST_TIMEFTM_NONE
] = { .name
= "notime" },
137 [LAST_TIMEFTM_SHORT
] = {
141 .in_fmt
= LAST_TIMEFTM_CTIME
,
142 .out_fmt
= LAST_TIMEFTM_HHMM
144 [LAST_TIMEFTM_CTIME
] = {
148 .in_fmt
= LAST_TIMEFTM_CTIME
,
149 .out_fmt
= LAST_TIMEFTM_CTIME
151 [LAST_TIMEFTM_ISO8601
] = {
155 .in_fmt
= LAST_TIMEFTM_ISO8601
,
156 .out_fmt
= LAST_TIMEFTM_ISO8601
160 /* Global variables */
161 static unsigned int recsdone
; /* Number of records listed */
162 static time_t lastdate
; /* Last date we've seen */
163 static time_t currentdate
; /* date when we started processing the file */
166 /* --time-format=option parser */
167 static int which_time_format(const char *s
)
171 for (i
= 0; i
< ARRAY_SIZE(timefmts
); i
++) {
172 if (strcmp(timefmts
[i
].name
, s
) == 0)
175 errx(EXIT_FAILURE
, _("unknown time format: %s"), s
);
180 * Read one utmp entry, return in new format.
181 * Automatically reposition file pointer.
183 static int uread(FILE *fp
, struct utmpx
*u
, int *quit
, const char *filename
)
186 static char buf
[UCHUNKSIZE
];
192 if (quit
== NULL
&& u
!= NULL
) {
196 return fread(u
, sizeof(struct utmpx
), 1, fp
);
201 * Initialize and position.
203 utsize
= sizeof(struct utmpx
);
204 fseeko(fp
, 0, SEEK_END
);
208 o
= ((fpos
- 1) / UCHUNKSIZE
) * UCHUNKSIZE
;
209 if (fseeko(fp
, o
, SEEK_SET
) < 0) {
210 warn(_("seek on %s failed"), filename
);
213 bpos
= (int)(fpos
- o
);
214 if (fread(buf
, bpos
, 1, fp
) != 1) {
215 warn(_("cannot read %s"), filename
);
223 * Read one struct. From the buffer if possible.
227 memcpy(u
, buf
+ bpos
, sizeof(struct utmpx
));
232 * Oops we went "below" the buffer. We should be able to
233 * seek back UCHUNKSIZE bytes.
240 * Copy whatever is left in the buffer.
242 memcpy(tmp
+ (-bpos
), buf
, utsize
+ bpos
);
243 if (fseeko(fp
, fpos
, SEEK_SET
) < 0) {
244 warn(_("seek on %s failed"), filename
);
249 * Read another UCHUNKSIZE bytes.
251 if (fread(buf
, UCHUNKSIZE
, 1, fp
) != 1) {
252 warn(_("cannot read %s"), filename
);
257 * The end of the UCHUNKSIZE byte buffer should be the first
258 * few bytes of the current struct utmp.
260 memcpy(tmp
, buf
+ UCHUNKSIZE
+ bpos
, -bpos
);
263 memcpy(u
, tmp
, sizeof(struct utmpx
));
270 * Print a short date.
272 static char *showdate(void)
274 static char s
[CTIME_BUFSIZ
];
276 ctime_r(&lastdate
, s
);
284 static void int_handler(int sig
__attribute__((unused
)))
286 errx(EXIT_FAILURE
, _("Interrupted %s"), showdate());
292 static void quit_handler(int sig
__attribute__((unused
)))
294 warnx(_("Interrupted %s"), showdate());
295 signal(SIGQUIT
, quit_handler
);
300 * Lookup a host with DNS.
302 static int dns_lookup(char *result
, int size
, int useip
, int32_t *a
)
304 struct sockaddr_in sin
;
305 struct sockaddr_in6 sin6
;
310 flags
= useip
? NI_NUMERICHOST
: 0;
314 * 1. If last 3 4bytes are 0, must be IPv4
315 * 2. If IPv6 in IPv4, handle as IPv4
316 * 3. Anything else is IPv6
320 if (a
[0] == 0 && a
[1] == 0 && a
[2] == (int32_t)htonl (0xffff))
323 if (mapped
|| (a
[1] == 0 && a
[2] == 0 && a
[3] == 0)) {
325 sin
.sin_family
= AF_INET
;
327 sin
.sin_addr
.s_addr
= mapped
? a
[3] : a
[0];
328 sa
= (struct sockaddr
*)&sin
;
332 memset(&sin6
, 0, sizeof(sin6
));
333 sin6
.sin6_family
= AF_INET6
;
335 memcpy(sin6
.sin6_addr
.s6_addr
, a
, 16);
336 sa
= (struct sockaddr
*)&sin6
;
337 salen
= sizeof(sin6
);
340 return getnameinfo(sa
, salen
, result
, size
, NULL
, 0, flags
);
343 static int time_formatter(int fmt
, char *dst
, size_t dlen
, time_t *when
)
348 case LAST_TIMEFTM_NONE
:
351 case LAST_TIMEFTM_HHMM
:
355 localtime_r(when
, &tm
);
356 if (!snprintf(dst
, dlen
, "%02d:%02d", tm
.tm_hour
, tm
.tm_min
))
360 case LAST_TIMEFTM_CTIME
:
362 char buf
[CTIME_BUFSIZ
];
365 snprintf(dst
, dlen
, "%s", buf
);
366 ret
= rtrim_whitespace((unsigned char *) dst
);
369 case LAST_TIMEFTM_ISO8601
:
370 ret
= strtime_iso(when
, ISO_TIMESTAMP_T
, dst
, dlen
);
379 * Remove trailing spaces from a string.
381 static void trim_trailing_spaces(char *s
)
387 while (p
> s
&& isspace(*--p
))
396 * Show one line of information on screen
398 static int list(const struct last_control
*ctl
, struct utmpx
*p
, time_t logout_time
, int what
)
400 time_t secs
, utmp_time
;
401 char logintime
[LAST_TIMESTAMP_LEN
];
402 char logouttime
[LAST_TIMESTAMP_LEN
];
403 char length
[LAST_TIMESTAMP_LEN
];
405 char utline
[sizeof(p
->ut_line
) + 1];
408 int mins
, hours
, days
;
410 struct last_timefmt
*fmt
;
413 * uucp and ftp have special-type entries
415 mem2strcpy(utline
, p
->ut_line
, sizeof(p
->ut_line
), sizeof(utline
));
416 if (strncmp(utline
, "ftp", 3) == 0 && isdigit(utline
[3]))
418 if (strncmp(utline
, "uucp", 4) == 0 && isdigit(utline
[4]))
422 * Is this something we want to show?
426 for (walk
= ctl
->show
; *walk
; walk
++) {
427 if (strncmp(p
->ut_user
, *walk
, sizeof(p
->ut_user
)) == 0 ||
428 strcmp(utline
, *walk
) == 0 ||
429 (strncmp(utline
, "tty", 3) == 0 &&
430 strcmp(utline
+ 3, *walk
) == 0)) break;
432 if (*walk
== NULL
) return 0;
438 fmt
= &timefmts
[ctl
->time_fmt
];
440 utmp_time
= p
->ut_tv
.tv_sec
;
443 if (ctl
->present
< utmp_time
)
445 if (0 < logout_time
&& logout_time
< ctl
->present
)
450 if (time_formatter(fmt
->in_fmt
, logintime
,
451 sizeof(logintime
), &utmp_time
) < 0)
452 errx(EXIT_FAILURE
, _("preallocation size exceeded"));
455 secs
= logout_time
- utmp_time
; /* Under strange circumstances, secs < 0 can happen */
456 mins
= (secs
/ 60) % 60;
457 hours
= (secs
/ 3600) % 24;
460 strcpy(logouttime
, "- ");
461 if (time_formatter(fmt
->out_fmt
, logouttime
+ 2,
462 sizeof(logouttime
) - 2, &logout_time
) < 0)
463 errx(EXIT_FAILURE
, _("preallocation size exceeded"));
465 if (logout_time
== currentdate
) {
466 if (ctl
->time_fmt
> LAST_TIMEFTM_SHORT
) {
467 snprintf(logouttime
, sizeof(logouttime
), " still running");
470 snprintf(logouttime
, sizeof(logouttime
), " still");
471 snprintf(length
, sizeof(length
), "running");
474 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 */
476 snprintf(length
, sizeof(length
), " (%02d:%02d)", hours
, abs(mins
)); /* mins always shown as positive (w/o minus sign!) even if secs < 0 */
477 } else if (secs
>= 0) {
478 snprintf(length
, sizeof(length
), " (%02d:%02d)", hours
, mins
);
480 snprintf(length
, sizeof(length
), " (-00:%02d)", abs(mins
)); /* mins always shown as positive (w/o minus sign!) even if secs < 0 */
485 snprintf(logouttime
, sizeof(logouttime
), "- crash");
488 snprintf(logouttime
, sizeof(logouttime
), "- down ");
491 if (ctl
->time_fmt
> LAST_TIMEFTM_SHORT
) {
492 snprintf(logouttime
, sizeof(logouttime
), " still logged in");
495 snprintf(logouttime
, sizeof(logouttime
), " still");
496 snprintf(length
, sizeof(length
), "logged in");
500 if (ctl
->time_fmt
> LAST_TIMEFTM_SHORT
) {
501 snprintf(logouttime
, sizeof(logouttime
), " gone - no logout");
503 } else if (ctl
->time_fmt
== LAST_TIMEFTM_SHORT
) {
504 snprintf(logouttime
, sizeof(logouttime
), " gone");
505 snprintf(length
, sizeof(length
), "- no logout");
508 snprintf(length
, sizeof(length
), "no logout");
523 * Look up host with DNS if needed.
526 if (ctl
->usedns
|| ctl
->useip
)
527 r
= dns_lookup(domain
, sizeof(domain
), ctl
->useip
, (int32_t*)p
->ut_addr_v6
);
529 mem2strcpy(domain
, p
->ut_host
, sizeof(p
->ut_host
), sizeof(domain
));
533 len
= snprintf(final
, sizeof(final
),
534 "%-8.*s %-12.12s %-16.*s %-*.*s %-*.*s %s\n",
535 ctl
->name_len
, p
->ut_user
, utline
,
536 ctl
->domain_len
, domain
,
537 fmt
->in_len
, fmt
->in_len
, logintime
, fmt
->out_len
, fmt
->out_len
,
540 len
= snprintf(final
, sizeof(final
),
541 "%-8.*s %-12.12s %-*.*s %-*.*s %-12.12s %s\n",
542 ctl
->name_len
, p
->ut_user
, utline
,
543 fmt
->in_len
, fmt
->in_len
, logintime
, fmt
->out_len
, fmt
->out_len
,
544 logouttime
, length
, domain
);
547 len
= snprintf(final
, sizeof(final
),
548 "%-8.*s %-12.12s %-*.*s %-*.*s %s\n",
549 ctl
->name_len
, p
->ut_user
, utline
,
550 fmt
->in_len
, fmt
->in_len
, logintime
, fmt
->out_len
, fmt
->out_len
,
553 #if defined(__GLIBC__)
554 # if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0)
555 final
[sizeof(final
)-1] = '\0';
559 trim_trailing_spaces(final
);
561 * Print out "final" string safely.
563 for (s
= final
; *s
; s
++)
564 fputc_careful(*s
, stdout
, '*');
566 if (len
< 0 || (size_t)len
>= sizeof(final
))
570 if (ctl
->maxrecs
&& ctl
->maxrecs
<= recsdone
)
577 static void __attribute__((__noreturn__
)) usage(const struct last_control
*ctl
)
580 fputs(USAGE_HEADER
, out
);
582 " %s [options] [<username>...] [<tty>...]\n"), program_invocation_short_name
);
584 fputs(USAGE_SEPARATOR
, out
);
585 fputs(_("Show a listing of last logged in users.\n"), out
);
587 fputs(USAGE_OPTIONS
, out
);
588 fputs(_(" -<number> how many lines to show\n"), out
);
589 fputs(_(" -a, --hostlast display hostnames in the last column\n"), out
);
590 fputs(_(" -d, --dns translate the IP number back into a hostname\n"), out
);
592 _(" -f, --file <file> use a specific file instead of %s\n"), ctl
->lastb
? _PATH_BTMP
: _PATH_WTMP
);
593 fputs(_(" -F, --fulltimes print full login and logout times and dates\n"), out
);
594 fputs(_(" -i, --ip display IP numbers in numbers-and-dots notation\n"), out
);
595 fputs(_(" -n, --limit <number> how many lines to show\n"), out
);
596 fputs(_(" -R, --nohostname don't display the hostname field\n"), out
);
597 fputs(_(" -s, --since <time> display the lines since the specified time\n"), out
);
598 fputs(_(" -t, --until <time> display the lines until the specified time\n"), out
);
599 fputs(_(" -p, --present <time> display who were present at the specified time\n"), out
);
600 fputs(_(" -w, --fullnames display full user and domain names\n"), out
);
601 fputs(_(" -x, --system display system shutdown entries and run level changes\n"), out
);
602 fputs(_(" --time-format <format> show timestamps in the specified <format>:\n"
603 " notime|short|full|iso\n"), out
);
605 fputs(USAGE_SEPARATOR
, out
);
606 printf(USAGE_HELP_OPTIONS(22));
607 printf(USAGE_MAN_TAIL("last(1)"));
609 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
613 static int is_phantom(const struct last_control
*ctl
, struct utmpx
*ut
)
616 char path
[sizeof(ut
->ut_line
) + 16];
619 if (ut
->ut_tv
.tv_sec
< ctl
->boot_time
.tv_sec
)
621 ut
->ut_user
[sizeof(ut
->ut_user
) - 1] = '\0';
622 pw
= getpwnam(ut
->ut_user
);
625 snprintf(path
, sizeof(path
), "/proc/%u/loginuid", ut
->ut_pid
);
626 if (access(path
, R_OK
) == 0) {
627 unsigned int loginuid
;
630 if (!(f
= fopen(path
, "r")))
632 if (fscanf(f
, "%u", &loginuid
) != 1)
635 if (!ret
&& pw
->pw_uid
!= loginuid
)
639 char utline
[sizeof(ut
->ut_line
) + 1];
641 mem2strcpy(utline
, ut
->ut_line
, sizeof(ut
->ut_line
), sizeof(utline
));
643 snprintf(path
, sizeof(path
), "/dev/%s", utline
);
646 if (pw
->pw_uid
!= st
.st_uid
)
652 static void process_wtmp_file(const struct last_control
*ctl
,
653 const char *filename
)
655 FILE *fp
; /* File pointer of wtmp file */
657 struct utmpx ut
; /* Current utmp entry */
658 struct utmplist
*ulist
= NULL
; /* All entries */
659 struct utmplist
*p
; /* Pointer into utmplist */
660 struct utmplist
*next
; /* Pointer into utmplist */
662 time_t lastboot
= 0; /* Last boottime */
663 time_t lastrch
= 0; /* Last run level change */
664 time_t lastdown
; /* Last downtime */
665 time_t begintime
; /* When wtmp begins */
666 int whydown
= 0; /* Why we went down: crash or shutdown */
668 int c
, x
; /* Scratch */
669 struct stat st
; /* To stat the [uw]tmp file */
670 int quit
= 0; /* Flag */
671 int down
= 0; /* Down flag */
676 lastdown
= 1596001948;
681 lastdate
= currentdate
= lastrch
= lastdown
;
685 * Install signal handlers
687 signal(SIGINT
, int_handler
);
688 signal(SIGQUIT
, quit_handler
);
694 if ((fp
= fopen(filename
, "r")) == NULL
)
695 err(EXIT_FAILURE
, _("cannot open %s"), filename
);
698 * Optimize the buffer size.
700 setvbuf(fp
, NULL
, _IOFBF
, UCHUNKSIZE
);
703 * Read first structure to capture the time field
705 if (uread(fp
, &ut
, NULL
, filename
) == 1)
706 begintime
= ut
.ut_tv
.tv_sec
;
708 if (fstat(fileno(fp
), &st
) != 0)
709 err(EXIT_FAILURE
, _("stat of %s failed"), filename
);
710 begintime
= st
.st_ctime
;
715 * Go to end of file minus one structure
716 * and/or initialize utmp reading code.
718 uread(fp
, NULL
, NULL
, filename
);
721 * Read struct after struct backwards from the file.
725 if (uread(fp
, &ut
, &quit
, filename
) != 1)
728 if (ctl
->since
&& ut
.ut_tv
.tv_sec
< ctl
->since
)
731 if (ctl
->until
&& ctl
->until
< ut
.ut_tv
.tv_sec
)
734 lastdate
= ut
.ut_tv
.tv_sec
;
737 quit
= list(ctl
, &ut
, ut
.ut_tv
.tv_sec
, R_NORMAL
);
742 * Set ut_type to the correct type.
744 if (strncmp(ut
.ut_line
, "~", 1) == 0) {
745 if (strncmp(ut
.ut_user
, "shutdown", 8) == 0)
746 ut
.ut_type
= SHUTDOWN_TIME
;
747 else if (strncmp(ut
.ut_user
, "reboot", 6) == 0)
748 ut
.ut_type
= BOOT_TIME
;
749 else if (strncmp(ut
.ut_user
, "runlevel", 8) == 0)
750 ut
.ut_type
= RUN_LVL
;
754 * For stupid old applications that don't fill in
758 if (ut
.ut_type
!= DEAD_PROCESS
&&
759 ut
.ut_user
[0] && ut
.ut_line
[0] &&
760 strncmp(ut
.ut_user
, "LOGIN", 5) != 0)
761 ut
.ut_type
= USER_PROCESS
;
763 * Even worse, applications that write ghost
764 * entries: ut_type set to USER_PROCESS but
767 if (ut
.ut_user
[0] == 0)
768 ut
.ut_type
= DEAD_PROCESS
;
773 if (strncmp(ut
.ut_user
, "date", 4) == 0) {
774 if (ut
.ut_line
[0] == '|')
775 ut
.ut_type
= OLD_TIME
;
776 if (ut
.ut_line
[0] == '{')
777 ut
.ut_type
= NEW_TIME
;
781 switch (ut
.ut_type
) {
784 strcpy(ut
.ut_line
, "system down");
785 quit
= list(ctl
, &ut
, lastboot
, R_NORMAL
);
787 lastdown
= lastrch
= ut
.ut_tv
.tv_sec
;
794 ut
.ut_type
== NEW_TIME
? "new time" :
796 quit
= list(ctl
, &ut
, lastdown
, R_TIMECHANGE
);
800 strcpy(ut
.ut_line
, "system boot");
801 quit
= list(ctl
, &ut
, lastdown
, R_REBOOT
);
802 lastboot
= ut
.ut_tv
.tv_sec
;
808 snprintf(ut
.ut_line
, sizeof(ut
.ut_line
), "(to lvl %c)", x
);
809 quit
= list(ctl
, &ut
, lastrch
, R_NORMAL
);
811 if (x
== '0' || x
== '6') {
812 lastdown
= ut
.ut_tv
.tv_sec
;
814 ut
.ut_type
= SHUTDOWN_TIME
;
816 lastrch
= ut
.ut_tv
.tv_sec
;
821 * This was a login - show the first matching
822 * logout record and delete all records with
826 for (p
= ulist
; p
; p
= next
) {
828 if (strncmp(p
->ut
.ut_line
, ut
.ut_line
,
829 sizeof(ut
.ut_line
)) == 0) {
832 quit
= list(ctl
, &ut
, p
->ut
.ut_tv
.tv_sec
, R_NORMAL
);
836 p
->next
->prev
= p
->prev
;
838 p
->prev
->next
= p
->next
;
845 * Not found? Then crashed, down, still
846 * logged in, or missing logout record.
851 /* Is process still alive? */
852 if (is_phantom(ctl
, &ut
))
856 quit
= list(ctl
, &ut
, lastboot
, c
);
862 * Just store the data if it is
863 * interesting enough.
865 if (ut
.ut_line
[0] == 0)
867 p
= xmalloc(sizeof(struct utmplist
));
868 memcpy(&p
->ut
, &ut
, sizeof(struct utmpx
));
882 /* ignored ut_types */
886 warnx("unrecognized ut_type: %d", ut
.ut_type
);
890 * If we saw a shutdown/reboot record we can remove
891 * the entire current ulist.
894 lastboot
= ut
.ut_tv
.tv_sec
;
895 whydown
= (ut
.ut_type
== SHUTDOWN_TIME
) ? R_DOWN
: R_CRASH
;
896 for (p
= ulist
; p
; p
= next
) {
905 if (ctl
->time_fmt
!= LAST_TIMEFTM_NONE
) {
906 struct last_timefmt
*fmt
;
907 char timestr
[LAST_TIMESTAMP_LEN
];
908 char *tmp
= xstrdup(filename
);
910 fmt
= &timefmts
[ctl
->time_fmt
];
911 if (time_formatter(fmt
->in_fmt
, timestr
,
912 sizeof(timestr
), &begintime
) < 0)
913 errx(EXIT_FAILURE
, _("preallocation size exceeded"));
914 printf(_("\n%s begins %s\n"), basename(tmp
), timestr
);
920 for (p
= ulist
; p
; p
= next
) {
929 int LLVMFuzzerTestOneInput(const uint8_t *data
, size_t size
) {
930 struct last_control ctl
= {
932 .name_len
= LAST_LOGIN_LEN
,
933 .time_fmt
= LAST_TIMEFTM_SHORT
,
934 .domain_len
= LAST_DOMAIN_LEN
,
936 .tv_sec
= 1595978419,
940 char name
[] = "/tmp/test-last-fuzz.XXXXXX";
943 fd
= mkstemp_cloexec(name
);
945 err(EXIT_FAILURE
, "mkstemp() failed");
946 if (write_all(fd
, data
, size
) != 0)
947 err(EXIT_FAILURE
, "write() failed");
949 process_wtmp_file(&ctl
, name
);
957 int main(int argc
, char **argv
)
959 struct last_control ctl
= {
961 .name_len
= LAST_LOGIN_LEN
,
962 .time_fmt
= LAST_TIMEFTM_SHORT
,
963 .domain_len
= LAST_DOMAIN_LEN
966 size_t i
, nfiles
= 0;
971 OPT_TIME_FORMAT
= CHAR_MAX
+ 1
973 static const struct option long_opts
[] = {
974 { "limit", required_argument
, NULL
, 'n' },
975 { "help", no_argument
, NULL
, 'h' },
976 { "file", required_argument
, NULL
, 'f' },
977 { "nohostname", no_argument
, NULL
, 'R' },
978 { "version", no_argument
, NULL
, 'V' },
979 { "hostlast", no_argument
, NULL
, 'a' },
980 { "since", required_argument
, NULL
, 's' },
981 { "until", required_argument
, NULL
, 't' },
982 { "present", required_argument
, NULL
, 'p' },
983 { "system", no_argument
, NULL
, 'x' },
984 { "dns", no_argument
, NULL
, 'd' },
985 { "ip", no_argument
, NULL
, 'i' },
986 { "fulltimes", no_argument
, NULL
, 'F' },
987 { "fullnames", no_argument
, NULL
, 'w' },
988 { "time-format", required_argument
, NULL
, OPT_TIME_FORMAT
},
991 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
992 { 'F', OPT_TIME_FORMAT
}, /* fulltime, time-format */
995 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
997 setlocale(LC_ALL
, "");
998 bindtextdomain(PACKAGE
, LOCALEDIR
);
1000 close_stdout_atexit();
1002 * Which file do we want to read?
1004 ctl
.lastb
= strcmp(program_invocation_short_name
, "lastb") == 0 ? 1 : 0;
1005 while ((c
= getopt_long(argc
, argv
,
1006 "hVf:n:RxadFit:p:s:0123456789w", long_opts
, NULL
)) != -1) {
1008 err_exclusive_options(c
, long_opts
, excl
, excl_st
);
1015 print_version(EXIT_SUCCESS
);
1023 ctl
.maxrecs
= strtos32_or_err(optarg
, _("failed to parse number"));
1027 files
= xmalloc(sizeof(char *) * argc
);
1028 files
[nfiles
++] = xstrdup(optarg
);
1040 ctl
.time_fmt
= LAST_TIMEFTM_CTIME
;
1043 if (parse_timestamp(optarg
, &p
) < 0)
1044 errx(EXIT_FAILURE
, _("invalid time value \"%s\""), optarg
);
1045 ctl
.present
= (time_t) (p
/ 1000000);
1048 if (parse_timestamp(optarg
, &p
) < 0)
1049 errx(EXIT_FAILURE
, _("invalid time value \"%s\""), optarg
);
1050 ctl
.since
= (time_t) (p
/ 1000000);
1053 if (parse_timestamp(optarg
, &p
) < 0)
1054 errx(EXIT_FAILURE
, _("invalid time value \"%s\""), optarg
);
1055 ctl
.until
= (time_t) (p
/ 1000000);
1058 if (ctl
.name_len
< sizeof(((struct utmpx
*) 0)->ut_user
))
1059 ctl
.name_len
= sizeof(((struct utmpx
*) 0)->ut_user
);
1060 if (ctl
.domain_len
< sizeof(((struct utmpx
*) 0)->ut_host
))
1061 ctl
.domain_len
= sizeof(((struct utmpx
*) 0)->ut_host
);
1063 case '0': case '1': case '2': case '3': case '4':
1064 case '5': case '6': case '7': case '8': case '9':
1065 ctl
.maxrecs
= 10 * ctl
.maxrecs
+ c
- '0';
1067 case OPT_TIME_FORMAT
:
1068 ctl
.time_fmt
= which_time_format(optarg
);
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
;