blob: 24b2b39df3f3870cd80e43531d78cf855f8747db [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 Vlasenko4eed2c62017-07-18 22:01:24 +020015//config: bool "uptime (632 bytes)"
Pere Orgad91b1382011-08-09 04:09:17 +020016//config: default y
17//config: select PLATFORM_LINUX #sysinfo()
18//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020019//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 Orgad91b1382011-08-09 04:09:17 +020022//config:
23//config:config FEATURE_UPTIME_UTMP_SUPPORT
Denys Vlasenkof5604222017-01-10 14:58:54 +010024//config: bool "Show the number of users"
Pere Orgad91b1382011-08-09 04:09:17 +020025//config: default y
26//config: depends on UPTIME && FEATURE_UTMP
27//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020028//config: Display the number of users currently logged on.
Pere Orgad91b1382011-08-09 04:09:17 +020029
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010030//applet:IF_UPTIME(APPLET(uptime, BB_DIR_USR_BIN, BB_SUID_DROP))
31
32//kbuild:lib-$(CONFIG_UPTIME) += uptime.o
33
Pere Orga5bc8c002011-04-11 03:29:49 +020034//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 Vlasenkob6adbf12007-05-26 19:00:18 +000043#include "libbb.h"
Denys Vlasenko67905e22011-07-26 13:42:12 +020044#ifdef __linux__
45# include <sys/sysinfo.h>
46#endif
47
Erik Andersen65fc1c72000-03-05 08:16:03 +000048
Mike Frysinger038b0762005-08-27 18:18:06 +000049#ifndef FSHIFT
50# define FSHIFT 16 /* nr of bits of precision */
51#endif
Denys Vlasenko5845a062011-08-10 13:00:04 +020052#define FIXED_1 (1 << FSHIFT) /* 1.0 as fixed-point */
53#define LOAD_INT(x) (unsigned)((x) >> FSHIFT)
54#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1 - 1)) * 100)
Erik Andersen65fc1c72000-03-05 08:16:03 +000055
Eric Andersen10dc9d42000-06-26 10:45:52 +000056
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000057int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000058int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Erik Andersen65fc1c72000-03-05 08:16:03 +000059{
Denys Vlasenko5845a062011-08-10 13:00:04 +020060 unsigned updays, uphours, upminutes;
Erik Andersen65fc1c72000-03-05 08:16:03 +000061 struct sysinfo info;
62 struct tm *current_time;
63 time_t current_secs;
64
65 time(&current_secs);
66 current_time = localtime(&current_secs);
67
68 sysinfo(&info);
69
Denys Vlasenko5845a062011-08-10 13:00:04 +020070 printf(" %02u:%02u:%02u up ",
Eric Andersenba9b22a2004-04-06 11:10:50 +000071 current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
Denys Vlasenko5845a062011-08-10 13:00:04 +020072 updays = (unsigned) info.uptime / (unsigned)(60*60*24);
Erik Andersen65fc1c72000-03-05 08:16:03 +000073 if (updays)
Denys Vlasenko5845a062011-08-10 13:00:04 +020074 printf("%u day%s, ", updays, (updays != 1) ? "s" : "");
75 upminutes = (unsigned) info.uptime / (unsigned)60;
76 uphours = (upminutes / (unsigned)60) % (unsigned)24;
Erik Andersen65fc1c72000-03-05 08:16:03 +000077 upminutes %= 60;
Denis Vlasenko51742f42007-04-12 00:32:05 +000078 if (uphours)
Denys Vlasenko5845a062011-08-10 13:00:04 +020079 printf("%2u:%02u", uphours, upminutes);
Erik Andersen65fc1c72000-03-05 08:16:03 +000080 else
Denys Vlasenko5845a062011-08-10 13:00:04 +020081 printf("%u min", upminutes);
Erik Andersen65fc1c72000-03-05 08:16:03 +000082
Pere Orgad91b1382011-08-09 04:09:17 +020083#if ENABLE_FEATURE_UPTIME_UTMP_SUPPORT
Denys Vlasenko5845a062011-08-10 13:00:04 +020084 {
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020085 struct utmpx *ut;
Denys Vlasenko5845a062011-08-10 13:00:04 +020086 unsigned users = 0;
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020087 while ((ut = getutxent()) != NULL) {
88 if ((ut->ut_type == USER_PROCESS) && (ut->ut_user[0] != '\0'))
Denys Vlasenko5845a062011-08-10 13:00:04 +020089 users++;
90 }
91 printf(", %u users", users);
Pere Orgad91b1382011-08-09 04:09:17 +020092 }
Pere Orgad91b1382011-08-09 04:09:17 +020093#endif
94
Denys Vlasenko5845a062011-08-10 13:00:04 +020095 printf(", load average: %u.%02u, %u.%02u, %u.%02u\n",
Eric Andersenc7bda1c2004-03-15 08:29:22 +000096 LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
97 LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
Erik Andersen65fc1c72000-03-05 08:16:03 +000098 LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
99
Matt Kraai3e856ce2000-12-01 02:55:13 +0000100 return EXIT_SUCCESS;
Erik Andersen65fc1c72000-03-05 08:16:03 +0000101}