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