ctdb-daemon: make bool assignment more obvious
[samba.git] / ctdb / server / ctdbd.c
index b0b075c4f31931de8f57028bd8fda5d08a9b9470..e09611f8e50ccc317b5d83f3031a133f16b95415 100644 (file)
    along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "includes.h"
-#include "lib/tevent/tevent.h"
+#include "replace.h"
 #include "system/filesys.h"
-#include "popt.h"
 #include "system/time.h"
 #include "system/wait.h"
 #include "system/network.h"
-#include "cmdline.h"
-#include "../include/ctdb_private.h"
+
+#include <popt.h>
+#include <talloc.h>
+/* Allow use of deprecated function tevent_loop_allow_nesting() */
+#define TEVENT_DEPRECATED
+#include <tevent.h>
+
+#include "lib/util/debug.h"
+#include "lib/util/samba_util.h"
+
+#include "ctdb_private.h"
+
+#include "common/reqid.h"
+#include "common/system.h"
+#include "common/cmdline.h"
+#include "common/common.h"
+#include "common/logging.h"
 
 static struct {
        const char *nlist;
@@ -34,32 +47,30 @@ static struct {
        const char *public_address_list;
        const char *event_script_dir;
        const char *notification_script;
-       const char *logfile;
-       const char *recovery_lock_file;
+       const char *logging;
+       const char *recovery_lock;
        const char *db_dir;
        const char *db_dir_persistent;
        const char *db_dir_state;
        const char *public_interface;
-       const char *single_public_ip;
-       const char *node_ip;
        int         valgrinding;
-       int         use_syslog;
+       int         nosetsched;
        int         start_as_disabled;
        int         start_as_stopped;
        int         no_lmaster;
        int         no_recmaster;
-       int         lvs;
        int         script_log_level;
        int         no_publicipcheck;
        int         max_persistent_check_errors;
 } options = {
-       .nlist = ETCDIR "/ctdb/nodes",
+       .nlist = NULL,
+       .public_address_list = NULL,
        .transport = "tcp",
-       .event_script_dir = ETCDIR "/ctdb/events.d",
-       .logfile = LOGDIR "/log.ctdb",
-       .db_dir = VARDIR "/ctdb",
-       .db_dir_persistent = VARDIR "/ctdb/persistent",
-       .db_dir_state = VARDIR "/ctdb/state",
+       .event_script_dir = NULL,
+       .logging = "file:" LOGDIR "/log.ctdb",
+       .db_dir = CTDB_VARDIR,
+       .db_dir_persistent = CTDB_VARDIR "/persistent",
+       .db_dir_state = CTDB_VARDIR "/state",
        .script_log_level = DEBUG_ERR,
 };
 
@@ -73,13 +84,13 @@ static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t len
 {
        struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
 
-       ctdb->statistics.node_packets_recv++;
+       CTDB_INCREMENT_STAT(ctdb, node_packets_recv);
 
        /* up the counter for this source node, so we know its alive */
        if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
                /* as a special case, redirected calls don't increment the rx_cnt */
                if (hdr->operation != CTDB_REQ_CALL ||
-                   ((struct ctdb_req_call *)hdr)->hopcount == 0) {
+                   ((struct ctdb_req_call_old *)hdr)->hopcount == 0) {
                        ctdb->nodes[hdr->srcnode]->rx_cnt++;
                }
        }
@@ -87,17 +98,6 @@ static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t len
        ctdb_input_pkt(ctdb, hdr);
 }
 
