dbwrap: add enum dbwrap_req_state
[Samba.git] / ctdb / server / eventscript.c
blob438fbbbcaa4966fde7801c4976d34d61841183bd
1 /*
2 event script handling
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/>.
20 #include "replace.h"
21 #include "system/filesys.h"
22 #include "system/network.h"
23 #include "system/wait.h"
24 #include "system/dir.h"
25 #include "system/locale.h"
26 #include "system/time.h"
27 #include "system/dir.h"
29 #include <talloc.h>
30 #include <tevent.h>
32 #include "lib/util/dlinklist.h"
33 #include "lib/util/debug.h"
34 #include "lib/util/samba_util.h"
35 #include "lib/util/sys_rw.h"
37 #include "ctdb_private.h"
39 #include "common/rb_tree.h"
40 #include "common/common.h"
41 #include "common/logging.h"
42 #include "common/reqid.h"
43 #include "common/sock_io.h"
45 #include "protocol/protocol_api.h"
48 * Setting up event daemon
51 struct eventd_context {
52 struct tevent_context *ev;
53 const char *path;
54 const char *script_dir;
55 const char *pidfile;
56 const char *socket;
57 const char *debug_hung_script;
59 /* server state */
60 pid_t eventd_pid;
61 struct tevent_fd *eventd_fde;
63 /* client state */
64 struct reqid_context *idr;
65 struct sock_queue *queue;
66 struct eventd_client_state *calls;
69 static bool eventd_context_init(TALLOC_CTX *mem_ctx,
70 struct ctdb_context *ctdb,
71 struct eventd_context **out)
73 struct eventd_context *ectx;
74 const char *eventd = CTDB_HELPER_BINDIR "/ctdb_eventd";
75 const char *debug_hung_script = CTDB_ETCDIR "/debug-hung-script.sh";
76 const char *value;
77 char *socket;
78 int ret;
80 ectx = talloc_zero(mem_ctx, struct eventd_context);
81 if (ectx == NULL) {
82 return false;
85 ectx->ev = ctdb->ev;
87 value = getenv("CTDB_EVENTD");
88 if (value != NULL) {
89 eventd = value;
92 ectx->path = talloc_strdup(ectx, eventd);
93 if (ectx->path == NULL) {
94 talloc_free(ectx);
95 return false;
98 ectx->script_dir = ctdb->event_script_dir;
100 socket = talloc_strdup(ectx, ctdb_get_socketname(ctdb));
101 if (socket == NULL) {
102 talloc_free(ectx);
103 return NULL;
106 ectx->socket = talloc_asprintf(ectx, "%s/eventd.sock",
107 dirname(socket));
108 if (ectx->socket == NULL) {
109 talloc_free(ectx);
110 return false;
113 talloc_free(socket);
115 value = getenv("CTDB_DEBUG_HUNG_SCRIPT");
116 if (value != NULL) {
117 if (value[0] == '\0') {
118 debug_hung_script = NULL;
119 } else {
120 debug_hung_script = value;
124 if (debug_hung_script != NULL) {
125 ectx->debug_hung_script = talloc_strdup(ectx,
126 debug_hung_script);
127 if (ectx->debug_hung_script == NULL) {
128 talloc_free(ectx);
129 return false;
133 ret = reqid_init(ectx, 1, &ectx->idr);
134 if (ret != 0) {
135 talloc_free(ectx);
136 return false;
139 ectx->eventd_pid = -1;
141 *out = ectx;
142 return true;
146 * Start and stop event daemon
149 static bool eventd_client_connect(struct eventd_context *ectx);
150 static void eventd_dead_handler(struct tevent_context *ev,
151 struct tevent_fd *fde, uint16_t flags,
152 void *private_data);
154 int ctdb_start_eventd(struct ctdb_context *ctdb)
156 struct eventd_context *ectx;
157 const char **argv;
158 int fd[2];
159 pid_t pid;
160 int ret, i;
161 bool status;
163 if (ctdb->ectx == NULL) {
164 status = eventd_context_init(ctdb, ctdb, &ctdb->ectx);
165 if (! status) {
166 DEBUG(DEBUG_ERR,
167 ("Failed to initialize eventd context\n"));
168 return -1;
172 ectx = ctdb->ectx;
174 if (! sock_clean(ectx->socket)) {
175 return -1;
178 argv = talloc_array(ectx, const char *, 14);
179 if (argv == NULL) {
180 return -1;
183 argv[0] = ectx->path;
184 argv[1] = "-e";
185 argv[2] = ectx->script_dir;
186 argv[3] = "-s";
187 argv[4] = ectx->socket;
188 argv[5] = "-P";
189 argv[6] = talloc_asprintf(argv, "%d", ctdb->ctdbd_pid);
190 argv[7] = "-l";
191 argv[8] = getenv("CTDB_LOGGING");
192 argv[9] = "-d";
193 argv[10] = debug_level_to_string(DEBUGLEVEL);
194 if (ectx->debug_hung_script == NULL) {
195 argv[11] = NULL;
196 argv[12] = NULL;
197 } else {
198 argv[11] = "-D";
199 argv[12] = ectx->debug_hung_script;
201 argv[13] = NULL;
203 if (argv[6] == NULL) {
204 talloc_free(argv);
205 return -1;
208 DEBUG(DEBUG_NOTICE,
209 ("Starting event daemon %s %s %s %s %s %s %s %s %s %s %s\n",
210 argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
211 argv[6], argv[7], argv[8], argv[9], argv[10]));
213 ret = pipe(fd);
214 if (ret != 0) {
215 return -1;
218 pid = ctdb_fork(ctdb);
219 if (pid == -1) {
220 close(fd[0]);
221 close(fd[1]);
222 return -1;
225 if (pid == 0) {
226 close(fd[0]);
227 ret = execv(argv[0], discard_const(argv));
228 if (ret == -1) {
229 _exit(errno);
231 _exit(0);
234 talloc_free(argv);
235 close(fd[1]);
237 ectx->eventd_fde = tevent_add_fd(ctdb->ev, ectx, fd[0],
238 TEVENT_FD_READ,
239 eventd_dead_handler, ectx);
240 if (ectx->eventd_fde == NULL) {
241 ctdb_kill(ctdb, pid, SIGKILL);
242 close(fd[0]);
243 return -1;
246 tevent_fd_set_auto_close(ectx->eventd_fde);
247 ectx->eventd_pid = pid;
249 /* Wait to connect to eventd */
250 for (i=0; i<10; i++) {
251 status = eventd_client_connect(ectx);
252 if (status) {
253 break;
255 sleep(1);
258 if (! status) {
259 DEBUG(DEBUG_ERR, ("Failed to initialize event daemon\n"));
260 ctdb_stop_eventd(ctdb);
261 return -1;
264 return 0;
267 static void eventd_dead_handler(struct tevent_context *ev,
268 struct tevent_fd *fde, uint16_t flags,
269 void *private_data)
271 struct eventd_context *ectx = talloc_get_type_abort(
272 private_data, struct eventd_context);
274 DEBUG(DEBUG_ERR, ("Eventd went away\n"));
276 TALLOC_FREE(ectx->eventd_fde);
277 ectx->eventd_pid = -1;
280 void ctdb_stop_eventd(struct ctdb_context *ctdb)
282 struct eventd_context *ectx = ctdb->ectx;
284 if (ectx == NULL) {
285 return;
288 TALLOC_FREE(ectx->eventd_fde);
289 if (ectx->eventd_pid != -1) {
290 kill(ectx->eventd_pid, SIGTERM);
291 ectx->eventd_pid = -1;
293 TALLOC_FREE(ctdb->ectx);
297 * Connect to event daemon
300 struct eventd_client_state {
301 struct eventd_client_state *prev, *next;
303 struct eventd_context *ectx;
304 void (*callback)(struct ctdb_event_reply *reply, void *private_data);
305 void *private_data;
307 uint32_t reqid;
308 uint8_t *buf;
309 size_t buflen;
312 static void eventd_client_read(uint8_t *buf, size_t buflen,
313 void *private_data);
314 static int eventd_client_state_destructor(struct eventd_client_state *state);
316 static bool eventd_client_connect(struct eventd_context *ectx)
318 int fd;
320 if (ectx->queue != NULL) {
321 return true;
324 fd = sock_connect(ectx->socket);
325 if (fd == -1) {
326 return false;
329 ectx->queue = sock_queue_setup(ectx, ectx->ev, fd,
330 eventd_client_read, ectx);
331 if (ectx->queue == NULL) {
332 close(fd);
333 return false;
336 return true;
339 static int eventd_client_write(struct eventd_context *ectx,
340 TALLOC_CTX *mem_ctx,
341 struct ctdb_event_request *request,
342 void (*callback)(struct ctdb_event_reply *reply,
343 void *private_data),
344 void *private_data)
346 struct eventd_client_state *state;
347 int ret;
349 if (! eventd_client_connect(ectx)) {
350 return -1;
353 state = talloc_zero(mem_ctx, struct eventd_client_state);
354 if (state == NULL) {
355 return -1;
358 state->ectx = ectx;
359 state->callback = callback;
360 state->private_data = private_data;
362 state->reqid = reqid_new(ectx->idr, state);
363 if (state->reqid == REQID_INVALID) {
364 talloc_free(state);
365 return -1;
368 talloc_set_destructor(state, eventd_client_state_destructor);
370 ctdb_event_header_fill(&request->header, state->reqid);
371 state->buflen = ctdb_event_request_len(request);
372 state->buf = talloc_size(state, state->buflen);
373 if (state->buf == NULL) {
374 talloc_free(state);
375 return -1;
378 ret = ctdb_event_request_push(request, state->buf, &state->buflen);
379 if (ret != 0) {
380 talloc_free(state);
381 return -1;
384 ret = sock_queue_write(ectx->queue, state->buf, state->buflen);
385 if (ret != 0) {
386 talloc_free(state);
387 return -1;
390 DLIST_ADD(ectx->calls, state);
392 return 0;
395 static int eventd_client_state_destructor(struct eventd_client_state *state)
397 struct eventd_context *ectx = state->ectx;
399 reqid_remove(ectx->idr, state->reqid);
400 DLIST_REMOVE(ectx->calls, state);
401 return 0;
404 static void eventd_client_read(uint8_t *buf, size_t buflen,
405 void *private_data)
407 struct eventd_context *ectx = talloc_get_type_abort(
408 private_data, struct eventd_context);
409 struct eventd_client_state *state;
410 struct ctdb_event_reply *reply;
411 int ret;
413 if (buf == NULL) {
414 /* connection lost */
415 TALLOC_FREE(ectx->queue);
416 return;
419 reply = talloc_zero(ectx, struct ctdb_event_reply);
420 if (reply == NULL) {
421 return;
424 ret = ctdb_event_reply_pull(buf, buflen, reply, reply);
425 if (ret != 0) {
426 D_ERR("Invalid packet received, ret=%d\n", ret);
427 talloc_free(reply);
428 return;
431 if (buflen != reply->header.length) {
432 D_ERR("Packet size mismatch %zu != %"PRIu32"\n",
433 buflen, reply->header.length);
434 talloc_free(reply);
435 return;
438 state = reqid_find(ectx->idr, reply->header.reqid,
439 struct eventd_client_state);
440 if (state == NULL) {
441 talloc_free(reply);
442 return;
445 if (state->reqid != reply->header.reqid) {
446 talloc_free(reply);
447 return;
450 state = talloc_steal(reply, state);
451 state->callback(reply, state->private_data);
452 talloc_free(reply);
456 * Run an event
459 struct eventd_client_run_state {
460 struct eventd_context *ectx;
461 void (*callback)(int result, void *private_data);
462 void *private_data;
465 static void eventd_client_run_done(struct ctdb_event_reply *reply,
466 void *private_data);
468 static int eventd_client_run(struct eventd_context *ectx,
469 TALLOC_CTX *mem_ctx,
470 void (*callback)(int result,
471 void *private_data),
472 void *private_data,
473 enum ctdb_event event,
474 const char *arg_str,
475 uint32_t timeout)
477 struct eventd_client_run_state *state;
478 struct ctdb_event_request request;
479 struct ctdb_event_request_run rdata;
480 int ret;
482 state = talloc_zero(mem_ctx, struct eventd_client_run_state);
483 if (state == NULL) {
484 return -1;
487 state->ectx = ectx;
488 state->callback = callback;
489 state->private_data = private_data;
491 rdata.event = event;
492 rdata.timeout = timeout;
493 rdata.arg_str = arg_str;
495 request.rdata.command = CTDB_EVENT_COMMAND_RUN;
496 request.rdata.data.run = &rdata;
498 ret = eventd_client_write(ectx, state, &request,
499 eventd_client_run_done, state);
500 if (ret != 0) {
501 talloc_free(state);
502 return ret;
505 return 0;
508 static void eventd_client_run_done(struct ctdb_event_reply *reply,
509 void *private_data)
511 struct eventd_client_run_state *state = talloc_get_type_abort(
512 private_data, struct eventd_client_run_state);
514 state = talloc_steal(state->ectx, state);
515 state->callback(reply->rdata.result, state->private_data);
516 talloc_free(state);
520 * CTDB event script functions
523 int ctdb_event_script_run(struct ctdb_context *ctdb,
524 TALLOC_CTX *mem_ctx,
525 void (*callback)(struct ctdb_context *ctdb,
526 int result, void *private_data),
527 void *private_data,
528 enum ctdb_event event,
529 const char *fmt, va_list ap)
530 PRINTF_ATTRIBUTE(6,0);
532 struct ctdb_event_script_run_state {
533 struct ctdb_context *ctdb;
534 void (*callback)(struct ctdb_context *ctdb, int result,
535 void *private_data);
536 void *private_data;
537 enum ctdb_event event;
540 static bool event_allowed_during_recovery(enum ctdb_event event);
541 static void ctdb_event_script_run_done(int result, void *private_data);
542 static bool check_options(enum ctdb_event call, const char *options);
544 int ctdb_event_script_run(struct ctdb_context *ctdb,
545 TALLOC_CTX *mem_ctx,
546 void (*callback)(struct ctdb_context *ctdb,
547 int result, void *private_data),
548 void *private_data,
549 enum ctdb_event event,
550 const char *fmt, va_list ap)
552 struct ctdb_event_script_run_state *state;
553 char *arg_str;
554 int ret;
556 if ( (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) &&
557 (! event_allowed_during_recovery(event)) ) {
558 DEBUG(DEBUG_ERR,
559 ("Refusing to run event '%s' while in recovery\n",
560 ctdb_eventscript_call_names[event]));
563 state = talloc_zero(mem_ctx, struct ctdb_event_script_run_state);
564 if (state == NULL) {
565 return -1;
568 state->ctdb = ctdb;
569 state->callback = callback;
570 state->private_data = private_data;
571 state->event = event;
573 if (fmt != NULL) {
574 arg_str = talloc_vasprintf(state, fmt, ap);
575 if (arg_str == NULL) {
576 talloc_free(state);
577 return -1;
579 } else {
580 arg_str = NULL;
583 if (! check_options(event, arg_str)) {
584 DEBUG(DEBUG_ERR,
585 ("Bad event script arguments '%s' for '%s'\n",
586 arg_str, ctdb_eventscript_call_names[event]));
587 talloc_free(arg_str);
588 return -1;
591 ret = eventd_client_run(ctdb->ectx, state,
592 ctdb_event_script_run_done, state,
593 event, arg_str, ctdb->tunable.script_timeout);
594 if (ret != 0) {
595 talloc_free(state);
596 return ret;
599 DEBUG(DEBUG_INFO,
600 (__location__ " Running event %s with arguments %s\n",
601 ctdb_eventscript_call_names[event], arg_str));
603 talloc_free(arg_str);
604 return 0;
607 static void ctdb_event_script_run_done(int result, void *private_data)
609 struct ctdb_event_script_run_state *state = talloc_get_type_abort(
610 private_data, struct ctdb_event_script_run_state);
612 if (result == -ETIME) {
613 switch (state->event) {
614 case CTDB_EVENT_START_RECOVERY:
615 case CTDB_EVENT_RECOVERED:
616 case CTDB_EVENT_TAKE_IP:
617 case CTDB_EVENT_RELEASE_IP:
618 DEBUG(DEBUG_ERR,
619 ("Ignoring hung script for %s event\n",
620 ctdb_eventscript_call_names[state->event]));
621 result = 0;
622 break;
624 default:
625 break;
629 state = talloc_steal(state->ctdb, state);
630 state->callback(state->ctdb, result, state->private_data);
631 talloc_free(state);
635 static unsigned int count_words(const char *options)
637 unsigned int words = 0;
639 if (options == NULL) {
640 return 0;
643 options += strspn(options, " \t");
644 while (*options) {
645 words++;
646 options += strcspn(options, " \t");
647 options += strspn(options, " \t");
649 return words;
652 static bool check_options(enum ctdb_event call, const char *options)
654 switch (call) {
655 /* These all take no arguments. */
656 case CTDB_EVENT_INIT:
657 case CTDB_EVENT_SETUP:
658 case CTDB_EVENT_STARTUP:
659 case CTDB_EVENT_START_RECOVERY:
660 case CTDB_EVENT_RECOVERED:
661 case CTDB_EVENT_MONITOR:
662 case CTDB_EVENT_SHUTDOWN:
663 case CTDB_EVENT_IPREALLOCATED:
664 return count_words(options) == 0;
666 case CTDB_EVENT_TAKE_IP: /* interface, IP address, netmask bits. */
667 case CTDB_EVENT_RELEASE_IP:
668 return count_words(options) == 3;
670 case CTDB_EVENT_UPDATE_IP: /* old interface, new interface, IP address, netmask bits. */
671 return count_words(options) == 4;
673 default:
674 DEBUG(DEBUG_ERR,(__location__ "Unknown ctdb_event %u\n", call));
675 return false;
679 /* only specific events are allowed while in recovery */
680 static bool event_allowed_during_recovery(enum ctdb_event event)
682 const enum ctdb_event allowed_events[] = {
683 CTDB_EVENT_INIT,
684 CTDB_EVENT_SETUP,
685 CTDB_EVENT_START_RECOVERY,
686 CTDB_EVENT_SHUTDOWN,
687 CTDB_EVENT_RELEASE_IP,
688 CTDB_EVENT_IPREALLOCATED,
690 int i;
692 for (i = 0; i < ARRAY_SIZE(allowed_events); i++) {
693 if (event == allowed_events[i]) {
694 return true;
698 return false;
702 run the event script in the background, calling the callback when
703 finished. If mem_ctx is freed, callback will never be called.
705 int ctdb_event_script_callback(struct ctdb_context *ctdb,
706 TALLOC_CTX *mem_ctx,
707 void (*callback)(struct ctdb_context *, int, void *),
708 void *private_data,
709 enum ctdb_event call,
710 const char *fmt, ...)
712 va_list ap;
713 int ret;
715 va_start(ap, fmt);
716 ret = ctdb_event_script_run(ctdb, mem_ctx, callback, private_data,
717 call, fmt, ap);
718 va_end(ap);
720 return ret;
724 struct ctdb_event_script_args_state {
725 bool done;
726 int status;
729 static void ctdb_event_script_args_done(struct ctdb_context *ctdb,
730 int status, void *private_data)
732 struct ctdb_event_script_args_state *s =
733 (struct ctdb_event_script_args_state *)private_data;
735 s->done = true;
736 s->status = status;
740 run the event script, waiting for it to complete. Used when the caller
741 doesn't want to continue till the event script has finished.
743 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_event call,
744 const char *fmt, ...)
746 va_list ap;
747 int ret;
748 struct ctdb_event_script_args_state state = {
749 .status = -1,
750 .done = false,
753 va_start(ap, fmt);
754 ret = ctdb_event_script_run(ctdb, ctdb,
755 ctdb_event_script_args_done, &state,
756 call, fmt, ap);
757 va_end(ap);
758 if (ret != 0) {
759 return ret;
762 while (! state.done) {
763 tevent_loop_once(ctdb->ev);
766 if (state.status == -ETIME) {
767 /* Don't ban self if CTDB is starting up or shutting down */
768 if (call != CTDB_EVENT_INIT && call != CTDB_EVENT_SHUTDOWN) {
769 DEBUG(DEBUG_ERR,
770 (__location__ " eventscript for '%s' timed out."
771 " Immediately banning ourself for %d seconds\n",
772 ctdb_eventscript_call_names[call],
773 ctdb->tunable.recovery_ban_period));
774 ctdb_ban_self(ctdb);
778 return state.status;
781 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_event call)
783 /* GCC complains about empty format string, so use %s and "". */
784 return ctdb_event_script_args(ctdb, call, NULL);