diff options
author | Daniel Mack <daniel@zonque.org> | 2015-04-02 13:43:18 +0200 |
---|---|---|
committer | Daniel Mack <daniel@zonque.org> | 2015-04-03 15:29:18 +0200 |
commit | 34a4071e998f327945993ea6e6cbcaa0292b4093 (patch) | |
tree | f5d850771de78559cfbfc396fa65aaaf5d34ca0d /src/bootchart/store.c | |
parent | 039958632036808f9baa207e72b9827fa97b7a10 (diff) |
bootchart: clean up control flow logic
Don't blindly exit() from random functions, but return a proper error
and upchain error conditions.
squash! bootchart: clean up control flow logic
When pread() returns "0", it's a read failure, so don't make the caller think
log_sample() was successful, return meaningful error code instead of 0.
Diffstat (limited to 'src/bootchart/store.c')
-rw-r--r-- | src/bootchart/store.c | 53 |
1 files changed, 25 insertions, 28 deletions
diff --git a/src/bootchart/store.c b/src/bootchart/store.c index 0663e100e8..53b827fe59 100644 --- a/src/bootchart/store.c +++ b/src/bootchart/store.c @@ -107,7 +107,7 @@ static int pid_cmdline_strscpy(char *buffer, size_t buf_len, int pid) { return 0; } -void log_sample(int sample, struct list_sample_data **ptr) { +int log_sample(int sample, struct list_sample_data **ptr) { static int vmstat = -1; static int schedstat = -1; char buf[4096]; @@ -134,7 +134,7 @@ void log_sample(int sample, struct list_sample_data **ptr) { /* find all processes */ proc = opendir("/proc"); if (!proc) - return; + return -errno; procfd = dirfd(proc); } else { rewinddir(proc); @@ -149,9 +149,10 @@ 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; + vmstat = safe_close(vmstat); + if (n < 0) + return -errno; + return -ENODATA; } buf[n] = '\0'; @@ -174,17 +175,16 @@ vmstat_next: if (schedstat < 0) { /* overall CPU utilization */ schedstat = openat(procfd, "schedstat", O_RDONLY); - if (schedstat == -1) { - log_error_errno(errno, "Failed to open /proc/schedstat (requires CONFIG_SCHEDSTATS=y in kernel config): %m"); - exit(EXIT_FAILURE); - } + if (schedstat == -1) + return log_error_errno(errno, "Failed to open /proc/schedstat (requires CONFIG_SCHEDSTATS=y in kernel config): %m"); } n = pread(schedstat, buf, sizeof(buf) - 1, 0); if (n <= 0) { - close(schedstat); - schedstat = -1; - return; + schedstat = safe_close(schedstat); + if (n < 0) + return -errno; + return -ENODATA; } buf[n] = '\0'; @@ -215,10 +215,8 @@ schedstat_next: if (arg_entropy) { 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 == -1) + return log_error_errno(errno, "Failed to open /proc/sys/kernel/random/entropy_avail: %m"); } n = pread(e_fd, buf, sizeof(buf) - 1, 0); @@ -259,20 +257,18 @@ schedstat_next: int r; ps->next_ps = new0(struct ps_struct, 1); - if (!ps->next_ps) { - log_oom(); - exit (EXIT_FAILURE); - } + if (!ps->next_ps) + return log_oom(); + ps = ps->next_ps; ps->pid = pid; ps->sched = -1; ps->schedstat = -1; ps->sample = new0(struct ps_sched_struct, 1); - if (!ps->sample) { - log_oom(); - exit (EXIT_FAILURE); - } + if (!ps->sample) + return log_oom(); + ps->sample->sampledata = sampledata; pscount++; @@ -414,10 +410,9 @@ schedstat_next: continue; ps->sample->next = new0(struct ps_sched_struct, 1); - if (!ps->sample->next) { - log_oom(); - exit(EXIT_FAILURE); - } + if (!ps->sample->next) + return log_oom(); + ps->sample->next->prev = ps->sample; ps->sample = ps->sample->next; ps->last = ps->sample; @@ -528,4 +523,6 @@ catch_rename: pid_cmdline_strscpy(ps->name, sizeof(ps->name), pid); } } + + return 0; } |