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