ctdb-logging: New option CTDB_LOGGING, remove CTDB_LOGFILE, CTDB_SYSLOG
[samba.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 "includes.h"
21 #include "system/filesys.h"
22 #include "popt.h"
23 #include "system/time.h"
24 #include "system/wait.h"
25 #include "system/network.h"
26 #include "cmdline.h"
27 #include "../include/ctdb_private.h"
28
29 static struct {
30         const char *nlist;
31         const char *transport;
32         const char *myaddress;
33         const char *public_address_list;
34         const char *event_script_dir;
35         const char *notification_script;
36         const char *logging;
37         const char *recovery_lock_file;
38         const char *db_dir;
39         const char *db_dir_persistent;
40         const char *db_dir_state;
41         const char *public_interface;
42         const char *single_public_ip;
43         int         valgrinding;
44         int         nosetsched;
45         int         start_as_disabled;
46         int         start_as_stopped;
47         int         no_lmaster;
48         int         no_recmaster;
49         int         lvs;
50         int         script_log_level;
51         int         no_publicipcheck;
52         int         max_persistent_check_errors;
53 } options = {
54         .nlist = NULL,
55         .public_address_list = NULL,
56         .transport = "tcp",
57         .event_script_dir = NULL,
58         .logging = "file:" LOGDIR "/log.ctdb",
59         .db_dir = CTDB_VARDIR,
60         .db_dir_persistent = CTDB_VARDIR "/persistent",
61         .db_dir_state = CTDB_VARDIR "/state",
62         .script_log_level = DEBUG_ERR,
63 };
64
65 int script_log_level;
66 bool fast_start;
67
68 /*
69   called by the transport layer when a packet comes in
70 */
71 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
72 {
73         struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
74
75         CTDB_INCREMENT_STAT(ctdb, node_packets_recv);
76
77         /* up the counter for this source node, so we know its alive */
78         if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
79                 /* as a special case, redirected calls don't increment the rx_cnt */
80                 if (hdr->operation != CTDB_REQ_CALL ||
81                     ((struct ctdb_req_call *)hdr)->hopcount == 0) {
82                         ctdb->nodes[hdr->srcnode]->rx_cnt++;
83                 }
84         }
85
86         ctdb_input_pkt(ctdb, hdr);
87 }
88
89 static const struct ctdb_upcalls ctdb_upcalls = {
90         .recv_pkt       = ctdb_recv_pkt,
91         .node_dead      = ctdb_node_dead,
92         .node_connected = ctdb_node_connected
93 };
94
95
96
97 /*
98   main program
99 */
100 int main(int argc, const char *argv[])
101 {
102         struct ctdb_context *ctdb;
103         int interactive = 0;
104
105         struct poptOption popt_options[] = {
106                 POPT_AUTOHELP
107                 POPT_CTDB_CMDLINE
108                 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
109                 { "public-addresses", 0, POPT_ARG_STRING, &options.public_address_list, 0, "public address list file", "filename" },
110                 { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"},
111                 { "single-public-ip", 0, POPT_ARG_STRING, &options.single_public_ip, 0, "single public ip", "ip-address"},
112                 { "event-script-dir", 0, POPT_ARG_STRING, &options.event_script_dir, 0, "event script directory", "dirname" },
113                 { "logging", 0, POPT_ARG_STRING, &options.logging, 0, "logging method to be used", NULL },
114                 { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "filename" },
115                 { "notification-script", 0, POPT_ARG_STRING, &options.notification_script, 0, "notification script", "filename" },
116                 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
117                 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
118                 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
119                 { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
120                 { "dbdir-state", 0, POPT_ARG_STRING, &options.db_dir_state, 0, "directory for internal state tdb files", NULL },
121                 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock_file, 0, "location of recovery lock file", "filename" },
122                 { "pidfile", 0, POPT_ARG_STRING, &ctdbd_pidfile, 0, "location of PID file", "filename" },
123                 { "valgrinding", 0, POPT_ARG_NONE, &options.valgrinding, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
124                 { "nosetsched", 0, POPT_ARG_NONE, &options.nosetsched, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
125                 { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
126                 { "start-as-stopped", 0, POPT_ARG_NONE, &options.start_as_stopped, 0, "Node starts in stopped state", NULL },
127                 { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL },
128                 { "no-recmaster", 0, POPT_ARG_NONE, &options.no_recmaster, 0, "disable recmaster role on this node", NULL },
129                 { "lvs", 0, POPT_ARG_NONE, &options.lvs, 0, "lvs is enabled on this node", NULL },
130                 { "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, 0, "log level of event script output", NULL },
131                 { "nopublicipcheck", 0, POPT_ARG_NONE, &options.no_publicipcheck, 0, "don't check we have/don't have the correct public ip addresses", NULL },
132                 { "max-persistent-check-errors", 0, POPT_ARG_INT,
133                   &options.max_persistent_check_errors, 0,
134                   "max allowed persistent check errors (default 0)", NULL },
135                 { "sloppy-start", 0, POPT_ARG_NONE, &fast_start, 0, "Do not perform full recovery on start", NULL },
136                 POPT_TABLEEND
137         };
138         int opt, ret;
139         const char **extra_argv;
140         int extra_argc = 0;
141         poptContext pc;
142         struct event_context *ev;
143
144         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
145
146         while ((opt = poptGetNextOpt(pc)) != -1) {
147                 switch (opt) {
148                 default:
149                         fprintf(stderr, "Invalid option %s: %s\n", 
150                                 poptBadOption(pc, 0), poptStrerror(opt));
151                         exit(1);
152                 }
153         }
154
155         /* setup the remaining options for the main program to use */
156         extra_argv = poptGetArgs(pc);
157         if (extra_argv) {
158                 extra_argv++;
159                 while (extra_argv[extra_argc]) extra_argc++;
160         }
161
162         talloc_enable_null_tracking();
163
164         fault_setup();
165
166         ev = event_context_init(NULL);
167         tevent_loop_allow_nesting(ev);
168
169         ctdb = ctdb_cmdline_init(ev);
170
171         ctdb->start_as_disabled = options.start_as_disabled;
172         ctdb->start_as_stopped  = options.start_as_stopped;
173
174         script_log_level = options.script_log_level;
175
176         if (!ctdb_logging_init(ctdb, options.logging)) {
177                 exit(1);
178         }
179
180         DEBUG(DEBUG_NOTICE,("CTDB starting on node\n"));
181
182         gettimeofday(&ctdb->ctdbd_start_time, NULL);
183         gettimeofday(&ctdb->last_recovery_started, NULL);
184         gettimeofday(&ctdb->last_recovery_finished, NULL);
185         ctdb->recovery_mode    = CTDB_RECOVERY_NORMAL;
186         ctdb->recovery_master  = (uint32_t)-1;
187         ctdb->upcalls          = &ctdb_upcalls;
188         ctdb->idr              = idr_init(ctdb);
189         ctdb->recovery_lock_fd = -1;
190
191         ctdb_tunables_set_defaults(ctdb);
192
193         ret = ctdb_set_recovery_lock_file(ctdb, options.recovery_lock_file);
194         if (ret == -1) {
195                 DEBUG(DEBUG_ALERT,("ctdb_set_recovery_lock_file failed - %s\n", ctdb_errstr(ctdb)));
196                 exit(1);
197         }
198
199         ret = ctdb_set_transport(ctdb, options.transport);
200         if (ret == -1) {
201                 DEBUG(DEBUG_ALERT,("ctdb_set_transport failed - %s\n", ctdb_errstr(ctdb)));
202                 exit(1);
203         }
204
205         /* tell ctdb what address to listen on */
206         if (options.myaddress) {
207                 ret = ctdb_set_address(ctdb, options.myaddress);
208                 if (ret == -1) {
209                         DEBUG(DEBUG_ALERT,("ctdb_set_address failed - %s\n", ctdb_errstr(ctdb)));
210                         exit(1);
211                 }
212         }
213
214         /* set ctdbd capabilities */
215         ctdb->capabilities = 0;
216         if (options.no_lmaster == 0) {
217                 ctdb->capabilities |= CTDB_CAP_LMASTER;
218         }
219         if (options.no_recmaster == 0) {
220                 ctdb->capabilities |= CTDB_CAP_RECMASTER;
221         }
222         if (options.lvs != 0) {
223                 ctdb->capabilities |= CTDB_CAP_LVS;
224         }
225
226         /* Initialise this node's PNN to the unknown value.  This will
227          * be set to the correct value by either ctdb_add_node() as
228          * part of loading the nodes file or by
229          * ctdb_tcp_listen_automatic() when the transport is
230          * initialised.  At some point we should de-optimise this and
231          * pull it out into ctdb_start_daemon() so it is done clearly
232          * and only in one place.
233          */
234         ctdb->pnn = -1;
235
236         /* Default value for CTDB_BASE - don't override */
237         setenv("CTDB_BASE", CTDB_ETCDIR, 0);
238
239         /* tell ctdb what nodes are available */
240         if (options.nlist != NULL) {
241                 ctdb->nodes_file = options.nlist;
242         } else {
243                 ctdb->nodes_file =
244                         talloc_asprintf(ctdb, "%s/nodes", getenv("CTDB_BASE"));
245                 if (ctdb->nodes_file == NULL) {
246                         DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
247                         exit(1);
248                 }
249         }
250         ctdb_load_nodes_file(ctdb);
251
252         ctdb->db_directory = options.db_dir;
253         mkdir_p_or_die(ctdb->db_directory, 0700);
254
255         ctdb->db_directory_persistent = options.db_dir_persistent;
256         mkdir_p_or_die(ctdb->db_directory_persistent, 0700);
257
258         ctdb->db_directory_state = options.db_dir_state;
259         mkdir_p_or_die(ctdb->db_directory_state, 0700);
260
261         if (options.public_interface) {
262                 ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
263                 CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
264         }
265
266         if (options.single_public_ip) {
267                 if (options.public_interface == NULL) {
268                         DEBUG(DEBUG_ALERT,("--single_public_ip used but --public_interface is not specified. You must specify the public interface when using single public ip. Exiting\n"));
269                         exit(10);
270                 }
271
272                 ret = ctdb_set_single_public_ip(ctdb, options.public_interface,
273                                                 options.single_public_ip);
274                 if (ret != 0) {
275                         DEBUG(DEBUG_ALERT,("Invalid --single-public-ip argument : %s . This is not a valid ip address. Exiting.\n", options.single_public_ip));
276                         exit(10);
277                 }
278         }
279
280         if (options.event_script_dir != NULL) {
281                 ctdb->event_script_dir = options.event_script_dir;
282         } else {
283                 ctdb->event_script_dir = talloc_asprintf(ctdb, "%s/events.d",
284                                                          getenv("CTDB_BASE"));
285                 if (ctdb->event_script_dir == NULL) {
286                         DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
287                         exit(1);
288                 }
289         }
290
291         if (options.notification_script != NULL) {
292                 ret = ctdb_set_notification_script(ctdb, options.notification_script);
293                 if (ret == -1) {
294                         DEBUG(DEBUG_ALERT,("Unable to setup notification script\n"));
295                         exit(1);
296                 }
297         }
298
299         ctdb->valgrinding = options.valgrinding;
300         if (options.valgrinding || options.nosetsched) {
301                 ctdb->do_setsched = 0;
302         } else {
303                 ctdb->do_setsched = 1;
304         }
305
306         ctdb->public_addresses_file = options.public_address_list;
307         ctdb->do_checkpublicip = !options.no_publicipcheck;
308
309         if (options.max_persistent_check_errors < 0) {
310                 ctdb->max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL;
311         } else {
312                 ctdb->max_persistent_check_errors = (uint64_t)options.max_persistent_check_errors;
313         }
314
315         /* start the protocol running (as a child) */
316         return ctdb_start_daemon(ctdb, interactive?false:true);
317 }