1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef MOD_WATCHDOG_H
18 #define MOD_WATCHDOG_H
21 * @file mod_watchdog.h
22 * @brief Watchdog module for Apache
24 * @defgroup MOD_WATCHDOG watchdog
25 * @ingroup APACHE_MODS
30 #include "http_config.h"
32 #include "ap_provider.h"
35 #include "apr_strings.h"
36 #include "apr_pools.h"
39 #include "apr_hooks.h"
40 #include "apr_optional.h"
41 #include "apr_file_io.h"
43 #include "apr_thread_proc.h"
44 #include "apr_global_mutex.h"
45 #include "apr_thread_mutex.h"
52 * Default singleton watchdog instance name.
53 * Singleton watchdog is protected by mutex and
54 * guaranteed to be run inside a single child process
57 #define AP_WATCHDOG_SINGLETON "_singleton_"
60 * Default watchdog instance name
62 #define AP_WATCHDOG_DEFAULT "_default_"
65 * Default Watchdog interval
67 #define AP_WD_TM_INTERVAL APR_TIME_C(1000000) /* 1 second */
70 * Watchdog thread timer resolution
72 #define AP_WD_TM_SLICE APR_TIME_C(100000) /* 100 ms */
74 /* State values for callback */
75 #define AP_WATCHDOG_STATE_STARTING 1
76 #define AP_WATCHDOG_STATE_RUNNING 2
77 #define AP_WATCHDOG_STATE_STOPPING 3
79 typedef struct ap_watchdog_t ap_watchdog_t;
81 /* Create a set of AP_WD_DECLARE(type), AP_WD_DECLARE_NONSTD(type) and
82 * AP_WD_DECLARE_DATA with appropriate export and import tags for the platform
84 #if !defined(AP_WD_DECLARE)
86 #define AP_WD_DECLARE(type) type
87 #define AP_WD_DECLARE_NONSTD(type) type
88 #define AP_WD_DECLARE_DATA
89 #elif defined(AP_WD_DECLARE_STATIC)
90 #define AP_WD_DECLARE(type) type __stdcall
91 #define AP_WD_DECLARE_NONSTD(type) type
92 #define AP_WD_DECLARE_DATA
93 #elif defined(AP_WD_DECLARE_EXPORT)
94 #define AP_WD_DECLARE(type) __declspec(dllexport) type __stdcall
95 #define AP_WD_DECLARE_NONSTD(type) __declspec(dllexport) type
96 #define AP_WD_DECLARE_DATA __declspec(dllexport)
98 #define AP_WD_DECLARE(type) __declspec(dllimport) type __stdcall
99 #define AP_WD_DECLARE_NONSTD(type) __declspec(dllimport) type
100 #define AP_WD_DECLARE_DATA __declspec(dllimport)
105 * Callback function used for watchdog.
106 * @param state Watchdog state function. See @p AP_WATCHDOG_STATE_ .
107 * @param data is what was passed to @p ap_watchdog_register_callback function.
108 * @param pool Temporary callback pool destroyed after the call.
109 * @return APR_SUCCESS to continue calling this callback.
111 typedef apr_status_t ap_watchdog_callback_fn_t(int state, void *data,
115 * Get watchdog instance.
116 * @param watchdog Storage for watchdog instance.
117 * @param name Watchdog name.
118 * @param parent Non-zero to get the parent process watchdog instance.
119 * @param singleton Non-zero to get the singleton watchdog instance.
120 * @param p The pool to use.
121 * @return APR_SUCCESS if all went well
122 * @remark Use @p AP_WATCHDOG_DEFAULT to get default watchdog instance.
123 * If separate watchdog thread is needed provide unique name
124 * and function will create a new watchdog instance.
125 * Note that default client process watchdog works in singleton mode.
127 APR_DECLARE_OPTIONAL_FN(apr_status_t, ap_watchdog_get_instance,
128 (ap_watchdog_t **watchdog, const char *name, int parent,
129 int singleton, apr_pool_t *p));
132 * Register watchdog callback.
133 * @param watchdog Watchdog to use
134 * @param interval Interval on which the callback function will execute.
135 * @param callback The function to call on watchdog event.
136 * @param data The data to pass to the callback function.
137 * @return APR_SUCCESS if all went well. APR_EEXIST if already registered.
139 APR_DECLARE_OPTIONAL_FN(apr_status_t, ap_watchdog_register_callback,
140 (ap_watchdog_t *watchdog, apr_interval_time_t interval,
141 const void *data, ap_watchdog_callback_fn_t *callback));
144 * Update registered watchdog callback interval.
145 * @param w Watchdog to use
146 * @param interval New interval on which the callback function will execute.
147 * @param callback The function to call on watchdog event.
148 * @param data The data to pass to the callback function.
149 * @return APR_SUCCESS if all went well. APR_EOF if callback was not found.
151 APR_DECLARE_OPTIONAL_FN(apr_status_t, ap_watchdog_set_callback_interval,
152 (ap_watchdog_t *w, apr_interval_time_t interval,
153 const void *data, ap_watchdog_callback_fn_t *callback));
156 * Watchdog require hook.
157 * @param s The server record
158 * @param name Watchdog name.
159 * @param parent Non-zero to indicate the parent process watchdog mode.
160 * @param singleton Non-zero to indicate the singleton watchdog mode.
161 * @return OK to enable notifications from this watchdog, DECLINED otherwise.
162 * @remark This is called in post config phase for all watchdog instances
163 * that have no callbacks registered. Modules using this hook
164 * should ensure that their post_config hook is called after watchdog
167 APR_DECLARE_EXTERNAL_HOOK(ap, AP_WD, int, watchdog_need, (server_rec *s,
169 int parent, int singleton))
173 * Watchdog initialize hook.
174 * It is called after the watchdog thread is initialized.
175 * @param s The server record
176 * @param name Watchdog name.
177 * @param pool The pool used to create the watchdog.
179 APR_DECLARE_EXTERNAL_HOOK(ap, AP_WD, int, watchdog_init, (
185 * Watchdog terminate hook.
186 * It is called when the watchdog thread is terminated.
187 * @param s The server record
188 * @param name Watchdog name.
189 * @param pool The pool used to create the watchdog.
191 APR_DECLARE_EXTERNAL_HOOK(ap, AP_WD, int, watchdog_exit, (
197 * Fixed interval watchdog hook.
198 * It is called regularly on @p AP_WD_TM_INTERVAL interval.
199 * @param s The server record
200 * @param name Watchdog name.
201 * @param pool Temporary pool destroyed after the call.
203 APR_DECLARE_EXTERNAL_HOOK(ap, AP_WD, int, watchdog_step, (
212 #endif /* MOD_WATCHDOG_H */