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"
43 const char *debuglevel;
44 const char *transport;
45 const char *myaddress;
47 const char *recovery_lock;
49 const char *db_dir_persistent;
50 const char *db_dir_state;
52 int start_as_disabled;
57 int max_persistent_check_errors;
59 .debuglevel = "NOTICE",
61 .logging = "file:" LOGDIR "/log.ctdb",
62 .db_dir = CTDB_VARDIR,
63 .db_dir_persistent = CTDB_VARDIR "/persistent",
64 .db_dir_state = CTDB_VARDIR "/state",
65 .script_log_level = DEBUG_ERR,
72 called by the transport layer when a packet comes in
74 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
76 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
78 CTDB_INCREMENT_STAT(ctdb, node_packets_recv);
80 /* up the counter for this source node, so we know its alive */
81 if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
82 /* as a special case, redirected calls don't increment the rx_cnt */
83 if (hdr->operation != CTDB_REQ_CALL ||
84 ((struct ctdb_req_call_old *)hdr)->hopcount == 0) {
85 ctdb->nodes[hdr->srcnode]->rx_cnt++;
89 ctdb_input_pkt(ctdb, hdr);
92 static const struct ctdb_upcalls ctdb_upcalls = {
93 .recv_pkt = ctdb_recv_pkt,
94 .node_dead = ctdb_node_dead,
95 .node_connected = ctdb_node_connected
98 static struct ctdb_context *ctdb_init(struct tevent_context *ev)
101 struct ctdb_context *ctdb;
103 ctdb = talloc_zero(ev, struct ctdb_context);
105 DBG_ERR("Memory error\n");
110 /* Wrap early to exercise code. */
111 ret = reqid_init(ctdb, INT_MAX-200, &ctdb->idr);
113 D_ERR("reqid_init failed (%s)\n", strerror(ret));
118 ret = srvid_init(ctdb, &ctdb->srv);
120 D_ERR("srvid_init failed (%s)\n", strerror(ret));
125 ret = ctdb_set_socketname(ctdb, CTDB_SOCKET);
127 DBG_ERR("ctdb_set_socketname failed.\n");
132 ctdb->statistics.statistics_start_time = timeval_current();
141 int main(int argc, const char *argv[])
143 struct ctdb_context *ctdb;
145 const char *ctdb_socket;
147 struct poptOption popt_options[] = {
149 { "debug", 'd', POPT_ARG_STRING, &options.debuglevel, 0, "debug level", NULL },
150 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
151 { "logging", 0, POPT_ARG_STRING, &options.logging, 0, "logging method to be used", NULL },
152 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
153 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
154 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
155 { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
156 { "dbdir-state", 0, POPT_ARG_STRING, &options.db_dir_state, 0, "directory for internal state tdb files", NULL },
157 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock, 0, "recovery lock", "lock" },
158 { "nosetsched", 0, POPT_ARG_NONE, &options.nosetsched, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
159 { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
160 { "start-as-stopped", 0, POPT_ARG_NONE, &options.start_as_stopped, 0, "Node starts in stopped state", NULL },
161 { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL },
162 { "no-recmaster", 0, POPT_ARG_NONE, &options.no_recmaster, 0, "disable recmaster role on this node", NULL },
163 { "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, 0, "log level of event script output", NULL },
164 { "max-persistent-check-errors", 0, POPT_ARG_INT,
165 &options.max_persistent_check_errors, 0,
166 "max allowed persistent check errors (default 0)", NULL },
170 const char **extra_argv;
172 struct tevent_context *ev;
173 const char *ctdb_base;
176 /* Environment variable overrides default */
177 ctdbd_pidfile = getenv("CTDB_PIDFILE");
178 if (ctdbd_pidfile == NULL) {
179 ctdbd_pidfile = CTDB_RUNDIR "/ctdbd.pid";
182 /* Environment variable overrides default */
183 ctdb_socket = getenv("CTDB_SOCKET");
184 if (ctdb_socket == NULL) {
185 ctdb_socket = CTDB_SOCKET;
188 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
190 while ((opt = poptGetNextOpt(pc)) != -1) {
193 fprintf(stderr, "Invalid option %s: %s\n",
194 poptBadOption(pc, 0), poptStrerror(opt));
199 /* If there are extra arguments then exit with usage message */
200 extra_argv = poptGetArgs(pc);
204 poptPrintHelp(pc, stdout, 0);
209 talloc_enable_null_tracking();
213 ev = tevent_context_init(NULL);
215 fprintf(stderr, "tevent_context_init() failed\n");
218 tevent_loop_allow_nesting(ev);
220 ctdb = ctdb_init(ev);
222 fprintf(stderr, "Failed to init ctdb\n");
226 /* Log to stderr when running as interactive */
228 options.logging = "file:";
231 /* Initialize logging and set the debug level */
232 if (!ctdb_logging_init(ctdb, options.logging, options.debuglevel)) {
235 setenv("CTDB_LOGGING", options.logging, 1);
236 setenv("CTDB_DEBUGLEVEL", debug_level_to_string(DEBUGLEVEL), 1);
238 ret = ctdb_set_socketname(ctdb, ctdb_socket);
240 DEBUG(DEBUG_ERR, ("ctdb_set_socketname() failed\n"));
244 ctdb->start_as_disabled = options.start_as_disabled;
245 ctdb->start_as_stopped = options.start_as_stopped;
247 script_log_level = options.script_log_level;
249 DEBUG(DEBUG_NOTICE,("CTDB starting on node\n"));
251 gettimeofday(&ctdb->ctdbd_start_time, NULL);
252 gettimeofday(&ctdb->last_recovery_started, NULL);
253 gettimeofday(&ctdb->last_recovery_finished, NULL);
254 ctdb->recovery_mode = CTDB_RECOVERY_NORMAL;
255 ctdb->recovery_master = (uint32_t)-1;
256 ctdb->upcalls = &ctdb_upcalls;
258 if (options.recovery_lock == NULL) {
259 DEBUG(DEBUG_WARNING, ("Recovery lock not set\n"));
261 ctdb->recovery_lock = options.recovery_lock;
263 ctdb_tunables_set_defaults(ctdb);
265 ret = ctdb_set_transport(ctdb, options.transport);
267 DEBUG(DEBUG_ERR,("ctdb_set_transport failed - %s\n",
272 /* tell ctdb what address to listen on */
273 if (options.myaddress) {
274 ret = ctdb_set_address(ctdb, options.myaddress);
276 DEBUG(DEBUG_ERR,("ctdb_set_address failed - %s\n",
282 /* set ctdbd capabilities */
283 ctdb->capabilities = CTDB_CAP_DEFAULT;
284 if (options.no_lmaster != 0) {
285 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
287 if (options.no_recmaster != 0) {
288 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
291 /* Initialise this node's PNN to the unknown value. This will
292 * be set to the correct value by either ctdb_add_node() as
293 * part of loading the nodes file or by
294 * ctdb_tcp_listen_automatic() when the transport is
295 * initialised. At some point we should de-optimise this and
296 * pull it out into ctdb_start_daemon() so it is done clearly
297 * and only in one place.
301 /* Default value for CTDB_BASE - don't override */
302 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
303 ctdb_base = getenv("CTDB_BASE");
304 if (ctdb_base == NULL) {
305 D_ERR("CTDB_BASE not set\n");
309 /* tell ctdb what nodes are available */
310 ctdb->nodes_file = talloc_asprintf(ctdb, "%s/nodes", ctdb_base);
311 if (ctdb->nodes_file == NULL) {
312 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
315 ctdb_load_nodes_file(ctdb);
317 ctdb->db_directory = options.db_dir;
318 mkdir_p_or_die(ctdb->db_directory, 0700);
320 ctdb->db_directory_persistent = options.db_dir_persistent;
321 mkdir_p_or_die(ctdb->db_directory_persistent, 0700);
323 ctdb->db_directory_state = options.db_dir_state;
324 mkdir_p_or_die(ctdb->db_directory_state, 0700);
326 ctdb->event_script_dir = talloc_asprintf(ctdb,
329 if (ctdb->event_script_dir == NULL) {
330 DBG_ERR("Out of memory\n");
334 ctdb->notification_script = talloc_asprintf(ctdb,
337 if (ctdb->notification_script == NULL) {
338 D_ERR("Unable to set notification script\n");
342 ctdb->do_setsched = (options.nosetsched != 1);
344 ctdb->do_checkpublicip = true;
346 t = getenv("CTDB_TEST_MODE");
348 ctdb->do_setsched = false;
349 ctdb->do_checkpublicip = false;
353 if (options.max_persistent_check_errors < 0) {
354 ctdb->max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL;
356 ctdb->max_persistent_check_errors = (uint64_t)options.max_persistent_check_errors;
359 /* start the protocol running (as a child) */
360 return ctdb_start_daemon(ctdb, interactive?false:true);