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/path.h"
42 #include "common/logging.h"
44 #include "conf/logging_conf.h"
45 #include "conf/cluster_conf.h"
46 #include "conf/ctdb_config.h"
52 called by the transport layer when a packet comes in
54 static void ctdb_recv_pkt(struct ctdb_context
*ctdb
, uint8_t *data
, uint32_t length
)
56 struct ctdb_req_header
*hdr
= (struct ctdb_req_header
*)data
;
58 CTDB_INCREMENT_STAT(ctdb
, node_packets_recv
);
60 /* up the counter for this source node, so we know its alive */
61 if (ctdb_validate_pnn(ctdb
, hdr
->srcnode
)) {
62 /* as a special case, redirected calls don't increment the rx_cnt */
63 if (hdr
->operation
!= CTDB_REQ_CALL
||
64 ((struct ctdb_req_call_old
*)hdr
)->hopcount
== 0) {
65 ctdb
->nodes
[hdr
->srcnode
]->rx_cnt
++;
69 ctdb_input_pkt(ctdb
, hdr
);
72 static const struct ctdb_upcalls ctdb_upcalls
= {
73 .recv_pkt
= ctdb_recv_pkt
,
74 .node_dead
= ctdb_node_dead
,
75 .node_connected
= ctdb_node_connected
78 static struct ctdb_context
*ctdb_init(struct tevent_context
*ev
)
81 struct ctdb_context
*ctdb
;
83 ctdb
= talloc_zero(ev
, struct ctdb_context
);
85 DBG_ERR("Memory error\n");
90 /* Wrap early to exercise code. */
91 ret
= reqid_init(ctdb
, INT_MAX
-200, &ctdb
->idr
);
93 D_ERR("reqid_init failed (%s)\n", strerror(ret
));
98 ret
= srvid_init(ctdb
, &ctdb
->srv
);
100 D_ERR("srvid_init failed (%s)\n", strerror(ret
));
105 ctdb
->daemon
.name
= path_socket(ctdb
, "ctdbd");
106 if (ctdb
->daemon
.name
== NULL
) {
107 DBG_ERR("Memory allocation error\n");
112 ctdbd_pidfile
= path_pidfile(ctdb
, "ctdbd");
113 if (ctdbd_pidfile
== NULL
) {
114 DBG_ERR("Memory allocation error\n");
119 gettimeofday(&ctdb
->ctdbd_start_time
, NULL
);
121 gettimeofday(&ctdb
->last_recovery_started
, NULL
);
122 gettimeofday(&ctdb
->last_recovery_finished
, NULL
);
124 ctdb
->recovery_mode
= CTDB_RECOVERY_NORMAL
;
126 ctdb
->upcalls
= &ctdb_upcalls
;
128 ctdb
->statistics
.statistics_start_time
= timeval_current();
130 ctdb
->capabilities
= CTDB_CAP_DEFAULT
;
133 * Initialise this node's PNN to the unknown value. This will
134 * be set to the correct value by either ctdb_add_node() as
135 * part of loading the nodes file or by
136 * ctdb_tcp_listen_automatic() when the transport is
137 * initialised. At some point we should de-optimise this and
138 * pull it out into ctdb_start_daemon() so it is done clearly
139 * and only in one place.
141 ctdb
->pnn
= CTDB_UNKNOWN_PNN
;
143 ctdb
->do_checkpublicip
= true;
152 int main(int argc
, const char *argv
[])
154 struct ctdb_context
*ctdb
= NULL
;
155 int interactive_opt
= 0;
156 bool interactive
= false;
158 struct poptOption popt_options
[] = {
160 { "interactive", 'i', POPT_ARG_NONE
, &interactive_opt
, 0,
161 "don't fork, log to stderr", NULL
},
165 const char **extra_argv
;
167 struct tevent_context
*ev
;
168 struct conf_context
*conf
;
169 const char *logging_location
;
170 const char *test_mode
;
173 setproctitle_init(argc
, discard_const(argv
), environ
);
179 talloc_enable_null_tracking();
183 ev
= tevent_context_init(NULL
);
185 fprintf(stderr
, "tevent_context_init() failed\n");
188 tevent_loop_allow_nesting(ev
);
190 ctdb
= ctdb_init(ev
);
192 fprintf(stderr
, "Failed to init ctdb\n");
196 /* Default value for CTDB_BASE - don't override */
197 ret
= setenv("CTDB_BASE", CTDB_ETCDIR
, 0);
199 D_ERR("Unable to set CTDB_BASE (errno=%d)\n", errno
);
204 * Command-line option handling
207 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
209 while ((opt
= poptGetNextOpt(pc
)) != -1) {
212 fprintf(stderr
, "Invalid option %s: %s\n",
213 poptBadOption(pc
, 0), poptStrerror(opt
));
218 /* If there are extra arguments then exit with usage message */
219 extra_argv
= poptGetArgs(pc
);
223 poptPrintHelp(pc
, stdout
, 0);
228 interactive
= (interactive_opt
!= 0);
231 * Configuration file handling
234 ret
= ctdb_config_load(ctdb
, &conf
, true);
236 /* ctdb_config_load() logs the failure */
241 * Logging setup/options
244 test_mode
= getenv("CTDB_TEST_MODE");
246 /* Log to stderr (ignoring configuration) when running as interactive */
248 logging_location
= "file:";
249 setenv("CTDB_INTERACTIVE", "true", 1);
251 logging_location
= logging_conf_location(conf
);
254 if (strcmp(logging_location
, "syslog") != 0 && test_mode
== NULL
) {
255 /* This can help when CTDB logging is misconfigured */
256 syslog(LOG_DAEMON
|LOG_NOTICE
,
257 "CTDB logging to location %s",
261 /* Initialize logging and set the debug level */
262 ok
= ctdb_logging_init(ctdb
,
264 logging_conf_log_level(conf
));
268 setenv("CTDB_LOGGING", logging_location
, 1);
269 setenv("CTDB_DEBUGLEVEL", debug_level_to_string(DEBUGLEVEL
), 1);
271 script_log_level
= debug_level_from_string(
272 ctdb_config
.script_log_level
);
274 D_NOTICE("CTDB starting on node\n");
277 * Cluster setup/options
280 ret
= ctdb_set_transport(ctdb
, ctdb_config
.transport
);
282 D_ERR("Failed to setup transport\n");
286 if (ctdb_config
.cluster_lock
!= NULL
) {
287 ctdb
->recovery_lock
= ctdb_config
.cluster_lock
;
288 } else if (ctdb_config
.recovery_lock
!= NULL
) {
289 ctdb
->recovery_lock
= ctdb_config
.recovery_lock
;
291 D_WARNING("Cluster lock not set\n");
294 /* tell ctdb what address to listen on */
295 if (ctdb_config
.node_address
) {
296 ret
= ctdb_set_address(ctdb
, ctdb_config
.node_address
);
298 D_ERR("Failed to set node address\n");
303 /* tell ctdb what nodes are available */
304 ctdb
->nodes_source
= cluster_conf_nodes_list(ctdb
, conf
);
305 if (ctdb
->nodes_source
== NULL
) {
306 DBG_ERR(" Out of memory\n");
309 ctdb_load_nodes(ctdb
);
312 * Database setup/options
315 ctdb
->db_directory
= ctdb_config
.dbdir_volatile
;
316 ok
= directory_exist(ctdb
->db_directory
);
318 D_ERR("Volatile database directory %s does not exist\n",
323 ctdb
->db_directory_persistent
= ctdb_config
.dbdir_persistent
;
324 ok
= directory_exist(ctdb
->db_directory_persistent
);
326 D_ERR("Persistent database directory %s does not exist\n",
327 ctdb
->db_directory_persistent
);
331 ctdb
->db_directory_state
= ctdb_config
.dbdir_state
;
332 ok
= directory_exist(ctdb
->db_directory_state
);
334 D_ERR("State database directory %s does not exist\n",
335 ctdb
->db_directory_state
);
339 if (ctdb_config
.lock_debug_script
!= NULL
) {
340 ret
= setenv("CTDB_DEBUG_LOCKS",
341 ctdb_config
.lock_debug_script
,
344 D_ERR("Failed to set up lock debugging (%s)\n",
351 * Legacy setup/options
354 ctdb
->start_as_disabled
= (int)ctdb_config
.start_as_disabled
;
355 ctdb
->start_as_stopped
= (int)ctdb_config
.start_as_stopped
;
357 /* set ctdbd capabilities */
358 if (!ctdb_config
.lmaster_capability
) {
359 ctdb
->capabilities
&= ~CTDB_CAP_LMASTER
;
361 if (!ctdb_config
.leader_capability
) {
362 ctdb
->capabilities
&= ~CTDB_CAP_RECMASTER
;
365 ctdb
->do_setsched
= ctdb_config
.realtime_scheduling
;
368 * Miscellaneous setup
371 ctdb_tunables_load(ctdb
);
373 ctdb
->event_script_dir
= path_etcdir_append(ctdb
, "events/legacy");
374 if (ctdb
->event_script_dir
== NULL
) {
375 DBG_ERR("Out of memory\n");
379 ctdb
->notification_script
= path_etcdir_append(ctdb
, "notify.sh");
380 if (ctdb
->notification_script
== NULL
) {
381 D_ERR("Unable to set notification script\n");
386 * Testing and debug options
389 if (test_mode
!= NULL
) {
390 ctdb
->do_setsched
= false;
391 ctdb
->do_checkpublicip
= false;
395 /* start the protocol running (as a child) */
396 return ctdb_start_daemon(ctdb
, interactive
, test_mode
!= NULL
);