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