2015-06-21 13:50:04 +05:30
|
|
|
/*
|
|
|
|
* uptime - uptime related functions - part of procps
|
|
|
|
*
|
|
|
|
* Copyright (C) 1992-1998 Michael K. Johnson <johnsonm@redhat.com>
|
|
|
|
* Copyright (C) ???? Larry Greenfield <greenfie@gauss.rutgers.edu>
|
|
|
|
* Copyright (C) 1993 J. Cowley
|
|
|
|
* Copyright (C) 1998-2003 Albert Cahalan
|
2021-01-21 12:07:48 +05:30
|
|
|
* Copyright (C) 2015 Craig Small <csmall@dropbear.xyz>
|
2015-06-21 13:50:04 +05:30
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <utmp.h>
|
|
|
|
|
2021-01-19 11:30:00 +05:30
|
|
|
#include <proc/misc.h>
|
2015-06-21 13:50:04 +05:30
|
|
|
#include "procps-private.h"
|
|
|
|
|
|
|
|
#define UPTIME_FILE "/proc/uptime"
|
|
|
|
|
1970-01-01 05:30:00 +05:30
|
|
|
static __thread char upbuf[256];
|
|
|
|
static __thread char shortbuf[256];
|
2015-06-21 13:50:04 +05:30
|
|
|
|
|
|
|
static int count_users(void)
|
|
|
|
{
|
|
|
|
int numuser = 0;
|
|
|
|
struct utmp *ut;
|
|
|
|
|
|
|
|
setutent();
|
|
|
|
while ((ut = getutent())) {
|
2015-06-29 17:39:59 +05:30
|
|
|
if ((ut->ut_type == USER_PROCESS) && (ut->ut_name[0] != '\0'))
|
|
|
|
numuser++;
|
2015-06-21 13:50:04 +05:30
|
|
|
}
|
|
|
|
endutent();
|
|
|
|
|
|
|
|
return numuser;
|
|
|
|
}
|
2015-06-29 17:39:59 +05:30
|
|
|
|
2015-06-21 13:50:04 +05:30
|
|
|
/*
|
|
|
|
* uptime:
|
|
|
|
*
|
|
|
|
* Find the uptime and idle time of the system.
|
|
|
|
* These numbers are found in /proc/uptime
|
|
|
|
* Unlike other procps functions this closes the file each time
|
|
|
|
* Either uptime_secs or idle_secs can be null
|
|
|
|
*
|
2016-05-01 12:20:25 +05:30
|
|
|
* Returns: 0 on success and <0 on failure
|
2015-06-21 13:50:04 +05:30
|
|
|
*/
|
2015-06-29 17:39:59 +05:30
|
|
|
PROCPS_EXPORT int procps_uptime(
|
|
|
|
double *restrict uptime_secs,
|
|
|
|
double *restrict idle_secs)
|
2015-06-21 13:50:04 +05:30
|
|
|
{
|
|
|
|
double up=0, idle=0;
|
2017-11-15 10:30:00 +05:30
|
|
|
char savelocale[128];
|
2015-06-21 13:50:04 +05:30
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if ((fp = fopen(UPTIME_FILE, "r")) == NULL)
|
2015-06-29 17:39:59 +05:30
|
|
|
return -errno;
|
|
|
|
|
2017-11-15 10:30:00 +05:30
|
|
|
snprintf(savelocale, sizeof(savelocale), "%s", setlocale(LC_NUMERIC, NULL));
|
2015-06-21 13:50:04 +05:30
|
|
|
setlocale(LC_NUMERIC, "C");
|
|
|
|
if (fscanf(fp, "%lf %lf", &up, &idle) < 2) {
|
2015-06-29 17:39:59 +05:30
|
|
|
setlocale(LC_NUMERIC, savelocale);
|
|
|
|
fclose(fp);
|
|
|
|
return -ERANGE;
|
2015-06-21 13:50:04 +05:30
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
setlocale(LC_NUMERIC, savelocale);
|
|
|
|
if (uptime_secs)
|
2015-06-29 17:39:59 +05:30
|
|
|
*uptime_secs = up;
|
2015-06-21 13:50:04 +05:30
|
|
|
if (idle_secs)
|
2015-06-29 17:39:59 +05:30
|
|
|
*idle_secs = idle;
|
2016-05-01 12:20:25 +05:30
|
|
|
return 0;
|
2015-06-21 13:50:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-06-29 21:46:52 +05:30
|
|
|
* procps_uptime_sprint:
|
2015-06-21 13:50:04 +05:30
|
|
|
*
|
|
|
|
* Print current time in nice format
|
|
|
|
*
|
|
|
|
* Returns a statically allocated upbuf or NULL on error
|
|
|
|
*/
|
2015-06-29 17:39:59 +05:30
|
|
|
PROCPS_EXPORT char *procps_uptime_sprint(void)
|
2015-06-21 13:50:04 +05:30
|
|
|
{
|
|
|
|
int upminutes, uphours, updays, users;
|
|
|
|
int pos;
|
|
|
|
time_t realseconds;
|
|
|
|
struct tm *realtime;
|
|
|
|
double uptime_secs, idle_secs;
|
|
|
|
double av1, av5, av15;
|
|
|
|
|
2015-06-23 17:52:50 +05:30
|
|
|
upbuf[0] = '\0';
|
2015-06-21 13:50:04 +05:30
|
|
|
if (time(&realseconds) < 0)
|
2015-06-29 17:39:59 +05:30
|
|
|
return upbuf;
|
2015-06-21 13:50:04 +05:30
|
|
|
realtime = localtime(&realseconds);
|
2015-06-29 17:39:59 +05:30
|
|
|
if (procps_uptime(&uptime_secs, &idle_secs) < 0)
|
|
|
|
return upbuf;
|
2015-06-21 13:50:04 +05:30
|
|
|
|
|
|
|
updays = ((int) uptime_secs / (60*60*24));
|
2015-06-24 17:46:16 +05:30
|
|
|
uphours = ((int) uptime_secs / (60*60)) % 24;
|
2015-06-21 13:50:04 +05:30
|
|
|
upminutes = ((int) uptime_secs / (60)) % 60;
|
|
|
|
|
2015-09-04 10:30:00 +05:30
|
|
|
pos = sprintf(upbuf, " %02d:%02d:%02d up ",
|
|
|
|
realtime->tm_hour, realtime->tm_min, realtime->tm_sec);
|
|
|
|
|
|
|
|
if (updays)
|
2015-09-07 14:08:39 +05:30
|
|
|
pos += sprintf(upbuf + pos, "%d %s, ", updays, (updays > 1) ? "days" : "day");
|
2015-09-04 10:30:00 +05:30
|
|
|
|
2015-06-21 13:50:04 +05:30
|
|
|
if (uphours)
|
2015-06-29 17:39:59 +05:30
|
|
|
pos += sprintf(upbuf + pos, "%2d:%02d, ", uphours, upminutes);
|
2015-06-21 13:50:04 +05:30
|
|
|
else
|
2015-09-04 10:30:00 +05:30
|
|
|
pos += sprintf(upbuf + pos, "%d min, ", upminutes);
|
2015-06-21 13:50:04 +05:30
|
|
|
|
|
|
|
users = count_users();
|
2015-07-01 17:17:30 +05:30
|
|
|
procps_loadavg(&av1, &av5, &av15);
|
2015-06-21 13:50:04 +05:30
|
|
|
|
2015-09-04 10:30:00 +05:30
|
|
|
pos += sprintf(upbuf + pos, "%2d %s, load average: %.2f, %.2f, %.2f",
|
|
|
|
users, users > 1 ? "users" : "user",
|
2015-06-29 17:39:59 +05:30
|
|
|
av1, av5, av15);
|
2015-06-21 13:50:04 +05:30
|
|
|
|
2015-06-23 17:52:50 +05:30
|
|
|
return upbuf;
|
2015-06-21 13:50:04 +05:30
|
|
|
}
|
|
|
|
|
2015-06-29 17:39:59 +05:30
|
|
|
/*
|
2015-06-29 21:46:52 +05:30
|
|
|
* procps_uptime_sprint_short:
|
2015-06-29 17:39:59 +05:30
|
|
|
*
|
|
|
|
* Print current time in nice format
|
|
|
|
*
|
|
|
|
* Returns a statically allocated buffer or NULL on error
|
|
|
|
*/
|
|
|
|
PROCPS_EXPORT char *procps_uptime_sprint_short(void)
|
2015-06-21 13:50:04 +05:30
|
|
|
{
|
|
|
|
int updecades, upyears, upweeks, updays, uphours, upminutes;
|
|
|
|
int pos = 3;
|
|
|
|
int comma = 0;
|
|
|
|
double uptime_secs, idle_secs;
|
|
|
|
|
2015-06-23 17:52:50 +05:30
|
|
|
shortbuf[0] = '\0';
|
2015-06-29 17:39:59 +05:30
|
|
|
if (procps_uptime(&uptime_secs, &idle_secs) < 0)
|
|
|
|
return shortbuf;
|
2015-06-21 13:50:04 +05:30
|
|
|
|
|
|
|
updecades = (int) uptime_secs / (60*60*24*365*10);
|
|
|
|
upyears = ((int) uptime_secs / (60*60*24*365)) % 10;
|
|
|
|
upweeks = ((int) uptime_secs / (60*60*24*7)) % 52;
|
|
|
|
updays = ((int) uptime_secs / (60*60*24)) % 7;
|
2015-09-04 10:30:00 +05:30
|
|
|
uphours = ((int) uptime_secs / (60*60)) % 24;
|
2015-06-21 13:50:04 +05:30
|
|
|
upminutes = ((int) uptime_secs / (60)) % 60;
|
|
|
|
|
|
|
|
strcat(shortbuf, "up ");
|
|
|
|
|
|
|
|
if (updecades) {
|
2015-06-29 17:39:59 +05:30
|
|
|
pos += sprintf(shortbuf + pos, "%d %s",
|
|
|
|
updecades, updecades > 1 ? "decades" : "decade");
|
2015-09-04 10:30:00 +05:30
|
|
|
comma += 1;
|
2015-06-21 13:50:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (upyears) {
|
2015-06-29 17:39:59 +05:30
|
|
|
pos += sprintf(shortbuf + pos, "%s%d %s",
|
|
|
|
comma > 0 ? ", " : "", upyears,
|
|
|
|
upyears > 1 ? "years" : "year");
|
|
|
|
comma += 1;
|
2015-06-21 13:50:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (upweeks) {
|
2015-06-29 17:39:59 +05:30
|
|
|
pos += sprintf(shortbuf + pos, "%s%d %s",
|
|
|
|
comma > 0 ? ", " : "", upweeks,
|
|
|
|
upweeks > 1 ? "weeks" : "week");
|
|
|
|
comma += 1;
|
2015-06-21 13:50:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (updays) {
|
2015-06-29 17:39:59 +05:30
|
|
|
pos += sprintf(shortbuf + pos, "%s%d %s",
|
|
|
|
comma > 0 ? ", " : "", updays,
|
|
|
|
updays > 1 ? "days" : "day");
|
|
|
|
comma += 1;
|
2015-06-21 13:50:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (uphours) {
|
2015-06-29 17:39:59 +05:30
|
|
|
pos += sprintf(shortbuf + pos, "%s%d %s",
|
|
|
|
comma > 0 ? ", " : "", uphours,
|
|
|
|
uphours > 1 ? "hours" : "hour");
|
|
|
|
comma += 1;
|
2015-06-21 13:50:04 +05:30
|
|
|
}
|
|
|
|
|
2017-09-25 04:06:51 +05:30
|
|
|
if (upminutes || (!upminutes && uptime_secs < 60)) {
|
2015-06-29 17:39:59 +05:30
|
|
|
pos += sprintf(shortbuf + pos, "%s%d %s",
|
2017-09-25 04:06:51 +05:30
|
|
|
comma > 0 ? ", " : "", upminutes,
|
|
|
|
upminutes != 1 ? "minutes" : "minute");
|
2015-06-29 17:39:59 +05:30
|
|
|
comma += 1;
|
2015-06-21 13:50:04 +05:30
|
|
|
}
|
|
|
|
return shortbuf;
|
|
|
|
}
|
|
|
|
|