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