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 *public_interface;
52 .nlist = ETCDIR "/ctdb/nodes",
54 .event_script_dir = ETCDIR "/ctdb/events.d",
55 .logfile = VARDIR "/log/log.ctdb",
56 .db_dir = VARDIR "/ctdb",
61 called by the transport layer when a packet comes in
63 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
65 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
67 ctdb->statistics.node_packets_recv++;
69 /* up the counter for this source node, so we know its alive */
70 if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
71 /* as a special case, redirected calls don't increment the rx_cnt */
72 if (hdr->operation != CTDB_REQ_CALL ||
73 ((struct ctdb_req_call *)hdr)->hopcount == 0) {
74 ctdb->nodes[hdr->srcnode]->rx_cnt++;
78 ctdb_input_pkt(ctdb, hdr);
83 static const struct ctdb_upcalls ctdb_upcalls = {
84 .recv_pkt = ctdb_recv_pkt,
85 .node_dead = ctdb_node_dead,
86 .node_connected = ctdb_node_connected
94 int main(int argc, const char *argv[])
96 struct ctdb_context *ctdb;
99 struct poptOption popt_options[] = {
102 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
103 { "public-addresses", 0, POPT_ARG_STRING, &options.public_address_list, 0, "public address list file", "filename" },
104 { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"},
105 { "event-script-dir", 0, POPT_ARG_STRING, &options.event_script_dir, 0, "event script directory", "dirname" },
106 { "logfile", 0, POPT_ARG_STRING, &options.logfile, 0, "log file location", "filename" },
107 { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "filename" },
108 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
109 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
110 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
111 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock_file, 0, "location of recovery lock file", "filename" },
112 { "nosetsched", 0, POPT_ARG_NONE, &options.no_setsched, 0, "disable setscheduler SCHED_FIFO call", NULL },
116 const char **extra_argv;
119 struct event_context *ev;
121 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
123 while ((opt = poptGetNextOpt(pc)) != -1) {
126 fprintf(stderr, "Invalid option %s: %s\n",
127 poptBadOption(pc, 0), poptStrerror(opt));
132 /* setup the remaining options for the main program to use */
133 extra_argv = poptGetArgs(pc);
136 while (extra_argv[extra_argc]) extra_argc++;
139 if (!options.recovery_lock_file) {
140 DEBUG(0,("You must specifiy the location of a recovery lock file with --reclock\n"));
144 block_signal(SIGPIPE);
146 ev = event_context_init(NULL);
148 ctdb = ctdb_cmdline_init(ev);
150 ret = ctdb_set_logfile(ctdb, options.logfile);
152 printf("ctdb_set_logfile to %s failed - %s\n", options.logfile, ctdb_errstr(ctdb));
156 DEBUG(0,("Starting CTDB daemon\n"));
158 ctdb->recovery_mode = CTDB_RECOVERY_NORMAL;
159 ctdb->recovery_master = (uint32_t)-1;
160 ctdb->upcalls = &ctdb_upcalls;
161 ctdb->idr = idr_init(ctdb);
162 ctdb->recovery_lock_fd = -1;
163 ctdb->monitoring_mode = CTDB_MONITORING_ACTIVE;
165 ctdb_tunables_set_defaults(ctdb);
167 ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
169 DEBUG(0,("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb)));
173 ret = ctdb_set_transport(ctdb, options.transport);
175 DEBUG(0,("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb)));
179 /* tell ctdb what address to listen on */
180 if (options.myaddress) {
181 ret = ctdb_set_address(ctdb, options.myaddress);
183 DEBUG(0,("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb)));
188 /* tell ctdb what nodes are available */
189 ret = ctdb_set_nlist(ctdb, options.nlist);
191 DEBUG(0,("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb)));
195 if (options.db_dir) {
196 ret = ctdb_set_tdb_dir(ctdb, options.db_dir);
198 DEBUG(0,("ctdb_set_tdb_dir failed - %s\n", ctdb_errstr(ctdb)));
203 if (options.public_interface) {
204 ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
205 CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
208 if (options.public_address_list) {
209 ret = ctdb_set_public_addresses(ctdb, options.public_address_list);
211 DEBUG(0,("Unable to setup public address list\n"));
216 ret = ctdb_set_event_script_dir(ctdb, options.event_script_dir);
218 DEBUG(0,("Unable to setup event script directory\n"));
222 /* useful default logfile */
223 if (ctdb->logfile == NULL) {
224 char *name = talloc_asprintf(ctdb, "%s/log.ctdb.pnn%u",
226 ctdb_set_logfile(ctdb, name);
230 ctdb->do_setsched = !options.no_setsched;
232 /* setup a environment variable for the event scripts to use to find the
233 installation directory */
234 setenv("CTDB_BASE", ETCDIR "/ctdb", 1);
236 /* start the protocol running (as a child) */
237 return ctdb_start_daemon(ctdb, interactive?False:True);