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;
46 const char *notification_script;
48 const char *recovery_lock;
50 const char *db_dir_persistent;
51 const char *db_dir_state;
54 int start_as_disabled;
60 int max_persistent_check_errors;
63 .debuglevel = "NOTICE",
65 .logging = "file:" LOGDIR "/log.ctdb",
66 .db_dir = CTDB_VARDIR,
67 .db_dir_persistent = CTDB_VARDIR "/persistent",
68 .db_dir_state = CTDB_VARDIR "/state",
69 .script_log_level = DEBUG_ERR,
76 called by the transport layer when a packet comes in
78 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
80 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
82 CTDB_INCREMENT_STAT(ctdb, node_packets_recv);
84 /* up the counter for this source node, so we know its alive */
85 if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
86 /* as a special case, redirected calls don't increment the rx_cnt */
87 if (hdr->operation != CTDB_REQ_CALL ||
88 ((struct ctdb_req_call_old *)hdr)->hopcount == 0) {
89 ctdb->nodes[hdr->srcnode]->rx_cnt++;
93 ctdb_input_pkt(ctdb, hdr);
96 static const struct ctdb_upcalls ctdb_upcalls = {
97 .recv_pkt = ctdb_recv_pkt,
98 .node_dead = ctdb_node_dead,
99 .node_connected = ctdb_node_connected
107 int main(int argc, const char *argv[])
109 struct ctdb_context *ctdb;
111 const char *ctdb_socket;
113 struct poptOption popt_options[] = {
115 { "debug", 'd', POPT_ARG_STRING, &options.debuglevel, 0, "debug level", NULL },
116 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
117 { "logging", 0, POPT_ARG_STRING, &options.logging, 0, "logging method to be used", NULL },
118 { "notification-script", 0, POPT_ARG_STRING, &options.notification_script, 0, "notification script", "filename" },
119 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
120 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
121 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
122 { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
123 { "dbdir-state", 0, POPT_ARG_STRING, &options.db_dir_state, 0, "directory for internal state tdb files", NULL },
124 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock, 0, "recovery lock", "lock" },
125 { "valgrinding", 0, POPT_ARG_NONE, &options.valgrinding, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
126 { "nosetsched", 0, POPT_ARG_NONE, &options.nosetsched, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
127 { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
128 { "start-as-stopped", 0, POPT_ARG_NONE, &options.start_as_stopped, 0, "Node starts in stopped state", NULL },
129 { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL },
130 { "no-recmaster", 0, POPT_ARG_NONE, &options.no_recmaster, 0, "disable recmaster role on this node", NULL },
131 { "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, 0, "log level of event script output", NULL },
132 { "nopublicipcheck", 0, POPT_ARG_NONE, &options.no_publicipcheck, 0, "don't check we have/don't have the correct public ip addresses", NULL },
133 { "max-persistent-check-errors", 0, POPT_ARG_INT,
134 &options.max_persistent_check_errors, 0,
135 "max allowed persistent check errors (default 0)", NULL },
136 { "sloppy-start", 0, POPT_ARG_NONE, &fast_start, 0, "Do not perform full recovery on start", NULL },
137 { "torture", 0, POPT_ARG_NONE, &options.torture, 0, "enable nastiness in library", NULL },
141 const char **extra_argv;
143 struct tevent_context *ev;
145 /* Environment variable overrides default */
146 ctdbd_pidfile = getenv("CTDB_PIDFILE");
147 if (ctdbd_pidfile == NULL) {
148 ctdbd_pidfile = CTDB_RUNDIR "/ctdbd.pid";
151 /* Environment variable overrides default */
152 ctdb_socket = getenv("CTDB_SOCKET");
153 if (ctdb_socket == NULL) {
154 ctdb_socket = CTDB_SOCKET;
157 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
159 while ((opt = poptGetNextOpt(pc)) != -1) {
162 fprintf(stderr, "Invalid option %s: %s\n",
163 poptBadOption(pc, 0), poptStrerror(opt));
168 /* If there are extra arguments then exit with usage message */
169 extra_argv = poptGetArgs(pc);
173 poptPrintHelp(pc, stdout, 0);
178 talloc_enable_null_tracking();
182 ev = tevent_context_init(NULL);
184 fprintf(stderr, "tevent_context_init() failed\n");
187 tevent_loop_allow_nesting(ev);
189 ctdb = ctdb_init(ev);
191 fprintf(stderr, "Failed to init ctdb\n");
195 if (options.torture == 1) {
196 ctdb_set_flags(ctdb, CTDB_FLAG_TORTURE);
199 /* Log to stderr when running as interactive */
201 options.logging = "file:";
204 /* Initialize logging and set the debug level */
205 if (!ctdb_logging_init(ctdb, options.logging, options.debuglevel)) {
208 setenv("CTDB_LOGGING", options.logging, 1);
209 setenv("CTDB_DEBUGLEVEL", debug_level_to_string(DEBUGLEVEL), 1);
211 ret = ctdb_set_socketname(ctdb, ctdb_socket);
213 DEBUG(DEBUG_ERR, ("ctdb_set_socketname() failed\n"));
217 ctdb->start_as_disabled = options.start_as_disabled;
218 ctdb->start_as_stopped = options.start_as_stopped;
220 script_log_level = options.script_log_level;
222 DEBUG(DEBUG_NOTICE,("CTDB starting on node\n"));
224 gettimeofday(&ctdb->ctdbd_start_time, NULL);
225 gettimeofday(&ctdb->last_recovery_started, NULL);
226 gettimeofday(&ctdb->last_recovery_finished, NULL);
227 ctdb->recovery_mode = CTDB_RECOVERY_NORMAL;
228 ctdb->recovery_master = (uint32_t)-1;
229 ctdb->upcalls = &ctdb_upcalls;
231 if (options.recovery_lock == NULL) {
232 DEBUG(DEBUG_WARNING, ("Recovery lock not set\n"));
234 ctdb->recovery_lock = options.recovery_lock;
236 TALLOC_FREE(ctdb->idr);
237 ret = reqid_init(ctdb, 0, &ctdb->idr);;
239 DEBUG(DEBUG_ERR, ("reqid_init failed (%s)\n", strerror(ret)));
243 ctdb_tunables_set_defaults(ctdb);
245 ret = ctdb_set_transport(ctdb, options.transport);
247 DEBUG(DEBUG_ERR,("ctdb_set_transport failed - %s\n",
252 /* tell ctdb what address to listen on */
253 if (options.myaddress) {
254 ret = ctdb_set_address(ctdb, options.myaddress);
256 DEBUG(DEBUG_ERR,("ctdb_set_address failed - %s\n",
262 /* set ctdbd capabilities */
263 ctdb->capabilities = CTDB_CAP_DEFAULT;
264 if (options.no_lmaster != 0) {
265 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
267 if (options.no_recmaster != 0) {
268 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
271 /* Initialise this node's PNN to the unknown value. This will
272 * be set to the correct value by either ctdb_add_node() as
273 * part of loading the nodes file or by
274 * ctdb_tcp_listen_automatic() when the transport is
275 * initialised. At some point we should de-optimise this and
276 * pull it out into ctdb_start_daemon() so it is done clearly
277 * and only in one place.
281 /* Default value for CTDB_BASE - don't override */
282 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
284 /* tell ctdb what nodes are available */
286 talloc_asprintf(ctdb, "%s/nodes", getenv("CTDB_BASE"));
287 if (ctdb->nodes_file == NULL) {
288 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
291 ctdb_load_nodes_file(ctdb);
293 ctdb->db_directory = options.db_dir;
294 mkdir_p_or_die(ctdb->db_directory, 0700);
296 ctdb->db_directory_persistent = options.db_dir_persistent;
297 mkdir_p_or_die(ctdb->db_directory_persistent, 0700);
299 ctdb->db_directory_state = options.db_dir_state;
300 mkdir_p_or_die(ctdb->db_directory_state, 0700);
302 ctdb->event_script_dir = talloc_asprintf(ctdb,
304 getenv("CTDB_BASE"));
305 if (ctdb->event_script_dir == NULL) {
306 DBG_ERR("Out of memory\n");
310 if (options.notification_script != NULL) {
311 ret = ctdb_set_notification_script(ctdb, options.notification_script);
313 DEBUG(DEBUG_ERR,("Unable to setup notification script\n"));
318 ctdb->valgrinding = (options.valgrinding == 1);
319 ctdb->do_setsched = (options.nosetsched != 1);
320 if (ctdb->valgrinding) {
321 ctdb->do_setsched = false;
324 ctdb->do_checkpublicip = (options.no_publicipcheck == 0);
326 if (options.max_persistent_check_errors < 0) {
327 ctdb->max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL;
329 ctdb->max_persistent_check_errors = (uint64_t)options.max_persistent_check_errors;
332 /* start the protocol running (as a child) */
333 return ctdb_start_daemon(ctdb, interactive?false:true);