root/src/noit_check_tools.c

Revision 5a9b91bc0a14543e0995eba973fbeacc05aaee8b, 5.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

svn merge -r 327:330 https://labs.omniti.com/reconnoiter/branches/dev/udns .

closes #38

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7 #include "noit_check_tools.h"
8 #include "utils/noit_str.h"
9
10 #include <assert.h>
11
12 typedef struct {
13   noit_module_t *self;
14   noit_check_t *check;
15   dispatch_func_t dispatch;
16 } recur_closure_t;
17
18 static noit_hash_table interpolation_operators = NOIT_HASH_EMPTY;
19
20 static int
21 interpolate_oper_copy(char *buff, int len, const char *replacement) {
22   strlcpy(buff, replacement, len);
23   return strlen(buff);
24 }
25
26 API_EXPORT(int)
27 noit_check_interpolate_register_oper_fn(const char *name,
28                                         intperpolate_oper_fn f) {
29   noit_hash_replace(&interpolation_operators,
30                     strdup(name), strlen(name),
31                     f,
32                     free, NULL);
33   return 0;
34 }
35
36 API_EXPORT(int)
37 noit_check_interpolate(char *buff, int len, const char *fmt,
38                        noit_hash_table *attrs,
39                        noit_hash_table *config) {
40   char *copy = NULL;
41   char closer;
42   const char *fmte, *key;
43   int replaced_something = 1;
44   int iterations = 3;
45
46   while(replaced_something && iterations > 0) {
47     char *cp = buff, * const end = buff + len;
48     iterations--;
49     replaced_something = 0;
50     while(*fmt && cp < end) {
51       switch(*fmt) {
52         case '%':
53           if(fmt[1] == '{' || fmt[1] == '[') {
54             closer = (fmt[1] == '{') ? '}' : ']';
55             fmte = fmt + 2;
56             key = fmte;
57             while(*fmte && *fmte != closer) fmte++;
58             if(*fmte == closer) {
59               /* We have a full key here */
60               const char *replacement, *oper, *nkey;
61               intperpolate_oper_fn oper_sprint;
62
63               /* keys can be of the form: :operator:key */
64               oper = key;
65               if(*oper == ':' &&
66                  (nkey = strnstrn(":", 1, oper + 1, fmte - key - 1)) != NULL) {
67                 oper++;
68                 /* find oper, nkey-oper */
69                 if(!noit_hash_retrieve(&interpolation_operators,
70                                        oper, nkey - oper,
71                                        (void **)&oper_sprint)) {
72                   /* else oper <- copy */
73                   oper_sprint = interpolate_oper_copy;
74                 }
75                 nkey++;
76               }
77               else {
78                 oper_sprint = interpolate_oper_copy;
79                 nkey = key;
80               }
81               if(!noit_hash_retrieve((closer == '}') ?  config : attrs,
82                                      nkey, fmte - nkey, (void **)&replacement))
83                 replacement = "";
84               fmt = fmte + 1; /* Format points just after the end of the key */
85               cp += oper_sprint(cp, end-cp, replacement);
86               *(end-1) = '\0'; /* In case the oper_sprint didn't teminate */
87               replaced_something = 1;
88               break;
89             }
90           }
91         default:
92           *cp++ = *fmt++;
93       }
94     }
95     *cp = '\0';
96     if(copy) free(copy);
97     if(replaced_something)
98       copy = strdup(buff);
99     fmt = copy;
100   }
101   return strlen(buff);
102 }
103
104 static int
105 noit_check_recur_handler(eventer_t e, int mask, void *closure,
106                               struct timeval *now) {
107   recur_closure_t *rcl = closure;
108   rcl->check->fire_event = NULL; /* This is us, we get free post-return */
109   noit_check_schedule_next(rcl->self, &e->whence, rcl->check, now,
110                            rcl->dispatch);
111   rcl->dispatch(rcl->self, rcl->check);
112   free(rcl);
113   return 0;
114 }
115
116 int
117 noit_check_schedule_next(noit_module_t *self,
118                          struct timeval *last_check, noit_check_t *check,
119                          struct timeval *now, dispatch_func_t dispatch) {
120   eventer_t newe;
121   struct timeval period, earliest;
122   recur_closure_t *rcl;
123
124   assert(check->fire_event == NULL);
125   if(check->period == 0) return 0;
126   if(NOIT_CHECK_DISABLED(check) || NOIT_CHECK_KILLED(check)) return 0;
127
128   /* If we have an event, we know when we intended it to fire.  This means
129    * we should schedule that point + period.
130    */
131   if(now)
132     memcpy(&earliest, now, sizeof(earliest));
133   else
134     gettimeofday(&earliest, NULL);
135   period.tv_sec = check->period / 1000;
136   period.tv_usec = (check->period % 1000) * 1000;
137
138   newe = eventer_alloc();
139   memcpy(&newe->whence, last_check, sizeof(*last_check));
140   add_timeval(newe->whence, period, &newe->whence);
141   if(compare_timeval(newe->whence, earliest) < 0)
142     memcpy(&newe->whence, &earliest, sizeof(earliest));
143   newe->mask = EVENTER_TIMER;
144   newe->callback = noit_check_recur_handler;
145   rcl = calloc(1, sizeof(*rcl));
146   rcl->self = self;
147   rcl->check = check;
148   rcl->dispatch = dispatch;
149   newe->closure = rcl;
150
151   eventer_add(newe);
152   check->fire_event = newe;
153   return 0;
154 }
155
156 void
157 noit_check_run_full_asynch(noit_check_t *check, eventer_func_t callback) {
158   struct timeval __now, p_int;
159   eventer_t e;
160   e = eventer_alloc();
161   e->fd = -1;
162   e->mask = EVENTER_ASYNCH;
163   gettimeofday(&__now, NULL);
164   memcpy(&e->whence, &__now, sizeof(__now));
165   p_int.tv_sec = check->timeout / 1000;
166   p_int.tv_usec = (check->timeout % 1000) * 1000;
167   add_timeval(e->whence, p_int, &e->whence);
168   e->callback = callback;
169   e->closure =  check->closure;
170   eventer_add(e);
171 }
172
173 void
174 noit_check_make_attrs(noit_check_t *check, noit_hash_table *attrs) {
175 #define CA_STORE(a,b) noit_hash_store(attrs, a, strlen(a), b)
176   CA_STORE("target", check->target);
177   CA_STORE("name", check->name);
178   CA_STORE("module", check->module);
179 }
180 void
181 noit_check_release_attrs(noit_hash_table *attrs) {
182   noit_hash_destroy(attrs, NULL, NULL);
183 }
Note: See TracBrowser for help on using the browser.