No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / cron / job.c
blobd3cebff7f58127ac0dd2695ba00b2d0094ee6555
1 /* $NetBSD: job.c,v 1.5 2003/08/13 03:51:15 atatat Exp $ */
3 /* Copyright 1988,1990,1993,1994 by Paul Vixie
4 * All rights reserved
6 * Distribute freely, except: don't remove my name from the source or
7 * documentation (don't take credit for my work), mark your changes (don't
8 * get me blamed for your possible bugs), don't alter or remove this
9 * notice. May be sold if buildable source is provided to buyer. No
10 * warrantee of any kind, express or implied, is included with this
11 * software; use at your own risk, responsibility for damages (if any) to
12 * anyone resulting from the use of this software rests entirely with the
13 * user.
15 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
16 * I'll try to keep a version up to date. I can be reached as follows:
17 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
20 #include <sys/cdefs.h>
21 #include <errno.h>
22 #if SYS_TIME_H
23 # include <sys/time.h>
24 #else
25 # include <time.h>
26 #endif
27 #if !defined(lint) && !defined(LINT)
28 #if 0
29 static char rcsid[] = "Id: job.c,v 1.6 1994/01/15 20:43:43 vixie Exp";
30 #else
31 __RCSID("$NetBSD: job.c,v 1.5 2003/08/13 03:51:15 atatat Exp $");
32 #endif
33 #endif
36 #include "cron.h"
39 typedef struct _job {
40 struct _job *next;
41 entry *e;
42 user *u;
43 time_t t;
44 } job;
47 static job *jhead = NULL, *jtail = NULL;
50 static int okay_to_go(job *);
53 void
54 job_add(entry *e, user *u)
56 job *j;
58 /* if already on queue, keep going */
59 for (j=jhead; j; j=j->next)
60 if (j->e == e && j->u == u) {
61 j->t = TargetTime;
62 return;
65 /* build a job queue element */
66 j = (job*)malloc(sizeof(job));
67 j->next = (job*) NULL;
68 j->e = e;
69 j->u = u;
70 j->t = TargetTime;
72 /* add it to the tail */
73 if (!jhead) { jhead=j; }
74 else { jtail->next=j; }
75 jtail = j;
79 int
80 job_runqueue(void)
82 job *j, *jn;
83 int run = 0;
85 for (j=jhead; j; j=jn) {
86 if (okay_to_go(j))
87 do_command(j->e, j->u);
88 else {
89 char *x = mkprints((u_char *)j->e->cmd,
90 strlen(j->e->cmd));
91 char *usernm = env_get("LOGNAME", j->e->envp);
93 log_it(usernm, getpid(), "CMD (skipped)", x);
94 free(x);
96 jn = j->next;
97 free(j);
98 run++;
100 jhead = jtail = NULL;
101 return run;
105 static int
106 okay_to_go(job *j)
108 char *within, *t;
109 int delta;
111 if (j->e->flags & WHEN_REBOOT)
112 return (1);
114 within = env_get("CRON_WITHIN", j->e->envp);
115 if (within == NULL)
116 return (1);
118 /* XXX handle 2m, 4h, etc? */
119 errno = 0;
120 delta = strtol(within, &t, 10);
121 if (errno == ERANGE || *t != '\0' || delta <= 0)
122 return (1);
124 return ((j->t + delta) > time(NULL));