4 Copyright (C) Andrew Tridgell 2006
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.
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.
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/>.
21 #include "system/filesys.h"
23 #include "system/time.h"
24 #include "system/wait.h"
25 #include "system/network.h"
27 #include "../include/ctdb_private.h"
31 const char *transport;
32 const char *myaddress;
33 const char *public_address_list;
34 const char *event_script_dir;
35 const char *notification_script;
36 const char *debug_hung_script;
38 const char *recovery_lock_file;
40 const char *db_dir_persistent;
41 const char *db_dir_state;
42 const char *public_interface;
43 const char *single_public_ip;
47 int start_as_disabled;
54 int max_persistent_check_errors;
56 .nlist = ETCDIR "/ctdb/nodes",
58 .event_script_dir = ETCDIR "/ctdb/events.d",
59 .logfile = LOGDIR "/log.ctdb",
60 .db_dir = VARDIR "/ctdb",
61 .db_dir_persistent = VARDIR "/ctdb/persistent",
62 .db_dir_state = VARDIR "/ctdb/state",
63 .script_log_level = DEBUG_ERR,
64 .debug_hung_script = "/etc/ctdb/debug-hung-script.sh",
71 called by the transport layer when a packet comes in
73 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
75 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
77 CTDB_INCREMENT_STAT(ctdb, node_packets_recv);
79 /* up the counter for this source node, so we know its alive */
80 if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
81 /* as a special case, redirected calls don't increment the rx_cnt */
82 if (hdr->operation != CTDB_REQ_CALL ||
83 ((struct ctdb_req_call *)hdr)->hopcount == 0) {
84 ctdb->nodes[hdr->srcnode]->rx_cnt++;
88 ctdb_input_pkt(ctdb, hdr);
91 void ctdb_load_nodes_file(struct ctdb_context *ctdb)
95 ret = ctdb_set_nlist(ctdb, options.nlist);
97 DEBUG(DEBUG_ALERT,("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb)));
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
113 int main(int argc, const char *argv[])
115 struct ctdb_context *ctdb;
118 struct poptOption popt_options[] = {
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 { "logfile", 0, POPT_ARG_STRING, &options.logfile, 0, "log file location", "filename" },
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 { "debug-hung-script", 0, POPT_ARG_STRING, &options.debug_hung_script, 0, "debug script for hung eventscripts", "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 { "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 { "syslog", 0, POPT_ARG_NONE, &options.use_syslog, 0, "log messages to syslog", 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, DEBUG_ERR, "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 { "log-ringbuf-size", 0, POPT_ARG_INT, &log_ringbuf_size, DEBUG_ERR, "Number of log messages we can store in the memory ringbuffer", NULL },
150 { "sloppy-start", 0, POPT_ARG_NONE, &fast_start, 0, "Do not perform full recovery on start", NULL },
154 const char **extra_argv;
157 struct event_context *ev;
159 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
161 while ((opt = poptGetNextOpt(pc)) != -1) {
164 fprintf(stderr, "Invalid option %s: %s\n",
165 poptBadOption(pc, 0), poptStrerror(opt));
170 /* setup the remaining options for the main program to use */
171 extra_argv = poptGetArgs(pc);
174 while (extra_argv[extra_argc]) extra_argc++;
177 talloc_enable_null_tracking();
179 ctdb_block_signal(SIGPIPE);
180 fault_setup("ctdbd");
182 ev = event_context_init(NULL);
183 tevent_loop_allow_nesting(ev);
185 ctdb = ctdb_cmdline_init(ev);
187 ctdb->start_as_disabled = options.start_as_disabled;
188 ctdb->start_as_stopped = options.start_as_stopped;
190 script_log_level = options.script_log_level;
192 ret = ctdb_set_logfile(ctdb, options.logfile, options.use_syslog);
194 printf("ctdb_set_logfile to %s failed - %s\n",
195 options.use_syslog?"syslog":options.logfile, ctdb_errstr(ctdb));
199 DEBUG(DEBUG_NOTICE,("CTDB starting on node\n"));
201 gettimeofday(&ctdb->ctdbd_start_time, NULL);
202 gettimeofday(&ctdb->last_recovery_started, NULL);
203 gettimeofday(&ctdb->last_recovery_finished, NULL);
204 ctdb->recovery_mode = CTDB_RECOVERY_NORMAL;
205 ctdb->recovery_master = (uint32_t)-1;
206 ctdb->upcalls = &ctdb_upcalls;
207 ctdb->idr = idr_init(ctdb);
208 ctdb->recovery_lock_fd = -1;
210 ctdb_tunables_set_defaults(ctdb);
212 ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
214 DEBUG(DEBUG_ALERT,("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb)));
218 ret = ctdb_set_transport(ctdb, options.transport);
220 DEBUG(DEBUG_ALERT,("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb)));
224 /* tell ctdb what address to listen on */
225 if (options.myaddress) {
226 ret = ctdb_set_address(ctdb, options.myaddress);
228 DEBUG(DEBUG_ALERT,("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb)));
233 /* set ctdbd capabilities */
234 ctdb->capabilities = 0;
235 if (options.no_lmaster == 0) {
236 ctdb->capabilities |= CTDB_CAP_LMASTER;
238 if (options.no_recmaster == 0) {
239 ctdb->capabilities |= CTDB_CAP_RECMASTER;
241 if (options.lvs != 0) {
242 ctdb->capabilities |= CTDB_CAP_LVS;
245 /* tell ctdb what nodes are available */
246 ctdb_load_nodes_file(ctdb);
248 if (options.db_dir) {
249 ret = ctdb_set_tdb_dir(ctdb, options.db_dir);
251 DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir failed - %s\n", ctdb_errstr(ctdb)));
255 if (options.db_dir_persistent) {
256 ret = ctdb_set_tdb_dir_persistent(ctdb, options.db_dir_persistent);
258 DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir_persistent failed - %s\n", ctdb_errstr(ctdb)));
262 if (options.db_dir_state) {
263 ret = ctdb_set_tdb_dir_state(ctdb, options.db_dir_state);
265 DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir_state failed - %s\n", ctdb_errstr(ctdb)));
270 if (options.public_interface) {
271 ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
272 CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
275 if (options.single_public_ip) {
276 if (options.public_interface == NULL) {
277 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"));
281 ret = ctdb_set_single_public_ip(ctdb, options.public_interface,
282 options.single_public_ip);
284 DEBUG(DEBUG_ALERT,("Invalid --single-public-ip argument : %s . This is not a valid ip address. Exiting.\n", options.single_public_ip));
289 ret = ctdb_set_event_script_dir(ctdb, options.event_script_dir);
291 DEBUG(DEBUG_ALERT,("Unable to setup event script directory\n"));
295 if (options.notification_script != NULL) {
296 ret = ctdb_set_notification_script(ctdb, options.notification_script);
298 DEBUG(DEBUG_ALERT,("Unable to setup notification script\n"));
303 if (options.debug_hung_script != NULL) {
304 ret = ctdb_set_debug_hung_script(ctdb, options.debug_hung_script);
307 DEBUG(DEBUG_ALERT,("Unable to setup script to debug hung eventscripts\n"));
311 ctdb->valgrinding = options.valgrinding;
312 if (options.valgrinding || options.nosetsched) {
313 ctdb->do_setsched = 0;
315 ctdb->do_setsched = 1;
318 ctdb->do_checkpublicip = !options.no_publicipcheck;
320 if (options.max_persistent_check_errors < 0) {
321 ctdb->max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL;
323 ctdb->max_persistent_check_errors = (uint64_t)options.max_persistent_check_errors;
326 if (getenv("CTDB_BASE") == NULL) {
327 /* setup a environment variable for the event scripts to use
328 to find the installation directory */
329 setenv("CTDB_BASE", ETCDIR "/ctdb", 1);
332 /* start the protocol running (as a child) */
333 return ctdb_start_daemon(ctdb, interactive?false:true, options.use_syslog, options.public_address_list);