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 the GPLv2 or later, see the file LICENSE in this tarball.
14 # define SHUTDOWN_TIME 254
17 #define HEADER_FORMAT "%-8.8s %-12.12s %-*.*s %-16.16s %-7.7s %s\n"
18 #define HEADER_LINE "USER", "TTY", \
19 INET_ADDRSTRLEN, INET_ADDRSTRLEN, "HOST", "LOGIN", " TIME", ""
20 #define HEADER_LINE_WIDE "USER", "TTY", \
21 INET6_ADDRSTRLEN, INET6_ADDRSTRLEN, "HOST", "LOGIN", " TIME", ""
33 LAST_OPT_W = (1 << 0), /* -W wide */
34 LAST_OPT_f = (1 << 1), /* -f input file */
35 LAST_OPT_H = (1 << 2), /* -H header */
38 #define show_wide (option_mask32 & LAST_OPT_W)
40 static void show_entry(struct utmp *ut, int state, time_t dur_secs)
42 unsigned days, hours, mins;
46 const char *logout_str;
47 const char *duration_str;
49 safe_strncpy(login_time, ctime(&(ut->ut_tv.tv_sec)), 17);
50 snprintf(logout_time, 8, "- %s", ctime(&dur_secs) + 11);
52 dur_secs = MAX(dur_secs - (time_t)ut->ut_tv.tv_sec, (time_t)0);
53 /* unsigned int is easier to divide than time_t (which may be signed long) */
55 days = mins / (24*60);
56 mins = mins % (24*60);
61 sprintf(duration, "(%u+%02u:%02u)", days, hours, mins);
63 // sprintf(duration, " (%02u:%02u)", hours, mins);
66 logout_str = logout_time;
67 duration_str = duration;
72 logout_str = " still";
73 duration_str = "logged in";
76 logout_str = "- down ";
81 logout_str = "- crash";
85 duration_str = "- no logout";
92 show_wide ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN,
93 show_wide ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN,
100 static int get_ut_type(struct utmp *ut)
102 if (ut->ut_line[0] == '~') {
103 if (strcmp(ut->ut_user, "shutdown") == 0) {
104 return SHUTDOWN_TIME;
106 if (strcmp(ut->ut_user, "reboot") == 0) {
109 if (strcmp(ut->ut_user, "runlevel") == 0) {
115 if (ut->ut_user[0] == 0) {
119 if ((ut->ut_type != DEAD_PROCESS)
120 && (strcmp(ut->ut_user, "LOGIN") != 0)
124 ut->ut_type = USER_PROCESS;
127 if (strcmp(ut->ut_user, "date") == 0) {
128 if (ut->ut_line[0] == '|') {
131 if (ut->ut_line[0] == '{') {
138 static int is_runlevel_shutdown(struct utmp *ut)
140 if (((ut->ut_pid & 255) == '0') || ((ut->ut_pid & 255) == '6')) {
147 int last_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
148 int last_main(int argc ATTRIBUTE_UNUSED, char **argv)
151 const char *filename = _PATH_WTMP;
162 opt = getopt32(argv, "Wf:" /* "H" */, &filename);
163 #ifdef BUT_UTIL_LINUX_LAST_HAS_NO_SUCH_OPT
164 if (opt & LAST_OPT_H) {
165 /* Print header line */
166 if (opt & LAST_OPT_W) {
167 printf(HEADER_FORMAT, HEADER_LINE_WIDE);
169 printf(HEADER_FORMAT, HEADER_LINE);
174 file = xopen(filename, O_RDONLY);
176 /* in case the file is empty... */
179 start_time = st.st_ctime;
184 boot_down = NORMAL; /* 0 */
187 /* get file size, rounding down to last full record */
188 pos = xlseek(file, 0, SEEK_END) / sizeof(ut) * sizeof(ut);
190 pos -= (off_t)sizeof(ut);
192 /* Beyond the beginning of the file boundary =>
193 * the whole file has been read. */
196 xlseek(file, pos, SEEK_SET);
197 xread(file, &ut, sizeof(ut));
198 /* rewritten by each record, eventially will have
199 * first record's ut_tv.tv_sec: */
200 start_time = ut.ut_tv.tv_sec;
202 switch (get_ut_type(&ut)) {
204 down_time = ut.ut_tv.tv_sec;
209 if (is_runlevel_shutdown(&ut)) {
210 down_time = ut.ut_tv.tv_sec;
216 strcpy(ut.ut_line, "system boot");
217 show_entry(&ut, REBOOT, down_time);
222 if (!ut.ut_line[0]) {
226 llist_add_to(&zlist, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
231 if (!ut.ut_line[0]) {
238 for (el = zlist; el; el = next) {
239 struct utmp *up = (struct utmp *)el->data;
241 if (strncmp(up->ut_line, ut.ut_line, UT_LINESIZE) == 0) {
243 show_entry(&ut, NORMAL, up->ut_tv.tv_sec);
246 llist_unlink(&zlist, el);
254 int state = boot_down;
256 if (boot_time == 0) {
258 /* Check if the process is alive */
260 && (kill(ut.ut_pid, 0) != 0)
261 && (errno == ESRCH)) {
265 show_entry(&ut, state, boot_time);
268 llist_add_to(&zlist, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
274 boot_time = ut.ut_tv.tv_sec;
275 llist_free(zlist, free);
281 if (ENABLE_FEATURE_CLEAN_UP) {
282 llist_free(zlist, free);
285 printf("\nwtmp begins %s", ctime(&start_time));
287 if (ENABLE_FEATURE_CLEAN_UP)
289 fflush_stdout_and_exit(EXIT_SUCCESS);