procps: Use sysinfo(2) to get uptime

Instead of trying to read /proc/uptime, which is not allowed on Android,
we patch procps to use the sysinfo(2) syscall to figure out the uptime.

This should fix at least "ps -o etime".
This commit is contained in:
Fredrik Fornwall 2019-08-07 22:00:54 +02:00
parent eed780953b
commit 000c2ec8fe
2 changed files with 40 additions and 35 deletions

View File

@ -2,7 +2,7 @@ TERMUX_PKG_HOMEPAGE=https://gitlab.com/procps-ng/procps
TERMUX_PKG_DESCRIPTION="Utilities that give information about processes using the /proc filesystem" TERMUX_PKG_DESCRIPTION="Utilities that give information about processes using the /proc filesystem"
TERMUX_PKG_LICENSE="LGPL-2.0" TERMUX_PKG_LICENSE="LGPL-2.0"
TERMUX_PKG_VERSION=3.3.15 TERMUX_PKG_VERSION=3.3.15
TERMUX_PKG_REVISION=4 TERMUX_PKG_REVISION=5
TERMUX_PKG_SRCURL=https://fossies.org/linux/misc/procps-ng-$TERMUX_PKG_VERSION.tar.xz TERMUX_PKG_SRCURL=https://fossies.org/linux/misc/procps-ng-$TERMUX_PKG_VERSION.tar.xz
TERMUX_PKG_SHA256=10bd744ffcb3de2d591d2f6acf1a54a7ba070fdcc432a855931a5057149f0465 TERMUX_PKG_SHA256=10bd744ffcb3de2d591d2f6acf1a54a7ba070fdcc432a855931a5057149f0465
TERMUX_PKG_DEPENDS="ncurses" TERMUX_PKG_DEPENDS="ncurses"

View File

