f9e83f8c3f8d826a21306ef887b0df24ff36e14f
[martins/ctdb.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 "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         int         valgrinding;
44         int         nosetsched;
45         int         use_syslog;
46         int         start_as_disabled;
47         int         start_as_stopped;
48         int         no_lmaster;
49         int         no_recmaster;
50         int         lvs;
51         int         script_log_level;
52         int         no_publicipcheck;
53         int         max_persistent_check_errors;
54 } options = {
55         .nlist = NULL,
56         .public_address_list = NULL,
57         .transport = "tcp",
58         .event_script_dir = NULL,
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 static const struct ctdb_upcalls ctdb_upcalls = {
91         .recv_pkt       = ctdb_recv_pkt,
92         .node_dead      = ctdb_node_dead,
93         .node_connected = ctdb_node_connected
94 };
95
96
97
98 /*
99   main program
100 */
101 int main(int argc, const char *argv[])
102 {
103         struct ctdb_context *ctdb;
104         int interactive = 0;
105
106         struct poptOption popt_options[] = {
107                 POPT_AUTOHELP
108                 POPT_CTDB_CMDLINE
109                 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
110                 { "public-addresses", 0, POPT_ARG_STRING, &options.public_address_list, 0, "public address list file", "filename" },
111                 { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"},
112                 { "single-public-ip", 0, POPT_ARG_STRING, &options.single_public_ip, 0, "single public ip", "ip-address"},
113                 { "event-script-dir", 0, POPT_ARG_STRING, &options.event_script_dir, 0, "event script directory", "dirname" },
114                 { "logfile", 0, POPT_ARG_STRING, &options.logfile, 0, "log file location", "filename" },
115                 { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "filename" },
116                 { "notification-script", 0, POPT_ARG_STRING, &options.notification_script, 0, "notification script", "filename" },
117                 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
118                 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
119                 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
120                 { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
121                 { "dbdir-state", 0, POPT_ARG_STRING, &options.db_dir_state, 0, "directory for internal state tdb files", NULL },
122                 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock_file, 0, "location of recovery lock file", "filename" },
123                 { "pidfile", 0, POPT_ARG_STRING, &ctdbd_pidfile, 0, "location of PID file", "filename" },
124                 { "valgrinding", 0, POPT_ARG_NONE, &options.valgrinding, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
125                 { "nosetsched", 0, POPT_ARG_NONE, &options.nosetsched, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
126                 { "syslog", 0, POPT_ARG_NONE, &options.use_syslog, 0, "log messages to syslog", NULL },
127                 { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
128                 { "start-as-stopped", 0, POPT_ARG_NONE, &options.start_as_stopped, 0, "Node starts in stopped state", NULL },
129                 { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL },
130                 { "no-recmaster", 0, POPT_ARG_NONE, &options.no_recmaster, 0, "disable recmaster role on this node", NULL },
131                 { "lvs", 0, POPT_ARG_NONE, &options.lvs, 0, "lvs is enabled on this node", NULL },
132                 { "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, 0, "log level of event script output", NULL },
133                 { "nopublicipcheck", 0, POPT_ARG_NONE, &options.no_publicipcheck, 0, "don't check we have/don't have the correct public ip addresses", NULL },
134                 { "max-persistent-check-errors", 0, POPT_ARG_INT,
135                   &options.max_persistent_check_errors, 0,
136                   "max allowed persistent check errors (default 0)", NULL },
137                 { "log-ringbuf-size", 0, POPT_ARG_INT, &log_ringbuf_size, 0, "Number of log messages we can store in the memory ringbuffer", NULL },
138                 { "sloppy-start", 0, POPT_ARG_NONE, &fast_start, 0, "Do not perform full recovery on start", NULL },
139                 POPT_TABLEEND
140         };
141         int opt, ret;
142         const char **extra_argv;
143         int extra_argc = 0;
144         poptContext pc;
145         struct event_context *ev;
146
147         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
148
149         while ((opt = poptGetNextOpt(pc)) != -1) {
150                 switch (opt) {
151                 default:
152                         fprintf(stderr, "Invalid option %s: %s\n", 
153                                 poptBadOption(pc, 0), poptStrerror(opt));
154                         exit(1);
155                 }
156         }
157
158         /* setup the remaining options for the main program to use */
159         extra_argv = poptGetArgs(pc);
160         if (extra_argv) {
161                 extra_argv++;
162                 while (extra_argv[extra_argc]) extra_argc++;
163         }
164
165         talloc_enable_null_tracking();
166
167         ctdb_block_signal(SIGPIPE);
168         fault_setup("ctdbd");
169
170         ev = event_context_init(NULL);
171         tevent_loop_allow_nesting(ev);
172
173         ctdb = ctdb_cmdline_init(ev);
174
175         ctdb->start_as_disabled = options.start_as_disabled;
176         ctdb->start_as_stopped  = options.start_as_stopped;
177
178         script_log_level = options.script_log_level;
179
180         ret = ctdb_set_logfile(ctdb, options.logfile, options.use_syslog);
181         if (ret == -1) {
182                 printf("ctdb_set_logfile to %s failed - %s\n", 
183                        options.use_syslog?"syslog":options.logfile, ctdb_errstr(ctdb));
184                 exit(1);
185         }
186
187         DEBUG(DEBUG_NOTICE,("CTDB starting on node\n"));
188
189         gettimeofday(&ctdb->ctdbd_start_time, NULL);
190         gettimeofday(&ctdb->last_recovery_started, NULL);
191         gettimeofday(&ctdb->last_recovery_finished, NULL);
192         ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
193         ctdb->recovery_master  = (uint32_t)-1;
194         ctdb->upcalls          = &ctdb_upcalls;
195         ctdb->idr              = idr_init(ctdb);
196         ctdb->recovery_lock_fd = -1;
197
198         ctdb_tunables_set_defaults(ctdb);
199
200         ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
201         if (ret == -1) {
202                 DEBUG(DEBUG_ALERT,("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb)));
203                 exit(1);
204         }
205
206         ret = ctdb_set_transport(ctdb, options.transport);
207         if (ret == -1) {
208                 DEBUG(DEBUG_ALERT,("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb)));
209                 exit(1);
210         }
211
212         /* tell ctdb what address to listen on */
213         if (options.myaddress) {
214                 ret = ctdb_set_address(ctdb, options.myaddress);
215                 if (ret == -1) {
216                         DEBUG(DEBUG_ALERT,("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb)));
217                         exit(1);
218                 }
219         }
220
221         /* set ctdbd capabilities */
222         ctdb->capabilities = 0;
223         if (options.no_lmaster == 0) {
224                 ctdb->capabilities |= CTDB_CAP_LMASTER;
225         }
226         if (options.no_recmaster == 0) {
227                 ctdb->capabilities |= CTDB_CAP_RECMASTER;
228         }
229         if (options.lvs != 0) {
230                 ctdb->capabilities |= CTDB_CAP_LVS;
231         }
232
233         /* Initialise this node's PNN to the unknown value.  This will
234          * be set to the correct value by either ctdb_add_node() as
235          * part of loading the nodes file or by
236          * ctdb_tcp_listen_automatic() when the transport is
237          * initialised.  At some point we should de-optimise this and
238          * pull it out into ctdb_start_daemon() so it is done clearly
239          * and only in one place.
240          */
241         ctdb->pnn = -1;
242
243         /* Default value for CTDB_BASE - don't override */
244         setenv("CTDB_BASE", ETCDIR "/ctdb", 0);
245
246         /* tell ctdb what nodes are available */
247         if (options.nlist != NULL) {
248                 ctdb->nodes_file = options.nlist;
249         } else {
250                 ctdb->nodes_file =
251                         talloc_asprintf(ctdb, "%s/nodes", getenv("CTDB_BASE"));
252                 if (ctdb->nodes_file == NULL) {
253                         DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
254                         exit(1);
255                 }
256         }
257         ctdb_load_nodes_file(ctdb);
258
259         if (options.db_dir) {
260                 ret = ctdb_set_tdb_dir(ctdb, options.db_dir);
261                 if (ret == -1) {
262                         DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir failed - %s\n", ctdb_errstr(ctdb)));
263                         exit(1);
264                 }
265         }
266         ctdb_mkdir_p_or_die(ctdb, ctdb->db_directory, 0700);
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         ctdb_mkdir_p_or_die(ctdb, ctdb->db_directory_persistent, 0700);
276
277         if (options.db_dir_state) {
278                 ret = ctdb_set_tdb_dir_state(ctdb, options.db_dir_state);
279                 if (ret == -1) {
280                         DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir_state failed - %s\n", ctdb_errstr(ctdb)));
281                         exit(1);
282                 }
283         }
284         ctdb_mkdir_p_or_die(ctdb, ctdb->db_directory_state, 0700);
285
286         if (options.public_interface) {
287                 ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
288                 CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
289         }
290
291         if (options.single_public_ip) {
292                 if (options.public_interface == NULL) {
293                         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"));
294                         exit(10);
295                 }
296
297                 ret = ctdb_set_single_public_ip(ctdb, options.public_interface,
298                                                 options.single_public_ip);
299                 if (ret != 0) {
300                         DEBUG(DEBUG_ALERT,("Invalid --single-public-ip argument : %s . This is not a valid ip address. Exiting.\n", options.single_public_ip));
301                         exit(10);
302                 }
303         }
304
305         if (options.event_script_dir != NULL) {
306                 ctdb->event_script_dir = options.event_script_dir;
307         } else {
308                 ctdb->event_script_dir = talloc_asprintf(ctdb, "%s/events.d",
309                                                          getenv("CTDB_BASE"));
310                 if (ctdb->event_script_dir == NULL) {
311                         DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
312                         exit(1);
313                 }
314         }
315
316         if (options.notification_script != NULL) {
317                 ret = ctdb_set_notification_script(ctdb, options.notification_script);
318                 if (ret == -1) {
319                         DEBUG(DEBUG_ALERT,("Unable to setup notification script\n"));
320                         exit(1);
321                 }
322         }
323
324         ctdb->valgrinding = options.valgrinding;
325         if (options.valgrinding || options.nosetsched) {
326                 ctdb->do_setsched = 0;
327         } else {
328                 ctdb->do_setsched = 1;
329         }
330
331         ctdb->public_addresses_file = options.public_address_list;
332         ctdb->do_checkpublicip = !options.no_publicipcheck;
333
334         if (options.max_persistent_check_errors < 0) {
335                 ctdb->max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL;
336         } else {
337                 ctdb->max_persistent_check_errors = (uint64_t)options.max_persistent_check_errors;
338         }
339
340         /* start the protocol running (as a child) */
341         return ctdb_start_daemon(ctdb, interactive?false:true, options.use_syslog);
342 }