3
0

uptime.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 (632 bytes)"
  15. //config: default y
  16. //config: select PLATFORM_LINUX #sysinfo()
  17. //config: help
  18. //config: uptime gives a one line display of the current time, how long
  19. //config: the system has been running, how many users are currently logged
  20. //config: on, and the system load averages for the past 1, 5, and 15 minutes.
  21. //config:
  22. //config:config FEATURE_UPTIME_UTMP_SUPPORT
  23. //config: bool "Show the number of users"
  24. //config: default y
  25. //config: depends on UPTIME && FEATURE_UTMP
  26. //config: help
  27. //config: Display the number of users currently logged on.
  28. //applet:IF_UPTIME(APPLET_NOEXEC(uptime, uptime, BB_DIR_USR_BIN, BB_SUID_DROP, uptime))
  29. //kbuild:lib-$(CONFIG_UPTIME) += uptime.o
  30. //usage:#define uptime_trivial_usage
  31. //usage: ""
  32. //usage:#define uptime_full_usage "\n\n"
  33. //usage: "Display the time since the last boot"
  34. //usage:
  35. //usage:#define uptime_example_usage
  36. //usage: "$ uptime\n"
  37. //usage: " 1:55pm up 2:30, load average: 0.09, 0.04, 0.00\n"
  38. #include "libbb.h"
  39. #ifdef __linux__
  40. # include <sys/sysinfo.h>
  41. #endif
  42. #ifndef FSHIFT
  43. # define FSHIFT 16 /* nr of bits of precision */
  44. #endif
  45. #define FIXED_1 (1 << FSHIFT) /* 1.0 as fixed-point */
  46. #define LOAD_INT(x) (unsigned)((x) >> FSHIFT)
  47. #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1 - 1)) * 100)
  48. int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  49. int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  50. {
  51. unsigned updays, uphours, upminutes;
  52. struct sysinfo info;
  53. struct tm *current_time;
  54. time_t current_secs;
  55. time(&current_secs);
  56. current_time = localtime(&current_secs);
  57. sysinfo(&info);
  58. printf(" %02u:%02u:%02u up ",
  59. current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
  60. updays = (unsigned) info.uptime / (unsigned)(60*60*24);
  61. if (updays)
  62. printf("%u day%s, ", updays, (updays != 1) ? "s" : "");
  63. upminutes = (unsigned) info.uptime / (unsigned)60;
  64. uphours = (upminutes / (unsigned)60) % (unsigned)24;
  65. upminutes %= 60;
  66. if (uphours)
  67. printf("%2u:%02u", uphours, upminutes);
  68. else
  69. printf("%u min", upminutes);
  70. #if ENABLE_FEATURE_UPTIME_UTMP_SUPPORT
  71. {
  72. struct utmpx *ut;
  73. unsigned users = 0;
  74. while ((ut = getutxent()) != NULL) {
  75. if ((ut->ut_type == USER_PROCESS) && (ut->ut_user[0] != '\0'))
  76. users++;
  77. }
  78. printf(", %u users", users);
  79. }
  80. #endif
  81. printf(", load average: %u.%02u, %u.%02u, %u.%02u\n",
  82. LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
  83. LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
  84. LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
  85. return EXIT_SUCCESS;
  86. }