xref: /openbsd/usr.sbin/cron/job.c (revision 898184e3)
1 /*	$OpenBSD: job.c,v 1.8 2009/10/27 23:59:51 deraadt 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 #include "cron.h"
25 
26 typedef	struct _job {
27 	struct _job	*next;
28 	entry		*e;
29 	user		*u;
30 } job;
31 
32 static job	*jhead = NULL, *jtail = NULL;
33 
34 void
35 job_add(entry *e, user *u) {
36 	job *j;
37 
38 	/* if already on queue, keep going */
39 	for (j = jhead; j != NULL; j = j->next)
40 		if (j->e == e && j->u == u)
41 			return;
42 
43 	/* build a job queue element */
44 	if ((j = (job *)malloc(sizeof(job))) == NULL)
45 		return;
46 	j->next = NULL;
47 	j->e = e;
48 	j->u = u;
49 
50 	/* add it to the tail */
51 	if (jhead == NULL)
52 		jhead = j;
53 	else
54 		jtail->next = j;
55 	jtail = j;
56 }
57 
58 int
59 job_runqueue(void) {
60 	job *j, *jn;
61 	int run = 0;
62 
63 	for (j = jhead; j; j = jn) {
64 		do_command(j->e, j->u);
65 		jn = j->next;
66 		free(j);
67 		run++;
68 	}
69 	jhead = jtail = NULL;
70 	return (run);
71 }
72