TODO:
-
module design
./minicrontab &
fork -> fork -> the running instance -> fork to create scheduler -> fork job 1,2..N
-
study process session
-
redirect input/output
-
how to handle user to modify configuration file
-
sleep interval
-
other detailed implementation
Time conversion algorithm:
- The daemon process has start time
- The schedule process has the now time
- So the clockTime = startTime + ( nowTime - startTime ) * rate
- We can use clockTime to choose which job should be executed now.
study the http://enterprise-storage-os.googlecode.com/files/vixie-cron-4.1.tar.bz2
ISC cron tool code analysis
- fork processes to setup the correct process structure
- close input output stderr
- load the cron data to the cron database(read from files)
- calculate time and sleep
- check all the entry in database whether it should be run now
- add the to be running job into job queue
- run the job from job queue
- loop to step 3
data structure:
typedef struct _user {
struct _user *next, *prev; /* links */
char *name;
time_t mtime; /* last modtime of crontab */
entry *crontab; /* this person's crontab */
} user;
typedef struct _cron_db {
user *head, *tail; /* links */
time_t mtime; /* last modtime on spooldir */
} cron_db;
typedef struct _entry {
struct _entry *next;
struct passwd *pwd;
char **envp;
char *cmd;
char min[60];
char hour[12];
char dom[31]; // day of month
char month[12];
char dow[7]; // day of week
int flags;
#define MIN_STAR 0x01
#define HR_STAR 0x02
#define DOM_STAR 0x04
#define DOW_STAR 0x08
#define WHEN_REBOOT 0x10
#define DONT_LOG 0x20
} entry;
typedef struct _job {
struct _job *next;
entry *e;
user *u;
} job;
static job *jhead = NULL, *jtail = NULL;