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