X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fbootchart%2Flog.c;h=ccec03f12cb18c6b85c49dcdc888df50d5d5ba07;hb=0ea9530d401827e299c6e04a433e69a7a2a89d80;hp=cf6c3a73f6bbe0214262d1c706de953550bd5d69;hpb=f2f85884caac671da84256acb44148df9a4dca70;p=elogind.git diff --git a/src/bootchart/log.c b/src/bootchart/log.c index cf6c3a73f..ccec03f12 100644 --- a/src/bootchart/log.c +++ b/src/bootchart/log.c @@ -57,7 +57,7 @@ double gettime_ns(void) void log_uptime(void) { - FILE *f; + FILE _cleanup_fclose_ *f = NULL; char str[32]; double uptime; @@ -65,11 +65,9 @@ void log_uptime(void) if (!f) return; - if (!fscanf(f, "%s %*s", str)) { - fclose(f); + if (!fscanf(f, "%s %*s", str)) return; - } - fclose(f); + uptime = strtod(str, NULL); log_start = gettime_ns(); @@ -95,12 +93,31 @@ static char *bufgetline(char *buf) return c; } +static int pid_cmdline_strncpy(char *buffer, int pid, size_t buf_len) { + char filename[PATH_MAX]; + int _cleanup_close_ fd=-1; + ssize_t n; + + sprintf(filename, "%d/cmdline", pid); + fd = openat(procfd, filename, O_RDONLY); + if (fd < 0) + return -errno; + + n = read(fd, buffer, buf_len-1); + if (n > 0) { + int i; + for (i = 0; i < n; i++) + if (buffer[i] == '\0') + buffer[i] = ' '; + buffer[n] = '\0'; + } + return 0; +} void log_sample(int sample) { static int vmstat; static int schedstat; - FILE *st; char buf[4095]; char key[256]; char val[256]; @@ -233,6 +250,7 @@ schedstat_next: /* end of our LL? then append a new record */ if (ps->pid != pid) { + FILE _cleanup_fclose_ *st = NULL; char t[32]; struct ps_struct *parent; @@ -244,7 +262,7 @@ schedstat_next: ps = ps->next_ps; ps->pid = pid; - ps->sample = calloc(len + 1, sizeof(struct ps_sched_struct)); + ps->sample = calloc(samples_len + 1, sizeof(struct ps_sched_struct)); if (!ps->sample) { perror("calloc(ps_struct)"); exit (EXIT_FAILURE); @@ -273,7 +291,12 @@ schedstat_next: if (!sscanf(buf, "%s %*s %*s", key)) continue; - strncpy(ps->name, key, 16); + strncpy(ps->name, key, 256); + + /* cmdline */ + if (show_cmdline) + pid_cmdline_strncpy(ps->name, pid, 256); + /* discard line 2 */ m = bufgetline(buf); if (!m) @@ -295,10 +318,8 @@ schedstat_next: if (!st) continue; if (!fscanf(st, "%*s %*s %*s %i", &p)) { - fclose(st); continue; } - fclose(st); ps->ppid = p; /* @@ -433,7 +454,11 @@ catch_rename: if (!sscanf(buf, "%s %*s %*s", key)) continue; - strncpy(ps->name, key, 16); + strncpy(ps->name, key, 256); + + /* cmdline */ + if (show_cmdline) + pid_cmdline_strncpy(ps->name, pid, 256); } } }