ctdb-daemon: Drop most ctdbd command-line options
[metze/samba/wip.git] / 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 "replace.h"
21 #include "system/filesys.h"
22 #include "system/time.h"
23 #include "system/wait.h"
24 #include "system/network.h"
25 #include "system/syslog.h"
26
27 #include <popt.h>
28 #include <talloc.h>
29 /* Allow use of deprecated function tevent_loop_allow_nesting() */
30 #define TEVENT_DEPRECATED
31 #include <tevent.h>
32
33 #include "lib/util/debug.h"
34 #include "lib/util/samba_util.h"
35
36 #include "ctdb_private.h"
37
38 #include "common/reqid.h"
39 #include "common/system.h"
40 #include "common/common.h"
41 #include "common/logging.h"
42 #include "common/logging_conf.h"
43
44 #include "ctdb_config.h"
45
46 int script_log_level;
47 bool fast_start;
48
49 /*
50   called by the transport layer when a packet comes in
51 */
52 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
53 {
54         struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
55
56         CTDB_INCREMENT_STAT(ctdb, node_packets_recv);
57
58         /* up the counter for this source node, so we know its alive */
59         if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
60                 /* as a special case, redirected calls don't increment the rx_cnt */
61                 if (hdr->operation != CTDB_REQ_CALL ||
62                     ((struct ctdb_req_call_old *)hdr)->hopcount == 0) {
63                         ctdb->nodes[hdr->srcnode]->rx_cnt++;
64                 }
65         }
66
67         ctdb_input_pkt(ctdb, hdr);
68 }
69
70 static const struct ctdb_upcalls ctdb_upcalls = {
71         .recv_pkt       = ctdb_recv_pkt,
72         .node_dead      = ctdb_node_dead,
73         .node_connected = ctdb_node_connected
74 };
75
76 static struct ctdb_context *ctdb_init(struct tevent_context *ev)
77 {
78         int ret;
79         struct ctdb_context *ctdb;
80
81         ctdb = talloc_zero(ev, struct ctdb_context);
82         if (ctdb == NULL) {
83                 DBG_ERR("Memory error\n");
84                 return NULL;
85         }
86         ctdb->ev  = ev;
87
88         /* Wrap early to exercise code. */
89         ret = reqid_init(ctdb, INT_MAX-200, &ctdb->idr);
90         if (ret != 0) {
91                 D_ERR("reqid_init failed (%s)\n", strerror(ret));
92                 talloc_free(ctdb);
93                 return NULL;
94         }
95
96         ret = srvid_init(ctdb, &ctdb->srv);
97         if (ret != 0) {
98                 D_ERR("srvid_init failed (%s)\n", strerror(ret));
99                 talloc_free(ctdb);
100                 return NULL;
101         }
102
103         ret = ctdb_set_socketname(ctdb, CTDB_SOCKET);
104         if (ret != 0) {
105                 DBG_ERR("ctdb_set_socketname failed.\n");
106                 talloc_free(ctdb);
107                 return NULL;
108         }
109
110         gettimeofday(&ctdb->ctdbd_start_time, NULL);
111
112         gettimeofday(&ctdb->last_recovery_started, NULL);
113         gettimeofday(&ctdb->last_recovery_finished, NULL);
114
115         ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
116         ctdb->recovery_master  = (uint32_t)-1;
117
118         ctdb->upcalls = &ctdb_upcalls;
119
120         ctdb->statistics.statistics_start_time = timeval_current();
121
122         ctdb->capabilities = CTDB_CAP_DEFAULT;
123
124         /*
125          * Initialise this node's PNN to the unknown value.  This will
126          * be set to the correct value by either ctdb_add_node() as
127          * part of loading the nodes file or by
128          * ctdb_tcp_listen_automatic() when the transport is
129          * initialised.  At some point we should de-optimise this and
130          * pull it out into ctdb_start_daemon() so it is done clearly
131          * and only in one place.
132          */
133         ctdb->pnn = CTDB_UNKNOWN_PNN;
134
135         ctdb->do_checkpublicip = true;
136
137         return ctdb;
138 }
139
140
141 /*
142   main program
143 */
144 int main(int argc, const char *argv[])
145 {
146         struct ctdb_context *ctdb = NULL;
147         int interactive = 0;
148         const char *ctdb_socket;
149
150         struct poptOption popt_options[] = {
151                 POPT_AUTOHELP
152                 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0,
153                   "don't fork", NULL },
154                 POPT_TABLEEND
155         };
156         int opt, ret;
157         const char **extra_argv;
158         poptContext pc;
159         struct tevent_context *ev;
160         const char *ctdb_base;
161         struct conf_context *conf;
162         const char *logging_location;
163         const char *t;
164         bool ok;
165
166         /*
167          * Basic setup
168          */
169
170         talloc_enable_null_tracking();
171
172         fault_setup();
173
174         ev = tevent_context_init(NULL);
175         if (ev == NULL) {
176                 fprintf(stderr, "tevent_context_init() failed\n");
177                 exit(1);
178         }
179         tevent_loop_allow_nesting(ev);
180
181         ctdb = ctdb_init(ev);
182         if (ctdb == NULL) {
183                 fprintf(stderr, "Failed to init ctdb\n");
184                 exit(1);
185         }
186
187         /* Default value for CTDB_BASE - don't override */
188         setenv("CTDB_BASE", CTDB_ETCDIR, 0);
189         ctdb_base = getenv("CTDB_BASE");
190         if (ctdb_base == NULL) {
191                 D_ERR("CTDB_BASE not set\n");
192                 exit(1);
193         }
194
195         /*
196          * Command-line option handling
197          */
198
199         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
200
201         while ((opt = poptGetNextOpt(pc)) != -1) {
202                 switch (opt) {
203                 default:
204                         fprintf(stderr, "Invalid option %s: %s\n", 
205                                 poptBadOption(pc, 0), poptStrerror(opt));
206                         goto fail;
207                 }
208         }
209
210         /* If there are extra arguments then exit with usage message */
211         extra_argv = poptGetArgs(pc);
212         if (extra_argv) {
213                 extra_argv++;
214                 if (extra_argv[0])  {
215                         poptPrintHelp(pc, stdout, 0);
216                         goto fail;
217                 }
218         }
219
220         /*
221          * Configuration file handling
222          */
223
224         ret = ctdbd_config_load(ctdb, &conf);
225         if (ret != 0) {
226                 fprintf(stderr, "Failed to setup config file handling\n");
227                 goto fail;
228         }
229
230         /*
231          * Logging setup/options
232          */
233
234         /* Log to stderr (ignoring configuration) when running as interactive */
235         if (interactive) {
236                 logging_location = "file:";
237         } else {
238                 logging_location = logging_conf_location(conf);
239         }
240
241         if (strcmp(logging_location, "syslog") != 0) {
242                 /* This can help when CTDB logging is misconfigured */
243                 syslog(LOG_DAEMON|LOG_NOTICE,
244                        "CTDB logging to location %s",
245                        logging_location);
246         }
247
248         /* Initialize logging and set the debug level */
249         ok = ctdb_logging_init(ctdb,
250                                logging_location,
251                                logging_conf_log_level(conf));
252         if (!ok) {
253                 goto fail;
254         }
255         setenv("CTDB_LOGGING", logging_location, 1);
256         setenv("CTDB_DEBUGLEVEL", debug_level_to_string(DEBUGLEVEL), 1);
257
258         script_log_level = debug_level_from_string(
259                                         ctdb_config.script_log_level);
260
261         D_NOTICE("CTDB starting on node\n");
262
263         /*
264          * Cluster setup/options
265          */
266
267         ret = ctdb_set_transport(ctdb, ctdb_config.transport);
268         if (ret == -1) {
269                 D_ERR("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb));
270                 goto fail;
271         }
272
273         if (ctdb_config.recovery_lock == NULL) {
274                 D_WARNING("Recovery lock not set\n");
275         }
276         ctdb->recovery_lock = ctdb_config.recovery_lock;
277
278         /* tell ctdb what address to listen on */
279         if (ctdb_config.node_address) {
280                 ret = ctdb_set_address(ctdb, ctdb_config.node_address);
281                 if (ret == -1) {
282                         D_ERR("ctdb_set_address failed - %s\n",
283                               ctdb_errstr(ctdb));
284                         goto fail;
285                 }
286         }
287
288         /* tell ctdb what nodes are available */
289         ctdb->nodes_file = talloc_asprintf(ctdb, "%s/nodes", ctdb_base);
290         if (ctdb->nodes_file == NULL) {
291                 DBG_ERR(" Out of memory\n");
292                 goto fail;
293         }
294         ctdb_load_nodes_file(ctdb);
295
296         /*
297          * Database setup/options
298          */
299
300         ctdb->db_directory = ctdb_config.dbdir_volatile;
301         ctdb->db_directory_persistent = ctdb_config.dbdir_persistent;
302         ctdb->db_directory_state = ctdb_config.dbdir_state;
303
304         if (ctdb_config.lock_debug_script != NULL) {
305                 ret = setenv("CTDB_DEBUG_LOCKS",
306                              ctdb_config.lock_debug_script,
307                              1);
308                 if (ret != 0) {
309                         D_ERR("Failed to set up lock debugging (%s)\n",
310                               strerror(ret));
311                         goto fail;
312                 }
313         }
314
315         /*
316          * Event setup/options
317          */
318         if (ctdb_config.event_debug_script != NULL) {
319                 ret = setenv("CTDB_DEBUG_HUNG_SCRIPT",
320                              ctdb_config.event_debug_script,
321                              1);
322                 if (ret != 0) {
323                         D_ERR("Failed to set up event script debugging (%s)\n",
324                               strerror(ret));
325                         goto fail;
326                 }
327         }
328
329         /*
330          * Legacy setup/options
331          */
332
333         ctdb->start_as_disabled = (int)ctdb_config.start_as_disabled;
334         ctdb->start_as_stopped  = (int)ctdb_config.start_as_stopped;
335
336         /* set ctdbd capabilities */
337         if (!ctdb_config.lmaster_capability) {
338                 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
339         }
340         if (!ctdb_config.recmaster_capability) {
341                 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
342         }
343
344         ctdb->do_setsched = !ctdb_config.no_realtime;
345
346         /*
347          * Miscellaneous setup
348          */
349
350         ctdb_tunables_set_defaults(ctdb);
351
352         ctdb->event_script_dir = talloc_asprintf(ctdb,
353                                                  "%s/events.d",
354                                                  ctdb_base);
355         if (ctdb->event_script_dir == NULL) {
356                 DBG_ERR("Out of memory\n");
357                 goto fail;
358         }
359
360         ctdb->notification_script = talloc_asprintf(ctdb,
361                                                     "%s/notify.sh",
362                                                     ctdb_base);
363         if (ctdb->notification_script == NULL) {
364                 D_ERR("Unable to set notification script\n");
365                 goto fail;
366         }
367
368         /*
369          * Testing and debug options
370          */
371
372         /* Environment variable overrides default */
373         ctdbd_pidfile = getenv("CTDB_PIDFILE");
374         if (ctdbd_pidfile == NULL) {
375                 ctdbd_pidfile = CTDB_RUNDIR "/ctdbd.pid";
376         }
377
378         /* Environment variable overrides default */
379         ctdb_socket = getenv("CTDB_SOCKET");
380         if (ctdb_socket == NULL) {
381                 ctdb_socket = CTDB_SOCKET;
382         }
383         ret = ctdb_set_socketname(ctdb, ctdb_socket);
384         if (ret == -1) {
385                 D_ERR("ctdb_set_socketname() failed\n");
386                 goto fail;
387         }
388
389         t = getenv("CTDB_TEST_MODE");
390         if (t != NULL) {
391                 ctdb->do_setsched = false;
392                 ctdb->do_checkpublicip = false;
393                 fast_start = true;
394         }
395
396         /* start the protocol running (as a child) */
397         return ctdb_start_daemon(ctdb, interactive?false:true);
398
399 fail:
400         talloc_free(ctdb);
401         exit(1);
402 }