ctdb-tests: Drop ctdbd --event-script-dir option
[samba.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
26 #include <popt.h>
27 #include <talloc.h>
28 /* Allow use of deprecated function tevent_loop_allow_nesting() */
29 #define TEVENT_DEPRECATED
30 #include <tevent.h>
31
32 #include "lib/util/debug.h"
33 #include "lib/util/samba_util.h"
34
35 #include "ctdb_private.h"
36
37 #include "common/reqid.h"
38 #include "common/system.h"
39 #include "common/common.h"
40 #include "common/logging.h"
41
42 static struct {
43         const char *socket;
44         const char *debuglevel;
45         const char *nlist;
46         const char *transport;
47         const char *myaddress;
48         const char *public_address_list;
49         const char *notification_script;
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         const char *public_interface;
56         int         valgrinding;
57         int         nosetsched;
58         int         start_as_disabled;
59         int         start_as_stopped;
60         int         no_lmaster;
61         int         no_recmaster;
62         int         script_log_level;
63         int         no_publicipcheck;
64         int         max_persistent_check_errors;
65         int         torture;
66 } options = {
67         .socket = CTDB_SOCKET,
68         .debuglevel = "NOTICE",
69         .nlist = NULL,
70         .public_address_list = NULL,
71         .transport = "tcp",
72         .logging = "file:" LOGDIR "/log.ctdb",
73         .db_dir = CTDB_VARDIR,
74         .db_dir_persistent = CTDB_VARDIR "/persistent",
75         .db_dir_state = CTDB_VARDIR "/state",
76         .script_log_level = DEBUG_ERR,
77 };
78
79 int script_log_level;
80 bool fast_start;
81
82 /*
83   called by the transport layer when a packet comes in
84 */
85 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
86 {
87         struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
88
89         CTDB_INCREMENT_STAT(ctdb, node_packets_recv);
90
91         /* up the counter for this source node, so we know its alive */
92         if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
93                 /* as a special case, redirected calls don't increment the rx_cnt */
94                 if (hdr->operation != CTDB_REQ_CALL ||
95                     ((struct ctdb_req_call_old *)hdr)->hopcount == 0) {
96                         ctdb->nodes[hdr->srcnode]->rx_cnt++;
97                 }
98         }
99
100         ctdb_input_pkt(ctdb, hdr);
101 }
102
103 static const struct ctdb_upcalls ctdb_upcalls = {
104         .recv_pkt       = ctdb_recv_pkt,
105         .node_dead      = ctdb_node_dead,
106         .node_connected = ctdb_node_connected
107 };
108
109
110
111 /*
112   main program
113 */
114 int main(int argc, const char *argv[])
115 {
116         struct ctdb_context *ctdb;
117         int interactive = 0;
118
119         struct poptOption popt_options[] = {
120                 POPT_AUTOHELP
121                 { "socket", 0, POPT_ARG_STRING, &options.socket, 0, "local socket name", "filename" },
122                 { "debug", 'd', POPT_ARG_STRING, &options.debuglevel, 0, "debug level", NULL },
123                 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
124                 { "public-addresses", 0, POPT_ARG_STRING, &options.public_address_list, 0, "public address list file", "filename" },
125                 { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"},
126                 { "logging", 0, POPT_ARG_STRING, &options.logging, 0, "logging method to be used", NULL },
127                 { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "filename" },
128                 { "notification-script", 0, POPT_ARG_STRING, &options.notification_script, 0, "notification script", "filename" },
129                 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
130                 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
131                 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
132                 { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
133                 { "dbdir-state", 0, POPT_ARG_STRING, &options.db_dir_state, 0, "directory for internal state tdb files", NULL },
134                 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock, 0, "recovery lock", "lock" },
135                 { "valgrinding", 0, POPT_ARG_NONE, &options.valgrinding, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
136                 { "nosetsched", 0, POPT_ARG_NONE, &options.nosetsched, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
137                 { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
138                 { "start-as-stopped", 0, POPT_ARG_NONE, &options.start_as_stopped, 0, "Node starts in stopped state", NULL },
139                 { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL },
140                 { "no-recmaster", 0, POPT_ARG_NONE, &options.no_recmaster, 0, "disable recmaster role on this node", NULL },
141                 { "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, 0, "log level of event script output", NULL },
142                 { "nopublicipcheck", 0, POPT_ARG_NONE, &options.no_publicipcheck, 0, "don't check we have/don't have the correct public ip addresses", NULL },
143                 { "max-persistent-check-errors", 0, POPT_ARG_INT,
144                   &options.max_persistent_check_errors, 0,
145                   "max allowed persistent check errors (default 0)", NULL },
146                 { "sloppy-start", 0, POPT_ARG_NONE, &fast_start, 0, "Do not perform full recovery on start", NULL },
147                 { "torture", 0, POPT_ARG_NONE, &options.torture, 0, "enable nastiness in library", NULL },
148                 POPT_TABLEEND
149         };
150         int opt, ret;
151         const char **extra_argv;
152         poptContext pc;
153         struct tevent_context *ev;
154
155         /* Environment variable overrides default */
156         ctdbd_pidfile = getenv("CTDB_PIDFILE");
157         if (ctdbd_pidfile == NULL) {
158                 ctdbd_pidfile = CTDB_RUNDIR "/ctdbd.pid";
159         }
160
161         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
162
163         while ((opt = poptGetNextOpt(pc)) != -1) {
164                 switch (opt) {
165                 default:
166                         fprintf(stderr, "Invalid option %s: %s\n", 
167                                 poptBadOption(pc, 0), poptStrerror(opt));
168                         exit(1);
169                 }
170         }
171
172         /* If there are extra arguments then exit with usage message */
173         extra_argv = poptGetArgs(pc);
174         if (extra_argv) {
175                 extra_argv++;
176                 if (extra_argv[0])  {
177                         poptPrintHelp(pc, stdout, 0);
178                         exit(1);
179                 }
180         }
181
182         talloc_enable_null_tracking();
183
184         fault_setup();
185
186         ev = tevent_context_init(NULL);
187         if (ev == NULL) {
188                 fprintf(stderr, "tevent_context_init() failed\n");
189                 exit(1);
190         }
191         tevent_loop_allow_nesting(ev);
192
193         ctdb = ctdb_init(ev);
194         if (ctdb == NULL) {
195                 fprintf(stderr, "Failed to init ctdb\n");
196                 exit(1);
197         }
198
199         if (options.torture == 1) {
200                 ctdb_set_flags(ctdb, CTDB_FLAG_TORTURE);
201         }
202
203         /* Log to stderr when running as interactive */
204         if (interactive) {
205                 options.logging = "file:";
206         }
207
208         /* Initialize logging and set the debug level */
209         if (!ctdb_logging_init(ctdb, options.logging, options.debuglevel)) {
210                 exit(1);
211         }
212         setenv("CTDB_LOGGING", options.logging, 1);
213         setenv("CTDB_DEBUGLEVEL", debug_level_to_string(DEBUGLEVEL), 1);
214
215         setenv("CTDB_SOCKET", options.socket, 1);
216         ret = ctdb_set_socketname(ctdb, options.socket);
217         if (ret == -1) {
218                 DEBUG(DEBUG_ERR, ("ctdb_set_socketname() failed\n"));
219                 exit(1);
220         }
221
222         ctdb->start_as_disabled = options.start_as_disabled;
223         ctdb->start_as_stopped  = options.start_as_stopped;
224
225         script_log_level = options.script_log_level;
226
227         DEBUG(DEBUG_NOTICE,("CTDB starting on node\n"));
228
229         gettimeofday(&ctdb->ctdbd_start_time, NULL);
230         gettimeofday(&ctdb->last_recovery_started, NULL);
231         gettimeofday(&ctdb->last_recovery_finished, NULL);
232         ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
233         ctdb->recovery_master  = (uint32_t)-1;
234         ctdb->upcalls          = &ctdb_upcalls;
235
236         if (options.recovery_lock == NULL) {
237                 DEBUG(DEBUG_WARNING, ("Recovery lock not set\n"));
238         }
239         ctdb->recovery_lock = options.recovery_lock;
240
241         TALLOC_FREE(ctdb->idr);
242         ret = reqid_init(ctdb, 0, &ctdb->idr);;
243         if (ret != 0) {
244                 DEBUG(DEBUG_ERR, ("reqid_init failed (%s)\n", strerror(ret)));
245                 exit(1);
246         }
247
248         ctdb_tunables_set_defaults(ctdb);
249
250         ret = ctdb_set_transport(ctdb, options.transport);
251         if (ret == -1) {
252                 DEBUG(DEBUG_ERR,("ctdb_set_transport failed - %s\n",
253                                  ctdb_errstr(ctdb)));
254                 exit(1);
255         }
256
257         /* tell ctdb what address to listen on */
258         if (options.myaddress) {
259                 ret = ctdb_set_address(ctdb, options.myaddress);
260                 if (ret == -1) {
261                         DEBUG(DEBUG_ERR,("ctdb_set_address failed - %s\n",
262                                          ctdb_errstr(ctdb)));
263                         exit(1);
264                 }
265         }
266
267         /* set ctdbd capabilities */
268         ctdb->capabilities = CTDB_CAP_DEFAULT;
269         if (options.no_lmaster != 0) {
270                 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
271         }
272         if (options.no_recmaster != 0) {
273                 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
274         }
275
276         /* Initialise this node's PNN to the unknown value.  This will
277          * be set to the correct value by either ctdb_add_node() as
278          * part of loading the nodes file or by
279          * ctdb_tcp_listen_automatic() when the transport is
280          * initialised.  At some point we should de-optimise this and
281          * pull it out into ctdb_start_daemon() so it is done clearly
282          * and only in one place.
283          */
284         ctdb->pnn = -1;
285
286         /* Default value for CTDB_BASE - don't override */
287         setenv("CTDB_BASE", CTDB_ETCDIR, 0);
288
289         /* tell ctdb what nodes are available */
290         if (options.nlist != NULL) {
291                 ctdb->nodes_file = options.nlist;
292         } else {
293                 ctdb->nodes_file =
294                         talloc_asprintf(ctdb, "%s/nodes", getenv("CTDB_BASE"));
295                 if (ctdb->nodes_file == NULL) {
296                         DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
297                         exit(1);
298                 }
299         }
300         ctdb_load_nodes_file(ctdb);
301
302         ctdb->db_directory = options.db_dir;
303         mkdir_p_or_die(ctdb->db_directory, 0700);
304
305         ctdb->db_directory_persistent = options.db_dir_persistent;
306         mkdir_p_or_die(ctdb->db_directory_persistent, 0700);
307
308         ctdb->db_directory_state = options.db_dir_state;
309         mkdir_p_or_die(ctdb->db_directory_state, 0700);
310
311         if (options.public_interface) {
312                 ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
313                 CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
314         }
315
316         ctdb->event_script_dir = talloc_asprintf(ctdb,
317                                                  "%s/events.d",
318                                                  getenv("CTDB_BASE"));
319         if (ctdb->event_script_dir == NULL) {
320                 DBG_ERR("Out of memory\n");
321                 exit(1);
322         }
323
324         if (options.notification_script != NULL) {
325                 ret = ctdb_set_notification_script(ctdb, options.notification_script);
326                 if (ret == -1) {
327                         DEBUG(DEBUG_ERR,("Unable to setup notification script\n"));
328                         exit(1);
329                 }
330         }
331
332         ctdb->valgrinding = (options.valgrinding == 1);
333         ctdb->do_setsched = (options.nosetsched != 1);
334         if (ctdb->valgrinding) {
335                 ctdb->do_setsched = false;
336         }
337
338         ctdb->public_addresses_file = options.public_address_list;
339         ctdb->do_checkpublicip = (options.no_publicipcheck == 0);
340
341         if (options.max_persistent_check_errors < 0) {
342                 ctdb->max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL;
343         } else {
344                 ctdb->max_persistent_check_errors = (uint64_t)options.max_persistent_check_errors;
345         }
346
347         /* start the protocol running (as a child) */
348         return ctdb_start_daemon(ctdb, interactive?false:true);
349 }