root/src/noit_check_tools.c

Revision 78bf9035137ab04dfa29092585c07d27820a584f, 4.6 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 3 years ago)

refs #344. This is a race where we schedule wrong before we notice we've looked up

  • 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 "noit_check_tools_shared.h"
36 #include "utils/noit_str.h"
37
38 #include <assert.h>
39
40 typedef struct {
41   noit_module_t *self;
42   noit_check_t *check;
43   dispatch_func_t dispatch;
44 } recur_closure_t;
45
46 static int
47 noit_check_recur_handler(eventer_t e, int mask, void *closure,
48                               struct timeval *now) {
49   recur_closure_t *rcl = closure;
50   rcl->check->fire_event = NULL; /* This is us, we get free post-return */
51   noit_check_resolve(rcl->check);
52   noit_check_schedule_next(rcl->self, &e->whence, rcl->check, now,
53                            rcl->dispatch);
54   if(NOIT_CHECK_RESOLVED(rcl->check))
55     rcl->dispatch(rcl->self, rcl->check);
56   else
57     noitL(noit_debug, "skipping %s`%s`%s, unresolved\n",
58           rcl->check->target, rcl->check->module, rcl->check->name);
59   free(rcl);
60   return 0;
61 }
62
63 int
64 noit_check_schedule_next(noit_module_t *self,
65                          struct timeval *last_check, noit_check_t *check,
66                          struct timeval *now, dispatch_func_t dispatch) {
67   eventer_t newe;
68   struct timeval period, earliest;
69   recur_closure_t *rcl;
70
71   assert(check->fire_event == NULL);
72   if(check->period == 0) return 0;
73   if(NOIT_CHECK_DISABLED(check) || NOIT_CHECK_KILLED(check)) return 0;
74
75   /* If we have an event, we know when we intended it to fire.  This means
76    * we should schedule that point + period.
77    */
78   if(now)
79     memcpy(&earliest, now, sizeof(earliest));
80   else
81     gettimeofday(&earliest, NULL);
82
83   /* If the check is unconfigured and needs resolving, we'll set the
84    * period down a bit lower so we can pick up the resolution quickly.
85    */
86   if(!NOIT_CHECK_RESOLVED(check) && NOIT_CHECK_SHOULD_RESOLVE(check) &&
87       check->period > 1000) {
88     period.tv_sec = 1;
89     period.tv_usec = 0;
90   }
91   else {
92     period.tv_sec = check->period / 1000;
93     period.tv_usec = (check->period % 1000) * 1000;
94   }
95
96   newe = eventer_alloc();
97   memcpy(&newe->whence, last_check, sizeof(*last_check));
98   add_timeval(newe->whence, period, &newe->whence);
99   if(compare_timeval(newe->whence, earliest) < 0)
100     memcpy(&newe->whence, &earliest, sizeof(earliest));
101   newe->mask = EVENTER_TIMER;
102   newe->callback = noit_check_recur_handler;
103   rcl = calloc(1, sizeof(*rcl));
104   rcl->self = self;
105   rcl->check = check;
106   rcl->dispatch = dispatch;
107   newe->closure = rcl;
108
109   eventer_add(newe);
110   check->fire_event = newe;
111   return 0;
112 }
113
114 void
115 noit_check_run_full_asynch(noit_check_t *check, eventer_func_t callback) {
116   struct timeval __now, p_int;
117   eventer_t e;
118   e = eventer_alloc();
119   e->fd = -1;
120   e->mask = EVENTER_ASYNCH;
121   gettimeofday(&__now, NULL);
122   memcpy(&e->whence, &__now, sizeof(__now));
123   p_int.tv_sec = check->timeout / 1000;
124   p_int.tv_usec = (check->timeout % 1000) * 1000;
125   add_timeval(e->whence, p_int, &e->whence);
126   e->callback = callback;
127   e->closure =  check->closure;
128   eventer_add(e);
129 }
130
131 void
132 noit_check_tools_init() {
133   noit_check_tools_shared_init();
134   eventer_name_callback("noit_check_recur_handler", noit_check_recur_handler);
135 }
136
Note: See TracBrowser for help on using the browser.