blob: 4fd0c9d2da6ac4b54c1526fd38ee851158bc5ce3 [file] [log] [blame]
Erik Andersen65fc1c72000-03-05 08:16:03 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini uptime implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Erik Andersen65fc1c72000-03-05 08:16:03 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Erik Andersen65fc1c72000-03-05 08:16:03 +00008 */
9
Pere Orgad91b1382011-08-09 04:09:17 +020010/* 2011 Pere Orga <gotrunks@gmail.com>
11 *
12 * Added FEATURE_UPTIME_UTMP_SUPPORT flag.
Erik Andersen65fc1c72000-03-05 08:16:03 +000013 */
Pere Orgad91b1382011-08-09 04:09:17 +020014//config:config UPTIME
Denys Vlasenkob097a842018-12-28 03:20:17 +010015//config: bool "uptime (3.7 kb)"
Pere Orgad91b1382011-08-09 04:09:17 +020016//config: default y
Pere Orgad91b1382011-08-09 04:09:17 +020017//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020018//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.
Pere Orgad91b1382011-08-09 04:09:17 +020021//config:
22//config:config FEATURE_UPTIME_UTMP_SUPPORT
Denys Vlasenkof5604222017-01-10 14:58:54 +010023//config: bool "Show the number of users"
Pere Orgad91b1382011-08-09 04:09:17 +020024//config: default y
25//config: depends on UPTIME && FEATURE_UTMP
26//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020027//config: Display the number of users currently logged on.
Pere Orgad91b1382011-08-09 04:09:17 +020028
Denys Vlasenko1a1203f2017-08-07 16:47:34 +020029//applet:IF_UPTIME(APPLET_NOEXEC(uptime, uptime, BB_DIR_USR_BIN, BB_SUID_DROP, uptime))
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010030
31//kbuild:lib-$(CONFIG_UPTIME) += uptime.o
32
Pere Orga5bc8c002011-04-11 03:29:49 +020033//usage:#define uptime_trivial_usage
34//usage: ""
35//usage:#define uptime_full_usage "\n\n"
36//usage: "Display the time since the last boot"
37//usage:
38//usage:#define uptime_example_usage
39//usage: "$ uptime\n"
40//usage: " 1:55pm up 2:30, load average: 0.09, 0.04, 0.00\n"
41
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000042#include "libbb.h"
Denys Vlasenko67905e22011-07-26 13:42:12 +020043#ifdef __linux__
44# include <sys/sysinfo.h>
45#endif
46
Mike Frysinger038b0762005-08-27 18:18:06 +000047#ifndef FSHIFT
48# define FSHIFT 16 /* nr of bits of precision */
49#endif
Denys Vlasenko5845a062011-08-10 13:00:04 +020050#define FIXED_1 (1 << FSHIFT) /* 1.0 as fixed-point */
51#define LOAD_INT(x) (unsigned)((x) >> FSHIFT)
52#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1 - 1)) * 100)
Erik Andersen65fc1c72000-03-05 08:16:03 +000053
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000054int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000055int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Erik Andersen65fc1c72000-03-05 08:16:03 +000056{
Denys Vlasenko5845a062011-08-10 13:00:04 +020057 unsigned updays, uphours, upminutes;
Denys Vlasenko3f2e9632018-08-03 19:53:49 +020058 unsigned opts;
Erik Andersen65fc1c72000-03-05 08:16:03 +000059 struct sysinfo info;
60 struct tm *current_time;
61 time_t current_secs;
62
Denys Vlasenko3f2e9632018-08-03 19:53:49 +020063 opts = getopt32(argv, "s");
Erik Andersen65fc1c72000-03-05 08:16:03 +000064
Denys Vlasenko3f2e9632018-08-03 19:53:49 +020065 time(&current_secs);
Erik Andersen65fc1c72000-03-05 08:16:03 +000066 sysinfo(&info);
67
Denys Vlasenko3f2e9632018-08-03 19:53:49 +020068 if (opts) // -s
69 current_secs -= info.uptime;
70
71 current_time = localtime(&current_secs);
72
73 if (opts) { // -s
74 printf("%04u-%02u-%02u %02u:%02u:%02u\n",
75 current_time->tm_year + 1900, current_time->tm_mon + 1, current_time->tm_mday,
76 current_time->tm_hour, current_time->tm_min, current_time->tm_sec
77 );
78 /* The above way of calculating boot time is wobbly,
79 * info.uptime has only 1 second precision, which makes
80 * "uptime -s" wander +- one second.
81 * /proc/uptime may be better, it has 0.01s precision.
82 */
83 return EXIT_SUCCESS;
84 }
85
Denys Vlasenko5845a062011-08-10 13:00:04 +020086 printf(" %02u:%02u:%02u up ",
Denys Vlasenko3f2e9632018-08-03 19:53:49 +020087 current_time->tm_hour, current_time->tm_min, current_time->tm_sec
88 );
Denys Vlasenko5845a062011-08-10 13:00:04 +020089 updays = (unsigned) info.uptime / (unsigned)(60*60*24);
Denys Vlasenko3f2e9632018-08-03 19:53:49 +020090 if (updays != 0)
Denys Vlasenko5845a062011-08-10 13:00:04 +020091 printf("%u day%s, ", updays, (updays != 1) ? "s" : "");
92 upminutes = (unsigned) info.uptime / (unsigned)60;
93 uphours = (upminutes / (unsigned)60) % (unsigned)24;
Erik Andersen65fc1c72000-03-05 08:16:03 +000094 upminutes %= 60;
Denys Vlasenko3f2e9632018-08-03 19:53:49 +020095 if (uphours != 0)
Denys Vlasenko5845a062011-08-10 13:00:04 +020096 printf("%2u:%02u", uphours, upminutes);
Erik Andersen65fc1c72000-03-05 08:16:03 +000097 else
Denys Vlasenko5845a062011-08-10 13:00:04 +020098 printf("%u min", upminutes);
Erik Andersen65fc1c72000-03-05 08:16:03 +000099
Pere Orgad91b1382011-08-09 04:09:17 +0200100#if ENABLE_FEATURE_UPTIME_UTMP_SUPPORT
Denys Vlasenko5845a062011-08-10 13:00:04 +0200101 {
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +0200102 struct utmpx *ut;
Denys Vlasenko5845a062011-08-10 13:00:04 +0200103 unsigned users = 0;
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +0200104 while ((ut = getutxent()) != NULL) {
105 if ((ut->ut_type == USER_PROCESS) && (ut->ut_user[0] != '\0'))
Denys Vlasenko5845a062011-08-10 13:00:04 +0200106 users++;
107 }
108 printf(", %u users", users);
Pere Orgad91b1382011-08-09 04:09:17 +0200109 }
Pere Orgad91b1382011-08-09 04:09:17 +0200110#endif
111
Denys Vlasenko5845a062011-08-10 13:00:04 +0200112 printf(", load average: %u.%02u, %u.%02u, %u.%02u\n",
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000113 LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
114 LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
Erik Andersen65fc1c72000-03-05 08:16:03 +0000115 LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
116
Matt Kraai3e856ce2000-12-01 02:55:13 +0000117 return EXIT_SUCCESS;
Erik Andersen65fc1c72000-03-05 08:16:03 +0000118}