OpenDNSSEC-signer  1.3.16
task.c
Go to the documentation of this file.
1 /*
2  * $Id: task.c 4613 2011-03-22 07:54:50Z rb $
3  *
4  * Copyright (c) 2009 NLNet Labs. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
34 #include "config.h"
35 #include "scheduler/task.h"
36 #include "shared/allocator.h"
37 #include "shared/duration.h"
38 #include "shared/file.h"
39 #include "shared/log.h"
40 #include "signer/backup.h"
41 
42 static const char* task_str = "task";
43 
44 
49 task_type*
50 task_create(task_id what, time_t when, const char* who, void* zone)
51 {
52  allocator_type* allocator = NULL;
53  task_type* task = NULL;
54 
55  if (!who || !zone) {
56  ods_log_error("[%s] cannot create: missing zone info", task_str);
57  return NULL;
58  }
59  ods_log_assert(who);
60  ods_log_assert(zone);
61 
62  allocator = allocator_create(malloc, free);
63  if (!allocator) {
64  ods_log_error("[%s] cannot create: create allocator failed", task_str);
65  return NULL;
66  }
67  ods_log_assert(allocator);
68 
69  task = (task_type*) allocator_alloc(allocator, sizeof(task_type));
70  if (!task) {
71  ods_log_error("[%s] cannot create: allocator failed", task_str);
72  allocator_cleanup(allocator);
73  return NULL;
74  }
75  task->allocator = allocator;
76  task->what = what;
77  task->interrupt = TASK_NONE;
78  task->halted = TASK_NONE;
79  task->when = when;
80  task->backoff = 0;
81  task->who = allocator_strdup(allocator, who);
82  task->dname = ldns_dname_new_frm_str(who);
83  task->flush = 0;
84  task->zone = zone;
85  return task;
86 }
87 
88 
93 task_type*
94 task_recover_from_backup(const char* filename, void* zone)
95 {
96  task_type* task = NULL;
97  FILE* fd = NULL;
98  const char* who = NULL;
99  int what = 0;
100  time_t when = 0;
101  int flush = 0;
102  time_t backoff = 0;
103 
104  ods_log_assert(zone);
105  fd = ods_fopen(filename, NULL, "r");
106  if (fd) {
107  if (!backup_read_check_str(fd, ODS_SE_FILE_MAGIC) ||
108  !backup_read_check_str(fd, ";who:") ||
109  !backup_read_str(fd, &who) ||
110  !backup_read_check_str(fd, ";what:") ||
111  !backup_read_int(fd, &what) ||
112  !backup_read_check_str(fd, ";when:") ||
113  !backup_read_time_t(fd, &when) ||
114  !backup_read_check_str(fd, ";flush:") ||
115  !backup_read_int(fd, &flush) ||
116  !backup_read_check_str(fd, ";backoff:") ||
117  !backup_read_time_t(fd, &backoff) ||
118  !backup_read_check_str(fd, ODS_SE_FILE_MAGIC))
119  {
120  ods_log_error("[%s] unable to recover task from file %s: file corrupted",
121  task_str, filename?filename:"(null)");
122  task = NULL;
123  } else {
124  task = task_create((task_id) what, when, who, (void*) zone);
125  task->flush = flush;
126  task->backoff = backoff;
127  }
128  free((void*)who);
129  ods_fclose(fd);
130  return task;
131  }
132 
133  ods_log_debug("[%s] unable to recover task from file %s: no such file or directory",
134  task_str, filename?filename:"(null)");
135  return NULL;
136 }
137 
138 
143 void
144 task_backup(FILE* fd, task_type* task)
145 {
146  if (!fd || !task) {
147  return;
148  }
149  ods_log_assert(fd);
150  ods_log_assert(task);
151 
152  fprintf(fd, ";;Task: when %u what %i interrupt %i halted %i backoff %i "
153  "flush %i\n",
154  (unsigned) task->when,
155  (int) task->what,
156  (int) task->interrupt,
157  (int) task->halted,
158  (unsigned) task->backoff,
159  task->flush);
160  return;
161 }
162 
163 
168 void
170 {
171  allocator_type* allocator;
172 
173  if (!task) {
174  return;
175  }
176  allocator = task->allocator;
177  if (task->dname) {
178  ldns_rdf_deep_free(task->dname);
179  task->dname = NULL;
180  }
181  allocator_deallocate(allocator, (void*) task->who);
182  allocator_deallocate(allocator, (void*) task);
183  allocator_cleanup(allocator);
184  return;
185 }
186 
187 
192 int
193 task_compare(const void* a, const void* b)
194 {
195  task_type* x = (task_type*)a;
196  task_type* y = (task_type*)b;
197 
198  ods_log_assert(x);
199  ods_log_assert(y);
200 
201  if (!ldns_dname_compare((const void*) x->dname, (const void*) y->dname)) {
202  /* if dname is the same, consider the same task */
203  return 0;
204  }
205 
206  /* order task on time, what to do, dname */
207  if (x->when != y->when) {
208  return (int) x->when - y->when;
209  }
210  if (x->what != y->what) {
211  return (int) x->what - y->what;
212  }
213  return ldns_dname_compare((const void*) x->dname, (const void*) y->dname);
214 }
215 
216 
221 const char*
222 task_what2str(int what)
223 {
224  switch (what) {
225  case TASK_NONE:
226  return "[do nothing with]";
227  break;
228  case TASK_SIGNCONF:
229  return "[load signconf for]";
230  break;
231  case TASK_READ:
232  return "[read]";
233  break;
234  case TASK_NSECIFY:
235  return "[nsecify]";
236  break;
237  case TASK_SIGN:
238  return "[sign]";
239  break;
240  case TASK_AUDIT:
241  return "[audit]";
242  break;
243  case TASK_WRITE:
244  return "[write]";
245  break;
246  default:
247  return "[???]";
248  break;
249  }
250  return "[???]";
251 }
252 
253 
258 const char*
259 task_who2str(const char* who)
260 {
261  if (who) {
262  return who;
263  }
264  return "(null)";
265 }
266 
267 
272 char*
273 task2str(task_type* task, char* buftask)
274 {
275  char* strtime = NULL;
276  char* strtask = NULL;
277 
278  if (task) {
279  strtime = ctime(&task->when);
280  if (strtime) {
281  strtime[strlen(strtime)-1] = '\0';
282  }
283  if (buftask) {
284  (void)snprintf(buftask, ODS_SE_MAXLINE, "%s %s I will %s zone %s"
285  "\n", task->flush?"Flush":"On", strtime?strtime:"(null)",
286  task_what2str(task->what), task_who2str(task->who));
287  return buftask;
288  } else {
289  strtask = (char*) calloc(ODS_SE_MAXLINE, sizeof(char));
290  snprintf(strtask, ODS_SE_MAXLINE, "%s %s I will %s zone %s\n",
291  task->flush?"Flush":"On", strtime?strtime:"(null)",
292  task_what2str(task->what), task_who2str(task->who));
293  return strtask;
294  }
295  }
296  return NULL;
297 }
298 
299 
304 void
305 task_print(FILE* out, task_type* task)
306 {
307  char* strtime = NULL;
308 
309  if (out && task) {
310  strtime = ctime(&task->when);
311  if (strtime) {
312  strtime[strlen(strtime)-1] = '\0';
313  }
314  fprintf(out, "%s %s I will %s zone %s\n",
315  task->flush?"Flush":"On", strtime?strtime:"(null)",
316  task_what2str(task->what), task_who2str(task->who));
317  }
318  return;
319 }
320 
321 
326 void
328 {
329  char* strtime = NULL;
330 
331  if (task) {
332  strtime = ctime(&task->when);
333  if (strtime) {
334  strtime[strlen(strtime)-1] = '\0';
335  }
336  ods_log_debug("[%s] %s %s I will %s zone %s", task_str,
337  task->flush?"Flush":"On", strtime?strtime:"(null)",
338  task_what2str(task->what), task_who2str(task->who));
339  }
340  return;
341 }
int backup_read_str(FILE *in, const char **str)
Definition: backup.c:94
Definition: task.h:43
void ods_log_debug(const char *format,...)
Definition: log.c:285
time_t when
Definition: task.h:62
task_id interrupt
Definition: task.h:60
void * allocator_alloc(allocator_type *allocator, size_t size)
Definition: allocator.c:67
void task_log(task_type *task)
Definition: task.c:327
int flush
Definition: task.h:64
int backup_read_time_t(FILE *in, time_t *v)
Definition: backup.c:111
time_t backoff
Definition: task.h:63
void ods_log_error(const char *format,...)
Definition: log.c:349
Definition: task.h:47
void * zone
Definition: task.h:67
int backup_read_int(FILE *in, int *v)
Definition: backup.c:162
enum task_id_enum task_id
Definition: task.h:51
FILE * ods_fopen(const char *file, const char *dir, const char *mode)
Definition: file.c:188
task_type * task_create(task_id what, time_t when, const char *who, void *zone)
Definition: task.c:50
allocator_type * allocator_create(void *(*allocator)(size_t size), void(*deallocator)(void *))
Definition: allocator.c:48
ldns_rdf * dname
Definition: task.h:66
allocator_type * allocator
Definition: task.h:58
Definition: task.h:45
task_type * task_recover_from_backup(const char *filename, void *zone)
Definition: task.c:94
task_id halted
Definition: task.h:61
char * allocator_strdup(allocator_type *allocator, const char *string)
Definition: allocator.c:122
void task_cleanup(task_type *task)
Definition: task.c:169
const char * who
Definition: task.h:65
const char * task_what2str(int what)
Definition: task.c:222
const char * task_who2str(const char *who)
Definition: task.c:259
task_id what
Definition: task.h:59
void ods_fclose(FILE *fd)
Definition: file.c:245
void allocator_cleanup(allocator_type *allocator)
Definition: allocator.c:153
int backup_read_check_str(FILE *in, const char *str)
Definition: backup.c:74
void allocator_deallocate(allocator_type *allocator, void *data)
Definition: allocator.c:136
int task_compare(const void *a, const void *b)
Definition: task.c:193
char * task2str(task_type *task, char *buftask)
Definition: task.c:273
void task_print(FILE *out, task_type *task)
Definition: task.c:305
#define ods_log_assert(x)
Definition: log.h:141
void task_backup(FILE *fd, task_type *task)
Definition: task.c:144