tar: support -T - and -X -
[oweals/busybox.git] / init / bootchartd.c
index f7de13e2f1fc011f29505e5b574710a5852d24cd..009e2690c5635793386447fbaa32e70082ab34f2 100644 (file)
@@ -1,8 +1,12 @@
 /* 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.
  */
 
+//applet:IF_BOOTCHARTD(APPLET(bootchartd, BB_DIR_SBIN, BB_SUID_DROP))
+
+//kbuild:lib-$(CONFIG_BOOTCHARTD) += bootchartd.o
+
 //config:config BOOTCHARTD
 //config:      bool "bootchartd"
 //config:      default y
@@ -18,7 +22,7 @@
 //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
@@ -35,7 +39,7 @@
 //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
 #include "libbb.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)
-#endif
-#ifndef MNT_DETACH
-# define MNT_DETACH 0x00000002
+
+#ifdef __linux__
+# include <sys/mount.h>
+# ifndef MS_SILENT
+#  define MS_SILENT      (1 << 15)
+# endif
+# ifndef MNT_DETACH
+#  define MNT_DETACH 0x00000002
+# endif
 #endif
 
 #define BC_VERSION_STR "0.8"
@@ -156,7 +163,7 @@ static int dump_procs(FILE *fp, int look_for_login_process)
                                continue;
                        p++;
                        strchrnul(p, ')')[0] = '\0';
-                       /* If is gdm, kdm or a getty? */
+                       /* Is it gdm, kdm or a getty? */
                        if (((p[0] == 'g' || p[0] == 'k' || p[0] == 'x') && p[1] == 'd' && p[2] == 'm')
                         || strstr(p, "getty")
                        ) {
@@ -174,6 +181,7 @@ 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.
                 * Since we unmount it at once, we can mount it anywhere.
@@ -191,13 +199,16 @@ static char *make_tempdir(void)
                if (umount2(try_dir, MNT_DETACH) != 0) {
                        bb_perror_msg_and_die("can't %smount tmpfs", "un");
                }
+#else
+               bb_perror_msg_and_die("can't create temporary directory");
+#endif
        } else {
                xchdir(tempdir);
        }
        return tempdir;
 }
 
-static void do_logging(int sample_period_us)
+static void do_logging(unsigned sample_period_us)
 {
        //# Enable process accounting if configured
        //if [ "$PROCESS_ACCOUNTING" = "yes" ]; then
@@ -210,7 +221,7 @@ static void do_logging(int sample_period_us)
        //FILE *proc_netdev = xfopen("proc_netdev.log", "w");
        FILE *proc_ps = xfopen("proc_ps.log", "w");
        int look_for_login_process = (getppid() == 1);
-       unsigned count = 60*1000*1000 / (200*1000); /* ~1 minute */
+       unsigned count = 60*1000*1000 / sample_period_us; /* ~1 minute */
 
        while (--count && !bb_got_signal) {
                char *p;
@@ -235,8 +246,8 @@ static void do_logging(int sample_period_us)
                        /* dump_procs saw a getty or {g,k,x}dm
                         * stop logging in 2 seconds:
                         */
-                       if (count > 2*1000*1000 / (200*1000))
-                               count = 2*1000*1000 / (200*1000);
+                       if (count > 2*1000*1000 / sample_period_us)
+                               count = 2*1000*1000 / sample_period_us;
                }
                fflush_all();
  wait_more:
@@ -258,13 +269,12 @@ static void finalize(char *tempdir, const char *prog)
                fprintf(header_fp, "profile.process = %s\n", prog);
 
        fputs("version = "BC_VERSION_STR"\n", header_fp);
-
        if (ENABLE_FEATURE_BOOTCHARTD_BLOATED_HEADER) {
                char *hostname;
                char *kcmdline;
                time_t t;
                struct tm tm_time;
-               /* x2 for possible localized data */
+               /* x2 for possible localized weekday/month names */
                char date_buf[sizeof("Mon Jun 21 05:29:03 CEST 2010") * 2];
                struct utsname unamebuf;
 
@@ -313,20 +323,20 @@ static void finalize(char *tempdir, const char *prog)
         */
 }
 
-/* Usage:
- * bootchartd start [PROG ARGS]: start logging in background, USR1 stops it.
- *     With PROG, runs PROG, then kills background logging.
- * bootchartd stop: same as "killall -USR1 bootchartd"
- * bootchartd init: start logging in background
- *     Stop when getty/gdm is seen (if AUTO_STOP_LOGGER = yes).
- *     Meant to be used from init scripts.
- * bootchartd (pid==1): as init, but then execs $bootchart_init, /init, /sbin/init
- *     Meant to be used as kernel's init process.
- */
+//usage:#define bootchartd_trivial_usage
+//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)"
+//usage:     "\nUnder PID 1: as init, then exec $bootchart_init, /init, /sbin/init"
+
 int bootchartd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int bootchartd_main(int argc UNUSED_PARAM, char **argv)
 {
-       int sample_period_us;
+       unsigned sample_period_us;
        pid_t parent_pid, logger_pid;
        smallint cmd;
        enum {
@@ -358,7 +368,7 @@ int bootchartd_main(int argc UNUSED_PARAM, char **argv)
                cmd = CMD_PID1;
        }
 
-       /* Here we are in START or INIT state */
+       /* Here we are in START, INIT or CMD_PID1 state */
 
        /* Read config file: */
        sample_period_us = 200 * 1000;
@@ -373,6 +383,8 @@ int bootchartd_main(int argc UNUSED_PARAM, char **argv)
                }
                config_close(parser);
        }
+       if ((int)sample_period_us <= 0)
+               sample_period_us = 1; /* prevent division by 0 */
 
        /* Create logger child: */
        logger_pid = fork_or_rexec(argv);
@@ -422,17 +434,14 @@ int bootchartd_main(int argc UNUSED_PARAM, char **argv)
                        execl(bootchart_init, bootchart_init, NULL);
                execl("/init", "init", NULL);
                execl("/sbin/init", "init", NULL);
-               bb_perror_msg_and_die("can't exec '%s'", "/sbin/init");
+               bb_perror_msg_and_die("can't execute '%s'", "/sbin/init");
        }
 
        if (cmd == CMD_START && argv[2]) { /* "start PROG ARGS" */
-               pid_t pid = vfork();
-               if (pid < 0)
-                       bb_perror_msg_and_die("vfork");
+               pid_t pid = xvfork();
                if (pid == 0) { /* child */
                        argv += 2;
-                       execvp(argv[0], argv);
-                       bb_perror_msg_and_die("can't exec '%s'", argv[0]);
+                       BB_EXECVP_or_die(argv);
                }
                /* parent */
                waitpid(pid, NULL, 0);