ctdb-daemon: Drop ctdbd --notification-script command-line 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 *debuglevel;
44         const char *transport;
45         const char *myaddress;
46         const char *logging;
47         const char *recovery_lock;
48         const char *db_dir;
49         const char *db_dir_persistent;
50         const char *db_dir_state;
51         int         valgrinding;
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         no_publicipcheck;
59         int         max_persistent_check_errors;
60         int         torture;
61 } options = {
62         .debuglevel = "NOTICE",
63         .transport = "tcp",
64         .logging = "file:" LOGDIR "/log.ctdb",
65         .db_dir = CTDB_VARDIR,
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
102
103 /*
104   main program
105 */
106 int main(int argc, const char *argv[])
107 {
108         struct ctdb_context *ctdb;
109         int interactive = 0;
110         const char *ctdb_socket;
111
112         struct poptOption popt_options[] = {
113                 POPT_AUTOHELP
114                 { "debug", 'd', POPT_ARG_STRING, &options.debuglevel, 0, "debug level", NULL },
115                 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
116                 { "logging", 0, POPT_ARG_STRING, &options.logging, 0, "logging method to be used", NULL },
117                 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
118                 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
119                 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
120                 { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
121                 { "dbdir-state", 0, POPT_ARG_STRING, &options.db_dir_state, 0, "directory for internal state tdb files", NULL },
122                 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock, 0, "recovery lock", "lock" },
123                 { "valgrinding", 0, POPT_ARG_NONE, &options.valgrinding, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
124                 { "nosetsched", 0, POPT_ARG_NONE, &options.nosetsched, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
125                 { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
126                 { "start-as-stopped", 0, POPT_ARG_NONE, &options.start_as_stopped, 0, "Node starts in stopped state", NULL },
127                 { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL },
128                 { "no-recmaster", 0, POPT_ARG_NONE, &options.no_recmaster, 0, "disable recmaster role on this node", NULL },
129                 { "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, 0, "log level of event script output", NULL },
130                 { "nopublicipcheck", 0, POPT_ARG_NONE, &options.no_publicipcheck, 0, "don't check we have/don't have the correct public ip addresses", NULL },
131                 { "max-persistent-check-errors", 0, POPT_ARG_INT,
132                   &options.max_persistent_check_errors, 0,
133                   "max allowed persistent check errors (default 0)", NULL },
134                 { "sloppy-start", 0, POPT_ARG_NONE, &fast_start, 0, "Do not perform full recovery on start", NULL },
135                 { "torture", 0, POPT_ARG_NONE, &options.torture, 0, "enable nastiness in library", NULL },
136                 POPT_TABLEEND
137         };
138         int opt, ret;
139         const char **extra_argv;
140         poptContext pc;
141         struct tevent_context *ev;
142         const char *ctdb_base;
143
144         /* Environment variable overrides default */
145         ctdbd_pidfile = getenv("CTDB_PIDFILE");
146         if (ctdbd_pidfile == NULL) {
147                 ctdbd_pidfile = CTDB_RUNDIR "/ctdbd.pid";
148         }
149
150         /* Environment variable overrides default */
151         ctdb_socket = getenv("CTDB_SOCKET");
152         if (ctdb_socket == NULL) {
153                 ctdb_socket = CTDB_SOCKET;
154         }
155
156         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
157
158         while ((opt = poptGetNextOpt(pc)) != -1) {
159                 switch (opt) {
160                 default:
161                         fprintf(stderr, "Invalid option %s: %s\n", 
162                                 poptBadOption(pc, 0), poptStrerror(opt));
163                         exit(1);
164                 }
165         }
166
167         /* If there are extra arguments then exit with usage message */
168         extra_argv = poptGetArgs(pc);
169         if (extra_argv) {
170                 extra_argv++;
171                 if (extra_argv[0])  {
172                         poptPrintHelp(pc, stdout, 0);
173                         exit(1);
174                 }
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         if (options.torture == 1) {
195                 ctdb_set_flags(ctdb, CTDB_FLAG_TORTURE);
196         }
197
198         /* Log to stderr when running as interactive */
199         if (interactive) {
200                 options.logging = "file:";
201         }
202
203         /* Initialize logging and set the debug level */
204         if (!ctdb_logging_init(ctdb, options.logging, options.debuglevel)) {
205                 exit(1);
206         }
207         setenv("CTDB_LOGGING", options.logging, 1);
208         setenv("CTDB_DEBUGLEVEL", debug_level_to_string(DEBUGLEVEL), 1);
209
210         ret = ctdb_set_socketname(ctdb, ctdb_socket);
211         if (ret == -1) {
212                 DEBUG(DEBUG_ERR, ("ctdb_set_socketname() failed\n"));
213                 exit(1);
214         }
215
216         ctdb->start_as_disabled = options.start_as_disabled;
217         ctdb->start_as_stopped  = options.start_as_stopped;
218
219         script_log_level = options.script_log_level;
220
221         DEBUG(DEBUG_NOTICE,("CTDB starting on node\n"));
222
223         gettimeofday(&ctdb->ctdbd_start_time, NULL);
224         gettimeofday(&ctdb->last_recovery_started, NULL);
225         gettimeofday(&ctdb->last_recovery_finished, NULL);
226         ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
227         ctdb->recovery_master  = (uint32_t)-1;
228         ctdb->upcalls          = &ctdb_upcalls;
229
230         if (options.recovery_lock == NULL) {
231                 DEBUG(DEBUG_WARNING, ("Recovery lock not set\n"));
232         }
233         ctdb->recovery_lock = options.recovery_lock;
234
235         TALLOC_FREE(ctdb->idr);
236         ret = reqid_init(ctdb, 0, &ctdb->idr);;
237         if (ret != 0) {
238                 DEBUG(DEBUG_ERR, ("reqid_init failed (%s)\n", strerror(ret)));
239                 exit(1);
240         }
241
242         ctdb_tunables_set_defaults(ctdb);
243
244         ret = ctdb_set_transport(ctdb, options.transport);
245         if (ret == -1) {
246                 DEBUG(DEBUG_ERR,("ctdb_set_transport failed - %s\n",
247                                  ctdb_errstr(ctdb)));
248                 exit(1);
249         }
250
251         /* tell ctdb what address to listen on */
252         if (options.myaddress) {
253                 ret = ctdb_set_address(ctdb, options.myaddress);
254                 if (ret == -1) {
255                         DEBUG(DEBUG_ERR,("ctdb_set_address failed - %s\n",
256                                          ctdb_errstr(ctdb)));
257                         exit(1);
258                 }
259         }
260
261         /* set ctdbd capabilities */
262         ctdb->capabilities = CTDB_CAP_DEFAULT;
263         if (options.no_lmaster != 0) {
264                 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
265         }
266         if (options.no_recmaster != 0) {
267                 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
268         }
269
270         /* Initialise this node's PNN to the unknown value.  This will
271          * be set to the correct value by either ctdb_add_node() as
272          * part of loading the nodes file or by
273          * ctdb_tcp_listen_automatic() when the transport is
274          * initialised.  At some point we should de-optimise this and
275          * pull it out into ctdb_start_daemon() so it is done clearly
276          * and only in one place.
277          */
278         ctdb->pnn = -1;
279
280         /* Default value for CTDB_BASE - don't override */
281         setenv("CTDB_BASE", CTDB_ETCDIR, 0);
282         ctdb_base = getenv("CTDB_BASE");
283         if (ctdb_base == NULL) {
284                 D_ERR("CTDB_BASE not set\n");
285                 exit(1);
286         }
287
288         /* tell ctdb what nodes are available */
289         ctdb->nodes_file = talloc_asprintf(ctdb, "%s/nodes", ctdb_base);
290         if (ctdb->nodes_file == NULL) {
291                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
292                 exit(1);
293         }
294         ctdb_load_nodes_file(ctdb);
295
296         ctdb->db_directory = options.db_dir;
297         mkdir_p_or_die(ctdb->db_directory, 0700);
298
299         ctdb->db_directory_persistent = options.db_dir_persistent;
300         mkdir_p_or_die(ctdb->db_directory_persistent, 0700);
301
302         ctdb->db_directory_state = options.db_dir_state;
303         mkdir_p_or_die(ctdb->db_directory_state, 0700);
304
305         ctdb->event_script_dir = talloc_asprintf(ctdb,
306                                                  "%s/events.d",
307                                                  ctdb_base);
308         if (ctdb->event_script_dir == NULL) {
309                 DBG_ERR("Out of memory\n");
310                 exit(1);
311         }
312
313         ctdb->notification_script = talloc_asprintf(ctdb,
314                                                     "%s/notify.sh",
315                                                     ctdb_base);
316         if (ctdb->notification_script == NULL) {
317                 D_ERR("Unable to set notification script\n");
318                 exit(1);
319         }
320
321         ctdb->valgrinding = (options.valgrinding == 1);
322         ctdb->do_setsched = (options.nosetsched != 1);
323         if (ctdb->valgrinding) {
324                 ctdb->do_setsched = false;
325         }
326
327         ctdb->do_checkpublicip = (options.no_publicipcheck == 0);
328
329         if (options.max_persistent_check_errors < 0) {
330                 ctdb->max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL;
331         } else {
332                 ctdb->max_persistent_check_errors = (uint64_t)options.max_persistent_check_errors;
333         }
334
335         /* start the protocol running (as a child) */
336         return ctdb_start_daemon(ctdb, interactive?false:true);
337 }