Merge Samba3 and Samba4 together
[amitay/samba.git] / source4 / cluster / ctdb / 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 *public_interface;
46         const char *event_script;
47         const char *logfile;
48         const char *recovery_lock_file;
49         const char *db_dir;
50 } options = {
51         .nlist = ETCDIR "/ctdb/nodes",
52         .transport = "tcp",
53         .event_script = ETCDIR "/ctdb/events",
54         .logfile = VARDIR "/log/log.ctdb",
55         .db_dir = VARDIR "/ctdb",
56 };
57
58
59 /*
60   called by the transport layer when a packet comes in
61 */
62 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
63 {
64         struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
65
66         ctdb->statistics.node_packets_recv++;
67
68         /* up the counter for this source node, so we know its alive */
69         if (ctdb_validate_vnn(ctdb, hdr->srcnode)) {
70                 /* as a special case, redirected calls don't increment the rx_cnt */
71                 if (hdr->operation != CTDB_REQ_CALL ||
72                     ((struct ctdb_req_call *)hdr)->hopcount == 0) {
73                         ctdb->nodes[hdr->srcnode]->rx_cnt++;
74                 }
75         }
76
77         ctdb_input_pkt(ctdb, hdr);
78 }
79
80
81
82 static const struct ctdb_upcalls ctdb_upcalls = {
83         .recv_pkt       = ctdb_recv_pkt,
84         .node_dead      = ctdb_node_dead,
85         .node_connected = ctdb_node_connected
86 };
87
88
89
90 /*
91   main program
92 */
93 int main(int argc, const char *argv[])
94 {
95         struct ctdb_context *ctdb;
96         int interactive = 0;
97
98         struct poptOption popt_options[] = {
99                 POPT_AUTOHELP
100                 POPT_CTDB_CMDLINE
101                 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
102                 { "public-addresses", 0, POPT_ARG_STRING, &options.public_address_list, 0, "public address list file", "filename" },
103                 { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"},
104                 { "event-script", 0, POPT_ARG_STRING, &options.event_script, 0, "event script", "filename" },
105                 { "logfile", 0, POPT_ARG_STRING, &options.logfile, 0, "log file location", "filename" },
106                 { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "filename" },
107                 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
108                 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
109                 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
110                 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock_file, 0, "location of recovery lock file", "filename" },
111                 POPT_TABLEEND
112         };
113         int opt, ret;
114         const char **extra_argv;
115         int extra_argc = 0;
116         poptContext pc;
117         struct event_context *ev;
118
119         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
120
121         while ((opt = poptGetNextOpt(pc)) != -1) {
122                 switch (opt) {
123                 default:
124                         fprintf(stderr, "Invalid option %s: %s\n", 
125                                 poptBadOption(pc, 0), poptStrerror(opt));
126                         exit(1);
127                 }
128         }
129
130         /* setup the remaining options for the main program to use */
131         extra_argv = poptGetArgs(pc);
132         if (extra_argv) {
133                 extra_argv++;
134                 while (extra_argv[extra_argc]) extra_argc++;
135         }
136
137         if (!options.recovery_lock_file) {
138                 DEBUG(0,("You must specifiy the location of a recovery lock file with --reclock\n"));
139                 exit(1);
140         }
141
142         block_signal(SIGPIPE);
143
144         ev = s4_event_context_init(NULL);
145
146         ctdb = ctdb_cmdline_init(ev);
147
148         ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
149         ctdb->recovery_master  = (uint32_t)-1;
150         ctdb->upcalls          = &ctdb_upcalls;
151         ctdb->idr              = idr_init(ctdb);
152         ctdb->recovery_lock_fd = -1;
153         ctdb->monitoring_mode  = CTDB_MONITORING_ACTIVE;
154
155         ctdb_tunables_set_defaults(ctdb);
156
157         ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
158         if (ret == -1) {
159                 printf("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb));
160                 exit(1);
161         }
162
163         ret = ctdb_set_transport(ctdb, options.transport);
164         if (ret == -1) {
165                 printf("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb));
166                 exit(1);
167         }
168
169         /* tell ctdb what address to listen on */
170         if (options.myaddress) {
171                 ret = ctdb_set_address(ctdb, options.myaddress);
172                 if (ret == -1) {
173                         printf("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb));
174                         exit(1);
175                 }
176         }
177
178         /* tell ctdb what nodes are available */
179         ret = ctdb_set_nlist(ctdb, options.nlist);
180         if (ret == -1) {
181                 printf("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb));
182                 exit(1);
183         }
184
185         if (options.db_dir) {
186                 ret = ctdb_set_tdb_dir(ctdb, options.db_dir);
187                 if (ret == -1) {
188                         printf("ctdb_set_tdb_dir failed - %s\n", ctdb_errstr(ctdb));
189                         exit(1);
190                 }
191         }
192
193         ret = ctdb_set_logfile(ctdb, options.logfile);
194         if (ret == -1) {
195                 printf("ctdb_set_logfile to %s failed - %s\n", options.logfile, ctdb_errstr(ctdb));
196                 exit(1);
197         }
198
199         if (options.public_interface) {
200                 ctdb->takeover.interface = talloc_strdup(ctdb, options.public_interface);
201                 CTDB_NO_MEMORY(ctdb, ctdb->takeover.interface);
202         }
203
204         if (options.public_address_list) {
205                 ret = ctdb_set_public_addresses(ctdb, options.public_address_list);
206                 if (ret == -1) {
207                         printf("Unable to setup public address list\n");
208                         exit(1);
209                 }
210                 ctdb->takeover.enabled = true;
211         }
212
213         ret = ctdb_set_event_script(ctdb, options.event_script);
214         if (ret == -1) {
215                 printf("Unable to setup event script\n");
216                 exit(1);
217         }
218
219         /* useful default logfile */
220         if (ctdb->logfile == NULL) {
221                 char *name = talloc_asprintf(ctdb, "%s/log.ctdb.vnn%u", 
222                                              VARDIR, ctdb->vnn);
223                 ctdb_set_logfile(ctdb, name);
224                 talloc_free(name);
225         }
226
227         /* start the protocol running (as a child) */
228         return ctdb_start_daemon(ctdb, interactive?False:True);
229 }