@ -1,39 +1,44 @@
diff -uNr procps-ng-3.3.15/proc/sysinfo.c procps-ng-3.3.15.mod/proc/sysinfo.c diff -u -r ../procps-ng-3.3.15/proc/sysinfo.c ./proc/sysinfo.c
--- procps-ng-3.3.15/proc/sysinfo.c 2018-05-19 00:32:22.010979780 +0300 --- ../procps-ng-3.3.15/proc/sysinfo.c 2018-05-18 21:32:22.010979780 +0000
+++ procps-ng-3.3.15.mod/proc/sysinfo.c 2019-08-07 15:35:19.297799005 +0300 +++ ./proc/sysinfo.c 2019-08-07 19:53:05.528343190 +0000
@@ -105,19 +105,31 @@ @@ -33,6 +33,9 @@
#ifdef __CYGWIN__
#include <sys/param.h>
#endif
+#ifdef __ANDROID__
+#include <sys/sysinfo.h>
+#endif
#include "alloc.h"
#include "version.h"
#include "sysinfo.h" /* include self to verify prototypes */
@@ -104,6 +107,18 @@
/***********************************************************************/ /***********************************************************************/
int uptime(double *restrict uptime_secs, double *restrict idle_secs) { int uptime(double *restrict uptime_secs, double *restrict idle_secs) {
double up=0, idle=0; +#ifdef __ANDROID__
+#ifndef __ANDROID__ + /* Android does not allow read access to /proc/uptime */
char *savelocale; + SET_IF_DESIRED(idle_secs, 0.);
+#endif + struct sysinfo system_information;
+ + if (sysinfo(&system_information) == 0) {
+ if (uptime_fd == -1 && (uptime_fd = open(UPTIME_FILE, O_RDONLY)) == -1) { + SET_IF_DESIRED(uptime_secs, (double) system_information.uptime);
+ return 0; + return (double) system_information.uptime;
+ } else {
+ SET_IF_DESIRED(uptime_secs, 0.);
+ return 0;
+ } + }
+#else
double up=0, idle=0;
char *savelocale;
FILE_TO_BUF(UPTIME_FILE,uptime_fd); @@ -121,6 +136,7 @@
+#ifndef __ANDROID__
savelocale = strdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC,"C");
+#endif
if (sscanf(buf, "%lf %lf", &up, &idle) < 2) {
+#ifndef __ANDROID__
setlocale(LC_NUMERIC,savelocale);
free(savelocale);
+#endif
fputs("bad data in " UPTIME_FILE "\n", stderr);
return 0;
}
+#ifndef __ANDROID__
setlocale(LC_NUMERIC,savelocale);
free(savelocale);
+#endif
SET_IF_DESIRED(uptime_secs, up); SET_IF_DESIRED(uptime_secs, up);
SET_IF_DESIRED(idle_secs, idle); SET_IF_DESIRED(idle_secs, idle);
return up; /* assume never be zero seconds in practice */ return up; /* assume never be zero seconds in practice */
@@ -134,6 +146,7 @@ +#endif
}
unsigned long getbtime(void) {
@@ -134,6 +150,7 @@
/* /proc/stat can get very large on multi-CPU systems so we /* /proc/stat can get very large on multi-CPU systems so we
can't use FILE_TO_BUF */ can't use FILE_TO_BUF */
if (!(f = fopen(STAT_FILE, "r"))) { if (!(f = fopen(STAT_FILE, "r"))) {
@ -41,7 +46,7 @@ diff -uNr procps-ng-3.3.15/proc/sysinfo.c procps-ng-3.3.15.mod/proc/sysinfo.c
fputs(BAD_OPEN_MESSAGE, stderr); fputs(BAD_OPEN_MESSAGE, stderr);
fflush(NULL); fflush(NULL);
_exit(102); _exit(102);
@@ -193,7 +206,9 @@ @@ -193,7 +210,9 @@
double up_1, up_2, seconds; double up_1, up_2, seconds;
unsigned long long jiffies; unsigned long long jiffies;
unsigned h; unsigned h;
@ -51,7 +56,7 @@ diff -uNr procps-ng-3.3.15/proc/sysinfo.c procps-ng-3.3.15.mod/proc/sysinfo.c
long hz; long hz;
#ifdef _SC_CLK_TCK #ifdef _SC_CLK_TCK
@@ -204,8 +219,10 @@ @@ -204,8 +223,10 @@
#endif #endif
wait_j = hirq_j = sirq_j = stol_j = 0; wait_j = hirq_j = sirq_j = stol_j = 0;
@ -62,7 +67,7 @@ diff -uNr procps-ng-3.3.15/proc/sysinfo.c procps-ng-3.3.15.mod/proc/sysinfo.c
do{ do{
FILE_TO_BUF(UPTIME_FILE,uptime_fd); sscanf(buf, "%lf", &up_1); FILE_TO_BUF(UPTIME_FILE,uptime_fd); sscanf(buf, "%lf", &up_1);
/* uptime(&up_1, NULL); */ /* uptime(&up_1, NULL); */
@@ -214,8 +231,10 @@ @@ -214,8 +235,10 @@
FILE_TO_BUF(UPTIME_FILE,uptime_fd); sscanf(buf, "%lf", &up_2); FILE_TO_BUF(UPTIME_FILE,uptime_fd); sscanf(buf, "%lf", &up_2);
/* uptime(&up_2, NULL); */ /* uptime(&up_2, NULL); */
} while((long long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */ } while((long long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */
@ -73,7 +78,7 @@ diff -uNr procps-ng-3.3.15/proc/sysinfo.c procps-ng-3.3.15.mod/proc/sysinfo.c
jiffies = user_j + nice_j + sys_j + other_j + wait_j + hirq_j + sirq_j + stol_j ; jiffies = user_j + nice_j + sys_j + other_j + wait_j + hirq_j + sirq_j + stol_j ;
seconds = (up_1 + up_2) / 2; seconds = (up_1 + up_2) / 2;
h = (unsigned)( (double)jiffies/seconds/smp_num_cpus ); h = (unsigned)( (double)jiffies/seconds/smp_num_cpus );
@@ -445,18 +464,30 @@ @@ -445,18 +468,30 @@
/***********************************************************************/ /***********************************************************************/
void loadavg(double *restrict av1, double *restrict av5, double *restrict av15) { void loadavg(double *restrict av1, double *restrict av5, double *restrict av15) {
double avg_1=0, avg_5=0, avg_15=0; double avg_1=0, avg_5=0, avg_15=0;
@ -104,7 +109,7 @@ diff -uNr procps-ng-3.3.15/proc/sysinfo.c procps-ng-3.3.15.mod/proc/sysinfo.c
SET_IF_DESIRED(av1, avg_1); SET_IF_DESIRED(av1, avg_1);
SET_IF_DESIRED(av5, avg_5); SET_IF_DESIRED(av5, avg_5);
SET_IF_DESIRED(av15, avg_15); SET_IF_DESIRED(av15, avg_15);
@@ -791,27 +822,8 @@ @@ -791,27 +826,8 @@
mem_used = kb_main_total - kb_main_free; mem_used = kb_main_total - kb_main_free;
kb_main_used = (unsigned long)mem_used; kb_main_used = (unsigned long)mem_used;