e242a3e224e028d0c6645dcbbd2b57c4e063a1eb
[metze/samba/wip.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 "replace.h"
21 #include "system/filesys.h"
22 #include "system/time.h"
23 #include "system/wait.h"
24 #include "system/network.h"
25 #include "system/syslog.h"
26
27 #include <popt.h>
28 #include <talloc.h>
29 /* Allow use of deprecated function tevent_loop_allow_nesting() */
30 #define TEVENT_DEPRECATED
31 #include <tevent.h>
32
33 #include "lib/util/debug.h"
34 #include "lib/util/samba_util.h"
35
36 #include "ctdb_private.h"
37
38 #include "common/reqid.h"
39 #include "common/system.h"
40 #include "common/common.h"
41 #include "common/logging.h"
42 #include "common/logging_conf.h"
43
44 #include "ctdb_config.h"
45
46 static struct {
47         const char *debuglevel;
48         const char *transport;
49         const char *myaddress;
50         const char *logging;
51         const char *recovery_lock;
52         const char *db_dir;
53         const char *db_dir_persistent;
54         const char *db_dir_state;
55         int         nosetsched;
56         int         start_as_disabled;
57         int         start_as_stopped;
58         int         no_lmaster;
59         int         no_recmaster;
60         int         script_log_level;
61 } options = {
62         .debuglevel = "NOTICE",
63         .transport = "tcp",
64         .logging = "file:" LOGDIR "/log.ctdb",
65         .db_dir = CTDB_VARDIR "/volatile",
66         .db_dir_persistent = CTDB_VARDIR "/persistent",
67         .db_dir_state = CTDB_VARDIR "/state",
68         .script_log_level = DEBUG_ERR,
69 };
70
71 int script_log_level;
72 bool fast_start;
73
74 /*
75   called by the transport layer when a packet comes in
76 */
77 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
78 {
79         struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
80
81         CTDB_INCREMENT_STAT(ctdb, node_packets_recv);
82
83         /* up the counter for this source node, so we know its alive */
84         if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
85                 /* as a special case, redirected calls don't increment the rx_cnt */
86                 if (hdr->operation != CTDB_REQ_CALL ||
87                     ((struct ctdb_req_call_old *)hdr)->hopcount == 0) {
88                         ctdb->nodes[hdr->srcnode]->rx_cnt++;
89                 }
90         }
91
92         ctdb_input_pkt(ctdb, hdr);
93 }
94
95 static const struct ctdb_upcalls ctdb_upcalls = {
96         .recv_pkt       = ctdb_recv_pkt,
97         .node_dead      = ctdb_node_dead,
98         .node_connected = ctdb_node_connected
99 };
100
101 static struct ctdb_context *ctdb_init(struct tevent_context *ev)
102 {
103         int ret;
104         struct ctdb_context *ctdb;
105
106         ctdb = talloc_zero(ev, struct ctdb_context);
107         if (ctdb == NULL) {
108                 DBG_ERR("Memory error\n");
109                 return NULL;
110         }
111         ctdb->ev  = ev;
112
113         /* Wrap early to exercise code. */
114         ret = reqid_init(ctdb, INT_MAX-200, &ctdb->idr);
115         if (ret != 0) {
116                 D_ERR("reqid_init failed (%s)\n", strerror(ret));
117                 talloc_free(ctdb);
118                 return NULL;
119         }
120
121         ret = srvid_init(ctdb, &ctdb->srv);
122         if (ret != 0) {
123                 D_ERR("srvid_init failed (%s)\n", strerror(ret));
124                 talloc_free(ctdb);
125                 return NULL;
126         }
127
128         ret = ctdb_set_socketname(ctdb, CTDB_SOCKET);
129         if (ret != 0) {
130                 DBG_ERR("ctdb_set_socketname failed.\n");
131                 talloc_free(ctdb);
132                 return NULL;
133         }
134
135         gettimeofday(&ctdb->ctdbd_start_time, NULL);
136
137         gettimeofday(&ctdb->last_recovery_started, NULL);
138         gettimeofday(&ctdb->last_recovery_finished, NULL);
139
140         ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
141         ctdb->recovery_master  = (uint32_t)-1;
142
143         ctdb->upcalls = &ctdb_upcalls;
144
145         ctdb->statistics.statistics_start_time = timeval_current();
146
147         ctdb->capabilities = CTDB_CAP_DEFAULT;
148
149         /*
150          * Initialise this node's PNN to the unknown value.  This will
151          * be set to the correct value by either ctdb_add_node() as
152          * part of loading the nodes file or by
153          * ctdb_tcp_listen_automatic() when the transport is
154          * initialised.  At some point we should de-optimise this and
155          * pull it out into ctdb_start_daemon() so it is done clearly
156          * and only in one place.
157          */
158         ctdb->pnn = CTDB_UNKNOWN_PNN;
159
160         ctdb->do_checkpublicip = true;
161
162         return ctdb;
163 }
164
165
166 /*
167   main program
168 */
169 int main(int argc, const char *argv[])
170 {
171         struct ctdb_context *ctdb = NULL;
172         int interactive = 0;
173         const char *ctdb_socket;
174
175         struct poptOption popt_options[] = {
176                 POPT_AUTOHELP
177                 { "debug", 'd', POPT_ARG_STRING, &options.debuglevel, 0, "debug level", NULL },
178                 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
179                 { "logging", 0, POPT_ARG_STRING, &options.logging, 0, "logging method to be used", NULL },
180                 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
181                 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
182                 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
183                 { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
184                 { "dbdir-state", 0, POPT_ARG_STRING, &options.db_dir_state, 0, "directory for internal state tdb files", NULL },
185                 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock, 0, "recovery lock", "lock" },
186                 { "nosetsched", 0, POPT_ARG_NONE, &options.nosetsched, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
187                 { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
188                 { "start-as-stopped", 0, POPT_ARG_NONE, &options.start_as_stopped, 0, "Node starts in stopped state", NULL },
189                 { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL },
190                 { "no-recmaster", 0, POPT_ARG_NONE, &options.no_recmaster, 0, "disable recmaster role on this node", NULL },
191                 { "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, 0, "log level of event script output", NULL },
192                 POPT_TABLEEND
193         };
194         int opt, ret;
195         const char **extra_argv;
196         poptContext pc;
197         struct tevent_context *ev;
198         const char *ctdb_base;
199         struct conf_context *conf;
200         const char *logging_location;
201         const char *t;
202         bool ok;
203
204         /*
205          * Basic setup
206          */
207
208         talloc_enable_null_tracking();
209
210         fault_setup();
211
212         ev = tevent_context_init(NULL);
213         if (ev == NULL) {
214                 fprintf(stderr, "tevent_context_init() failed\n");
215                 exit(1);
216         }
217         tevent_loop_allow_nesting(ev);
218
219         ctdb = ctdb_init(ev);
220         if (ctdb == NULL) {
221                 fprintf(stderr, "Failed to init ctdb\n");
222                 exit(1);
223         }
224
225         /* Default value for CTDB_BASE - don't override */
226         setenv("CTDB_BASE", CTDB_ETCDIR, 0);
227         ctdb_base = getenv("CTDB_BASE");
228         if (ctdb_base == NULL) {
229                 D_ERR("CTDB_BASE not set\n");
230                 exit(1);
231         }
232
233         /*
234          * Command-line option handling
235          */
236
237         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
238
239         while ((opt = poptGetNextOpt(pc)) != -1) {
240                 switch (opt) {
241                 default:
242                         fprintf(stderr, "Invalid option %s: %s\n", 
243                                 poptBadOption(pc, 0), poptStrerror(opt));
244                         goto fail;
245                 }
246         }
247
248         /* If there are extra arguments then exit with usage message */
249         extra_argv = poptGetArgs(pc);
250         if (extra_argv) {
251                 extra_argv++;
252                 if (extra_argv[0])  {
253                         poptPrintHelp(pc, stdout, 0);
254                         goto fail;
255                 }
256         }
257
258         /*
259          * Configuration file handling
260          */
261
262         ret = ctdbd_config_load(ctdb, &conf);
263         if (ret != 0) {
264                 fprintf(stderr, "Failed to setup config file handling\n");
265                 goto fail;
266         }
267
268         /*
269          * Logging setup/options
270          */
271
272         /* Log to stderr (ignoring configuration) when running as interactive */
273         if (interactive) {
274                 logging_location = "file:";
275         } else {
276                 logging_location = logging_conf_location(conf);
277         }
278
279         if (strcmp(logging_location, "syslog") != 0) {
280                 /* This can help when CTDB logging is misconfigured */
281                 syslog(LOG_DAEMON|LOG_NOTICE,
282                        "CTDB logging to location %s",
283                        logging_location);
284         }
285
286         /* Initialize logging and set the debug level */
287         ok = ctdb_logging_init(ctdb,
288                                logging_location,
289                                logging_conf_log_level(conf));
290         if (!ok) {
291                 goto fail;
292         }
293         setenv("CTDB_LOGGING", logging_location, 1);
294         setenv("CTDB_DEBUGLEVEL", debug_level_to_string(DEBUGLEVEL), 1);
295
296         script_log_level = debug_level_from_string(
297                                         ctdb_config.script_log_level);
298
299         D_NOTICE("CTDB starting on node\n");
300
301         /*
302          * Cluster setup/options
303          */
304
305         ret = ctdb_set_transport(ctdb, ctdb_config.transport);
306         if (ret == -1) {
307                 D_ERR("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb));
308                 goto fail;
309         }
310
311         if (ctdb_config.recovery_lock == NULL) {
312                 D_WARNING("Recovery lock not set\n");
313         }
314         ctdb->recovery_lock = ctdb_config.recovery_lock;
315
316         /* tell ctdb what address to listen on */
317         if (ctdb_config.node_address) {
318                 ret = ctdb_set_address(ctdb, ctdb_config.node_address);
319                 if (ret == -1) {
320                         D_ERR("ctdb_set_address failed - %s\n",
321                               ctdb_errstr(ctdb));
322                         goto fail;
323                 }
324         }
325
326         /* tell ctdb what nodes are available */
327         ctdb->nodes_file = talloc_asprintf(ctdb, "%s/nodes", ctdb_base);
328         if (ctdb->nodes_file == NULL) {
329                 DBG_ERR(" Out of memory\n");
330                 goto fail;
331         }
332         ctdb_load_nodes_file(ctdb);
333
334         /*
335          * Database setup/options
336          */
337
338         ctdb->db_directory = ctdb_config.dbdir_volatile;
339         ctdb->db_directory_persistent = ctdb_config.dbdir_persistent;
340         ctdb->db_directory_state = ctdb_config.dbdir_state;
341
342         if (ctdb_config.lock_debug_script != NULL) {
343                 ret = setenv("CTDB_DEBUG_LOCKS",
344                              ctdb_config.lock_debug_script,
345                              1);
346                 if (ret != 0) {
347                         D_ERR("Failed to set up lock debugging (%s)\n",
348                               strerror(ret));
349                         goto fail;
350                 }
351         }
352
353         /*
354          * Event setup/options
355          */
356         if (ctdb_config.event_debug_script != NULL) {
357                 ret = setenv("CTDB_DEBUG_HUNG_SCRIPT",
358                              ctdb_config.event_debug_script,
359                              1);
360                 if (ret != 0) {
361                         D_ERR("Failed to set up event script debugging (%s)\n",
362                               strerror(ret));
363                         goto fail;
364                 }
365         }
366
367         /*
368          * Legacy setup/options
369          */
370
371         ctdb->start_as_disabled = (int)ctdb_config.start_as_disabled;
372         ctdb->start_as_stopped  = (int)ctdb_config.start_as_stopped;
373
374         /* set ctdbd capabilities */
375         if (!ctdb_config.lmaster_capability) {
376                 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
377         }
378         if (!ctdb_config.recmaster_capability) {
379                 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
380         }
381
382         ctdb->do_setsched = !ctdb_config.no_realtime;
383
384         /*
385          * Miscellaneous setup
386          */
387
388         ctdb_tunables_set_defaults(ctdb);
389
390         ctdb->event_script_dir = talloc_asprintf(ctdb,
391                                                  "%s/events.d",
392                                                  ctdb_base);
393         if (ctdb->event_script_dir == NULL) {
394                 DBG_ERR("Out of memory\n");
395                 goto fail;
396         }
397
398         ctdb->notification_script = talloc_asprintf(ctdb,
399                                                     "%s/notify.sh",
400                                                     ctdb_base);
401         if (ctdb->notification_script == NULL) {
402                 D_ERR("Unable to set notification script\n");
403                 goto fail;
404         }
405
406         /*
407          * Testing and debug options
408          */
409
410         /* Environment variable overrides default */
411         ctdbd_pidfile = getenv("CTDB_PIDFILE");
412         if (ctdbd_pidfile == NULL) {
413                 ctdbd_pidfile = CTDB_RUNDIR "/ctdbd.pid";
414         }
415
416         /* Environment variable overrides default */
417         ctdb_socket = getenv("CTDB_SOCKET");
418         if (ctdb_socket == NULL) {
419                 ctdb_socket = CTDB_SOCKET;
420         }
421         ret = ctdb_set_socketname(ctdb, ctdb_socket);
422         if (ret == -1) {
423                 D_ERR("ctdb_set_socketname() failed\n");
424                 goto fail;
425         }
426
427         t = getenv("CTDB_TEST_MODE");
428         if (t != NULL) {
429                 ctdb->do_setsched = false;
430                 ctdb->do_checkpublicip = false;
431                 fast_start = true;
432         }
433
434         /* start the protocol running (as a child) */
435         return ctdb_start_daemon(ctdb, interactive?false:true);
436
437 fail:
438         talloc_free(ctdb);
439         exit(1);
440 }