uptime.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini uptime implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. /* 2011 Pere Orga <gotrunks@gmail.com>
  10. *
  11. * Added FEATURE_UPTIME_UTMP_SUPPORT flag.
  12. */
  13. //config:config UPTIME
  14. //config: bool "uptime (3.7 kb)"
  15. //config: default y
  16. //config: help
  17. //config: uptime gives a one line display of the current time, how long
  18. //config: the system has been running, how many users are currently logged
  19. //config: on, and the system load averages for the past 1, 5, and 15 minutes.
  20. //config:
  21. //config:config FEATURE_UPTIME_UTMP_SUPPORT
  22. //config: bool "Show the number of users"
  23. //config: default y
  24. //config: depends on UPTIME && FEATURE_UTMP
  25. //config: help
  26. //config: Display the number of users currently logged on.
  27. //applet:IF_UPTIME(APPLET_NOEXEC(uptime, uptime, BB_DIR_USR_BIN, BB_SUID_DROP, uptime))
  28. //kbuild:lib-$(CONFIG_UPTIME) += uptime.o
  29. //usage:#define uptime_trivial_usage
  30. //usage: ""
  31. //usage:#define uptime_full_usage "\n\n"
  32. //usage: "Display the time since the last boot"
  33. //usage:
  34. //usage:#define uptime_example_usage
  35. //usage: "$ uptime\n"
  36. //usage: " 1:55pm up 2:30, load average: 0.09, 0.04, 0.00\n"
  37. #include "libbb.h"
  38. #ifdef __linux__
  39. # include <sys/sysinfo.h>
  40. #endif
  41. #ifndef FSHIFT
  42. # define FSHIFT 16 /* nr of bits of precision */
  43. #endif
  44. #define FIXED_1 (1 << FSHIFT) /* 1.0 as fixed-point */
  45. #define LOAD_INT(x) (unsigned)((x) >> FSHIFT)
  46. #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1 - 1)) * 100)
  47. int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  48. int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  49. {
  50. unsigned updays, uphours, upminutes;
  51. unsigned opts;
  52. struct sysinfo info;
  53. struct tm *current_time;
  54. time_t current_secs;
  55. opts = getopt32(argv, "s");
  56. time(&current_secs);
  57. sysinfo(&info);
  58. if (opts) // -s
  59. current_secs -= info.uptime;
  60. current_time = localtime(&current_secs);
  61. if (opts) { // -s
  62. printf("%04u-%02u-%02u %02u:%02u:%02u\n",
  63. current_time->tm_year + 1900, current_time->tm_mon + 1, current_time->tm_mday,
  64. current_time->tm_hour, current_time->tm_min, current_time->tm_sec
  65. );
  66. /* The above way of calculating boot time is wobbly,
  67. * info.uptime has only 1 second precision, which makes
  68. * "uptime -s" wander +- one second.
  69. * /proc/uptime may be better, it has 0.01s precision.
  70. */
  71. return EXIT_SUCCESS;
  72. }
  73. printf(" %02u:%02u:%02u up ",
  74. current_time->tm_hour, current_time->tm_min, current_time->tm_sec
  75. );
  76. updays = (unsigned) info.uptime / (unsigned)(60*60*24);
  77. if (updays != 0)
  78. printf("%u day%s, ", updays, (updays != 1) ? "s" : "");
  79. upminutes = (unsigned) info.uptime / (unsigned)60;
  80. uphours = (upminutes / (unsigned)60) % (unsigned)24;
  81. upminutes %= 60;
  82. if (uphours != 0)
  83. printf("%2u:%02u", uphours, upminutes);
  84. else
  85. printf("%u min", upminutes);
  86. #if ENABLE_FEATURE_UPTIME_UTMP_SUPPORT
  87. {
  88. struct utmpx *ut;
  89. unsigned users = 0;
  90. while ((ut = getutxent()) != NULL) {
  91. if ((ut->ut_type == USER_PROCESS) && (ut->ut_user[0] != '\0'))
  92. users++;
  93. }
  94. printf(", %u users", users);
  95. }
  96. #endif
  97. printf(", load average: %u.%02u, %u.%02u, %u.%02u\n",
  98. LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
  99. LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
  100. LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
  101. return EXIT_SUCCESS;
  102. }