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
49#include "libbb.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#define BC_VERSION_STR "0.8"
64
65
66
67
68#define DO_SIGNAL_SYNC 1
69
70
71
72
73
74
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
109struct globals {
110 char jiffy_line[COMMON_BUFSIZE];
111} FIX_ALIASING;
112#define G (*(struct globals*)&bb_common_bufsiz1)
113#define INIT_G() do { } while (0)
114
115static 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
127static 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
142
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';
166
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
179static char *make_tempdir(void)
180{
181 char template[] = "/tmp/bootchart.XXXXXX";
182 char *tempdir = xstrdup(mkdtemp(template));
183 if (!tempdir) {
184#ifdef __linux__
185
186
187
188
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
198 xchdir(try_dir);
199 if (umount2(try_dir, MNT_DETACH) != 0) {
200 bb_perror_msg_and_die("can't %smount tmpfs", "un");
201 }
202#else
203 bb_perror_msg_and_die("can't create temporary directory");
204#endif
205 } else {
206 xchdir(tempdir);
207 }
208 return tempdir;
209}
210
211static void do_logging(unsigned sample_period_us)
212{
213
214
215
216
217
218
219 FILE *proc_stat = xfopen("proc_stat.log", "w");
220 FILE *proc_diskstats = xfopen("proc_diskstats.log", "w");
221
222 FILE *proc_ps = xfopen("proc_ps.log", "w");
223 int look_for_login_process = (getppid() == 1);
224 unsigned count = 60*1000*1000 / sample_period_us;
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
232
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
245 if (dump_procs(proc_ps, look_for_login_process)) {
246
247
248
249 if (count > 2*1000*1000 / sample_period_us)
250 count = 2*1000*1000 / sample_period_us;
251 }
252 fflush_all();
253 wait_more:
254 usleep(sample_period_us);
255 }
256
257
258}
259
260static void finalize(char *tempdir, const char *prog)
261{
262
263
264
265
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);
272 if (ENABLE_FEATURE_BOOTCHARTD_BLOATED_HEADER) {
273 char *hostname;
274 char *kcmdline;
275 time_t t;
276 struct tm tm_time;
277
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);
290
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
299
300
301 kcmdline = xmalloc_open_read_close("/proc/cmdline", NULL);
302
303 fprintf(header_fp, "system.kernel.options = %s", kcmdline);
304 if (ENABLE_FEATURE_CLEAN_UP)
305 free(kcmdline);
306 }
307 fclose(header_fp);
308
309
310 system("tar -zcf /var/log/bootchart.tgz header *.log");
311
312 if (tempdir) {
313 unlink("header");
314 unlink("proc_stat.log");
315 unlink("proc_diskstats.log");
316
317 unlink("proc_ps.log");
318 rmdir(tempdir);
319 }
320
321
322
323
324}
325
326
327
328
329
330
331
332
333
334
335
336int bootchartd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
337int bootchartd_main(int argc UNUSED_PARAM, char **argv)
338{
339 unsigned sample_period_us;
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,
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
371
372
373
374 sample_period_us = 200 * 1000;
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])
382 sample_period_us = atof(token[1]) * 1000000;
383 }
384 config_close(parser);
385 }
386 if ((int)sample_period_us <= 0)
387 sample_period_us = 1;
388
389
390 logger_pid = fork_or_rexec(argv);
391
392 if (logger_pid == 0) {
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
406 raise(SIGSTOP);
407
408
409
410
411 if (cmd == CMD_PID1 && !getenv("PATH"))
412 putenv((char*)bb_PATH_root_path);
413
414 tempdir = make_tempdir();
415 do_logging(sample_period_us);
416 finalize(tempdir, cmd == CMD_START ? argv[2] : NULL);
417 return EXIT_SUCCESS;
418 }
419
420
421
422 if (DO_SIGNAL_SYNC) {
423
424
425
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);
437 bb_perror_msg_and_die("can't execute '%s'", "/sbin/init");
438 }
439
440 if (cmd == CMD_START && argv[2]) {
441 pid_t pid = xvfork();
442 if (pid == 0) {
443 argv += 2;
444 execvp(argv[0], argv);
445 bb_perror_msg_and_die("can't execute '%s'", argv[0]);
446 }
447
448 waitpid(pid, NULL, 0);
449 kill(logger_pid, SIGUSR1);
450 }
451
452 return EXIT_SUCCESS;
453}
454