#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>
}
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];
int c;
int p;
int mod;
- static int e_fd;
+ static int e_fd = -1;
ssize_t s;
ssize_t n;
struct dirent *ent;
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);
}
}
n = pread(vmstat, buf, sizeof(buf) - 1, 0);
if (n <= 0) {
close(vmstat);
+ vmstat = -1;
return;
}
buf[n] = '\0';
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);
}
}
n = pread(schedstat, buf, sizeof(buf) - 1, 0);
if (n <= 0) {
close(schedstat);
+ schedstat = -1;
return;
}
buf[n] = '\0';
}
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);
}
}
}
ps = ps->next_ps;
ps->pid = pid;
+ ps->sched = -1;
+ ps->schedstat = -1;
ps->sample = new0(struct ps_sched_struct, 1);
if (!ps->sample) {
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)
s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
if (s <= 0) {
close(ps->sched);
+ ps->sched = -1;
continue;
}
buf[s] = '\0';
/* 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;
}
* 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)
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;
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 {
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;