1 /* bootchartd.c - bootchartd is commonly used to profile the boot process.
2 *
3 * Copyright 2014 Bilal Qureshi <bilal.jmi@gmail.com>
4 * Copyright 2014 Kyungwan Han <asura321@gmail.com>
5 *
6 * No Standard
7
8 USE_BOOTCHARTD(NEWTOY(bootchartd, 0, TOYFLAG_STAYROOT|TOYFLAG_USR|TOYFLAG_BIN))
9
10 config BOOTCHARTD
11 bool "bootchartd"
12 default n
13 depends on TOYBOX_FORK
14 help
15 usage: bootchartd {start [PROG ARGS]}|stop|init
16
17 Create /var/log/bootlog.tgz with boot chart data
18
19 start: start background logging; with PROG, run PROG,
20 then kill logging with USR1
21 stop: send USR1 to all bootchartd processes
22 init: start background logging; stop when getty/xdm is seen
23 (for init scripts)
24
25 Under PID 1: as init, then exec $bootchart_init, /init, /sbin/init
26 */
27
28 #define FOR_bootchartd
29 #include "toys.h"
30
GLOBALS(char buf[32];long msec;int proc_accounting;pid_t pid;)31 GLOBALS(
32 char buf[32];
33 long msec;
34 int proc_accounting;
35
36 pid_t pid;
37 )
38
39 static void dump_data_in_file(char *fname, int wfd)
40 {
41 int rfd = open(fname, O_RDONLY);
42
43 if (rfd != -1) {
44 xwrite(wfd, TT.buf, strlen(TT.buf));
45 xsendfile(rfd, wfd);
46 close(rfd);
47 xwrite(wfd, "\n", 1);
48 }
49 }
50
dump_proc_data(FILE * fp)51 static int dump_proc_data(FILE *fp)
52 {
53 struct dirent *pid_dir;
54 int login_flag = 0;
55 pid_t pid;
56 DIR *proc_dir = opendir("/proc");
57
58 fputs(TT.buf, fp);
59 while ((pid_dir = readdir(proc_dir))) {
60 char filename[64];
61 int fd;
62
63 if (!isdigit(pid_dir->d_name[0])) continue;
64 sscanf(pid_dir->d_name, "%d", &pid);
65 sprintf(filename, "/proc/%d/stat", pid);
66 if ((fd = open(filename, O_RDONLY)) != -1 ) {
67 char *ptr;
68 ssize_t len;
69
70 if ((len = readall(fd, toybuf, sizeof(toybuf)-1)) < 0) {
71 xclose(fd);
72 continue;
73 }
74 toybuf[len] = '\0';
75 close(fd);
76 fputs(toybuf, fp);
77 if (TT.pid != 1) continue;
78 if ((ptr = strchr(toybuf, '('))) {
79 char *tmp = strchr(++ptr, ')');
80
81 if (tmp) *tmp = '\0';
82 }
83 // Checks for gdm, kdm or getty
84 if (((ptr[0] == 'g' || ptr[0] == 'k' || ptr[0] == 'x') && ptr[1] == 'd'
85 && ptr[2] == 'm') || strstr(ptr, "getty")) login_flag = 1;
86 }
87 }
88 closedir(proc_dir);
89 fputc('\n', fp);
90 return login_flag;
91 }
92
parse_config_file(char * fname)93 static int parse_config_file(char *fname)
94 {
95 size_t len = 0;
96 char *line = 0;
97 FILE *fp = fopen(fname, "r");
98
99 if (!fp) return 0;
100 for (;getline(&line, &len, fp) != -1; line = 0) {
101 char *ptr = line;
102
103 while (*ptr == ' ' || *ptr == '\t') ptr++;
104 if (!*ptr || *ptr == '#' || *ptr == '\n') continue;
105 if (strstart(&ptr, "SAMPLE_PERIOD=")) {
106 double dd;
107
108 sscanf(ptr, "%lf", &dd);
109 if ((TT.msec = dd*1000)<1) TT.msec = 1;
110 } else if (strstart(&ptr, "PROCESS_ACCOUNTING="))
111 if (strstart(&ptr, "\"on\"") || strstart(&ptr, "\"yes\""))
112 TT.proc_accounting = 1;
113 free(line);
114 }
115 fclose(fp);
116 return 1;
117 }
118
create_tmp_dir()119 static char *create_tmp_dir()
120 {
121 char *dir_list[] = {"/tmp", "/mnt", "/boot", "/proc"}, **target = dir_list;
122 char *dir, dir_path[] = "/tmp/bootchart.XXXXXX";
123
124 if ((dir = mkdtemp(dir_path))) {
125 xchdir((dir = xstrdup(dir)));
126 return dir;
127 }
128 while (mount("none", *target, "tmpfs", (1<<15), "size=16m")) //MS_SILENT
129 if (!++target) perror_exit("can't mount tmpfs");
130 xchdir(*target);
131 if (umount2(*target, MNT_DETACH)) perror_exit("Can't unmount tmpfs");
132 return *target;
133 }
134
start_logging()135 static void start_logging()
136 {
137 int proc_stat_fd = xcreate("proc_stat.log",
138 O_WRONLY | O_CREAT | O_TRUNC, 0644);
139 int proc_diskstats_fd = xcreate("proc_diskstats.log",
140 O_WRONLY | O_CREAT | O_TRUNC, 0644);
141 FILE *proc_ps_fp = xfopen("proc_ps.log", "w");
142 long tcnt = 60 * 1000 / TT.msec;
143
144 if (tcnt <= 0) tcnt = 1;
145 if (TT.proc_accounting) {
146 int kp_fd = xcreate("kernel_procs_acct", O_WRONLY | O_CREAT | O_TRUNC,0666);
147
148 xclose(kp_fd);
149 acct("kernel_procs_acct");
150 }
151 memset(TT.buf, 0, sizeof(TT.buf));
152 while (--tcnt && !toys.signal) {
153 int i = 0, j = 0, fd = open("/proc/uptime", O_RDONLY);
154 if (fd < 0) goto wait;
155 char *line = get_line(fd);
156
157 if (!line) goto wait;
158 while (line[i] != ' ') {
159 if (line[i] == '.') {
160 i++;
161 continue;
162 }
163 TT.buf[j++] = line[i++];
164 }
165 TT.buf[j++] = '\n';
166 TT.buf[j] = 0;
167 free(line);
168 close(fd);
169 dump_data_in_file("/proc/stat", proc_stat_fd);
170 dump_data_in_file("/proc/diskstats", proc_diskstats_fd);
171 // stop proc dumping in 2 secs if getty or gdm, kdm, xdm found
172 if (dump_proc_data(proc_ps_fp))
173 if (tcnt > 2 * 1000 / TT.msec) tcnt = 2 * 1000 / TT.msec;
174 fflush(0);
175 wait:
176 msleep(TT.msec);
177 }
178 xclose(proc_stat_fd);
179 xclose(proc_diskstats_fd);
180 fclose(proc_ps_fp);
181 }
182
stop_logging(char * tmp_dir,char * prog)183 static void stop_logging(char *tmp_dir, char *prog)
184 {
185 char host_name[32];
186 int kcmd_line_fd;
187 time_t t;
188 struct tm st;
189 struct utsname uts;
190 FILE *hdr_fp = xfopen("header", "w");
191
192 if (TT.proc_accounting) acct(NULL);
193 if (prog) fprintf(hdr_fp, "profile.process = %s\n", prog);
194 gethostname(host_name, sizeof(host_name));
195 time(&t);
196 localtime_r(&t, &st);
197 memset(toybuf, 0, sizeof(toybuf));
198 strftime(toybuf, sizeof(toybuf), "%a %b %e %H:%M:%S %Z %Y", &st);
199 fprintf(hdr_fp, "version = TBX_BCHARTD_VER 1.0.0\n");
200 fprintf(hdr_fp, "title = Boot chart for %s (%s)\n", host_name, toybuf);
201 if (uname(&uts) < 0) perror_exit("uname");
202 fprintf(hdr_fp, "system.uname = %s %s %s %s\n", uts.sysname, uts.release,
203 uts.version, uts.machine);
204 memset(toybuf, 0, sizeof(toybuf));
205 if ((kcmd_line_fd = open("/proc/cmdline", O_RDONLY)) != -1) {
206 ssize_t len;
207
208 if ((len = readall(kcmd_line_fd, toybuf, sizeof(toybuf)-1)) > 0) {
209 toybuf[len] = 0;
210 while (--len >= 0 && !toybuf[len]) continue;
211 for (; len > 0; len--) if (toybuf[len] < ' ') toybuf[len] = ' ';
212 } else *toybuf = 0;
213 }
214 fprintf(hdr_fp, "system.kernel.options = %s", toybuf);
215 close(kcmd_line_fd);
216 fclose(hdr_fp);
217 memset(toybuf, 0, sizeof(toybuf));
218 snprintf(toybuf, sizeof(toybuf), "tar -zcf /var/log/bootlog.tgz header %s *.log",
219 TT.proc_accounting ? "kernel_procs_acct" : "");
220 system(toybuf);
221 if (tmp_dir) {
222 unlink("header");
223 unlink("proc_stat.log");
224 unlink("proc_diskstats.log");
225 unlink("proc_ps.log");
226 if (TT.proc_accounting) unlink("kernel_procs_acct");
227 rmdir(tmp_dir);
228 }
229 }
230
signal_pid(pid_t pid,char * name)231 static int signal_pid(pid_t pid, char *name)
232 {
233 if (pid != TT.pid) kill(pid, SIGUSR1);
234 return 0;
235 }
236
bootchartd_main()237 void bootchartd_main()
238 {
239 pid_t lgr_pid;
240 int bchartd_opt = 0; // 0=PID1, 1=start, 2=stop, 3=init
241
242 TT.pid = getpid();
243 TT.msec = 200;
244
245 if (*toys.optargs) {
246 if (!strcmp("start", *toys.optargs)) bchartd_opt = 1;
247 else if (!strcmp("stop", *toys.optargs)) bchartd_opt = 2;
248 else if (!strcmp("init", *toys.optargs)) bchartd_opt = 3;
249 else error_exit("Unknown option '%s'", *toys.optargs);
250
251 if (bchartd_opt == 2) {
252 char *process_name[] = {"bootchartd", NULL};
253
254 names_to_pid(process_name, signal_pid, 0);
255 return;
256 }
257 } else if (TT.pid != 1) error_exit("not PID 1");
258
259 // Execute the code below for start or init or PID1
260 if (!parse_config_file("bootchartd.conf"))
261 parse_config_file("/etc/bootchartd.conf");
262
263 memset(toybuf, 0, sizeof(toybuf));
264 if (!(lgr_pid = xfork())) {
265 char *tmp_dir = create_tmp_dir();
266
267 sigatexit(generic_signal);
268 raise(SIGSTOP);
269 if (!bchartd_opt && !getenv("PATH"))
270 putenv("PATH=/sbin:/usr/sbin:/bin:/usr/bin");
271 start_logging();
272 stop_logging(tmp_dir, bchartd_opt == 1 ? toys.optargs[1] : NULL);
273 return;
274 }
275 waitpid(lgr_pid, NULL, WUNTRACED);
276 kill(lgr_pid, SIGCONT);
277
278 if (!bchartd_opt) {
279 char *pbchart_init = getenv("bootchart_init");
280
281 if (pbchart_init) execl(pbchart_init, pbchart_init, NULL);
282 execl("/init", "init", (void *)0);
283 execl("/sbin/init", "init", (void *)0);
284 }
285 if (bchartd_opt == 1 && toys.optargs[1]) {
286 pid_t prog_pid;
287
288 if (!(prog_pid = xfork())) xexec(toys.optargs+1);
289 waitpid(prog_pid, NULL, 0);
290 kill(lgr_pid, SIGUSR1);
291 }
292 }
293