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