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
*/
# include "includes.h"
# include "lib/events/events.h"
# include "system/filesys.h"
# include "popt.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"
2007-04-17 16:16:50 +04:00
# include "cmdline.h"
2007-04-30 00:42:23 +04:00
# include "../include/ctdb_private.h"
2007-04-11 15:17:36 +04:00
2007-05-29 06:49:25 +04:00
static struct {
2007-06-02 04:03:28 +04:00
const char * nlist ;
const char * transport ;
const char * myaddress ;
2007-05-29 06:49:25 +04:00
const char * public_address_list ;
2007-08-15 08:44:03 +04:00
const char * event_script_dir ;
2007-05-29 09:26:38 +04:00
const char * logfile ;
2007-06-02 04:03:28 +04:00
const char * recovery_lock_file ;
const char * db_dir ;
2007-09-21 06:24:02 +04:00
const char * db_dir_persistent ;
2007-09-10 08:26:35 +04:00
const char * public_interface ;
2007-10-10 03:42:32 +04:00
const char * single_public_ip ;
2007-11-26 02:52:55 +03:00
const char * node_ip ;
2007-07-13 02:47:02 +04:00
int no_setsched ;
2007-05-29 06:49:25 +04:00
} options = {
2007-06-02 04:03:28 +04:00
. nlist = ETCDIR " /ctdb/nodes " ,
. transport = " tcp " ,
2007-08-15 08:44:03 +04:00
. event_script_dir = ETCDIR " /ctdb/events.d " ,
2007-06-02 04:03:28 +04:00
. logfile = VARDIR " /log/log.ctdb " ,
. db_dir = VARDIR " /ctdb " ,
2007-09-21 06:24:02 +04:00
. db_dir_persistent = VARDIR " /ctdb/persistent " ,
2007-05-29 06:49:25 +04:00
} ;
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 ;
ctdb - > statistics . node_packets_recv + + ;
/* 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 | |
( ( struct ctdb_req_call * ) hdr ) - > hopcount = = 0 ) {
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 ;
2007-04-11 12:54:22 +04:00
struct poptOption popt_options [ ] = {
POPT_AUTOHELP
2007-04-17 16:13:06 +04:00
POPT_CTDB_CMDLINE
2007-05-15 03:44:33 +04:00
{ " interactive " , ' i ' , POPT_ARG_NONE , & interactive , 0 , " don't fork " , NULL } ,
2007-05-29 06:49:25 +04:00
{ " public-addresses " , 0 , POPT_ARG_STRING , & options . public_address_list , 0 , " public address list file " , " filename " } ,
2007-09-10 08:26:35 +04:00
{ " public-interface " , 0 , POPT_ARG_STRING , & options . public_interface , 0 , " public interface " , " interface " } ,
2007-10-10 03:42:32 +04:00
{ " single-public-ip " , 0 , POPT_ARG_STRING , & options . single_public_ip , 0 , " single public ip " , " ip-address " } ,
2007-08-15 08:44:03 +04:00
{ " event-script-dir " , 0 , POPT_ARG_STRING , & options . event_script_dir , 0 , " event script directory " , " dirname " } ,
2007-05-29 09:26:38 +04:00
{ " logfile " , 0 , POPT_ARG_STRING , & options . logfile , 0 , " log file location " , " filename " } ,
2007-06-02 04:03:28 +04:00
{ " nlist " , 0 , POPT_ARG_STRING , & options . nlist , 0 , " node list file " , " filename " } ,
2007-11-26 02:52:55 +03:00
{ " node-ip " , 0 , POPT_ARG_STRING , & options . node_ip , 0 , " node ip " , " ip-address " } ,
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 } ,
2007-06-02 05:36:42 +04:00
{ " reclock " , 0 , POPT_ARG_STRING , & options . recovery_lock_file , 0 , " location of recovery lock file " , " filename " } ,
2007-07-13 02:47:02 +04:00
{ " nosetsched " , 0 , POPT_ARG_NONE , & options . no_setsched , 0 , " disable setscheduler SCHED_FIFO call " , 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 ;
int extra_argc = 0 ;
poptContext pc ;
struct event_context * ev ;
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 ) ;
}
}
/* setup the remaining options for the main program to use */
extra_argv = poptGetArgs ( pc ) ;
if ( extra_argv ) {
extra_argv + + ;
while ( extra_argv [ extra_argc ] ) extra_argc + + ;
}
2007-06-02 05:36:42 +04:00
if ( ! options . recovery_lock_file ) {
DEBUG ( 0 , ( " You must specifiy the location of a recovery lock file with --reclock \n " ) ) ;
exit ( 1 ) ;
}
2008-01-16 01:44:48 +03:00
talloc_enable_null_tracking ( ) ;
2008-01-10 01:43:14 +03:00
ctdb_block_signal ( SIGPIPE ) ;
2007-04-11 15:17:36 +04:00
2007-04-11 12:54:22 +04:00
ev = event_context_init ( NULL ) ;
2007-04-17 16:13:06 +04:00
ctdb = ctdb_cmdline_init ( ev ) ;
2007-04-11 12:54:22 +04:00
2007-08-21 03:33:03 +04:00
ret = ctdb_set_logfile ( ctdb , options . logfile ) ;
if ( ret = = - 1 ) {
printf ( " ctdb_set_logfile to %s failed - %s \n " , options . logfile , ctdb_errstr ( ctdb ) ) ;
exit ( 1 ) ;
}
DEBUG ( 0 , ( " Starting CTDB daemon \n " ) ) ;
2007-06-07 16:06:19 +04:00
ctdb - > recovery_mode = CTDB_RECOVERY_NORMAL ;
ctdb - > recovery_master = ( uint32_t ) - 1 ;
ctdb - > upcalls = & ctdb_upcalls ;
ctdb - > idr = idr_init ( ctdb ) ;
ctdb - > recovery_lock_fd = - 1 ;
ctdb_tunables_set_defaults ( ctdb ) ;
2007-06-02 05:36:42 +04:00
ret = ctdb_set_recovery_lock_file ( ctdb , options . recovery_lock_file ) ;
if ( ret = = - 1 ) {
2007-08-21 03:33:03 +04:00
DEBUG ( 0 , ( " ctdb_set_recovery_lock_file failed - %s \n " , ctdb_errstr ( ctdb ) ) ) ;
2007-06-02 05:36:42 +04:00
exit ( 1 ) ;
}
2007-06-02 04:03:28 +04:00
ret = ctdb_set_transport ( ctdb , options . transport ) ;
if ( ret = = - 1 ) {
2007-08-21 03:33:03 +04:00
DEBUG ( 0 , ( " 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 ) {
2007-08-21 03:33:03 +04:00
DEBUG ( 0 , ( " ctdb_set_address failed - %s \n " , ctdb_errstr ( ctdb ) ) ) ;
2007-06-02 04:03:28 +04:00
exit ( 1 ) ;
}
}
/* tell ctdb what nodes are available */
ret = ctdb_set_nlist ( ctdb , options . nlist ) ;
if ( ret = = - 1 ) {
2007-08-21 03:33:03 +04:00
DEBUG ( 0 , ( " ctdb_set_nlist failed - %s \n " , ctdb_errstr ( ctdb ) ) ) ;
2007-06-02 04:03:28 +04:00
exit ( 1 ) ;
}
2007-11-26 02:52:55 +03:00
/* if a node-ip was specified, verify that it exists in the
nodes file
*/
if ( options . node_ip ! = NULL ) {
DEBUG ( 0 , ( " IP for this node is %s \n " , options . node_ip ) ) ;
ret = ctdb_ip_to_nodeid ( ctdb , options . node_ip ) ;
if ( ret = = - 1 ) {
DEBUG ( 0 , ( " The specified node-ip:%s is not a valid node address. Exiting. \n " , options . node_ip ) ) ;
exit ( 1 ) ;
}
ctdb - > node_ip = options . node_ip ;
DEBUG ( 0 , ( " This is node %d \n " , ret ) ) ;
}
2007-06-02 04:03:28 +04:00
if ( options . db_dir ) {
ret = ctdb_set_tdb_dir ( ctdb , options . db_dir ) ;
if ( ret = = - 1 ) {
2007-08-21 03:33:03 +04:00
DEBUG ( 0 , ( " ctdb_set_tdb_dir failed - %s \n " , ctdb_errstr ( ctdb ) ) ) ;
2007-06-02 04:03:28 +04:00
exit ( 1 ) ;
}
}
2007-09-21 06:24:02 +04:00
if ( options . db_dir_persistent ) {
ret = ctdb_set_tdb_dir_persistent ( ctdb , options . db_dir_persistent ) ;
if ( ret = = - 1 ) {
DEBUG ( 0 , ( " ctdb_set_tdb_dir_persistent failed - %s \n " , ctdb_errstr ( ctdb ) ) ) ;
exit ( 1 ) ;
}
}
2007-06-02 04:03:28 +04:00
2007-09-10 08:26:35 +04:00
if ( options . public_interface ) {
ctdb - > default_public_interface = talloc_strdup ( ctdb , options . public_interface ) ;
CTDB_NO_MEMORY ( ctdb , ctdb - > default_public_interface ) ;
}
2007-10-10 03:42:32 +04:00
if ( options . single_public_ip ) {
struct ctdb_vnn * svnn ;
if ( options . public_interface = = NULL ) {
DEBUG ( 0 , ( " --single_public_ip used but --public_interface is not specified. You must specify the public interface when using single public ip. Exiting \n " ) ) ;
exit ( 10 ) ;
}
svnn = talloc_zero ( ctdb , struct ctdb_vnn ) ;
CTDB_NO_MEMORY ( ctdb , svnn ) ;
ctdb - > single_ip_vnn = svnn ;
svnn - > iface = talloc_strdup ( svnn , options . public_interface ) ;
CTDB_NO_MEMORY ( ctdb , svnn - > iface ) ;
if ( inet_aton ( options . single_public_ip ,
& svnn - > public_address . sin_addr ) = = 0 ) {
DEBUG ( 0 , ( " Invalid --single-public-ip argument : %s . This is not a valid ip address. Exiting. \n " , options . single_public_ip ) ) ;
exit ( 10 ) ;
}
svnn - > public_address . sin_family = AF_INET ;
svnn - > public_address . sin_port = 0 ;
}
2007-05-29 06:49:25 +04:00
if ( options . public_address_list ) {
ret = ctdb_set_public_addresses ( ctdb , options . public_address_list ) ;
if ( ret = = - 1 ) {
2007-08-21 03:33:03 +04:00
DEBUG ( 0 , ( " Unable to setup public address list \n " ) ) ;
2007-05-29 06:49:25 +04:00
exit ( 1 ) ;
}
}
2007-08-15 08:44:03 +04:00
ret = ctdb_set_event_script_dir ( ctdb , options . event_script_dir ) ;
if ( ret = = - 1 ) {
2007-08-21 03:33:03 +04:00
DEBUG ( 0 , ( " Unable to setup event script directory \n " ) ) ;
2007-08-15 08:44:03 +04:00
exit ( 1 ) ;
}
2007-04-30 00:42:23 +04:00
/* useful default logfile */
if ( ctdb - > logfile = = NULL ) {
2007-09-04 04:06:36 +04:00
char * name = talloc_asprintf ( ctdb , " %s/log.ctdb.pnn%u " ,
VARDIR , ctdb - > pnn ) ;
2007-04-30 00:42:23 +04:00
ctdb_set_logfile ( ctdb , name ) ;
talloc_free ( name ) ;
}
2007-07-13 03:14:31 +04:00
ctdb - > do_setsched = ! options . no_setsched ;
2007-07-13 02:47:02 +04:00
2007-09-14 08:14:03 +04:00
/* setup a environment variable for the event scripts to use to find the
installation directory */
setenv ( " CTDB_BASE " , ETCDIR " /ctdb " , 1 ) ;
2007-04-29 18:19:40 +04:00
/* start the protocol running (as a child) */
2007-05-15 03:44:33 +04:00
return ctdb_start_daemon ( ctdb , interactive ? False : True ) ;
2007-04-11 12:54:22 +04:00
}