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