.gitlab-ci-main.yml: Add safe.directory '*'
[Samba.git] / ctdb / server / ctdbd.c
blob67311c6a5da8274ccb9c0685943d3818f389eb08
1 /*
2 standalone ctdb daemon
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/>.
20 #include "replace.h"
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"
27 #include <popt.h>
28 #include <talloc.h>
29 /* Allow use of deprecated function tevent_loop_allow_nesting() */
30 #define TEVENT_DEPRECATED
31 #include <tevent.h>
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/path.h"
42 #include "common/logging.h"
44 #include "conf/logging_conf.h"
45 #include "conf/ctdb_config.h"
47 int script_log_level;
48 bool fast_start;
51 called by the transport layer when a packet comes in
53 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
55 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
57 CTDB_INCREMENT_STAT(ctdb, node_packets_recv);
59 /* up the counter for this source node, so we know its alive */
60 if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
61 /* as a special case, redirected calls don't increment the rx_cnt */
62 if (hdr->operation != CTDB_REQ_CALL ||
63 ((struct ctdb_req_call_old *)hdr)->hopcount == 0) {
64 ctdb->nodes[hdr->srcnode]->rx_cnt++;
68 ctdb_input_pkt(ctdb, hdr);
71 static const struct ctdb_upcalls ctdb_upcalls = {
72 .recv_pkt = ctdb_recv_pkt,
73 .node_dead = ctdb_node_dead,
74 .node_connected = ctdb_node_connected
77 static struct ctdb_context *ctdb_init(struct tevent_context *ev)
79 int ret;
80 struct ctdb_context *ctdb;
82 ctdb = talloc_zero(ev, struct ctdb_context);
83 if (ctdb == NULL) {
84 DBG_ERR("Memory error\n");
85 return NULL;
87 ctdb->ev = ev;
89 /* Wrap early to exercise code. */
90 ret = reqid_init(ctdb, INT_MAX-200, &ctdb->idr);
91 if (ret != 0) {
92 D_ERR("reqid_init failed (%s)\n", strerror(ret));
93 talloc_free(ctdb);
94 return NULL;
97 ret = srvid_init(ctdb, &ctdb->srv);
98 if (ret != 0) {
99 D_ERR("srvid_init failed (%s)\n", strerror(ret));
100 talloc_free(ctdb);
101 return NULL;
104 ctdb->daemon.name = path_socket(ctdb, "ctdbd");
105 if (ctdb->daemon.name == NULL) {
106 DBG_ERR("Memory allocation error\n");
107 talloc_free(ctdb);
108 return NULL;
111 ctdbd_pidfile = path_pidfile(ctdb, "ctdbd");
112 if (ctdbd_pidfile == NULL) {
113 DBG_ERR("Memory allocation error\n");
114 talloc_free(ctdb);
115 return NULL;
118 gettimeofday(&ctdb->ctdbd_start_time, NULL);
120 gettimeofday(&ctdb->last_recovery_started, NULL);
121 gettimeofday(&ctdb->last_recovery_finished, NULL);
123 ctdb->recovery_mode = CTDB_RECOVERY_NORMAL;
125 ctdb->upcalls = &ctdb_upcalls;
127 ctdb->statistics.statistics_start_time = timeval_current();
129 ctdb->capabilities = CTDB_CAP_DEFAULT;
132 * Initialise this node's PNN to the unknown value. This will
133 * be set to the correct value by either ctdb_add_node() as
134 * part of loading the nodes file or by
135 * ctdb_tcp_listen_automatic() when the transport is
136 * initialised. At some point we should de-optimise this and
137 * pull it out into ctdb_start_daemon() so it is done clearly
138 * and only in one place.
140 ctdb->pnn = CTDB_UNKNOWN_PNN;
142 ctdb->do_checkpublicip = true;
144 return ctdb;
149 main program
151 int main(int argc, const char *argv[])
153 struct ctdb_context *ctdb = NULL;
154 int interactive_opt = 0;
155 bool interactive = false;
157 struct poptOption popt_options[] = {
158 POPT_AUTOHELP
159 { "interactive", 'i', POPT_ARG_NONE, &interactive_opt, 0,
160 "don't fork, log to stderr", NULL },
161 POPT_TABLEEND
163 int opt, ret;
164 const char **extra_argv;
165 poptContext pc;
166 struct tevent_context *ev;
167 const char *ctdb_base;
168 struct conf_context *conf;
169 const char *logging_location;
170 const char *test_mode;
171 bool ok;
173 setproctitle_init(argc, discard_const(argv), environ);
176 * Basic setup
179 talloc_enable_null_tracking();
181 fault_setup();
183 ev = tevent_context_init(NULL);
184 if (ev == NULL) {
185 fprintf(stderr, "tevent_context_init() failed\n");
186 exit(1);
188 tevent_loop_allow_nesting(ev);
190 ctdb = ctdb_init(ev);
191 if (ctdb == NULL) {
192 fprintf(stderr, "Failed to init ctdb\n");
193 exit(1);
196 /* Default value for CTDB_BASE - don't override */
197 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
198 ctdb_base = getenv("CTDB_BASE");
199 if (ctdb_base == NULL) {
200 D_ERR("CTDB_BASE not set\n");
201 exit(1);
205 * Command-line option handling
208 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
210 while ((opt = poptGetNextOpt(pc)) != -1) {
211 switch (opt) {
212 default:
213 fprintf(stderr, "Invalid option %s: %s\n",
214 poptBadOption(pc, 0), poptStrerror(opt));
215 goto fail;
219 /* If there are extra arguments then exit with usage message */
220 extra_argv = poptGetArgs(pc);
221 if (extra_argv) {
222 extra_argv++;
223 if (extra_argv[0]) {
224 poptPrintHelp(pc, stdout, 0);
225 goto fail;
229 interactive = (interactive_opt != 0);
232 * Configuration file handling
235 ret = ctdb_config_load(ctdb, &conf);
236 if (ret != 0) {
237 /* ctdb_config_load() logs the failure */
238 goto fail;
242 * Logging setup/options
245 test_mode = getenv("CTDB_TEST_MODE");
247 /* Log to stderr (ignoring configuration) when running as interactive */
248 if (interactive) {
249 logging_location = "file:";
250 setenv("CTDB_INTERACTIVE", "true", 1);
251 } else {
252 logging_location = logging_conf_location(conf);
255 if (strcmp(logging_location, "syslog") != 0 && test_mode == NULL) {
256 /* This can help when CTDB logging is misconfigured */
257 syslog(LOG_DAEMON|LOG_NOTICE,
258 "CTDB logging to location %s",
259 logging_location);
262 /* Initialize logging and set the debug level */
263 ok = ctdb_logging_init(ctdb,
264 logging_location,
265 logging_conf_log_level(conf));
266 if (!ok) {
267 goto fail;
269 setenv("CTDB_LOGGING", logging_location, 1);
270 setenv("CTDB_DEBUGLEVEL", debug_level_to_string(DEBUGLEVEL), 1);
272 script_log_level = debug_level_from_string(
273 ctdb_config.script_log_level);
275 D_NOTICE("CTDB starting on node\n");
278 * Cluster setup/options
281 ret = ctdb_set_transport(ctdb, ctdb_config.transport);
282 if (ret == -1) {
283 D_ERR("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb));
284 goto fail;
287 if (ctdb_config.cluster_lock != NULL) {
288 ctdb->recovery_lock = ctdb_config.cluster_lock;
289 } else if (ctdb_config.recovery_lock != NULL) {
290 ctdb->recovery_lock = ctdb_config.recovery_lock;
291 } else {
292 D_WARNING("Cluster lock not set\n");
295 /* tell ctdb what address to listen on */
296 if (ctdb_config.node_address) {
297 ret = ctdb_set_address(ctdb, ctdb_config.node_address);
298 if (ret == -1) {
299 D_ERR("ctdb_set_address failed - %s\n",
300 ctdb_errstr(ctdb));
301 goto fail;
305 /* tell ctdb what nodes are available */
306 ctdb->nodes_file = talloc_asprintf(ctdb, "%s/nodes", ctdb_base);
307 if (ctdb->nodes_file == NULL) {
308 DBG_ERR(" Out of memory\n");
309 goto fail;
311 ctdb_load_nodes_file(ctdb);
314 * Database setup/options
317 ctdb->db_directory = ctdb_config.dbdir_volatile;
318 ok = directory_exist(ctdb->db_directory);
319 if (! ok) {
320 D_ERR("Volatile database directory %s does not exist\n",
321 ctdb->db_directory);
322 goto fail;
325 ctdb->db_directory_persistent = ctdb_config.dbdir_persistent;
326 ok = directory_exist(ctdb->db_directory_persistent);
327 if (! ok) {
328 D_ERR("Persistent database directory %s does not exist\n",
329 ctdb->db_directory_persistent);
330 goto fail;
333 ctdb->db_directory_state = ctdb_config.dbdir_state;
334 ok = directory_exist(ctdb->db_directory_state);
335 if (! ok) {
336 D_ERR("State database directory %s does not exist\n",
337 ctdb->db_directory_state);
338 goto fail;
341 if (ctdb_config.lock_debug_script != NULL) {
342 ret = setenv("CTDB_DEBUG_LOCKS",
343 ctdb_config.lock_debug_script,
345 if (ret != 0) {
346 D_ERR("Failed to set up lock debugging (%s)\n",
347 strerror(errno));
348 goto fail;
353 * Legacy setup/options
356 ctdb->start_as_disabled = (int)ctdb_config.start_as_disabled;
357 ctdb->start_as_stopped = (int)ctdb_config.start_as_stopped;
359 /* set ctdbd capabilities */
360 if (!ctdb_config.lmaster_capability) {
361 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
363 if (!ctdb_config.leader_capability) {
364 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
367 ctdb->do_setsched = ctdb_config.realtime_scheduling;
370 * Miscellaneous setup
373 ctdb_tunables_load(ctdb);
375 ctdb->event_script_dir = talloc_asprintf(ctdb,
376 "%s/events/legacy",
377 ctdb_base);
378 if (ctdb->event_script_dir == NULL) {
379 DBG_ERR("Out of memory\n");
380 goto fail;
383 ctdb->notification_script = talloc_asprintf(ctdb,
384 "%s/notify.sh",
385 ctdb_base);
386 if (ctdb->notification_script == NULL) {
387 D_ERR("Unable to set notification script\n");
388 goto fail;
392 * Testing and debug options
395 if (test_mode != NULL) {
396 ctdb->do_setsched = false;
397 ctdb->do_checkpublicip = false;
398 fast_start = true;
401 /* start the protocol running (as a child) */
402 return ctdb_start_daemon(ctdb, interactive, test_mode != NULL);
404 fail:
405 talloc_free(ctdb);
406 exit(1);