潘小安 2019-06-28
BusyBox是一个遵循GPL协议、以自由软件形式发行的应用程序。Busybox在单一的可执行文件中提供了精简的Unix工具集,可运行于多款POSIX环境的操作系统,例如Linux(包括Android[6])、Hurd[7]、FreeBSD8等等。由于BusyBox可执行文件的文件大小比较小、并通常使用Linux内核,这使得它非常适合使用于嵌入式系统。作者将BusyBox称为“嵌入式Linux的瑞士军刀”
BusyBox 内嵌了许多 Linux 常用的工具, cp,mv,cron 等。
此次分析 BusyBox 中的 cron 流程。原代码在此
流程如下



用户输入的 cron 配置经过解析后,存到如下两个结构体中。
typedef struct CronFile {
struct CronFile *cf_next;
struct CronLine *cf_lines;
char *cf_username;
smallint cf_wants_starting; /* bool: one or more jobs ready */
smallint cf_has_running; /* bool: one or more jobs running */
smallint cf_deleted; /* marked for deletion (but still has running jobs) */
} CronFile;
typedef struct CronLine {
struct CronLine *cl_next;
char *cl_cmd; /* shell command */
pid_t cl_pid; /* >0:running, <0:needs to be started in this minute, 0:dormant */
#define START_ME_REBOOT -2
#define START_ME_NORMAL -1
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
int cl_empty_mail_size; /* size of mail header only, 0 if no mailfile */
char *cl_mailto; /* whom to mail results, may be NULL */
#endif
char *cl_shell;
/* ordered by size, not in natural order. makes code smaller: */
char cl_Dow[7]; /* 0-6, beginning sunday */
char cl_Mons[12]; /* 0-11 */
char cl_Hrs[24]; /* 0-23 */
char cl_Days[32]; /* 1-31 */
char cl_Mins[60]; /* 0-59 */
} CronLine;