Merge commit 'origin/master'
[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 *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 *public_interface;
42         const char *single_public_ip;
43         const char *node_ip;
44         int         no_setsched;
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 } options = {
54         .nlist = ETCDIR "/ctdb/nodes",
55         .transport = "tcp",
56         .event_script_dir = ETCDIR "/ctdb/events.d",
57         .logfile = LOGDIR "/log.ctdb",
58         .db_dir = VARDIR "/ctdb",
59         .db_dir_persistent = VARDIR "/ctdb/persistent",
60         .script_log_level = DEBUG_ERR,
61 };
62
63 int script_log_level;
64
65 /*
66   called by the transport layer when a packet comes in
67 */
68 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
69 {
70         struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
71
72         ctdb->statistics.node_packets_recv++;
73
74         /* up the counter for this source node, so we know its alive */
75         if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
76                 /* as a special case, redirected calls don't increment the rx_cnt */
77                 if (hdr->operation != CTDB_REQ_CALL ||
78                     ((struct ctdb_req_call *)hdr)->hopcount == 0) {
79                         ctdb->nodes[hdr->srcnode]->rx_cnt++;
80                 }
81         }
82
83         ctdb_input_pkt(ctdb, hdr);
84 }
85
86 void ctdb_load_nodes_file(struct ctdb_context *ctdb)
87 {
88         int ret;
89
90         ret = ctdb_set_nlist(ctdb, options.nlist);
91         if (ret == -1) {
92                 DEBUG(DEBUG_ALERT,("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb)));
93                 exit(1);
94         }
95 }
96
97 static const struct ctdb_upcalls ctdb_upcalls = {
98         .recv_pkt       = ctdb_recv_pkt,
99         .node_dead      = ctdb_node_dead,
100         .node_connected = ctdb_node_connected
101 };
102
103
104
105 /*
106   main program
107 */
108 int main(int argc, const char *argv[])
109 {
110         struct ctdb_context *ctdb;
111         int interactive = 0;
112
113         struct poptOption popt_options[] = {
114                 POPT_AUTOHELP
115                 POPT_CTDB_CMDLINE
116                 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
117                 { "public-addresses", 0, POPT_ARG_STRING, &options.public_address_list, 0, "public address list file", "filename" },
118                 { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"},
119                 { "single-public-ip", 0, POPT_ARG_STRING, &options.single_public_ip, 0, "single public ip", "ip-address"},
120                 { "event-script-dir", 0, POPT_ARG_STRING, &options.event_script_dir, 0, "event script directory", "dirname" },
121                 { "logfile", 0, POPT_ARG_STRING, &options.logfile, 0, "log file location", "filename" },
122                 { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "filename" },
123                 { "node-ip", 0, POPT_ARG_STRING, &options.node_ip, 0, "node ip", "ip-address"},
124                 { "notification-script", 0, POPT_ARG_STRING, &options.notification_script, 0, "notification script", "filename" },
125                 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
126                 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
127                 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
128                 { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
129                 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock_file, 0, "location of recovery lock file", "filename" },
130                 { "nosetsched", 0, POPT_ARG_NONE, &options.no_setsched, 0, "disable setscheduler SCHED_FIFO call", NULL },
131                 { "syslog", 0, POPT_ARG_NONE, &options.use_syslog, 0, "log messages to syslog", NULL },
132                 { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
133                 { "start-as-stopped", 0, POPT_ARG_NONE, &options.start_as_stopped, 0, "Node starts in stopped state", NULL },
134                 { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL },
135                 { "no-recmaster", 0, POPT_ARG_NONE, &options.no_recmaster, 0, "disable recmaster role on this node", NULL },
136                 { "lvs", 0, POPT_ARG_NONE, &options.lvs, 0, "lvs is enabled on this node", NULL },
137                 { "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, DEBUG_ERR, "log level of event script output", NULL },
138                 { "nopublicipcheck", 0, POPT_ARG_NONE, &options.no_publicipcheck, 0, "dont check we have/dont have the correct public ip addresses", 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
169         ev = event_context_init(NULL);
170
171         ctdb = ctdb_cmdline_init(ev);
172
173         ctdb->start_as_disabled = options.start_as_disabled;
174         ctdb->start_as_stopped  = options.start_as_stopped;
175
176         script_log_level = options.script_log_level;
177
178         ret = ctdb_set_logfile(ctdb, options.logfile, options.use_syslog);
179         if (ret == -1) {
180                 printf("ctdb_set_logfile to %s failed - %s\n", 
181                        options.use_syslog?"syslog":options.logfile, ctdb_errstr(ctdb));
182                 exit(1);
183         }
184
185         DEBUG(DEBUG_NOTICE,("Starting CTDB daemon\n"));
186         gettimeofday(&ctdb->ctdbd_start_time, NULL);
187         gettimeofday(&ctdb->last_recovery_started, NULL);
188         gettimeofday(&ctdb->last_recovery_finished, NULL);
189         ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
190         ctdb->recovery_master  = (uint32_t)-1;
191         ctdb->upcalls          = &ctdb_upcalls;
192         ctdb->idr              = idr_init(ctdb);
193         ctdb->recovery_lock_fd = -1;
194
195         ctdb_tunables_set_defaults(ctdb);
196
197
198         ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
199         if (ret == -1) {
200                 DEBUG(DEBUG_ALERT,("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb)));
201                 exit(1);
202         }
203
204         ret = ctdb_set_transport(ctdb, options.transport);
205         if (ret == -1) {
206                 DEBUG(DEBUG_ALERT,("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb)));
207                 exit(1);
208         }
209
210         /* tell ctdb what address to listen on */
211         if (options.myaddress) {
212                 ret = ctdb_set_address(ctdb, options.myaddress);
213                 if (ret == -1) {
214                         DEBUG(DEBUG_ALERT,("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb)));
215                         exit(1);
216                 }
217         }
218
219         /* set ctdbd capabilities */
220         ctdb->capabilities = 0;
221         if (options.no_lmaster == 0) {
222                 ctdb->capabilities |= CTDB_CAP_LMASTER;
223         }
224         if (options.no_recmaster == 0) {
225                 ctdb->capabilities |= CTDB_CAP_RECMASTER;
226         }
227         if (options.lvs != 0) {
228                 ctdb->capabilities |= CTDB_CAP_LVS;
229         }
230
231         /* tell ctdb what nodes are available */
232         ctdb_load_nodes_file(ctdb);
233
234         /* if a node-ip was specified, verify that it exists in the
235            nodes file
236         */
237         if (options.node_ip != NULL) {
238                 DEBUG(DEBUG_NOTICE,("IP for this node is %s\n", options.node_ip));
239                 ret = ctdb_ip_to_nodeid(ctdb, options.node_ip);
240                 if (ret == -1) {
241                         DEBUG(DEBUG_ALERT,("The specified node-ip:%s is not a valid node address. Exiting.\n", options.node_ip));
242                         exit(1);
243                 }
244                 ctdb->node_ip = options.node_ip;
245                 DEBUG(DEBUG_NOTICE,("This is node %d\n", ret));
246         }
247
248         if (options.db_dir) {
249                 ret = ctdb_set_tdb_dir(ctdb, options.db_dir);
250                 if (ret == -1) {
251                         DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir failed - %s\n", ctdb_errstr(ctdb)));
252                         exit(1);
253                 }
254         }
255         if (options.db_dir_persistent) {
256                 ret = ctdb_set_tdb_dir_persistent(ctdb, options.db_dir_persistent);
257                 if (ret == -1) {
258                         DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir_persistent failed - %s\n", ctdb_errstr(ctdb)));
259                         exit(1);
260                 }
261         }
262
263         if (options.public_interface) {
264                 ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
265                 CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
266         }
267
268         if (options.single_public_ip) {
269                 struct ctdb_vnn *svnn;
270
271                 if (options.public_interface == NULL) {
272                         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"));
273                         exit(10);
274                 }
275
276                 svnn = talloc_zero(ctdb, struct ctdb_vnn);
277                 CTDB_NO_MEMORY(ctdb, svnn);
278
279                 ctdb->single_ip_vnn = svnn;
280                 svnn->iface = talloc_strdup(svnn, options.public_interface);
281                 CTDB_NO_MEMORY(ctdb, svnn->iface);
282
283                 if (parse_ip(options.single_public_ip, 
284                                 svnn->iface,
285                                 0,
286                                 &svnn->public_address) == 0) {
287                         DEBUG(DEBUG_ALERT,("Invalid --single-public-ip argument : %s . This is not a valid ip address. Exiting.\n", options.single_public_ip));
288                         exit(10);
289                 }
290         }
291
292         if (options.public_address_list) {
293                 ret = ctdb_set_public_addresses(ctdb, options.public_address_list);
294                 if (ret == -1) {
295                         DEBUG(DEBUG_ALERT,("Unable to setup public address list\n"));
296                         exit(1);
297                 }
298         }
299
300         ret = ctdb_set_event_script_dir(ctdb, options.event_script_dir);
301         if (ret == -1) {
302                 DEBUG(DEBUG_ALERT,("Unable to setup event script directory\n"));
303                 exit(1);
304         }
305
306         if (options.notification_script != NULL) {
307                 ret = ctdb_set_notification_script(ctdb, options.notification_script);
308                 if (ret == -1) {
309                         DEBUG(DEBUG_ALERT,("Unable to setup notification script\n"));
310                         exit(1);
311                 }
312         }
313
314         ctdb->do_setsched = !options.no_setsched;
315
316         ctdb->do_checkpublicip = !options.no_publicipcheck;
317
318         if (getenv("CTDB_BASE") == NULL) {
319                 /* setup a environment variable for the event scripts to use
320                    to find the installation directory */
321                 setenv("CTDB_BASE", ETCDIR "/ctdb", 1);
322         }
323
324         /* start the protocol running (as a child) */
325         return ctdb_start_daemon(ctdb, interactive?False:True, options.use_syslog);
326 }