last.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "libbb.h"
  10. #include <utmp.h>
  11. /* NB: ut_name and ut_user are the same field, use only one name (ut_user)
  12. * to reduce confusion */
  13. #ifndef SHUTDOWN_TIME
  14. # define SHUTDOWN_TIME 254
  15. #endif
  16. /* Grr... utmp char[] members do not have to be nul-terminated.
  17. * Do what we can while still keeping this reasonably small.
  18. * Note: We are assuming the ut_id[] size is fixed at 4. */
  19. #if defined UT_LINESIZE \
  20. && ((UT_LINESIZE != 32) || (UT_NAMESIZE != 32) || (UT_HOSTSIZE != 256))
  21. #error struct utmp member char[] size(s) have changed!
  22. #elif defined __UT_LINESIZE \
  23. && ((__UT_LINESIZE != 32) || (__UT_NAMESIZE != 64) || (__UT_HOSTSIZE != 256))
  24. #error struct utmp member char[] size(s) have changed!
  25. #endif
  26. #if EMPTY != 0 || RUN_LVL != 1 || BOOT_TIME != 2 || NEW_TIME != 3 || \
  27. OLD_TIME != 4
  28. #error Values for the ut_type field of struct utmp changed
  29. #endif
  30. int last_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  31. int last_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  32. {
  33. struct utmp ut;
  34. int n, file = STDIN_FILENO;
  35. time_t t_tmp;
  36. off_t pos;
  37. static const char _ut_usr[] ALIGN1 =
  38. "runlevel\0" "reboot\0" "shutdown\0";
  39. static const char _ut_lin[] ALIGN1 =
  40. "~\0" "{\0" "|\0" /* "LOGIN\0" "date\0" */;
  41. enum {
  42. TYPE_RUN_LVL = RUN_LVL, /* 1 */
  43. TYPE_BOOT_TIME = BOOT_TIME, /* 2 */
  44. TYPE_SHUTDOWN_TIME = SHUTDOWN_TIME
  45. };
  46. enum {
  47. _TILDE = EMPTY, /* 0 */
  48. TYPE_NEW_TIME, /* NEW_TIME, 3 */
  49. TYPE_OLD_TIME /* OLD_TIME, 4 */
  50. };
  51. if (argv[1]) {
  52. bb_show_usage();
  53. }
  54. file = xopen(bb_path_wtmp_file, O_RDONLY);
  55. printf("%-10s %-14s %-18s %-12.12s %s\n",
  56. "USER", "TTY", "HOST", "LOGIN", "TIME");
  57. /* yikes. We reverse over the file and that is a not too elegant way */
  58. pos = xlseek(file, 0, SEEK_END);
  59. pos = lseek(file, pos - sizeof(ut), SEEK_SET);
  60. while ((n = full_read(file, &ut, sizeof(ut))) > 0) {
  61. if (n != sizeof(ut)) {
  62. bb_perror_msg_and_die("short read");
  63. }
  64. n = index_in_strings(_ut_lin, ut.ut_line);
  65. if (n == _TILDE) { /* '~' */
  66. #if 1
  67. /* do we really need to be cautious here? */
  68. n = index_in_strings(_ut_usr, ut.ut_user);
  69. if (++n > 0)
  70. ut.ut_type = n != 3 ? n : SHUTDOWN_TIME;
  71. #else
  72. if (strncmp(ut.ut_user, "shutdown", 8) == 0)
  73. ut.ut_type = SHUTDOWN_TIME;
  74. else if (strncmp(ut.ut_user, "reboot", 6) == 0)
  75. ut.ut_type = BOOT_TIME;
  76. else if (strncmp(ut.ut_user, "runlevel", 8) == 0)
  77. ut.ut_type = RUN_LVL;
  78. #endif
  79. } else {
  80. if (ut.ut_user[0] == '\0' || strcmp(ut.ut_user, "LOGIN") == 0) {
  81. /* Don't bother. This means we can't find how long
  82. * someone was logged in for. Oh well. */
  83. goto next;
  84. }
  85. if (ut.ut_type != DEAD_PROCESS
  86. && ut.ut_user[0]
  87. && ut.ut_line[0]
  88. ) {
  89. ut.ut_type = USER_PROCESS;
  90. }
  91. if (strcmp(ut.ut_user, "date") == 0) {
  92. if (n == TYPE_OLD_TIME) { /* '|' */
  93. ut.ut_type = OLD_TIME;
  94. }
  95. if (n == TYPE_NEW_TIME) { /* '{' */
  96. ut.ut_type = NEW_TIME;
  97. }
  98. }
  99. }
  100. if (ut.ut_type != USER_PROCESS) {
  101. switch (ut.ut_type) {
  102. case OLD_TIME:
  103. case NEW_TIME:
  104. case RUN_LVL:
  105. case SHUTDOWN_TIME:
  106. goto next;
  107. case BOOT_TIME:
  108. strcpy(ut.ut_line, "system boot");
  109. }
  110. }
  111. /* manpages say ut_tv.tv_sec *is* time_t,
  112. * but some systems have it wrong */
  113. t_tmp = (time_t)ut.ut_tv.tv_sec;
  114. printf("%-10s %-14s %-18s %-12.12s\n",
  115. ut.ut_user, ut.ut_line, ut.ut_host, ctime(&t_tmp) + 4);
  116. next:
  117. pos -= sizeof(ut);
  118. if (pos <= 0)
  119. break; /* done. */
  120. xlseek(file, pos, SEEK_SET);
  121. }
  122. fflush_stdout_and_exit(EXIT_SUCCESS);
  123. }