blob: 149bae6e59dd2f046cbddf634e64a676acc2c18a [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 */
14
Mark Whitley827e45c2001-03-09 23:59:51 +000015/* getopt not needed */
16
Pere Orgad91b1382011-08-09 04:09:17 +020017//config:config UPTIME
18//config: bool "uptime"
19//config: default y
20//config: select PLATFORM_LINUX #sysinfo()
21//config: help
22//config: uptime gives a one line display of the current time, how long
23//config: the system has been running, how many users are currently logged
24//config: on, and the system load averages for the past 1, 5, and 15 minutes.
25//config:
26//config:config FEATURE_UPTIME_UTMP_SUPPORT
27//config: bool "Support for showing the number of users"
28//config: default y
29//config: depends on UPTIME && FEATURE_UTMP
30//config: help
31//config: Makes uptime display the number of users currently logged on.
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
Erik Andersen65fc1c72000-03-05 08:16:03 +000047
Mike Frysinger038b0762005-08-27 18:18:06 +000048#ifndef FSHIFT
49# define FSHIFT 16 /* nr of bits of precision */
50#endif
Denys Vlasenko5845a062011-08-10 13:00:04 +020051#define FIXED_1 (1 << FSHIFT) /* 1.0 as fixed-point */
52#define LOAD_INT(x) (unsigned)((x) >> FSHIFT)
53#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1 - 1)) * 100)
Erik Andersen65fc1c72000-03-05 08:16:03 +000054
Eric Andersen10dc9d42000-06-26 10:45:52 +000055
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000056int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000057int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Erik Andersen65fc1c72000-03-05 08:16:03 +000058{
Denys Vlasenko5845a062011-08-10 13:00:04 +020059 unsigned updays, uphours, upminutes;
Erik Andersen65fc1c72000-03-05 08:16:03 +000060 struct sysinfo info;
61 struct tm *current_time;
62 time_t current_secs;
63
64 time(&current_secs);
65 current_time = localtime(&current_secs);
66
67 sysinfo(&info);
68
Denys Vlasenko5845a062011-08-10 13:00:04 +020069 printf(" %02u:%02u:%02u up ",
Eric Andersenba9b22a2004-04-06 11:10:50 +000070 current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
Denys Vlasenko5845a062011-08-10 13:00:04 +020071 updays = (unsigned) info.uptime / (unsigned)(60*60*24);
Erik Andersen65fc1c72000-03-05 08:16:03 +000072 if (updays)
Denys Vlasenko5845a062011-08-10 13:00:04 +020073 printf("%u day%s, ", updays, (updays != 1) ? "s" : "");
74 upminutes = (unsigned) info.uptime / (unsigned)60;
75 uphours = (upminutes / (unsigned)60) % (unsigned)24;
Erik Andersen65fc1c72000-03-05 08:16:03 +000076 upminutes %= 60;
Denis Vlasenko51742f42007-04-12 00:32:05 +000077 if (uphours)
Denys Vlasenko5845a062011-08-10 13:00:04 +020078 printf("%2u:%02u", uphours, upminutes);
Erik Andersen65fc1c72000-03-05 08:16:03 +000079 else
Denys Vlasenko5845a062011-08-10 13:00:04 +020080 printf("%u min", upminutes);
Erik Andersen65fc1c72000-03-05 08:16:03 +000081
Pere Orgad91b1382011-08-09 04:09:17 +020082#if ENABLE_FEATURE_UPTIME_UTMP_SUPPORT
Denys Vlasenko5845a062011-08-10 13:00:04 +020083 {
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020084 struct utmpx *ut;
Denys Vlasenko5845a062011-08-10 13:00:04 +020085 unsigned users = 0;
Bernhard Reutner-Fischer86a7f182015-04-02 23:03:46 +020086 while ((ut = getutxent()) != NULL) {
87 if ((ut->ut_type == USER_PROCESS) && (ut->ut_user[0] != '\0'))
Denys Vlasenko5845a062011-08-10 13:00:04 +020088 users++;
89 }
90 printf(", %u users", users);
Pere Orgad91b1382011-08-09 04:09:17 +020091 }
Pere Orgad91b1382011-08-09 04:09:17 +020092#endif
93
Denys Vlasenko5845a062011-08-10 13:00:04 +020094 printf(", load average: %u.%02u, %u.%02u, %u.%02u\n",
Eric Andersenc7bda1c2004-03-15 08:29:22 +000095 LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
96 LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
Erik Andersen65fc1c72000-03-05 08:16:03 +000097 LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
98
Matt Kraai3e856ce2000-12-01 02:55:13 +000099 return EXIT_SUCCESS;
Erik Andersen65fc1c72000-03-05 08:16:03 +0000100}