ctdb-daemon: Use a local variable instead of repeating getenv()
[samba.git] / ctdb / server / ctdbd.c
index 5fc1db6318d99d9df1a6d99c7616c3b5d93e2528..4a3c5f65b305d66e1ca0e0f444f35e8c00ec507a 100644 (file)
 
 #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;
+       const char *debuglevel;
        const char *transport;
        const char *myaddress;
-       const char *public_address_list;
-       const char *event_script_dir;
        const char *notification_script;
        const char *logging;
-       const char *recovery_lock_file;
+       const char *recovery_lock;
        const char *db_dir;
        const char *db_dir_persistent;
        const char *db_dir_state;
-       const char *public_interface;
        int         valgrinding;
        int         nosetsched;
        int         start_as_disabled;
@@ -62,11 +58,10 @@ static struct {
        int         script_log_level;
        int         no_publicipcheck;
        int         max_persistent_check_errors;
+       int         torture;
 } options = {
-       .nlist = NULL,
-       .public_address_list = NULL,
+       .debuglevel = "NOTICE",
        .transport = "tcp",
-       .event_script_dir = NULL,
        .logging = "file:" LOGDIR "/log.ctdb",
        .db_dir = CTDB_VARDIR,
        .db_dir_persistent = CTDB_VARDIR "/persistent",
@@ -113,24 +108,20 @@ int main(int argc, const char *argv[])
 {
        struct ctdb_context *ctdb;
        int interactive = 0;
+       const char *ctdb_socket;
 
        struct poptOption popt_options[] = {
                POPT_AUTOHELP
-               POPT_CTDB_CMDLINE
+               { "debug", 'd', POPT_ARG_STRING, &options.debuglevel, 0, "debug level", NULL },
                { "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"},
-               { "event-script-dir", 0, POPT_ARG_STRING, &options.event_script_dir, 0, "event script directory", "dirname" },
                { "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" },
                { "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" },
-               { "pidfile", 0, POPT_ARG_STRING, &ctdbd_pidfile, 0, "location of PID file", "filename" },
+               { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock, 0, "recovery lock", "lock" },
                { "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 },
@@ -143,13 +134,26 @@ int main(int argc, const char *argv[])
                  &options.max_persistent_check_errors, 0,
                  "max allowed persistent check errors (default 0)", NULL },
                { "sloppy-start", 0, POPT_ARG_NONE, &fast_start, 0, "Do not perform full recovery on start", NULL },
+               { "torture", 0, POPT_ARG_NONE, &options.torture, 0, "enable nastiness in library", NULL },
                POPT_TABLEEND
        };
        int opt, ret;
        const char **extra_argv;
-       int extra_argc = 0;
        poptContext pc;
        struct tevent_context *ev;
+       const char *ctdb_base;
+
+       /* Environment variable overrides default */
+       ctdbd_pidfile = getenv("CTDB_PIDFILE");
+       if (ctdbd_pidfile == NULL) {
+               ctdbd_pidfile = CTDB_RUNDIR "/ctdbd.pid";
+       }
+
+       /* Environment variable overrides default */
+       ctdb_socket = getenv("CTDB_SOCKET");
+       if (ctdb_socket == NULL) {
+               ctdb_socket = CTDB_SOCKET;
+       }
 
        pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
 
@@ -162,11 +166,14 @@ int main(int argc, const char *argv[])
                }
        }
 
-       /* setup the remaining options for the main program to use */
+       /* If there are extra arguments then exit with usage message */
        extra_argv = poptGetArgs(pc);
        if (extra_argv) {
                extra_argv++;
-               while (extra_argv[extra_argc]) extra_argc++;
+               if (extra_argv[0])  {
+                       poptPrintHelp(pc, stdout, 0);
+                       exit(1);
+               }
        }
 
        talloc_enable_null_tracking();
@@ -174,18 +181,44 @@ int main(int argc, const char *argv[])
        fault_setup();
 
        ev = tevent_context_init(NULL);
+       if (ev == NULL) {
+               fprintf(stderr, "tevent_context_init() failed\n");
+               exit(1);
+       }
        tevent_loop_allow_nesting(ev);
 
-       ctdb = ctdb_cmdline_init(ev);
+       ctdb = ctdb_init(ev);
+       if (ctdb == NULL) {
+               fprintf(stderr, "Failed to init ctdb\n");
+               exit(1);
+       }
 
-       ctdb->start_as_disabled = options.start_as_disabled;
-       ctdb->start_as_stopped  = options.start_as_stopped;
+       if (options.torture == 1) {
+               ctdb_set_flags(ctdb, CTDB_FLAG_TORTURE);
+       }
 
-       script_log_level = options.script_log_level;
+       /* Log to stderr when running as interactive */
+       if (interactive) {
+               options.logging = "file:";
+       }
 
