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