2000-03-05 08:16:03 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini uptime implementation for busybox
|
|
|
|
*
|
2004-03-15 08:29:22 +00:00
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
2000-03-05 08:16:03 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2000-03-05 08:16:03 +00:00
|
|
|
*/
|
|
|
|
|
2011-08-09 02:09:17 +00:00
|
|
|
/* 2011 Pere Orga <gotrunks@gmail.com>
|
|
|
|
*
|
|
|
|
* Added FEATURE_UPTIME_UTMP_SUPPORT flag.
|
2000-03-05 08:16:03 +00:00
|
|
|
*/
|
|
|
|
|
2001-03-09 23:59:51 +00:00
|
|
|
/* getopt not needed */
|
|
|
|
|
2011-08-09 02:09:17 +00:00
|
|
|
//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.
|
|
|
|
|
2011-04-11 01:29:49 +00:00
|
|
|
//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-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2011-07-26 11:42:12 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
# include <sys/sysinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2000-03-05 08:16:03 +00:00
|
|
|
|
2005-08-27 18:18:06 +00:00
|
|
|
#ifndef FSHIFT
|
|
|
|
# define FSHIFT 16 /* nr of bits of precision */
|
|
|
|
#endif
|
2011-08-10 11:00:04 +00:00
|
|
|
#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 08:16:03 +00:00
|
|
|
|
2000-06-26 10:45:52 +00:00
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
|
2000-03-05 08:16:03 +00:00
|
|
|
{
|
2011-08-10 11:00:04 +00:00
|
|
|
unsigned updays, uphours, upminutes;
|
2000-03-05 08:16:03 +00:00
|
|
|
struct sysinfo info;
|
|
|
|
struct tm *current_time;
|
|
|
|
time_t current_secs;
|
|
|
|
|
|
|
|
time(¤t_secs);
|
|
|
|
current_time = localtime(¤t_secs);
|
|
|
|
|
|
|
|
sysinfo(&info);
|
|
|
|
|
2011-08-10 11:00:04 +00:00
|
|
|
printf(" %02u:%02u:%02u up ",
|
2004-04-06 11:10:50 +00:00
|
|
|
current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
|
2011-08-10 11:00:04 +00:00
|
|
|
updays = (unsigned) info.uptime / (unsigned)(60*60*24);
|
2000-03-05 08:16:03 +00:00
|
|
|
if (updays)
|
2011-08-10 11:00:04 +00:00
|
|
|
printf("%u day%s, ", updays, (updays != 1) ? "s" : "");
|
|
|
|
upminutes = (unsigned) info.uptime / (unsigned)60;
|
|
|
|
uphours = (upminutes / (unsigned)60) % (unsigned)24;
|
2000-03-05 08:16:03 +00:00
|
|
|
upminutes %= 60;
|
2007-04-12 00:32:05 +00:00
|
|
|
if (uphours)
|
2011-08-10 11:00:04 +00:00
|
|
|
printf("%2u:%02u", uphours, upminutes);
|
2000-03-05 08:16:03 +00:00
|
|
|
else
|
2011-08-10 11:00:04 +00:00
|
|
|
printf("%u min", upminutes);
|
2000-03-05 08:16:03 +00:00
|
|
|
|
2011-08-09 02:09:17 +00:00
|
|
|
#if ENABLE_FEATURE_UPTIME_UTMP_SUPPORT
|
2011-08-10 11:00:04 +00:00
|
|
|
{
|
|
|
|
struct utmp *ut;
|
|
|
|
unsigned users = 0;
|
|
|
|
while ((ut = getutent()) != NULL) {
|
|
|
|
if ((ut->ut_type == USER_PROCESS) && (ut->ut_name[0] != '\0'))
|
|
|
|
users++;
|
|
|
|
}
|
|
|
|
printf(", %u users", users);
|
2011-08-09 02:09:17 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-08-10 11:00:04 +00:00
|
|
|
printf(", load average: %u.%02u, %u.%02u, %u.%02u\n",
|
2004-03-15 08:29:22 +00:00
|
|
|
LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
|
|
|
|
LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
|
2000-03-05 08:16:03 +00:00
|
|
|
LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
|
|
|
|
|
2000-12-01 02:55:13 +00:00
|
|
|
return EXIT_SUCCESS;
|
2000-03-05 08:16:03 +00:00
|
|
|
}
|