1 /* $NetBSD: job.c,v 1.2 2010/05/06 18:53:17 christos 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 #include <sys/cdefs.h> 24 25 #if !defined(lint) && !defined(LINT) 26 #if 0 27 static char rcsid[] = "Id: job.c,v 1.6 2004/01/23 18:56:43 vixie Exp"; 28 #else 29 __RCSID("$NetBSD: job.c,v 1.2 2010/05/06 18:53:17 christos Exp $"); 30 #endif 31 #endif 32 33 #include "cron.h" 34 35 typedef struct _job { 36 struct _job *next; 37 entry *e; 38 user *u; 39 time_t t; 40 } job; 41 42 static job *jhead = NULL, *jtail = NULL; 43 44 static int okay_to_go(job *); 45 46 void 47 job_add(entry *e, user *u, time_t target_time) { 48 job *j; 49 50 /* if already on queue, keep going */ 51 for (j = jhead; j != NULL; j = j->next) 52 if (j->e == e && j->u == u) { 53 j->t = target_time; 54 return; 55 } 56 57 /* build a job queue element */ 58 if ((j = malloc(sizeof(*j))) == NULL) 59 return; 60 j->next = NULL; 61 j->e = e; 62 j->u = u; 63 j->t = target_time; 64 65 /* add it to the tail */ 66 if (jhead == NULL) 67 jhead = j; 68 else 69 jtail->next = j; 70 jtail = j; 71 } 72 73 int 74 job_runqueue(void) { 75 job *j, *jn; 76 int run = 0; 77 78 for (j = jhead; j; j = jn) { 79 if (okay_to_go(j)) 80 do_command(j->e, j->u); 81 else { 82 char *x = mkprints(j->e->cmd, strlen(j->e->cmd)); 83 char *usernm = env_get("LOGNAME", j->e->envp); 84 85 log_it(usernm, getpid(), "CMD (skipped)", x); 86 free(x); 87 } 88 jn = j->next; 89 free(j); 90 run++; 91 } 92 jhead = jtail = NULL; 93 return (run); 94 } 95 96 97 static int 98 okay_to_go(job *j) 99 { 100 char *within, *t; 101 long delta; 102 103 if (j->e->flags & WHEN_REBOOT) 104 return (1); 105 106 within = env_get("CRON_WITHIN", j->e->envp); 107 if (within == NULL) 108 return (1); 109 110 /* XXX handle 2m, 4h, etc? */ 111 errno = 0; 112 delta = strtol(within, &t, 10); 113 if (errno == ERANGE || *t != '\0' || delta <= 0) 114 return (1); 115 116 return ((j->t + delta) > time(NULL)); 117 } 118