merge from ronnie
[vlendec/samba-autobuild/.git] / ctdb / direct / ctdbd.c
1 /* 
2    standalone ctdb daemon
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2 of the License, or (at your option) any later version.
10
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "includes.h"
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
24 #include "popt.h"
25
26
27 /*
28   main program
29 */
30 int main(int argc, const char *argv[])
31 {
32         struct ctdb_context *ctdb;
33         const char *nlist = NULL;
34         const char *transport = "tcp";
35         const char *myaddress = NULL;
36         int self_connect=0;
37         int daemon_mode=0;
38
39         struct poptOption popt_options[] = {
40                 POPT_AUTOHELP
41                 { "nlist", 0, POPT_ARG_STRING, &nlist, 0, "node list file", "filename" },
42                 { "listen", 0, POPT_ARG_STRING, &myaddress, 0, "address to listen on", "address" },
43                 { "transport", 0, POPT_ARG_STRING, &transport, 0, "protocol transport", NULL },
44                 { "self-connect", 0, POPT_ARG_NONE, &self_connect, 0, "enable self connect", "boolean" },
45                 { "daemon", 0, POPT_ARG_NONE, &daemon_mode, 0, "spawn a ctdb daemon", "boolean" },
46                 POPT_TABLEEND
47         };
48         int opt;
49         const char **extra_argv;
50         int extra_argc = 0;
51         int ret;
52         poptContext pc;
53         struct event_context *ev;
54
55         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
56
57         while ((opt = poptGetNextOpt(pc)) != -1) {
58                 switch (opt) {
59                 default:
60                         fprintf(stderr, "Invalid option %s: %s\n", 
61                                 poptBadOption(pc, 0), poptStrerror(opt));
62                         exit(1);
63                 }
64         }
65
66         /* setup the remaining options for the main program to use */
67         extra_argv = poptGetArgs(pc);
68         if (extra_argv) {
69                 extra_argv++;
70                 while (extra_argv[extra_argc]) extra_argc++;
71         }
72
73         if (nlist == NULL || myaddress == NULL) {
74                 printf("You must provide a node list with --nlist and an address with --listen\n");
75                 exit(1);
76         }
77
78         ev = event_context_init(NULL);
79
80         /* initialise ctdb */
81         ctdb = ctdb_init(ev);
82         if (ctdb == NULL) {
83                 printf("Failed to init ctdb\n");
84                 exit(1);
85         }
86
87         if (self_connect) {
88                 ctdb_set_flags(ctdb, CTDB_FLAG_SELF_CONNECT);
89         }
90         if (daemon_mode) {
91                 ctdb_set_flags(ctdb, CTDB_FLAG_DAEMON_MODE);
92         }
93
94         ret = ctdb_set_transport(ctdb, transport);
95         if (ret == -1) {
96                 printf("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb));
97                 exit(1);
98         }
99
100         /* tell ctdb what address to listen on */
101         ret = ctdb_set_address(ctdb, myaddress);
102         if (ret == -1) {
103                 printf("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb));
104                 exit(1);
105         }
106
107         /* tell ctdb what nodes are available */
108         ret = ctdb_set_nlist(ctdb, nlist);
109         if (ret == -1) {
110                 printf("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb));
111                 exit(1);
112         }
113
114         /* start the protocol running */
115         ret = ctdb_start(ctdb);
116
117         while (1) {
118                 event_loop_once(ev);
119         }
120        
121         /* shut it down */
122         talloc_free(ctdb);
123         return 0;
124 }