2007-04-11 12:54:22 +04:00
/*
standalone ctdb daemon
Copyright ( C ) Andrew Tridgell 2006
2007-05-31 07:50:53 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-10 09:29:31 +04:00
the Free Software Foundation ; either version 3 of the License , or
2007-05-31 07:50:53 +04:00
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
2007-04-11 12:54:22 +04:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
2007-05-31 07:50:53 +04:00
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
2007-07-10 09:29:31 +04:00
along with this program ; if not , see < http : //www.gnu.org/licenses/>.
2007-04-11 12:54:22 +04:00
*/
2015-10-26 08:50:46 +03:00
# include "replace.h"
2007-04-11 12:54:22 +04:00
# include "system/filesys.h"
2008-01-17 03:33:23 +03:00
# include "system/time.h"
2007-04-11 16:19:46 +04:00
# include "system/wait.h"
2007-10-16 05:29:33 +04:00
# include "system/network.h"
2015-10-26 08:50:46 +03:00
# include <popt.h>
# include <talloc.h>
/* Allow use of deprecated function tevent_loop_allow_nesting() */
# define TEVENT_DEPRECATED
# include <tevent.h>
# include "lib/util/debug.h"
# include "lib/util/samba_util.h"
# include "ctdb_private.h"
2015-03-17 06:30:18 +03:00
# include "common/reqid.h"
2015-10-23 06:11:53 +03:00
# include "common/system.h"
2015-10-23 06:17:34 +03:00
# include "common/common.h"
2015-11-11 07:41:10 +03:00
# include "common/logging.h"
2007-04-11 15:17:36 +04:00
2007-05-29 06:49:25 +04:00
static struct {
2016-11-18 06:52:38 +03:00
const char * debuglevel ;
2007-06-02 04:03:28 +04:00
const char * transport ;
const char * myaddress ;
2009-03-31 07:23:31 +04:00
const char * notification_script ;
2014-08-11 11:07:41 +04:00
const char * logging ;
2016-05-17 11:28:56 +03:00
const char * recovery_lock ;
2007-06-02 04:03:28 +04:00
const char * db_dir ;
2007-09-21 06:24:02 +04:00
const char * db_dir_persistent ;
2009-11-23 16:38:03 +03:00
const char * db_dir_state ;
2009-12-16 13:29:15 +03:00
int valgrinding ;
2011-01-24 03:42:50 +03:00
int nosetsched ;
2008-02-22 01:42:52 +03:00
int start_as_disabled ;
2009-07-09 05:57:20 +04:00
int start_as_stopped ;
2008-05-06 04:41:22 +04:00
int no_lmaster ;
int no_recmaster ;
2008-10-17 00:56:12 +04:00
int script_log_level ;
2008-11-24 11:06:02 +03:00
int no_publicipcheck ;
2009-12-07 15:28:11 +03:00
int max_persistent_check_errors ;
2016-11-18 06:52:38 +03:00
int torture ;
2007-05-29 06:49:25 +04:00
} options = {
2016-11-30 08:46:19 +03:00
. debuglevel = " NOTICE " ,
2007-06-02 04:03:28 +04:00
. transport = " tcp " ,
2014-08-11 11:07:41 +04:00
. logging = " file: " LOGDIR " /log.ctdb " ,
2013-10-03 09:47:30 +04:00
. db_dir = CTDB_VARDIR ,
. db_dir_persistent = CTDB_VARDIR " /persistent " ,
. db_dir_state = CTDB_VARDIR " /state " ,
2008-10-17 00:56:12 +04:00
. script_log_level = DEBUG_ERR ,
2007-05-29 06:49:25 +04:00
} ;
2008-10-17 00:56:12 +04:00
int script_log_level ;
2010-06-22 17:22:34 +04:00
bool fast_start ;
2007-04-11 12:54:22 +04:00
2007-06-07 16:06:19 +04:00
/*
called by the transport layer when a packet comes in
*/
static void ctdb_recv_pkt ( struct ctdb_context * ctdb , uint8_t * data , uint32_t length )
{
struct ctdb_req_header * hdr = ( struct ctdb_req_header * ) data ;
2010-09-29 04:38:41 +04:00
CTDB_INCREMENT_STAT ( ctdb , node_packets_recv ) ;
2007-06-07 16:06:19 +04:00
/* up the counter for this source node, so we know its alive */
2007-09-04 04:09:58 +04:00
if ( ctdb_validate_pnn ( ctdb , hdr - > srcnode ) ) {
2007-06-07 16:06:19 +04:00
/* as a special case, redirected calls don't increment the rx_cnt */
if ( hdr - > operation ! = CTDB_REQ_CALL | |
2015-10-29 08:26:29 +03:00
( ( struct ctdb_req_call_old * ) hdr ) - > hopcount = = 0 ) {
2007-06-07 16:06:19 +04:00
ctdb - > nodes [ hdr - > srcnode ] - > rx_cnt + + ;
}
}
ctdb_input_pkt ( ctdb , hdr ) ;
}
static const struct ctdb_upcalls ctdb_upcalls = {
. recv_pkt = ctdb_recv_pkt ,
. node_dead = ctdb_node_dead ,
. node_connected = ctdb_node_connected
} ;
2007-05-29 09:26:38 +04:00
2007-04-11 12:54:22 +04:00
/*
main program
*/
int main ( int argc , const char * argv [ ] )
{
struct ctdb_context * ctdb ;
2007-05-15 03:44:33 +04:00
int interactive = 0 ;
2018-02-21 06:46:39 +03:00
const char * ctdb_socket ;
2007-04-11 12:54:22 +04:00
struct poptOption popt_options [ ] = {
POPT_AUTOHELP
2016-11-18 06:52:38 +03:00
{ " debug " , ' d ' , POPT_ARG_STRING , & options . debuglevel , 0 , " debug level " , NULL } ,
2007-05-15 03:44:33 +04:00
{ " interactive " , ' i ' , POPT_ARG_NONE , & interactive , 0 , " don't fork " , NULL } ,
2014-08-11 11:07:41 +04:00
{ " logging " , 0 , POPT_ARG_STRING , & options . logging , 0 , " logging method to be used " , NULL } ,
2009-03-31 07:23:31 +04:00
{ " notification-script " , 0 , POPT_ARG_STRING , & options . notification_script , 0 , " notification script " , " filename " } ,
2007-06-02 04:03:28 +04:00
{ " listen " , 0 , POPT_ARG_STRING , & options . myaddress , 0 , " address to listen on " , " address " } ,
{ " transport " , 0 , POPT_ARG_STRING , & options . transport , 0 , " protocol transport " , NULL } ,
{ " dbdir " , 0 , POPT_ARG_STRING , & options . db_dir , 0 , " directory for the tdb files " , NULL } ,
2007-09-21 06:24:02 +04:00
{ " dbdir-persistent " , 0 , POPT_ARG_STRING , & options . db_dir_persistent , 0 , " directory for persistent tdb files " , NULL } ,
2009-11-23 16:38:03 +03:00
{ " dbdir-state " , 0 , POPT_ARG_STRING , & options . db_dir_state , 0 , " directory for internal state tdb files " , NULL } ,
2016-05-17 11:28:56 +03:00
{ " reclock " , 0 , POPT_ARG_STRING , & options . recovery_lock , 0 , " recovery lock " , " lock " } ,
2011-01-10 05:35:39 +03:00
{ " valgrinding " , 0 , POPT_ARG_NONE , & options . valgrinding , 0 , " disable setscheduler SCHED_FIFO call, use mmap for tdbs " , NULL } ,
2011-01-24 03:42:50 +03:00
{ " nosetsched " , 0 , POPT_ARG_NONE , & options . nosetsched , 0 , " disable setscheduler SCHED_FIFO call, use mmap for tdbs " , NULL } ,
2008-02-22 01:42:52 +03:00
{ " start-as-disabled " , 0 , POPT_ARG_NONE , & options . start_as_disabled , 0 , " Node starts in disabled state " , NULL } ,
2009-07-09 05:57:20 +04:00
{ " start-as-stopped " , 0 , POPT_ARG_NONE , & options . start_as_stopped , 0 , " Node starts in stopped state " , NULL } ,
2008-05-06 04:41:22 +04:00
{ " no-lmaster " , 0 , POPT_ARG_NONE , & options . no_lmaster , 0 , " disable lmaster role on this node " , NULL } ,
{ " no-recmaster " , 0 , POPT_ARG_NONE , & options . no_recmaster , 0 , " disable recmaster role on this node " , NULL } ,
2013-10-21 12:42:32 +04:00
{ " script-log-level " , 0 , POPT_ARG_INT , & options . script_log_level , 0 , " log level of event script output " , NULL } ,
2010-01-05 13:04:24 +03:00
{ " nopublicipcheck " , 0 , POPT_ARG_NONE , & options . no_publicipcheck , 0 , " don't check we have/don't have the correct public ip addresses " , NULL } ,
2009-12-07 15:28:11 +03:00
{ " max-persistent-check-errors " , 0 , POPT_ARG_INT ,
& options . max_persistent_check_errors , 0 ,
" max allowed persistent check errors (default 0) " , NULL } ,
2010-06-22 17:22:34 +04:00
{ " sloppy-start " , 0 , POPT_ARG_NONE , & fast_start , 0 , " Do not perform full recovery on start " , NULL } ,
2016-11-18 06:52:38 +03:00
{ " torture " , 0 , POPT_ARG_NONE , & options . torture , 0 , " enable nastiness in library " , NULL } ,
2007-04-11 12:54:22 +04:00
POPT_TABLEEND
} ;
2007-05-29 06:49:25 +04:00
int opt , ret ;
2007-04-11 12:54:22 +04:00
const char * * extra_argv ;
poptContext pc ;
2015-10-26 08:50:09 +03:00
struct tevent_context * ev ;
2018-04-26 13:32:30 +03:00
const char * ctdb_base ;
2007-04-11 12:54:22 +04:00
2018-03-05 13:19:30 +03:00
/* Environment variable overrides default */
ctdbd_pidfile = getenv ( " CTDB_PIDFILE " ) ;
if ( ctdbd_pidfile = = NULL ) {
ctdbd_pidfile = CTDB_RUNDIR " /ctdbd.pid " ;
}
2018-03-07 04:11:53 +03:00
2018-02-21 06:46:39 +03:00
/* Environment variable overrides default */
ctdb_socket = getenv ( " CTDB_SOCKET " ) ;
if ( ctdb_socket = = NULL ) {
ctdb_socket = CTDB_SOCKET ;
}
2007-04-11 12:54:22 +04:00
pc = poptGetContext ( argv [ 0 ] , argc , argv , popt_options , POPT_CONTEXT_KEEP_FIRST ) ;
while ( ( opt = poptGetNextOpt ( pc ) ) ! = - 1 ) {
switch ( opt ) {
default :
fprintf ( stderr , " Invalid option %s: %s \n " ,
poptBadOption ( pc , 0 ) , poptStrerror ( opt ) ) ;
exit ( 1 ) ;
}
}
2016-11-28 01:51:48 +03:00
/* If there are extra arguments then exit with usage message */
2007-04-11 12:54:22 +04:00
extra_argv = poptGetArgs ( pc ) ;
if ( extra_argv ) {
extra_argv + + ;
2016-11-28 01:51:48 +03:00
if ( extra_argv [ 0 ] ) {
poptPrintHelp ( pc , stdout , 0 ) ;
exit ( 1 ) ;
}
2007-04-11 12:54:22 +04:00
}
2008-01-16 01:44:48 +03:00
talloc_enable_null_tracking ( ) ;
2014-08-15 09:55:20 +04:00
fault_setup ( ) ;
2007-04-11 15:17:36 +04:00
2015-10-26 08:50:09 +03:00
ev = tevent_context_init ( NULL ) ;
2016-07-27 04:45:49 +03:00
if ( ev = = NULL ) {
2016-11-30 09:04:54 +03:00
fprintf ( stderr , " tevent_context_init() failed \n " ) ;
2016-07-27 04:45:49 +03:00
exit ( 1 ) ;
}
2010-08-18 04:11:59 +04:00
tevent_loop_allow_nesting ( ev ) ;
2007-04-11 12:54:22 +04:00
2016-11-18 06:52:38 +03:00
ctdb = ctdb_init ( ev ) ;
if ( ctdb = = NULL ) {
fprintf ( stderr , " Failed to init ctdb \n " ) ;
exit ( 1 ) ;
}
if ( options . torture = = 1 ) {
ctdb_set_flags ( ctdb , CTDB_FLAG_TORTURE ) ;
}
2016-11-29 09:52:00 +03:00
/* Log to stderr when running as interactive */
if ( interactive ) {
options . logging = " file: " ;
}
2016-11-30 08:46:19 +03:00
/* Initialize logging and set the debug level */
if ( ! ctdb_logging_init ( ctdb , options . logging , options . debuglevel ) ) {
exit ( 1 ) ;
2016-11-18 06:52:38 +03:00
}
2016-11-30 08:46:19 +03:00
setenv ( " CTDB_LOGGING " , options . logging , 1 ) ;
setenv ( " CTDB_DEBUGLEVEL " , debug_level_to_string ( DEBUGLEVEL ) , 1 ) ;
2016-11-18 06:52:38 +03:00
2018-02-21 06:46:39 +03:00
ret = ctdb_set_socketname ( ctdb , ctdb_socket ) ;
2016-11-18 06:52:38 +03:00
if ( ret = = - 1 ) {
2016-11-30 09:04:54 +03:00
DEBUG ( DEBUG_ERR , ( " ctdb_set_socketname() failed \n " ) ) ;
2016-11-18 06:52:38 +03:00
exit ( 1 ) ;
}
2007-04-11 12:54:22 +04:00
2008-02-22 01:42:52 +03:00
ctdb - > start_as_disabled = options . start_as_disabled ;
2009-07-09 05:57:20 +04:00
ctdb - > start_as_stopped = options . start_as_stopped ;
2008-02-22 01:42:52 +03:00
2008-10-17 00:56:12 +04:00
script_log_level = options . script_log_level ;
2011-08-04 07:44:25 +04:00
DEBUG ( DEBUG_NOTICE , ( " CTDB starting on node \n " ) ) ;
2010-09-22 04:59:01 +04:00
2008-01-17 03:33:23 +03:00
gettimeofday ( & ctdb - > ctdbd_start_time , NULL ) ;
2008-07-02 07:55:59 +04:00
gettimeofday ( & ctdb - > last_recovery_started , NULL ) ;
gettimeofday ( & ctdb - > last_recovery_finished , NULL ) ;
2007-06-07 16:06:19 +04:00
ctdb - > recovery_mode = CTDB_RECOVERY_NORMAL ;
ctdb - > recovery_master = ( uint32_t ) - 1 ;
ctdb - > upcalls = & ctdb_upcalls ;
2016-05-17 11:24:53 +03:00
2016-05-17 11:28:56 +03:00
if ( options . recovery_lock = = NULL ) {
DEBUG ( DEBUG_WARNING , ( " Recovery lock not set \n " ) ) ;
2016-05-17 11:24:53 +03:00
}
2016-05-17 11:28:56 +03:00
ctdb - > recovery_lock = options . recovery_lock ;
2007-06-07 16:06:19 +04:00
2016-04-05 10:11:17 +03:00
TALLOC_FREE ( ctdb - > idr ) ;
2015-03-17 06:30:18 +03:00
ret = reqid_init ( ctdb , 0 , & ctdb - > idr ) ; ;
if ( ret ! = 0 ) {
2016-11-30 09:04:54 +03:00
DEBUG ( DEBUG_ERR , ( " reqid_init failed (%s) \n " , strerror ( ret ) ) ) ;
2015-03-17 06:30:18 +03:00
exit ( 1 ) ;
}
2007-06-07 16:06:19 +04:00
ctdb_tunables_set_defaults ( ctdb ) ;
2007-06-02 04:03:28 +04:00
ret = ctdb_set_transport ( ctdb , options . transport ) ;
if ( ret = = - 1 ) {
2016-11-30 09:04:54 +03:00
DEBUG ( DEBUG_ERR , ( " ctdb_set_transport failed - %s \n " ,
ctdb_errstr ( ctdb ) ) ) ;
2007-06-02 04:03:28 +04:00
exit ( 1 ) ;
}
/* tell ctdb what address to listen on */
if ( options . myaddress ) {
ret = ctdb_set_address ( ctdb , options . myaddress ) ;
if ( ret = = - 1 ) {
2016-11-30 09:04:54 +03:00
DEBUG ( DEBUG_ERR , ( " ctdb_set_address failed - %s \n " ,
ctdb_errstr ( ctdb ) ) ) ;
2007-06-02 04:03:28 +04:00
exit ( 1 ) ;
}
}
2008-05-06 04:02:27 +04:00
/* set ctdbd capabilities */
2015-09-17 09:13:55 +03:00
ctdb - > capabilities = CTDB_CAP_DEFAULT ;
2015-10-08 12:25:20 +03:00
if ( options . no_lmaster ! = 0 ) {
ctdb - > capabilities & = ~ CTDB_CAP_LMASTER ;
2008-05-06 04:41:22 +04:00
}
2015-10-08 12:25:20 +03:00
if ( options . no_recmaster ! = 0 ) {
ctdb - > capabilities & = ~ CTDB_CAP_RECMASTER ;
2008-05-06 04:41:22 +04:00
}
2008-05-06 04:02:27 +04:00
2012-12-04 07:28:06 +04:00
/* Initialise this node's PNN to the unknown value. This will
* be set to the correct value by either ctdb_add_node ( ) as
* part of loading the nodes file or by
* ctdb_tcp_listen_automatic ( ) when the transport is
* initialised . At some point we should de - optimise this and
* pull it out into ctdb_start_daemon ( ) so it is done clearly
* and only in one place .
*/
ctdb - > pnn = - 1 ;
2013-10-21 12:33:10 +04:00
/* Default value for CTDB_BASE - don't override */
2014-06-23 12:03:17 +04:00
setenv ( " CTDB_BASE " , CTDB_ETCDIR , 0 ) ;
2018-04-26 13:32:30 +03:00
ctdb_base = getenv ( " CTDB_BASE " ) ;
if ( ctdb_base = = NULL ) {
D_ERR ( " CTDB_BASE not set \n " ) ;
exit ( 1 ) ;
}
2013-10-21 12:33:10 +04:00
2007-06-02 04:03:28 +04:00
/* tell ctdb what nodes are available */
2018-04-26 13:32:30 +03:00
ctdb - > nodes_file = talloc_asprintf ( ctdb , " %s/nodes " , ctdb_base ) ;
2018-03-14 07:34:57 +03:00
if ( ctdb - > nodes_file = = NULL ) {
DEBUG ( DEBUG_ERR , ( __location__ " Out of memory \n " ) ) ;
exit ( 1 ) ;
2013-10-21 12:33:10 +04:00
}
2008-02-19 06:44:48 +03:00
ctdb_load_nodes_file ( ctdb ) ;
2007-06-02 04:03:28 +04:00
2013-10-21 12:36:36 +04:00
ctdb - > db_directory = options . db_dir ;
2014-06-06 09:24:20 +04:00
mkdir_p_or_die ( ctdb - > db_directory , 0700 ) ;
2013-10-21 12:36:36 +04:00
2013-10-21 12:36:36 +04:00
ctdb - > db_directory_persistent = options . db_dir_persistent ;
2014-06-06 09:24:20 +04:00
mkdir_p_or_die ( ctdb - > db_directory_persistent , 0700 ) ;
2013-10-21 12:36:36 +04:00
2013-10-21 12:36:36 +04:00
ctdb - > db_directory_state = options . db_dir_state ;
2014-06-06 09:24:20 +04:00
mkdir_p_or_die ( ctdb - > db_directory_state , 0700 ) ;
2007-06-02 04:03:28 +04:00
2018-03-09 08:27:32 +03:00
ctdb - > event_script_dir = talloc_asprintf ( ctdb ,
" %s/events.d " ,
2018-04-26 13:32:30 +03:00
ctdb_base ) ;
2018-03-09 08:27:32 +03:00
if ( ctdb - > event_script_dir = = NULL ) {
DBG_ERR ( " Out of memory \n " ) ;
exit ( 1 ) ;
2007-08-15 08:44:03 +04:00
}
2009-03-31 07:23:31 +04:00
if ( options . notification_script ! = NULL ) {
2018-03-29 06:50:17 +03:00
ctdb - > notification_script = talloc_strdup (
ctdb , options . notification_script ) ;
2018-03-29 06:58:49 +03:00
} else {
ctdb - > notification_script = talloc_asprintf ( ctdb ,
" %s/notify.sh " ,
ctdb_base ) ;
}
if ( ctdb - > notification_script = = NULL ) {
D_ERR ( " Unable to set notification script \n " ) ;
exit ( 1 ) ;
2009-03-31 07:23:31 +04:00
}
2016-06-17 11:09:37 +03:00
ctdb - > valgrinding = ( options . valgrinding = = 1 ) ;
2016-06-20 11:42:43 +03:00
ctdb - > do_setsched = ( options . nosetsched ! = 1 ) ;
if ( ctdb - > valgrinding ) {
2016-06-17 11:10:16 +03:00
ctdb - > do_setsched = false ;
2011-01-24 03:42:50 +03:00
}
2007-07-13 02:47:02 +04:00
2016-06-17 11:09:37 +03:00
ctdb - > do_checkpublicip = ( options . no_publicipcheck = = 0 ) ;
2012-03-22 08:27:25 +04:00
2009-12-07 15:28:11 +03:00
if ( options . max_persistent_check_errors < 0 ) {
ctdb - > max_persistent_check_errors = 0xFFFFFFFFFFFFFFFFLL ;
} else {
ctdb - > max_persistent_check_errors = ( uint64_t ) options . max_persistent_check_errors ;
}
2007-04-29 18:19:40 +04:00
/* start the protocol running (as a child) */
2014-08-08 14:57:05 +04:00
return ctdb_start_daemon ( ctdb , interactive ? false : true ) ;
2007-04-11 12:54:22 +04:00
}