-       if (!ctdb_logging_init(ctdb, options.logging)) {
+       /* Initialize logging and set the debug level */
+       if (!ctdb_logging_init(ctdb, options.logging, options.debuglevel)) {
                exit(1);
        }
+       setenv("CTDB_LOGGING", options.logging, 1);
+       setenv("CTDB_DEBUGLEVEL", debug_level_to_string(DEBUGLEVEL), 1);
+
+       ret = ctdb_set_socketname(ctdb, ctdb_socket);
+       if (ret == -1) {
+               DEBUG(DEBUG_ERR, ("ctdb_set_socketname() failed\n"));
+               exit(1);
+       }
+
+       ctdb->start_as_disabled = options.start_as_disabled;
+       ctdb->start_as_stopped  = options.start_as_stopped;
+
+       script_log_level = options.script_log_level;
 
        DEBUG(DEBUG_NOTICE,("CTDB starting on node\n"));
 
@@ -195,26 +228,25 @@ int main(int argc, const char *argv[])
        ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
        ctdb->recovery_master  = (uint32_t)-1;
        ctdb->upcalls          = &ctdb_upcalls;
-       ctdb->recovery_lock_fd = -1;
+
+       if (options.recovery_lock == NULL) {
+               DEBUG(DEBUG_WARNING, ("Recovery lock not set\n"));
+       }
+       ctdb->recovery_lock = options.recovery_lock;
 
        TALLOC_FREE(ctdb->idr);
        ret = reqid_init(ctdb, 0, &ctdb->idr);;
        if (ret != 0) {
-               DEBUG(DEBUG_ALERT, ("reqid_init failed (%s)\n", strerror(ret)));
+               DEBUG(DEBUG_ERR, ("reqid_init failed (%s)\n", strerror(ret)));
                exit(1);
        }
 
        ctdb_tunables_set_defaults(ctdb);
 
-       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)));
-               exit(1);
-       }
-
        ret = ctdb_set_transport(ctdb, options.transport);
        if (ret == -1) {
-               DEBUG(DEBUG_ALERT,("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb)));
+               DEBUG(DEBUG_ERR,("ctdb_set_transport failed - %s\n",
+                                ctdb_errstr(ctdb)));
                exit(1);
        }
 
@@ -222,7 +254,8 @@ int main(int argc, const char *argv[])
        if (options.myaddress) {
                ret = ctdb_set_address(ctdb, options.myaddress);
                if (ret == -1) {
-                       DEBUG(DEBUG_ALERT,("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb)));
+                       DEBUG(DEBUG_ERR,("ctdb_set_address failed - %s\n",
+                                        ctdb_errstr(ctdb)));
                        exit(1);
                }
        }
@@ -248,17 +281,17 @@ int main(int argc, const char *argv[])
 
        /* Default value for CTDB_BASE - don't override */
        setenv("CTDB_BASE", CTDB_ETCDIR, 0);
+       ctdb_base = getenv("CTDB_BASE");
+       if (ctdb_base == NULL) {
+               D_ERR("CTDB_BASE not set\n");
+               exit(1);
+       }
 
        /* 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->nodes_file = talloc_asprintf(ctdb, "%s/nodes", ctdb_base);
+       if (ctdb->nodes_file == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
+               exit(1);
        }
        ctdb_load_nodes_file(ctdb);
 
@@ -271,39 +304,30 @@ int main(int argc, const char *argv[])
        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.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);
-               }
+       ctdb->event_script_dir = talloc_asprintf(ctdb,
+                                                "%s/events.d",
+                                                ctdb_base);
+       if (ctdb->event_script_dir == NULL) {
+               DBG_ERR("Out of memory\n");
+               exit(1);
        }
 
        if (options.notification_script != NULL) {
-               ret = ctdb_set_notification_script(ctdb, options.notification_script);
-               if (ret == -1) {
-                       DEBUG(DEBUG_ALERT,("Unable to setup notification script\n"));
+               ctdb->notification_script = talloc_strdup(
+                               ctdb, options.notification_script);
+               if (ctdb->notification_script == NULL) {
+                       D_ERR("Unable to set notification script\n");
                        exit(1);
                }
        }
 
-       ctdb->valgrinding = options.valgrinding;
-       if (options.valgrinding || options.nosetsched) {
-               ctdb->do_setsched = 0;
-       } else {
-               ctdb->do_setsched = 1;
+       ctdb->valgrinding = (options.valgrinding == 1);
+       ctdb->do_setsched = (options.nosetsched != 1);
+       if (ctdb->valgrinding) {
+               ctdb->do_setsched = false;
        }
 
-       ctdb->public_addresses_file = options.public_address_list;
-       ctdb->do_checkpublicip = !options.no_publicipcheck;
+       ctdb->do_checkpublicip = (options.no_publicipcheck == 0);
 
        if (options.max_persistent_check_errors < 0) {
                ctdb->max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL;