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"
25 #include "system/syslog.h"
29 /* Allow use of deprecated function tevent_loop_allow_nesting() */
30 #define TEVENT_DEPRECATED
33 #include "lib/util/debug.h"
34 #include "lib/util/samba_util.h"
36 #include "ctdb_private.h"
38 #include "common/reqid.h"
39 #include "common/system.h"
40 #include "common/common.h"
41 #include "common/logging.h"
42 #include "common/logging_conf.h"
44 #include "ctdb_config.h"
50 called by the transport layer when a packet comes in
52 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
54 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
56 CTDB_INCREMENT_STAT(ctdb, node_packets_recv);
58 /* up the counter for this source node, so we know its alive */
59 if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
60 /* as a special case, redirected calls don't increment the rx_cnt */
61 if (hdr->operation != CTDB_REQ_CALL ||
62 ((struct ctdb_req_call_old *)hdr)->hopcount == 0) {
63 ctdb->nodes[hdr->srcnode]->rx_cnt++;
67 ctdb_input_pkt(ctdb, hdr);
70 static const struct ctdb_upcalls ctdb_upcalls = {
71 .recv_pkt = ctdb_recv_pkt,
72 .node_dead = ctdb_node_dead,
73 .node_connected = ctdb_node_connected
76 static struct ctdb_context *ctdb_init(struct tevent_context *ev)
79 struct ctdb_context *ctdb;
81 ctdb = talloc_zero(ev, struct ctdb_context);
83 DBG_ERR("Memory error\n");
88 /* Wrap early to exercise code. */
89 ret = reqid_init(ctdb, INT_MAX-200, &ctdb->idr);
91 D_ERR("reqid_init failed (%s)\n", strerror(ret));
96 ret = srvid_init(ctdb, &ctdb->srv);
98 D_ERR("srvid_init failed (%s)\n", strerror(ret));
103 ret = ctdb_set_socketname(ctdb, CTDB_SOCKET);
105 DBG_ERR("ctdb_set_socketname failed.\n");
110 gettimeofday(&ctdb->ctdbd_start_time, NULL);
112 gettimeofday(&ctdb->last_recovery_started, NULL);
113 gettimeofday(&ctdb->last_recovery_finished, NULL);
115 ctdb->recovery_mode = CTDB_RECOVERY_NORMAL;
116 ctdb->recovery_master = (uint32_t)-1;
118 ctdb->upcalls = &ctdb_upcalls;
120 ctdb->statistics.statistics_start_time = timeval_current();
122 ctdb->capabilities = CTDB_CAP_DEFAULT;
125 * Initialise this node's PNN to the unknown value. This will
126 * be set to the correct value by either ctdb_add_node() as
127 * part of loading the nodes file or by
128 * ctdb_tcp_listen_automatic() when the transport is
129 * initialised. At some point we should de-optimise this and
130 * pull it out into ctdb_start_daemon() so it is done clearly
131 * and only in one place.
133 ctdb->pnn = CTDB_UNKNOWN_PNN;
135 ctdb->do_checkpublicip = true;
144 int main(int argc, const char *argv[])
146 struct ctdb_context *ctdb = NULL;
148 const char *ctdb_socket;
150 struct poptOption popt_options[] = {
152 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0,
153 "don't fork", NULL },
157 const char **extra_argv;
159 struct tevent_context *ev;
160 const char *ctdb_base;
161 struct conf_context *conf;
162 const char *logging_location;
170 talloc_enable_null_tracking();
174 ev = tevent_context_init(NULL);
176 fprintf(stderr, "tevent_context_init() failed\n");
179 tevent_loop_allow_nesting(ev);
181 ctdb = ctdb_init(ev);
183 fprintf(stderr, "Failed to init ctdb\n");
187 /* Default value for CTDB_BASE - don't override */
188 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
189 ctdb_base = getenv("CTDB_BASE");
190 if (ctdb_base == NULL) {
191 D_ERR("CTDB_BASE not set\n");
196 * Command-line option handling
199 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
201 while ((opt = poptGetNextOpt(pc)) != -1) {
204 fprintf(stderr, "Invalid option %s: %s\n",
205 poptBadOption(pc, 0), poptStrerror(opt));
210 /* If there are extra arguments then exit with usage message */
211 extra_argv = poptGetArgs(pc);
215 poptPrintHelp(pc, stdout, 0);
221 * Configuration file handling
224 ret = ctdbd_config_load(ctdb, &conf);
226 fprintf(stderr, "Failed to setup config file handling\n");
231 * Logging setup/options
234 /* Log to stderr (ignoring configuration) when running as interactive */
236 logging_location = "file:";
238 logging_location = logging_conf_location(conf);
241 if (strcmp(logging_location, "syslog") != 0) {
242 /* This can help when CTDB logging is misconfigured */
243 syslog(LOG_DAEMON|LOG_NOTICE,
244 "CTDB logging to location %s",
248 /* Initialize logging and set the debug level */
249 ok = ctdb_logging_init(ctdb,
251 logging_conf_log_level(conf));
255 setenv("CTDB_LOGGING", logging_location, 1);
256 setenv("CTDB_DEBUGLEVEL", debug_level_to_string(DEBUGLEVEL), 1);
258 script_log_level = debug_level_from_string(
259 ctdb_config.script_log_level);
261 D_NOTICE("CTDB starting on node\n");
264 * Cluster setup/options
267 ret = ctdb_set_transport(ctdb, ctdb_config.transport);
269 D_ERR("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb));
273 if (ctdb_config.recovery_lock == NULL) {
274 D_WARNING("Recovery lock not set\n");
276 ctdb->recovery_lock = ctdb_config.recovery_lock;
278 /* tell ctdb what address to listen on */
279 if (ctdb_config.node_address) {
280 ret = ctdb_set_address(ctdb, ctdb_config.node_address);
282 D_ERR("ctdb_set_address failed - %s\n",
288 /* tell ctdb what nodes are available */
289 ctdb->nodes_file = talloc_asprintf(ctdb, "%s/nodes", ctdb_base);
290 if (ctdb->nodes_file == NULL) {
291 DBG_ERR(" Out of memory\n");
294 ctdb_load_nodes_file(ctdb);
297 * Database setup/options
300 ctdb->db_directory = ctdb_config.dbdir_volatile;
301 ctdb->db_directory_persistent = ctdb_config.dbdir_persistent;
302 ctdb->db_directory_state = ctdb_config.dbdir_state;
304 if (ctdb_config.lock_debug_script != NULL) {
305 ret = setenv("CTDB_DEBUG_LOCKS",
306 ctdb_config.lock_debug_script,
309 D_ERR("Failed to set up lock debugging (%s)\n",
316 * Event setup/options
318 if (ctdb_config.event_debug_script != NULL) {
319 ret = setenv("CTDB_DEBUG_HUNG_SCRIPT",
320 ctdb_config.event_debug_script,
323 D_ERR("Failed to set up event script debugging (%s)\n",
330 * Legacy setup/options
333 ctdb->start_as_disabled = (int)ctdb_config.start_as_disabled;
334 ctdb->start_as_stopped = (int)ctdb_config.start_as_stopped;
336 /* set ctdbd capabilities */
337 if (!ctdb_config.lmaster_capability) {
338 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
340 if (!ctdb_config.recmaster_capability) {
341 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
344 ctdb->do_setsched = !ctdb_config.no_realtime;
347 * Miscellaneous setup
350 ctdb_tunables_set_defaults(ctdb);
352 ctdb->event_script_dir = talloc_asprintf(ctdb,
355 if (ctdb->event_script_dir == NULL) {
356 DBG_ERR("Out of memory\n");
360 ctdb->notification_script = talloc_asprintf(ctdb,
363 if (ctdb->notification_script == NULL) {
364 D_ERR("Unable to set notification script\n");
369 * Testing and debug options
372 /* Environment variable overrides default */
373 ctdbd_pidfile = getenv("CTDB_PIDFILE");
374 if (ctdbd_pidfile == NULL) {
375 ctdbd_pidfile = CTDB_RUNDIR "/ctdbd.pid";
378 /* Environment variable overrides default */
379 ctdb_socket = getenv("CTDB_SOCKET");
380 if (ctdb_socket == NULL) {
381 ctdb_socket = CTDB_SOCKET;
383 ret = ctdb_set_socketname(ctdb, ctdb_socket);
385 D_ERR("ctdb_set_socketname() failed\n");
389 t = getenv("CTDB_TEST_MODE");
391 ctdb->do_setsched = false;
392 ctdb->do_checkpublicip = false;
396 /* start the protocol running (as a child) */
397 return ctdb_start_daemon(ctdb, interactive?false:true);