Merge remote branch 'amitay/tevent-sync'
[sfrench/samba-autobuild/.git] / ctdb / 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 "system/filesys.h"
22 #include "popt.h"
23 #include "system/time.h"
24 #include "system/wait.h"
25 #include "system/network.h"
26 #include "cmdline.h"
27 #include "../include/ctdb_private.h"
28
29 static struct {
30         const char *nlist;
31         const char *transport;
32         const char *myaddress;
33         const char *public_address_list;
34         const char *event_script_dir;
35         const char *notification_script;
36         const char *logfile;
37         const char *recovery_lock_file;
38         const char *db_dir;
39         const char *db_dir_persistent;
40         const char *db_dir_state;
41         const char *public_interface;
42         const char *single_public_ip;
43         const char *node_ip;
44         int         valgrinding;
45         int         nosetsched;
46         int         use_syslog;
47         int         start_as_disabled;
48         int         start_as_stopped;
49         int         no_lmaster;
50         int         no_recmaster;
51         int         lvs;
52         int         script_log_level;
53         int         no_publicipcheck;
54         int         max_persistent_check_errors;
55 } options = {
56         .nlist = ETCDIR "/ctdb/nodes",
57         .transport = "tcp",
58         .event_script_dir = ETCDIR "/ctdb/events.d",
59         .logfile = LOGDIR "/log.ctdb",
60         .db_dir = VARDIR "/ctdb",
61         .db_dir_persistent = VARDIR "/ctdb/persistent",
62         .db_dir_state = VARDIR "/ctdb/state",
63         .script_log_level = DEBUG_ERR,
64 };
65
66 int script_log_level;
67 bool fast_start;
68
69 /*
70   called by the transport layer when a packet comes in
71 */
72 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
73 {
74         struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
75
76         CTDB_INCREMENT_STAT(ctdb, node_packets_recv);
77
78         /* up the counter for this source node, so we know its alive */
79         if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
80                 /* as a special case, redirected calls don't increment the rx_cnt */
81                 if (hdr->operation != CTDB_REQ_CALL ||
82                     ((struct ctdb_req_call *)hdr)->hopcount == 0) {
83                         ctdb->nodes[hdr->srcnode]->rx_cnt++;
84                 }
85         }
86
87         ctdb_input_pkt(ctdb, hdr);
88 }
89
90 void ctdb_load_nodes_file(struct ctdb_context *ctdb)
91 {
92         int ret;
93
94         ret = ctdb_set_nlist(ctdb, options.nlist);
95         if (ret == -1) {
96                 DEBUG(DEBUG_ALERT,("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb)));
97                 exit(1);
98         }
99 }
100
101 static const struct ctdb_upcalls ctdb_upcalls = {
102         .recv_pkt       = ctdb_recv_pkt,
103         .node_dead      = ctdb_node_dead,
104         .node_connected = ctdb_node_connected
105 };
106
107
108
109 /*
110   main program
111 */
112 int main(int argc, const char *argv[])
113 {
114         struct ctdb_context *ctdb;
115         int interactive = 0;
116
117         struct poptOption popt_options[] = {
118                 POPT_AUTOHELP
119                 POPT_CTDB_CMDLINE
120                 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
121                 { "public-addresses", 0, POPT_ARG_STRING, &options.public_address_list, 0, "public address list file", "filename" },
122                 { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"},
123                 { "single-public-ip", 0, POPT_ARG_STRING, &options.single_public_ip, 0, "single public ip", "ip-address"},
124                 { "event-script-dir", 0, POPT_ARG_STRING, &options.event_script_dir, 0, "event script directory", "dirname" },
125                 { "logfile", 0, POPT_ARG_STRING, &options.logfile, 0, "log file location", "filename" },
126                 { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "filename" },
127                 { "node-ip", 0, POPT_ARG_STRING, &options.node_ip, 0, "node ip", "ip-address"},
128                 { "notification-script", 0, POPT_ARG_STRING, &options.notification_script, 0, "notification script", "filename" },
129                 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
130                 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
131                 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
132                 { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
133                 { "dbdir-state", 0, POPT_ARG_STRING, &options.db_dir_state, 0, "directory for internal state tdb files", NULL },
134                 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock_file, 0, "location of recovery lock file", "filename" },
135                 { "valgrinding", 0, POPT_ARG_NONE, &options.valgrinding, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
136                 { "nosetsched", 0, POPT_ARG_NONE, &options.nosetsched, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
137                 { "syslog", 0, POPT_ARG_NONE, &options.use_syslog, 0, "log messages to syslog", NULL },
138                 { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
139                 { "start-as-stopped", 0, POPT_ARG_NONE, &options.start_as_stopped, 0, "Node starts in stopped state", NULL },
140                 { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL },
141                 { "no-recmaster", 0, POPT_ARG_NONE, &options.no_recmaster, 0, "disable recmaster role on this node", NULL },
142                 { "lvs", 0, POPT_ARG_NONE, &options.lvs, 0, "lvs is enabled on this node", NULL },
143                 { "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, DEBUG_ERR, "log level of event script output", NULL },
144                 { "nopublicipcheck", 0, POPT_ARG_NONE, &options.no_publicipcheck, 0, "don't check we have/don't have the correct public ip addresses", NULL },
145                 { "max-persistent-check-errors", 0, POPT_ARG_INT,
146                   &options.max_persistent_check_errors, 0,
147                   "max allowed persistent check errors (default 0)", NULL },
148                 { "log-ringbuf-size", 0, POPT_ARG_INT, &log_ringbuf_size, DEBUG_ERR, "Number of log messages we can store in the memory ringbuffer", NULL },
149                 { "sloppy-start", 0, POPT_ARG_NONE, &fast_start, 0, "Do not perform full recovery on start", NULL },
150                 POPT_TABLEEND
151         };
152         int opt, ret;
153         const char **extra_argv;
154         int extra_argc = 0;
155         poptContext pc;
156         struct event_context *ev;
157
158         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
159
160         while ((opt = poptGetNextOpt(pc)) != -1) {
161                 switch (opt) {
162                 default:
163                         fprintf(stderr, "Invalid option %s: %s\n", 
164                                 poptBadOption(pc, 0), poptStrerror(opt));
165                         exit(1);
166                 }
167         }
168
169         /* setup the remaining options for the main program to use */
170         extra_argv = poptGetArgs(pc);
171         if (extra_argv) {
172                 extra_argv++;
173                 while (extra_argv[extra_argc]) extra_argc++;
174         }
175
176         talloc_enable_null_tracking();
177
178         ctdb_block_signal(SIGPIPE);
179         fault_setup("ctdbd");
180
181         ev = event_context_init(NULL);
182         tevent_loop_allow_nesting(ev);
183
184         ctdb = ctdb_cmdline_init(ev);
185
186         ctdb->start_as_disabled = options.start_as_disabled;
187         ctdb->start_as_stopped  = options.start_as_stopped;
188
189         script_log_level = options.script_log_level;
190
191         ret = ctdb_set_logfile(ctdb, options.logfile, options.use_syslog);
192         if (ret == -1) {
193                 printf("ctdb_set_logfile to %s failed - %s\n", 
194                        options.use_syslog?"syslog":options.logfile, ctdb_errstr(ctdb));
195                 exit(1);
196         }
197
198         DEBUG(DEBUG_NOTICE,("CTDB starting on node\n"));
199
200         gettimeofday(&ctdb->ctdbd_start_time, NULL);
201         gettimeofday(&ctdb->last_recovery_started, NULL);
202         gettimeofday(&ctdb->last_recovery_finished, NULL);
203         ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
204         ctdb->recovery_master  = (uint32_t)-1;
205         ctdb->upcalls          = &ctdb_upcalls;
206         ctdb->idr              = idr_init(ctdb);
207         ctdb->recovery_lock_fd = -1;
208
209         ctdb_tunables_set_defaults(ctdb);
210
211         ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
212         if (ret == -1) {
213                 DEBUG(DEBUG_ALERT,("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb)));
214                 exit(1);
215         }
216
217         ret = ctdb_set_transport(ctdb, options.transport);
218         if (ret == -1) {
219                 DEBUG(DEBUG_ALERT,("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb)));
220                 exit(1);
221         }
222
223         /* tell ctdb what address to listen on */
224         if (options.myaddress) {
225                 ret = ctdb_set_address(ctdb, options.myaddress);
226                 if (ret == -1) {
227                         DEBUG(DEBUG_ALERT,("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb)));
228                         exit(1);
229                 }
230         }
231
232         /* set ctdbd capabilities */
233         ctdb->capabilities = 0;
234         if (options.no_lmaster == 0) {
235                 ctdb->capabilities |= CTDB_CAP_LMASTER;
236         }
237         if (options.no_recmaster == 0) {
238                 ctdb->capabilities |= CTDB_CAP_RECMASTER;
239         }
240         if (options.lvs != 0) {
241                 ctdb->capabilities |= CTDB_CAP_LVS;
242         }
243
244         /* tell ctdb what nodes are available */
245         ctdb_load_nodes_file(ctdb);
246
247         /* if a node-ip was specified, verify that it exists in the
248            nodes file
249         */
250         if (options.node_ip != NULL) {
251                 DEBUG(DEBUG_NOTICE,("IP for this node is %s\n", options.node_ip));
252                 ret = ctdb_ip_to_nodeid(ctdb, options.node_ip);
253                 if (ret == -1) {
254                         DEBUG(DEBUG_ALERT,("The specified node-ip:%s is not a valid node address. Exiting.\n", options.node_ip));
255                         exit(1);
256                 }
257                 ctdb->node_ip = options.node_ip;
258                 DEBUG(DEBUG_NOTICE,("This is node %d\n", ret));
259         }
260
261         if (options.db_dir) {
262                 ret = ctdb_set_tdb_dir(ctdb, options.db_dir);
263                 if (ret == -1) {
264                         DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir failed - %s\n", ctdb_errstr(ctdb)));
265                         exit(1);
266                 }
267         }
268         if (options.db_dir_persistent) {
269                 ret = ctdb_set_tdb_dir_persistent(ctdb, options.db_dir_persistent);
270                 if (ret == -1) {
271                         DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir_persistent failed - %s\n", ctdb_errstr(ctdb)));
272                         exit(1);
273                 }
274         }
275         if (options.db_dir_state) {
276                 ret = ctdb_set_tdb_dir_state(ctdb, options.db_dir_state);
277                 if (ret == -1) {
278                         DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir_state failed - %s\n", ctdb_errstr(ctdb)));
279                         exit(1);
280                 }
281         }
282
283         if (options.public_interface) {
284                 ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
285                 CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
286         }
287
288         if (options.single_public_ip) {
289                 if (options.public_interface == NULL) {
290                         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"));
291                         exit(10);
292                 }
293
294                 ret = ctdb_set_single_public_ip(ctdb, options.public_interface,
295                                                 options.single_public_ip);
296                 if (ret != 0) {
297                         DEBUG(DEBUG_ALERT,("Invalid --single-public-ip argument : %s . This is not a valid ip address. Exiting.\n", options.single_public_ip));
298                         exit(10);
299                 }
300         }
301
302         ret = ctdb_set_event_script_dir(ctdb, options.event_script_dir);
303         if (ret == -1) {
304                 DEBUG(DEBUG_ALERT,("Unable to setup event script directory\n"));
305                 exit(1);
306         }
307
308         if (options.notification_script != NULL) {
309                 ret = ctdb_set_notification_script(ctdb, options.notification_script);
310                 if (ret == -1) {
311                         DEBUG(DEBUG_ALERT,("Unable to setup notification script\n"));
312                         exit(1);
313                 }
314         }
315
316         ctdb->valgrinding = options.valgrinding;
317         if (options.valgrinding || options.nosetsched) {
318                 ctdb->do_setsched = 0;
319         } else {
320                 ctdb->do_setsched = 1;
321         }
322
323         ctdb->do_checkpublicip = !options.no_publicipcheck;
324
325         if (options.max_persistent_check_errors < 0) {
326                 ctdb->max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL;
327         } else {
328                 ctdb->max_persistent_check_errors = (uint64_t)options.max_persistent_check_errors;
329         }
330
331         if (getenv("CTDB_BASE") == NULL) {
332                 /* setup a environment variable for the event scripts to use
333                    to find the installation directory */
334                 setenv("CTDB_BASE", ETCDIR "/ctdb", 1);
335         }
336
337         /* start the protocol running (as a child) */
338         return ctdb_start_daemon(ctdb, interactive?False:True, options.use_syslog, options.public_address_list);
339 }