root/src/noit_check_tools.c

Revision 8c720f9a4bd5979520e9c7ec00d9bea8d305391b, 8.7 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 3 years ago)

this is a big patch. adds an optional extended-id. you should upgrade your iep first, then your stratcon, then your noits. This is most certainly a flag-day, *but* the feature is off by default... refs #331

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "noit_defines.h"
34 #include "noit_check_tools.h"
35 #include "utils/noit_str.h"
36
37 #include <assert.h>
38
39 typedef struct {
40   noit_module_t *self;
41   noit_check_t *check;
42   dispatch_func_t dispatch;
43 } recur_closure_t;
44
45 static noit_hash_table interpolation_operators = NOIT_HASH_EMPTY;
46
47 static int
48 interpolate_oper_copy(char *buff, int len, const char *replacement) {
49   strlcpy(buff, replacement, len);
50   return strlen(buff);
51 }
52 static int
53 interpolate_oper_ccns(char *buff, int len, const char *replacement) {
54   char *start;
55   start = strstr(replacement, "::");
56   return interpolate_oper_copy(buff, len, start ? (start + 2) : replacement);
57 }
58
59 int
60 noit_check_interpolate_register_oper_fn(const char *name,
61                                         intperpolate_oper_fn f) {
62   noit_hash_replace(&interpolation_operators,
63                     strdup(name), strlen(name),
64                     (void *)f,
65                     free, NULL);
66   return 0;
67 }
68
69 int
70 noit_check_interpolate(char *buff, int len, const char *fmt,
71                        noit_hash_table *attrs,
72                        noit_hash_table *config) {
73   char *copy = NULL;
74   char closer;
75   const char *fmte, *key;
76   int replaced_something = 1;
77   int iterations = 3;
78
79   while(replaced_something && iterations > 0) {
80     char *cp = buff, * const end = buff + len;
81     iterations--;
82     replaced_something = 0;
83     while(*fmt && cp < end) {
84       switch(*fmt) {
85         case '%':
86           if(fmt[1] == '{' || fmt[1] == '[') {
87             closer = (fmt[1] == '{') ? '}' : ']';
88             fmte = fmt + 2;
89             key = fmte;
90             while(*fmte && *fmte != closer) fmte++;
91             if(*fmte == closer) {
92               /* We have a full key here */
93               const char *replacement, *oper, *nkey;
94               intperpolate_oper_fn oper_sprint;
95
96               /* keys can be of the form: :operator:key */
97               oper = key;
98               if(*oper == ':' &&
99                  (nkey = strnstrn(":", 1, oper + 1, fmte - key - 1)) != NULL) {
100                 void *voper;
101                 oper++;
102                 /* find oper, nkey-oper */
103                 if(!noit_hash_retrieve(&interpolation_operators,
104                                        oper, nkey - oper,
105                                        &voper)) {
106                   /* else oper <- copy */
107                   oper_sprint = interpolate_oper_copy;
108                 }
109                 else
110                   oper_sprint = (intperpolate_oper_fn)voper;
111                 nkey++;
112               }
113               else {
114                 oper_sprint = interpolate_oper_copy;
115                 nkey = key;
116               }
117               if(!noit_hash_retr_str((closer == '}') ?  config : attrs,
118                                      nkey, fmte - nkey, &replacement))
119                 replacement = "";
120               fmt = fmte + 1; /* Format points just after the end of the key */
121               cp += oper_sprint(cp, end-cp, replacement);
122               *(end-1) = '\0'; /* In case the oper_sprint didn't teminate */
123               replaced_something = 1;
124               break;
125             }
126           }
127         default:
128           *cp++ = *fmt++;
129       }
130     }
131     *cp = '\0';
132     if(copy) free(copy);
133     if(replaced_something)
134       copy = strdup(buff);
135     fmt = copy;
136   }
137   return strlen(buff);
138 }
139
140 static int
141 noit_check_recur_handler(eventer_t e, int mask, void *closure,
142                               struct timeval *now) {
143   recur_closure_t *rcl = closure;
144   rcl->check->fire_event = NULL; /* This is us, we get free post-return */
145   noit_check_schedule_next(rcl->self, &e->whence, rcl->check, now,
146                            rcl->dispatch);
147   rcl->dispatch(rcl->self, rcl->check);
148   free(rcl);
149   return 0;
150 }
151
152 int
153 noit_check_schedule_next(noit_module_t *self,
154                          struct timeval *last_check, noit_check_t *check,
155                          struct timeval *now, dispatch_func_t dispatch) {
156   eventer_t newe;
157   struct timeval period, earliest;
158   recur_closure_t *rcl;
159
160   assert(check->fire_event == NULL);
161   if(check->period == 0) return 0;
162   if(NOIT_CHECK_DISABLED(check) || NOIT_CHECK_KILLED(check)) return 0;
163
164   /* If we have an event, we know when we intended it to fire.  This means
165    * we should schedule that point + period.
166    */
167   if(now)
168     memcpy(&earliest, now, sizeof(earliest));
169   else
170     gettimeofday(&earliest, NULL);
171   period.tv_sec = check->period / 1000;
172   period.tv_usec = (check->period % 1000) * 1000;
173
174   newe = eventer_alloc();
175   memcpy(&newe->whence, last_check, sizeof(*last_check));
176   add_timeval(newe->whence, period, &newe->whence);
177   if(compare_timeval(newe->whence, earliest) < 0)
178     memcpy(&newe->whence, &earliest, sizeof(earliest));
179   newe->mask = EVENTER_TIMER;
180   newe->callback = noit_check_recur_handler;
181   rcl = calloc(1, sizeof(*rcl));
182   rcl->self = self;
183   rcl->check = check;
184   rcl->dispatch = dispatch;
185   newe->closure = rcl;
186
187   eventer_add(newe);
188   check->fire_event = newe;
189   return 0;
190 }
191
192 void
193 noit_check_run_full_asynch(noit_check_t *check, eventer_func_t callback) {
194   struct timeval __now, p_int;
195   eventer_t e;
196   e = eventer_alloc();
197   e->fd = -1;
198   e->mask = EVENTER_ASYNCH;
199   gettimeofday(&__now, NULL);
200   memcpy(&e->whence, &__now, sizeof(__now));
201   p_int.tv_sec = check->timeout / 1000;
202   p_int.tv_usec = (check->timeout % 1000) * 1000;
203   add_timeval(e->whence, p_int, &e->whence);
204   e->callback = callback;
205   e->closure =  check->closure;
206   eventer_add(e);
207 }
208
209 void
210 noit_check_make_attrs(noit_check_t *check, noit_hash_table *attrs) {
211 #define CA_STORE(a,b) noit_hash_store(attrs, a, strlen(a), b)
212   CA_STORE("target", check->target);
213   CA_STORE("name", check->name);
214   CA_STORE("module", check->module);
215 }
216 void
217 noit_check_release_attrs(noit_hash_table *attrs) {
218   noit_hash_destroy(attrs, NULL, NULL);
219 }
220
221 void
222 noit_check_extended_id_split(const char *in, int len,
223                              char *target, int target_len,
224                              char *module, int module_len,
225                              char *name, int name_len,
226                              char *uuid, int uuid_len) {
227   if(target) *target = '\0';
228   if(module) *module = '\0';
229   if(name) *name = '\0';
230   if(uuid) *uuid = '\0';
231   if(len >= UUID_STR_LEN) {
232     memcpy(uuid, in + len - UUID_STR_LEN, UUID_STR_LEN);
233     uuid[UUID_STR_LEN] = '\0';
234   }
235   if(len > UUID_STR_LEN) {
236     const char *tcp = in;
237     const char *mcp, *ncp, *ucp;
238     /* find the end of the target */
239     mcp = strchr(tcp,'`');
240     if(!mcp) return;
241     /* copy in the target */
242     if(target_len > mcp-tcp) {
243       memcpy(target,tcp,mcp-tcp);
244       target[mcp-tcp] = '\0';
245     }
246     mcp++;
247     ncp = strchr(mcp,'`');
248     if(!ncp) return;
249     /* copy in the module */
250     if(module_len > ncp-mcp) {
251       memcpy(module,mcp,ncp-mcp);
252       module[ncp-mcp] = '\0';
253     }
254     ncp++;
255     /* copy in the name */
256     ucp = in + len - UUID_STR_LEN - 1;
257     if(ncp < ucp) {
258       if(name_len > ucp-ncp) {
259         memcpy(name, ncp, ucp-ncp);
260         name[ucp-ncp] = '\0';
261       }
262     }
263   }
264 }
265
266 void
267 noit_check_tools_init() {
268   noit_check_interpolate_register_oper_fn("copy", interpolate_oper_copy);
269   noit_check_interpolate_register_oper_fn("ccns", interpolate_oper_ccns);
270   eventer_name_callback("noit_check_recur_handler", noit_check_recur_handler);
271 }
272
Note: See TracBrowser for help on using the browser.