8000 minix/commands/cron/tab.c at v3.2.0 · blackskygg/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
816 lines (740 loc) · 18.7 KB

tab.c

File metadata and controls

816 lines (740 loc) · 18.7 KB
/* Comment or empty. */
while (*p != 0 && *p++ != '\n') {}
continue;
}
/* One new job coming up. */
*ajob= job= allocate(sizeof(*job));
*(ajob= &job->next)= nil;
job->tab= tab;
if (!range_parse(file, q, job->min, 0, 59, &wc)) {
ok= 0;
break;
}
q= get_token(&p);
if (!range_parse(file, q, job->hour, 0, 23, &wc)) {
ok= 0;
break;
}
q= get_token(&p);
if (!range_parse(file, q, job->mday, 1, 31, &wc)) {
ok= 0;
break;
}
job->do_mday= !wc;
q= get_token(&p);
if (!range_parse(file, q, job->mon, 1, 12, &wc)) {
ok= 0;
break;
}
job->do_mday |= !wc;
q= get_token(&p);
if (!range_parse(file, q, job->wday, 0, 7, &wc)) {
ok= 0;
break;
}
job->do_wday= !wc;
/* 7 is Sunday, but 0 is a common mistake because it is in the
* tm_wday range. We allow and even prefer it internally.
*/
if (bit_isset(job->wday, 7)) {
bit_clr(job->wday, 7);
bit_set(job->wday, 0);
}
/* The month range is 1-12, but tm_mon likes 0-11. */
job->mon[0] >>= 1;
if (bit_isset(job->mon, 8)) bit_set(job->mon, 7);
job->mon[1] >>= 1;
/* Scan for options. */
job->user= nil;
while (q= get_token(&p), *q == '-') {
q++;
if (q[0] == '-' && q+1 == p) {
/* -- */
q= get_token(&p);
break;
}
while (q < p) switch (*q++) {
case 'u':
if (q == p) q= get_token(&p);
if (q == p) goto usage;
memmove(q-1, q, p-q); /* gross... */
p[-1]= 0;
job->user= q-1;
q= p;
break;
default:
usage:
log(LOG_ERR,
"%s: bad option -%c, good options are: -u username\n",
file, q[-1]);
ok= 0;
goto endtab;
}
}
/* A crontab owned by a user can only do things as that user. */
if (tab->user != nil) job->user= tab->user;
/* Inspect the first character of the command. */
job->cmd= q;
if (q == p || *q == '#') {
/* Rest of the line is empty, i.e. the commands are on
* the following lines indented by one tab.
*/
while (*p != 0 && *p++ != '\n') {}
if (*p++ != '\t') {
log(LOG_ERR, "%s: contains an empty command\n",
file);
ok= 0;
goto endtab;
}
while (*p != 0) {
if ((*q = *p++) == '\n') {
if (*p != '\t') break;
p++;
}
q++;
}
} else {
/* The command is on this line. Alas we must now be
* backwards compatible and change %'s to newlines.
*/
p= q;
while (*p != 0) {
if ((*q = *p++) == '\n') break;
if (*q == '%') *q= '\n';
q++;
}
}
*q= 0;
job->rtime= now;
job->late= 0; /* It is on time. */
job->atjob= 0; /* True cron job. */
job->pid= IDLE_PID; /* Not running yet. */
tab_reschedule(job); /* Compute next time to run. */
}
endtab:
if (ok) tab->current= 1;
}
void tab_find_atjob(char *atdir)
/* Find the first to be executed AT job and kludge up an crontab job for it.
* We set tab->file to "atdir/jobname", tab->data to "atdir/past/jobname",
* and job->cmd to "jobname".
*/
{
DIR *spool;
struct dirent *entry;
time_t t0, t1;
struct tm tmnow, tmt1;
static char template[] = "96.365.1546.00";
char firstjob[sizeof(template)];
int i;
crontab_t *tab;
cronjob_t *job;
if ((spool= opendir(atdir)) == nil) return;
tmnow= *localtime(&now);
t0= NEVER;
while ((entry= readdir(spool)) != nil) {
/* Check if the name fits the template. */
for (i= 0; template[i] != 0; i++) {
if (template[i] == '.') {
if (entry->d_name[i] != '.') break;
} else {
if (!isdigit(entry->d_name[i])) break;
}
}
if (template[i] != 0 || entry->d_name[i] != 0) continue;
/* Convert the name to a time. Careful with the century. */
memset(&tmt1, 0, sizeof(tmt1));
tmt1.tm_year= atoi(entry->d_name+0);
while (tmt1.tm_year < tmnow.tm_year-10) tmt1.tm_year+= 100;
tmt1.tm_mday= 1+atoi(entry->d_name+3);
tmt1.tm_min= atoi(entry->d_name+7);
tmt1.tm_hour= tmt1.tm_min / 100;
tmt1.tm_min%= 100;
tmt1.tm_isdst= -1;
if ((t1= mktime(&tmt1)) == -1) {
/* Illegal time? Try in winter time. */
tmt1.tm_isdst= 0;
if ((t1= mktime(&tmt1)) == -1) continue;
}
if (t1 < t0) {
t0= t1;
strcpy(firstjob, entry->d_name);
}
}
closedir(spool);
if (t0 == NEVER) return; /* AT job spool is empty. */
/* Create new table and job structures. */
tab= allocate(sizeof(*tab));
tab->file= allocate((strlen(atdir) + 1 + sizeof(template))
* sizeof(tab->file[0]));
strcpy(tab->file, atdir);
strcat(tab->file, "/");
strcat(tab->file, firstjob);
tab->data= allocate((strlen(atdir) + 6 + sizeof(template))
* sizeof(tab->data[0]));
strcpy(tab->data, atdir);
strcat(tab->data, "/past/");
strcat(tab->data, firstjob);
tab->user= nil;
tab->mtime= 0;
tab->current= 1;
tab->next= crontabs;
crontabs= tab;
tab->jobs= job= allocate(sizeof(*job));
job->next= nil;
job->tab= tab;
job->user= nil;
job->cmd= tab->data + strlen(atdir) + 6;
job->rtime= t0;
job->late= 0;
job->atjob= 1; /* One AT job disguised as a cron job. */
job->pid= IDLE_PID;
if (job->rtime < next) next= job->rtime;
}
void tab_purge(void)
/* Remove table data that is no longer current. E.g. a crontab got removed.
*/
{
crontab_t **atab, *tab;
cronjob_t *job;
atab= &crontabs;
while ((tab= *atab) != nil) {
if (tab->current) {
/* Table is fine. */
tab->current= 0;
atab= &tab->next;
} else {
/* Table is not, or no longer valid; delete. */
*atab= tab->next;
while ((job= tab->jobs) != nil) {
tab->jobs= job->next;
deallocate(job);
}
deallocate(tab->data);
deallocate(tab->file);
deallocate(tab->user);
deallocate(tab);
}
}
}
static cronjob_t *reap_or_find(pid_t pid)
/* Find a finished job or search for the next one to run. */
{
crontab_t *tab;
cronjob_t *job;
cronjob_t *nextjob;
nextjob= nil;
next= NEVER;
for (tab= crontabs; tab != nil; tab= tab->next) {
for (job= tab->jobs; job != nil; job= job->next) {
if (job->pid == pid) {
job->pid= IDLE_PID;
tab_reschedule(job);
}
if (job->pid != IDLE_PID) continue;
if (job->rtime < next) next= job->rtime;
if (job->rtime <= now) nextjob= job;
}
}
return nextjob;
}
void tab_reap_job(pid_t pid)
/* A job has finished. Try to find it among the crontab data and reschedule
* it. Recompute time next to run a job.
*/
{
(void) reap_or_find(pid);
}
cronjob_t *tab_nextjob(void)
/* Find a job that should be run now. If none are found return null.
* Update 'next'.
*/
{
return reap_or_find(NO_PID);
}
static void pr_map(FILE *fp, bitmap_t map)
{
int last_bit= -1, bit;
char *sep;
sep= "";
for (bit= 0; bit < 64; bit++) {
if (bit_isset(map, bit)) {
if (last_bit == -1) last_bit= bit;
} else {
if (last_bit != -1) {
fprintf(fp, "%s%d", sep, last_bit);
if (last_bit != bit-1) {
fprintf(fp, "-%d", bit-1);
}
last_bit= -1;
sep= ",";
}
}
}
}
void tab_print(FILE *fp)
/* Print out a stored crontab file for debugging purposes. */
{
crontab_t *tab;
cronjob_t *job;
char *p;
struct tm tm;
for (tab= crontabs; tab != nil; tab= tab->next) {
fprintf(fp, "tab->file = \"%s\"\n", tab->file);
fprintf(fp, "tab->user = \"%s\"\n",
tab->user == nil ? "(root)" : tab->user);
fprintf(fp, "tab->mtime = %s", ctime(&tab->mtime));
for (job= tab->jobs; job != nil; job= job->next) {
if (job->atjob) {
fprintf(fp, "AT job");
} else {
pr_map(fp, job->min); fputc(' ', fp);
pr_map(fp, job->hour); fputc(' ', fp);
pr_map(fp, job->mday); fputc(' ', fp);
pr_map(fp, job->mon); fputc(' ', fp);
pr_map(fp, job->wday);
}
if (job->user != nil && job->user != tab->user) {
fprintf(fp, " -u %s", job->user);
}
fprintf(fp, "\n\t");
for (p= job->cmd; *p != 0; p++) {
fputc(*p, fp);
if (*p == '\n') fputc('\t', fp);
}
fputc('\n', fp);
tm= *localtime(&job->rtime);
fprintf(fp, " rtime = %.24s%s\n", asctime(&tm),
tm.tm_isdst ? " (DST)" : "");
if (job->pid != IDLE_PID) {
fprintf(fp, " pid = %ld\n", (long) job->pid);
}
}
}
}
/*
* $PchId: tab.c,v 1.5 2000/07/25 22:07:51 philip Exp $
*/
0