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