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