last.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * last implementation for busybox
  4. *
  5. * Copyright (C) 2003-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. //config:config LAST
  10. //config: bool "last (6.2 kb)"
  11. //config: default y
  12. //config: depends on FEATURE_WTMP
  13. //config: help
  14. //config: 'last' displays a list of the last users that logged into the system.
  15. //config:
  16. //config:config FEATURE_LAST_FANCY
  17. //config: bool "Output extra information"
  18. //config: default y
  19. //config: depends on LAST
  20. //config: help
  21. //config: 'last' displays detailed information about the last users that
  22. //config: logged into the system (mimics sysvinit last). +900 bytes.
  23. //applet:IF_LAST(APPLET(last, BB_DIR_USR_BIN, BB_SUID_DROP))
  24. //kbuild:ifeq ($(CONFIG_FEATURE_LAST_FANCY),y)
  25. //kbuild:lib-$(CONFIG_FEATURE_LAST_FANCY) += last_fancy.o
  26. //kbuild:else
  27. //kbuild:lib-$(CONFIG_LAST) += last.o
  28. //kbuild:endif
  29. //usage:#define last_trivial_usage
  30. //usage: ""IF_FEATURE_LAST_FANCY("[-HW] [-f FILE]")
  31. //usage:#define last_full_usage "\n\n"
  32. //usage: "Show listing of the last users that logged into the system"
  33. //usage: IF_FEATURE_LAST_FANCY( "\n"
  34. /* //usage: "\n -H Show header line" */
  35. //usage: "\n -W Display with no host column truncation"
  36. //usage: "\n -f FILE Read from FILE instead of /var/log/wtmp"
  37. //usage: )
  38. #include "libbb.h"
  39. /* NB: ut_name and ut_user are the same field, use only one name (ut_user)
  40. * to reduce confusion */
  41. #ifndef SHUTDOWN_TIME
  42. # define SHUTDOWN_TIME 254
  43. #endif
  44. /* Grr... utmp char[] members do not have to be nul-terminated.
  45. * Do what we can while still keeping this reasonably small.
  46. * Note: We are assuming the ut_id[] size is fixed at 4. */
  47. #if defined UT_LINESIZE \
  48. && ((UT_LINESIZE != 32) || (UT_NAMESIZE != 32) || (UT_HOSTSIZE != 256))
  49. #error struct utmpx member char[] size(s) have changed!
  50. #elif defined __UT_LINESIZE \
  51. && ((__UT_LINESIZE != 32) || (__UT_NAMESIZE != 32) || (__UT_HOSTSIZE != 256))
  52. /* __UT_NAMESIZE was checked with 64 above, but glibc-2.11 definitely uses 32! */
  53. #error struct utmpx member char[] size(s) have changed!
  54. #endif
  55. #if EMPTY != 0 || RUN_LVL != 1 || BOOT_TIME != 2 || NEW_TIME != 3 || \
  56. OLD_TIME != 4
  57. #error Values for the ut_type field of struct utmpx changed
  58. #endif
  59. int last_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  60. int last_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  61. {
  62. struct utmpx ut;
  63. int n, file = STDIN_FILENO;
  64. time_t t_tmp;
  65. off_t pos;
  66. static const char _ut_usr[] ALIGN1 =
  67. "runlevel\0" "reboot\0" "shutdown\0";
  68. static const char _ut_lin[] ALIGN1 =
  69. "~\0" "{\0" "|\0" /* "LOGIN\0" "date\0" */;
  70. enum {
  71. TYPE_RUN_LVL = RUN_LVL, /* 1 */
  72. TYPE_BOOT_TIME = BOOT_TIME, /* 2 */
  73. TYPE_SHUTDOWN_TIME = SHUTDOWN_TIME
  74. };
  75. enum {
  76. _TILDE = EMPTY, /* 0 */
  77. TYPE_NEW_TIME, /* NEW_TIME, 3 */
  78. TYPE_OLD_TIME /* OLD_TIME, 4 */
  79. };
  80. if (argv[1]) {
  81. bb_show_usage();
  82. }
  83. file = xopen(bb_path_wtmp_file, O_RDONLY);
  84. printf("%-10s %-14s %-18s %-12.12s %s\n",
  85. "USER", "TTY", "HOST", "LOGIN", "TIME");
  86. /* yikes. We reverse over the file and that is a not too elegant way */
  87. pos = xlseek(file, 0, SEEK_END);
  88. pos = lseek(file, pos - sizeof(ut), SEEK_SET);
  89. while ((n = full_read(file, &ut, sizeof(ut))) > 0) {
  90. if (n != sizeof(ut)) {
  91. bb_perror_msg_and_die("short read");
  92. }
  93. n = index_in_strings(_ut_lin, ut.ut_line);
  94. if (n == _TILDE) { /* '~' */
  95. #if 1
  96. /* do we really need to be cautious here? */
  97. n = index_in_strings(_ut_usr, ut.ut_user);
  98. if (++n > 0)
  99. ut.ut_type = n != 3 ? n : SHUTDOWN_TIME;
  100. #else
  101. if (is_prefixed_with(ut.ut_user, "shutdown"))
  102. ut.ut_type = SHUTDOWN_TIME;
  103. else if (is_prefixed_with(ut.ut_user, "reboot"))
  104. ut.ut_type = BOOT_TIME;
  105. else if (is_prefixed_with(ut.ut_user, "runlevel"))
  106. ut.ut_type = RUN_LVL;
  107. #endif
  108. } else {
  109. if (ut.ut_user[0] == '\0' || strcmp(ut.ut_user, "LOGIN") == 0) {
  110. /* Don't bother. This means we can't find how long
  111. * someone was logged in for. Oh well. */
  112. goto next;
  113. }
  114. if (ut.ut_type != DEAD_PROCESS
  115. && ut.ut_user[0]
  116. && ut.ut_line[0]
  117. ) {
  118. ut.ut_type = USER_PROCESS;
  119. }
  120. if (strcmp(ut.ut_user, "date") == 0) {
  121. if (n == TYPE_OLD_TIME) { /* '|' */
  122. ut.ut_type = OLD_TIME;
  123. }
  124. if (n == TYPE_NEW_TIME) { /* '{' */
  125. ut.ut_type = NEW_TIME;
  126. }
  127. }
  128. }
  129. if (ut.ut_type != USER_PROCESS) {
  130. switch (ut.ut_type) {
  131. case OLD_TIME:
  132. case NEW_TIME:
  133. case RUN_LVL:
  134. case SHUTDOWN_TIME:
  135. goto next;
  136. case BOOT_TIME:
  137. strcpy(ut.ut_line, "system boot");
  138. }
  139. }
  140. /* manpages say ut_tv.tv_sec *is* time_t,
  141. * but some systems have it wrong */
  142. t_tmp = (time_t)ut.ut_tv.tv_sec;
  143. printf("%-10s %-14s %-18s %-12.12s\n",
  144. ut.ut_user, ut.ut_line, ut.ut_host, ctime(&t_tmp) + 4);
  145. next:
  146. pos -= sizeof(ut);
  147. if (pos <= 0)
  148. break; /* done. */
  149. xlseek(file, pos, SEEK_SET);
  150. }
  151. fflush_stdout_and_exit(EXIT_SUCCESS);
  152. }