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