later times are a lower priority, not a higher priority
[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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
24 #include "popt.h"
25 #include "system/wait.h"
26 #include "cmdline.h"
27 #include "../include/ctdb_private.h"
28
29 static void block_signal(int signum)
30 {
31         struct sigaction act;
32
33         memset(&act, 0, sizeof(act));
34
35         act.sa_handler = SIG_IGN;
36         sigemptyset(&act.sa_mask);
37         sigaddset(&act.sa_mask, signum);
38         sigaction(signum, &act, NULL);
39 }
40
41 static struct {
42         const char *nlist;
43         const char *transport;
44         const char *myaddress;
45         const char *public_address_list;
46         const char *public_interface;
47         const char *event_script;
48         const char *logfile;
49         const char *recovery_lock_file;
50         const char *db_dir;
51 } options = {
52         .nlist = ETCDIR "/ctdb/nodes",
53         .transport = "tcp",
54         .event_script = ETCDIR "/ctdb/events",
55         .logfile = VARDIR "/log/log.ctdb",
56         .db_dir = VARDIR "/ctdb",
57 };
58
59
60 /*
61   called by the transport layer when a packet comes in
62 */
63 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
64 {
65         struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
66
67         ctdb->statistics.node_packets_recv++;
68
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++;
75                 }
76         }
77
78         ctdb_input_pkt(ctdb, hdr);
79 }
80
81
82
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
87 };
88
89
90
91 /*
92   main program
93 */
94 int main(int argc, const char *argv[])
95 {
96         struct ctdb_context *ctdb;
97         int interactive = 0;
98
99         struct poptOption popt_options[] = {
100                 POPT_AUTOHELP
101                 POPT_CTDB_CMDLINE
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" },
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(0,("You must specifiy the location of a recovery lock file with --reclock\n"));
140                 exit(1);
141         }
142
143         block_signal(SIGPIPE);
144
145         ev = event_context_init(NULL);
146
147         ctdb = ctdb_cmdline_init(ev);
148
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;
155
156         ctdb_tunables_set_defaults(ctdb);
157
158         ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
159         if (ret == -1) {
160                 printf("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb));
161                 exit(1);
162         }
163
164         ret = ctdb_set_transport(ctdb, options.transport);
165         if (ret == -1) {
166                 printf("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb));
167                 exit(1);
168         }
169
170         /* tell ctdb what address to listen on */
171         if (options.myaddress) {
172                 ret = ctdb_set_address(ctdb, options.myaddress);
173                 if (ret == -1) {
174                         printf("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb));
175                         exit(1);
176                 }
177         }
178
179         /* tell ctdb what nodes are available */
180         ret = ctdb_set_nlist(ctdb, options.nlist);
181         if (ret == -1) {
182                 printf("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb));
183                 exit(1);
184         }
185
186         if (options.db_dir) {
187                 ret = ctdb_set_tdb_dir(ctdb, options.db_dir);
188                 if (ret == -1) {
189                         printf("ctdb_set_tdb_dir failed - %s\n", ctdb_errstr(ctdb));
190                         exit(1);
191                 }
192         }
193
194         ret = ctdb_set_logfile(ctdb, options.logfile);
195         if (ret == -1) {
196                 printf("ctdb_set_logfile to %s failed - %s\n", options.logfile, ctdb_errstr(ctdb));
197                 exit(1);
198         }
199
200         if (options.public_interface) {
201                 ctdb->takeover.interface = talloc_strdup(ctdb, options.public_interface);
202                 CTDB_NO_MEMORY(ctdb, ctdb->takeover.interface);
203         }
204
205         if (options.public_address_list) {
206                 ret = ctdb_set_public_addresses(ctdb, options.public_address_list);
207                 if (ret == -1) {
208                         printf("Unable to setup public address list\n");
209                         exit(1);
210                 }
211                 ctdb->takeover.enabled = true;
212         }
213
214         ret = ctdb_set_event_script(ctdb, options.event_script);
215         if (ret == -1) {
216                 printf("Unable to setup event script\n");
217                 exit(1);
218         }
219
220         /* useful default logfile */
221         if (ctdb->logfile == NULL) {
222                 char *name = talloc_asprintf(ctdb, "%s/log.ctdb.vnn%u", 
223                                              VARDIR, ctdb->vnn);
224                 ctdb_set_logfile(ctdb, name);
225                 talloc_free(name);
226         }
227
228         /* start the protocol running (as a child) */
229         return ctdb_start_daemon(ctdb, interactive?False:True);
230 }