-void ctdb_load_nodes_file(struct ctdb_context *ctdb)
-{
-       int ret;
-
-       ret = ctdb_set_nlist(ctdb, options.nlist);
-       if (ret == -1) {
-               DEBUG(DEBUG_ALERT,("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb)));
-               exit(1);
-       }
-}
-
 static const struct ctdb_upcalls ctdb_upcalls = {
        .recv_pkt       = ctdb_recv_pkt,
        .node_dead      = ctdb_node_dead,
@@ -120,31 +120,28 @@ int main(int argc, const char *argv[])
                { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
                { "public-addresses", 0, POPT_ARG_STRING, &options.public_address_list, 0, "public address list file", "filename" },
                { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"},
-               { "single-public-ip", 0, POPT_ARG_STRING, &options.single_public_ip, 0, "single public ip", "ip-address"},
                { "event-script-dir", 0, POPT_ARG_STRING, &options.event_script_dir, 0, "event script directory", "dirname" },
-               { "logfile", 0, POPT_ARG_STRING, &options.logfile, 0, "log file location", "filename" },
+               { "logging", 0, POPT_ARG_STRING, &options.logging, 0, "logging method to be used", NULL },
                { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "filename" },
-               { "node-ip", 0, POPT_ARG_STRING, &options.node_ip, 0, "node ip", "ip-address"},
                { "notification-script", 0, POPT_ARG_STRING, &options.notification_script, 0, "notification script", "filename" },
                { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
                { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
                { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
                { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
                { "dbdir-state", 0, POPT_ARG_STRING, &options.db_dir_state, 0, "directory for internal state tdb files", NULL },
-               { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock_file, 0, "location of recovery lock file", "filename" },
-               { "valgrinding", 0, POPT_ARG_NONE, &options.valgrinding, 0, "make valgrind more effective", NULL },
-               { "syslog", 0, POPT_ARG_NONE, &options.use_syslog, 0, "log messages to syslog", NULL },
+               { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock, 0, "recovery lock", "lock" },
+               { "pidfile", 0, POPT_ARG_STRING, &ctdbd_pidfile, 0, "location of PID file", "filename" },
+               { "valgrinding", 0, POPT_ARG_NONE, &options.valgrinding, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
+               { "nosetsched", 0, POPT_ARG_NONE, &options.nosetsched, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
                { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
                { "start-as-stopped", 0, POPT_ARG_NONE, &options.start_as_stopped, 0, "Node starts in stopped state", NULL },
                { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL },
                { "no-recmaster", 0, POPT_ARG_NONE, &options.no_recmaster, 0, "disable recmaster role on this node", NULL },
-               { "lvs", 0, POPT_ARG_NONE, &options.lvs, 0, "lvs is enabled on this node", NULL },
-               { "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, DEBUG_ERR, "log level of event script output", NULL },
+               { "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, 0, "log level of event script output", NULL },
                { "nopublicipcheck", 0, POPT_ARG_NONE, &options.no_publicipcheck, 0, "don't check we have/don't have the correct public ip addresses", NULL },
                { "max-persistent-check-errors", 0, POPT_ARG_INT,
                  &options.max_persistent_check_errors, 0,
                  "max allowed persistent check errors (default 0)", NULL },
-               { "log-ringbuf-size", 0, POPT_ARG_INT, &log_ringbuf_size, DEBUG_ERR, "Number of log messages we can store in the memory ringbuffer", NULL },
                { "sloppy-start", 0, POPT_ARG_NONE, &fast_start, 0, "Do not perform full recovery on start", NULL },
                POPT_TABLEEND
        };
@@ -152,7 +149,7 @@ int main(int argc, const char *argv[])
        const char **extra_argv;
        int extra_argc = 0;
        poptContext pc;
-       struct event_context *ev;
+       struct tevent_context *ev;
 
        pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
 
@@ -174,10 +171,10 @@ int main(int argc, const char *argv[])
 
        talloc_enable_null_tracking();
 
-       ctdb_block_signal(SIGPIPE);
-       fault_setup("ctdbd");
+       fault_setup();
 
-       ev = event_context_init(NULL);
+       ev = tevent_context_init(NULL);
+       tevent_loop_allow_nesting(ev);
 
        ctdb = ctdb_cmdline_init(ev);
 
@@ -186,32 +183,33 @@ int main(int argc, const char *argv[])
 
        script_log_level = options.script_log_level;
 
-       ret = ctdb_set_logfile(ctdb, options.logfile, options.use_syslog);
-       if (ret == -1) {
-               printf("ctdb_set_logfile to %s failed - %s\n", 
-                      options.use_syslog?"syslog":options.logfile, ctdb_errstr(ctdb));
+       if (!ctdb_logging_init(ctdb, options.logging)) {
                exit(1);
        }
 
-       DEBUG(DEBUG_NOTICE,("Starting CTDB daemon\n"));
+       DEBUG(DEBUG_NOTICE,("CTDB starting on node\n"));
+
        gettimeofday(&ctdb->ctdbd_start_time, NULL);
        gettimeofday(&ctdb->last_recovery_started, NULL);
        gettimeofday(&ctdb->last_recovery_finished, NULL);
        ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
        ctdb->recovery_master  = (uint32_t)-1;
        ctdb->upcalls          = &ctdb_upcalls;
-       ctdb->idr              = idr_init(ctdb);
-       ctdb->recovery_lock_fd = -1;
-
-       ctdb_tunables_set_defaults(ctdb);
 
+       if (options.recovery_lock == NULL) {
+               DEBUG(DEBUG_WARNING, ("Recovery lock not set\n"));
+       }
+       ctdb->recovery_lock = options.recovery_lock;
 
-       ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
-       if (ret == -1) {
-               DEBUG(DEBUG_ALERT,("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb)));
+       TALLOC_FREE(ctdb->idr);
+       ret = reqid_init(ctdb, 0, &ctdb->idr);;
+       if (ret != 0) {
+               DEBUG(DEBUG_ALERT, ("reqid_init failed (%s)\n", strerror(ret)));
                exit(1);
        }
 
+       ctdb_tunables_set_defaults(ctdb);
+
        ret = ctdb_set_transport(ctdb, options.transport);
        if (ret == -1) {
                DEBUG(DEBUG_ALERT,("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb)));
@@ -228,89 +226,65 @@ int main(int argc, const char *argv[])
        }
 
        /* set ctdbd capabilities */
-       ctdb->capabilities = 0;
-       if (options.no_lmaster == 0) {
-               ctdb->capabilities |= CTDB_CAP_LMASTER;
-       }
-       if (options.no_recmaster == 0) {
-               ctdb->capabilities |= CTDB_CAP_RECMASTER;
+       ctdb->capabilities = CTDB_CAP_DEFAULT;
+       if (options.no_lmaster != 0) {
+               ctdb->capabilities &= ~CTDB_CAP_LMASTER;
        }
-       if (options.lvs != 0) {
-               ctdb->capabilities |= CTDB_CAP_LVS;
+       if (options.no_recmaster != 0) {
+               ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
        }
 
-       /* tell ctdb what nodes are available */
-       ctdb_load_nodes_file(ctdb);
+       /* Initialise this node's PNN to the unknown value.  This will
+        * be set to the correct value by either ctdb_add_node() as
+        * part of loading the nodes file or by
+        * ctdb_tcp_listen_automatic() when the transport is
+        * initialised.  At some point we should de-optimise this and
+        * pull it out into ctdb_start_daemon() so it is done clearly
+        * and only in one place.
+        */
+       ctdb->pnn = -1;
 
-       /* if a node-ip was specified, verify that it exists in the
-          nodes file
-       */
-       if (options.node_ip != NULL) {
-               DEBUG(DEBUG_NOTICE,("IP for this node is %s\n", options.node_ip));
-               ret = ctdb_ip_to_nodeid(ctdb, options.node_ip);
-               if (ret == -1) {
-                       DEBUG(DEBUG_ALERT,("The specified node-ip:%s is not a valid node address. Exiting.\n", options.node_ip));
-                       exit(1);
-               }
-               ctdb->node_ip = options.node_ip;
-               DEBUG(DEBUG_NOTICE,("This is node %d\n", ret));
-       }
+       /* Default value for CTDB_BASE - don't override */
+       setenv("CTDB_BASE", CTDB_ETCDIR, 0);
 
-       if (options.db_dir) {
-               ret = ctdb_set_tdb_dir(ctdb, options.db_dir);
-               if (ret == -1) {
-                       DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir failed - %s\n", ctdb_errstr(ctdb)));
-                       exit(1);
-               }
-       }
-       if (options.db_dir_persistent) {
-               ret = ctdb_set_tdb_dir_persistent(ctdb, options.db_dir_persistent);
-               if (ret == -1) {
-                       DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir_persistent failed - %s\n", ctdb_errstr(ctdb)));
-                       exit(1);
-               }
-       }
-       if (options.db_dir_state) {
-               ret = ctdb_set_tdb_dir_state(ctdb, options.db_dir_state);
-               if (ret == -1) {
-                       DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir_state failed - %s\n", ctdb_errstr(ctdb)));
+       /* tell ctdb what nodes are available */
+       if (options.nlist != NULL) {
+               ctdb->nodes_file = options.nlist;
+       } else {
+               ctdb->nodes_file =
+                       talloc_asprintf(ctdb, "%s/nodes", getenv("CTDB_BASE"));
+               if (ctdb->nodes_file == NULL) {
+                       DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
                        exit(1);
                }
        }
+       ctdb_load_nodes_file(ctdb);
+
+       ctdb->db_directory = options.db_dir;
+       mkdir_p_or_die(ctdb->db_directory, 0700);
+
+       ctdb->db_directory_persistent = options.db_dir_persistent;
+       mkdir_p_or_die(ctdb->db_directory_persistent, 0700);
+
+       ctdb->db_directory_state = options.db_dir_state;
+       mkdir_p_or_die(ctdb->db_directory_state, 0700);
 
        if (options.public_interface) {
                ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
                CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
        }
 
-       if (options.single_public_ip) {
-               if (options.public_interface == NULL) {
-                       DEBUG(DEBUG_ALERT,("--single_public_ip used but --public_interface is not specified. You must specify the public interface when using single public ip. Exiting\n"));
-                       exit(10);
-               }
-
-               ret = ctdb_set_single_public_ip(ctdb, options.public_interface,
-                                               options.single_public_ip);
-               if (ret != 0) {
-                       DEBUG(DEBUG_ALERT,("Invalid --single-public-ip argument : %s . This is not a valid ip address. Exiting.\n", options.single_public_ip));
-                       exit(10);
-               }
-       }
-
-       if (options.public_address_list) {
-               ret = ctdb_set_public_addresses(ctdb, options.public_address_list);
-               if (ret == -1) {
-                       DEBUG(DEBUG_ALERT,("Unable to setup public address list\n"));
+       if (options.event_script_dir != NULL) {
+               ctdb->event_script_dir = options.event_script_dir;
+       } else {
+               ctdb->event_script_dir = talloc_asprintf(ctdb, "%s/events.d",
+                                                        getenv("CTDB_BASE"));
+               if (ctdb->event_script_dir == NULL) {
+                       DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
                        exit(1);
                }
        }
 
-       ret = ctdb_set_event_script_dir(ctdb, options.event_script_dir);
-       if (ret == -1) {
-               DEBUG(DEBUG_ALERT,("Unable to setup event script directory\n"));
-               exit(1);
-       }
-
        if (options.notification_script != NULL) {
                ret = ctdb_set_notification_script(ctdb, options.notification_script);
                if (ret == -1) {
@@ -319,9 +293,14 @@ int main(int argc, const char *argv[])
                }
        }
 
-       ctdb->valgrinding = options.valgrinding;
+       ctdb->valgrinding = (options.valgrinding == 1);
+       ctdb->do_setsched = (options.nosetsched != 1);
+       if (ctdb->valgrinding) {
+               ctdb->do_setsched = false;
+       }
 
-       ctdb->do_checkpublicip = !options.no_publicipcheck;
+       ctdb->public_addresses_file = options.public_address_list;
+       ctdb->do_checkpublicip = (options.no_publicipcheck == 0);
 
        if (options.max_persistent_check_errors < 0) {
                ctdb->max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL;
@@ -329,12 +308,6 @@ int main(int argc, const char *argv[])
                ctdb->max_persistent_check_errors = (uint64_t)options.max_persistent_check_errors;
        }
 
-       if (getenv("CTDB_BASE") == NULL) {
-               /* setup a environment variable for the event scripts to use
-                  to find the installation directory */
-               setenv("CTDB_BASE", ETCDIR "/ctdb", 1);
-       }
-
        /* start the protocol running (as a child) */
-       return ctdb_start_daemon(ctdb, interactive?False:True, options.use_syslog);
+       return ctdb_start_daemon(ctdb, interactive?false:true);
 }