chiark / gitweb /
mount: don't run quotaon only for network filesystems
[elogind.git] / src / bootchart / store.c
index e0719838d5089a6e369ae546715335fc4b4c91c2..fb3dc9ad6e6cb138a2b614f4f41055cc63c46cce 100644 (file)
@@ -25,8 +25,6 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <limits.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <stdio.h>
 #include <string.h>
 #include <dirent.h>
@@ -34,6 +32,7 @@
 #include <time.h>
 
 #include "util.h"
+#include "time-util.h"
 #include "strxcpyx.h"
 #include "store.h"
 #include "bootchart.h"
@@ -54,30 +53,25 @@ double gettime_ns(void) {
 
         clock_gettime(CLOCK_MONOTONIC, &n);
 
-        return (n.tv_sec + (n.tv_nsec / 1000000000.0));
+        return (n.tv_sec + (n.tv_nsec / (double) NSEC_PER_SEC));
 }
 
-void log_uptime(void) {
-        _cleanup_fclose_ FILE *f = NULL;
-        char str[32];
-        double uptime;
-
-        f = fopen("/proc/uptime", "re");
-
-        if (!f)
-                return;
-        if (!fscanf(f, "%s %*s", str))
-                return;
-
-        uptime = strtod(str, NULL);
+static double gettime_up(void) {
+        struct timespec n;
 
-        log_start = gettime_ns();
+        clock_gettime(CLOCK_BOOTTIME, &n);
+        return (n.tv_sec + (n.tv_nsec / (double) NSEC_PER_SEC));
+}
 
-        /* start graph at kernel boot time */
+void log_uptime(void) {
         if (arg_relative)
-                graph_start = log_start;
-        else
+                graph_start = log_start = gettime_ns();
+        else {
+                double uptime = gettime_up();
+
+                log_start = gettime_ns();
                 graph_start = log_start - uptime;
+        }
 }
 
 static char *bufgetline(char *buf) {
@@ -114,8 +108,8 @@ static int pid_cmdline_strscpy(char *buffer, size_t buf_len, int pid) {
 }
 
 void log_sample(int sample, struct list_sample_data **ptr) {
-        static int vmstat;
-        static int schedstat;
+        static int vmstat = -1;
+        static int schedstat = -1;
         char buf[4096];
         char key[256];
         char val[256];
@@ -125,7 +119,7 @@ void log_sample(int sample, struct list_sample_data **ptr) {
         int c;
         int p;
         int mod;
-        static int e_fd;
+        static int e_fd = -1;
         ssize_t s;
         ssize_t n;
         struct dirent *ent;
@@ -146,11 +140,11 @@ void log_sample(int sample, struct list_sample_data **ptr) {
                 rewinddir(proc);
         }
 
-        if (!vmstat) {
+        if (vmstat < 0) {
                 /* block stuff */
                 vmstat = openat(procfd, "vmstat", O_RDONLY);
                 if (vmstat == -1) {
-                        log_error("Failed to open /proc/vmstat: %m");
+                        log_error_errno(errno, "Failed to open /proc/vmstat: %m");
                         exit(EXIT_FAILURE);
                 }
         }
@@ -158,6 +152,7 @@ void log_sample(int sample, struct list_sample_data **ptr) {
         n = pread(vmstat, buf, sizeof(buf) - 1, 0);
         if (n <= 0) {
                 close(vmstat);
+                vmstat = -1;
                 return;
         }
         buf[n] = '\0';
@@ -178,11 +173,11 @@ vmstat_next:
                         break;
         }
 
-        if (!schedstat) {
+        if (schedstat < 0) {
                 /* overall CPU utilization */
                 schedstat = openat(procfd, "schedstat", O_RDONLY);
                 if (schedstat == -1) {
-                        log_error("Failed to open /proc/schedstat: %m");
+                        log_error_errno(errno, "Failed to open /proc/schedstat (requires CONFIG_SCHEDSTATS=y in kernel config): %m");
                         exit(EXIT_FAILURE);
                 }
         }
@@ -190,18 +185,21 @@ vmstat_next:
         n = pread(schedstat, buf, sizeof(buf) - 1, 0);
         if (n <= 0) {
                 close(schedstat);
+                schedstat = -1;
                 return;
         }
         buf[n] = '\0';
 
         m = buf;
         while (m) {
+                int r;
+
                 if (sscanf(m, "%s %*s %*s %*s %*s %*s %*s %s %s", key, rt, wt) < 3)
                         goto schedstat_next;
 
                 if (strstr(key, "cpu")) {
-                        c = atoi((const char*)(key+3));
-                        if (c > MAXCPUS)
+                        r = safe_atoi((const char*)(key+3), &c);
+                        if (r < 0 || c > MAXCPUS -1)
                                 /* Oops, we only have room for MAXCPUS data */
                                 break;
                         sampledata->runtime[c] = atoll(rt);
@@ -217,16 +215,21 @@ schedstat_next:
         }
 
         if (arg_entropy) {
-                if (!e_fd) {
+                if (e_fd < 0) {
                         e_fd = openat(procfd, "sys/kernel/random/entropy_avail", O_RDONLY);
+                        if (e_fd == -1) {
+                                log_error_errno(errno, "Failed to open /proc/sys/kernel/random/entropy_avail: %m");
+                                exit(EXIT_FAILURE);
+                        }
                 }
 
-                if (e_fd) {
-                        n = pread(e_fd, buf, sizeof(buf) - 1, 0);
-                        if (n > 0) {
-                                buf[n] = '\0';
-                                sampledata->entropy_avail = atoi(buf);
-                        }
+                n = pread(e_fd, buf, sizeof(buf) - 1, 0);
+                if (n <= 0) {
+                        close(e_fd);
+                        e_fd = -1;
+                } else {
+                        buf[n] = '\0';
+                        sampledata->entropy_avail = atoi(buf);
                 }
         }
 
@@ -255,6 +258,7 @@ schedstat_next:
                         _cleanup_fclose_ FILE *st = NULL;
                         char t[32];
                         struct ps_struct *parent;
+                        int r;
 
                         ps->next_ps = new0(struct ps_struct, 1);
                         if (!ps->next_ps) {
@@ -263,6 +267,8 @@ schedstat_next:
                         }
                         ps = ps->next_ps;
                         ps->pid = pid;
+                        ps->sched = -1;
+                        ps->schedstat = -1;
 
                         ps->sample = new0(struct ps_sched_struct, 1);
                         if (!ps->sample) {
@@ -279,7 +285,7 @@ schedstat_next:
                         ps->sample->waittime = atoll(wt);
 
                         /* get name, start time */
-                        if (!ps->sched) {
+                        if (ps->sched < 0) {
                                 sprintf(filename, "%d/sched", pid);
                                 ps->sched = openat(procfd, filename, O_RDONLY);
                                 if (ps->sched == -1)
@@ -289,6 +295,7 @@ schedstat_next:
                         s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
                         if (s <= 0) {
                                 close(ps->sched);
+                                ps->sched = -1;
                                 continue;
                         }
                         buf[s] = '\0';
@@ -314,7 +321,11 @@ schedstat_next:
                         if (!sscanf(m, "%*s %*s %s", t))
                                 continue;
 
-                        ps->starttime = strtod(t, NULL) / 1000.0;
+                        r = safe_atod(t, &ps->starttime);
+                        if (r < 0)
+                                continue;
+
+                        ps->starttime /= 1000.0;
 
                         if (arg_show_cgroup)
                                 /* if this fails, that's OK */
@@ -324,9 +335,13 @@ schedstat_next:
                         /* ppid */
                         sprintf(filename, "%d/stat", pid);
                         fd = openat(procfd, filename, O_RDONLY);
+                        if (fd == -1)
+                                continue;
                         st = fdopen(fd, "r");
-                        if (!st)
+                        if (!st) {
+                                close(fd);
                                 continue;
+                        }
                         if (!fscanf(st, "%*s %*s %*s %i", &p)) {
                                 continue;
                         }
@@ -376,7 +391,7 @@ schedstat_next:
                  * iteration */
 
                 /* rt, wt */
-                if (!ps->schedstat) {
+                if (ps->schedstat < 0) {
                         sprintf(filename, "%d/schedstat", pid);
                         ps->schedstat = openat(procfd, filename, O_RDONLY);
                         if (ps->schedstat == -1)
@@ -386,8 +401,11 @@ schedstat_next:
                 if (s <= 0) {
                         /* clean up our file descriptors - assume that the process exited */
                         close(ps->schedstat);
-                        if (ps->sched)
+                        ps->schedstat = -1;
+                        if (ps->sched) {
                                 close(ps->sched);
+                                ps->sched = -1;
+                        }
                         //if (ps->smaps)
                         //        fclose(ps->smaps);
                         continue;
@@ -398,7 +416,7 @@ schedstat_next:
                         continue;
 
                 ps->sample->next = new0(struct ps_sched_struct, 1);
-                if (!ps->sample) {
+                if (!ps->sample->next) {
                         log_oom();
                         exit(EXIT_FAILURE);
                 }
@@ -423,9 +441,13 @@ schedstat_next:
                 if (!ps->smaps) {
                         sprintf(filename, "%d/smaps", pid);
                         fd = openat(procfd, filename, O_RDONLY);
+                        if (fd == -1)
+                                continue;
                         ps->smaps = fdopen(fd, "r");
-                        if (!ps->smaps)
+                        if (!ps->smaps) {
+                                close(fd);
                                 continue;
+                        }
                         setvbuf(ps->smaps, smaps_buf, _IOFBF, sizeof(smaps_buf));
                 }
                 else {
@@ -487,8 +509,11 @@ catch_rename:
                         if (s <= 0) {
                                 /* clean up file descriptors */
                                 close(ps->sched);
-                                if (ps->schedstat)
+                                ps->sched = -1;
+                                if (ps->schedstat) {
                                         close(ps->schedstat);
+                                        ps->schedstat = -1;
+                                }
                                 //if (ps->smaps)
                                 //        fclose(ps->smaps);
                                 continue;