1 /* $OpenBSD: job.c,v 1.7 2004/06/17 22:11:55 millert Exp $ */
2
3 /* Copyright 1988,1990,1993,1994 by Paul Vixie
4 * All rights reserved
5 */
6
7 /*
8 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
9 * Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
10 *
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24 #if !defined(lint) && !defined(LINT)
25 static char const rcsid[] = "$OpenBSD: job.c,v 1.7 2004/06/17 22:11:55 millert Exp $";
26 #endif
27
28 #include "cron.h"
29
30 typedef struct _job {
31 struct _job *next;
32 entry *e;
33 user *u;
34 } job;
35
36 static job *jhead = NULL, *jtail = NULL;
37
38 void
job_add(entry * e,user * u)39 job_add(entry *e, user *u) {
40 job *j;
41
42 /* if already on queue, keep going */
43 for (j = jhead; j != NULL; j = j->next)
44 if (j->e == e && j->u == u)
45 return;
46
47 /* build a job queue element */
48 if ((j = (job *)malloc(sizeof(job))) == NULL)
49 return;
50 j->next = NULL;
51 j->e = e;
52 j->u = u;
53
54 /* add it to the tail */
55 if (jhead == NULL)
56 jhead = j;
57 else
58 jtail->next = j;
59 jtail = j;
60 }
61
62 int
job_runqueue(void)63 job_runqueue(void) {
64 job *j, *jn;
65 int run = 0;
66
67 for (j = jhead; j; j = jn) {
68 do_command(j->e, j->u);
69 jn = j->next;
70 free(j);
71 run++;
72 }
73 jhead = jtail = NULL;
74 return (run);
75 }
76