clean out some more cruft
[vlendec/samba-autobuild/.git] / ctdb / direct / ctdbd.c
index f52a282b2bae6becb7028618d294f031bcc76508..a2092a697b2ed83d5a93ff0c69eaaa361a32e9d9 100644 (file)
@@ -3,19 +3,19 @@
 
    Copyright (C) Andrew Tridgell  2006
 
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
-
-   This library is distributed in the hope that it will be useful,
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
 #include "includes.h"
@@ -23,6 +23,8 @@
 #include "system/filesys.h"
 #include "popt.h"
 #include "system/wait.h"
+#include "cmdline.h"
+#include "../include/ctdb_private.h"
 
 static void block_signal(int signum)
 {
@@ -36,6 +38,25 @@ static void block_signal(int signum)
        sigaction(signum, &act, NULL);
 }
 
+static struct {
+       const char *nlist;
+       const char *transport;
+       const char *myaddress;
+       const char *public_address_list;
+       const char *public_interface;
+       const char *event_script;
+       const char *logfile;
+       const char *recovery_lock_file;
+       const char *db_dir;
+} options = {
+       .nlist = ETCDIR "/ctdb/nodes",
+       .transport = "tcp",
+       .event_script = ETCDIR "/ctdb/events",
+       .logfile = VARDIR "/log/log.ctdb",
+       .db_dir = VARDIR "/ctdb",
+};
+
+
 
 /*
   main program
@@ -43,25 +64,26 @@ static void block_signal(int signum)
 int main(int argc, const char *argv[])
 {
        struct ctdb_context *ctdb;
-       const char *nlist = NULL;
-       const char *transport = "tcp";
-       const char *myaddress = NULL;
-       int self_connect=0;
-       int daemon_mode=0;
+       int interactive = 0;
 
        struct poptOption popt_options[] = {
                POPT_AUTOHELP
-               { "nlist", 0, POPT_ARG_STRING, &nlist, 0, "node list file", "filename" },
-               { "listen", 0, POPT_ARG_STRING, &myaddress, 0, "address to listen on", "address" },
-               { "transport", 0, POPT_ARG_STRING, &transport, 0, "protocol transport", NULL },
-               { "self-connect", 0, POPT_ARG_NONE, &self_connect, 0, "enable self connect", "boolean" },
-               { "daemon", 0, POPT_ARG_NONE, &daemon_mode, 0, "spawn a ctdb daemon", "boolean" },
+               POPT_CTDB_CMDLINE
+               { "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", 0, POPT_ARG_STRING, &options.event_script, 0, "event script", "filename" },
+               { "logfile", 0, POPT_ARG_STRING, &options.logfile, 0, "log file location", "filename" },
+               { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "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 },
+               { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock_file, 0, "location of recovery lock file", "filename" },
                POPT_TABLEEND
        };
-       int opt;
+       int opt, ret;
        const char **extra_argv;
        int extra_argc = 0;
-       int ret;
        poptContext pc;
        struct event_context *ev;
 
@@ -83,8 +105,8 @@ int main(int argc, const char *argv[])
                while (extra_argv[extra_argc]) extra_argc++;
        }
 
-       if (nlist == NULL || myaddress == NULL) {
-               printf("You must provide a node list with --nlist and an address with --listen\n");
+       if (!options.recovery_lock_file) {
+               DEBUG(0,("You must specifiy the location of a recovery lock file with --reclock\n"));
                exit(1);
        }
 
@@ -92,46 +114,78 @@ int main(int argc, const char *argv[])
 
        ev = event_context_init(NULL);
 
-       /* initialise ctdb */
-       ctdb = ctdb_init(ev);
-       if (ctdb == NULL) {
-               printf("Failed to init ctdb\n");
+       ctdb = ctdb_cmdline_init(ev);
+
+       ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
+       if (ret == -1) {
+               printf("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb));
                exit(1);
        }
 
-       if (self_connect) {
-               ctdb_set_flags(ctdb, CTDB_FLAG_SELF_CONNECT);
+       ret = ctdb_set_transport(ctdb, options.transport);
+       if (ret == -1) {
+               printf("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb));
+               exit(1);
        }
-       if (daemon_mode) {
-               ctdb_set_flags(ctdb, CTDB_FLAG_DAEMON_MODE);
+
+       /* tell ctdb what address to listen on */
+       if (options.myaddress) {
+               ret = ctdb_set_address(ctdb, options.myaddress);
+               if (ret == -1) {
+                       printf("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb));
+                       exit(1);
+               }
        }
 
-       ret = ctdb_set_transport(ctdb, transport);
+       /* tell ctdb what nodes are available */
+       ret = ctdb_set_nlist(ctdb, options.nlist);
        if (ret == -1) {
-               printf("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb));
+               printf("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb));
                exit(1);
        }
 
-       /* tell ctdb what address to listen on */
-       ret = ctdb_set_address(ctdb, myaddress);
+       if (options.db_dir) {
+               ret = ctdb_set_tdb_dir(ctdb, options.db_dir);
+               if (ret == -1) {
+                       printf("ctdb_set_tdb_dir failed - %s\n", ctdb_errstr(ctdb));
+                       exit(1);
+               }
+       }
+
+       ret = ctdb_set_logfile(ctdb, options.logfile);
        if (ret == -1) {
-               printf("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb));
+               printf("ctdb_set_logfile to %s failed - %s\n", options.logfile, ctdb_errstr(ctdb));
                exit(1);
        }
 
-       /* tell ctdb what nodes are available */
-       ret = ctdb_set_nlist(ctdb, nlist);
+       if (options.public_interface) {
+               ctdb->takeover.interface = talloc_strdup(ctdb, options.public_interface);
+               CTDB_NO_MEMORY(ctdb, ctdb->takeover.interface);
+       }
+
+       if (options.public_address_list) {
+               ret = ctdb_set_public_addresses(ctdb, options.public_address_list);
+               if (ret == -1) {
+                       printf("Unable to setup public address list\n");
+                       exit(1);
+               }
+               ctdb->takeover.enabled = true;
+       }
+
+       ret = ctdb_set_event_script(ctdb, options.event_script);
        if (ret == -1) {
-               printf("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb));
+               printf("Unable to setup event script\n");
                exit(1);
        }
 
-       /* start the protocol running */
-       ret = ctdb_start(ctdb);
+       /* useful default logfile */
+       if (ctdb->logfile == NULL) {
+               char *name = talloc_asprintf(ctdb, "%s/log.ctdb.vnn%u", 
+                                            VARDIR, ctdb->vnn);
+               ctdb_set_logfile(ctdb, name);
+               talloc_free(name);
+       }
 
-       event_loop_wait(ev);
-       
-       /* shut it down */
-       talloc_free(ev);
-       return 0;
+       /* start the protocol running (as a child) */
+       return ctdb_start_daemon(ctdb, interactive?False:True);
 }