Bump version to 1.32.0
[oweals/busybox.git] / init / bootchartd.c
index b3e08af92a6c1c6355e046672dc0b493ce239804..750f673560ef584bc8eb80fdfd7eb910d825e054 100644 (file)
@@ -1,56 +1,69 @@
 /* vi: set sw=4 ts=4: */
 /*
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
-
 //config:config BOOTCHARTD
-//config:      bool "bootchartd"
+//config:      bool "bootchartd (10 kb)"
 //config:      default y
 //config:      help
-//config:        bootchartd is commonly used to profile the boot process
-//config:        for the purpose of speeding it up. In this case, it is started
-//config:        by the kernel as the init process. This is configured by adding
-//config:        the init=/sbin/bootchartd option to the kernel command line.
+//config:      bootchartd is commonly used to profile the boot process
+//config:      for the purpose of speeding it up. In this case, it is started
+//config:      by the kernel as the init process. This is configured by adding
+//config:      the init=/sbin/bootchartd option to the kernel command line.
 //config:
-//config:        It can also be used to monitor the resource usage of a specific
-//config:        application or the running system in general. In this case,
-//config:        bootchartd is started interactively by running bootchartd start
-//config:        and stopped using bootchartd stop.
+//config:      It can also be used to monitor the resource usage of a specific
+//config:      application or the running system in general. In this case,
+//config:      bootchartd is started interactively by running bootchartd start
+//config:      and stopped using bootchartd stop.
 //config:
 //config:config FEATURE_BOOTCHARTD_BLOATED_HEADER
-//config:      bool "bootchartd"
+//config:      bool "Compatible, bloated header"
 //config:      default y
 //config:      depends on BOOTCHARTD
 //config:      help
-//config:        Create extended header file compatible with "big" bootchartd.
-//config:        "Big" bootchartd is a shell script and it dumps some
-//config:        "convenient" info int the header, such as:
-//config:          title = Boot chart for `hostname` (`date`)
-//config:          system.uname = `uname -srvm`
-//config:          system.release = `cat /etc/DISTRO-release`
-//config:          system.cpu = `grep '^model name' /proc/cpuinfo | head -1` ($cpucount)
-//config:          system.kernel.options = `cat /proc/cmdline`
-//config:        This data is not mandatory for bootchart graph generation,
-//config:        and is considered bloat. Nevertheless, this option
-//config:        makes bootchartd applet to dump a subset of it.
+//config:      Create extended header file compatible with "big" bootchartd.
+//config:      "Big" bootchartd is a shell script and it dumps some
+//config:      "convenient" info into the header, such as:
+//config:              title = Boot chart for `hostname` (`date`)
+//config:              system.uname = `uname -srvm`
+//config:              system.release = `cat /etc/DISTRO-release`
+//config:              system.cpu = `grep '^model name' /proc/cpuinfo | head -1` ($cpucount)
+//config:              system.kernel.options = `cat /proc/cmdline`
+//config:      This data is not mandatory for bootchart graph generation,
+//config:      and is considered bloat. Nevertheless, this option
+//config:      makes bootchartd applet to dump a subset of it.
 //config:
 //config:config FEATURE_BOOTCHARTD_CONFIG_FILE
-//config:      bool "bootchartd"
+//config:      bool "Support bootchartd.conf"
 //config:      default y
 //config:      depends on BOOTCHARTD
 //config:      help
-//config:        Enable reading and parsing of $PWD/bootchartd.conf
-//config:        and /etc/bootchartd.conf files.
+//config:      Enable reading and parsing of $PWD/bootchartd.conf
+//config:      and /etc/bootchartd.conf files.
+
+//applet:IF_BOOTCHARTD(APPLET(bootchartd, BB_DIR_SBIN, BB_SUID_DROP))
+
+//kbuild:lib-$(CONFIG_BOOTCHARTD) += bootchartd.o
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 /* After libbb.h, since it needs sys/types.h on some systems */
 #include <sys/utsname.h>
-#include <sys/mount.h>
-#ifndef MS_SILENT
-# define MS_SILENT      (1 << 15)
+
+#ifdef __linux__
+# include <sys/mount.h>
+# ifndef MS_SILENT
+#  define MS_SILENT      (1 << 15)
+# endif
+# ifndef MNT_DETACH
+#  define MNT_DETACH 0x00000002
+# endif
 #endif
-#ifndef MNT_DETACH
-# define MNT_DETACH 0x00000002
+
+#if !ENABLE_TAR && !ENABLE_WERROR
+# warning Note: bootchartd requires tar command, but you did not select it.
+#elif !ENABLE_FEATURE_SEAMLESS_GZ && !ENABLE_WERROR
+# warning Note: bootchartd requires tar -z support, but you did not select it.
 #endif
 
 #define BC_VERSION_STR "0.8"
 struct globals {
        char jiffy_line[COMMON_BUFSIZE];
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
-#define INIT_G() do { } while (0)
+#define G (*(struct globals*)bb_common_bufsiz1)
+#define INIT_G() do { setup_common_bufsiz(); } while (0)
 
 static void dump_file(FILE *fp, const char *filename)
 {
@@ -157,7 +170,9 @@ static int dump_procs(FILE *fp, int look_for_login_process)
                        p++;
                        strchrnul(p, ')')[0] = '\0';
                        /* Is it gdm, kdm or a getty? */
-                       if (((p[0] == 'g' || p[0] == 'k' || p[0] == 'x') && p[1] == 'd' && p[2] == 'm')
+                       if (((p[0] == 'g' || p[0] == 'k' || p[0] == 'x')
+                            && p[1] == 'd' && p[2] == 'm' && p[3] == '\0'
+                           )
                         || strstr(p, "getty")
                        ) {
                                found_login_process = 1;
@@ -174,12 +189,13 @@ static char *make_tempdir(void)
        char template[] = "/tmp/bootchart.XXXXXX";
        char *tempdir = xstrdup(mkdtemp(template));
        if (!tempdir) {
+#ifdef __linux__
                /* /tmp is not writable (happens when we are used as init).
-                * Try to mount a tmpfs, them cd and lazily unmount it.
+                * Try to mount a tmpfs, then cd and lazily unmount it.
                 * Since we unmount it at once, we can mount it anywhere.
                 * Try a few locations which are likely ti exist.
                 */
-               static const char dirs[] = "/mnt\0""/tmp\0""/boot\0""/proc\0";
+               static const char dirs[] ALIGN1 = "/mnt\0""/tmp\0""/boot\0""/proc\0";
                const char *try_dir = dirs;
                while (mount("none", try_dir, "tmpfs", MS_SILENT, "size=16m") != 0) {
                        try_dir += strlen(try_dir) + 1;
@@ -191,20 +207,17 @@ static char *make_tempdir(void)
                if (umount2(try_dir, MNT_DETACH) != 0) {
                        bb_perror_msg_and_die("can't %smount tmpfs", "un");
                }
+#else
+               bb_simple_perror_msg_and_die("can't create temporary directory");
+#endif
        } else {
                xchdir(tempdir);
        }
        return tempdir;
 }
 
-static void do_logging(unsigned sample_period_us)
+static void do_logging(unsigned sample_period_us, int process_accounting)
 {
-       //# Enable process accounting if configured
-       //if [ "$PROCESS_ACCOUNTING" = "yes" ]; then
-       //      [ -e kernel_pacct ] || : > kernel_pacct
-       //      accton kernel_pacct
-       //fi
-
        FILE *proc_stat = xfopen("proc_stat.log", "w");
        FILE *proc_diskstats = xfopen("proc_diskstats.log", "w");
        //FILE *proc_netdev = xfopen("proc_netdev.log", "w");
@@ -212,6 +225,11 @@ static void do_logging(unsigned sample_period_us)
        int look_for_login_process = (getppid() == 1);
        unsigned count = 60*1000*1000 / sample_period_us; /* ~1 minute */
 
+       if (process_accounting) {
+               close(xopen("kernel_pacct", O_WRONLY | O_CREAT | O_TRUNC));
+               acct("kernel_pacct");
+       }
+
        while (--count && !bb_got_signal) {
                char *p;
                int len = open_read_close("/proc/uptime", G.jiffy_line, sizeof(G.jiffy_line)-2);
@@ -242,11 +260,9 @@ static void do_logging(unsigned sample_period_us)
  wait_more:
                usleep(sample_period_us);
        }
-
-       // [ -e kernel_pacct ] && accton off
 }
 
-static void finalize(char *tempdir, const char *prog)
+static void finalize(char *tempdir, const char *prog, int process_accounting)
 {
        //# Stop process accounting if configured
        //local pacct=
@@ -254,6 +270,9 @@ static void finalize(char *tempdir, const char *prog)
 
        FILE *header_fp = xfopen("header", "w");
 
+       if (process_accounting)
+               acct(NULL);
+
        if (prog)
                fprintf(header_fp, "profile.process = %s\n", prog);
 
@@ -296,7 +315,7 @@ static void finalize(char *tempdir, const char *prog)
        fclose(header_fp);
 
        /* Package log files */
-       system("tar -zcf /var/log/bootchart.tgz header *.log"); // + $pacct
+       system(xasprintf("tar -zcf /var/log/bootlog.tgz header %s *.log", process_accounting ? "kernel_pacct" : ""));
        /* Clean up (if we are not in detached tmpfs) */
        if (tempdir) {
                unlink("header");
@@ -304,6 +323,8 @@ static void finalize(char *tempdir, const char *prog)
                unlink("proc_diskstats.log");
                //unlink("proc_netdev.log");
                unlink("proc_ps.log");
+               if (process_accounting)
+                       unlink("kernel_pacct");
                rmdir(tempdir);
        }
 
@@ -316,7 +337,6 @@ static void finalize(char *tempdir, const char *prog)
 //usage:       "start [PROG ARGS]|stop|init"
 //usage:#define bootchartd_full_usage "\n\n"
 //usage:       "Create /var/log/bootchart.tgz with boot chart data\n"
-//usage:     "\nOptions:"
 //usage:     "\nstart: start background logging; with PROG, run PROG, then kill logging with USR1"
 //usage:     "\nstop: send USR1 to all bootchartd processes"
 //usage:     "\ninit: start background logging; stop when getty/xdm is seen (for init scripts)"
@@ -328,6 +348,7 @@ int bootchartd_main(int argc UNUSED_PARAM, char **argv)
        unsigned sample_period_us;
        pid_t parent_pid, logger_pid;
        smallint cmd;
+       int process_accounting;
        enum {
                CMD_STOP = 0,
                CMD_START,
@@ -361,6 +382,7 @@ int bootchartd_main(int argc UNUSED_PARAM, char **argv)
 
        /* Read config file: */
        sample_period_us = 200 * 1000;
+       process_accounting = 0;
        if (ENABLE_FEATURE_BOOTCHARTD_CONFIG_FILE) {
                char* token[2];
                parser_t *parser = config_open2("/etc/bootchartd.conf" + 5, fopen_for_read);
@@ -369,11 +391,16 @@ int bootchartd_main(int argc UNUSED_PARAM, char **argv)
                while (config_read(parser, token, 2, 0, "#=", PARSE_NORMAL & ~PARSE_COLLAPSE)) {
                        if (strcmp(token[0], "SAMPLE_PERIOD") == 0 && token[1])
                                sample_period_us = atof(token[1]) * 1000000;
+                       if (strcmp(token[0], "PROCESS_ACCOUNTING") == 0 && token[1]
+                        && (strcmp(token[1], "on") == 0 || strcmp(token[1], "yes") == 0)
+                       ) {
+                               process_accounting = 1;
+                       }
                }
                config_close(parser);
+               if ((int)sample_period_us <= 0)
+                       sample_period_us = 1; /* prevent division by 0 */
        }
-       if ((int)sample_period_us <= 0)
-               sample_period_us = 1; /* prevent division by 0 */
 
        /* Create logger child: */
        logger_pid = fork_or_rexec(argv);
@@ -401,13 +428,15 @@ int bootchartd_main(int argc UNUSED_PARAM, char **argv)
                        putenv((char*)bb_PATH_root_path);
 
                tempdir = make_tempdir();
-               do_logging(sample_period_us);
-               finalize(tempdir, cmd == CMD_START ? argv[2] : NULL);
+               do_logging(sample_period_us, process_accounting);
+               finalize(tempdir, cmd == CMD_START ? argv[2] : NULL, process_accounting);
                return EXIT_SUCCESS;
        }
 
        /* parent */
 
+       USE_FOR_NOMMU(argv[0][0] &= 0x7f); /* undo fork_or_rexec() damage */
+
        if (DO_SIGNAL_SYNC) {
                /* Wait for logger child to set handlers, then unpause it.
                 * Otherwise with short-lived PROG (e.g. "bootchartd start true")
@@ -430,8 +459,7 @@ int bootchartd_main(int argc UNUSED_PARAM, char **argv)
                pid_t pid = xvfork();
                if (pid == 0) { /* child */
                        argv += 2;
-                       execvp(argv[0], argv);
-                       bb_perror_msg_and_die("can't execute '%s'", argv[0]);
+                       BB_EXECVP_or_die(argv);
                }
                /* parent */
                waitpid(pid, NULL, 0);