OpenDNSSEC-signer  2.0.4
task.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 NLNet Labs. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
32 #include "config.h"
33 #include "scheduler/task.h"
34 #include "status.h"
35 #include "duration.h"
36 #include "file.h"
37 #include "log.h"
38 #include "signer/zone.h"
39 
40 static const char* task_str = "task";
41 
42 
47 task_type*
48 task_create(task_id what, time_t when, void* zone)
49 {
50  task_type* task = NULL;
51 
52  if (!zone) {
53  return NULL;
54  }
55  CHECKALLOC(task = (task_type*) malloc(sizeof(task_type)));
56  task->what = what;
57  task->interrupt = TASK_NONE;
58  task->halted = TASK_NONE;
59  task->when = when;
60  task->halted_when = 0;
61  task->backoff = 0;
62  task->flush = 0;
63  task->zone = zone;
64  return task;
65 }
66 
67 
72 int
73 task_compare(const void* a, const void* b)
74 {
75  task_type* x = (task_type*)a;
76  task_type* y = (task_type*)b;
77  zone_type* zx = NULL;
78  zone_type* zy = NULL;
79 
80  ods_log_assert(x);
81  ods_log_assert(y);
82  zx = (zone_type*) x->zone;
83  zy = (zone_type*) y->zone;
84  if (!ldns_dname_compare((const void*) zx->apex,
85  (const void*) zy->apex)) {
86  /* if dname is the same, consider the same task */
87  return 0;
88  }
89  /* order task on time, what to do, dname */
90  if (x->when != y->when) {
91  return (int) x->when - y->when;
92  }
93  if (x->what != y->what) {
94  return (int) x->what - y->what;
95  }
96  /* this is unfair, it prioritizes zones that are first in canonical line */
97  return ldns_dname_compare((const void*) zx->apex,
98  (const void*) zy->apex);
99 }
100 
101 
106 const char*
108 {
109  switch (what) {
110  case TASK_NONE:
111  return "[ignore]";
112  break;
113  case TASK_SIGNCONF:
114  return "[configure]";
115  break;
116  case TASK_READ:
117  return "[read]";
118  break;
119  case TASK_SIGN:
120  return "[sign]";
121  break;
122  case TASK_WRITE:
123  return "[write]";
124  break;
125  default:
126  break;
127  }
128  return "[???]";
129 }
130 
131 
136 const char*
138 {
139  zone_type* zone = NULL;
140  if (task) {
141  zone = (zone_type*) task->zone;
142  }
143  if (zone && zone->name) {
144  return zone->name;
145  }
146  return "(null)";
147 }
148 
149 
154 char*
155 task2str(task_type* task, char* buftask)
156 {
157  char* strtime = NULL;
158  char* strtask = NULL;
159 
160  if (task) {
161  strtime = ctime(&task->when);
162  if (strtime) {
163  strtime[strlen(strtime)-1] = '\0';
164  }
165  if (buftask) {
166  (void)snprintf(buftask, ODS_SE_MAXLINE, "%s %s I will %s zone %s"
167  "\n", task->flush?"Flush":"On", strtime?strtime:"(null)",
168  task_what2str(task->what), task_who2str(task));
169  return buftask;
170  } else {
171  strtask = (char*) calloc(ODS_SE_MAXLINE, sizeof(char));
172  if (strtask) {
173  snprintf(strtask, ODS_SE_MAXLINE, "%s %s I will %s zone %s\n",
174  task->flush?"Flush":"On", strtime?strtime:"(null)",
175  task_what2str(task->what), task_who2str(task));
176  return strtask;
177  } else {
178  ods_log_error("[%s] unable to convert task to string: malloc "
179  "error", task_str);
180  }
181  }
182  }
183  return NULL;
184 }
185 
186 
191 void
193 {
194  char* strtime = NULL;
195 
196  if (task) {
197  strtime = ctime(&task->when);
198  if (strtime) {
199  strtime[strlen(strtime)-1] = '\0';
200  }
201  ods_log_debug("[%s] %s %s I will %s zone %s", task_str,
202  task->flush?"Flush":"On", strtime?strtime:"(null)",
203  task_what2str(task->what), task_who2str(task));
204  }
205 }
206 
207 
212 void
214 {
215  if (!task) {
216  return;
217  }
218  free(task);
219 }
Definition: task.h:39
time_t when
Definition: task.h:60
task_id interrupt
Definition: task.h:58
void task_log(task_type *task)
Definition: task.c:192
int flush
Definition: task.h:63
const char * task_who2str(task_type *task)
Definition: task.c:137
time_t backoff
Definition: task.h:62
Definition: task.h:43
enum task_id_enum task_id
Definition: task.h:46
zone_type * zone
Definition: task.h:64
Definition: task.h:41
time_t halted_when
Definition: task.h:61
task_id halted
Definition: task.h:59
void task_cleanup(task_type *task)
Definition: task.c:213
task_id what
Definition: task.h:57
const char * name
Definition: zone.h:67
int task_compare(const void *a, const void *b)
Definition: task.c:73
char * task2str(task_type *task, char *buftask)
Definition: task.c:155
ldns_rdf * apex
Definition: zone.h:59
const char * task_what2str(task_id what)
Definition: task.c:107
task_type * task_create(task_id what, time_t when, void *zone)
Definition: task.c:48