2005-03-16 05:53:55 +03:00
|
|
|
/* $NetBSD: job.c,v 1.6 2005/03/16 02:53:55 xtraeme Exp $ */
|
1997-03-13 09:19:07 +03:00
|
|
|
|
1994-01-12 21:35:59 +03:00
|
|
|
/* Copyright 1988,1990,1993,1994 by Paul Vixie
|
1994-01-05 23:40:12 +03:00
|
|
|
* All rights reserved
|
|
|
|
*
|
|
|
|
* Distribute freely, except: don't remove my name from the source or
|
|
|
|
* documentation (don't take credit for my work), mark your changes (don't
|
|
|
|
* get me blamed for your possible bugs), don't alter or remove this
|
|
|
|
* notice. May be sold if buildable source is provided to buyer. No
|
|
|
|
* warrantee of any kind, express or implied, is included with this
|
|
|
|
* software; use at your own risk, responsibility for damages (if any) to
|
|
|
|
* anyone resulting from the use of this software rests entirely with the
|
|
|
|
* user.
|
|
|
|
*
|
|
|
|
* Send bug reports, bug fixes, enhancements, requests, flames, etc., and
|
|
|
|
* I'll try to keep a version up to date. I can be reached as follows:
|
|
|
|
* Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
|
|
|
|
*/
|
|
|
|
|
1998-01-31 17:40:13 +03:00
|
|
|
#include <sys/cdefs.h>
|
2002-04-25 18:45:05 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#if SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#else
|
|
|
|
# include <time.h>
|
|
|
|
#endif
|
1994-01-05 23:40:12 +03:00
|
|
|
#if !defined(lint) && !defined(LINT)
|
1998-01-31 17:40:13 +03:00
|
|
|
#if 0
|
|
|
|
static char rcsid[] = "Id: job.c,v 1.6 1994/01/15 20:43:43 vixie Exp";
|
|
|
|
#else
|
2005-03-16 05:53:55 +03:00
|
|
|
__RCSID("$NetBSD: job.c,v 1.6 2005/03/16 02:53:55 xtraeme Exp $");
|
1998-01-31 17:40:13 +03:00
|
|
|
#endif
|
1994-01-05 23:40:12 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#include "cron.h"
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct _job {
|
|
|
|
struct _job *next;
|
|
|
|
entry *e;
|
|
|
|
user *u;
|
2002-04-25 18:45:05 +04:00
|
|
|
time_t t;
|
1994-01-05 23:40:12 +03:00
|
|
|
} job;
|
|
|
|
|
|
|
|
|
|
|
|
static job *jhead = NULL, *jtail = NULL;
|
|
|
|
|
|
|
|
|
2005-03-16 05:53:55 +03:00
|
|
|
static int okay_to_go(job *);
|
2002-04-25 18:45:05 +04:00
|
|
|
|
|
|
|
|
1994-01-05 23:40:12 +03:00
|
|
|
void
|
2005-03-16 05:53:55 +03:00
|
|
|
job_add(entry *e, user *u)
|
1994-01-05 23:40:12 +03:00
|
|
|
{
|
1998-01-31 17:40:13 +03:00
|
|
|
job *j;
|
1994-01-05 23:40:12 +03:00
|
|
|
|
|
|
|
/* if already on queue, keep going */
|
|
|
|
for (j=jhead; j; j=j->next)
|
2002-04-25 18:45:05 +04:00
|
|
|
if (j->e == e && j->u == u) {
|
|
|
|
j->t = TargetTime;
|
|
|
|
return;
|
|
|
|
}
|
1994-01-05 23:40:12 +03:00
|
|
|
|
|
|
|
/* build a job queue element */
|
|
|
|
j = (job*)malloc(sizeof(job));
|
|
|
|
j->next = (job*) NULL;
|
|
|
|
j->e = e;
|
|
|
|
j->u = u;
|
2002-04-25 18:45:05 +04:00
|
|
|
j->t = TargetTime;
|
1994-01-05 23:40:12 +03:00
|
|
|
|
|
|
|
/* add it to the tail */
|
|
|
|
if (!jhead) { jhead=j; }
|
|
|
|
else { jtail->next=j; }
|
|
|
|
jtail = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-03-16 05:53:55 +03:00
|
|
|
job_runqueue(void)
|
1994-01-05 23:40:12 +03:00
|
|
|
{
|
1998-01-31 17:40:13 +03:00
|
|
|
job *j, *jn;
|
|
|
|
int run = 0;
|
1994-01-05 23:40:12 +03:00
|
|
|
|
|
|
|
for (j=jhead; j; j=jn) {
|
2002-04-25 18:45:05 +04:00
|
|
|
if (okay_to_go(j))
|
|
|
|
do_command(j->e, j->u);
|
|
|
|
else {
|
|
|
|
char *x = mkprints((u_char *)j->e->cmd,
|
|
|
|
strlen(j->e->cmd));
|
|
|
|
char *usernm = env_get("LOGNAME", j->e->envp);
|
|
|
|
|
|
|
|
log_it(usernm, getpid(), "CMD (skipped)", x);
|
|
|
|
free(x);
|
|
|
|
}
|
1994-01-05 23:40:12 +03:00
|
|
|
jn = j->next;
|
|
|
|
free(j);
|
|
|
|
run++;
|
|
|
|
}
|
|
|
|
jhead = jtail = NULL;
|
|
|
|
return run;
|
|
|
|
}
|
2002-04-25 18:45:05 +04:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
2005-03-16 05:53:55 +03:00
|
|
|
okay_to_go(job *j)
|
2002-04-25 18:45:05 +04:00
|
|
|
{
|
|
|
|
char *within, *t;
|
|
|
|
int delta;
|
|
|
|
|
2003-08-13 07:51:15 +04:00
|
|
|
if (j->e->flags & WHEN_REBOOT)
|
|
|
|
return (1);
|
|
|
|
|
2002-04-25 18:45:05 +04:00
|
|
|
within = env_get("CRON_WITHIN", j->e->envp);
|
|
|
|
if (within == NULL)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
/* XXX handle 2m, 4h, etc? */
|
|
|
|
errno = 0;
|
|
|
|
delta = strtol(within, &t, 10);
|
|
|
|
if (errno == ERANGE || *t != '\0' || delta <= 0)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
return ((j->t + delta) > time(NULL));
|
|
|
|
}
|