s3:secrets: let secrets_delete_machine_password_ex() remove SID and GUID too
[Samba.git] / ctdb / server / ctdbd.c
blobdcd54c228532b287c1f7e12483993a38c0c663dd
1 /*
2 standalone ctdb daemon
4 Copyright (C) Andrew Tridgell 2006
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.
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.
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/>.
20 #include "replace.h"
21 #include "system/filesys.h"
22 #include "system/time.h"
23 #include "system/wait.h"
24 #include "system/network.h"
26 #include <popt.h>
27 #include <talloc.h>
28 /* Allow use of deprecated function tevent_loop_allow_nesting() */
29 #define TEVENT_DEPRECATED
30 #include <tevent.h>
32 #include "lib/util/debug.h"
33 #include "lib/util/samba_util.h"
35 #include "ctdb_private.h"
37 #include "common/reqid.h"
38 #include "common/system.h"
39 #include "common/common.h"
40 #include "common/logging.h"
42 static struct {
43 const char *socket;
44 const char *debuglevel;
45 const char *nlist;
46 const char *transport;
47 const char *myaddress;
48 const char *public_address_list;
49 const char *event_script_dir;
50 const char *notification_script;
51 const char *logging;
52 const char *recovery_lock;
53 const char *db_dir;
54 const char *db_dir_persistent;
55 const char *db_dir_state;
56 const char *public_interface;
57 int valgrinding;
58 int nosetsched;
59 int start_as_disabled;
60 int start_as_stopped;
61 int no_lmaster;
62 int no_recmaster;
63 int script_log_level;
64 int no_publicipcheck;
65 int max_persistent_check_errors;
66 int torture;
67 } options = {
68 .socket = CTDB_RUNDIR "/ctdbd.socket",
69 .debuglevel = "NOTICE",
70 .nlist = NULL,
71 .public_address_list = NULL,
72 .transport = "tcp",
73 .event_script_dir = NULL,
74 .logging = "file:" LOGDIR "/log.ctdb",
75 .db_dir = CTDB_VARDIR,
76 .db_dir_persistent = CTDB_VARDIR "/persistent",
77 .db_dir_state = CTDB_VARDIR "/state",
78 .script_log_level = DEBUG_ERR,
81 int script_log_level;
82 bool fast_start;
85 called by the transport layer when a packet comes in
87 static void ctdb_recv_pkt(struct ctdb_context *ctdb, uint8_t *data, uint32_t length)
89 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
91 CTDB_INCREMENT_STAT(ctdb, node_packets_recv);
93 /* up the counter for this source node, so we know its alive */
94 if (ctdb_validate_pnn(ctdb, hdr->srcnode)) {
95 /* as a special case, redirected calls don't increment the rx_cnt */
96 if (hdr->operation != CTDB_REQ_CALL ||
97 ((struct ctdb_req_call_old *)hdr)->hopcount == 0) {
98 ctdb->nodes[hdr->srcnode]->rx_cnt++;
102 ctdb_input_pkt(ctdb, hdr);
105 static const struct ctdb_upcalls ctdb_upcalls = {
106 .recv_pkt = ctdb_recv_pkt,
107 .node_dead = ctdb_node_dead,
108 .node_connected = ctdb_node_connected
114 main program
116 int main(int argc, const char *argv[])
118 struct ctdb_context *ctdb;
119 int interactive = 0;
121 struct poptOption popt_options[] = {
122 POPT_AUTOHELP
123 { "socket", 0, POPT_ARG_STRING, &options.socket, 0, "local socket name", "filename" },
124 { "debug", 'd', POPT_ARG_STRING, &options.debuglevel, 0, "debug level", NULL },
125 { "interactive", 'i', POPT_ARG_NONE, &interactive, 0, "don't fork", NULL },
126 { "public-addresses", 0, POPT_ARG_STRING, &options.public_address_list, 0, "public address list file", "filename" },
127 { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"},
128 { "event-script-dir", 0, POPT_ARG_STRING, &options.event_script_dir, 0, "event script directory", "dirname" },
129 { "logging", 0, POPT_ARG_STRING, &options.logging, 0, "logging method to be used", NULL },
130 { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "filename" },
131 { "notification-script", 0, POPT_ARG_STRING, &options.notification_script, 0, "notification script", "filename" },
132 { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" },
133 { "transport", 0, POPT_ARG_STRING, &options.transport, 0, "protocol transport", NULL },
134 { "dbdir", 0, POPT_ARG_STRING, &options.db_dir, 0, "directory for the tdb files", NULL },
135 { "dbdir-persistent", 0, POPT_ARG_STRING, &options.db_dir_persistent, 0, "directory for persistent tdb files", NULL },
136 { "dbdir-state", 0, POPT_ARG_STRING, &options.db_dir_state, 0, "directory for internal state tdb files", NULL },
137 { "reclock", 0, POPT_ARG_STRING, &options.recovery_lock, 0, "recovery lock", "lock" },
138 { "pidfile", 0, POPT_ARG_STRING, &ctdbd_pidfile, 0, "location of PID file", "filename" },
139 { "valgrinding", 0, POPT_ARG_NONE, &options.valgrinding, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
140 { "nosetsched", 0, POPT_ARG_NONE, &options.nosetsched, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL },
141 { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL },
142 { "start-as-stopped", 0, POPT_ARG_NONE, &options.start_as_stopped, 0, "Node starts in stopped state", NULL },
143 { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL },
144 { "no-recmaster", 0, POPT_ARG_NONE, &options.no_recmaster, 0, "disable recmaster role on this node", NULL },
145 { "script-log-level", 0, POPT_ARG_INT, &options.script_log_level, 0, "log level of event script output", NULL },
146 { "nopublicipcheck", 0, POPT_ARG_NONE, &options.no_publicipcheck, 0, "don't check we have/don't have the correct public ip addresses", NULL },
147 { "max-persistent-check-errors", 0, POPT_ARG_INT,
148 &options.max_persistent_check_errors, 0,
149 "max allowed persistent check errors (default 0)", NULL },
150 { "sloppy-start", 0, POPT_ARG_NONE, &fast_start, 0, "Do not perform full recovery on start", NULL },
151 { "torture", 0, POPT_ARG_NONE, &options.torture, 0, "enable nastiness in library", NULL },
152 POPT_TABLEEND
154 int opt, ret;
155 const char **extra_argv;
156 poptContext pc;
157 struct tevent_context *ev;
159 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
161 while ((opt = poptGetNextOpt(pc)) != -1) {
162 switch (opt) {
163 default:
164 fprintf(stderr, "Invalid option %s: %s\n",
165 poptBadOption(pc, 0), poptStrerror(opt));
166 exit(1);
170 /* If there are extra arguments then exit with usage message */
171 extra_argv = poptGetArgs(pc);
172 if (extra_argv) {
173 extra_argv++;
174 if (extra_argv[0]) {
175 poptPrintHelp(pc, stdout, 0);
176 exit(1);
180 talloc_enable_null_tracking();
182 fault_setup();
184 ev = tevent_context_init(NULL);
185 if (ev == NULL) {
186 fprintf(stderr, "tevent_context_init() failed\n");
187 exit(1);
189 tevent_loop_allow_nesting(ev);
191 ctdb = ctdb_init(ev);
192 if (ctdb == NULL) {
193 fprintf(stderr, "Failed to init ctdb\n");
194 exit(1);
197 if (options.torture == 1) {
198 ctdb_set_flags(ctdb, CTDB_FLAG_TORTURE);
201 /* Log to stderr when running as interactive */
202 if (interactive) {
203 options.logging = "file:";
206 /* Initialize logging and set the debug level */
207 if (!ctdb_logging_init(ctdb, options.logging, options.debuglevel)) {
208 exit(1);
210 setenv("CTDB_LOGGING", options.logging, 1);
211 setenv("CTDB_DEBUGLEVEL", debug_level_to_string(DEBUGLEVEL), 1);
213 setenv("CTDB_SOCKET", options.socket, 1);
214 ret = ctdb_set_socketname(ctdb, options.socket);
215 if (ret == -1) {
216 DEBUG(DEBUG_ERR, ("ctdb_set_socketname() failed\n"));
217 exit(1);
220 ctdb->start_as_disabled = options.start_as_disabled;
221 ctdb->start_as_stopped = options.start_as_stopped;
223 script_log_level = options.script_log_level;
225 DEBUG(DEBUG_NOTICE,("CTDB starting on node\n"));
227 gettimeofday(&ctdb->ctdbd_start_time, NULL);
228 gettimeofday(&ctdb->last_recovery_started, NULL);
229 gettimeofday(&ctdb->last_recovery_finished, NULL);
230 ctdb->recovery_mode = CTDB_RECOVERY_NORMAL;
231 ctdb->recovery_master = (uint32_t)-1;
232 ctdb->upcalls = &ctdb_upcalls;
234 if (options.recovery_lock == NULL) {
235 DEBUG(DEBUG_WARNING, ("Recovery lock not set\n"));
237 ctdb->recovery_lock = options.recovery_lock;
239 TALLOC_FREE(ctdb->idr);
240 ret = reqid_init(ctdb, 0, &ctdb->idr);;
241 if (ret != 0) {
242 DEBUG(DEBUG_ERR, ("reqid_init failed (%s)\n", strerror(ret)));
243 exit(1);
246 ctdb_tunables_set_defaults(ctdb);
248 ret = ctdb_set_transport(ctdb, options.transport);
249 if (ret == -1) {
250 DEBUG(DEBUG_ERR,("ctdb_set_transport failed - %s\n",
251 ctdb_errstr(ctdb)));
252 exit(1);
255 /* tell ctdb what address to listen on */
256 if (options.myaddress) {
257 ret = ctdb_set_address(ctdb, options.myaddress);
258 if (ret == -1) {
259 DEBUG(DEBUG_ERR,("ctdb_set_address failed - %s\n",
260 ctdb_errstr(ctdb)));
261 exit(1);
265 /* set ctdbd capabilities */
266 ctdb->capabilities = CTDB_CAP_DEFAULT;
267 if (options.no_lmaster != 0) {
268 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
270 if (options.no_recmaster != 0) {
271 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
274 /* Initialise this node's PNN to the unknown value. This will
275 * be set to the correct value by either ctdb_add_node() as
276 * part of loading the nodes file or by
277 * ctdb_tcp_listen_automatic() when the transport is
278 * initialised. At some point we should de-optimise this and
279 * pull it out into ctdb_start_daemon() so it is done clearly
280 * and only in one place.
282 ctdb->pnn = -1;
284 /* Default value for CTDB_BASE - don't override */
285 setenv("CTDB_BASE", CTDB_ETCDIR, 0);
287 /* tell ctdb what nodes are available */
288 if (options.nlist != NULL) {
289 ctdb->nodes_file = options.nlist;
290 } else {
291 ctdb->nodes_file =
292 talloc_asprintf(ctdb, "%s/nodes", getenv("CTDB_BASE"));
293 if (ctdb->nodes_file == NULL) {
294 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
295 exit(1);
298 ctdb_load_nodes_file(ctdb);
300 ctdb->db_directory = options.db_dir;
301 mkdir_p_or_die(ctdb->db_directory, 0700);
303 ctdb->db_directory_persistent = options.db_dir_persistent;
304 mkdir_p_or_die(ctdb->db_directory_persistent, 0700);
306 ctdb->db_directory_state = options.db_dir_state;
307 mkdir_p_or_die(ctdb->db_directory_state, 0700);
309 if (options.public_interface) {
310 ctdb->default_public_interface = talloc_strdup(ctdb, options.public_interface);
311 CTDB_NO_MEMORY(ctdb, ctdb->default_public_interface);
314 if (options.event_script_dir != NULL) {
315 ctdb->event_script_dir = options.event_script_dir;
316 } else {
317 ctdb->event_script_dir = talloc_asprintf(ctdb, "%s/events.d",
318 getenv("CTDB_BASE"));
319 if (ctdb->event_script_dir == NULL) {
320 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
321 exit(1);
325 if (options.notification_script != NULL) {
326 ret = ctdb_set_notification_script(ctdb, options.notification_script);
327 if (ret == -1) {
328 DEBUG(DEBUG_ERR,("Unable to setup notification script\n"));
329 exit(1);
333 ctdb->valgrinding = (options.valgrinding == 1);
334 ctdb->do_setsched = (options.nosetsched != 1);
335 if (ctdb->valgrinding) {
336 ctdb->do_setsched = false;
339 ctdb->public_addresses_file = options.public_address_list;
340 ctdb->do_checkpublicip = (options.no_publicipcheck == 0);
342 if (options.max_persistent_check_errors < 0) {
343 ctdb->max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL;
344 } else {
345 ctdb->max_persistent_check_errors = (uint64_t)options.max_persistent_check_errors;
348 /* start the protocol running (as a child) */
349 return ctdb_start_daemon(ctdb, interactive?false:true);