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 20:14:46 +02:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2000-03-05 08:16:03 +00:00
|
|
|
*/
|
|
|
|
|
2011-08-09 04:09:17 +02: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 04:09:17 +02: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 03:29:49 +02: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 13:42:12 +02: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
|
2000-03-05 08:16:03 +00:00
|
|
|
#define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
|
|
|
|
#define LOAD_INT(x) ((x) >> FSHIFT)
|
|
|
|
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
|
|
|
|
|
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
|
|
|
{
|
|
|
|
int updays, uphours, upminutes;
|
|
|
|
struct sysinfo info;
|
|
|
|
struct tm *current_time;
|
|
|
|
time_t current_secs;
|
|
|
|
|
|
|
|
time(¤t_secs);
|
|
|
|
current_time = localtime(¤t_secs);
|
|
|
|
|
|
|
|
sysinfo(&info);
|
|
|
|
|
2004-04-06 11:10:50 +00:00
|
|
|
printf(" %02d:%02d:%02d up ",
|
|
|
|
current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
|
2000-03-05 08:16:03 +00:00
|
|
|
updays = (int) info.uptime / (60*60*24);
|
|
|
|
if (updays)
|
2001-01-18 02:57:08 +00:00
|
|
|
printf("%d day%s, ", updays, (updays != 1) ? "s" : "");
|
2000-03-05 08:16:03 +00:00
|
|
|
upminutes = (int) info.uptime / 60;
|
|
|
|
uphours = (upminutes / 60) % 24;
|
|
|
|
upminutes %= 60;
|
2007-04-12 00:32:05 +00:00
|
|
|
if (uphours)
|
2001-01-18 02:57:08 +00:00
|
|
|
printf("%2d:%02d, ", uphours, upminutes);
|
2000-03-05 08:16:03 +00:00
|
|
|
else
|
2001-01-18 02:57:08 +00:00
|
|
|
printf("%d min, ", upminutes);
|
2000-03-05 08:16:03 +00:00
|
|
|
|
2011-08-09 04:09:17 +02:00
|
|
|
#if ENABLE_FEATURE_UPTIME_UTMP_SUPPORT
|
|
|
|
{
|
|
|
|
struct utmp *ut;
|
|
|
|
int users = 0;
|
|
|
|
while ((ut = getutent())) {
|
|
|
|
if ((ut->ut_type == USER_PROCESS) && (ut->ut_name[0] != '\0'))
|
|
|
|
users++;
|
|
|
|
}
|
|
|
|
printf("%d users, ", users);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-03-15 08:29:22 +00:00
|
|
|
printf("load average: %ld.%02ld, %ld.%02ld, %ld.%02ld\n",
|
|
|
|
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
|
|
|
}
|