chiark / gitweb /
bootchart: parse /etc/os-release rather than system-release
[elogind.git] / src / bootchart / bootchart.c
index 6b9252dbb982043de65983ce76c85e369b9519a5..7affacfdbfebb01e685d1584ec18a464a75bc627 100644 (file)
@@ -23,6 +23,7 @@
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/resource.h>
+#include <sys/stat.h>
 #include <stdio.h>
 #include <signal.h>
 #include <stdlib.h>
 #include <getopt.h>
 #include <limits.h>
 #include <errno.h>
+#include <fcntl.h>
 
 
 #include "bootchart.h"
 #include "util.h"
+#include "fileio.h"
 
 double graph_start;
 double log_start;
@@ -47,9 +50,10 @@ struct cpu_stat_struct cpustat[MAXCPUS];
 int pscount;
 int cpus;
 double interval;
-FILE *of;
+FILE *of = NULL;
 int overrun = 0;
 static int exiting = 0;
+int sysfd=-1;
 
 /* graph defaults */
 int entropy = 0;
@@ -64,7 +68,7 @@ double scale_x = 100.0; /* 100px = 1sec */
 double scale_y = 20.0;  /* 16px = 1 process bar */
 
 char init_path[PATH_MAX] = "/sbin/init";
-char output_path[PATH_MAX] = "/var/log";
+char output_path[PATH_MAX] = "/run/log";
 
 static struct rlimit rlim;
 
@@ -78,6 +82,7 @@ static void signal_handler(int sig)
 
 int main(int argc, char *argv[])
 {
+        _cleanup_free_ char *build = NULL;
         struct sigaction sig;
         struct ps_struct *ps;
         char output_file[PATH_MAX];
@@ -235,6 +240,7 @@ int main(int argc, char *argv[])
                         execl(init_path, init_path, NULL);
                 }
         }
+       argv[0][0] = '@';
 
         /* start with empty ps LL */
         ps_first = calloc(1, sizeof(struct ps_struct));
@@ -264,6 +270,23 @@ int main(int argc, char *argv[])
 
                 sampletime[samples] = gettime_ns();
 
+                if (!of && (access(output_path, R_OK|W_OK|X_OK) == 0)) {
+                        t = time(NULL);
+                        strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
+                        snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", output_path, datestr);
+                        of = fopen(output_file, "w");
+                }
+
+                if (sysfd < 0) {
+                        sysfd = open("/sys", O_RDONLY);
+                }
+
+                if (!build) {
+                        parse_env_file("/etc/os-release", NEWLINE,
+                                       "PRETTY_NAME", &build,
+                                       NULL);
+                }
+
                 /* wait for /proc to become available, discarding samples */
                 if (!(graph_start > 0.0))
                         log_uptime();
@@ -321,23 +344,27 @@ int main(int argc, char *argv[])
                 if (ps->smaps)
                         fclose(ps->smaps);
         }
-        closedir(proc);
 
-        t = time(NULL);
-        strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
-        snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", output_path, datestr);
+        if (!of) {
+                t = time(NULL);
+                strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
+                snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", output_path, datestr);
+                of = fopen(output_file, "w");
+        }
 
-        of = fopen(output_file, "w");
         if (!of) {
                 perror("open output_file");
                 exit (EXIT_FAILURE);
         }
 
-        svg_do();
+        svg_do(build);
 
         fprintf(stderr, "bootchartd: Wrote %s\n", output_file);
         fclose(of);
 
+        closedir(proc);
+        close(sysfd);
+
         /* nitpic cleanups */
         ps = ps_first;
         while (ps->next_ps) {