1 /* vi: set sw=4 ts=4: */
3 * (sysvinit like) last implementation
5 * Copyright (C) 2008 by Patricia Muscalu <patricia.muscalu@axis.com>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
12 /* NB: ut_name and ut_user are the same field, use only one name (ut_user)
13 * to reduce confusion */
16 # define SHUTDOWN_TIME 254
19 #define HEADER_FORMAT "%-8.8s %-12.12s %-*.*s %-16.16s %-7.7s %s\n"
20 #define HEADER_LINE "USER", "TTY", \
21 INET_ADDRSTRLEN, INET_ADDRSTRLEN, "HOST", "LOGIN", " TIME", ""
22 #define HEADER_LINE_WIDE "USER", "TTY", \
23 INET6_ADDRSTRLEN, INET6_ADDRSTRLEN, "HOST", "LOGIN", " TIME", ""
35 LAST_OPT_W = (1 << 0), /* -W wide */
36 LAST_OPT_f = (1 << 1), /* -f input file */
37 LAST_OPT_H = (1 << 2), /* -H header */
40 #define show_wide (option_mask32 & LAST_OPT_W)
42 static void show_entry(struct utmp *ut, int state, time_t dur_secs)
44 unsigned days, hours, mins;
48 const char *logout_str;
49 const char *duration_str;
52 /* manpages say ut_tv.tv_sec *is* time_t,
53 * but some systems have it wrong */
54 tmp = ut->ut_tv.tv_sec;
55 safe_strncpy(login_time, ctime(&tmp), 17);
56 snprintf(logout_time, 8, "- %s", ctime(&dur_secs) + 11);
58 dur_secs = MAX(dur_secs - (time_t)ut->ut_tv.tv_sec, (time_t)0);
59 /* unsigned int is easier to divide than time_t (which may be signed long) */
61 days = mins / (24*60);
62 mins = mins % (24*60);
67 sprintf(duration, "(%u+%02u:%02u)", days, hours, mins);
69 // sprintf(duration, " (%02u:%02u)", hours, mins);
72 logout_str = logout_time;
73 duration_str = duration;
78 logout_str = " still";
79 duration_str = "logged in";
82 logout_str = "- down ";
87 logout_str = "- crash";
91 duration_str = "- no logout";
98 show_wide ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN,
99 show_wide ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN,
106 static int get_ut_type(struct utmp *ut)
108 if (ut->ut_line[0] == '~') {
109 if (strcmp(ut->ut_user, "shutdown") == 0) {
110 return SHUTDOWN_TIME;
112 if (strcmp(ut->ut_user, "reboot") == 0) {
115 if (strcmp(ut->ut_user, "runlevel") == 0) {
121 if (ut->ut_user[0] == 0) {
125 if ((ut->ut_type != DEAD_PROCESS)
126 && (strcmp(ut->ut_user, "LOGIN") != 0)
130 ut->ut_type = USER_PROCESS;
133 if (strcmp(ut->ut_user, "date") == 0) {
134 if (ut->ut_line[0] == '|') {
137 if (ut->ut_line[0] == '{') {
144 static int is_runlevel_shutdown(struct utmp *ut)
146 if (((ut->ut_pid & 255) == '0') || ((ut->ut_pid & 255) == '6')) {
153 int last_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
154 int last_main(int argc UNUSED_PARAM, char **argv)
157 const char *filename = _PATH_WTMP;
168 opt = getopt32(argv, "Wf:" /* "H" */, &filename);
169 #ifdef BUT_UTIL_LINUX_LAST_HAS_NO_SUCH_OPT
170 if (opt & LAST_OPT_H) {
171 /* Print header line */
172 if (opt & LAST_OPT_W) {
173 printf(HEADER_FORMAT, HEADER_LINE_WIDE);
175 printf(HEADER_FORMAT, HEADER_LINE);
180 file = xopen(filename, O_RDONLY);
182 /* in case the file is empty... */
185 start_time = st.st_ctime;
190 boot_down = NORMAL; /* 0 */
193 /* get file size, rounding down to last full record */
194 pos = xlseek(file, 0, SEEK_END) / sizeof(ut) * sizeof(ut);
196 pos -= (off_t)sizeof(ut);
198 /* Beyond the beginning of the file boundary =>
199 * the whole file has been read. */
202 xlseek(file, pos, SEEK_SET);
203 xread(file, &ut, sizeof(ut));
204 /* rewritten by each record, eventially will have
205 * first record's ut_tv.tv_sec: */
206 start_time = ut.ut_tv.tv_sec;
208 switch (get_ut_type(&ut)) {
210 down_time = ut.ut_tv.tv_sec;
215 if (is_runlevel_shutdown(&ut)) {
216 down_time = ut.ut_tv.tv_sec;
222 strcpy(ut.ut_line, "system boot");
223 show_entry(&ut, REBOOT, down_time);
228 if (!ut.ut_line[0]) {
232 llist_add_to(&zlist, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
237 if (!ut.ut_line[0]) {
244 for (el = zlist; el; el = next) {
245 struct utmp *up = (struct utmp *)el->data;
247 if (strncmp(up->ut_line, ut.ut_line, UT_LINESIZE) == 0) {
249 show_entry(&ut, NORMAL, up->ut_tv.tv_sec);
252 llist_unlink(&zlist, el);
260 int state = boot_down;
262 if (boot_time == 0) {
264 /* Check if the process is alive */
266 && (kill(ut.ut_pid, 0) != 0)
267 && (errno == ESRCH)) {
271 show_entry(&ut, state, boot_time);
274 llist_add_to(&zlist, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
280 boot_time = ut.ut_tv.tv_sec;
281 llist_free(zlist, free);
287 if (ENABLE_FEATURE_CLEAN_UP) {
288 llist_free(zlist, free);
291 printf("\nwtmp begins %s", ctime(&start_time));
293 if (ENABLE_FEATURE_CLEAN_UP)
295 fflush_stdout_and_exit(EXIT_SUCCESS);