along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
-#include "includes.h"
-#include "db_wrap.h"
-#include "lib/tdb/include/tdb.h"
-#include "lib/util/dlinklist.h"
+#include "replace.h"
#include "system/network.h"
#include "system/filesys.h"
#include "system/wait.h"
-#include "../include/ctdb_client.h"
-#include "../include/ctdb_private.h"
-#include "../common/rb_tree.h"
-#include <sys/socket.h>
+#include "system/time.h"
+
+#include <talloc.h>
+/* Allow use of deprecated function tevent_loop_allow_nesting() */
+#define TEVENT_DEPRECATED
+#include <tevent.h>
+#include <tdb.h>
+
+#include "lib/tdb_wrap/tdb_wrap.h"
+#include "lib/util/dlinklist.h"
+#include "lib/util/debug.h"
+#include "lib/util/samba_util.h"
+
+#include "ctdb_version.h"
+#include "ctdb_private.h"
+#include "ctdb_client.h"
+#include "ctdb_logging.h"
+
+#include "common/rb_tree.h"
+#include "common/reqid.h"
+#include "common/system.h"
+#include "common/common.h"
struct ctdb_client_pid_list {
struct ctdb_client_pid_list *next, *prev;
struct ctdb_client *client;
};
+const char *ctdbd_pidfile = NULL;
+
static void daemon_incoming_packet(void *, struct ctdb_req_header *);
static void print_exit_message(void)
{
- DEBUG(DEBUG_NOTICE,("CTDB daemon shutting down\n"));
+ if (debug_extra != NULL && debug_extra[0] != '\0') {
+ DEBUG(DEBUG_NOTICE,("CTDB %s shutting down\n", debug_extra));
+ } else {
+ DEBUG(DEBUG_NOTICE,("CTDB daemon shutting down\n"));
+
+ /* Wait a second to allow pending log messages to be flushed */
+ sleep(1);
+ }
}
-static void ctdb_time_tick(struct event_context *ev, struct timed_event *te,
+static void ctdb_time_tick(struct tevent_context *ev, struct tevent_timer *te,
struct timeval t, void *private_data)
{
struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
- if (getpid() != ctdbd_pid) {
+ if (getpid() != ctdb->ctdbd_pid) {
return;
}
- event_add_timed(ctdb->ev, ctdb,
- timeval_current_ofs(1, 0),
- ctdb_time_tick, ctdb);
+ tevent_add_timer(ctdb->ev, ctdb,
+ timeval_current_ofs(1, 0),
+ ctdb_time_tick, ctdb);
}
/* Used to trigger a dummy event once per second, to make
*/
static void ctdb_start_time_tickd(struct ctdb_context *ctdb)
{
- event_add_timed(ctdb->ev, ctdb,
- timeval_current_ofs(1, 0),
- ctdb_time_tick, ctdb);
+ tevent_add_timer(ctdb->ev, ctdb,
+ timeval_current_ofs(1, 0),
+ ctdb_time_tick, ctdb);
}
-
-/* called when the "startup" event script has finished */
-static void ctdb_start_transport(struct ctdb_context *ctdb)
+static void ctdb_start_periodic_events(struct ctdb_context *ctdb)
{
- if (ctdb->methods == NULL) {
- DEBUG(DEBUG_ALERT,(__location__ " startup event finished but transport is DOWN.\n"));
- ctdb_fatal(ctdb, "transport is not initialized but startup completed");
- }
-
- /* start the transport running */
- if (ctdb->methods->start(ctdb) != 0) {
- DEBUG(DEBUG_ALERT,("transport failed to start!\n"));
- ctdb_fatal(ctdb, "transport failed to start");
- }
-
- /* start the recovery daemon process */
- if (ctdb_start_recoverd(ctdb) != 0) {
- DEBUG(DEBUG_ALERT,("Failed to start recovery daemon\n"));
- exit(11);
- }
-
- /* Make sure we log something when the daemon terminates */
- atexit(print_exit_message);
-
/* start monitoring for connected/disconnected nodes */
ctdb_start_keepalive(ctdb);
- /* start monitoring for node health */
- ctdb_start_monitoring(ctdb);
-
/* start periodic update of tcp tickle lists */
ctdb_start_tcp_tickle_update(ctdb);
ctdb_start_time_tickd(ctdb);
}
-static void block_signal(int signum)
+static void ignore_signal(int signum)
{
struct sigaction act;
message handler for when we are in daemon mode. This redirects the message
to the right client
*/
-static void daemon_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
- TDB_DATA data, void *private_data)
+static void daemon_message_handler(uint64_t srvid, TDB_DATA data,
+ void *private_data)
{
struct ctdb_client *client = talloc_get_type(private_data, struct ctdb_client);
- struct ctdb_req_message *r;
+ struct ctdb_req_message_old *r;
int len;
/* construct a message to send to the client containing the data */
- len = offsetof(struct ctdb_req_message, data) + data.dsize;
- r = ctdbd_allocate_pkt(ctdb, ctdb, CTDB_REQ_MESSAGE,
- len, struct ctdb_req_message);
- CTDB_NO_MEMORY_VOID(ctdb, r);
+ len = offsetof(struct ctdb_req_message_old, data) + data.dsize;
+ r = ctdbd_allocate_pkt(client->ctdb, client->ctdb, CTDB_REQ_MESSAGE,
+ len, struct ctdb_req_message_old);
+ CTDB_NO_MEMORY_VOID(client->ctdb, r);
talloc_set_name_const(r, "req_message packet");
*/
int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid)
{
- struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
+ struct ctdb_client *client = reqid_find(ctdb->idr, client_id, struct ctdb_client);
int res;
if (client == NULL) {
DEBUG(DEBUG_ERR,("Bad client_id in daemon_request_register_message_handler\n"));
return -1;
}
- res = ctdb_register_message_handler(ctdb, client, srvid, daemon_message_handler, client);
+ res = srvid_register(ctdb->srv, client, srvid, daemon_message_handler,
+ client);
if (res != 0) {
DEBUG(DEBUG_ERR,(__location__ " Failed to register handler %llu in daemon\n",
(unsigned long long)srvid));
*/
int daemon_deregister_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid)
{
- struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
+ struct ctdb_client *client = reqid_find(ctdb->idr, client_id, struct ctdb_client);
if (client == NULL) {
DEBUG(DEBUG_ERR,("Bad client_id in daemon_request_deregister_message_handler\n"));
return -1;
}
- return ctdb_deregister_message_handler(ctdb, srvid, client);
+ return srvid_deregister(ctdb->srv, srvid, client);
}
int daemon_check_srvids(struct ctdb_context *ctdb, TDB_DATA indata,
return -1;
}
for (i=0; i<num_ids; i++) {
- struct ctdb_message_list *ml;
- for (ml=ctdb->message_list; ml; ml=ml->next) {
- if (ml->srvid == ids[i]) {
- break;
- }
- }
- if (ml != NULL) {
+ if (srvid_exists(ctdb->srv, ids[i]) == 0) {
results[i/8] |= (1 << (i%8));
}
}
struct ctdb_db_context *ctdb_db;
ctdb_takeover_client_destructor_hook(client);
- ctdb_reqid_remove(client->ctdb, client->client_id);
- CTDB_DECREMENT_STAT(client->ctdb, num_clients);
+ reqid_remove(client->ctdb->idr, client->client_id);
+ client->ctdb->num_clients--;
if (client->num_persistent_updates != 0) {
DEBUG(DEBUG_ERR,(__location__ " Client disconnecting with %u persistent updates in flight. Starting recovery\n", client->num_persistent_updates));
"commit active. Forcing recovery.\n"));
client->ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
- /* legacy trans2 transaction state: */
- ctdb_db->transaction_active = false;
-
/*
* trans3 transaction state:
*
from a local client over the unix domain socket
*/
static void daemon_request_message_from_client(struct ctdb_client *client,
- struct ctdb_req_message *c)
+ struct ctdb_req_message_old *c)
{
TDB_DATA data;
int res;
{
struct daemon_call_state *dstate = talloc_get_type(state->async.private_data,
struct daemon_call_state);
- struct ctdb_reply_call *r;
+ struct ctdb_reply_call_old *r;
int res;
uint32_t length;
struct ctdb_client *client = dstate->client;
return;
}
- length = offsetof(struct ctdb_reply_call, data) + dstate->call->reply_data.dsize;
+ length = offsetof(struct ctdb_reply_call_old, data) + dstate->call->reply_data.dsize;
/* If the client asked for readonly FETCH, we remapped this to
FETCH_WITH_HEADER when calling the daemon. So we must
strip the extra header off the reply data before passing
}
r = ctdbd_allocate_pkt(client->ctdb, dstate, CTDB_REPLY_CALL,
- length, struct ctdb_reply_call);
+ length, struct ctdb_reply_call_old);
if (r == NULL) {
DEBUG(DEBUG_ERR, (__location__ " Failed to allocate reply_call in ctdb daemon\n"));
CTDB_DECREMENT_STAT(client->ctdb, pending_calls);
return;
}
- client = ctdb_reqid_find(w->ctdb, w->client_id, struct ctdb_client);
+ client = reqid_find(w->ctdb->idr, w->client_id, struct ctdb_client);
if (client == NULL) {
DEBUG(DEBUG_ERR,(__location__ " Packet for disconnected client %u\n",
w->client_id));
struct ctdb_deferred_fetch_call {
struct ctdb_deferred_fetch_call *next, *prev;
- struct ctdb_req_call *c;
+ struct ctdb_req_call_old *c;
struct ctdb_daemon_packet_wrap *w;
};
};
/* called from a timer event and starts reprocessing the deferred call.*/
-static void reprocess_deferred_call(struct event_context *ev, struct timed_event *te,
- struct timeval t, void *private_data)
+static void reprocess_deferred_call(struct tevent_context *ev,
+ struct tevent_timer *te,
+ struct timeval t, void *private_data)
{
struct ctdb_deferred_requeue *dfr = (struct ctdb_deferred_requeue *)private_data;
struct ctdb_client *client = dfr->client;
DLIST_REMOVE(dfq->deferred_calls, dfc);
- client = ctdb_reqid_find(dfc->w->ctdb, dfc->w->client_id, struct ctdb_client);
+ client = reqid_find(dfc->w->ctdb->idr, dfc->w->client_id, struct ctdb_client);
if (client == NULL) {
DEBUG(DEBUG_ERR,(__location__ " Packet for disconnected client %u\n",
dfc->w->client_id));
dfr->dfc = talloc_steal(dfr, dfc);
dfr->client = client;
- event_add_timed(dfc->w->ctdb->ev, client, timeval_zero(), reprocess_deferred_call, dfr);
+ tevent_add_timer(dfc->w->ctdb->ev, client, timeval_zero(),
+ reprocess_deferred_call, dfr);
}
return 0;
free the context and context for all deferred requests to cause them to be
re-inserted into the event system.
*/
-static void dfq_timeout(struct event_context *ev, struct timed_event *te,
- struct timeval t, void *private_data)
+static void dfq_timeout(struct tevent_context *ev, struct tevent_timer *te,
+ struct timeval t, void *private_data)
{
talloc_free(private_data);
}
uint32_t *k;
struct ctdb_deferred_fetch_queue *dfq;
- k = talloc_zero_size(call, ((call->key.dsize + 3) & 0xfffffffc) + 4);
+ k = ctdb_key_to_idkey(call, call->key);
if (k == NULL) {
DEBUG(DEBUG_ERR,("Failed to allocate key for deferred fetch\n"));
return -1;
}
- k[0] = (call->key.dsize + 3) / 4 + 1;
- memcpy(&k[1], call->key.dptr, call->key.dsize);
-
dfq = talloc(call, struct ctdb_deferred_fetch_queue);
if (dfq == NULL) {
DEBUG(DEBUG_ERR,("Failed to allocate key for deferred fetch queue structure\n"));
/* if the fetch havent completed in 30 seconds, just tear it all down
and let it try again as the events are reissued */
- event_add_timed(ctdb_db->ctdb->ev, dfq, timeval_current_ofs(30, 0), dfq_timeout, dfq);
+ tevent_add_timer(ctdb_db->ctdb->ev, dfq, timeval_current_ofs(30, 0),
+ dfq_timeout, dfq);
talloc_free(k);
return 0;
if it is, make this call deferred to be reprocessed later when
the in-flight fetch completes.
*/
-static int requeue_duplicate_fetch(struct ctdb_db_context *ctdb_db, struct ctdb_client *client, TDB_DATA key, struct ctdb_req_call *c)
+static int requeue_duplicate_fetch(struct ctdb_db_context *ctdb_db, struct ctdb_client *client, TDB_DATA key, struct ctdb_req_call_old *c)
{
uint32_t *k;
struct ctdb_deferred_fetch_queue *dfq;
struct ctdb_deferred_fetch_call *dfc;
- k = talloc_zero_size(c, ((key.dsize + 3) & 0xfffffffc) + 4);
+ k = ctdb_key_to_idkey(c, key);
if (k == NULL) {
DEBUG(DEBUG_ERR,("Failed to allocate key for deferred fetch\n"));
return -1;
}
- k[0] = (key.dsize + 3) / 4 + 1;
- memcpy(&k[1], key.dptr, key.dsize);
-
dfq = trbt_lookuparray32(ctdb_db->deferred_fetch, k[0], &k[0]);
if (dfq == NULL) {
talloc_free(k);
from a local client over the unix domain socket
*/
static void daemon_request_call_from_client(struct ctdb_client *client,
- struct ctdb_req_call *c)
+ struct ctdb_req_call_old *c)
{
struct ctdb_call_state *state;
struct ctdb_db_context *ctdb_db;
struct ctdb_daemon_packet_wrap *w;
CTDB_INCREMENT_STAT(ctdb, total_calls);
- CTDB_DECREMENT_STAT(ctdb, pending_calls);
+ CTDB_INCREMENT_STAT(ctdb, pending_calls);
ctdb_db = find_ctdb_db(client->ctdb, c->db_id);
if (!ctdb_db) {
ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header,
(struct ctdb_req_header *)c, &data,
- daemon_incoming_packet_wrap, w, True);
+ daemon_incoming_packet_wrap, w, true);
if (ret == -2) {
/* will retry later */
CTDB_DECREMENT_STAT(ctdb, pending_calls);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
}
+ CTDB_DECREMENT_STAT(ctdb, pending_calls);
return;
}
}
}
if (header.flags & CTDB_REC_RO_REVOKE_COMPLETE) {
- header.flags &= ~(CTDB_REC_RO_HAVE_DELEGATIONS|CTDB_REC_RO_HAVE_READONLY|CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_REVOKE_COMPLETE);
+ header.flags &= ~CTDB_REC_RO_FLAGS;
CTDB_INCREMENT_STAT(ctdb, total_ro_revokes);
CTDB_INCREMENT_DB_STAT(ctdb_db, db_ro_revokes);
if (ctdb_ltdb_store(ctdb_db, key, &header, data) != 0) {
if (ctdb_add_revoke_deferred_call(ctdb, ctdb_db, key, (struct ctdb_req_header *)c, daemon_incoming_packet, client) != 0) {
ctdb_fatal(ctdb, "Failed to add deferred call for revoke child");
}
+ CTDB_DECREMENT_STAT(ctdb, pending_calls);
return;
}
ctdb_fatal(ctdb, "Failed to add deferred call for revoke child");
}
+ CTDB_DECREMENT_STAT(ctdb, pending_calls);
return;
}
static void daemon_request_control_from_client(struct ctdb_client *client,
- struct ctdb_req_control *c);
+ struct ctdb_req_control_old *c);
/* data contains a packet from the client */
static void daemon_incoming_packet(void *p, struct ctdb_req_header *hdr)
goto done;
}
- if (hdr->ctdb_version != CTDB_VERSION) {
+ if (hdr->ctdb_version != CTDB_PROTOCOL) {
ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
goto done;
}
switch (hdr->operation) {
case CTDB_REQ_CALL:
CTDB_INCREMENT_STAT(ctdb, client.req_call);
- daemon_request_call_from_client(client, (struct ctdb_req_call *)hdr);
+ daemon_request_call_from_client(client, (struct ctdb_req_call_old *)hdr);
break;
case CTDB_REQ_MESSAGE:
CTDB_INCREMENT_STAT(ctdb, client.req_message);
- daemon_request_message_from_client(client, (struct ctdb_req_message *)hdr);
+ daemon_request_message_from_client(client, (struct ctdb_req_message_old *)hdr);
break;
case CTDB_REQ_CONTROL:
CTDB_INCREMENT_STAT(ctdb, client.req_control);
- daemon_request_control_from_client(client, (struct ctdb_req_control *)hdr);
+ daemon_request_control_from_client(client, (struct ctdb_req_control_old *)hdr);
break;
default:
return;
}
- if (hdr->ctdb_version != CTDB_VERSION) {
+ if (hdr->ctdb_version != CTDB_PROTOCOL) {
ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
return;
}
}
-static void ctdb_accept_client(struct event_context *ev, struct fd_event *fde,
- uint16_t flags, void *private_data)
+static void ctdb_accept_client(struct tevent_context *ev,
+ struct tevent_fd *fde, uint16_t flags,
+ void *private_data)
{
struct sockaddr_un addr;
socklen_t len;
client->ctdb = ctdb;
client->fd = fd;
- client->client_id = ctdb_reqid_new(ctdb, client);
+ client->client_id = reqid_new(ctdb->idr, client);
client->pid = peer_pid;
client_pid = talloc(client, struct ctdb_client_pid_list);
talloc_set_destructor(client, ctdb_client_destructor);
talloc_set_destructor(client_pid, ctdb_clientpid_destructor);
- CTDB_INCREMENT_STAT(ctdb, num_clients);
+ ctdb->num_clients++;
}
return -1;
}
- set_close_on_exec(ctdb->daemon.sd);
- set_nonblocking(ctdb->daemon.sd);
-
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
- strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path));
+ strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path)-1);
+
+ /* First check if an old ctdbd might be running */
+ if (connect(ctdb->daemon.sd,
+ (struct sockaddr *)&addr, sizeof(addr)) == 0) {
+ DEBUG(DEBUG_CRIT,
+ ("Something is already listening on ctdb socket '%s'\n",
+ ctdb->daemon.name));
+ goto failed;
+ }
+
+ /* Remove any old socket */
+ unlink(ctdb->daemon.name);
+
+ set_close_on_exec(ctdb->daemon.sd);
+ set_nonblocking(ctdb->daemon.sd);
if (bind(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
DEBUG(DEBUG_CRIT,("Unable to bind on ctdb socket '%s'\n", ctdb->daemon.name));
goto failed;
- }
+ }
if (chown(ctdb->daemon.name, geteuid(), getegid()) != 0 ||
chmod(ctdb->daemon.name, 0700) != 0) {
DEBUG(DEBUG_CRIT,("Unable to secure ctdb socket '%s', ctdb->daemon.name\n", ctdb->daemon.name));
goto failed;
- }
+ }
if (listen(ctdb->daemon.sd, 100) != 0) {
return -1;
}
-static void sig_child_handler(struct event_context *ev,
- struct signal_event *se, int signum, int count,
- void *dont_care,
- void *private_data)
+static void initialise_node_flags (struct ctdb_context *ctdb)
{
-// struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
- int status;
- pid_t pid = -1;
-
- while (pid != 0) {
- pid = waitpid(-1, &status, WNOHANG);
- if (pid == -1) {
- DEBUG(DEBUG_ERR, (__location__ " waitpid() returned error. errno:%d\n", errno));
- return;
- }
- if (pid > 0) {
- DEBUG(DEBUG_DEBUG, ("SIGCHLD from %d\n", (int)pid));
- }
+ if (ctdb->pnn == -1) {
+ ctdb_fatal(ctdb, "PNN is set to -1 (unknown value)");
+ }
+
+ ctdb->nodes[ctdb->pnn]->flags &= ~NODE_FLAGS_DISCONNECTED;
+
+ /* do we start out in DISABLED mode? */
+ if (ctdb->start_as_disabled != 0) {
+ DEBUG(DEBUG_NOTICE, ("This node is configured to start in DISABLED state\n"));
+ ctdb->nodes[ctdb->pnn]->flags |= NODE_FLAGS_DISABLED;
+ }
+ /* do we start out in STOPPED mode? */
+ if (ctdb->start_as_stopped != 0) {
+ DEBUG(DEBUG_NOTICE, ("This node is configured to start in STOPPED state\n"));
+ ctdb->nodes[ctdb->pnn]->flags |= NODE_FLAGS_STOPPED;
}
}
void *private_data)
{
if (status != 0) {
- ctdb_fatal(ctdb, "Failed to run setup event\n");
- return;
+ ctdb_die(ctdb, "Failed to run setup event");
}
ctdb_run_notification_script(ctdb, "setup");
0, CTDB_CONTROL_STARTUP, 0,
CTDB_CTRL_FLAG_NOREPLY,
tdb_null, NULL, NULL);
+
+ /* Start the recovery daemon */
+ if (ctdb_start_recoverd(ctdb) != 0) {
+ DEBUG(DEBUG_ALERT,("Failed to start recovery daemon\n"));
+ exit(11);
+ }
+
+ ctdb_start_periodic_events(ctdb);
+
+ ctdb_wait_for_first_recovery(ctdb);
+}
+
+static struct timeval tevent_before_wait_ts;
+static struct timeval tevent_after_wait_ts;
+
+static void ctdb_tevent_trace(enum tevent_trace_point tp,
+ void *private_data)
+{
+ struct timeval diff;
+ struct timeval now;
+ struct ctdb_context *ctdb =
+ talloc_get_type(private_data, struct ctdb_context);
+
+ if (getpid() != ctdb->ctdbd_pid) {
+ return;
+ }
+
+ now = timeval_current();
+
+ switch (tp) {
+ case TEVENT_TRACE_BEFORE_WAIT:
+ if (!timeval_is_zero(&tevent_after_wait_ts)) {
+ diff = timeval_until(&tevent_after_wait_ts, &now);
+ if (diff.tv_sec > 3) {
+ DEBUG(DEBUG_ERR,
+ ("Handling event took %ld seconds!\n",
+ (long)diff.tv_sec));
+ }
+ }
+ tevent_before_wait_ts = now;
+ break;
+
+ case TEVENT_TRACE_AFTER_WAIT:
+ if (!timeval_is_zero(&tevent_before_wait_ts)) {
+ diff = timeval_until(&tevent_before_wait_ts, &now);
+ if (diff.tv_sec > 3) {
+ DEBUG(DEBUG_CRIT,
+ ("No event for %ld seconds!\n",
+ (long)diff.tv_sec));
+ }
+ }
+ tevent_after_wait_ts = now;
+ break;
+
+ default:
+ /* Do nothing for future tevent trace points */ ;
+ }
+}
+
+static void ctdb_remove_pidfile(void)
+{
+ /* Only the main ctdbd's PID matches the SID */
+ if (ctdbd_pidfile != NULL && getsid(0) == getpid()) {
+ if (unlink(ctdbd_pidfile) == 0) {
+ DEBUG(DEBUG_NOTICE, ("Removed PID file %s\n",
+ ctdbd_pidfile));
+ } else {
+ DEBUG(DEBUG_WARNING, ("Failed to Remove PID file %s\n",
+ ctdbd_pidfile));
+ }
+ }
+}
+
+static void ctdb_create_pidfile(pid_t pid)
+{
+ if (ctdbd_pidfile != NULL) {
+ FILE *fp;
+
+ fp = fopen(ctdbd_pidfile, "w");
+ if (fp == NULL) {
+ DEBUG(DEBUG_ALERT,
+ ("Failed to open PID file %s\n", ctdbd_pidfile));
+ exit(11);
+ }
+
+ fprintf(fp, "%d\n", pid);
+ fclose(fp);
+ DEBUG(DEBUG_NOTICE, ("Created PID file %s\n", ctdbd_pidfile));
+ atexit(ctdb_remove_pidfile);
+ }
+}
+
+static void ctdb_initialise_vnn_map(struct ctdb_context *ctdb)
+{
+ int i, j, count;
+
+ /* initialize the vnn mapping table, skipping any deleted nodes */
+ ctdb->vnn_map = talloc(ctdb, struct ctdb_vnn_map);
+ CTDB_NO_MEMORY_FATAL(ctdb, ctdb->vnn_map);
+
+ count = 0;
+ for (i = 0; i < ctdb->num_nodes; i++) {
+ if ((ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) == 0) {
+ count++;
+ }
+ }
+
+ ctdb->vnn_map->generation = INVALID_GENERATION;
+ ctdb->vnn_map->size = count;
+ ctdb->vnn_map->map = talloc_array(ctdb->vnn_map, uint32_t, ctdb->vnn_map->size);
+ CTDB_NO_MEMORY_FATAL(ctdb, ctdb->vnn_map->map);
+
+ for(i=0, j=0; i < ctdb->vnn_map->size; i++) {
+ if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
+ continue;
+ }
+ ctdb->vnn_map->map[j] = i;
+ j++;
+ }
+}
+
+static void ctdb_set_my_pnn(struct ctdb_context *ctdb)
+{
+ int nodeid;
+
+ if (ctdb->address == NULL) {
+ ctdb_fatal(ctdb,
+ "Can not determine PNN - node address is not set\n");
+ }
+
+ nodeid = ctdb_ip_to_nodeid(ctdb, ctdb->address);
+ if (nodeid == -1) {
+ ctdb_fatal(ctdb,
+ "Can not determine PNN - node address not found in node list\n");
+ }
+
+ ctdb->pnn = ctdb->nodes[nodeid]->pnn;
+ DEBUG(DEBUG_NOTICE, ("PNN is %u\n", ctdb->pnn));
}
/*
start the protocol going as a daemon
*/
-int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork, bool use_syslog, const char *public_address_list)
+int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork)
{
int res, ret = -1;
- struct fd_event *fde;
- const char *domain_socket_name;
- struct signal_event *se;
-
- /* get rid of any old sockets */
- unlink(ctdb->daemon.name);
+ struct tevent_fd *fde;
/* create a unix domain stream socket to listen to */
res = ux_socket_bind(ctdb);
if (res!=0) {
- DEBUG(DEBUG_ALERT,(__location__ " Failed to open CTDB unix domain socket\n"));
+ DEBUG(DEBUG_ALERT,("Cannot continue. Exiting!\n"));
exit(10);
}
return 0;
}
- tdb_reopen_all(False);
+ tdb_reopen_all(false);
if (do_fork) {
- setsid();
+ if (setsid() == -1) {
+ ctdb_die(ctdb, "Failed to setsid()\n");
+ }
close(0);
if (open("/dev/null", O_RDONLY) != 0) {
DEBUG(DEBUG_ALERT,(__location__ " Failed to setup stdin on /dev/null\n"));
exit(11);
}
}
- block_signal(SIGPIPE);
-
- ctdbd_pid = getpid();
- ctdb->ctdbd_pid = ctdbd_pid;
+ ignore_signal(SIGPIPE);
+ ignore_signal(SIGUSR1);
+ ctdb->ctdbd_pid = getpid();
+ DEBUG(DEBUG_ERR, ("Starting CTDBD (Version %s) as PID: %u\n",
+ CTDB_VERSION_STRING, ctdb->ctdbd_pid));
+ ctdb_create_pidfile(ctdb->ctdbd_pid);
- DEBUG(DEBUG_ERR, ("Starting CTDBD as pid : %u\n", ctdbd_pid));
+ /* Make sure we log something when the daemon terminates.
+ * This must be the first exit handler to run (so the last to
+ * be registered.
+ */
+ atexit(print_exit_message);
if (ctdb->do_setsched) {
/* try to set us up as realtime */
- ctdb_set_scheduler(ctdb);
- }
-
- /* ensure the socket is deleted on exit of the daemon */
- domain_socket_name = talloc_strdup(talloc_autofree_context(), ctdb->daemon.name);
- if (domain_socket_name == NULL) {
- DEBUG(DEBUG_ALERT,(__location__ " talloc_strdup failed.\n"));
- exit(12);
+ if (!set_scheduler()) {
+ exit(1);
+ }
+ DEBUG(DEBUG_NOTICE, ("Set real-time scheduler priority\n"));
}
- ctdb->ev = event_context_init(NULL);
+ ctdb->ev = tevent_context_init(NULL);
tevent_loop_allow_nesting(ctdb->ev);
+ tevent_set_trace_callback(ctdb->ev, ctdb_tevent_trace, ctdb);
ret = ctdb_init_tevent_logging(ctdb);
if (ret != 0) {
DEBUG(DEBUG_ALERT,("Failed to initialize TEVENT logging\n"));
exit(1);
}
+ /* set up a handler to pick up sigchld */
+ if (ctdb_init_sigchld(ctdb) == NULL) {
+ DEBUG(DEBUG_CRIT,("Failed to set up signal handler for SIGCHLD\n"));
+ exit(1);
+ }
+
ctdb_set_child_logging(ctdb);
+ if (srvid_init(ctdb, &ctdb->srv) != 0) {
+ DEBUG(DEBUG_CRIT,("Failed to setup message srvid context\n"));
+ exit(1);
+ }
+
/* initialize statistics collection */
ctdb_statistics_init(ctdb);
/* force initial recovery for election */
ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
+ ctdb_set_runstate(ctdb, CTDB_RUNSTATE_INIT);
+ ret = ctdb_event_script(ctdb, CTDB_EVENT_INIT);
+ if (ret != 0) {
+ ctdb_die(ctdb, "Failed to run init event\n");
+ }
+ ctdb_run_notification_script(ctdb, "init");
+
if (strcmp(ctdb->transport, "tcp") == 0) {
- int ctdb_tcp_init(struct ctdb_context *);
ret = ctdb_tcp_init(ctdb);
}
#ifdef USE_INFINIBAND
if (strcmp(ctdb->transport, "ib") == 0) {
- int ctdb_ibw_init(struct ctdb_context *);
ret = ctdb_ibw_init(ctdb);
}
#endif
ctdb_fatal(ctdb, "transport is unavailable. can not initialize.");
}
- /* initialise the transport */
+ /* Initialise the transport. This sets the node address if it
+ * was not set via the command-line. */
if (ctdb->methods->initialise(ctdb) != 0) {
ctdb_fatal(ctdb, "transport failed to initialise");
}
- if (public_address_list) {
- ret = ctdb_set_public_addresses(ctdb, public_address_list);
+
+ ctdb_set_my_pnn(ctdb);
+
+ initialise_node_flags(ctdb);
+
+ if (ctdb->public_addresses_file) {
+ ret = ctdb_set_public_addresses(ctdb, true);
if (ret == -1) {
DEBUG(DEBUG_ALERT,("Unable to setup public address list\n"));
exit(1);
}
}
+ ctdb_initialise_vnn_map(ctdb);
/* attach to existing databases */
if (ctdb_attach_databases(ctdb) != 0) {
ctdb_fatal(ctdb, "Failed to attach to databases\n");
}
- ret = ctdb_event_script(ctdb, CTDB_EVENT_INIT);
- if (ret != 0) {
- ctdb_fatal(ctdb, "Failed to run init event\n");
- }
- ctdb_run_notification_script(ctdb, "init");
-
/* start frozen, then let the first election sort things out */
- if (ctdb_blocking_freeze(ctdb)) {
+ if (!ctdb_blocking_freeze(ctdb)) {
ctdb_fatal(ctdb, "Failed to get initial freeze\n");
}
/* now start accepting clients, only can do this once frozen */
- fde = event_add_fd(ctdb->ev, ctdb, ctdb->daemon.sd,
- EVENT_FD_READ,
- ctdb_accept_client, ctdb);
- tevent_fd_set_auto_close(fde);
-
- /* release any IPs we hold from previous runs of the daemon */
- if (ctdb->tunable.disable_ip_failover == 0) {
- ctdb_release_all_ips(ctdb);
+ fde = tevent_add_fd(ctdb->ev, ctdb, ctdb->daemon.sd, TEVENT_FD_READ,
+ ctdb_accept_client, ctdb);
+ if (fde == NULL) {
+ ctdb_fatal(ctdb, "Failed to add daemon socket to event loop");
}
+ tevent_fd_set_auto_close(fde);
- /* start the transport going */
- ctdb_start_transport(ctdb);
-
- /* set up a handler to pick up sigchld */
- se = event_add_signal(ctdb->ev, ctdb,
- SIGCHLD, 0,
- sig_child_handler,
- ctdb);
- if (se == NULL) {
- DEBUG(DEBUG_CRIT,("Failed to set up signal handler for SIGCHLD\n"));
- exit(1);
+ /* Start the transport */
+ if (ctdb->methods->start(ctdb) != 0) {
+ DEBUG(DEBUG_ALERT,("transport failed to start!\n"));
+ ctdb_fatal(ctdb, "transport failed to start");
}
+ /* Recovery daemon and timed events are started from the
+ * callback, only after the setup event completes
+ * successfully.
+ */
+ ctdb_set_runstate(ctdb, CTDB_RUNSTATE_SETUP);
ret = ctdb_event_script_callback(ctdb,
ctdb,
ctdb_setup_event_callback,
ctdb,
- false,
CTDB_EVENT_SETUP,
"%s",
"");
exit(1);
}
- if (use_syslog) {
- if (start_syslog_daemon(ctdb)) {
- DEBUG(DEBUG_CRIT, ("Failed to start syslog daemon\n"));
- exit(10);
- }
- }
+ lockdown_memory(ctdb->valgrinding);
- ctdb_lockdown_memory(ctdb);
-
/* go into a wait loop to allow other nodes to complete */
- event_loop_wait(ctdb->ev);
+ tevent_loop_wait(ctdb->ev);
DEBUG(DEBUG_CRIT,("event_loop_wait() returned. this should not happen\n"));
exit(1);
hdr->length = length;
hdr->operation = operation;
hdr->ctdb_magic = CTDB_MAGIC;
- hdr->ctdb_version = CTDB_VERSION;
+ hdr->ctdb_version = CTDB_PROTOCOL;
hdr->generation = ctdb->vnn_map->generation;
hdr->srcnode = ctdb->pnn;
struct daemon_control_state {
struct daemon_control_state *next, *prev;
struct ctdb_client *client;
- struct ctdb_req_control *c;
+ struct ctdb_req_control_old *c;
uint32_t reqid;
struct ctdb_node *node;
};
from a local client over the unix domain socket
*/
static void daemon_request_control_from_client(struct ctdb_client *client,
- struct ctdb_req_control *c)
+ struct ctdb_req_control_old *c)
{
TDB_DATA data;
int res;
TDB_DATA data;
};
-static void ctdb_local_message_trigger(struct event_context *ev, struct timed_event *te,
+static void ctdb_local_message_trigger(struct tevent_context *ev,
+ struct tevent_timer *te,
struct timeval t, void *private_data)
{
- struct ctdb_local_message *m = talloc_get_type(private_data,
- struct ctdb_local_message);
- int res;
+ struct ctdb_local_message *m = talloc_get_type(
+ private_data, struct ctdb_local_message);
- res = ctdb_dispatch_message(m->ctdb, m->srvid, m->data);
- if (res != 0) {
- DEBUG(DEBUG_ERR, (__location__ " Failed to dispatch message for srvid=%llu\n",
- (unsigned long long)m->srvid));
- }
+ srvid_dispatch(m->ctdb->srv, m->srvid, CTDB_SRVID_ALL, m->data);
talloc_free(m);
}
}
/* this needs to be done as an event to prevent recursion */
- event_add_timed(ctdb->ev, m, timeval_zero(), ctdb_local_message_trigger, m);
+ tevent_add_timer(ctdb->ev, m, timeval_zero(),
+ ctdb_local_message_trigger, m);
return 0;
}
int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t pnn,
uint64_t srvid, TDB_DATA data)
{
- struct ctdb_req_message *r;
+ struct ctdb_req_message_old *r;
int len;
if (ctdb->methods == NULL) {
return ctdb_local_message(ctdb, srvid, data);
}
- len = offsetof(struct ctdb_req_message, data) + data.dsize;
+ len = offsetof(struct ctdb_req_message_old, data) + data.dsize;
r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_MESSAGE, len,
- struct ctdb_req_message);
+ struct ctdb_req_message_old);
CTDB_NO_MEMORY(ctdb, r);
r->hdr.destnode = pnn;
int32_t ctdb_control_register_notify(struct ctdb_context *ctdb, uint32_t client_id, TDB_DATA indata)
{
struct ctdb_client_notify_register *notify = (struct ctdb_client_notify_register *)indata.dptr;
- struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
+ struct ctdb_client *client = reqid_find(ctdb->idr, client_id, struct ctdb_client);
struct ctdb_client_notify_list *nl;
DEBUG(DEBUG_INFO,("Register srvid %llu for client %d\n", (unsigned long long)notify->srvid, client_id));
int32_t ctdb_control_deregister_notify(struct ctdb_context *ctdb, uint32_t client_id, TDB_DATA indata)
{
struct ctdb_client_notify_deregister *notify = (struct ctdb_client_notify_deregister *)indata.dptr;
- struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
+ struct ctdb_client *client = reqid_find(ctdb->idr, client_id, struct ctdb_client);
struct ctdb_client_notify_list *nl;
DEBUG(DEBUG_INFO,("Deregister srvid %llu for client %d\n", (unsigned long long)notify->srvid, client_id));
return kill(pid, 0);
}
+
+int ctdb_control_getnodesfile(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata)
+{
+ struct ctdb_node_map *node_map = NULL;
+
+ CHECK_CONTROL_DATA_SIZE(0);
+
+ node_map = ctdb_read_nodes_file(ctdb, ctdb->nodes_file);
+ if (node_map == NULL) {
+ DEBUG(DEBUG_ERR, ("Failed to read nodes file\n"));
+ return -1;
+ }
+
+ outdata->dptr = (unsigned char *)node_map;
+ outdata->dsize = talloc_get_size(outdata->dptr);
+
+ return 0;
+}
+
+void ctdb_shutdown_sequence(struct ctdb_context *ctdb, int exit_code)
+{
+ if (ctdb->runstate == CTDB_RUNSTATE_SHUTDOWN) {
+ DEBUG(DEBUG_NOTICE,("Already shutting down so will not proceed.\n"));
+ return;
+ }
+
+ DEBUG(DEBUG_NOTICE,("Shutdown sequence commencing.\n"));
+ ctdb_set_runstate(ctdb, CTDB_RUNSTATE_SHUTDOWN);
+ ctdb_stop_recoverd(ctdb);
+ ctdb_stop_keepalive(ctdb);
+ ctdb_stop_monitoring(ctdb);
+ ctdb_release_all_ips(ctdb);
+ ctdb_event_script(ctdb, CTDB_EVENT_SHUTDOWN);
+ if (ctdb->methods != NULL) {
+ ctdb->methods->shutdown(ctdb);
+ }
+
+ DEBUG(DEBUG_NOTICE,("Shutdown sequence complete, exiting.\n"));
+ exit(exit_code);
+}