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