chiark / gitweb /
mount: don't run quotaon only for network filesystems
[elogind.git] / src / bootchart / store.c
index 9ea1b27de4b1666614170b91d55a8f212bb4ca11..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>
@@ -110,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];
@@ -121,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;
@@ -142,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);
                 }
         }
@@ -154,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';
@@ -174,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);
                 }
         }
@@ -186,6 +185,7 @@ vmstat_next:
         n = pread(schedstat, buf, sizeof(buf) - 1, 0);
         if (n <= 0) {
                 close(schedstat);
+                schedstat = -1;
                 return;
         }
         buf[n] = '\0';
@@ -215,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);
                 }
         }
 
@@ -262,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) {
@@ -278,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)
@@ -288,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';
@@ -327,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;
                         }
@@ -379,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)
@@ -389,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;
@@ -426,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 {
@@ -490,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;