static void daemon_incoming_packet(void *, struct ctdb_req_header *);
+/* called when the "startup" event script has finished */
+static void ctdb_start_transport(struct ctdb_context *ctdb, int status)
+{
+ if (status != 0) {
+ DEBUG(0,("startup event failed!\n"));
+ ctdb_fatal(ctdb, "startup event script failed");
+ }
+
+ /* start the transport running */
+ if (ctdb->methods->start(ctdb) != 0) {
+ DEBUG(0,("transport failed to start!\n"));
+ ctdb_fatal(ctdb, "transport failed to start");
+ }
+
+ /* start the recovery daemon process */
+ if (ctdb_start_recoverd(ctdb) != 0) {
+ DEBUG(0,("Failed to start recovery daemon\n"));
+ exit(11);
+ }
+}
+
+/* go into main ctdb loop */
static void ctdb_main_loop(struct ctdb_context *ctdb)
{
int ret = -1;
return;
}
- ret = ctdb_event_script(ctdb, "startup");
- if (ret != 0) {
- DEBUG(0,("Failed startup event script\n"));
- return;
- }
-
- /* start the transport running */
- if (ctdb->methods->start(ctdb) != 0) {
- DEBUG(0,("transport failed to start!\n"));
- ctdb_fatal(ctdb, "transport failed to start");
+ /* initialise the transport */
+ if (ctdb->methods->initialise(ctdb) != 0) {
+ DEBUG(0,("transport failed to initialise!\n"));
+ ctdb_fatal(ctdb, "transport failed to initialise");
}
/* tell all other nodes we've just started up */
CTDB_CTRL_FLAG_NOREPLY,
tdb_null, NULL, NULL);
+ ret = ctdb_event_script_callback(ctdb, ctdb_start_transport, "startup");
+ if (ret != 0) {
+ DEBUG(0,("Failed startup event script\n"));
+ return;
+ }
+
/* go into a wait loop to allow other nodes to complete */
event_loop_wait(ctdb->ev);
/* try to set us up as realtime */
ctdb_set_realtime();
- /* start the recovery daemon process */
- if (ctdb_start_recoverd(ctdb) != 0) {
- DEBUG(0,("Failed to start recovery daemon\n"));
- exit(11);
- }
-
/* ensure the socket is deleted on exit of the daemon */
domain_socket_name = talloc_strdup(talloc_autofree_context(), ctdb->daemon.name);
talloc_set_destructor(domain_socket_name, unlink_destructor);
close(fd[1]);
+ /* we need a new event context */
+ talloc_free(ctdb->ev);
+ ctdb->ev = event_context_init(ctdb);
+
event_add_fd(ctdb->ev, ctdb, fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
ctdb_recoverd_parent, &fd[0]);
}
/*
- * Start infiniband
+ * initialise infiniband
*/
-static int ctdb_ibw_start(struct ctdb_context *ctdb)
+static int ctdb_ibw_initialise(struct ctdb_context *ctdb)
{
int i, ret;
}
}
+ return 0;
+}
+
+
+/*
+ * Start infiniband
+ */
+static int ctdb_ibw_start(struct ctdb_context *ctdb)
+{
+ int i, ret;
+
/* listen on our own address */
if (ctdb_ibw_listen(ctdb, 10)) /* TODO: backlog as param */
return -1;
#endif /* __NOTDEF__ */
static const struct ctdb_methods ctdb_ibw_methods = {
+ .initialise= ctdb_ibw_initialise,
.start = ctdb_ibw_start,
.queue_pkt = ctdb_ibw_queue_pkt,
.add_node = ctdb_ibw_add_node,
transport specific methods
*/
struct ctdb_methods {
+ int (*initialise)(struct ctdb_context *); /* initialise transport structures */
int (*start)(struct ctdb_context *); /* start protocol processing */
int (*add_node)(struct ctdb_node *); /* setup a new node */
int (*queue_pkt)(struct ctdb_node *, uint8_t *data, uint32_t length);
void ctdb_takeover_client_destructor_hook(struct ctdb_client *client);
int ctdb_event_script(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
+int ctdb_event_script_callback(struct ctdb_context *ctdb,
+ void (*callback)(struct ctdb_context *, int),
+ const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
void ctdb_release_all_ips(struct ctdb_context *ctdb);
#include "system/filesys.h"
#include "system/wait.h"
#include "../include/ctdb_private.h"
+#include "lib/events/events.h"
#include <net/ethernet.h>
#include <net/if_arp.h>
}
/*
- run the event script
+ run the event script - varargs version
*/
-int ctdb_event_script(struct ctdb_context *ctdb, const char *fmt, ...)
+static int ctdb_event_script_v(struct ctdb_context *ctdb, const char *fmt, va_list ap)
{
- va_list ap;
char *options, *cmdstr;
int ret;
+ va_list ap2;
+ struct stat st;
- va_start(ap, fmt);
- options = talloc_vasprintf(ctdb, fmt, ap);
- va_end(ap);
+ if (stat(ctdb->takeover.event_script, &st) != 0 &&
+ errno == ENOENT) {
+ DEBUG(0,("No event script found at '%s'\n", ctdb->takeover.event_script));
+ return 0;
+ }
+
+ va_copy(ap2, ap);
+ options = talloc_vasprintf(ctdb, fmt, ap2);
+ va_end(ap2);
CTDB_NO_MEMORY(ctdb, options);
cmdstr = talloc_asprintf(ctdb, "%s %s", ctdb->takeover.event_script, options);
return ret;
}
+/*
+ run the event script
+ */
+int ctdb_event_script(struct ctdb_context *ctdb, const char *fmt, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start(ap, fmt);
+ ret = ctdb_event_script_v(ctdb, fmt, ap);
+ va_end(ap);
+
+ return ret;
+}
+
+
+struct ctdb_event_script_state {
+ struct ctdb_context *ctdb;
+ pid_t child;
+ void (*callback)(struct ctdb_context *, int);
+ int fd[2];
+};
+
+/* called when child is finished */
+static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde,
+ uint16_t flags, void *p)
+{
+ struct ctdb_event_script_state *state =
+ talloc_get_type(p, struct ctdb_event_script_state);
+ int status = -1;
+ waitpid(state->child, &status, 0);
+ if (status != -1) {
+ status = WEXITSTATUS(status);
+ }
+ state->callback(state->ctdb, status);
+ talloc_free(state);
+}
+
+
+/*
+ run the event script in the background, calling the callback when
+ finished
+ */
+int ctdb_event_script_callback(struct ctdb_context *ctdb,
+ void (*callback)(struct ctdb_context *, int),
+ const char *fmt, ...)
+{
+ struct ctdb_event_script_state *state;
+ va_list ap;
+ int ret;
+
+ state = talloc(ctdb, struct ctdb_event_script_state);
+ CTDB_NO_MEMORY(ctdb, state);
+
+ state->ctdb = ctdb;
+ state->callback = callback;
+
+ ret = pipe(state->fd);
+ if (ret != 0) {
+ talloc_free(state);
+ return -1;
+ }
+
+ state->child = fork();
+
+ if (state->child == (pid_t)-1) {
+ close(state->fd[0]);
+ close(state->fd[1]);
+ talloc_free(state);
+ return -1;
+ }
+
+ if (state->child == 0) {
+ close(state->fd[0]);
+ va_start(ap, fmt);
+ ret = ctdb_event_script_v(ctdb, fmt, ap);
+ va_end(ap);
+ _exit(ret);
+ }
+
+ close(state->fd[1]);
+
+ event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
+ ctdb_event_script_handler, state);
+
+ return 0;
+}
}
/*
- start the protocol going
+ initialise transport structures
*/
-static int ctdb_tcp_start(struct ctdb_context *ctdb)
+static int ctdb_tcp_initialise(struct ctdb_context *ctdb)
{
int i;
}
}
+ return 0;
+}
+
+/*
+ start the protocol going
+*/
+static int ctdb_tcp_start(struct ctdb_context *ctdb)
+{
+ int i;
+
/* listen on our own address */
if (ctdb_tcp_listen(ctdb) != 0) return -1;
static const struct ctdb_methods ctdb_tcp_methods = {
+ .initialise = ctdb_tcp_initialise,
.start = ctdb_tcp_start,
.queue_pkt = ctdb_tcp_queue_pkt,
.add_node = ctdb_tcp_add_node,