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