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 "lib/events/events.h"
22 #include "system/filesys.h"
24 #include "system/wait.h"
26 #include "../include/ctdb_private.h"
28 static void block_signal(int signum)
32 memset(&act, 0, sizeof(act));
34 act.sa_handler = SIG_IGN;
35 sigemptyset(&act.sa_mask);
36 sigaddset(&act.sa_mask, signum);
37 sigaction(signum, &act, NULL);
42 const char *transport;
43 const char *myaddress;
44 const char *public_address_list;
45 const char *event_script_dir;
47 const char *recovery_lock_file;
49 const char *db_dir_persistent;
50 const char *public_interface;
51 const char *single_public_ip;
54 .nlist = ETCDIR "/ctdb/nodes",
56 .event_script_dir = ETCDIR "/ctdb/events.d",
57 .logfile = VARDIR "/log/log.ctdb",
58 .db_dir = VARDIR "/ctdb",
59 .db_dir_persistent = VARDIR "/ctdb/persistent",
64 called by the transport layer when a packet comes in
66 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
68 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
70 ctdb->statistics.node_packets_recv++;
72 /* up the counter for this source node, so we know its alive */
73 if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
74 /* as a special case, redirected calls don't increment the rx_cnt */
75 if (hdr->operation != CTDB_REQ_CALL ||
76 ((struct ctdb_req_call *)hdr)->hopcount == 0) {
77 ctdb->nodes[hdr->srcnode]->rx_cnt++;
81 ctdb_input_pkt(ctdb, hdr);
86 static const struct ctdb_upcalls ctdb_upcalls = {
87 .recv_pkt = ctdb_recv_pkt,
88 .node_dead = ctdb_node_dead,
89 .node_connected = ctdb_node_connected
97 int main(int argc, const char *argv[])
99 struct ctdb_context *ctdb;
102 struct poptOption popt_options[] = {
105 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
106 { "public-addresses", 0, POPT_ARG_STRING, &options.public_address_list, 0, "public address list file", "filename" },
107 { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"},
108 { "single-public-ip", 0, POPT_ARG_STRING, &options.single_public_ip, 0, "single public ip", "ip-address"},
109 { "event-script-dir", 0, POPT_ARG_STRING, &options.event_script_dir, 0, "event script directory", "dirname" },
110 { "logfile", 0, POPT_ARG_STRING, &options.logfile, 0, "log file location", "filename" },
111 { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "filename" },
112 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
113 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
114 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
115 { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
116 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock_file, 0, "location of recovery lock file", "filename" },
117 { "nosetsched", 0, POPT_ARG_NONE, &options.no_setsched, 0, "disable setscheduler SCHED_FIFO call", NULL },
121 const char **extra_argv;
124 struct event_context *ev;
126 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
128 while ((opt = poptGetNextOpt(pc)) != -1) {
131 fprintf(stderr, "Invalid option %s: %s\n",
132 poptBadOption(pc, 0), poptStrerror(opt));
137 /* setup the remaining options for the main program to use */
138 extra_argv = poptGetArgs(pc);
141 while (extra_argv[extra_argc]) extra_argc++;
144 if (!options.recovery_lock_file) {
145 DEBUG(0,("You must specifiy the location of a recovery lock file with --reclock\n"));
149 block_signal(SIGPIPE);
151 ev = event_context_init(NULL);
153 ctdb = ctdb_cmdline_init(ev);
155 ret = ctdb_set_logfile(ctdb, options.logfile);
157 printf("ctdb_set_logfile to %s failed - %s\n", options.logfile, ctdb_errstr(ctdb));
161 DEBUG(0,("Starting CTDB daemon\n"));
163 ctdb->recovery_mode = CTDB_RECOVERY_NORMAL;
164 ctdb->recovery_master = (uint32_t)-1;
165 ctdb->upcalls = &ctdb_upcalls;
166 ctdb->idr = idr_init(ctdb);
167 ctdb->recovery_lock_fd = -1;
168 ctdb->monitoring_mode = CTDB_MONITORING_ACTIVE;
170 ctdb_tunables_set_defaults(ctdb);
172 ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
174 DEBUG(0,("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb)));
178 ret = ctdb_set_transport(ctdb, options.transport);
180 DEBUG(0,("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb)));
184 /* tell ctdb what address to listen on */
185 if (options.myaddress) {
186 ret = ctdb_set_address(ctdb, options.myaddress);
188 DEBUG(0,("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb)));
193 /* tell ctdb what nodes are available */
194 ret = ctdb_set_nlist(ctdb, options.nlist);
196 DEBUG(0,("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb)));
200 if (options.db_dir) {
201 ret = ctdb_set_tdb_dir(ctdb, options.db_dir);
203 DEBUG(0,("ctdb_set_tdb_dir failed - %s\n", ctdb_errstr(ctdb)));
207 if (options.db_dir_persistent) {
208 ret = ctdb_set_tdb_dir_persistent(ctdb, options.db_dir_persistent);
210 DEBUG(0,("ctdb_set_tdb_dir_persistent failed - %s\n", ctdb_errstr(ctdb)));
215 if (options.public_interface) {
216 ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
217 CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
220 if (options.single_public_ip) {
221 struct ctdb_vnn *svnn;
223 if (options.public_interface == NULL) {
224 DEBUG(0,("--single_public_ip used but --public_interface is not specified. You must specify the public interface when using single public ip. Exiting\n"));
228 svnn = talloc_zero(ctdb, struct ctdb_vnn);
229 CTDB_NO_MEMORY(ctdb, svnn);
231 ctdb->single_ip_vnn = svnn;
232 svnn->iface = talloc_strdup(svnn, options.public_interface);
233 CTDB_NO_MEMORY(ctdb, svnn->iface);
235 if (inet_aton(options.single_public_ip,
236 &svnn->public_address.sin_addr) == 0) {
237 DEBUG(0,("Invalid --single-public-ip argument : %s . This is not a valid ip address. Exiting.\n", options.single_public_ip));
240 svnn->public_address.sin_family = AF_INET;
241 svnn->public_address.sin_port = 0;
244 if (options.public_address_list) {
245 ret = ctdb_set_public_addresses(ctdb, options.public_address_list);
247 DEBUG(0,("Unable to setup public address list\n"));
252 ret = ctdb_set_event_script_dir(ctdb, options.event_script_dir);
254 DEBUG(0,("Unable to setup event script directory\n"));
258 /* useful default logfile */
259 if (ctdb->logfile == NULL) {
260 char *name = talloc_asprintf(ctdb, "%s/log.ctdb.pnn%u",
262 ctdb_set_logfile(ctdb, name);
266 ctdb->do_setsched = !options.no_setsched;
268 /* setup a environment variable for the event scripts to use to find the
269 installation directory */
270 setenv("CTDB_BASE", ETCDIR "/ctdb", 1);
272 /* start the protocol running (as a child) */
273 return ctdb_start_daemon(ctdb, interactive?False:True);