cope with non-standard install dirs in event scripts
[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 *public_interface;
50         int         no_setsched;
51 } options = {
52         .nlist = ETCDIR "/ctdb/nodes",
53         .transport = "tcp",
54         .event_script_dir = ETCDIR "/ctdb/events.d",
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_pnn(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-dir", 0, POPT_ARG_STRING, &options.event_script_dir, 0, "event script directory", "dirname" },
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                 { "nosetsched", 0, POPT_ARG_NONE, &options.no_setsched, 0, "disable setscheduler SCHED_FIFO call", NULL },
113                 POPT_TABLEEND
114         };
115         int opt, ret;
116         const char **extra_argv;
117         int extra_argc = 0;
118         poptContext pc;
119         struct event_context *ev;
120
121         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
122
123         while ((opt = poptGetNextOpt(pc)) != -1) {
124                 switch (opt) {
125                 default:
126                         fprintf(stderr, "Invalid option %s: %s\n", 
127                                 poptBadOption(pc, 0), poptStrerror(opt));
128                         exit(1);
129                 }
130         }
131
132         /* setup the remaining options for the main program to use */
133         extra_argv = poptGetArgs(pc);
134         if (extra_argv) {
135                 extra_argv++;
136                 while (extra_argv[extra_argc]) extra_argc++;
137         }
138
139         if (!options.recovery_lock_file) {
140                 DEBUG(0,("You must specifiy the location of a recovery lock file with --reclock\n"));
141                 exit(1);
142         }
143
144         block_signal(SIGPIPE);
145
146         ev = event_context_init(NULL);
147
148         ctdb = ctdb_cmdline_init(ev);
149
150         ret = ctdb_set_logfile(ctdb, options.logfile);
151         if (ret == -1) {
152                 printf("ctdb_set_logfile to %s failed - %s\n", options.logfile, ctdb_errstr(ctdb));
153                 exit(1);
154         }
155
156         DEBUG(0,("Starting CTDB daemon\n"));
157
158         ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
159         ctdb->recovery_master  = (uint32_t)-1;
160         ctdb->upcalls          = &ctdb_upcalls;
161         ctdb->idr              = idr_init(ctdb);
162         ctdb->recovery_lock_fd = -1;
163         ctdb->monitoring_mode  = CTDB_MONITORING_ACTIVE;
164
165         ctdb_tunables_set_defaults(ctdb);
166
167         ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
168         if (ret == -1) {
169                 DEBUG(0,("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb)));
170                 exit(1);
171         }
172
173         ret = ctdb_set_transport(ctdb, options.transport);
174         if (ret == -1) {
175                 DEBUG(0,("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb)));
176                 exit(1);
177         }
178
179         /* tell ctdb what address to listen on */
180         if (options.myaddress) {
181                 ret = ctdb_set_address(ctdb, options.myaddress);
182                 if (ret == -1) {
183                         DEBUG(0,("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb)));
184                         exit(1);
185                 }
186         }
187
188         /* tell ctdb what nodes are available */
189         ret = ctdb_set_nlist(ctdb, options.nlist);
190         if (ret == -1) {
191                 DEBUG(0,("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb)));
192                 exit(1);
193         }
194
195         if (options.db_dir) {
196                 ret = ctdb_set_tdb_dir(ctdb, options.db_dir);
197                 if (ret == -1) {
198                         DEBUG(0,("ctdb_set_tdb_dir failed - %s\n", ctdb_errstr(ctdb)));
199                         exit(1);
200                 }
201         }
202
203         if (options.public_interface) {
204                 ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
205                 CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
206         }
207
208         if (options.public_address_list) {
209                 ret = ctdb_set_public_addresses(ctdb, options.public_address_list);
210                 if (ret == -1) {
211                         DEBUG(0,("Unable to setup public address list\n"));
212                         exit(1);
213                 }
214         }
215
216         ret = ctdb_set_event_script_dir(ctdb, options.event_script_dir);
217         if (ret == -1) {
218                 DEBUG(0,("Unable to setup event script directory\n"));
219                 exit(1);
220         }
221
222         /* useful default logfile */
223         if (ctdb->logfile == NULL) {
224                 char *name = talloc_asprintf(ctdb, "%s/log.ctdb.pnn%u", 
225                                              VARDIR, ctdb->pnn);
226                 ctdb_set_logfile(ctdb, name);
227                 talloc_free(name);
228         }
229
230         ctdb->do_setsched = !options.no_setsched;
231
232         /* setup a environment variable for the event scripts to use to find the
233            installation directory */
234         setenv("CTDB_BASE", ETCDIR "/ctdb", 1);
235
236         /* start the protocol running (as a child) */
237         return ctdb_start_daemon(ctdb, interactive?False:True);
238 }