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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
25 #include "system/wait.h"
27 #include "../include/ctdb_private.h"
29 static void block_signal(int signum)
33 memset(&act, 0, sizeof(act));
35 act.sa_handler = SIG_IGN;
36 sigemptyset(&act.sa_mask);
37 sigaddset(&act.sa_mask, signum);
38 sigaction(signum, &act, NULL);
43 const char *transport;
44 const char *myaddress;
45 const char *public_address_list;
46 const char *public_interface;
47 const char *event_script;
49 const char *recovery_lock_file;
52 .nlist = ETCDIR "/ctdb/nodes",
54 .event_script = ETCDIR "/ctdb/events",
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_vnn(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", 0, POPT_ARG_STRING, &options.event_script, 0, "event script", "filename" },
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" },
115 const char **extra_argv;
118 struct event_context *ev;
120 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
122 while ((opt = poptGetNextOpt(pc)) != -1) {
125 fprintf(stderr, "Invalid option %s: %s\n",
126 poptBadOption(pc, 0), poptStrerror(opt));
131 /* setup the remaining options for the main program to use */
132 extra_argv = poptGetArgs(pc);
135 while (extra_argv[extra_argc]) extra_argc++;
138 if (!options.recovery_lock_file) {
139 DEBUG(0,("You must specifiy the location of a recovery lock file with --reclock\n"));
143 block_signal(SIGPIPE);
145 ev = event_context_init(NULL);
147 ctdb = ctdb_cmdline_init(ev);
149 ctdb->recovery_mode = CTDB_RECOVERY_NORMAL;
150 ctdb->recovery_master = (uint32_t)-1;
151 ctdb->upcalls = &ctdb_upcalls;
152 ctdb->idr = idr_init(ctdb);
153 ctdb->recovery_lock_fd = -1;
154 ctdb->monitoring_mode = CTDB_MONITORING_ACTIVE;
156 ctdb_tunables_set_defaults(ctdb);
158 ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
160 printf("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb));
164 ret = ctdb_set_transport(ctdb, options.transport);
166 printf("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb));
170 /* tell ctdb what address to listen on */
171 if (options.myaddress) {
172 ret = ctdb_set_address(ctdb, options.myaddress);
174 printf("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb));
179 /* tell ctdb what nodes are available */
180 ret = ctdb_set_nlist(ctdb, options.nlist);
182 printf("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb));
186 if (options.db_dir) {
187 ret = ctdb_set_tdb_dir(ctdb, options.db_dir);
189 printf("ctdb_set_tdb_dir failed - %s\n", ctdb_errstr(ctdb));
194 ret = ctdb_set_logfile(ctdb, options.logfile);
196 printf("ctdb_set_logfile to %s failed - %s\n", options.logfile, ctdb_errstr(ctdb));
200 if (options.public_interface) {
201 ctdb->takeover.interface = talloc_strdup(ctdb, options.public_interface);
202 CTDB_NO_MEMORY(ctdb, ctdb->takeover.interface);
205 if (options.public_address_list) {
206 ret = ctdb_set_public_addresses(ctdb, options.public_address_list);
208 printf("Unable to setup public address list\n");
211 ctdb->takeover.enabled = true;
214 ret = ctdb_set_event_script(ctdb, options.event_script);
216 printf("Unable to setup event script\n");
220 /* useful default logfile */
221 if (ctdb->logfile == NULL) {
222 char *name = talloc_asprintf(ctdb, "%s/log.ctdb.vnn%u",
224 ctdb_set_logfile(ctdb, name);
228 /* start the protocol running (as a child) */
229 return ctdb_start_daemon(ctdb, interactive?False:True);