1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#include "libbb.h"
49#include "common_bufsiz.h"
50
51#include <sys/utsname.h>
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
61#endif
62
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
69#define BC_VERSION_STR "0.8"
70
71
72
73
74#define DO_SIGNAL_SYNC 1
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115struct globals {
116 char jiffy_line[COMMON_BUFSIZE];
117} FIX_ALIASING;
118#define G (*(struct globals*)bb_common_bufsiz1)
119#define INIT_G() do { setup_common_bufsiz(); } while (0)
120
121static 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
133static 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
148
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';
172
173 if (((p[0] == 'g' || p[0] == 'k' || p[0] == 'x')
174 && p[1] == 'd' && p[2] == 'm' && p[3] == '\0'
175 )
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
187static char *make_tempdir(void)
188{
189 char template[] = "/tmp/bootchart.XXXXXX";
190 char *tempdir = xstrdup(mkdtemp(template));
191 if (!tempdir) {
192#ifdef __linux__
193
194
195
196
197
198 static const char dirs[] ALIGN1 = "/mnt\0""/tmp\0""/boot\0""/proc\0";
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
206 xchdir(try_dir);
207 if (umount2(try_dir, MNT_DETACH) != 0) {
208 bb_perror_msg_and_die("can't %smount tmpfs", "un");
209 }
210#else
211 bb_simple_perror_msg_and_die("can't create temporary directory");
212#endif
213 } else {
214 xchdir(tempdir);
215 }
216 return tempdir;
217}
218
219static void do_logging(unsigned sample_period_us, int process_accounting)
220{
221 FILE *proc_stat = xfopen_for_write("proc_stat.log");
222 FILE *proc_diskstats = xfopen_for_write("proc_diskstats.log");
223
224 FILE *proc_ps = xfopen_for_write("proc_ps.log");
225 int look_for_login_process = (getppid() == 1);
226 unsigned count = 60*1000*1000 / sample_period_us;
227
228 if (process_accounting) {
229 close(xopen("kernel_pacct", O_WRONLY | O_CREAT | O_TRUNC));
230 acct("kernel_pacct");
231 }
232
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
239
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
252 if (dump_procs(proc_ps, look_for_login_process)) {
253
254
255
256 if (count > 2*1000*1000 / sample_period_us)
257 count = 2*1000*1000 / sample_period_us;
258 }
259 fflush_all();
260 wait_more:
261 usleep(sample_period_us);
262 }
263}
264
265static void finalize(char *tempdir, const char *prog, int process_accounting)
266{
267
268
269
270
271 FILE *header_fp = xfopen_for_write("header");
272
273 if (process_accounting)
274 acct(NULL);
275
276 if (prog)
277 fprintf(header_fp, "profile.process = %s\n", prog);
278
279 fputs("version = "BC_VERSION_STR"\n", header_fp);
280 if (ENABLE_FEATURE_BOOTCHARTD_BLOATED_HEADER) {
281 char *hostname;
282 char *kcmdline;
283 time_t t;
284 struct tm tm_time;
285
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);
298
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
307
308
309 kcmdline = xmalloc_open_read_close("/proc/cmdline", NULL);
310
311 fprintf(header_fp, "system.kernel.options = %s", kcmdline);
312 if (ENABLE_FEATURE_CLEAN_UP)
313 free(kcmdline);
314 }
315 fclose(header_fp);
316
317
318 system(xasprintf("tar -zcf /var/log/bootlog.tgz header %s *.log", process_accounting ? "kernel_pacct" : ""));
319
320 if (tempdir) {
321 unlink("header");
322 unlink("proc_stat.log");
323 unlink("proc_diskstats.log");
324
325 unlink("proc_ps.log");
326 if (process_accounting)
327 unlink("kernel_pacct");
328 rmdir(tempdir);
329 }
330
331
332
333
334}
335
336
337
338
339
340
341
342
343
344
345int bootchartd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
346int bootchartd_main(int argc UNUSED_PARAM, char **argv)
347{
348 unsigned sample_period_us;
349 pid_t parent_pid, logger_pid;
350 smallint cmd;
351 int process_accounting;
352 enum {
353 CMD_STOP = 0,
354 CMD_START,
355 CMD_INIT,
356 CMD_PID1,
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
381
382
383
384 sample_period_us = 200 * 1000;
385 process_accounting = 0;
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])
393 sample_period_us = atof(token[1]) * 1000000;
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 }
399 }
400 config_close(parser);
401 if ((int)sample_period_us <= 0)
402 sample_period_us = 1;
403 }
404
405
406 logger_pid = fork_or_rexec(argv);
407
408 if (logger_pid == 0) {
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
422 raise(SIGSTOP);
423
424
425
426
427 if (cmd == CMD_PID1 && !getenv("PATH"))
428 putenv((char*)bb_PATH_root_path);
429
430 tempdir = make_tempdir();
431 do_logging(sample_period_us, process_accounting);
432 finalize(tempdir, cmd == CMD_START ? argv[2] : NULL, process_accounting);
433 return EXIT_SUCCESS;
434 }
435
436
437
438 USE_FOR_NOMMU(argv[0][0] &= 0x7f);
439
440 if (DO_SIGNAL_SYNC) {
441
442
443
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);
455 bb_perror_msg_and_die("can't execute '%s'", "/sbin/init");
456 }
457
458 if (cmd == CMD_START && argv[2]) {
459 pid_t pid = xvfork();
460 if (pid == 0) {
461 argv += 2;
462 BB_EXECVP_or_die(argv);
463 }
464
465 waitpid(pid, NULL, 0);
466 kill(logger_pid, SIGUSR1);
467 }
468
469 return EXIT_SUCCESS;
470}
471