root/src/noit_check_tools.c

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

various changes to avoid dereferencing type-punned pointers and breaking strict-aliasing rules, refs #34

  • 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                 void *voper;
68                 oper++;
69                 /* find oper, nkey-oper */
70                 if(!noit_hash_retrieve(&interpolation_operators,
71                                        oper, nkey - oper,
72                                        &voper)) {
73                   /* else oper <- copy */
74                   oper_sprint = interpolate_oper_copy;
75                 }
76                 else
77                   oper_sprint = voper;
78                 nkey++;
79               }
80               else {
81                 oper_sprint = interpolate_oper_copy;
82                 nkey = key;
83               }
84               if(!noit_hash_retr_str((closer == '}') ?  config : attrs,
85                                      nkey, fmte - nkey, &replacement))
86                 replacement = "";
87               fmt = fmte + 1; /* Format points just after the end of the key */
88               cp += oper_sprint(cp, end-cp, replacement);
89               *(end-1) = '\0'; /* In case the oper_sprint didn't teminate */
90               replaced_something = 1;
91               break;
92             }
93           }
94         default:
95           *cp++ = *fmt++;
96       }
97     }
98     *cp = '\0';
99     if(copy) free(copy);
100     if(replaced_something)
101       copy = strdup(buff);
102     fmt = copy;
103   }
104   return strlen(buff);
105 }
106
107 static int
108 noit_check_recur_handler(eventer_t e, int mask, void *closure,
109                               struct timeval *now) {
110   recur_closure_t *rcl = closure;
111   rcl->check->fire_event = NULL; /* This is us, we get free post-return */
112   noit_check_schedule_next(rcl->self, &e->whence, rcl->check, now,
113                            rcl->dispatch);
114   rcl->dispatch(rcl->self, rcl->check);
115   free(rcl);
116   return 0;
117 }
118
119 int
120 noit_check_schedule_next(noit_module_t *self,
121                          struct timeval *last_check, noit_check_t *check,
122                          struct timeval *now, dispatch_func_t dispatch) {
123   eventer_t newe;
124   struct timeval period, earliest;
125   recur_closure_t *rcl;
126
127   assert(check->fire_event == NULL);
128   if(check->period == 0) return 0;
129   if(NOIT_CHECK_DISABLED(check) || NOIT_CHECK_KILLED(check)) return 0;
130
131   /* If we have an event, we know when we intended it to fire.  This means
132    * we should schedule that point + period.
133    */
134   if(now)
135     memcpy(&earliest, now, sizeof(earliest));
136   else
137     gettimeofday(&earliest, NULL);
138   period.tv_sec = check->period / 1000;
139   period.tv_usec = (check->period % 1000) * 1000;
140
141   newe = eventer_alloc();
142   memcpy(&newe->whence, last_check, sizeof(*last_check));
143   add_timeval(newe->whence, period, &newe->whence);
144   if(compare_timeval(newe->whence, earliest) < 0)
145     memcpy(&newe->whence, &earliest, sizeof(earliest));
146   newe->mask = EVENTER_TIMER;
147   newe->callback = noit_check_recur_handler;
148   rcl = calloc(1, sizeof(*rcl));
149   rcl->self = self;
150   rcl->check = check;
151   rcl->dispatch = dispatch;
152   newe->closure = rcl;
153
154   eventer_add(newe);
155   check->fire_event = newe;
156   return 0;
157 }
158
159 void
160 noit_check_run_full_asynch(noit_check_t *check, eventer_func_t callback) {
161   struct timeval __now, p_int;
162   eventer_t e;
163   e = eventer_alloc();
164   e->fd = -1;
165   e->mask = EVENTER_ASYNCH;
166   gettimeofday(&__now, NULL);
167   memcpy(&e->whence, &__now, sizeof(__now));
168   p_int.tv_sec = check->timeout / 1000;
169   p_int.tv_usec = (check->timeout % 1000) * 1000;
170   add_timeval(e->whence, p_int, &e->whence);
171   e->callback = callback;
172   e->closure =  check->closure;
173   eventer_add(e);
174 }
175
176 void
177 noit_check_make_attrs(noit_check_t *check, noit_hash_table *attrs) {
178 #define CA_STORE(a,b) noit_hash_store(attrs, a, strlen(a), b)
179   CA_STORE("target", check->target);
180   CA_STORE("name", check->name);
181   CA_STORE("module", check->module);
182 }
183 void
184 noit_check_release_attrs(noit_hash_table *attrs) {
185   noit_hash_destroy(attrs, NULL, NULL);
186 }
Note: See TracBrowser for help on using the browser.