c8e329ec3c37e624712c2eee14a9346e0188d758
[busybox-power] / debian / patches / hotfixes / busybox-1.20.1-ps.patch
1 --- busybox-1.20.1/procps/ps.c
2 +++ busybox-1.20.1-ps/procps/ps.c
3 @@ -69,6 +69,31 @@
4  /* Absolute maximum on output line length */
5  enum { MAX_WIDTH = 2*1024 };
6  
7 +#if ENABLE_FEATURE_PS_TIME || ENABLE_FEATURE_PS_LONG
8 +static long get_uptime(void)
9 +{
10 +#ifdef __linux__
11 +       struct sysinfo info;
12 +       if (sysinfo(&info) < 0)
13 +               return 0;
14 +       return info.uptime;
15 +#elif 1
16 +       char buf[64];
17 +       long uptime;
18 +       if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0)
19 +               bb_perror_msg_and_die("can't read %s", "/proc/uptime");
20 +       buf[sizeof(buf)-1] = '\0';
21 +       sscanf(buf, "%l", &uptime);
22 +       return uptime;
23 +#else
24 +       struct timespec ts;
25 +       if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
26 +               return 0;
27 +       return ts.tv_sec;
28 +#endif
29 +}
30 +#endif
31 +
32  #if ENABLE_DESKTOP
33  
34  #include <sys/times.h> /* for times() */
35 @@ -197,8 +222,6 @@ static inline unsigned get_HZ_by_waiting
36  
37  static unsigned get_kernel_HZ(void)
38  {
39 -       //char buf[64];
40 -       struct sysinfo info;
41  
42         if (kernel_HZ)
43                 return kernel_HZ;
44 @@ -208,12 +231,7 @@ static unsigned get_kernel_HZ(void)
45         if (kernel_HZ == (unsigned)-1)
46                 kernel_HZ = get_HZ_by_waiting();
47  
48 -       //if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0)
49 -       //      bb_perror_msg_and_die("can't read %s", "/proc/uptime");
50 -       //buf[sizeof(buf)-1] = '\0';
51 -       ///sscanf(buf, "%llu", &seconds_since_boot);
52 -       sysinfo(&info);
53 -       seconds_since_boot = info.uptime;
54 +       seconds_since_boot = get_uptime();
55  
56         return kernel_HZ;
57  }
58 @@ -635,7 +653,7 @@ int ps_main(int argc UNUSED_PARAM, char 
59         };
60  #if ENABLE_FEATURE_PS_LONG
61         time_t now = now;
62 -       struct sysinfo info;
63 +       long uptime;
64  #endif
65         int opts = 0;
66         /* If we support any options, parse argv */
67 @@ -695,7 +713,7 @@ int ps_main(int argc UNUSED_PARAM, char 
68                 puts("S   UID   PID  PPID   VSZ   RSS TTY   STIME TIME     CMD");
69  #if ENABLE_FEATURE_PS_LONG
70                 now = time(NULL);
71 -               sysinfo(&info);
72 +               uptime = get_uptime();
73  #endif
74         }
75         else {
76 @@ -727,7 +745,7 @@ int ps_main(int argc UNUSED_PARAM, char 
77                                 char tty[2 * sizeof(int)*3 + 2];
78                                 char *endp;
79                                 unsigned sut = (p->stime + p->utime) / 100;
80 -                               unsigned elapsed = info.uptime - (p->start_time / 100);
81 +                               unsigned elapsed = uptime - (p->start_time / 100);
82                                 time_t start = now - elapsed;
83                                 struct tm *tm = localtime(&start);
84