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