4 Copyright (C) Andrew Tridgell 2007
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "system/wait.h"
23 #include "../include/ctdb_private.h"
24 #include "lib/events/events.h"
25 #include "../common/rb_tree.h"
30 run the event script - varargs version
31 this function is called and run in the context of a forked child
32 which allows it to do blocking calls such as system()
34 static int ctdb_event_script_v(struct ctdb_context *ctdb, const char *fmt, va_list ap)
36 char *options, *cmdstr;
40 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
47 run the main event script
49 if (stat(ctdb->takeover.main_event_script, &st) != 0 &&
51 DEBUG(0,("No event script found at '%s'\n", ctdb->takeover.main_event_script));
57 options = talloc_vasprintf(tmp_ctx, fmt, ap2);
59 CTDB_NO_MEMORY(ctdb, options);
61 cmdstr = talloc_asprintf(tmp_ctx, "%s %s",
62 ctdb->takeover.main_event_script, options);
63 CTDB_NO_MEMORY(ctdb, cmdstr);
67 /* if the system() call was successful, translate ret into the
68 return code from the command
71 ret = WEXITSTATUS(ret);
73 /* return an error if the script failed */
82 the service specific event scripts
84 if (stat(ctdb->takeover.event_script_dir, &st) != 0 &&
86 DEBUG(0,("No event script directory found at '%s'\n", ctdb->takeover.event_script_dir));
91 /* create a tree to store all the script names in */
92 tree = trbt_create(tmp_ctx, 0);
94 /* scan all directory entries and insert all valid scripts into the
97 dir = opendir(ctdb->takeover.event_script_dir);
99 DEBUG(0,("Failed to open event script directory '%s'\n", ctdb->takeover.event_script_dir));
100 talloc_free(tmp_ctx);
103 while ((de=readdir(dir)) != NULL) {
107 namlen = strlen(de->d_name);
113 if (de->d_name[namlen-1] == '~') {
114 /* skip files emacs left behind */
118 if (de->d_name[2] != '.') {
122 if ( (!isdigit(de->d_name[0])) || (!isdigit(de->d_name[1])) ) {
126 sscanf(de->d_name, "%2d.", &num);
128 /* store the event script in the tree */
129 script = trbt_insert32(tree, num, talloc_strdup(tmp_ctx, de->d_name));
130 if (script != NULL) {
131 DEBUG(0,("CONFIG ERROR: Multiple event scripts with the same prefix : %s and %s. Each event script MUST have a unique prefix\n", script, de->d_name));
132 talloc_free(tmp_ctx);
140 /* fetch the scripts from the tree one by one and execute
143 while ((script=trbt_findfirstarray32(tree, 1)) != NULL) {
145 options = talloc_vasprintf(tmp_ctx, fmt, ap2);
147 CTDB_NO_MEMORY(ctdb, options);
149 cmdstr = talloc_asprintf(tmp_ctx, "%s/%s %s",
150 ctdb->takeover.event_script_dir,
152 CTDB_NO_MEMORY(ctdb, cmdstr);
154 DEBUG(1,("Executing event script %s\n",cmdstr));
156 ret = system(cmdstr);
157 /* if the system() call was successful, translate ret into the
158 return code from the command
161 ret = WEXITSTATUS(ret);
163 /* return an error if the script failed */
165 DEBUG(0,("Event script %s failed with error %d\n", cmdstr, ret));
166 talloc_free(tmp_ctx);
173 talloc_free(tmp_ctx);
177 struct ctdb_event_script_state {
178 struct ctdb_context *ctdb;
180 void (*callback)(struct ctdb_context *, int, void *);
185 /* called when child is finished */
186 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
187 uint16_t flags, void *p)
189 struct ctdb_event_script_state *state =
190 talloc_get_type(p, struct ctdb_event_script_state);
192 void (*callback)(struct ctdb_context *, int, void *) = state->callback;
193 void *private_data = state->private_data;
194 struct ctdb_context *ctdb = state->ctdb;
196 waitpid(state->child, &status, 0);
198 status = WEXITSTATUS(status);
200 talloc_set_destructor(state, NULL);
202 callback(ctdb, status, private_data);
206 /* called when child times out */
207 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te,
208 struct timeval t, void *p)
210 struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
211 void (*callback)(struct ctdb_context *, int, void *) = state->callback;
212 void *private_data = state->private_data;
213 struct ctdb_context *ctdb = state->ctdb;
215 DEBUG(0,("event script timed out\n"));
217 callback(ctdb, -1, private_data);
221 destroy a running event script
223 static int event_script_destructor(struct ctdb_event_script_state *state)
225 kill(state->child, SIGKILL);
226 waitpid(state->child, NULL, 0);
231 run the event script in the background, calling the callback when
234 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb,
235 struct timeval timeout,
237 void (*callback)(struct ctdb_context *, int, void *),
239 const char *fmt, va_list ap)
241 struct ctdb_event_script_state *state;
244 state = talloc(mem_ctx, struct ctdb_event_script_state);
245 CTDB_NO_MEMORY(ctdb, state);
248 state->callback = callback;
249 state->private_data = private_data;
251 ret = pipe(state->fd);
257 state->child = fork();
259 if (state->child == (pid_t)-1) {
266 if (state->child == 0) {
268 if (ctdb->do_setsched) {
269 ctdb_restore_scheduler(ctdb);
271 set_close_on_exec(state->fd[1]);
272 ret = ctdb_event_script_v(ctdb, fmt, ap);
276 talloc_set_destructor(state, event_script_destructor);
280 event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
281 ctdb_event_script_handler, state);
283 if (!timeval_is_zero(&timeout)) {
284 event_add_timed(ctdb->ev, state, timeout, ctdb_event_script_timeout, state);
292 run the event script in the background, calling the callback when
295 int ctdb_event_script_callback(struct ctdb_context *ctdb,
296 struct timeval timeout,
298 void (*callback)(struct ctdb_context *, int, void *),
300 const char *fmt, ...)
306 ret = ctdb_event_script_callback_v(ctdb, timeout, mem_ctx, callback, private_data, fmt, ap);
313 struct callback_status {
319 called when ctdb_event_script() finishes
321 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
323 struct callback_status *s = (struct callback_status *)private_data;
329 run the event script, waiting for it to complete. Used when the caller doesn't want to
330 continue till the event script has finished.
332 int ctdb_event_script(struct ctdb_context *ctdb, const char *fmt, ...)
336 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
337 struct callback_status status;
340 ret = ctdb_event_script_callback_v(ctdb, timeval_zero(), tmp_ctx, event_script_callback, &status, fmt, ap);
344 talloc_free(tmp_ctx);
351 while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
353 talloc_free(tmp_ctx);
355 return status.status;