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"
22 #include "system/time.h"
23 #include "system/wait.h"
24 #include "system/network.h"
28 /* Allow use of deprecated function tevent_loop_allow_nesting() */
29 #define TEVENT_DEPRECATED
32 #include "lib/util/debug.h"
33 #include "lib/util/samba_util.h"
35 #include "ctdb_private.h"
37 #include "common/reqid.h"
38 #include "common/system.h"
39 #include "common/common.h"
40 #include "common/logging.h"
44 const char *debuglevel;
46 const char *transport;
47 const char *myaddress;
48 const char *public_address_list;
49 const char *notification_script;
51 const char *recovery_lock;
53 const char *db_dir_persistent;
54 const char *db_dir_state;
55 const char *public_interface;
58 int start_as_disabled;
64 int max_persistent_check_errors;
67 .socket = CTDB_SOCKET,
68 .debuglevel = "NOTICE",
70 .public_address_list = 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,
83 called by the transport layer when a packet comes in
85 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
87 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
89 CTDB_INCREMENT_STAT(ctdb, node_packets_recv);
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++;
100 ctdb_input_pkt(ctdb, hdr);
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
114 int main(int argc, const char *argv[])
116 struct ctdb_context *ctdb;
119 struct poptOption popt_options[] = {
121 { "socket", 0, POPT_ARG_STRING, &options.socket, 0, "local socket name", "filename" },
122 { "debug", 'd', POPT_ARG_STRING, &options.debuglevel, 0, "debug level", NULL },
123 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
124 { "public-addresses", 0, POPT_ARG_STRING, &options.public_address_list, 0, "public address list file", "filename" },
125 { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"},
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, 0, "recovery lock", "lock" },
135 { "valgrinding", 0, POPT_ARG_NONE, &options.valgrinding, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
136 { "nosetsched", 0, POPT_ARG_NONE, &options.nosetsched, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
137 { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
138 { "start-as-stopped", 0, POPT_ARG_NONE, &options.start_as_stopped, 0, "Node starts in stopped state", NULL },
139 { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL },
140 { "no-recmaster", 0, POPT_ARG_NONE, &options.no_recmaster, 0, "disable recmaster role on this node", NULL },
141 { "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, 0, "log level of event script output", NULL },
142 { "nopublicipcheck", 0, POPT_ARG_NONE, &options.no_publicipcheck, 0, "don't check we have/don't have the correct public ip addresses", NULL },
143 { "max-persistent-check-errors", 0, POPT_ARG_INT,
144 &options.max_persistent_check_errors, 0,
145 "max allowed persistent check errors (default 0)", NULL },
146 { "sloppy-start", 0, POPT_ARG_NONE, &fast_start, 0, "Do not perform full recovery on start", NULL },
147 { "torture", 0, POPT_ARG_NONE, &options.torture, 0, "enable nastiness in library", NULL },
151 const char **extra_argv;
153 struct tevent_context *ev;
155 /* Environment variable overrides default */
156 ctdbd_pidfile = getenv("CTDB_PIDFILE");
157 if (ctdbd_pidfile == NULL) {
158 ctdbd_pidfile = CTDB_RUNDIR "/ctdbd.pid";
161 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
163 while ((opt = poptGetNextOpt(pc)) != -1) {
166 fprintf(stderr, "Invalid option %s: %s\n",
167 poptBadOption(pc, 0), poptStrerror(opt));
172 /* If there are extra arguments then exit with usage message */
173 extra_argv = poptGetArgs(pc);
177 poptPrintHelp(pc, stdout, 0);
182 talloc_enable_null_tracking();
186 ev = tevent_context_init(NULL);
188 fprintf(stderr, "tevent_context_init() failed\n");
191 tevent_loop_allow_nesting(ev);
193 ctdb = ctdb_init(ev);
195 fprintf(stderr, "Failed to init ctdb\n");
199 if (options.torture == 1) {
200 ctdb_set_flags(ctdb, CTDB_FLAG_TORTURE);
203 /* Log to stderr when running as interactive */
205 options.logging = "file:";
208 /* Initialize logging and set the debug level */
209 if (!ctdb_logging_init(ctdb, options.logging, options.debuglevel)) {
212 setenv("CTDB_LOGGING", options.logging, 1);
213 setenv("CTDB_DEBUGLEVEL", debug_level_to_string(DEBUGLEVEL), 1);
215 setenv("CTDB_SOCKET", options.socket, 1);
216 ret = ctdb_set_socketname(ctdb, options.socket);
218 DEBUG(DEBUG_ERR, ("ctdb_set_socketname() failed\n"));
222 ctdb->start_as_disabled = options.start_as_disabled;
223 ctdb->start_as_stopped = options.start_as_stopped;
225 script_log_level = options.script_log_level;
227 DEBUG(DEBUG_NOTICE,("CTDB starting on node\n"));
229 gettimeofday(&ctdb->ctdbd_start_time, NULL);
230 gettimeofday(&ctdb->last_recovery_started, NULL);
231 gettimeofday(&ctdb->last_recovery_finished, NULL);
232 ctdb->recovery_mode = CTDB_RECOVERY_NORMAL;
233 ctdb->recovery_master = (uint32_t)-1;
234 ctdb->upcalls = &ctdb_upcalls;
236 if (options.recovery_lock == NULL) {
237 DEBUG(DEBUG_WARNING, ("Recovery lock not set\n"));
239 ctdb->recovery_lock = options.recovery_lock;
241 TALLOC_FREE(ctdb->idr);
242 ret = reqid_init(ctdb, 0, &ctdb->idr);;
244 DEBUG(DEBUG_ERR, ("reqid_init failed (%s)\n", strerror(ret)));
248 ctdb_tunables_set_defaults(ctdb);
250 ret = ctdb_set_transport(ctdb, options.transport);
252 DEBUG(DEBUG_ERR,("ctdb_set_transport failed - %s\n",
257 /* tell ctdb what address to listen on */
258 if (options.myaddress) {
259 ret = ctdb_set_address(ctdb, options.myaddress);
261 DEBUG(DEBUG_ERR,("ctdb_set_address failed - %s\n",
267 /* set ctdbd capabilities */
268 ctdb->capabilities = CTDB_CAP_DEFAULT;
269 if (options.no_lmaster != 0) {
270 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
272 if (options.no_recmaster != 0) {
273 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
276 /* Initialise this node's PNN to the unknown value. This will
277 * be set to the correct value by either ctdb_add_node() as
278 * part of loading the nodes file or by
279 * ctdb_tcp_listen_automatic() when the transport is
280 * initialised. At some point we should de-optimise this and
281 * pull it out into ctdb_start_daemon() so it is done clearly
282 * and only in one place.
286 /* Default value for CTDB_BASE - don't override */
287 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
289 /* tell ctdb what nodes are available */
290 if (options.nlist != NULL) {
291 ctdb->nodes_file = options.nlist;
294 talloc_asprintf(ctdb, "%s/nodes", getenv("CTDB_BASE"));
295 if (ctdb->nodes_file == NULL) {
296 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
300 ctdb_load_nodes_file(ctdb);
302 ctdb->db_directory = options.db_dir;
303 mkdir_p_or_die(ctdb->db_directory, 0700);
305 ctdb->db_directory_persistent = options.db_dir_persistent;
306 mkdir_p_or_die(ctdb->db_directory_persistent, 0700);
308 ctdb->db_directory_state = options.db_dir_state;
309 mkdir_p_or_die(ctdb->db_directory_state, 0700);
311 if (options.public_interface) {
312 ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
313 CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
316 ctdb->event_script_dir = talloc_asprintf(ctdb,
318 getenv("CTDB_BASE"));
319 if (ctdb->event_script_dir == NULL) {
320 DBG_ERR("Out of memory\n");
324 if (options.notification_script != NULL) {
325 ret = ctdb_set_notification_script(ctdb, options.notification_script);
327 DEBUG(DEBUG_ERR,("Unable to setup notification script\n"));
332 ctdb->valgrinding = (options.valgrinding == 1);
333 ctdb->do_setsched = (options.nosetsched != 1);
334 if (ctdb->valgrinding) {
335 ctdb->do_setsched = false;
338 ctdb->public_addresses_file = options.public_address_list;
339 ctdb->do_checkpublicip = (options.no_publicipcheck == 0);
341 if (options.max_persistent_check_errors < 0) {
342 ctdb->max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL;
344 ctdb->max_persistent_check_errors = (uint64_t)options.max_persistent_check_errors;
347 /* start the protocol running (as a child) */
348 return ctdb_start_daemon(ctdb, interactive?false:true);