busybox/procps/uptime.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Mini uptime implementation for busybox
   4 *
   5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
   6 *
   7 * Licensed under GPLv2, see file LICENSE in this source tree.
   8 */
   9
  10/* 2011         Pere Orga <gotrunks@gmail.com>
  11 *
  12 * Added FEATURE_UPTIME_UTMP_SUPPORT flag.
  13 */
  14//config:config UPTIME
  15//config:       bool "uptime (3.7 kb)"
  16//config:       default y
  17//config:       help
  18//config:       uptime gives a one line display of the current time, how long
  19//config:       the system has been running, how many users are currently logged
  20//config:       on, and the system load averages for the past 1, 5, and 15 minutes.
  21//config:
  22//config:config FEATURE_UPTIME_UTMP_SUPPORT
  23//config:       bool "Show the number of users"
  24//config:       default y
  25//config:       depends on UPTIME && FEATURE_UTMP
  26//config:       help
  27//config:       Display the number of users currently logged on.
  28
  29//applet:IF_UPTIME(APPLET_NOEXEC(uptime, uptime, BB_DIR_USR_BIN, BB_SUID_DROP, uptime))
  30
  31//kbuild:lib-$(CONFIG_UPTIME) += uptime.o
  32
  33//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
  42#include "libbb.h"
  43#ifdef __linux__
  44# include <sys/sysinfo.h>
  45#endif
  46
  47#ifndef FSHIFT
  48# define FSHIFT 16              /* nr of bits of precision */
  49#endif
  50#define FIXED_1      (1 << FSHIFT)     /* 1.0 as fixed-point */
  51#define LOAD_INT(x)  (unsigned)((x) >> FSHIFT)
  52#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1 - 1)) * 100)
  53
  54int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  55int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  56{
  57        unsigned updays, uphours, upminutes;
  58        unsigned opts;
  59        struct sysinfo info;
  60        struct tm *current_time;
  61        time_t current_secs;
  62
  63        opts = getopt32(argv, "s");
  64
  65        time(&current_secs);
  66        sysinfo(&info);
  67
  68        if (opts) // -s
  69                current_secs -= info.uptime;
  70
  71        current_time = localtime(&current_secs);
  72
  73        if (opts) { // -s
  74                printf("%04u-%02u-%02u %02u:%02u:%02u\n",
  75                        current_time->tm_year + 1900, current_time->tm_mon + 1, current_time->tm_mday,
  76                        current_time->tm_hour, current_time->tm_min, current_time->tm_sec
  77                );
  78                /* The above way of calculating boot time is wobbly,
  79                 * info.uptime has only 1 second precision, which makes
  80                 * "uptime -s" wander +- one second.
  81                 * /proc/uptime may be better, it has 0.01s precision.
  82                 */
  83                return EXIT_SUCCESS;
  84        }
  85
  86        printf(" %02u:%02u:%02u up ",
  87                        current_time->tm_hour, current_time->tm_min, current_time->tm_sec
  88        );
  89        updays = (unsigned) info.uptime / (unsigned)(60*60*24);
  90        if (updays != 0)
  91                printf("%u day%s, ", updays, (updays != 1) ? "s" : "");
  92        upminutes = (unsigned) info.uptime / (unsigned)60;
  93        uphours = (upminutes / (unsigned)60) % (unsigned)24;
  94        upminutes %= 60;
  95        if (uphours != 0)
  96                printf("%2u:%02u", uphours, upminutes);
  97        else
  98                printf("%u min", upminutes);
  99
 100#if ENABLE_FEATURE_UPTIME_UTMP_SUPPORT
 101        {
 102                struct utmpx *ut;
 103                unsigned users = 0;
 104                while ((ut = getutxent()) != NULL) {
 105                        if ((ut->ut_type == USER_PROCESS) && (ut->ut_user[0] != '\0'))
 106                                users++;
 107                }
 108                printf(",  %u users", users);
 109        }
 110#endif
 111
 112        printf(",  load average: %u.%02u, %u.%02u, %u.%02u\n",
 113                        LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
 114                        LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
 115                        LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
 116
 117        return EXIT_SUCCESS;
 118}
 119