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