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