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", ""
25 #if !defined __UT_LINESIZE && defined UT_LINESIZE
26 # define __UT_LINESIZE UT_LINESIZE
39 LAST_OPT_W = (1 << 0), /* -W wide */
40 LAST_OPT_f = (1 << 1), /* -f input file */
41 LAST_OPT_H = (1 << 2), /* -H header */
44 #define show_wide (option_mask32 & LAST_OPT_W)
46 static void show_entry(struct utmpx *ut, int state, time_t dur_secs)
48 unsigned days, hours, mins;
49 char duration[sizeof("(%u+02:02)") + sizeof(int)*3];
52 const char *logout_str;
53 const char *duration_str;
56 /* manpages say ut_tv.tv_sec *is* time_t,
57 * but some systems have it wrong */
58 tmp = ut->ut_tv.tv_sec;
59 safe_strncpy(login_time, ctime(&tmp), 17);
61 snprintf(logout_time, 8, "- %s", ctime(&tmp) + 11);
63 dur_secs = MAX(dur_secs - (time_t)ut->ut_tv.tv_sec, (time_t)0);
64 /* unsigned int is easier to divide than time_t (which may be signed long) */
66 days = mins / (24*60);
67 mins = mins % (24*60);
72 sprintf(duration, "(%u+%02u:%02u)", days, hours, mins);
74 // sprintf(duration, " (%02u:%02u)", hours, mins);
77 logout_str = logout_time;
78 duration_str = duration;
83 logout_str = " still";
84 duration_str = "logged in";
87 logout_str = "- down ";
92 logout_str = "- crash";
96 duration_str = "- no logout";
100 printf(HEADER_FORMAT,
103 show_wide ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN,
104 show_wide ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN,
111 static int get_ut_type(struct utmpx *ut)
113 if (ut->ut_line[0] == '~') {
114 if (strcmp(ut->ut_user, "shutdown") == 0) {
115 return SHUTDOWN_TIME;
117 if (strcmp(ut->ut_user, "reboot") == 0) {
120 if (strcmp(ut->ut_user, "runlevel") == 0) {
126 if (ut->ut_user[0] == 0) {
130 if ((ut->ut_type != DEAD_PROCESS)
131 && (strcmp(ut->ut_user, "LOGIN") != 0)
135 ut->ut_type = USER_PROCESS;
138 if (strcmp(ut->ut_user, "date") == 0) {
139 if (ut->ut_line[0] == '|') {
142 if (ut->ut_line[0] == '{') {
149 static int is_runlevel_shutdown(struct utmpx *ut)
151 if (((ut->ut_pid & 255) == '0') || ((ut->ut_pid & 255) == '6')) {
158 int last_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
159 int last_main(int argc UNUSED_PARAM, char **argv)
162 const char *filename = _PATH_WTMP;
172 /*opt =*/ getopt32(argv, "Wf:" /* "H" */, &filename);
173 #ifdef BUT_UTIL_LINUX_LAST_HAS_NO_SUCH_OPT
174 if (opt & LAST_OPT_H) {
175 /* Print header line */
176 if (opt & LAST_OPT_W) {
177 printf(HEADER_FORMAT, HEADER_LINE_WIDE);
179 printf(HEADER_FORMAT, HEADER_LINE);
184 file = xopen(filename, O_RDONLY);
186 /* in case the file is empty... */
189 start_time = st.st_ctime;
194 boot_down = NORMAL; /* 0 */
197 /* get file size, rounding down to last full record */
198 pos = xlseek(file, 0, SEEK_END) / sizeof(ut) * sizeof(ut);
200 pos -= (off_t)sizeof(ut);
202 /* Beyond the beginning of the file boundary =>
203 * the whole file has been read. */
206 xlseek(file, pos, SEEK_SET);
207 xread(file, &ut, sizeof(ut));
208 /* rewritten by each record, eventially will have
209 * first record's ut_tv.tv_sec: */
210 start_time = ut.ut_tv.tv_sec;
212 switch (get_ut_type(&ut)) {
214 down_time = ut.ut_tv.tv_sec;
219 if (is_runlevel_shutdown(&ut)) {
220 down_time = ut.ut_tv.tv_sec;
226 strcpy(ut.ut_line, "system boot");
227 show_entry(&ut, REBOOT, down_time);
232 if (!ut.ut_line[0]) {
236 llist_add_to(&zlist, xmemdup(&ut, sizeof(ut)));
241 if (!ut.ut_line[0]) {
248 for (el = zlist; el; el = next) {
249 struct utmpx *up = (struct utmpx *)el->data;
251 if (strncmp(up->ut_line, ut.ut_line, __UT_LINESIZE) == 0) {
253 show_entry(&ut, NORMAL, up->ut_tv.tv_sec);
256 llist_unlink(&zlist, el);
264 int state = boot_down;
266 if (boot_time == 0) {
268 /* Check if the process is alive */
270 && (kill(ut.ut_pid, 0) != 0)
271 && (errno == ESRCH)) {
275 show_entry(&ut, state, boot_time);
278 llist_add_to(&zlist, xmemdup(&ut, sizeof(ut)));
284 boot_time = ut.ut_tv.tv_sec;
285 llist_free(zlist, free);
291 if (ENABLE_FEATURE_CLEAN_UP) {
292 llist_free(zlist, free);
295 printf("\nwtmp begins %s", ctime(&start_time));
297 if (ENABLE_FEATURE_CLEAN_UP)
299 fflush_stdout_and_exit(EXIT_SUCCESS);