Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 4 | */ |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 5 | |
Denys Vlasenko | b9f2d9f | 2011-01-18 13:58:01 +0100 | [diff] [blame] | 6 | //applet:IF_BOOTCHARTD(APPLET(bootchartd, BB_DIR_SBIN, BB_SUID_DROP)) |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 7 | |
| 8 | //kbuild:lib-$(CONFIG_BOOTCHARTD) += bootchartd.o |
| 9 | |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 10 | //config:config BOOTCHARTD |
| 11 | //config: bool "bootchartd" |
| 12 | //config: default y |
| 13 | //config: help |
| 14 | //config: bootchartd is commonly used to profile the boot process |
| 15 | //config: for the purpose of speeding it up. In this case, it is started |
| 16 | //config: by the kernel as the init process. This is configured by adding |
| 17 | //config: the init=/sbin/bootchartd option to the kernel command line. |
| 18 | //config: |
| 19 | //config: It can also be used to monitor the resource usage of a specific |
| 20 | //config: application or the running system in general. In this case, |
| 21 | //config: bootchartd is started interactively by running bootchartd start |
| 22 | //config: and stopped using bootchartd stop. |
| 23 | //config: |
| 24 | //config:config FEATURE_BOOTCHARTD_BLOATED_HEADER |
Denys Vlasenko | 7a2aa87 | 2010-07-06 15:49:01 +0200 | [diff] [blame] | 25 | //config: bool "Compatible, bloated header" |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 26 | //config: default y |
| 27 | //config: depends on BOOTCHARTD |
| 28 | //config: help |
| 29 | //config: Create extended header file compatible with "big" bootchartd. |
| 30 | //config: "Big" bootchartd is a shell script and it dumps some |
| 31 | //config: "convenient" info int the header, such as: |
| 32 | //config: title = Boot chart for `hostname` (`date`) |
| 33 | //config: system.uname = `uname -srvm` |
| 34 | //config: system.release = `cat /etc/DISTRO-release` |
| 35 | //config: system.cpu = `grep '^model name' /proc/cpuinfo | head -1` ($cpucount) |
| 36 | //config: system.kernel.options = `cat /proc/cmdline` |
| 37 | //config: This data is not mandatory for bootchart graph generation, |
| 38 | //config: and is considered bloat. Nevertheless, this option |
| 39 | //config: makes bootchartd applet to dump a subset of it. |
| 40 | //config: |
| 41 | //config:config FEATURE_BOOTCHARTD_CONFIG_FILE |
Denys Vlasenko | 7a2aa87 | 2010-07-06 15:49:01 +0200 | [diff] [blame] | 42 | //config: bool "Support bootchartd.conf" |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 43 | //config: default y |
| 44 | //config: depends on BOOTCHARTD |
| 45 | //config: help |
Denys Vlasenko | 6d9c88a | 2010-06-21 07:25:36 +0200 | [diff] [blame] | 46 | //config: Enable reading and parsing of $PWD/bootchartd.conf |
| 47 | //config: and /etc/bootchartd.conf files. |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 48 | |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 49 | #include "libbb.h" |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 50 | /* After libbb.h, since it needs sys/types.h on some systems */ |
| 51 | #include <sys/utsname.h> |
Jeremie Koenig | e7a0632 | 2010-07-29 04:29:53 +0200 | [diff] [blame] | 52 | |
| 53 | #ifdef __linux__ |
| 54 | # include <sys/mount.h> |
| 55 | # ifndef MS_SILENT |
| 56 | # define MS_SILENT (1 << 15) |
| 57 | # endif |
| 58 | # ifndef MNT_DETACH |
| 59 | # define MNT_DETACH 0x00000002 |
| 60 | # endif |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 61 | #endif |
| 62 | |
| 63 | #define BC_VERSION_STR "0.8" |
| 64 | |
| 65 | /* For debugging, set to 0: |
| 66 | * strace won't work with DO_SIGNAL_SYNC set to 1. |
| 67 | */ |
| 68 | #define DO_SIGNAL_SYNC 1 |
| 69 | |
| 70 | |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 71 | //$PWD/bootchartd.conf and /etc/bootchartd.conf: |
| 72 | //supported options: |
| 73 | //# Sampling period (in seconds) |
| 74 | //SAMPLE_PERIOD=0.2 |
| 75 | // |
| 76 | //not yet supported: |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 77 | //# tmpfs size |
| 78 | //# (32 MB should suffice for ~20 minutes worth of log data, but YMMV) |
| 79 | //TMPFS_SIZE=32m |
| 80 | // |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 81 | //# Whether to enable and store BSD process accounting information. The |
| 82 | //# kernel needs to be configured to enable v3 accounting |
| 83 | //# (CONFIG_BSD_PROCESS_ACCT_V3). accton from the GNU accounting utilities |
| 84 | //# is also required. |
| 85 | //PROCESS_ACCOUNTING="no" |
| 86 | // |
| 87 | //# Tarball for the various boot log files |
| 88 | //BOOTLOG_DEST=/var/log/bootchart.tgz |
| 89 | // |
| 90 | //# Whether to automatically stop logging as the boot process completes. |
| 91 | //# The logger will look for known processes that indicate bootup completion |
| 92 | //# at a specific runlevel (e.g. gdm-binary, mingetty, etc.). |
| 93 | //AUTO_STOP_LOGGER="yes" |
| 94 | // |
| 95 | //# Whether to automatically generate the boot chart once the boot logger |
| 96 | //# completes. The boot chart will be generated in $AUTO_RENDER_DIR. |
| 97 | //# Note that the bootchart package must be installed. |
| 98 | //AUTO_RENDER="no" |
| 99 | // |
| 100 | //# Image format to use for the auto-generated boot chart |
| 101 | //# (choose between png, svg and eps). |
| 102 | //AUTO_RENDER_FORMAT="png" |
| 103 | // |
| 104 | //# Output directory for auto-generated boot charts |
| 105 | //AUTO_RENDER_DIR="/var/log" |
| 106 | |
| 107 | |
| 108 | /* Globals */ |
| 109 | struct globals { |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 110 | char jiffy_line[COMMON_BUFSIZE]; |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 111 | } FIX_ALIASING; |
| 112 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 113 | #define INIT_G() do { } while (0) |
| 114 | |
| 115 | static void dump_file(FILE *fp, const char *filename) |
| 116 | { |
| 117 | int fd = open(filename, O_RDONLY); |
| 118 | if (fd >= 0) { |
| 119 | fputs(G.jiffy_line, fp); |
| 120 | fflush(fp); |
| 121 | bb_copyfd_eof(fd, fileno(fp)); |
| 122 | close(fd); |
| 123 | fputc('\n', fp); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | static int dump_procs(FILE *fp, int look_for_login_process) |
| 128 | { |
| 129 | struct dirent *entry; |
| 130 | DIR *dir = opendir("/proc"); |
| 131 | int found_login_process = 0; |
| 132 | |
| 133 | fputs(G.jiffy_line, fp); |
| 134 | while ((entry = readdir(dir)) != NULL) { |
| 135 | char name[sizeof("/proc/%u/cmdline") + sizeof(int)*3]; |
| 136 | int stat_fd; |
| 137 | unsigned pid = bb_strtou(entry->d_name, NULL, 10); |
| 138 | if (errno) |
| 139 | continue; |
| 140 | |
| 141 | /* Android's version reads /proc/PID/cmdline and extracts |
| 142 | * non-truncated process name. Do we want to do that? */ |
| 143 | |
| 144 | sprintf(name, "/proc/%u/stat", pid); |
| 145 | stat_fd = open(name, O_RDONLY); |
| 146 | if (stat_fd >= 0) { |
| 147 | char *p; |
| 148 | char stat_line[4*1024]; |
| 149 | int rd = safe_read(stat_fd, stat_line, sizeof(stat_line)-2); |
| 150 | |
| 151 | close(stat_fd); |
| 152 | if (rd < 0) |
| 153 | continue; |
| 154 | stat_line[rd] = '\0'; |
| 155 | p = strchrnul(stat_line, '\n'); |
| 156 | *p++ = '\n'; |
| 157 | *p = '\0'; |
| 158 | fputs(stat_line, fp); |
| 159 | if (!look_for_login_process) |
| 160 | continue; |
| 161 | p = strchr(stat_line, '('); |
| 162 | if (!p) |
| 163 | continue; |
| 164 | p++; |
| 165 | strchrnul(p, ')')[0] = '\0'; |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 166 | /* Is it gdm, kdm or a getty? */ |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 167 | if (((p[0] == 'g' || p[0] == 'k' || p[0] == 'x') && p[1] == 'd' && p[2] == 'm') |
| 168 | || strstr(p, "getty") |
| 169 | ) { |
| 170 | found_login_process = 1; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | closedir(dir); |
| 175 | fputc('\n', fp); |
| 176 | return found_login_process; |
| 177 | } |
| 178 | |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 179 | static char *make_tempdir(void) |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 180 | { |
| 181 | char template[] = "/tmp/bootchart.XXXXXX"; |
| 182 | char *tempdir = xstrdup(mkdtemp(template)); |
| 183 | if (!tempdir) { |
Jeremie Koenig | e7a0632 | 2010-07-29 04:29:53 +0200 | [diff] [blame] | 184 | #ifdef __linux__ |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 185 | /* /tmp is not writable (happens when we are used as init). |
| 186 | * Try to mount a tmpfs, them cd and lazily unmount it. |
| 187 | * Since we unmount it at once, we can mount it anywhere. |
| 188 | * Try a few locations which are likely ti exist. |
| 189 | */ |
| 190 | static const char dirs[] = "/mnt\0""/tmp\0""/boot\0""/proc\0"; |
| 191 | const char *try_dir = dirs; |
| 192 | while (mount("none", try_dir, "tmpfs", MS_SILENT, "size=16m") != 0) { |
| 193 | try_dir += strlen(try_dir) + 1; |
| 194 | if (!try_dir[0]) |
| 195 | bb_perror_msg_and_die("can't %smount tmpfs", ""); |
| 196 | } |
| 197 | //bb_error_msg("mounted tmpfs on %s", try_dir); |
| 198 | xchdir(try_dir); |
| 199 | if (umount2(try_dir, MNT_DETACH) != 0) { |
| 200 | bb_perror_msg_and_die("can't %smount tmpfs", "un"); |
| 201 | } |
Jeremie Koenig | e7a0632 | 2010-07-29 04:29:53 +0200 | [diff] [blame] | 202 | #else |
| 203 | bb_perror_msg_and_die("can't create temporary directory"); |
| 204 | #endif |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 205 | } else { |
| 206 | xchdir(tempdir); |
| 207 | } |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 208 | return tempdir; |
| 209 | } |
| 210 | |
Keisuke Yasui | c03fb3c | 2010-06-28 16:04:00 +0200 | [diff] [blame] | 211 | static void do_logging(unsigned sample_period_us) |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 212 | { |
| 213 | //# Enable process accounting if configured |
| 214 | //if [ "$PROCESS_ACCOUNTING" = "yes" ]; then |
| 215 | // [ -e kernel_pacct ] || : > kernel_pacct |
| 216 | // accton kernel_pacct |
| 217 | //fi |
| 218 | |
| 219 | FILE *proc_stat = xfopen("proc_stat.log", "w"); |
| 220 | FILE *proc_diskstats = xfopen("proc_diskstats.log", "w"); |
| 221 | //FILE *proc_netdev = xfopen("proc_netdev.log", "w"); |
| 222 | FILE *proc_ps = xfopen("proc_ps.log", "w"); |
| 223 | int look_for_login_process = (getppid() == 1); |
Keisuke Yasui | c03fb3c | 2010-06-28 16:04:00 +0200 | [diff] [blame] | 224 | unsigned count = 60*1000*1000 / sample_period_us; /* ~1 minute */ |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 225 | |
| 226 | while (--count && !bb_got_signal) { |
| 227 | char *p; |
| 228 | int len = open_read_close("/proc/uptime", G.jiffy_line, sizeof(G.jiffy_line)-2); |
| 229 | if (len < 0) |
| 230 | goto wait_more; |
| 231 | /* /proc/uptime has format "NNNNNN.MM NNNNNNN.MM" */ |
| 232 | /* we convert it to "NNNNNNMM\n" (using first value) */ |
| 233 | G.jiffy_line[len] = '\0'; |
| 234 | p = strchr(G.jiffy_line, '.'); |
| 235 | if (!p) |
| 236 | goto wait_more; |
| 237 | while (isdigit(*++p)) |
| 238 | p[-1] = *p; |
| 239 | p[-1] = '\n'; |
| 240 | p[0] = '\0'; |
| 241 | |
| 242 | dump_file(proc_stat, "/proc/stat"); |
| 243 | dump_file(proc_diskstats, "/proc/diskstats"); |
| 244 | //dump_file(proc_netdev, "/proc/net/dev"); |
| 245 | if (dump_procs(proc_ps, look_for_login_process)) { |
| 246 | /* dump_procs saw a getty or {g,k,x}dm |
| 247 | * stop logging in 2 seconds: |
| 248 | */ |
Keisuke Yasui | c03fb3c | 2010-06-28 16:04:00 +0200 | [diff] [blame] | 249 | if (count > 2*1000*1000 / sample_period_us) |
| 250 | count = 2*1000*1000 / sample_period_us; |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 251 | } |
| 252 | fflush_all(); |
| 253 | wait_more: |
Denys Vlasenko | 64606c6 | 2010-06-22 18:33:15 +0200 | [diff] [blame] | 254 | usleep(sample_period_us); |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // [ -e kernel_pacct ] && accton off |
| 258 | } |
| 259 | |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 260 | static void finalize(char *tempdir, const char *prog) |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 261 | { |
| 262 | //# Stop process accounting if configured |
| 263 | //local pacct= |
| 264 | //[ -e kernel_pacct ] && pacct=kernel_pacct |
| 265 | |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 266 | FILE *header_fp = xfopen("header", "w"); |
| 267 | |
| 268 | if (prog) |
| 269 | fprintf(header_fp, "profile.process = %s\n", prog); |
| 270 | |
| 271 | fputs("version = "BC_VERSION_STR"\n", header_fp); |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 272 | if (ENABLE_FEATURE_BOOTCHARTD_BLOATED_HEADER) { |
| 273 | char *hostname; |
| 274 | char *kcmdline; |
| 275 | time_t t; |
| 276 | struct tm tm_time; |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 277 | /* x2 for possible localized weekday/month names */ |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 278 | char date_buf[sizeof("Mon Jun 21 05:29:03 CEST 2010") * 2]; |
| 279 | struct utsname unamebuf; |
| 280 | |
| 281 | hostname = safe_gethostname(); |
| 282 | time(&t); |
| 283 | localtime_r(&t, &tm_time); |
| 284 | strftime(date_buf, sizeof(date_buf), "%a %b %e %H:%M:%S %Z %Y", &tm_time); |
| 285 | fprintf(header_fp, "title = Boot chart for %s (%s)\n", hostname, date_buf); |
| 286 | if (ENABLE_FEATURE_CLEAN_UP) |
| 287 | free(hostname); |
| 288 | |
| 289 | uname(&unamebuf); /* never fails */ |
| 290 | /* same as uname -srvm */ |
| 291 | fprintf(header_fp, "system.uname = %s %s %s %s\n", |
| 292 | unamebuf.sysname, |
| 293 | unamebuf.release, |
| 294 | unamebuf.version, |
| 295 | unamebuf.machine |
| 296 | ); |
| 297 | |
| 298 | //system.release = `cat /etc/DISTRO-release` |
| 299 | //system.cpu = `grep '^model name' /proc/cpuinfo | head -1` ($cpucount) |
| 300 | |
| 301 | kcmdline = xmalloc_open_read_close("/proc/cmdline", NULL); |
| 302 | /* kcmdline includes trailing "\n" */ |
| 303 | fprintf(header_fp, "system.kernel.options = %s", kcmdline); |
| 304 | if (ENABLE_FEATURE_CLEAN_UP) |
| 305 | free(kcmdline); |
| 306 | } |
| 307 | fclose(header_fp); |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 308 | |
| 309 | /* Package log files */ |
| 310 | system("tar -zcf /var/log/bootchart.tgz header *.log"); // + $pacct |
| 311 | /* Clean up (if we are not in detached tmpfs) */ |
| 312 | if (tempdir) { |
| 313 | unlink("header"); |
| 314 | unlink("proc_stat.log"); |
| 315 | unlink("proc_diskstats.log"); |
| 316 | //unlink("proc_netdev.log"); |
| 317 | unlink("proc_ps.log"); |
| 318 | rmdir(tempdir); |
| 319 | } |
| 320 | |
| 321 | /* shell-based bootchartd tries to run /usr/bin/bootchart if $AUTO_RENDER=yes: |
| 322 | * /usr/bin/bootchart -o "$AUTO_RENDER_DIR" -f $AUTO_RENDER_FORMAT "$BOOTLOG_DEST" |
| 323 | */ |
| 324 | } |
| 325 | |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 326 | //usage:#define bootchartd_trivial_usage |
| 327 | //usage: "start [PROG ARGS]|stop|init" |
| 328 | //usage:#define bootchartd_full_usage "\n\n" |
| 329 | //usage: "Create /var/log/bootchart.tgz with boot chart data\n" |
| 330 | //usage: "\nOptions:" |
| 331 | //usage: "\nstart: start background logging; with PROG, run PROG, then kill logging with USR1" |
| 332 | //usage: "\nstop: send USR1 to all bootchartd processes" |
| 333 | //usage: "\ninit: start background logging; stop when getty/xdm is seen (for init scripts)" |
| 334 | //usage: "\nUnder PID 1: as init, then exec $bootchart_init, /init, /sbin/init" |
| 335 | |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 336 | int bootchartd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 337 | int bootchartd_main(int argc UNUSED_PARAM, char **argv) |
| 338 | { |
Keisuke Yasui | c03fb3c | 2010-06-28 16:04:00 +0200 | [diff] [blame] | 339 | unsigned sample_period_us; |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 340 | pid_t parent_pid, logger_pid; |
| 341 | smallint cmd; |
| 342 | enum { |
| 343 | CMD_STOP = 0, |
| 344 | CMD_START, |
| 345 | CMD_INIT, |
| 346 | CMD_PID1, /* used to mark pid 1 case */ |
| 347 | }; |
| 348 | |
| 349 | INIT_G(); |
| 350 | |
| 351 | parent_pid = getpid(); |
| 352 | if (argv[1]) { |
| 353 | cmd = index_in_strings("stop\0""start\0""init\0", argv[1]); |
| 354 | if (cmd < 0) |
| 355 | bb_show_usage(); |
| 356 | if (cmd == CMD_STOP) { |
| 357 | pid_t *pidList = find_pid_by_name("bootchartd"); |
| 358 | while (*pidList != 0) { |
| 359 | if (*pidList != parent_pid) |
| 360 | kill(*pidList, SIGUSR1); |
| 361 | pidList++; |
| 362 | } |
| 363 | return EXIT_SUCCESS; |
| 364 | } |
| 365 | } else { |
| 366 | if (parent_pid != 1) |
| 367 | bb_show_usage(); |
| 368 | cmd = CMD_PID1; |
| 369 | } |
| 370 | |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 371 | /* Here we are in START, INIT or CMD_PID1 state */ |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 372 | |
| 373 | /* Read config file: */ |
Denys Vlasenko | 64606c6 | 2010-06-22 18:33:15 +0200 | [diff] [blame] | 374 | sample_period_us = 200 * 1000; |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 375 | if (ENABLE_FEATURE_BOOTCHARTD_CONFIG_FILE) { |
| 376 | char* token[2]; |
| 377 | parser_t *parser = config_open2("/etc/bootchartd.conf" + 5, fopen_for_read); |
| 378 | if (!parser) |
| 379 | parser = config_open2("/etc/bootchartd.conf", fopen_for_read); |
| 380 | while (config_read(parser, token, 2, 0, "#=", PARSE_NORMAL & ~PARSE_COLLAPSE)) { |
| 381 | if (strcmp(token[0], "SAMPLE_PERIOD") == 0 && token[1]) |
Denys Vlasenko | 64606c6 | 2010-06-22 18:33:15 +0200 | [diff] [blame] | 382 | sample_period_us = atof(token[1]) * 1000000; |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 383 | } |
| 384 | config_close(parser); |
| 385 | } |
Keisuke Yasui | c03fb3c | 2010-06-28 16:04:00 +0200 | [diff] [blame] | 386 | if ((int)sample_period_us <= 0) |
| 387 | sample_period_us = 1; /* prevent division by 0 */ |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 388 | |
| 389 | /* Create logger child: */ |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 390 | logger_pid = fork_or_rexec(argv); |
| 391 | |
| 392 | if (logger_pid == 0) { /* child */ |
| 393 | char *tempdir; |
| 394 | |
| 395 | bb_signals(0 |
| 396 | + (1 << SIGUSR1) |
| 397 | + (1 << SIGUSR2) |
| 398 | + (1 << SIGTERM) |
| 399 | + (1 << SIGQUIT) |
| 400 | + (1 << SIGINT) |
| 401 | + (1 << SIGHUP) |
| 402 | , record_signo); |
| 403 | |
| 404 | if (DO_SIGNAL_SYNC) |
| 405 | /* Inform parent that we are ready */ |
| 406 | raise(SIGSTOP); |
| 407 | |
Denys Vlasenko | b8ba6b6 | 2010-05-05 00:40:15 +0200 | [diff] [blame] | 408 | /* If we are started by kernel, PATH might be unset. |
| 409 | * In order to find "tar", let's set some sane PATH: |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 410 | */ |
| 411 | if (cmd == CMD_PID1 && !getenv("PATH")) |
| 412 | putenv((char*)bb_PATH_root_path); |
Denys Vlasenko | b8ba6b6 | 2010-05-05 00:40:15 +0200 | [diff] [blame] | 413 | |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 414 | tempdir = make_tempdir(); |
Denys Vlasenko | 64606c6 | 2010-06-22 18:33:15 +0200 | [diff] [blame] | 415 | do_logging(sample_period_us); |
Denys Vlasenko | adbbee4 | 2010-06-21 07:17:23 +0200 | [diff] [blame] | 416 | finalize(tempdir, cmd == CMD_START ? argv[2] : NULL); |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 417 | return EXIT_SUCCESS; |
| 418 | } |
| 419 | |
| 420 | /* parent */ |
| 421 | |
| 422 | if (DO_SIGNAL_SYNC) { |
| 423 | /* Wait for logger child to set handlers, then unpause it. |
| 424 | * Otherwise with short-lived PROG (e.g. "bootchartd start true") |
| 425 | * we might send SIGUSR1 before logger sets its handler. |
| 426 | */ |
| 427 | waitpid(logger_pid, NULL, WUNTRACED); |
| 428 | kill(logger_pid, SIGCONT); |
| 429 | } |
| 430 | |
| 431 | if (cmd == CMD_PID1) { |
| 432 | char *bootchart_init = getenv("bootchart_init"); |
| 433 | if (bootchart_init) |
| 434 | execl(bootchart_init, bootchart_init, NULL); |
| 435 | execl("/init", "init", NULL); |
| 436 | execl("/sbin/init", "init", NULL); |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 437 | bb_perror_msg_and_die("can't execute '%s'", "/sbin/init"); |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | if (cmd == CMD_START && argv[2]) { /* "start PROG ARGS" */ |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 441 | pid_t pid = xvfork(); |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 442 | if (pid == 0) { /* child */ |
| 443 | argv += 2; |
Denys Vlasenko | 1c31e9e | 2010-11-28 04:34:09 +0100 | [diff] [blame] | 444 | BB_EXECVP_or_die(argv); |
Denys Vlasenko | ff027d6 | 2010-05-04 15:45:25 +0200 | [diff] [blame] | 445 | } |
| 446 | /* parent */ |
| 447 | waitpid(pid, NULL, 0); |
| 448 | kill(logger_pid, SIGUSR1); |
| 449 | } |
| 450 | |
| 451 | return EXIT_SUCCESS; |
| 452 | } |