OpenDNSSEC-signer  1.4.5
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 "shared/allocator.h"
35 #include "shared/duration.h"
36 #include "shared/file.h"
37 #include "shared/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  allocator_type* allocator = NULL;
51  task_type* task = NULL;
52 
53  if (!zone) {
54  return NULL;
55  }
56  allocator = allocator_create(malloc, free);
57  if (!allocator) {
58  ods_log_error("[%s] unable to create task: allocator_create() failed",
59  task_str);
60  return NULL;
61  }
62  task = (task_type*) allocator_alloc(allocator, sizeof(task_type));
63  if (!task) {
64  ods_log_error("[%s] unable to create task: allocator_alloc() failed",
65  task_str);
66  allocator_cleanup(allocator);
67  return NULL;
68  }
69  task->allocator = allocator;
70  task->what = what;
71  task->interrupt = TASK_NONE;
72  task->halted = TASK_NONE;
73  task->when = when;
74  task->halted_when = 0;
75  task->backoff = 0;
76  task->flush = 0;
77  task->zone = zone;
78  return task;
79 }
80 
81 
86 void
87 task_backup(FILE* fd, task_type* task)
88 {
89  if (!fd || !task) {
90  return;
91  }
92  ods_log_assert(fd);
93  ods_log_assert(task);
94 
95  fprintf(fd, ";;Task: when %u what %i interrupt %i halted %i backoff %i "
96  "flush %i\n",
97  (unsigned) task->when,
98  (int) task->what,
99  (int) task->interrupt,
100  (int) task->halted,
101  (unsigned) task->backoff,
102  task->flush);
103  return;
104 }
105 
106 
111 int
112 task_compare(const void* a, const void* b)
113 {
114  task_type* x = (task_type*)a;
115  task_type* y = (task_type*)b;
116  zone_type* zx = NULL;
117  zone_type* zy = NULL;
118 
119  ods_log_assert(x);
120  ods_log_assert(y);
121  zx = (zone_type*) x->zone;
122  zy = (zone_type*) y->zone;
123  if (!ldns_dname_compare((const void*) zx->apex,
124  (const void*) zy->apex)) {
125  /* if dname is the same, consider the same task */
126  return 0;
127  }
128  /* order task on time, what to do, dname */
129  if (x->when != y->when) {
130  return (int) x->when - y->when;
131  }
132  if (x->what != y->what) {
133  return (int) x->what - y->what;
134  }
135  /* this is unfair, it prioritizes zones that are first in canonical line */
136  return ldns_dname_compare((const void*) zx->apex,
137  (const void*) zy->apex);
138 }
139 
140 
145 const char*
147 {
148  switch (what) {
149  case TASK_NONE:
150  return "[ignore]";
151  break;
152  case TASK_SIGNCONF:
153  return "[configure]";
154  break;
155  case TASK_READ:
156  return "[read]";
157  break;
158  case TASK_SIGN:
159  return "[sign]";
160  break;
161  case TASK_WRITE:
162  return "[write]";
163  break;
164  default:
165  break;
166  }
167  return "[???]";
168 }
169 
170 
175 const char*
177 {
178  zone_type* zone = NULL;
179  if (task) {
180  zone = (zone_type*) task->zone;
181  }
182  if (zone && zone->name) {
183  return zone->name;
184  }
185  return "(null)";
186 }
187 
188 
193 char*
194 task2str(task_type* task, char* buftask)
195 {
196  char* strtime = NULL;
197  char* strtask = NULL;
198 
199  if (task) {
200  strtime = ctime(&task->when);
201  if (strtime) {
202  strtime[strlen(strtime)-1] = '\0';
203  }
204  if (buftask) {
205  (void)snprintf(buftask, ODS_SE_MAXLINE, "%s %s I will %s zone %s"
206  "\n", task->flush?"Flush":"On", strtime?strtime:"(null)",
207  task_what2str(task->what), task_who2str(task));
208  return buftask;
209  } else {
210  strtask = (char*) calloc(ODS_SE_MAXLINE, sizeof(char));
211  if (strtask) {
212  snprintf(strtask, ODS_SE_MAXLINE, "%s %s I will %s zone %s\n",
213  task->flush?"Flush":"On", strtime?strtime:"(null)",
214  task_what2str(task->what), task_who2str(task));
215  return strtask;
216  } else {
217  ods_log_error("[%s] unable to convert task to string: malloc "
218  "error", task_str);
219  }
220  }
221  }
222  return NULL;
223 }
224 
225 
230 void
231 task_print(FILE* out, task_type* task)
232 {
233  char* strtime = NULL;
234 
235  if (out && task) {
236  strtime = ctime(&task->when);
237  if (strtime) {
238  strtime[strlen(strtime)-1] = '\0';
239  }
240  fprintf(out, "%s %s I will %s zone %s\n",
241  task->flush?"Flush":"On", strtime?strtime:"(null)",
242  task_what2str(task->what), task_who2str(task));
243  }
244  return;
245 }
246 
247 
252 void
254 {
255  char* strtime = NULL;
256 
257  if (task) {
258  strtime = ctime(&task->when);
259  if (strtime) {
260  strtime[strlen(strtime)-1] = '\0';
261  }
262  ods_log_debug("[%s] %s %s I will %s zone %s", task_str,
263  task->flush?"Flush":"On", strtime?strtime:"(null)",
264  task_what2str(task->what), task_who2str(task));
265  }
266  return;
267 }
268 
269 
274 void
276 {
277  allocator_type* allocator;
278  if (!task) {
279  return;
280  }
281  allocator = task->allocator;
282  allocator_deallocate(allocator, (void*) task);
283  allocator_cleanup(allocator);
284  return;
285 }
Definition: task.h:41
void ods_log_debug(const char *format,...)
Definition: log.c:270
time_t when
Definition: task.h:59
task_id interrupt
Definition: task.h:57
void * allocator_alloc(allocator_type *allocator, size_t size)
Definition: allocator.c:66
void task_log(task_type *task)
Definition: task.c:253
int flush
Definition: task.h:62
const char * task_who2str(task_type *task)
Definition: task.c:176
time_t backoff
Definition: task.h:61
void ods_log_error(const char *format,...)
Definition: log.c:334
Definition: task.h:45
void * zone
Definition: task.h:63
enum task_id_enum task_id
Definition: task.h:48
allocator_type * allocator_create(void *(*allocator)(size_t size), void(*deallocator)(void *))
Definition: allocator.c:47
allocator_type * allocator
Definition: task.h:55
Definition: task.h:43
time_t halted_when
Definition: task.h:60
task_id halted
Definition: task.h:58
void task_cleanup(task_type *task)
Definition: task.c:275
task_id what
Definition: task.h:56
void allocator_cleanup(allocator_type *allocator)
Definition: allocator.c:151
const char * name
Definition: zone.h:76
void allocator_deallocate(allocator_type *allocator, void *data)
Definition: allocator.c:135
int task_compare(const void *a, const void *b)
Definition: task.c:112
char * task2str(task_type *task, char *buftask)
Definition: task.c:194
void task_print(FILE *out, task_type *task)
Definition: task.c:231
#define ods_log_assert(x)
Definition: log.h:154
ldns_rdf * apex
Definition: zone.h:68
const char * task_what2str(task_id what)
Definition: task.c:146
task_type * task_create(task_id what, time_t when, void *zone)
Definition: task.c:48
void task_backup(FILE *fd, task_type *task)
Definition: task.c:87