Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini uptime implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Pere Orga | d91b138 | 2011-08-09 04:09:17 +0200 | [diff] [blame] | 10 | /* 2011 Pere Orga <gotrunks@gmail.com> |
| 11 | * |
| 12 | * Added FEATURE_UPTIME_UTMP_SUPPORT flag. |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 13 | */ |
Pere Orga | d91b138 | 2011-08-09 04:09:17 +0200 | [diff] [blame] | 14 | //config:config UPTIME |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 15 | //config: bool "uptime (632 bytes)" |
Pere Orga | d91b138 | 2011-08-09 04:09:17 +0200 | [diff] [blame] | 16 | //config: default y |
| 17 | //config: select PLATFORM_LINUX #sysinfo() |
| 18 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 19 | //config: uptime gives a one line display of the current time, how long |
| 20 | //config: the system has been running, how many users are currently logged |
| 21 | //config: on, and the system load averages for the past 1, 5, and 15 minutes. |
Pere Orga | d91b138 | 2011-08-09 04:09:17 +0200 | [diff] [blame] | 22 | //config: |
| 23 | //config:config FEATURE_UPTIME_UTMP_SUPPORT |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 24 | //config: bool "Show the number of users" |
Pere Orga | d91b138 | 2011-08-09 04:09:17 +0200 | [diff] [blame] | 25 | //config: default y |
| 26 | //config: depends on UPTIME && FEATURE_UTMP |
| 27 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 28 | //config: Display the number of users currently logged on. |
Pere Orga | d91b138 | 2011-08-09 04:09:17 +0200 | [diff] [blame] | 29 | |
Denys Vlasenko | 1a1203f | 2017-08-07 16:47:34 +0200 | [diff] [blame] | 30 | //applet:IF_UPTIME(APPLET_NOEXEC(uptime, uptime, BB_DIR_USR_BIN, BB_SUID_DROP, uptime)) |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 31 | |
| 32 | //kbuild:lib-$(CONFIG_UPTIME) += uptime.o |
| 33 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 34 | //usage:#define uptime_trivial_usage |
| 35 | //usage: "" |
| 36 | //usage:#define uptime_full_usage "\n\n" |
| 37 | //usage: "Display the time since the last boot" |
| 38 | //usage: |
| 39 | //usage:#define uptime_example_usage |
| 40 | //usage: "$ uptime\n" |
| 41 | //usage: " 1:55pm up 2:30, load average: 0.09, 0.04, 0.00\n" |
| 42 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 43 | #include "libbb.h" |
Denys Vlasenko | 67905e2 | 2011-07-26 13:42:12 +0200 | [diff] [blame] | 44 | #ifdef __linux__ |
| 45 | # include <sys/sysinfo.h> |
| 46 | #endif |
| 47 | |
Mike Frysinger | 038b076 | 2005-08-27 18:18:06 +0000 | [diff] [blame] | 48 | #ifndef FSHIFT |
| 49 | # define FSHIFT 16 /* nr of bits of precision */ |
| 50 | #endif |
Denys Vlasenko | 5845a06 | 2011-08-10 13:00:04 +0200 | [diff] [blame] | 51 | #define FIXED_1 (1 << FSHIFT) /* 1.0 as fixed-point */ |
| 52 | #define LOAD_INT(x) (unsigned)((x) >> FSHIFT) |
| 53 | #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1 - 1)) * 100) |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 54 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 55 | int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 56 | int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 57 | { |
Denys Vlasenko | 5845a06 | 2011-08-10 13:00:04 +0200 | [diff] [blame] | 58 | unsigned updays, uphours, upminutes; |
Denys Vlasenko | 3f2e963 | 2018-08-03 19:53:49 +0200 | [diff] [blame] | 59 | unsigned opts; |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 60 | struct sysinfo info; |
| 61 | struct tm *current_time; |
| 62 | time_t current_secs; |
| 63 | |
Denys Vlasenko | 3f2e963 | 2018-08-03 19:53:49 +0200 | [diff] [blame] | 64 | opts = getopt32(argv, "s"); |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 65 | |
Denys Vlasenko | 3f2e963 | 2018-08-03 19:53:49 +0200 | [diff] [blame] | 66 | time(¤t_secs); |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 67 | sysinfo(&info); |
| 68 | |
Denys Vlasenko | 3f2e963 | 2018-08-03 19:53:49 +0200 | [diff] [blame] | 69 | if (opts) // -s |
| 70 | current_secs -= info.uptime; |
| 71 | |
| 72 | current_time = localtime(¤t_secs); |
| 73 | |
| 74 | if (opts) { // -s |
| 75 | printf("%04u-%02u-%02u %02u:%02u:%02u\n", |
| 76 | current_time->tm_year + 1900, current_time->tm_mon + 1, current_time->tm_mday, |
| 77 | current_time->tm_hour, current_time->tm_min, current_time->tm_sec |
| 78 | ); |
| 79 | /* The above way of calculating boot time is wobbly, |
| 80 | * info.uptime has only 1 second precision, which makes |
| 81 | * "uptime -s" wander +- one second. |
| 82 | * /proc/uptime may be better, it has 0.01s precision. |
| 83 | */ |
| 84 | return EXIT_SUCCESS; |
| 85 | } |
| 86 | |
Denys Vlasenko | 5845a06 | 2011-08-10 13:00:04 +0200 | [diff] [blame] | 87 | printf(" %02u:%02u:%02u up ", |
Denys Vlasenko | 3f2e963 | 2018-08-03 19:53:49 +0200 | [diff] [blame] | 88 | current_time->tm_hour, current_time->tm_min, current_time->tm_sec |
| 89 | ); |
Denys Vlasenko | 5845a06 | 2011-08-10 13:00:04 +0200 | [diff] [blame] | 90 | updays = (unsigned) info.uptime / (unsigned)(60*60*24); |
Denys Vlasenko | 3f2e963 | 2018-08-03 19:53:49 +0200 | [diff] [blame] | 91 | if (updays != 0) |
Denys Vlasenko | 5845a06 | 2011-08-10 13:00:04 +0200 | [diff] [blame] | 92 | printf("%u day%s, ", updays, (updays != 1) ? "s" : ""); |
| 93 | upminutes = (unsigned) info.uptime / (unsigned)60; |
| 94 | uphours = (upminutes / (unsigned)60) % (unsigned)24; |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 95 | upminutes %= 60; |
Denys Vlasenko | 3f2e963 | 2018-08-03 19:53:49 +0200 | [diff] [blame] | 96 | if (uphours != 0) |
Denys Vlasenko | 5845a06 | 2011-08-10 13:00:04 +0200 | [diff] [blame] | 97 | printf("%2u:%02u", uphours, upminutes); |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 98 | else |
Denys Vlasenko | 5845a06 | 2011-08-10 13:00:04 +0200 | [diff] [blame] | 99 | printf("%u min", upminutes); |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 100 | |
Pere Orga | d91b138 | 2011-08-09 04:09:17 +0200 | [diff] [blame] | 101 | #if ENABLE_FEATURE_UPTIME_UTMP_SUPPORT |
Denys Vlasenko | 5845a06 | 2011-08-10 13:00:04 +0200 | [diff] [blame] | 102 | { |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 103 | struct utmpx *ut; |
Denys Vlasenko | 5845a06 | 2011-08-10 13:00:04 +0200 | [diff] [blame] | 104 | unsigned users = 0; |
Bernhard Reutner-Fischer | 86a7f18 | 2015-04-02 23:03:46 +0200 | [diff] [blame] | 105 | while ((ut = getutxent()) != NULL) { |
| 106 | if ((ut->ut_type == USER_PROCESS) && (ut->ut_user[0] != '\0')) |
Denys Vlasenko | 5845a06 | 2011-08-10 13:00:04 +0200 | [diff] [blame] | 107 | users++; |
| 108 | } |
| 109 | printf(", %u users", users); |
Pere Orga | d91b138 | 2011-08-09 04:09:17 +0200 | [diff] [blame] | 110 | } |
Pere Orga | d91b138 | 2011-08-09 04:09:17 +0200 | [diff] [blame] | 111 | #endif |
| 112 | |
Denys Vlasenko | 5845a06 | 2011-08-10 13:00:04 +0200 | [diff] [blame] | 113 | printf(", load average: %u.%02u, %u.%02u, %u.%02u\n", |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 114 | LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]), |
| 115 | LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]), |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 116 | LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2])); |
| 117 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 118 | return EXIT_SUCCESS; |
Erik Andersen | 65fc1c7 | 2000-03-05 08:16:03 +0000 | [diff] [blame] | 119 | } |