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