2000-03-05 13:46:03 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini uptime implementation for busybox
|
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
2000-03-05 13:46:03 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2000-03-05 13:46:03 +05:30
|
|
|
*/
|
|
|
|
|
2011-08-09 07:39:17 +05:30
|
|
|
/* 2011 Pere Orga <gotrunks@gmail.com>
|
|
|
|
*
|
|
|
|
* Added FEATURE_UPTIME_UTMP_SUPPORT flag.
|
2000-03-05 13:46:03 +05:30
|
|
|
*/
|
2011-08-09 07:39:17 +05:30
|
|
|
//config:config UPTIME
|
|
|
|
//config: bool "uptime"
|
|
|
|
//config: default y
|
|
|
|
//config: select PLATFORM_LINUX #sysinfo()
|
|
|
|
//config: help
|
|
|
|
//config: uptime gives a one line display of the current time, how long
|
|
|
|
//config: the system has been running, how many users are currently logged
|
|
|
|
//config: on, and the system load averages for the past 1, 5, and 15 minutes.
|
|
|
|
//config:
|
|
|
|
//config:config FEATURE_UPTIME_UTMP_SUPPORT
|
|
|
|
//config: bool "Support for showing the number of users"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on UPTIME && FEATURE_UTMP
|
|
|
|
//config: help
|
|
|
|
//config: Makes uptime display the number of users currently logged on.
|
|
|
|
|
2016-11-23 10:53:44 +05:30
|
|
|
//applet:IF_UPTIME(APPLET(uptime, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_UPTIME) += uptime.o
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define uptime_trivial_usage
|
|
|
|
//usage: ""
|
|
|
|
//usage:#define uptime_full_usage "\n\n"
|
|
|
|
//usage: "Display the time since the last boot"
|
|
|
|
//usage:
|
|
|
|
//usage:#define uptime_example_usage
|
|
|
|
//usage: "$ uptime\n"
|
|
|
|
//usage: " 1:55pm up 2:30, load average: 0.09, 0.04, 0.00\n"
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2011-07-26 17:12:12 +05:30
|
|
|
#ifdef __linux__
|
|
|
|
# include <sys/sysinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2000-03-05 13:46:03 +05:30
|
|
|
|
2005-08-27 23:48:06 +05:30
|
|
|
#ifndef FSHIFT
|
|
|
|
# define FSHIFT 16 /* nr of bits of precision */
|
|
|
|
#endif
|
2011-08-10 16:30:04 +05:30
|
|
|
#define FIXED_1 (1 << FSHIFT) /* 1.0 as fixed-point */
|
|
|
|
#define LOAD_INT(x) (unsigned)((x) >> FSHIFT)
|
|
|
|
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1 - 1)) * 100)
|
2000-03-05 13:46:03 +05:30
|
|
|
|
2000-06-26 16:15:52 +05:30
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
|
2000-03-05 13:46:03 +05:30
|
|
|
{
|
2011-08-10 16:30:04 +05:30
|
|
|
unsigned updays, uphours, upminutes;
|
2000-03-05 13:46:03 +05:30
|
|
|
struct sysinfo info;
|
|
|
|
struct tm *current_time;
|
|
|
|
time_t current_secs;
|
|
|
|
|
|
|
|
time(¤t_secs);
|
|
|
|
current_time = localtime(¤t_secs);
|
|
|
|
|
|
|
|
sysinfo(&info);
|
|
|
|
|
2011-08-10 16:30:04 +05:30
|
|
|
printf(" %02u:%02u:%02u up ",
|
2004-04-06 16:40:50 +05:30
|
|
|
current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
|
2011-08-10 16:30:04 +05:30
|
|
|
updays = (unsigned) info.uptime / (unsigned)(60*60*24);
|
2000-03-05 13:46:03 +05:30
|
|
|
if (updays)
|
2011-08-10 16:30:04 +05:30
|
|
|
printf("%u day%s, ", updays, (updays != 1) ? "s" : "");
|
|
|
|
upminutes = (unsigned) info.uptime / (unsigned)60;
|
|
|
|
uphours = (upminutes / (unsigned)60) % (unsigned)24;
|
2000-03-05 13:46:03 +05:30
|
|
|
upminutes %= 60;
|
2007-04-12 06:02:05 +05:30
|
|
|
if (uphours)
|
2011-08-10 16:30:04 +05:30
|
|
|
printf("%2u:%02u", uphours, upminutes);
|
2000-03-05 13:46:03 +05:30
|
|
|
else
|
2011-08-10 16:30:04 +05:30
|
|
|
printf("%u min", upminutes);
|
2000-03-05 13:46:03 +05:30
|
|
|
|
2011-08-09 07:39:17 +05:30
|
|
|
#if ENABLE_FEATURE_UPTIME_UTMP_SUPPORT
|
2011-08-10 16:30:04 +05:30
|
|
|
{
|
2015-04-03 02:33:46 +05:30
|
|
|
struct utmpx *ut;
|
2011-08-10 16:30:04 +05:30
|
|
|
unsigned users = 0;
|
2015-04-03 02:33:46 +05:30
|
|
|
while ((ut = getutxent()) != NULL) {
|
|
|
|
if ((ut->ut_type == USER_PROCESS) && (ut->ut_user[0] != '\0'))
|
2011-08-10 16:30:04 +05:30
|
|
|
users++;
|
|
|
|
}
|
|
|
|
printf(", %u users", users);
|
2011-08-09 07:39:17 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-08-10 16:30:04 +05:30
|
|
|
printf(", load average: %u.%02u, %u.%02u, %u.%02u\n",
|
2004-03-15 13:59:22 +05:30
|
|
|
LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
|
|
|
|
LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
|
2000-03-05 13:46:03 +05:30
|
|
|
LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
|
|
|
|
|
2000-12-01 08:25:13 +05:30
|
|
|
return EXIT_SUCCESS;
|
2000-03-05 13:46:03 +05:30
|
|
|
}
|