Add a capabilities field to the ctdb structure
[metze/ctdb/wip.git] / server / ctdbd.c
1 /* 
2    standalone ctdb daemon
3
4    Copyright (C) Andrew Tridgell  2006
5
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.
10    
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.
15    
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/>.
18 */
19
20 #include "includes.h"
21 #include "lib/events/events.h"
22 #include "system/filesys.h"
23 #include "popt.h"
24 #include "system/time.h"
25 #include "system/wait.h"
26 #include "system/network.h"
27 #include "cmdline.h"
28 #include "../include/ctdb_private.h"
29
30 static struct {
31         const char *nlist;
32         const char *transport;
33         const char *myaddress;
34         const char *public_address_list;
35         const char *event_script_dir;
36         const char *logfile;
37         const char *recovery_lock_file;
38         const char *db_dir;
39         const char *db_dir_persistent;
40         const char *public_interface;
41         const char *single_public_ip;
42         const char *node_ip;
43         int         no_setsched;
44         int         use_syslog;
45         int         start_as_disabled;
46 } options = {
47         .nlist = ETCDIR "/ctdb/nodes",
48         .transport = "tcp",
49         .event_script_dir = ETCDIR "/ctdb/events.d",
50         .logfile = VARDIR "/log/log.ctdb",
51         .db_dir = VARDIR "/ctdb",
52         .db_dir_persistent = VARDIR "/ctdb/persistent",
53 };
54
55
56 /*
57   called by the transport layer when a packet comes in
58 */
59 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
60 {
61         struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
62
63         ctdb->statistics.node_packets_recv++;
64
65         /* up the counter for this source node, so we know its alive */
66         if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
67                 /* as a special case, redirected calls don't increment the rx_cnt */
68                 if (hdr->operation != CTDB_REQ_CALL ||
69                     ((struct ctdb_req_call *)hdr)->hopcount == 0) {
70                         ctdb->nodes[hdr->srcnode]->rx_cnt++;
71                 }
72         }
73
74         ctdb_input_pkt(ctdb, hdr);
75 }
76
77 void ctdb_load_nodes_file(struct ctdb_context *ctdb)
78 {
79         int ret;
80
81         ret = ctdb_set_nlist(ctdb, options.nlist);
82         if (ret == -1) {
83                 DEBUG(DEBUG_ALERT,("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb)));
84                 exit(1);
85         }
86 }
87
88 static const struct ctdb_upcalls ctdb_upcalls = {
89         .recv_pkt       = ctdb_recv_pkt,
90         .node_dead      = ctdb_node_dead,
91         .node_connected = ctdb_node_connected
92 };
93
94
95
96 /*
97   main program
98 */
99 int main(int argc, const char *argv[])
100 {
101         struct ctdb_context *ctdb;
102         int interactive = 0;
103
104         struct poptOption popt_options[] = {
105                 POPT_AUTOHELP
106                 POPT_CTDB_CMDLINE
107                 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
108                 { "public-addresses", 0, POPT_ARG_STRING, &options.public_address_list, 0, "public address list file", "filename" },
109                 { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"},
110                 { "single-public-ip", 0, POPT_ARG_STRING, &options.single_public_ip, 0, "single public ip", "ip-address"},
111                 { "event-script-dir", 0, POPT_ARG_STRING, &options.event_script_dir, 0, "event script directory", "dirname" },
112                 { "logfile", 0, POPT_ARG_STRING, &options.logfile, 0, "log file location", "filename" },
113                 { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "filename" },
114                 { "node-ip", 0, POPT_ARG_STRING, &options.node_ip, 0, "node ip", "ip-address"},
115                 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
116                 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
117                 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
118                 { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
119                 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock_file, 0, "location of recovery lock file", "filename" },
120                 { "nosetsched", 0, POPT_ARG_NONE, &options.no_setsched, 0, "disable setscheduler SCHED_FIFO call", NULL },
121                 { "syslog", 0, POPT_ARG_NONE, &options.use_syslog, 0, "log messages to syslog", NULL },
122                 { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
123                 POPT_TABLEEND
124         };
125         int opt, ret;
126         const char **extra_argv;
127         int extra_argc = 0;
128         poptContext pc;
129         struct event_context *ev;
130
131         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
132
133         while ((opt = poptGetNextOpt(pc)) != -1) {
134                 switch (opt) {
135                 default:
136                         fprintf(stderr, "Invalid option %s: %s\n", 
137                                 poptBadOption(pc, 0), poptStrerror(opt));
138                         exit(1);
139                 }
140         }
141
142         /* setup the remaining options for the main program to use */
143         extra_argv = poptGetArgs(pc);
144         if (extra_argv) {
145                 extra_argv++;
146                 while (extra_argv[extra_argc]) extra_argc++;
147         }
148
149         if (!options.recovery_lock_file) {
150                 DEBUG(DEBUG_ALERT,("You must specifiy the location of a recovery lock file with --reclock\n"));
151                 exit(1);
152         }
153
154         talloc_enable_null_tracking();
155
156         ctdb_block_signal(SIGPIPE);
157
158         ev = event_context_init(NULL);
159
160         ctdb = ctdb_cmdline_init(ev);
161
162         ctdb->start_as_disabled = options.start_as_disabled;
163
164         ret = ctdb_set_logfile(ctdb, options.logfile, options.use_syslog);
165         if (ret == -1) {
166                 printf("ctdb_set_logfile to %s failed - %s\n", 
167                        options.use_syslog?"syslog":options.logfile, ctdb_errstr(ctdb));
168                 exit(1);
169         }
170
171         DEBUG(DEBUG_NOTICE,("Starting CTDB daemon\n"));
172         gettimeofday(&ctdb->ctdbd_start_time, NULL);
173         gettimeofday(&ctdb->last_recovery_time, NULL);
174         ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
175         ctdb->recovery_master  = (uint32_t)-1;
176         ctdb->upcalls          = &ctdb_upcalls;
177         ctdb->idr              = idr_init(ctdb);
178         ctdb->recovery_lock_fd = -1;
179
180         ctdb_tunables_set_defaults(ctdb);
181
182         ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
183         if (ret == -1) {
184                 DEBUG(DEBUG_ALERT,("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb)));
185                 exit(1);
186         }
187
188         ret = ctdb_set_transport(ctdb, options.transport);
189         if (ret == -1) {
190                 DEBUG(DEBUG_ALERT,("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb)));
191                 exit(1);
192         }
193
194         /* tell ctdb what address to listen on */
195         if (options.myaddress) {
196                 ret = ctdb_set_address(ctdb, options.myaddress);
197                 if (ret == -1) {
198                         DEBUG(DEBUG_ALERT,("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb)));
199                         exit(1);
200                 }
201         }
202
203         /* set ctdbd capabilities */
204         /* We can be an lmaster, we can also be a recmaster */
205         ctdb->capabilities = CTDB_CAP_LMASTER | CTDB_CAP_RECMASTER;
206
207         /* tell ctdb what nodes are available */
208         ctdb_load_nodes_file(ctdb);
209
210         /* if a node-ip was specified, verify that it exists in the
211            nodes file
212         */
213         if (options.node_ip != NULL) {
214                 DEBUG(DEBUG_NOTICE,("IP for this node is %s\n", options.node_ip));
215                 ret = ctdb_ip_to_nodeid(ctdb, options.node_ip);
216                 if (ret == -1) {
217                         DEBUG(DEBUG_ALERT,("The specified node-ip:%s is not a valid node address. Exiting.\n", options.node_ip));
218                         exit(1);
219                 }
220                 ctdb->node_ip = options.node_ip;
221                 DEBUG(DEBUG_NOTICE,("This is node %d\n", ret));
222         }
223
224         if (options.db_dir) {
225                 ret = ctdb_set_tdb_dir(ctdb, options.db_dir);
226                 if (ret == -1) {
227                         DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir failed - %s\n", ctdb_errstr(ctdb)));
228                         exit(1);
229                 }
230         }
231         if (options.db_dir_persistent) {
232                 ret = ctdb_set_tdb_dir_persistent(ctdb, options.db_dir_persistent);
233                 if (ret == -1) {
234                         DEBUG(DEBUG_ALERT,("ctdb_set_tdb_dir_persistent failed - %s\n", ctdb_errstr(ctdb)));
235                         exit(1);
236                 }
237         }
238
239         if (options.public_interface) {
240                 ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
241                 CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
242         }
243
244         if (options.single_public_ip) {
245                 struct ctdb_vnn *svnn;
246
247                 if (options.public_interface == NULL) {
248                         DEBUG(DEBUG_ALERT,("--single_public_ip used but --public_interface is not specified. You must specify the public interface when using single public ip. Exiting\n"));
249                         exit(10);
250                 }
251
252                 svnn = talloc_zero(ctdb, struct ctdb_vnn);
253                 CTDB_NO_MEMORY(ctdb, svnn);
254
255                 ctdb->single_ip_vnn = svnn;
256                 svnn->iface = talloc_strdup(svnn, options.public_interface);
257                 CTDB_NO_MEMORY(ctdb, svnn->iface);
258
259                 if (inet_aton(options.single_public_ip, 
260                                 &svnn->public_address.sin_addr) == 0) {
261                         DEBUG(DEBUG_ALERT,("Invalid --single-public-ip argument : %s . This is not a valid ip address. Exiting.\n", options.single_public_ip));
262                         exit(10);
263                 }
264                 svnn->public_address.sin_family = AF_INET;
265                 svnn->public_address.sin_port   = 0;
266         }
267
268         if (options.public_address_list) {
269                 ret = ctdb_set_public_addresses(ctdb, options.public_address_list);
270                 if (ret == -1) {
271                         DEBUG(DEBUG_ALERT,("Unable to setup public address list\n"));
272                         exit(1);
273                 }
274         }
275
276         ret = ctdb_set_event_script_dir(ctdb, options.event_script_dir);
277         if (ret == -1) {
278                 DEBUG(DEBUG_ALERT,("Unable to setup event script directory\n"));
279                 exit(1);
280         }
281
282         ctdb->do_setsched = !options.no_setsched;
283
284         /* setup a environment variable for the event scripts to use to find the
285            installation directory */
286         setenv("CTDB_BASE", ETCDIR "/ctdb", 1);
287
288         /* start the protocol running (as a child) */
289         return ctdb_start_daemon(ctdb, interactive?False:True);
290 }