ctdb-daemon: Rename struct ctdb_req_call to ctdb_req_call_old
[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 #include "common/common.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_file;
52         const char *db_dir;
53         const char *db_dir_persistent;
54         const char *db_dir_state;
55         const char *public_interface;
56         const char *single_public_ip;
57         int         valgrinding;
58         int         nosetsched;
59         int         start_as_disabled;
60         int         start_as_stopped;
61         int         no_lmaster;
62         int         no_recmaster;
63         int         lvs;
64         int         script_log_level;
65         int         no_publicipcheck;
66         int         max_persistent_check_errors;
67 } options = {
68         .nlist = NULL,
69         .public_address_list = NULL,
70         .transport = "tcp",
71         .event_script_dir = NULL,
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                 POPT_CTDB_CMDLINE
122                 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
123                 { "public-addresses", 0, POPT_ARG_STRING, &options.public_address_list, 0, "public address list file", "filename" },
124                 { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"},
125                 { "single-public-ip", 0, POPT_ARG_STRING, &options.single_public_ip, 0, "single public ip", "ip-address"},
126                 { "event-script-dir", 0, POPT_ARG_STRING, &options.event_script_dir, 0, "event script directory", "dirname" },
127                 { "logging", 0, POPT_ARG_STRING, &options.logging, 0, "logging method to be used", NULL },
128                 { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "filename" },
129                 { "notification-script", 0, POPT_ARG_STRING, &options.notification_script, 0, "notification script", "filename" },
130                 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
131                 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
132                 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
133                 { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
134                 { "dbdir-state", 0, POPT_ARG_STRING, &options.db_dir_state, 0, "directory for internal state tdb files", NULL },
135                 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock_file, 0, "location of recovery lock file", "filename" },
136                 { "pidfile", 0, POPT_ARG_STRING, &ctdbd_pidfile, 0, "location of PID file", "filename" },
137                 { "valgrinding", 0, POPT_ARG_NONE, &options.valgrinding, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
138                 { "nosetsched", 0, POPT_ARG_NONE, &options.nosetsched, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
139                 { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
140                 { "start-as-stopped", 0, POPT_ARG_NONE, &options.start_as_stopped, 0, "Node starts in stopped state", NULL },
141                 { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL },
142                 { "no-recmaster", 0, POPT_ARG_NONE, &options.no_recmaster, 0, "disable recmaster role on this node", NULL },
143                 { "lvs", 0, POPT_ARG_NONE, &options.lvs, 0, "lvs is enabled on this node", NULL },
144                 { "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, 0, "log level of event script output", NULL },
145                 { "nopublicipcheck", 0, POPT_ARG_NONE, &options.no_publicipcheck, 0, "don't check we have/don't have the correct public ip addresses", NULL },
146                 { "max-persistent-check-errors", 0, POPT_ARG_INT,
147                   &options.max_persistent_check_errors, 0,
148                   "max allowed persistent check errors (default 0)", NULL },
149                 { "sloppy-start", 0, POPT_ARG_NONE, &fast_start, 0, "Do not perform full recovery on start", NULL },
150                 POPT_TABLEEND
151         };
152         int opt, ret;
153         const char **extra_argv;
154         int extra_argc = 0;
155         poptContext pc;
156         struct tevent_context *ev;
157
158         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
159
160         while ((opt = poptGetNextOpt(pc)) != -1) {
161                 switch (opt) {
162                 default:
163                         fprintf(stderr, "Invalid option %s: %s\n", 
164                                 poptBadOption(pc, 0), poptStrerror(opt));
165                         exit(1);
166                 }
167         }
168
169         /* setup the remaining options for the main program to use */
170         extra_argv = poptGetArgs(pc);
171         if (extra_argv) {
172                 extra_argv++;
173                 while (extra_argv[extra_argc]) extra_argc++;
174         }
175
176         talloc_enable_null_tracking();
177
178         fault_setup();
179
180         ev = tevent_context_init(NULL);
181         tevent_loop_allow_nesting(ev);
182
183         ctdb = ctdb_cmdline_init(ev);
184
185         ctdb->start_as_disabled = options.start_as_disabled;
186         ctdb->start_as_stopped  = options.start_as_stopped;
187
188         script_log_level = options.script_log_level;
189
190         if (!ctdb_logging_init(ctdb, options.logging)) {
191                 exit(1);
192         }
193
194         DEBUG(DEBUG_NOTICE,("CTDB starting on node\n"));
195
196         gettimeofday(&ctdb->ctdbd_start_time, NULL);
197         gettimeofday(&ctdb->last_recovery_started, NULL);
198         gettimeofday(&ctdb->last_recovery_finished, NULL);
199         ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
200         ctdb->recovery_master  = (uint32_t)-1;
201         ctdb->upcalls          = &ctdb_upcalls;
202         ctdb->recovery_lock_fd = -1;
203
204         ret = reqid_init(ctdb, 0, &ctdb->idr);;
205         if (ret != 0) {
206                 DEBUG(DEBUG_ALERT, ("reqid_init failed (%s)\n", strerror(ret)));
207                 exit(1);
208         }
209
210         ctdb_tunables_set_defaults(ctdb);
211
212         ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
213         if (ret == -1) {
214                 DEBUG(DEBUG_ALERT,("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb)));
215                 exit(1);
216         }
217
218         ret = ctdb_set_transport(ctdb, options.transport);
219         if (ret == -1) {
220                 DEBUG(DEBUG_ALERT,("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb)));
221                 exit(1);
222         }
223
224         /* tell ctdb what address to listen on */
225         if (options.myaddress) {
226                 ret = ctdb_set_address(ctdb, options.myaddress);
227                 if (ret == -1) {
228                         DEBUG(DEBUG_ALERT,("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb)));
229                         exit(1);
230                 }
231         }
232
233         /* set ctdbd capabilities */
234         ctdb->capabilities = CTDB_CAP_DEFAULT;
235         if (options.no_lmaster != 0) {
236                 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
237         }
238         if (options.no_recmaster != 0) {
239                 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
240         }
241         if (options.lvs != 0) {
242                 ctdb->capabilities |= CTDB_CAP_LVS;
243         }
244
245         /* Initialise this node's PNN to the unknown value.  This will
246          * be set to the correct value by either ctdb_add_node() as
247          * part of loading the nodes file or by
248          * ctdb_tcp_listen_automatic() when the transport is
249          * initialised.  At some point we should de-optimise this and
250          * pull it out into ctdb_start_daemon() so it is done clearly
251          * and only in one place.
252          */
253         ctdb->pnn = -1;
254
255         /* Default value for CTDB_BASE - don't override */
256         setenv("CTDB_BASE", CTDB_ETCDIR, 0);
257
258         /* tell ctdb what nodes are available */
259         if (options.nlist != NULL) {
260                 ctdb->nodes_file = options.nlist;
261         } else {
262                 ctdb->nodes_file =
263                         talloc_asprintf(ctdb, "%s/nodes", getenv("CTDB_BASE"));
264                 if (ctdb->nodes_file == NULL) {
265                         DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
266                         exit(1);
267                 }
268         }
269         ctdb_load_nodes_file(ctdb);
270
271         ctdb->db_directory = options.db_dir;
272         mkdir_p_or_die(ctdb->db_directory, 0700);
273
274         ctdb->db_directory_persistent = options.db_dir_persistent;
275         mkdir_p_or_die(ctdb->db_directory_persistent, 0700);
276
277         ctdb->db_directory_state = options.db_dir_state;
278         mkdir_p_or_die(ctdb->db_directory_state, 0700);
279
280         if (options.public_interface) {
281                 ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
282                 CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
283         }
284
285         if (options.single_public_ip) {
286                 if (options.public_interface == NULL) {
287                         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"));
288                         exit(10);
289                 }
290
291                 ret = ctdb_set_single_public_ip(ctdb, options.public_interface,
292                                                 options.single_public_ip);
293                 if (ret != 0) {
294                         DEBUG(DEBUG_ALERT,("Invalid --single-public-ip argument : %s . This is not a valid ip address. Exiting.\n", options.single_public_ip));
295                         exit(10);
296                 }
297         }
298
299         if (options.event_script_dir != NULL) {
300                 ctdb->event_script_dir = options.event_script_dir;
301         } else {
302                 ctdb->event_script_dir = talloc_asprintf(ctdb, "%s/events.d",
303                                                          getenv("CTDB_BASE"));
304                 if (ctdb->event_script_dir == NULL) {
305                         DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
306                         exit(1);
307                 }
308         }
309
310         if (options.notification_script != NULL) {
311                 ret = ctdb_set_notification_script(ctdb, options.notification_script);
312                 if (ret == -1) {
313                         DEBUG(DEBUG_ALERT,("Unable to setup notification script\n"));
314                         exit(1);
315                 }
316         }
317
318         ctdb->valgrinding = options.valgrinding;
319         if (options.valgrinding || options.nosetsched) {
320                 ctdb->do_setsched = 0;
321         } else {
322                 ctdb->do_setsched = 1;
323         }
324
325         ctdb->public_addresses_file = options.public_address_list;
326         ctdb->do_checkpublicip = !options.no_publicipcheck;
327
328         if (options.max_persistent_check_errors < 0) {
329                 ctdb->max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL;
330         } else {
331                 ctdb->max_persistent_check_errors = (uint64_t)options.max_persistent_check_errors;
332         }
333
334         /* start the protocol running (as a child) */
335         return ctdb_start_daemon(ctdb, interactive?false:true);
336 }