[systemd-commits] 2 commits - src/bootchart

Daniel Mack zonque at kemper.freedesktop.org
Mon Mar 30 00:45:20 PDT 2015


 src/bootchart/store.c |   33 +++++++++++++++++++++++----------
 src/bootchart/svg.c   |    9 +++++++++
 2 files changed, 32 insertions(+), 10 deletions(-)

New commits:
commit c87664fef45fc1dadc2303675ed357e0dde61db9
Author: Alexander Sverdlin <alexander.sverdlin at gmail.com>
Date:   Sun Mar 29 20:46:42 2015 +0200

    systemd-bootchart: Repair Entropy Graph
    
    Entropy Graph code doesn't handle the error condition if open() of /proc entry
    fails. Moreover, the file is only opened once and only first sample will contain
    the correct value because the return value of pread() is also not handled
    properly and file is not re-opened. Fix both problems.

diff --git a/src/bootchart/store.c b/src/bootchart/store.c
index 8e9a62f..fb3dc9a 100644
--- a/src/bootchart/store.c
+++ b/src/bootchart/store.c
@@ -119,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;
@@ -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);
                 }
         }
 

commit 58ec01b35c046f5f167763343514c20170bfd2eb
Author: Alexander Sverdlin <alexander.sverdlin at gmail.com>
Date:   Sun Mar 29 20:44:04 2015 +0200

    systemd-bootchart: Prevent leaking file descriptors in open-fdopen combination
    
    Correctly handle the potential failure of fdopen() (because of OOM, for instance)
    after potentially successful open(). Prevent leaking open fd in such case.

diff --git a/src/bootchart/store.c b/src/bootchart/store.c
index 3900936..8e9a62f 100644
--- a/src/bootchart/store.c
+++ b/src/bootchart/store.c
@@ -330,9 +330,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;
                         }
@@ -432,9 +436,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 {
diff --git a/src/bootchart/svg.c b/src/bootchart/svg.c
index e1fc531..5412915 100644
--- a/src/bootchart/svg.c
+++ b/src/bootchart/svg.c
@@ -170,6 +170,9 @@ static void svg_title(const char *build) {
                 if (!fgets(cmdline, 255, f))
                         sprintf(cmdline, "Unknown");
                 fclose(f);
+        } else {
+                if (fd >= 0)
+                        close(fd);
         }
 
         /* extract root fs so we can find disk model name in sysfs */
@@ -185,6 +188,9 @@ static void svg_title(const char *build) {
                         if (!fgets(model, 255, f))
                                 fprintf(stderr, "Error reading disk model for %s\n", rootbdev);
                         fclose(f);
+                } else {
+                        if (fd >= 0)
+                                close(fd);
                 }
         }
 
@@ -208,6 +214,9 @@ static void svg_title(const char *build) {
                         }
                 }
                 fclose(f);
+        } else {
+                if (fd >= 0)
+                        close(fd);
         }
 
         svg("<text class=\"t1\" x=\"0\" y=\"30\">Bootchart for %s - %s</text>\n",



More information about the systemd-commits mailing list