1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-02-03 13:47:04 +03:00

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.
This commit is contained in:
Alexander Sverdlin 2015-03-29 20:44:04 +02:00 committed by Daniel Mack
parent 9964a9eb7b
commit 58ec01b35c
2 changed files with 21 additions and 4 deletions

View File

@ -330,9 +330,13 @@ schedstat_next:
/* ppid */
sprintf(filename, "%d/stat", pid);
fd = openat(procfd, filename, O_RDONLY);
st = fdopen(fd, "r");
if (!st)
if (fd == -1)
continue;
st = fdopen(fd, "r");
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);
ps->smaps = fdopen(fd, "r");
if (!ps->smaps)
if (fd == -1)
continue;
ps->smaps = fdopen(fd, "r");
if (!ps->smaps) {
close(fd);
continue;
}
setvbuf(ps->smaps, smaps_buf, _IOFBF, sizeof(smaps_buf));
}
else {

View File

@ -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",