procfs: implement reading uptime 85/180285/5
authorLukasz Stanislawski <l.stanislaws@samsung.com>
Mon, 14 May 2018 12:52:41 +0000 (14:52 +0200)
committerLukasz Stanislawski <l.stanislaws@samsung.com>
Tue, 12 Jun 2018 10:07:17 +0000 (10:07 +0000)
Change-Id: Ibed295319c087196cc551f1cbb1d2384465d2c61

src/procfs.c
src/procfs.h

index 346a151..95478a2 100644 (file)
@@ -21,6 +21,7 @@
 #include "err-check.h"
 
 #define LOADAVG_FILEPATH "/proc/loadavg"
+#define UPTIME_FILEPATH  "/proc/uptime"
 
 int procfs_read_system_load_average(struct procfs_load_average_info *info)
 {
@@ -74,9 +75,28 @@ int procfs_read_process_cpu_usage(int pid, struct procfs_process_cpu_usage_info
        return -1;
 }
 
-int procfs_read_uptime(int *uptime)
+int procfs_read_uptime(unsigned long *uptime)
 {
-       return -1;
+       ON_NULL_RETURN_VAL(uptime, -1);
+
+       double duptime;
+
+       FILE *uptime_fp = fopen(UPTIME_FILEPATH, "r");
+       if (!uptime_fp) {
+               ERR("failed to open " UPTIME_FILEPATH);
+               return -1;
+       }
+
+       if (fscanf(uptime_fp, "%lf ", &duptime) != 1) {
+               ERR("failed to read " UPTIME_FILEPATH);
+               fclose(uptime_fp);
+               return -1;
+       }
+
+       fclose(uptime_fp);
+       *uptime = (unsigned long)duptime;
+
+       return 0;
 }
 
 int procfs_read_cpu_count(int *cpu_count)
index 734bd23..acedc9f 100644 (file)
@@ -137,7 +137,7 @@ int procfs_read_process_cpu_usage(int pid, struct procfs_process_cpu_usage_info
  * @param[out] uptime Number of seconds after boot
  * @return: 0 on success, other value on error
  */
-int procfs_read_uptime(int *uptime);
+int procfs_read_uptime(unsigned long *uptime);
 
 /**
  * @brief Parses information from /sys/devices/system/cpu/possible