chiark / gitweb /
bootchart: make bootchart work from within the initrd
[elogind.git] / src / bootchart / bootchart.c
index 3d77bab129f5565c7bdb0f38fdcd027a78b81b0f..fb95c6b807351c46a7631978c929e8b7d2be97eb 100644 (file)
@@ -1,17 +1,24 @@
-/*
- * bootchart.c
- *
- * Copyright (C) 2009-2012 Intel Coproration
- *
- * Authors:
- *   Auke Kok <auke-jan.h.kok@intel.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; version 2
- * of the License.
- */
+/***
+  bootchart.c - This file is part of systemd-bootchart
 
+  Copyright (C) 2009-2013 Intel Coproration
+
+  Authors:
+    Auke Kok <auke-jan.h.kok@intel.com>
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+ ***/
 
 #include <sys/time.h>
 #include <sys/types.h>
@@ -28,6 +35,7 @@
 
 
 #include "bootchart.h"
+#include "util.h"
 
 double graph_start;
 double log_start;
@@ -39,7 +47,7 @@ struct cpu_stat_struct cpustat[MAXCPUS];
 int pscount;
 int cpus;
 double interval;
-FILE *of;
+FILE *of = NULL;
 int overrun = 0;
 static int exiting = 0;
 
@@ -56,7 +64,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;
 
@@ -74,13 +82,11 @@ int main(int argc, char *argv[])
         struct ps_struct *ps;
         char output_file[PATH_MAX];
         char datestr[200];
-        time_t t;
+        time_t t = 0;
         FILE *f;
         int gind;
         int i;
 
-        memset(&t, 0, sizeof(time_t));
-
         rlim.rlim_cur = 4096;
         rlim.rlim_max = 4096;
         (void) setrlimit(RLIMIT_NOFILE, &rlim);
@@ -109,25 +115,25 @@ int main(int argc, char *argv[])
 
                         // todo: filter leading/trailing whitespace
 
-                        if (!strcmp(key, "samples"))
+                        if (streq(key, "samples"))
                                 len = atoi(val);
-                        if (!strcmp(key, "freq"))
+                        if (streq(key, "freq"))
                                 hz = atof(val);
-                        if (!strcmp(key, "rel"))
+                        if (streq(key, "rel"))
                                 relative = atoi(val);
-                        if (!strcmp(key, "filter"))
+                        if (streq(key, "filter"))
                                 filter = atoi(val);
-                        if (!strcmp(key, "pss"))
+                        if (streq(key, "pss"))
                                 pss = atoi(val);
-                        if (!strcmp(key, "output"))
+                        if (streq(key, "output"))
                                 strncpy(output_path, val, PATH_MAX - 1);
-                        if (!strcmp(key, "init"))
+                        if (streq(key, "init"))
                                 strncpy(init_path, val, PATH_MAX - 1);
-                        if (!strcmp(key, "scale_x"))
+                        if (streq(key, "scale_x"))
                                 scale_x = atof(val);
-                        if (!strcmp(key, "scale_y"))
+                        if (streq(key, "scale_y"))
                                 scale_y = atof(val);
-                        if (!strcmp(key, "entropy"))
+                        if (streq(key, "entropy"))
                                 entropy = atoi(val);
                 }
                 fclose(f);
@@ -229,14 +235,14 @@ int main(int argc, char *argv[])
                         execl(init_path, init_path, NULL);
                 }
         }
+       argv[0][0] = '@';
 
         /* start with empty ps LL */
-        ps_first = malloc(sizeof(struct ps_struct));
+        ps_first = calloc(1, sizeof(struct ps_struct));
         if (!ps_first) {
-                perror("malloc(ps_struct)");
+                perror("calloc(ps_struct)");
                 exit(EXIT_FAILURE);
         }
-        memset(ps_first, 0, sizeof(struct ps_struct));
 
         /* handle TERM/INT nicely */
         memset(&sig, 0, sizeof(struct sigaction));
@@ -259,6 +265,14 @@ 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");
+                }
+
+
                 /* wait for /proc to become available, discarding samples */
                 if (!(graph_start > 0.0))
                         log_uptime();
@@ -318,11 +332,13 @@ int main(int argc, char *argv[])
         }
         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);