Revert "from Mathieu Parent <math.parent@gmail.com>"
[metze/ctdb/wip.git] / server / ctdbd.c
1 /* 
2    standalone ctdb daemon
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program 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
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/events/events.h"
22 #include "system/filesys.h"
23 #include "popt.h"
24 #include "system/time.h"
25 #include "system/wait.h"
26 #include "system/network.h"
27 #include "cmdline.h"
28 #include "../include/ctdb_private.h"
29
30 static struct {
31         const char *nlist;
32         const char *transport;
33         const char *myaddress;
34         const char *public_address_list;
35         const char *event_script_dir;
36         const char *logfile;
37         const char *recovery_lock_file;
38         const char *db_dir;
39         const char *db_dir_persistent;
40         const char *public_interface;
41         const char *single_public_ip;
42         const char *node_ip;
43         int         no_setsched;
44         int         use_syslog;
45         int         start_as_disabled;
46         int         no_lmaster;
47         int         no_recmaster;
48         int         lvs;
49 } options = {
50         .nlist = ETCDIR "/ctdb/nodes",
51         .transport = "tcp",
52         .event_script_dir = ETCDIR "/ctdb/events.d",
53         .logfile = VARDIR "/log/log.ctdb",
54         .db_dir = VARDIR "/ctdb",
55         .db_dir_persistent = VARDIR "/ctdb/persistent",
56 };
57
58
59 /*
60   called by the transport layer when a packet comes in
61 */
62 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
63 {
64         struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
65
66         ctdb->statistics.node_packets_recv++;
67
68         /* up the counter for this source node, so we know its alive */
69         if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
70                 /* as a special case, redirected calls don't increment the rx_cnt */
71                 if (hdr->operation != CTDB_REQ_CALL ||
72                     ((struct ctdb_req_call *)hdr)->hopcount == 0) {
73                         ctdb->nodes[hdr->srcnode]->rx_cnt++;
74                 }
75         }
76
77         ctdb_input_pkt(ctdb, hdr);
78 }
79
80 void ctdb_load_nodes_file(struct ctdb_context *ctdb)
81 {
82         int ret;
83
84         ret = ctdb_set_nlist(ctdb, options.nlist);
85         if (ret == -1) {
86                 DEBUG(DEBUG_ALERT,("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb)));
87                 exit(1);
88         }
89 }
90
91 static const struct ctdb_upcalls ctdb_upcalls = {
92         .recv_pkt       = ctdb_recv_pkt,
93         .node_dead      = ctdb_node_dead,
94         .node_connected = ctdb_node_connected
95 };
96
97
98
99 /*
100   main program
101 */
102 int main(int argc, const char *argv[])
103 {
104         struct ctdb_context *ctdb;
105         int interactive = 0;
106
107         struct poptOption popt_options[] = {
108                 POPT_AUTOHELP
109                 POPT_CTDB_CMDLINE
110                 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
111                 { "public-addresses", 0, POPT_ARG_STRING, &options.public_address_list, 0, "public address list file", "filename" },
112                 { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"},
113                 { "single-public-ip", 0, POPT_ARG_STRING, &options.single_public_ip, 0, "single public ip", "ip-address"},
114                 { "event-script-dir", 0, POPT_ARG_STRING, &options.event_script_dir, 0, "event script directory", "dirname" },
115                 { "logfile", 0, POPT_ARG_STRING, &options.logfile, 0, "log file location", "filename" },
116                 { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "filename" },
117                 { "node-ip", 0, POPT_ARG_STRING, &options.node_ip, 0, "node ip", "ip-address"},
118                 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
119                 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
120                 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
121                 { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
122                 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock_file, 0, "location of recovery lock file", "filename" },
123                 { "nosetsched", 0, POPT_ARG_NONE, &options.no_setsched, 0, "disable setscheduler SCHED_FIFO call", NULL },
124                 { "syslog", 0, POPT_ARG_NONE, &options.use_syslog, 0, "log messages to syslog", NULL },
125                 { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
126                 { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL },
127                 { "no-recmaster", 0, POPT_ARG_NONE, &options.no_recmaster, 0, "disable recmaster role on this node", NULL },
128                 { "lvs", 0, POPT_ARG_NONE, &options.lvs, 0, "lvs is enabled on this node", NULL },
129                 POPT_TABLEEND
130         };
131         int opt, ret;
132         const char **extra_argv;
133         int extra_argc = 0;
134         poptContext pc;
135         struct event_context *ev;
136
137         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
138
139         while ((opt = poptGetNextOpt(pc)) != -1) {
140                 switch (opt) {
141                 default:
142                         fprintf(stderr, "Invalid option %s: %s\n", 
143                                 poptBadOption(pc, 0), poptStrerror(opt));
144                         exit(1);
145                 }
146         }
147
148         /* setup the remaining options for the main program to use */
149         extra_argv = poptGetArgs(pc);
150         if (extra_argv) {
151                 extra_argv++;
152                 while (extra_argv[extra_argc]) extra_argc++;
153         }
154
155         if (!options.recovery_lock_file) {
156                 DEBUG(DEBUG_ALERT,("You must specifiy the location of a recovery lock file with --reclock\n"));
157                 exit(1);
158         }
159
160         talloc_enable_null_tracking();
161
162         ctdb_block_signal(SIGPIPE);
163
164         ev = event_context_init(NULL);
165
166         ctdb = ctdb_cmdline_init(ev);
167
168         ctdb->start_as_disabled = options.start_as_disabled;
169
170         ret = ctdb_set_logfile(ctdb, options.logfile, options.use_syslog);
171         if (ret == -1) {
172                 printf("ctdb_set_logfile to %s failed - %s\n", 
173                        options.use_syslog?"syslog":options.logfile, ctdb_errstr(ctdb));
174                 exit(1);
175         }
176
177         DEBUG(DEBUG_NOTICE,("Starting CTDB daemon\n"));
178         gettimeofday(&ctdb->ctdbd_start_time, NULL);
179         gettimeofday(&ctdb->last_recovery_started, NULL);
180         gettimeofday(&ctdb->last_recovery_finished, NULL);
181         ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
182         ctdb->recovery_master  = (uint32_t)-1;
183         ctdb->upcalls          = &ctdb_upcalls;
184         ctdb->idr              = idr_init(ctdb);
185         ctdb->recovery_lock_fd = -1;
186
187         ctdb_tunables_set_defaults(ctdb);
188
189         ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
190         if (ret == -1) {
191                 DEBUG(DEBUG_ALERT,("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb)));
192                 exit(1);
193         }
194
195         ret = ctdb_set_transport(ctdb, options.transport);
196         if (ret == -1) {
197                 DEBUG(DEBUG_ALERT,("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb)));
198                 exit(1);
199         }
200
201         /* tell ctdb what address to listen on */
202         if (options.myaddress) {
203                 ret = ctdb_set_address(ctdb, options.myaddress);
204                 if (ret == -1) {
205                         DEBUG(DEBUG_ALERT,("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb)));
206                         exit(1);
207                 }
208         }
209
210         /* set ctdbd capabilities */
211         ctdb->capabilities = 0;
212         if (options.no_lmaster == 0) {
213                 ctdb->capabilities |= CTDB_CAP_LMASTER;
214         }
215         if (options.no_recmaster == 0) {
216                 ctdb->capabilities |= CTDB_CAP_RECMASTER;
217         }
218         if (options.lvs != 0) {
219                 ctdb->capabilities |= CTDB_CAP_LVS;
220         }
221
222         /* tell ctdb what nodes are available */
223         ctdb_load_nodes_file(ctdb);
224
225         /* if a node-ip was specified, verify that it exists in the
226            nodes file
227         */
228         if (options.node_ip != NULL) {
229                 DEBUG(DEBUG_NOTICE,("IP for this node is %s\n", options.node_ip));
230                 ret = ctdb_ip_to_nodeid(ctdb, options.node_ip);
231                 if (ret == -1) {
232                         DEBUG(DEBUG_ALERT,("The specified node-ip:%s is not a valid node address. Exiting.\n", options.node_ip));
233                         exit(1);
234                 }
235                 ctdb->node_ip = options.node_ip;
236                 DEBUG(DEBUG_NOTICE,("This is node %d\n", ret));
237         }
238
239         if (options.db_dir) {
240                 ret = ctdb_set_tdb_dir(ctdb, options.db_dir);
241                 if (ret == -1) {
242                         DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir failed - %s\n", ctdb_errstr(ctdb)));
243                         exit(1);
244                 }
245         }
246         if (options.db_dir_persistent) {
247                 ret = ctdb_set_tdb_dir_persistent(ctdb, options.db_dir_persistent);
248                 if (ret == -1) {
249                         DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir_persistent failed - %s\n", ctdb_errstr(ctdb)));
250                         exit(1);
251                 }
252         }
253
254         if (options.public_interface) {
255                 ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
256                 CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
257         }
258
259         if (options.single_public_ip) {
260                 struct ctdb_vnn *svnn;
261
262                 if (options.public_interface == NULL) {
263                         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"));
264                         exit(10);
265                 }
266
267                 svnn = talloc_zero(ctdb, struct ctdb_vnn);
268                 CTDB_NO_MEMORY(ctdb, svnn);
269
270                 ctdb->single_ip_vnn = svnn;
271                 svnn->iface = talloc_strdup(svnn, options.public_interface);
272                 CTDB_NO_MEMORY(ctdb, svnn->iface);
273
274                 if (parse_ip(options.single_public_ip, 
275                                 &svnn->public_address) == 0) {
276                         DEBUG(DEBUG_ALERT,("Invalid --single-public-ip argument : %s . This is not a valid ip address. Exiting.\n", options.single_public_ip));
277                         exit(10);
278                 }
279         }
280
281         if (options.public_address_list) {
282                 ret = ctdb_set_public_addresses(ctdb, options.public_address_list);
283                 if (ret == -1) {
284                         DEBUG(DEBUG_ALERT,("Unable to setup public address list\n"));
285                         exit(1);
286                 }
287         }
288
289         ret = ctdb_set_event_script_dir(ctdb, options.event_script_dir);
290         if (ret == -1) {
291                 DEBUG(DEBUG_ALERT,("Unable to setup event script directory\n"));
292                 exit(1);
293         }
294
295         ctdb->do_setsched = !options.no_setsched;
296
297         /* setup a environment variable for the event scripts to use to find the
298            installation directory */
299         setenv("CTDB_BASE", ETCDIR "/ctdb", 1);
300
301         /* start the protocol running (as a child) */
302         return ctdb_start_daemon(ctdb, interactive?False:True);
303 }