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