2004-06-24 12:02:38 +04:00
/*
* Copyright ( C ) 2002 - 2004 Sistina Software , Inc . All rights reserved .
2014-03-21 15:56:01 +04:00
* Copyright ( C ) 2004 - 2014 Red Hat , Inc . All rights reserved .
2004-06-24 12:02:38 +04:00
*
* This file is part of LVM2 .
*
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
* of the GNU General Public License v .2 .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software Foundation ,
2016-01-21 13:49:46 +03:00
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
2004-06-24 12:02:38 +04:00
*/
/*
* CLVMD : Cluster LVM daemon
*/
2010-06-21 19:56:57 +04:00
# include "clvmd-common.h"
2008-11-04 19:41:47 +03:00
2004-06-24 12:02:38 +04:00
# include "clvmd-comms.h"
# include "clvm.h"
# include "clvmd.h"
2010-06-21 19:56:57 +04:00
# include "lvm-functions.h"
# include "lvm-version.h"
2006-10-04 12:22:16 +04:00
# include "refresh_clvmd.h"
2010-06-21 19:56:57 +04:00
# ifdef HAVE_COROSYNC_CONFDB_H
# include <corosync/confdb.h>
# endif
2014-04-15 15:27:47 +04:00
# include <pthread.h>
# include <getopt.h>
# include <ctype.h>
# include <stdarg.h>
2010-06-21 19:56:57 +04:00
# include <fcntl.h>
# include <netinet/in.h>
# include <signal.h>
# include <stddef.h>
# include <syslog.h>
# include <sys/un.h>
# include <sys/utsname.h>
2004-06-24 12:02:38 +04:00
# ifndef TRUE
# define TRUE 1
# endif
# ifndef FALSE
# define FALSE 0
# endif
2007-03-29 17:59:33 +04:00
# define MAX_RETRIES 4
2012-02-28 14:42:20 +04:00
# define MAX_MISSING_LEN 8000 /* Max supported clvmd message size ? */
2007-03-29 17:59:33 +04:00
2005-01-13 16:24:02 +03:00
# define ISLOCAL_CSID(c) (memcmp(c, our_csid, max_csid_len) == 0)
2004-06-24 12:02:38 +04:00
/* Head of the fd list. Also contains
the cluster_socket details */
static struct local_client local_client_head ;
static unsigned short global_xid = 0 ; /* Last transaction ID issued */
2006-10-09 18:11:57 +04:00
struct cluster_ops * clops = NULL ;
2005-01-13 16:24:02 +03:00
2004-06-24 12:02:38 +04:00
static char our_csid [ MAX_CSID_LEN ] ;
2005-01-13 16:24:02 +03:00
static unsigned max_csid_len ;
static unsigned max_cluster_message ;
static unsigned max_cluster_member_name_len ;
2004-06-24 12:02:38 +04:00
/* Structure of items on the LVM thread list */
struct lvm_thread_cmd {
2008-11-04 01:14:30 +03:00
struct dm_list list ;
2004-06-24 12:02:38 +04:00
struct local_client * client ;
struct clvm_header * msg ;
char csid [ MAX_CSID_LEN ] ;
int remote ; /* Flag */
int msglen ;
unsigned short xid ;
} ;
2007-08-17 15:51:23 +04:00
2010-04-20 18:07:37 +04:00
struct lvm_startup_params {
2011-09-25 00:48:34 +04:00
struct dm_hash_table * excl_uuid ;
2010-04-20 18:07:37 +04:00
} ;
2011-01-18 02:13:14 +03:00
static debug_t debug = DEBUG_OFF ;
static int foreground_mode = 0 ;
2004-06-24 12:02:38 +04:00
static pthread_t lvm_thread ;
2011-10-11 13:18:49 +04:00
/* Stack size 128KiB for thread, must be bigger then DEFAULT_RESERVED_STACK */
static const size_t STACK_SIZE = 128 * 1024 ;
static pthread_attr_t stack_attr ;
2014-03-21 21:01:38 +04:00
static int lvm_thread_exit = 0 ;
2004-06-24 12:02:38 +04:00
static pthread_mutex_t lvm_thread_mutex ;
2017-07-01 02:58:39 +03:00
static pthread_mutex_t _debuglog_mutex ;
2004-06-24 12:02:38 +04:00
static pthread_cond_t lvm_thread_cond ;
2011-10-11 13:26:04 +04:00
static pthread_barrier_t lvm_start_barrier ;
2008-11-04 01:14:30 +03:00
static struct dm_list lvm_cmd_head ;
2005-03-21 17:16:16 +03:00
static volatile sig_atomic_t quit = 0 ;
2005-04-19 14:36:42 +04:00
static volatile sig_atomic_t reread_config = 0 ;
2004-10-06 14:12:34 +04:00
static int child_pipe [ 2 ] ;
/* Reasons the daemon failed initialisation */
# define DFAIL_INIT 1
# define DFAIL_LOCAL_SOCK 2
# define DFAIL_CLUSTER_IF 3
# define DFAIL_MALLOC 4
2006-11-30 12:44:07 +03:00
# define DFAIL_TIMEOUT 5
2004-10-06 14:12:34 +04:00
# define SUCCESS 0
2004-06-24 12:02:38 +04:00
2011-08-09 22:11:00 +04:00
typedef enum { IF_AUTO , IF_CMAN , IF_OPENAIS , IF_COROSYNC , IF_SINGLENODE } if_type_t ;
2009-02-02 17:34:24 +03:00
2004-06-24 12:02:38 +04:00
/* Prototypes for code further down */
static void sigusr2_handler ( int sig ) ;
2005-04-19 14:36:42 +04:00
static void sighup_handler ( int sig ) ;
2004-06-24 12:02:38 +04:00
static void sigterm_handler ( int sig ) ;
static void send_local_reply ( struct local_client * client , int status ,
int clientid ) ;
static void free_reply ( struct local_client * client ) ;
static void send_version_message ( void ) ;
static void * pre_and_post_thread ( void * arg ) ;
2007-05-02 16:22:40 +04:00
static int send_message ( void * buf , int msglen , const char * csid , int fd ,
2004-06-24 12:02:38 +04:00
const char * errtext ) ;
static int read_from_local_sock ( struct local_client * thisfd ) ;
2013-09-09 02:01:44 +04:00
static int cleanup_zombie ( struct local_client * thisfd ) ;
2004-06-24 12:02:38 +04:00
static int process_local_command ( struct clvm_header * msg , int msglen ,
struct local_client * client ,
unsigned short xid ) ;
static void process_remote_command ( struct clvm_header * msg , int msglen , int fd ,
2007-05-02 16:22:40 +04:00
const char * csid ) ;
static int process_reply ( const struct clvm_header * msg , int msglen ,
const char * csid ) ;
2004-06-24 12:02:38 +04:00
static int open_local_sock ( void ) ;
2010-07-28 17:55:42 +04:00
static void close_local_sock ( int local_socket ) ;
2007-08-17 15:51:23 +04:00
static int check_local_clvmd ( void ) ;
2004-06-24 12:02:38 +04:00
static struct local_client * find_client ( int clientid ) ;
2014-03-21 14:34:25 +04:00
static void main_loop ( int cmd_timeout ) ;
2006-11-30 12:44:07 +03:00
static void be_daemon ( int start_timeout ) ;
2004-06-24 12:02:38 +04:00
static int check_all_clvmds_running ( struct local_client * client ) ;
static int local_rendezvous_callback ( struct local_client * thisfd , char * buf ,
2007-05-02 16:22:40 +04:00
int len , const char * csid ,
2004-06-24 12:02:38 +04:00
struct local_client * * new_client ) ;
2011-03-30 16:48:16 +04:00
static void * lvm_thread_fn ( void * ) __attribute__ ( ( noreturn ) ) ;
2004-06-24 12:02:38 +04:00
static int add_to_lvmqueue ( struct local_client * client , struct clvm_header * msg ,
2007-05-02 16:22:40 +04:00
int msglen , const char * csid ) ;
2004-06-24 12:02:38 +04:00
static int distribute_command ( struct local_client * thisfd ) ;
static void hton_clvm ( struct clvm_header * hdr ) ;
static void ntoh_clvm ( struct clvm_header * hdr ) ;
static void add_reply_to_list ( struct local_client * client , int status ,
2007-05-02 16:22:40 +04:00
const char * csid , const char * buf , int len ) ;
2009-02-02 17:34:24 +03:00
static if_type_t parse_cluster_interface ( char * ifname ) ;
2009-09-01 13:48:01 +04:00
static if_type_t get_cluster_type ( void ) ;
2004-06-24 12:02:38 +04:00
2010-12-20 16:48:28 +03:00
static void usage ( const char * prog , FILE * file )
2004-06-24 12:02:38 +04:00
{
2011-01-18 02:13:14 +03:00
fprintf ( file , " Usage: %s [options] \n "
2014-11-19 03:04:24 +03:00
" -C Sets debug level (from -d) on all clvmd instances clusterwide \n "
2015-09-21 23:40:49 +03:00
" -d[<n>] Set debug logging (0:none, 1:stderr (implies -f option), 2:syslog) \n "
2014-11-19 03:04:24 +03:00
" -E<uuid> Take this lock uuid as exclusively locked resource (for restart) \n "
2011-01-18 02:13:14 +03:00
" -f Don't fork, run in the foreground \n "
2014-11-19 03:04:24 +03:00
" -h Show this help information \n "
2010-12-20 16:48:28 +03:00
" -I<cmgr> Cluster manager (default: auto) \n "
" Available cluster managers: "
2009-02-02 17:34:24 +03:00
# ifdef USE_COROSYNC
2010-12-20 16:48:28 +03:00
" corosync "
2009-02-02 17:34:24 +03:00
# endif
# ifdef USE_CMAN
2010-12-20 16:48:28 +03:00
" cman "
2009-02-02 17:34:24 +03:00
# endif
# ifdef USE_OPENAIS
2010-12-20 16:48:28 +03:00
" openais "
2009-02-02 17:34:24 +03:00
# endif
2010-03-18 12:19:30 +03:00
# ifdef USE_SINGLENODE
2010-12-20 16:48:28 +03:00
" singlenode "
2009-02-02 17:34:24 +03:00
# endif
2015-05-03 01:30:14 +03:00
" \n "
2014-11-19 03:04:24 +03:00
" -R Tell all running clvmds in the cluster to reload their device cache \n "
" -S Restart clvmd, preserving exclusive locks \n "
" -t<secs> Command timeout (default: 60 seconds) \n "
" -T<secs> Startup timeout (default: 0 seconds) \n "
" -V Show version of clvmd \n "
2010-12-20 16:48:28 +03:00
" \n " , prog ) ;
2004-06-24 12:02:38 +04:00
}
2004-10-06 14:12:34 +04:00
/* Called to signal the parent how well we got on during initialisation */
static void child_init_signal ( int status )
{
2010-12-01 13:46:20 +03:00
if ( child_pipe [ 1 ] ) {
/* FIXME Use a proper wrapper around write */
if ( write ( child_pipe [ 1 ] , & status , sizeof ( status ) ) < 0 )
log_sys_error ( " write " , " child_pipe " ) ;
if ( close ( child_pipe [ 1 ] ) )
log_sys_error ( " close " , " child_pipe " ) ;
2004-10-06 16:36:47 +04:00
}
2010-11-08 22:37:40 +03:00
}
static __attribute__ ( ( noreturn ) ) void child_init_signal_and_exit ( int status )
{
child_init_signal ( status ) ;
exit ( status ) ;
2004-10-06 14:12:34 +04:00
}
2010-10-27 15:40:14 +04:00
static void safe_close ( int * fd )
{
if ( * fd > = 0 ) {
int to_close = * fd ;
* fd = - 1 ;
2012-02-27 15:28:47 +04:00
if ( close ( to_close ) )
log_sys_error ( " close " , " " ) ; /* path */
2010-10-27 15:40:14 +04:00
}
}
2004-10-06 14:12:34 +04:00
2006-11-30 16:19:42 +03:00
void debuglog ( const char * fmt , . . . )
{
time_t P ;
va_list ap ;
2007-08-17 15:51:23 +04:00
static int syslog_init = 0 ;
2014-04-09 09:51:36 +04:00
char buf_ctime [ 64 ] ;
2007-08-17 15:51:23 +04:00
2011-01-18 02:13:14 +03:00
switch ( clvmd_get_debug ( ) ) {
case DEBUG_STDERR :
2017-07-01 02:58:39 +03:00
pthread_mutex_lock ( & _debuglog_mutex ) ;
2007-08-17 15:51:23 +04:00
va_start ( ap , fmt ) ;
time ( & P ) ;
2014-04-09 09:51:36 +04:00
fprintf ( stderr , " CLVMD[%x]: %.15s " , ( int ) pthread_self ( ) , ctime_r ( & P , buf_ctime ) + 4 ) ;
2007-08-17 15:51:23 +04:00
vfprintf ( stderr , fmt , ap ) ;
va_end ( ap ) ;
2015-07-24 01:10:16 +03:00
fflush ( stderr ) ;
2017-07-01 02:58:39 +03:00
pthread_mutex_unlock ( & _debuglog_mutex ) ;
2011-01-18 02:13:14 +03:00
break ;
case DEBUG_SYSLOG :
2017-07-01 02:58:39 +03:00
pthread_mutex_lock ( & _debuglog_mutex ) ;
2008-06-13 11:44:14 +04:00
if ( ! syslog_init ) {
2007-08-17 15:51:23 +04:00
openlog ( " clvmd " , LOG_PID , LOG_DAEMON ) ;
2008-06-13 11:44:14 +04:00
syslog_init = 1 ;
}
2006-11-30 16:19:42 +03:00
2007-08-17 15:51:23 +04:00
va_start ( ap , fmt ) ;
vsyslog ( LOG_DEBUG , fmt , ap ) ;
va_end ( ap ) ;
2017-07-01 02:58:39 +03:00
pthread_mutex_unlock ( & _debuglog_mutex ) ;
2011-01-18 02:13:14 +03:00
break ;
case DEBUG_OFF :
break ;
2007-08-17 15:51:23 +04:00
}
2006-11-30 16:19:42 +03:00
}
2011-01-18 02:13:14 +03:00
void clvmd_set_debug ( debug_t new_debug )
{
if ( ! foreground_mode & & new_debug = = DEBUG_STDERR )
new_debug = DEBUG_SYSLOG ;
if ( new_debug > DEBUG_SYSLOG )
new_debug = DEBUG_SYSLOG ;
debug = new_debug ;
}
debug_t clvmd_get_debug ( void )
{
return debug ;
}
2013-04-21 00:23:36 +04:00
int clvmd_get_foreground ( void )
{
return foreground_mode ;
}
2008-06-05 18:24:28 +04:00
static const char * decode_cmd ( unsigned char cmdl )
{
static char buf [ 128 ] ;
const char * command ;
switch ( cmdl ) {
2009-04-22 14:38:16 +04:00
case CLVMD_CMD_TEST :
command = " TEST " ;
2008-06-05 18:24:28 +04:00
break ;
2009-04-22 14:38:16 +04:00
case CLVMD_CMD_LOCK_VG :
command = " LOCK_VG " ;
2008-06-05 18:24:28 +04:00
break ;
2009-04-22 14:38:16 +04:00
case CLVMD_CMD_LOCK_LV :
command = " LOCK_LV " ;
2008-06-05 18:24:28 +04:00
break ;
2009-04-22 14:38:16 +04:00
case CLVMD_CMD_REFRESH :
command = " REFRESH " ;
2008-06-05 18:24:28 +04:00
break ;
2009-04-22 14:38:16 +04:00
case CLVMD_CMD_SET_DEBUG :
command = " SET_DEBUG " ;
2008-06-05 18:24:28 +04:00
break ;
2009-04-22 14:38:16 +04:00
case CLVMD_CMD_GET_CLUSTERNAME :
2008-06-05 18:24:28 +04:00
command = " GET_CLUSTERNAME " ;
break ;
2009-04-22 14:38:16 +04:00
case CLVMD_CMD_VG_BACKUP :
command = " VG_BACKUP " ;
2008-06-05 18:24:28 +04:00
break ;
2009-04-22 14:38:16 +04:00
case CLVMD_CMD_REPLY :
command = " REPLY " ;
2008-06-05 18:24:28 +04:00
break ;
2009-04-22 14:38:16 +04:00
case CLVMD_CMD_VERSION :
command = " VERSION " ;
2008-06-05 18:24:28 +04:00
break ;
2009-04-22 14:38:16 +04:00
case CLVMD_CMD_GOAWAY :
command = " GOAWAY " ;
2008-06-05 18:24:28 +04:00
break ;
2009-04-22 14:38:16 +04:00
case CLVMD_CMD_LOCK :
command = " LOCK " ;
2008-06-05 18:24:28 +04:00
break ;
2009-04-22 14:38:16 +04:00
case CLVMD_CMD_UNLOCK :
command = " UNLOCK " ;
2008-06-05 18:24:28 +04:00
break ;
2009-05-19 14:38:58 +04:00
case CLVMD_CMD_LOCK_QUERY :
command = " LOCK_QUERY " ;
break ;
2010-04-20 18:07:37 +04:00
case CLVMD_CMD_RESTART :
command = " RESTART " ;
break ;
2011-06-28 17:42:15 +04:00
case CLVMD_CMD_SYNC_NAMES :
command = " SYNC_NAMES " ;
break ;
2009-04-22 14:38:16 +04:00
default :
command = " unknown " ;
2008-06-05 18:24:28 +04:00
break ;
}
2012-02-27 14:17:06 +04:00
snprintf ( buf , sizeof ( buf ) , " %s (0x%x) " , command , cmdl ) ;
2008-06-05 18:24:28 +04:00
return buf ;
}
2010-07-13 17:51:01 +04:00
static void remove_lockfile ( void )
{
2010-12-01 15:41:49 +03:00
if ( unlink ( CLVMD_PIDFILE ) )
log_sys_error ( " unlink " , CLVMD_PIDFILE ) ;
2010-07-13 17:51:01 +04:00
}
2010-07-28 17:55:42 +04:00
/*
* clvmd require dm - ioctl capability for operation
*/
2010-08-03 17:06:35 +04:00
static void check_permissions ( void )
2010-07-28 17:55:42 +04:00
{
if ( getuid ( ) | | geteuid ( ) ) {
log_error ( " Cannot run as a non-root user. " ) ;
/*
* Fail cleanly here if not run as root , instead of failing
* later when attempting a root - only operation
* Preferred exit code from an initscript for this .
*/
exit ( 4 ) ;
}
}
2004-06-24 12:02:38 +04:00
int main ( int argc , char * argv [ ] )
{
int local_sock ;
2011-03-30 16:36:19 +04:00
struct local_client * newfd , * delfd ;
2010-04-20 18:07:37 +04:00
struct lvm_startup_params lvm_params ;
2011-03-08 16:27:39 +03:00
int opt ;
2004-06-24 12:02:38 +04:00
int cmd_timeout = DEFAULT_CMD_TIMEOUT ;
2006-11-30 12:44:07 +03:00
int start_timeout = 0 ;
2009-02-02 17:34:24 +03:00
if_type_t cluster_iface = IF_AUTO ;
2004-06-24 12:02:38 +04:00
sigset_t ss ;
2012-03-02 01:14:43 +04:00
debug_t debug_opt = DEBUG_OFF ;
2011-01-18 02:13:14 +03:00
debug_t debug_arg = DEBUG_OFF ;
2007-08-17 15:51:23 +04:00
int clusterwide_opt = 0 ;
2010-12-13 13:49:02 +03:00
mode_t old_mask ;
2012-02-27 15:26:25 +04:00
int ret = 1 ;
2004-06-24 12:02:38 +04:00
2011-03-08 16:27:39 +03:00
struct option longopts [ ] = {
{ " help " , 0 , 0 , ' h ' } ,
{ NULL , 0 , 0 , 0 }
} ;
2011-09-25 00:48:34 +04:00
if ( ! ( lvm_params . excl_uuid = dm_hash_create ( 128 ) ) ) {
fprintf ( stderr , " Failed to allocate hash table \n " ) ;
return 1 ;
}
2004-06-24 12:02:38 +04:00
/* Deal with command-line arguments */
opterr = 0 ;
optind = 0 ;
2014-11-25 04:57:22 +03:00
while ( ( opt = getopt_long ( argc , argv , " Vhfd:t:RST:CI:E: " ,
2011-03-08 16:27:39 +03:00
longopts , NULL ) ) ! = - 1 ) {
2004-06-24 12:02:38 +04:00
switch ( opt ) {
case ' h ' :
usage ( argv [ 0 ] , stdout ) ;
exit ( 0 ) ;
2006-10-04 12:22:16 +04:00
case ' R ' :
2010-07-28 17:55:42 +04:00
check_permissions ( ) ;
2012-08-16 22:26:46 +04:00
ret = ( refresh_clvmd ( 1 ) = = 1 ) ? 0 : 1 ;
goto out ;
2010-04-20 18:07:37 +04:00
case ' S ' :
2010-07-28 17:55:42 +04:00
check_permissions ( ) ;
2012-02-27 15:26:25 +04:00
ret = ( restart_clvmd ( clusterwide_opt ) = = 1 ) ? 0 : 1 ;
goto out ;
2006-10-04 12:22:16 +04:00
2007-08-17 15:51:23 +04:00
case ' C ' :
clusterwide_opt = 1 ;
break ;
2004-06-24 12:02:38 +04:00
case ' d ' :
2012-03-02 01:14:43 +04:00
debug_opt = DEBUG_STDERR ;
2012-12-13 22:54:07 +04:00
debug_arg = ( debug_t ) atoi ( optarg ) ;
2011-01-18 02:13:14 +03:00
if ( debug_arg = = DEBUG_STDERR )
foreground_mode = 1 ;
2004-06-24 12:02:38 +04:00
break ;
2011-01-18 02:13:14 +03:00
case ' f ' :
foreground_mode = 1 ;
break ;
2004-06-24 12:02:38 +04:00
case ' t ' :
cmd_timeout = atoi ( optarg ) ;
if ( ! cmd_timeout ) {
fprintf ( stderr , " command timeout is invalid \n " ) ;
usage ( argv [ 0 ] , stderr ) ;
exit ( 1 ) ;
}
break ;
2009-02-02 17:34:24 +03:00
case ' I ' :
cluster_iface = parse_cluster_interface ( optarg ) ;
break ;
2011-09-25 00:48:34 +04:00
case ' E ' :
if ( ! dm_hash_insert ( lvm_params . excl_uuid , optarg , optarg ) ) {
fprintf ( stderr , " Failed to allocate hash entry \n " ) ;
2012-02-27 15:26:25 +04:00
goto out ;
2011-09-25 00:48:34 +04:00
}
break ;
2006-11-30 12:44:07 +03:00
case ' T ' :
start_timeout = atoi ( optarg ) ;
if ( start_timeout < = 0 ) {
fprintf ( stderr , " startup timeout is invalid \n " ) ;
usage ( argv [ 0 ] , stderr ) ;
exit ( 1 ) ;
}
break ;
2004-06-24 12:02:38 +04:00
case ' V ' :
2004-10-06 16:36:47 +04:00
printf ( " Cluster LVM daemon version: %s \n " , LVM_VERSION ) ;
printf ( " Protocol version: %d.%d.%d \n " ,
2004-06-24 12:02:38 +04:00
CLVMD_MAJOR_VERSION , CLVMD_MINOR_VERSION ,
CLVMD_PATCH_VERSION ) ;
2010-02-02 11:54:29 +03:00
exit ( 0 ) ;
2004-06-24 12:02:38 +04:00
break ;
2011-03-08 16:27:39 +03:00
default :
usage ( argv [ 0 ] , stderr ) ;
exit ( 2 ) ;
2004-06-24 12:02:38 +04:00
}
}
2010-07-28 17:55:42 +04:00
check_permissions ( ) ;
2010-04-13 23:54:16 +04:00
/*
* Switch to C locale to avoid reading large locale - archive file
* used by some glibc ( on some distributions it takes over 100 MB ) .
* Daemon currently needs to use mlockall ( ) .
*/
2013-01-22 14:25:02 +04:00
if ( setenv ( " LC_ALL " , " C " , 1 ) )
perror ( " Cannot set LC_ALL to C " ) ;
2010-04-13 23:54:16 +04:00
2011-01-18 02:13:14 +03:00
/* Setting debug options on an existing clvmd */
2012-01-26 21:55:55 +04:00
if ( debug_opt & & ! check_local_clvmd ( ) ) {
2012-01-26 02:36:33 +04:00
dm_hash_destroy ( lvm_params . excl_uuid ) ;
2011-01-18 02:13:14 +03:00
return debug_clvmd ( debug_arg , clusterwide_opt ) = = 1 ? 0 : 1 ;
2012-01-26 21:55:55 +04:00
}
2011-01-18 02:13:14 +03:00
2012-10-19 17:18:19 +04:00
clvmd_set_debug ( debug_arg ) ;
2011-01-18 02:13:14 +03:00
2004-10-06 14:12:34 +04:00
/* Fork into the background (unless requested not to) */
2011-01-18 02:13:14 +03:00
if ( ! foreground_mode )
2006-11-30 12:44:07 +03:00
be_daemon ( start_timeout ) ;
2004-10-06 14:12:34 +04:00
2011-03-03 16:05:40 +03:00
( void ) dm_prepare_selinux_context ( DEFAULT_RUN_DIR , S_IFDIR ) ;
2010-12-13 13:49:02 +03:00
old_mask = umask ( 0077 ) ;
if ( dm_create_dir ( DEFAULT_RUN_DIR ) = = 0 ) {
DEBUGLOG ( " clvmd: unable to create %s directory \n " ,
DEFAULT_RUN_DIR ) ;
umask ( old_mask ) ;
exit ( 1 ) ;
}
umask ( old_mask ) ;
2010-07-13 17:51:01 +04:00
/* Create pidfile */
2010-12-13 13:43:56 +03:00
( void ) dm_prepare_selinux_context ( CLVMD_PIDFILE , S_IFREG ) ;
2010-07-13 17:51:01 +04:00
if ( dm_create_lockfile ( CLVMD_PIDFILE ) = = 0 ) {
DEBUGLOG ( " clvmd: unable to create lockfile \n " ) ;
exit ( 1 ) ;
}
2010-12-13 13:43:56 +03:00
( void ) dm_prepare_selinux_context ( NULL , 0 ) ;
2010-07-13 17:51:01 +04:00
atexit ( remove_lockfile ) ;
2004-06-24 12:02:38 +04:00
DEBUGLOG ( " CLVMD started \n " ) ;
/* Open the Unix socket we listen for commands on.
We do this before opening the cluster socket so that
potential clients will block rather than error if we are running
but the cluster is not ready yet */
local_sock = open_local_sock ( ) ;
2010-11-08 22:37:40 +03:00
if ( local_sock < 0 ) {
child_init_signal_and_exit ( DFAIL_LOCAL_SOCK ) ;
/* NOTREACHED */
}
2004-06-24 12:02:38 +04:00
/* Set up signal handlers, USR1 is for cluster change notifications (in cman)
USR2 causes child threads to exit .
2011-08-12 06:16:46 +04:00
( HUP used to cause gulm to re - read the nodes list from CCS . )
2004-06-24 12:02:38 +04:00
PIPE should be ignored */
signal ( SIGUSR2 , sigusr2_handler ) ;
2005-04-19 14:36:42 +04:00
signal ( SIGHUP , sighup_handler ) ;
2004-06-24 12:02:38 +04:00
signal ( SIGPIPE , SIG_IGN ) ;
2009-03-24 14:49:15 +03:00
/* Block SIGUSR2/SIGINT/SIGTERM in process */
2004-06-24 12:02:38 +04:00
sigemptyset ( & ss ) ;
sigaddset ( & ss , SIGUSR2 ) ;
2009-03-24 14:49:15 +03:00
sigaddset ( & ss , SIGINT ) ;
sigaddset ( & ss , SIGTERM ) ;
2004-06-24 12:02:38 +04:00
sigprocmask ( SIG_BLOCK , & ss , NULL ) ;
/* Initialise the LVM thread variables */
2008-11-04 01:14:30 +03:00
dm_list_init ( & lvm_cmd_head ) ;
2011-10-11 13:18:49 +04:00
if ( pthread_attr_init ( & stack_attr ) | |
2017-02-11 20:14:00 +03:00
pthread_attr_setstacksize ( & stack_attr , STACK_SIZE + getpagesize ( ) ) ) {
2011-10-11 13:18:49 +04:00
log_sys_error ( " pthread_attr_init " , " " ) ;
exit ( 1 ) ;
}
2004-06-24 12:02:38 +04:00
pthread_mutex_init ( & lvm_thread_mutex , NULL ) ;
2017-07-01 02:58:39 +03:00
pthread_mutex_init ( & _debuglog_mutex , NULL ) ;
2004-06-24 12:02:38 +04:00
pthread_cond_init ( & lvm_thread_cond , NULL ) ;
2011-10-11 13:26:04 +04:00
pthread_barrier_init ( & lvm_start_barrier , NULL , 2 ) ;
2004-06-24 12:02:38 +04:00
init_lvhash ( ) ;
/* Start the cluster interface */
2009-09-01 13:48:01 +04:00
if ( cluster_iface = = IF_AUTO )
cluster_iface = get_cluster_type ( ) ;
2005-01-13 16:24:02 +03:00
# ifdef USE_CMAN
2014-03-10 15:15:26 +04:00
if ( ( cluster_iface = = IF_AUTO | | cluster_iface = = IF_CMAN ) & &
( clops = init_cman_cluster ( ) ) ) {
2005-01-13 16:24:02 +03:00
max_csid_len = CMAN_MAX_CSID_LEN ;
max_cluster_message = CMAN_MAX_CLUSTER_MESSAGE ;
2006-03-14 17:18:34 +03:00
max_cluster_member_name_len = CMAN_MAX_NODENAME_LEN ;
2005-01-24 18:31:13 +03:00
syslog ( LOG_NOTICE , " Cluster LVM daemon started - connected to CMAN " ) ;
2005-01-13 16:24:02 +03:00
}
# endif
2009-01-22 13:21:12 +03:00
# ifdef USE_COROSYNC
if ( ! clops )
2014-03-10 15:15:26 +04:00
if ( ( ( cluster_iface = = IF_AUTO | | cluster_iface = = IF_COROSYNC ) & &
( clops = init_corosync_cluster ( ) ) ) ) {
2009-01-22 13:21:12 +03:00
max_csid_len = COROSYNC_CSID_LEN ;
max_cluster_message = COROSYNC_MAX_CLUSTER_MESSAGE ;
max_cluster_member_name_len = COROSYNC_MAX_CLUSTER_MEMBER_NAME_LEN ;
syslog ( LOG_NOTICE , " Cluster LVM daemon started - connected to Corosync " ) ;
}
# endif
2009-02-02 17:34:24 +03:00
# ifdef USE_OPENAIS
if ( ! clops )
2014-03-10 15:15:26 +04:00
if ( ( cluster_iface = = IF_AUTO | | cluster_iface = = IF_OPENAIS ) & &
( clops = init_openais_cluster ( ) ) ) {
2009-02-02 17:34:24 +03:00
max_csid_len = OPENAIS_CSID_LEN ;
max_cluster_message = OPENAIS_MAX_CLUSTER_MESSAGE ;
max_cluster_member_name_len = OPENAIS_MAX_CLUSTER_MEMBER_NAME_LEN ;
syslog ( LOG_NOTICE , " Cluster LVM daemon started - connected to OpenAIS " ) ;
}
# endif
2010-03-18 12:19:30 +03:00
# ifdef USE_SINGLENODE
if ( ! clops )
2010-07-28 18:01:40 +04:00
if ( cluster_iface = = IF_SINGLENODE & & ( clops = init_singlenode_cluster ( ) ) ) {
2010-03-18 12:19:30 +03:00
max_csid_len = SINGLENODE_CSID_LEN ;
max_cluster_message = SINGLENODE_MAX_CLUSTER_MESSAGE ;
max_cluster_member_name_len = MAX_CLUSTER_MEMBER_NAME_LEN ;
syslog ( LOG_NOTICE , " Cluster LVM daemon started - running in single-node mode " ) ;
}
# endif
2005-01-13 16:24:02 +03:00
if ( ! clops ) {
2004-06-24 12:02:38 +04:00
DEBUGLOG ( " Can't initialise cluster interface \n " ) ;
2014-03-20 12:21:34 +04:00
log_error ( " Can't initialise cluster interface. " ) ;
2010-11-08 22:37:40 +03:00
child_init_signal_and_exit ( DFAIL_CLUSTER_IF ) ;
/* NOTREACHED */
2004-06-24 12:02:38 +04:00
}
DEBUGLOG ( " Cluster ready, doing some more initialisation \n " ) ;
/* Save our CSID */
2005-01-13 16:24:02 +03:00
clops - > get_our_csid ( our_csid ) ;
2004-06-24 12:02:38 +04:00
/* Initialise the FD list head */
2005-01-13 16:24:02 +03:00
local_client_head . fd = clops - > get_main_cluster_fd ( ) ;
2004-06-24 12:02:38 +04:00
local_client_head . type = CLUSTER_MAIN_SOCK ;
2005-01-13 16:24:02 +03:00
local_client_head . callback = clops - > cluster_fd_callback ;
2004-06-24 12:02:38 +04:00
/* Add the local socket to the list */
2014-03-09 01:55:47 +04:00
if ( ! ( newfd = dm_zalloc ( sizeof ( struct local_client ) ) ) ) {
2010-11-08 22:37:40 +03:00
child_init_signal_and_exit ( DFAIL_MALLOC ) ;
/* NOTREACHED */
2010-10-26 13:01:47 +04:00
}
2004-06-24 12:02:38 +04:00
newfd - > fd = local_sock ;
newfd - > type = LOCAL_RENDEZVOUS ;
newfd - > callback = local_rendezvous_callback ;
newfd - > next = local_client_head . next ;
local_client_head . next = newfd ;
/* This needs to be started after cluster initialisation
as it may need to take out locks */
2015-07-24 01:10:16 +03:00
DEBUGLOG ( " Starting LVM thread \n " ) ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Main cluster socket fd %d with local socket %d (%p) \n " ,
& local_client_head , local_client_head . fd , newfd - > fd , newfd ) ;
2008-11-21 16:48:00 +03:00
/* Don't let anyone else to do work until we are started */
2015-11-09 11:23:27 +03:00
if ( pthread_create ( & lvm_thread , & stack_attr , lvm_thread_fn , & lvm_params ) ) {
log_sys_error ( " pthread_create " , " " ) ;
goto out ;
}
2004-06-24 12:02:38 +04:00
2011-10-11 13:26:04 +04:00
/* Don't start until the LVM thread is ready */
pthread_barrier_wait ( & lvm_start_barrier ) ;
2004-06-24 12:02:38 +04:00
/* Tell the rest of the cluster our version number */
2005-01-13 16:24:02 +03:00
if ( clops - > cluster_init_completed )
clops - > cluster_init_completed ( ) ;
2004-06-24 12:02:38 +04:00
DEBUGLOG ( " clvmd ready for work \n " ) ;
2004-10-06 14:12:34 +04:00
child_init_signal ( SUCCESS ) ;
2004-06-24 12:02:38 +04:00
2005-01-05 17:41:54 +03:00
/* Try to shutdown neatly */
signal ( SIGTERM , sigterm_handler ) ;
signal ( SIGINT , sigterm_handler ) ;
2004-06-24 12:02:38 +04:00
/* Do some work */
2014-03-21 14:34:25 +04:00
main_loop ( cmd_timeout ) ;
2004-06-24 12:02:38 +04:00
2011-03-30 16:36:19 +04:00
pthread_mutex_lock ( & lvm_thread_mutex ) ;
2014-03-21 21:01:38 +04:00
lvm_thread_exit = 1 ;
2011-03-30 16:36:19 +04:00
pthread_cond_signal ( & lvm_thread_cond ) ;
pthread_mutex_unlock ( & lvm_thread_mutex ) ;
if ( ( errno = pthread_join ( lvm_thread , NULL ) ) )
log_sys_error ( " pthread_join " , " " ) ;
2010-07-28 17:55:42 +04:00
close_local_sock ( local_sock ) ;
2009-06-15 16:15:23 +04:00
2014-03-21 20:07:57 +04:00
while ( ( delfd = local_client_head . next ) ) {
local_client_head . next = delfd - > next ;
/* Failing cleanup_zombie leaks... */
if ( delfd - > type = = LOCAL_SOCK & & ! cleanup_zombie ( delfd ) )
cmd_client_cleanup ( delfd ) ; /* calls sync_unlock */
if ( delfd - > fd ! = local_sock )
safe_close ( & ( delfd - > fd ) ) ;
2014-03-09 01:55:47 +04:00
dm_free ( delfd ) ;
2011-03-30 16:36:19 +04:00
}
2014-03-20 17:06:16 +04:00
DEBUGLOG ( " cluster_closedown \n " ) ;
destroy_lvhash ( ) ;
2014-03-22 00:19:54 +04:00
clops - > cluster_closedown ( ) ;
2012-02-27 15:26:25 +04:00
ret = 0 ;
out :
2011-09-25 00:48:34 +04:00
dm_hash_destroy ( lvm_params . excl_uuid ) ;
2012-02-27 15:26:25 +04:00
return ret ;
2004-06-24 12:02:38 +04:00
}
2011-08-09 22:11:00 +04:00
/* Called when the cluster layer has completed initialisation.
2004-06-24 12:02:38 +04:00
We send the version message */
2011-03-30 00:30:05 +04:00
void clvmd_cluster_init_completed ( void )
2004-06-24 12:02:38 +04:00
{
send_version_message ( ) ;
}
/* Data on a connected socket */
static int local_sock_callback ( struct local_client * thisfd , char * buf , int len ,
2007-05-02 16:22:40 +04:00
const char * csid ,
struct local_client * * new_client )
2004-06-24 12:02:38 +04:00
{
* new_client = NULL ;
return read_from_local_sock ( thisfd ) ;
}
/* Data on a connected socket */
static int local_rendezvous_callback ( struct local_client * thisfd , char * buf ,
2007-05-02 16:22:40 +04:00
int len , const char * csid ,
2004-06-24 12:02:38 +04:00
struct local_client * * new_client )
{
/* Someone connected to our local socket, accept it. */
struct sockaddr_un socka ;
struct local_client * newfd ;
socklen_t sl = sizeof ( socka ) ;
int client_fd = accept ( thisfd - > fd , ( struct sockaddr * ) & socka , & sl ) ;
2007-03-29 17:59:33 +04:00
if ( client_fd = = - 1 & & errno = = EINTR )
return 1 ;
2004-06-24 12:02:38 +04:00
if ( client_fd > = 0 ) {
2014-03-20 12:24:45 +04:00
if ( ! ( newfd = dm_zalloc ( sizeof ( * newfd ) ) ) ) {
2012-02-28 14:14:06 +04:00
if ( close ( client_fd ) )
2014-03-21 15:56:01 +04:00
log_sys_error ( " close " , " socket " ) ;
2004-06-24 12:02:38 +04:00
return 1 ;
}
2010-04-20 18:07:37 +04:00
2014-04-09 10:06:16 +04:00
pthread_cond_init ( & newfd - > bits . localsock . cond , NULL ) ;
pthread_mutex_init ( & newfd - > bits . localsock . mutex , NULL ) ;
2010-04-20 18:07:37 +04:00
if ( fcntl ( client_fd , F_SETFD , 1 ) )
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Setting CLOEXEC on client fd %d failed: %s \n " , thisfd , client_fd , strerror ( errno ) ) ;
2010-04-20 18:07:37 +04:00
2004-06-24 12:02:38 +04:00
newfd - > fd = client_fd ;
newfd - > type = LOCAL_SOCK ;
newfd - > callback = local_sock_callback ;
newfd - > bits . localsock . all_success = 1 ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Got new connection on fd %d \n " , newfd , newfd - > fd ) ;
2004-06-24 12:02:38 +04:00
* new_client = newfd ;
}
return 1 ;
}
static int local_pipe_callback ( struct local_client * thisfd , char * buf ,
2007-05-02 16:22:40 +04:00
int maxlen , const char * csid ,
2004-06-24 12:02:38 +04:00
struct local_client * * new_client )
{
int len ;
char buffer [ PIPE_BUF ] ;
struct local_client * sock_client = thisfd - > bits . pipe . client ;
int status = - 1 ; /* in error by default */
len = read ( thisfd - > fd , buffer , sizeof ( int ) ) ;
2007-03-29 17:59:33 +04:00
if ( len = = - 1 & & errno = = EINTR )
return 1 ;
2004-06-24 12:02:38 +04:00
2014-03-10 15:15:26 +04:00
if ( len = = sizeof ( int ) )
2006-12-02 02:10:26 +03:00
memcpy ( & status , buffer , sizeof ( int ) ) ;
2004-06-24 12:02:38 +04:00
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Read on pipe %d, %d bytes, status %d \n " ,
thisfd , thisfd - > fd , len , status ) ;
2006-12-02 02:10:26 +03:00
2004-06-24 12:02:38 +04:00
/* EOF on pipe or an error, close it */
if ( len < = 0 ) {
2006-12-02 02:10:26 +03:00
void * ret = & status ;
2010-12-22 17:00:28 +03:00
if ( close ( thisfd - > fd ) )
log_sys_error ( " close " , " local_pipe " ) ;
2004-06-24 12:02:38 +04:00
/* Clear out the cross-link */
2014-03-10 15:16:09 +04:00
if ( thisfd - > bits . pipe . client )
thisfd - > bits . pipe . client - > bits . localsock . pipe_client = NULL ;
2004-06-24 12:02:38 +04:00
/* Reap child thread */
if ( thisfd - > bits . pipe . threadid ) {
2014-03-10 15:15:26 +04:00
if ( ( errno = pthread_join ( thisfd - > bits . pipe . threadid , & ret ) ) )
2010-12-22 15:10:56 +03:00
log_sys_error ( " pthread_join " , " " ) ;
2004-06-24 12:02:38 +04:00
thisfd - > bits . pipe . threadid = 0 ;
2014-03-10 15:15:26 +04:00
if ( thisfd - > bits . pipe . client )
thisfd - > bits . pipe . client - > bits . localsock . threadid = 0 ;
2004-06-24 12:02:38 +04:00
}
return - 1 ;
} else {
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Background routine status was %d, sock_client %p \n " ,
thisfd , status , sock_client ) ;
2004-06-24 12:02:38 +04:00
/* But has the client gone away ?? */
2014-03-10 15:15:26 +04:00
if ( ! sock_client ) {
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Got pipe response for dead client, ignoring it \n " , thisfd ) ;
2004-06-24 12:02:38 +04:00
} else {
/* If error then just return that code */
if ( status )
send_local_reply ( sock_client , status ,
sock_client - > fd ) ;
else {
2011-10-11 13:56:44 +04:00
/* FIXME: closer inspect this code since state is write thread protected */
pthread_mutex_lock ( & sock_client - > bits . localsock . mutex ) ;
2012-02-08 17:03:40 +04:00
if ( sock_client - > bits . localsock . state = = POST_COMMAND ) {
2011-10-11 13:56:44 +04:00
pthread_mutex_unlock ( & sock_client - > bits . localsock . mutex ) ;
2004-06-24 12:02:38 +04:00
send_local_reply ( sock_client , 0 ,
sock_client - > fd ) ;
2012-02-08 17:03:40 +04:00
} else {
/* PRE_COMMAND finished. */
2011-10-11 13:56:44 +04:00
pthread_mutex_unlock ( & sock_client - > bits . localsock . mutex ) ;
2012-02-08 17:03:40 +04:00
if ( ( status = distribute_command ( sock_client ) ) )
send_local_reply ( sock_client , EFBIG ,
sock_client - > fd ) ;
2004-06-24 12:02:38 +04:00
}
}
}
}
return len ;
}
/* If a noed is up, look for it in the reply array, if it's not there then
add one with " ETIMEDOUT " .
NOTE : This won ' t race with real replies because they happen in the same thread .
*/
2007-05-02 16:22:40 +04:00
static void timedout_callback ( struct local_client * client , const char * csid ,
2004-06-24 12:02:38 +04:00
int node_up )
{
2014-04-11 00:10:37 +04:00
struct node_reply * reply ;
char nodename [ max_cluster_member_name_len ] ;
2004-06-24 12:02:38 +04:00
2014-04-11 00:10:37 +04:00
if ( ! node_up )
return ;
clops - > name_from_csid ( csid , nodename ) ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Checking for a reply from %s \n " , client , nodename ) ;
2014-04-11 00:10:37 +04:00
pthread_mutex_lock ( & client - > bits . localsock . mutex ) ;
2004-06-24 12:02:38 +04:00
2014-04-11 00:10:37 +04:00
reply = client - > bits . localsock . replies ;
while ( reply & & strcmp ( reply - > node , nodename ) ! = 0 )
reply = reply - > next ;
2004-06-24 12:02:38 +04:00
2014-04-11 00:10:37 +04:00
pthread_mutex_unlock ( & client - > bits . localsock . mutex ) ;
2004-06-24 12:02:38 +04:00
2014-04-11 00:10:37 +04:00
if ( ! reply ) {
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Node %s timed-out \n " , client , nodename ) ;
2014-04-11 00:10:37 +04:00
add_reply_to_list ( client , ETIMEDOUT , csid ,
" Command timed out " , 18 ) ;
2004-06-24 12:02:38 +04:00
}
}
/* Called when the request has timed out on at least one node. We fill in
the remaining node entries with ETIMEDOUT and return .
By the time we get here the node that caused
the timeout could have gone down , in which case we will never get the expected
number of replies that triggers the post command so we need to do it here
*/
static void request_timed_out ( struct local_client * client )
{
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Request timed-out. padding \n " , client ) ;
2005-01-13 16:24:02 +03:00
clops - > cluster_do_node_callback ( client , timedout_callback ) ;
2004-06-24 12:02:38 +04:00
2014-04-14 13:39:24 +04:00
if ( ! client - > bits . localsock . threadid )
return ;
pthread_mutex_lock ( & client - > bits . localsock . mutex ) ;
if ( ! client - > bits . localsock . finished & &
( client - > bits . localsock . num_replies ! =
client - > bits . localsock . expected_replies ) ) {
2004-06-24 12:02:38 +04:00
/* Post-process the command */
2014-04-14 13:39:24 +04:00
client - > bits . localsock . state = POST_COMMAND ;
pthread_cond_signal ( & client - > bits . localsock . cond ) ;
2004-06-24 12:02:38 +04:00
}
2014-04-14 13:39:24 +04:00
pthread_mutex_unlock ( & client - > bits . localsock . mutex ) ;
2004-06-24 12:02:38 +04:00
}
/* This is where the real work happens */
2014-03-21 14:34:25 +04:00
static void main_loop ( int cmd_timeout )
2004-06-24 12:02:38 +04:00
{
2011-03-30 00:30:05 +04:00
sigset_t ss ;
2004-06-24 12:02:38 +04:00
DEBUGLOG ( " Using timeout of %d seconds \n " , cmd_timeout ) ;
2009-03-24 14:49:15 +03:00
sigemptyset ( & ss ) ;
sigaddset ( & ss , SIGINT ) ;
sigaddset ( & ss , SIGTERM ) ;
pthread_sigmask ( SIG_UNBLOCK , & ss , NULL ) ;
2004-06-24 12:02:38 +04:00
/* Main loop */
while ( ! quit ) {
fd_set in ;
int select_status ;
struct local_client * thisfd ;
struct timeval tv = { cmd_timeout , 0 } ;
2005-01-13 16:24:02 +03:00
int quorate = clops - > is_quorate ( ) ;
2015-07-24 01:10:16 +03:00
int client_count = 0 ;
int max_fd = 0 ;
2015-08-18 16:47:07 +03:00
struct local_client * lastfd = & local_client_head ;
struct local_client * nextfd = local_client_head . next ;
2004-06-24 12:02:38 +04:00
/* Wait on the cluster FD and all local sockets/pipes */
2006-03-14 17:18:34 +03:00
local_client_head . fd = clops - > get_main_cluster_fd ( ) ;
2004-06-24 12:02:38 +04:00
FD_ZERO ( & in ) ;
2015-07-24 01:10:16 +03:00
2014-03-21 15:56:01 +04:00
for ( thisfd = & local_client_head ; thisfd ; thisfd = thisfd - > next ) {
2015-07-24 01:10:16 +03:00
client_count + + ;
max_fd = max ( max_fd , thisfd - > fd ) ;
}
if ( max_fd > FD_SETSIZE - 32 ) {
fprintf ( stderr , " WARNING: There are too many connections to clvmd. Investigate and take action now! \n " ) ;
fprintf ( stderr , " WARNING: Your cluster may freeze up if the number of clvmd file descriptors (%d) exceeds %d. \n " , max_fd + 1 , FD_SETSIZE ) ;
}
for ( thisfd = & local_client_head ; thisfd ; thisfd = nextfd , nextfd = thisfd ? thisfd - > next : NULL ) {
if ( thisfd - > removeme & & ! cleanup_zombie ( thisfd ) ) {
struct local_client * free_fd = thisfd ;
lastfd - > next = nextfd ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) removeme set with %d monitored fds remaining \n " , thisfd , client_count - 1 ) ;
2015-07-24 01:10:16 +03:00
/* Queue cleanup, this also frees the client struct */
add_to_lvmqueue ( free_fd , NULL , 0 , NULL ) ;
continue ;
}
lastfd = thisfd ;
2005-06-14 14:35:02 +04:00
if ( thisfd - > removeme )
continue ;
2004-06-24 12:02:38 +04:00
/* if the cluster is not quorate then don't listen for new requests */
if ( ( thisfd - > type ! = LOCAL_RENDEZVOUS & &
thisfd - > type ! = LOCAL_SOCK ) | | quorate )
2015-07-24 01:10:16 +03:00
if ( thisfd - > fd < FD_SETSIZE )
FD_SET ( thisfd - > fd , & in ) ;
2004-06-24 12:02:38 +04:00
}
2005-04-19 14:36:42 +04:00
select_status = select ( FD_SETSIZE , & in , NULL , NULL , & tv ) ;
if ( reread_config ) {
int saved_errno = errno ;
reread_config = 0 ;
2013-04-18 01:16:30 +04:00
DEBUGLOG ( " got SIGHUP \n " ) ;
2005-04-19 14:36:42 +04:00
if ( clops - > reread_config )
clops - > reread_config ( ) ;
errno = saved_errno ;
}
if ( select_status > 0 ) {
2004-06-24 12:02:38 +04:00
char csid [ MAX_CSID_LEN ] ;
2005-01-13 16:24:02 +03:00
char buf [ max_cluster_message ] ;
2004-06-24 12:02:38 +04:00
2014-03-21 15:56:01 +04:00
for ( thisfd = & local_client_head ; thisfd ; thisfd = thisfd - > next ) {
2015-07-24 01:10:16 +03:00
if ( thisfd - > fd < FD_SETSIZE & & FD_ISSET ( thisfd - > fd , & in ) ) {
2008-03-28 15:58:09 +03:00
struct local_client * newfd = NULL ;
2004-06-24 12:02:38 +04:00
int ret ;
2015-07-24 01:10:16 +03:00
/* FIXME Remove from main thread in case it blocks! */
2004-06-24 12:02:38 +04:00
/* Do callback */
2014-03-21 15:56:01 +04:00
ret = thisfd - > callback ( thisfd , buf , sizeof ( buf ) ,
csid , & newfd ) ;
2004-06-24 12:02:38 +04:00
/* Ignore EAGAIN */
2015-02-18 14:45:43 +03:00
if ( ret < 0 & & ( errno = = EAGAIN | | errno = = EINTR ) ) {
2014-03-21 15:56:01 +04:00
continue ;
2015-02-18 14:45:43 +03:00
}
2004-06-24 12:02:38 +04:00
/* Got error or EOF: Remove it from the list safely */
if ( ret < = 0 ) {
int type = thisfd - > type ;
/* If the cluster socket shuts down, so do we */
if ( type = = CLUSTER_MAIN_SOCK | |
type = = CLUSTER_INTERNAL )
goto closedown ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) ret == %d, errno = %d. removing client \n " ,
thisfd , ret , errno ) ;
2013-09-09 02:01:44 +04:00
thisfd - > removeme = 1 ;
2015-07-24 01:10:16 +03:00
continue ;
2004-06-24 12:02:38 +04:00
}
/* New client...simply add it to the list */
if ( newfd ) {
newfd - > next = thisfd - > next ;
thisfd - > next = newfd ;
2015-07-24 01:10:16 +03:00
thisfd = newfd ;
2004-06-24 12:02:38 +04:00
}
}
}
}
/* Select timed out. Check for clients that have been waiting too long for a response */
if ( select_status = = 0 ) {
time_t the_time = time ( NULL ) ;
2014-03-21 15:56:01 +04:00
for ( thisfd = & local_client_head ; thisfd ; thisfd = thisfd - > next ) {
if ( thisfd - > type = = LOCAL_SOCK & &
thisfd - > bits . localsock . sent_out & &
( thisfd - > bits . localsock . sent_time + cmd_timeout ) < the_time & &
thisfd - > bits . localsock . expected_replies ! =
2004-06-24 12:02:38 +04:00
thisfd - > bits . localsock . num_replies ) {
/* Send timed out message + replies we already have */
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " Request to client %p timed-out (send: %ld, now: %ld) \n " ,
thisfd , thisfd - > bits . localsock . sent_time , the_time ) ;
2004-06-24 12:02:38 +04:00
thisfd - > bits . localsock . all_success = 0 ;
request_timed_out ( thisfd ) ;
}
}
}
if ( select_status < 0 ) {
if ( errno = = EINTR )
continue ;
# ifdef DEBUG
perror ( " select error " ) ;
exit ( - 1 ) ;
# endif
}
}
closedown :
2013-04-18 01:16:30 +04:00
if ( quit )
DEBUGLOG ( " SIGTERM received \n " ) ;
2004-06-24 12:02:38 +04:00
}
2006-12-02 02:10:26 +03:00
static __attribute__ ( ( noreturn ) ) void wait_for_child ( int c_pipe , int timeout )
2006-11-30 12:44:07 +03:00
{
int child_status ;
fd_set fds ;
struct timeval tv = { timeout , 0 } ;
FD_ZERO ( & fds ) ;
FD_SET ( c_pipe , & fds ) ;
2014-03-21 15:56:01 +04:00
switch ( select ( c_pipe + 1 , & fds , NULL , NULL , timeout ? & tv : NULL ) ) {
case 0 :
2006-11-30 12:44:07 +03:00
fprintf ( stderr , " clvmd startup timed out \n " ) ;
exit ( DFAIL_TIMEOUT ) ;
2014-03-21 15:56:01 +04:00
case 1 :
2006-11-30 12:44:07 +03:00
if ( read ( c_pipe , & child_status , sizeof ( child_status ) ) ! =
sizeof ( child_status ) ) {
fprintf ( stderr , " clvmd failed in initialisation \n " ) ;
exit ( DFAIL_INIT ) ;
}
2014-03-21 15:56:01 +04:00
switch ( child_status ) {
case SUCCESS :
break ;
case DFAIL_INIT :
fprintf ( stderr , " clvmd failed in initialisation \n " ) ;
break ;
case DFAIL_LOCAL_SOCK :
fprintf ( stderr , " clvmd could not create local socket \n " ) ;
fprintf ( stderr , " Another clvmd is probably already running \n " ) ;
break ;
case DFAIL_CLUSTER_IF :
fprintf ( stderr , " clvmd could not connect to cluster manager \n " ) ;
fprintf ( stderr , " Consult syslog for more information \n " ) ;
break ;
case DFAIL_MALLOC :
fprintf ( stderr , " clvmd failed, not enough memory \n " ) ;
break ;
default :
fprintf ( stderr , " clvmd failed, error was %d \n " , child_status ) ;
break ;
2006-11-30 12:44:07 +03:00
}
2014-03-21 15:56:01 +04:00
exit ( child_status ) ;
default :
fprintf ( stderr , " clvmd startup, select failed: %s \n " , strerror ( errno ) ) ;
exit ( DFAIL_INIT ) ;
2006-11-30 12:44:07 +03:00
}
}
2004-10-06 14:12:34 +04:00
/*
* Fork into the background and detach from our parent process .
* In the interests of user - friendliness we wait for the daemon
* to complete initialisation before returning its status
* the the user .
*/
2006-11-30 12:44:07 +03:00
static void be_daemon ( int timeout )
2004-06-24 12:02:38 +04:00
{
int devnull = open ( " /dev/null " , O_RDWR ) ;
if ( devnull = = - 1 ) {
perror ( " Can't open /dev/null " ) ;
exit ( 3 ) ;
}
2011-03-01 23:17:56 +03:00
if ( pipe ( child_pipe ) ) {
perror ( " Error creating pipe " ) ;
exit ( 3 ) ;
}
2004-10-06 14:12:34 +04:00
2010-11-29 15:44:52 +03:00
switch ( fork ( ) ) {
2004-06-24 12:02:38 +04:00
case - 1 :
perror ( " clvmd: can't fork " ) ;
exit ( 2 ) ;
2004-10-06 14:12:34 +04:00
case 0 : /* Child */
2012-03-02 01:12:37 +04:00
( void ) close ( child_pipe [ 0 ] ) ;
2004-06-24 12:02:38 +04:00
break ;
2004-10-06 14:12:34 +04:00
default : /* Parent */
2012-03-02 01:12:37 +04:00
( void ) close ( child_pipe [ 1 ] ) ;
2006-11-30 12:44:07 +03:00
wait_for_child ( child_pipe [ 0 ] , timeout ) ;
2004-06-24 12:02:38 +04:00
}
/* Detach ourself from the calling environment */
if ( close ( 0 ) | | close ( 1 ) | | close ( 2 ) ) {
perror ( " Error closing terminal FDs " ) ;
exit ( 4 ) ;
}
setsid ( ) ;
if ( dup2 ( devnull , 0 ) < 0 | | dup2 ( devnull , 1 ) < 0
| | dup2 ( devnull , 2 ) < 0 ) {
perror ( " Error setting terminal FDs to /dev/null " ) ;
log_error ( " Error setting terminal FDs to /dev/null: %m " ) ;
exit ( 5 ) ;
}
2013-08-06 18:07:50 +04:00
if ( ( devnull > STDERR_FILENO ) & & close ( devnull ) ) {
log_sys_error ( " close " , " /dev/null " ) ;
exit ( 7 ) ;
}
2004-06-24 12:02:38 +04:00
if ( chdir ( " / " ) ) {
log_error ( " Error setting current directory to /: %m " ) ;
exit ( 6 ) ;
}
2013-07-30 23:12:33 +04:00
}
static int verify_message ( char * buf , int len )
{
struct clvm_header * h = ( struct clvm_header * ) buf ;
2013-11-25 16:42:30 +04:00
if ( len < ( int ) sizeof ( struct clvm_header ) ) {
2014-03-20 12:21:34 +04:00
log_error ( " verify_message short len %d. " , len ) ;
2013-07-30 23:12:33 +04:00
return - 1 ;
}
switch ( h - > cmd ) {
case CLVMD_CMD_REPLY :
case CLVMD_CMD_VERSION :
case CLVMD_CMD_GOAWAY :
case CLVMD_CMD_TEST :
case CLVMD_CMD_LOCK :
case CLVMD_CMD_UNLOCK :
case CLVMD_CMD_LOCK_LV :
case CLVMD_CMD_LOCK_VG :
case CLVMD_CMD_LOCK_QUERY :
case CLVMD_CMD_REFRESH :
case CLVMD_CMD_GET_CLUSTERNAME :
case CLVMD_CMD_SET_DEBUG :
case CLVMD_CMD_VG_BACKUP :
case CLVMD_CMD_RESTART :
case CLVMD_CMD_SYNC_NAMES :
break ;
default :
2014-03-20 12:21:34 +04:00
log_error ( " verify_message bad cmd %x. " , h - > cmd ) ;
2013-07-30 23:12:33 +04:00
return - 1 ;
2014-03-10 15:15:26 +04:00
}
2013-07-30 23:12:33 +04:00
/* TODO: we may be able to narrow len/flags/clientid/arglen checks based on cmd */
2013-10-24 20:10:09 +04:00
if ( h - > flags & ~ ( CLVMD_FLAG_LOCAL | CLVMD_FLAG_SYSTEMLV | CLVMD_FLAG_NODEERRS | CLVMD_FLAG_REMOTE ) ) {
2014-03-20 12:21:34 +04:00
log_error ( " verify_message bad flags %x. " , h - > flags ) ;
2013-07-30 23:12:33 +04:00
return - 1 ;
}
2004-06-24 12:02:38 +04:00
2013-07-30 23:12:33 +04:00
if ( h - > arglen > max_cluster_message ) {
2014-03-20 12:21:34 +04:00
log_error ( " verify_message bad arglen %x max %d. " , h - > arglen , max_cluster_message ) ;
2013-07-30 23:12:33 +04:00
return - 1 ;
}
return 0 ;
}
static void dump_message ( char * buf , int len )
{
2014-03-20 13:22:41 +04:00
unsigned char row [ 8 ] ;
2013-07-30 23:12:33 +04:00
char str [ 9 ] ;
2014-03-20 13:22:41 +04:00
int i , j = 0 ;
2013-07-30 23:12:33 +04:00
2014-03-20 13:22:41 +04:00
str [ 8 ] = ' \0 ' ;
2013-07-30 23:12:33 +04:00
if ( len > 128 )
len = 128 ;
2014-03-20 13:22:41 +04:00
for ( i = 0 ; i < len ; + + i ) {
row [ j ] = buf [ i ] ;
str [ j ] = ( isprint ( buf [ i ] ) ) ? buf [ i ] : ' ' ;
2013-07-30 23:12:33 +04:00
2014-11-12 11:30:02 +03:00
if ( i + 1 = = len ) {
2014-03-20 13:22:41 +04:00
for ( ; j < 8 ; + + j ) {
row [ j ] = 0 ;
str [ j ] = ' ' ;
2013-07-30 23:12:33 +04:00
}
log_error ( " %02x %02x %02x %02x %02x %02x %02x %02x [%s] " ,
row [ 0 ] , row [ 1 ] , row [ 2 ] , row [ 3 ] , row [ 4 ] , row [ 5 ] , row [ 6 ] , row [ 7 ] , str ) ;
2014-03-20 13:22:41 +04:00
j = 0 ;
2013-07-30 23:12:33 +04:00
}
}
2004-06-24 12:02:38 +04:00
}
2013-09-09 02:01:44 +04:00
static int cleanup_zombie ( struct local_client * thisfd )
{
int * status ;
2014-03-20 17:24:06 +04:00
struct local_client * pipe_client ;
2013-09-09 02:01:44 +04:00
if ( thisfd - > type ! = LOCAL_SOCK )
return 0 ;
if ( ! thisfd - > bits . localsock . cleanup_needed )
return 0 ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) EOF on local socket %d: inprogress=%d \n " ,
thisfd , thisfd - > fd , thisfd - > bits . localsock . in_progress ) ;
2013-09-09 02:01:44 +04:00
2014-03-20 17:24:06 +04:00
if ( ( pipe_client = thisfd - > bits . localsock . pipe_client ) )
pipe_client = pipe_client - > bits . pipe . client ;
2013-09-09 02:01:44 +04:00
/* If the client went away in mid command then tidy up */
if ( thisfd - > bits . localsock . in_progress ) {
2014-03-20 17:24:06 +04:00
DEBUGLOG ( " Sending SIGUSR2 to pre&post thread (%p in-progress) \n " , pipe_client ) ;
2013-09-09 02:01:44 +04:00
pthread_kill ( thisfd - > bits . localsock . threadid , SIGUSR2 ) ;
if ( pthread_mutex_trylock ( & thisfd - > bits . localsock . mutex ) )
2014-03-20 12:18:10 +04:00
return 1 ;
2013-09-09 02:01:44 +04:00
thisfd - > bits . localsock . state = POST_COMMAND ;
2014-04-09 12:36:25 +04:00
thisfd - > bits . localsock . finished = 1 ;
2013-09-09 02:01:44 +04:00
pthread_cond_signal ( & thisfd - > bits . localsock . cond ) ;
pthread_mutex_unlock ( & thisfd - > bits . localsock . mutex ) ;
/* Free any unsent buffers */
free_reply ( thisfd ) ;
}
/* Kill the subthread & free resources */
if ( thisfd - > bits . localsock . threadid ) {
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Waiting for pre&post thread \n " , pipe_client ) ;
2013-09-09 02:01:44 +04:00
pthread_mutex_lock ( & thisfd - > bits . localsock . mutex ) ;
thisfd - > bits . localsock . state = PRE_COMMAND ;
2014-04-09 12:36:25 +04:00
thisfd - > bits . localsock . finished = 1 ;
2013-09-09 02:01:44 +04:00
pthread_cond_signal ( & thisfd - > bits . localsock . cond ) ;
pthread_mutex_unlock ( & thisfd - > bits . localsock . mutex ) ;
if ( ( errno = pthread_join ( thisfd - > bits . localsock . threadid ,
( void * * ) & status ) ) )
log_sys_error ( " pthread_join " , " " ) ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Joined pre&post thread \n " , pipe_client ) ;
2013-09-09 02:01:44 +04:00
thisfd - > bits . localsock . threadid = 0 ;
/* Remove the pipe client */
2014-03-21 15:56:01 +04:00
if ( thisfd - > bits . localsock . pipe_client ) {
2014-03-21 15:56:35 +04:00
struct local_client * delfd ;
struct local_client * lastfd ;
2013-09-09 02:01:44 +04:00
( void ) close ( thisfd - > bits . localsock . pipe_client - > fd ) ; /* Close pipe */
( void ) close ( thisfd - > bits . localsock . pipe ) ;
/* Remove pipe client */
2014-03-21 15:56:35 +04:00
for ( lastfd = & local_client_head ; ( delfd = lastfd - > next ) ; lastfd = delfd )
if ( thisfd - > bits . localsock . pipe_client = = delfd ) {
thisfd - > bits . localsock . pipe_client = NULL ;
lastfd - > next = delfd - > next ;
dm_free ( delfd ) ;
2013-09-09 02:01:44 +04:00
break ;
}
}
}
/* Free the command buffer */
2014-03-20 12:24:45 +04:00
dm_free ( thisfd - > bits . localsock . cmd ) ;
2013-09-09 02:01:44 +04:00
safe_close ( & ( thisfd - > fd ) ) ;
thisfd - > bits . localsock . cleanup_needed = 0 ;
2014-03-21 15:56:01 +04:00
2013-09-09 02:01:44 +04:00
return 0 ;
}
2004-06-24 12:02:38 +04:00
/* Called when we have a read from the local socket.
was in the main loop but it ' s grown up and is a big girl now */
static int read_from_local_sock ( struct local_client * thisfd )
{
int len ;
int argslen ;
int missing_len ;
2014-03-20 17:45:39 +04:00
char buffer [ PIPE_BUF + 1 ] ;
char csid [ MAX_CSID_LEN ] ;
int comms_pipe [ 2 ] ;
struct local_client * newfd ;
struct clvm_header * inheader = ( struct clvm_header * ) buffer ;
int status ;
2013-07-30 23:12:33 +04:00
2012-02-28 15:06:56 +04:00
len = read ( thisfd - > fd , buffer , sizeof ( buffer ) - 1 ) ;
2007-03-29 17:59:33 +04:00
if ( len = = - 1 & & errno = = EINTR )
return 1 ;
2004-06-24 12:02:38 +04:00
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Read on local socket %d, len = %d \n " , thisfd , thisfd - > fd , len ) ;
2004-06-24 12:02:38 +04:00
2014-03-20 17:45:39 +04:00
if ( len & & verify_message ( buffer , len ) < 0 ) {
log_error ( " read_from_local_sock from %d len %d bad verify. " ,
thisfd - > fd , len ) ;
dump_message ( buffer , len ) ;
/* force error handling below */
len = 0 ;
2013-07-30 23:12:33 +04:00
}
2004-06-24 12:02:38 +04:00
/* EOF or error on socket */
if ( len < = 0 ) {
2014-03-21 15:56:01 +04:00
thisfd - > bits . localsock . cleanup_needed = 1 ;
( void ) cleanup_zombie ( thisfd ) ; /* ignore errors here */
2004-06-24 12:02:38 +04:00
return 0 ;
2014-03-20 17:45:39 +04:00
}
2004-06-24 12:02:38 +04:00
2014-03-20 17:45:39 +04:00
buffer [ len ] = 0 ; /* Ensure \0 terminated */
2011-09-16 18:40:06 +04:00
2014-03-20 17:45:39 +04:00
/* Fill in the client ID */
inheader - > clientid = htonl ( thisfd - > fd ) ;
2004-06-24 12:02:38 +04:00
2014-03-20 17:45:39 +04:00
/* If we are already busy then return an error */
if ( thisfd - > bits . localsock . in_progress ) {
struct clvm_header reply = {
. cmd = CLVMD_CMD_REPLY ,
. status = EBUSY
} ;
send_message ( & reply , sizeof ( reply ) , our_csid , thisfd - > fd ,
" Error sending EBUSY reply to local user " ) ;
return len ;
}
2008-05-09 13:59:39 +04:00
2014-03-20 17:45:39 +04:00
/* See if we have the whole message */
argslen = len - strlen ( inheader - > node ) - sizeof ( struct clvm_header ) ;
missing_len = inheader - > arglen - argslen ;
if ( missing_len < 0 )
missing_len = 0 ;
/* We need at least sizeof(struct clvm_header) bytes in buffer */
if ( len < ( int ) sizeof ( struct clvm_header ) | | /* Already handled in verify_message() */
argslen < 0 | | missing_len > MAX_MISSING_LEN ) {
struct clvm_header reply = {
. cmd = CLVMD_CMD_REPLY ,
. status = EINVAL
} ;
send_message ( & reply , sizeof ( reply ) , our_csid , thisfd - > fd ,
" Error sending EINVAL reply to local user " ) ;
return 0 ;
}
2004-06-24 12:02:38 +04:00
2014-03-20 17:45:39 +04:00
/* Free any old buffer space */
dm_free ( thisfd - > bits . localsock . cmd ) ;
2004-06-24 12:02:38 +04:00
2014-03-20 17:45:39 +04:00
/* Save the message */
if ( ! ( thisfd - > bits . localsock . cmd = dm_malloc ( len + missing_len ) ) ) {
struct clvm_header reply = {
. cmd = CLVMD_CMD_REPLY ,
. status = ENOMEM
} ;
send_message ( & reply , sizeof ( reply ) , our_csid , thisfd - > fd ,
" Error sending ENOMEM reply to local user " ) ;
return 0 ;
}
memcpy ( thisfd - > bits . localsock . cmd , buffer , len ) ;
thisfd - > bits . localsock . cmd_len = len + missing_len ;
inheader = ( struct clvm_header * ) thisfd - > bits . localsock . cmd ;
/* If we don't have the full message then read the rest now */
if ( missing_len ) {
char * argptr = inheader - > node + strlen ( inheader - > node ) + 1 ;
while ( missing_len > 0 ) {
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) got %d bytes, need another %d (total %d) \n " ,
thisfd , argslen , missing_len , inheader - > arglen ) ;
2014-03-20 17:45:39 +04:00
len = read ( thisfd - > fd , argptr + argslen , missing_len ) ;
if ( len = = - 1 & & errno = = EINTR )
continue ;
2004-06-24 12:02:38 +04:00
2014-03-20 17:45:39 +04:00
if ( len < = 0 ) {
/* EOF or error on socket */
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) EOF on local socket \n " , thisfd ) ;
2014-03-20 17:45:39 +04:00
dm_free ( thisfd - > bits . localsock . cmd ) ;
thisfd - > bits . localsock . cmd = NULL ;
return 0 ;
}
2011-08-11 16:57:10 +04:00
2014-03-20 17:45:39 +04:00
missing_len - = len ;
argslen + = len ;
}
}
2012-02-28 13:53:55 +04:00
2014-03-20 17:45:39 +04:00
/* Only run the command if all the cluster nodes are running CLVMD */
if ( ( ( inheader - > flags & CLVMD_FLAG_LOCAL ) = = 0 ) & &
( check_all_clvmds_running ( thisfd ) = = - 1 ) ) {
thisfd - > bits . localsock . expected_replies = 0 ;
thisfd - > bits . localsock . num_replies = 0 ;
send_local_reply ( thisfd , EHOSTDOWN , thisfd - > fd ) ;
return len ;
}
2010-04-20 18:07:37 +04:00
2014-03-20 17:45:39 +04:00
/* Check the node name for validity */
if ( inheader - > node [ 0 ] & & clops - > csid_from_name ( csid , inheader - > node ) ) {
/* Error, node is not in the cluster */
struct clvm_header reply = {
. cmd = CLVMD_CMD_REPLY ,
. status = ENOENT
} ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Unknown node: '%s' \n " , thisfd , inheader - > node ) ;
2014-03-20 17:45:39 +04:00
send_message ( & reply , sizeof ( reply ) , our_csid , thisfd - > fd ,
" Error sending ENOENT reply to local user " ) ;
thisfd - > bits . localsock . expected_replies = 0 ;
thisfd - > bits . localsock . num_replies = 0 ;
thisfd - > bits . localsock . in_progress = FALSE ;
thisfd - > bits . localsock . sent_out = FALSE ;
return len ;
}
2010-04-20 18:07:37 +04:00
2014-03-20 17:45:39 +04:00
/* If we already have a subthread then just signal it to start */
if ( thisfd - > bits . localsock . threadid ) {
pthread_mutex_lock ( & thisfd - > bits . localsock . mutex ) ;
thisfd - > bits . localsock . state = PRE_COMMAND ;
pthread_cond_signal ( & thisfd - > bits . localsock . cond ) ;
pthread_mutex_unlock ( & thisfd - > bits . localsock . mutex ) ;
return len ;
}
2004-06-24 12:02:38 +04:00
2014-03-20 17:45:39 +04:00
/* Create a pipe and add the reading end to our FD list */
if ( pipe ( comms_pipe ) ) {
struct clvm_header reply = {
. cmd = CLVMD_CMD_REPLY ,
. status = EBUSY
} ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Creating pipe failed: %s \n " , thisfd , strerror ( errno ) ) ;
2014-03-20 17:45:39 +04:00
send_message ( & reply , sizeof ( reply ) , our_csid , thisfd - > fd ,
" Error sending EBUSY reply to local user " ) ;
return len ;
}
2004-06-24 12:02:38 +04:00
2014-03-20 17:45:39 +04:00
if ( ! ( newfd = dm_zalloc ( sizeof ( * newfd ) ) ) ) {
struct clvm_header reply = {
. cmd = CLVMD_CMD_REPLY ,
. status = ENOMEM
} ;
2004-06-24 12:02:38 +04:00
2014-03-20 17:45:39 +04:00
( void ) close ( comms_pipe [ 0 ] ) ;
( void ) close ( comms_pipe [ 1 ] ) ;
2004-06-24 12:02:38 +04:00
2014-03-20 17:45:39 +04:00
send_message ( & reply , sizeof ( reply ) , our_csid , thisfd - > fd ,
" Error sending ENOMEM reply to local user " ) ;
return len ;
2004-06-24 12:02:38 +04:00
}
2014-03-10 15:15:26 +04:00
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Creating pipe, [%d, %d] \n " , thisfd , comms_pipe [ 0 ] , comms_pipe [ 1 ] ) ;
2014-03-20 17:45:39 +04:00
if ( fcntl ( comms_pipe [ 0 ] , F_SETFD , 1 ) )
DEBUGLOG ( " setting CLOEXEC on pipe[0] failed: %s \n " , strerror ( errno ) ) ;
if ( fcntl ( comms_pipe [ 1 ] , F_SETFD , 1 ) )
DEBUGLOG ( " setting CLOEXEC on pipe[1] failed: %s \n " , strerror ( errno ) ) ;
newfd - > fd = comms_pipe [ 0 ] ;
newfd - > type = THREAD_PIPE ;
newfd - > callback = local_pipe_callback ;
newfd - > bits . pipe . client = thisfd ;
newfd - > next = thisfd - > next ;
thisfd - > next = newfd ;
/* Store a cross link to the pipe */
thisfd - > bits . localsock . pipe_client = newfd ;
thisfd - > bits . localsock . pipe = comms_pipe [ 1 ] ;
/* Make sure the thread has a copy of it's own ID */
newfd - > bits . pipe . threadid = thisfd - > bits . localsock . threadid ;
/* Run the pre routine */
thisfd - > bits . localsock . in_progress = TRUE ;
thisfd - > bits . localsock . state = PRE_COMMAND ;
thisfd - > bits . localsock . cleanup_needed = 1 ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Creating pre&post thread for pipe fd %d \n " , newfd , newfd - > fd ) ;
2014-03-20 17:45:39 +04:00
status = pthread_create ( & thisfd - > bits . localsock . threadid ,
& stack_attr , pre_and_post_thread , thisfd ) ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Created pre&post thread, state = %d \n " , newfd , status ) ;
2014-03-20 17:45:39 +04:00
2004-06-24 12:02:38 +04:00
return len ;
}
/* Add a file descriptor from the cluster or comms interface to
our list of FDs for select
*/
int add_client ( struct local_client * new_client )
{
new_client - > next = local_client_head . next ;
local_client_head . next = new_client ;
return 0 ;
}
/* Called when the pre-command has completed successfully - we
now execute the real command on all the requested nodes */
static int distribute_command ( struct local_client * thisfd )
{
struct clvm_header * inheader =
2014-03-21 15:56:01 +04:00
( struct clvm_header * ) thisfd - > bits . localsock . cmd ;
2004-06-24 12:02:38 +04:00
int len = thisfd - > bits . localsock . cmd_len ;
thisfd - > xid = global_xid + + ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) distribute command: XID = %d, flags=0x%x (%s%s) \n " ,
thisfd , thisfd - > xid , inheader - > flags ,
2012-01-21 09:31:54 +04:00
( inheader - > flags & CLVMD_FLAG_LOCAL ) ? " LOCAL " : " " ,
( inheader - > flags & CLVMD_FLAG_REMOTE ) ? " REMOTE " : " " ) ;
2004-06-24 12:02:38 +04:00
/* Forward it to other nodes in the cluster if needed */
if ( ! ( inheader - > flags & CLVMD_FLAG_LOCAL ) ) {
/* if node is empty then do it on the whole cluster */
if ( inheader - > node [ 0 ] = = ' \0 ' ) {
thisfd - > bits . localsock . expected_replies =
2014-03-21 15:56:01 +04:00
clops - > get_num_nodes ( ) ;
2004-06-24 12:02:38 +04:00
thisfd - > bits . localsock . num_replies = 0 ;
thisfd - > bits . localsock . sent_time = time ( NULL ) ;
thisfd - > bits . localsock . in_progress = TRUE ;
thisfd - > bits . localsock . sent_out = TRUE ;
2012-01-21 09:31:54 +04:00
/*
* Send to local node first , even if CLVMD_FLAG_REMOTE
* is set so we still get a reply if this is the
* only node .
*/
2004-06-24 12:02:38 +04:00
add_to_lvmqueue ( thisfd , inheader , len , NULL ) ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Sending message to all cluster nodes \n " , thisfd ) ;
2007-06-14 14:16:35 +04:00
inheader - > xid = thisfd - > xid ;
send_message ( inheader , len , NULL , - 1 ,
" Error forwarding message to cluster " ) ;
2004-06-24 12:02:38 +04:00
} else {
2014-03-21 15:56:01 +04:00
/* Do it on a single node */
2004-06-24 12:02:38 +04:00
char csid [ MAX_CSID_LEN ] ;
2014-03-21 15:56:01 +04:00
if ( clops - > csid_from_name ( csid , inheader - > node ) )
2004-06-24 12:02:38 +04:00
/* This has already been checked so should not happen */
return 0 ;
2014-03-21 15:56:01 +04:00
/* OK, found a node... */
thisfd - > bits . localsock . in_progress = TRUE ;
thisfd - > bits . localsock . expected_replies = 1 ;
thisfd - > bits . localsock . num_replies = 0 ;
/* Are we the requested node ?? */
if ( memcmp ( csid , our_csid , max_csid_len ) = = 0 ) {
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Doing command on local node only \n " , thisfd ) ;
2014-03-21 15:56:01 +04:00
add_to_lvmqueue ( thisfd , inheader , len , NULL ) ;
2004-06-24 12:02:38 +04:00
} else {
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Sending message to single node: %s \n " ,
thisfd , inheader - > node ) ;
2014-03-21 15:56:01 +04:00
inheader - > xid = thisfd - > xid ;
send_message ( inheader , len , csid , - 1 ,
" Error forwarding message to cluster node " ) ;
2004-06-24 12:02:38 +04:00
}
}
} else {
/* Local explicitly requested, ignore nodes */
thisfd - > bits . localsock . in_progress = TRUE ;
thisfd - > bits . localsock . expected_replies = 1 ;
thisfd - > bits . localsock . num_replies = 0 ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Doing command explicitly on local node only \n " , thisfd ) ;
2004-06-24 12:02:38 +04:00
add_to_lvmqueue ( thisfd , inheader , len , NULL ) ;
}
2014-03-10 15:15:26 +04:00
2004-06-24 12:02:38 +04:00
return 0 ;
}
/* Process a command from a remote node and return the result */
2006-12-02 02:10:26 +03:00
static void process_remote_command ( struct clvm_header * msg , int msglen , int fd ,
2007-05-02 16:22:40 +04:00
const char * csid )
2004-06-24 12:02:38 +04:00
{
char * replyargs ;
2005-01-13 16:24:02 +03:00
char nodename [ max_cluster_member_name_len ] ;
2004-06-24 12:02:38 +04:00
int replylen = 0 ;
2005-01-13 16:24:02 +03:00
int buflen = max_cluster_message - sizeof ( struct clvm_header ) - 1 ;
2004-06-24 12:02:38 +04:00
int status ;
/* Get the node name as we /may/ need it later */
2005-01-13 16:24:02 +03:00
clops - > name_from_csid ( csid , nodename ) ;
2004-06-24 12:02:38 +04:00
2008-06-05 18:24:28 +04:00
DEBUGLOG ( " process_remote_command %s for clientid 0x%x XID %d on node %s \n " ,
decode_cmd ( msg - > cmd ) , msg - > clientid , msg - > xid , nodename ) ;
2004-06-24 12:02:38 +04:00
/* Check for GOAWAY and sulk */
if ( msg - > cmd = = CLVMD_CMD_GOAWAY ) {
DEBUGLOG ( " Told to go away by %s \n " , nodename ) ;
2014-03-20 12:21:34 +04:00
log_error ( " Told to go away by %s. " , nodename ) ;
2004-06-24 12:02:38 +04:00
exit ( 99 ) ;
}
2014-03-21 15:56:01 +04:00
/* Version check is internal - don't bother exposing it in clvmd-command.c */
2004-06-24 12:02:38 +04:00
if ( msg - > cmd = = CLVMD_CMD_VERSION ) {
2007-03-29 17:59:33 +04:00
int version_nums [ 3 ] ;
2004-06-24 12:02:38 +04:00
char node [ 256 ] ;
2006-12-02 02:10:26 +03:00
memcpy ( version_nums , msg - > args , sizeof ( version_nums ) ) ;
2005-01-13 16:24:02 +03:00
clops - > name_from_csid ( csid , node ) ;
2004-06-24 12:02:38 +04:00
DEBUGLOG ( " Remote node %s is version %d.%d.%d \n " ,
2014-03-21 15:56:01 +04:00
node , ntohl ( version_nums [ 0 ] ) ,
2004-06-24 12:02:38 +04:00
ntohl ( version_nums [ 1 ] ) , ntohl ( version_nums [ 2 ] ) ) ;
if ( ntohl ( version_nums [ 0 ] ) ! = CLVMD_MAJOR_VERSION ) {
2014-03-21 15:56:56 +04:00
struct clvm_header byebyemsg = {
. cmd = CLVMD_CMD_GOAWAY
} ;
DEBUGLOG ( " Telling node %s to go away because of incompatible version number \n " ,
node ) ;
log_notice ( " Telling node %s to go away because of incompatible version number %d.%d.%d \n " ,
node , ntohl ( version_nums [ 0 ] ) ,
ntohl ( version_nums [ 1 ] ) , ntohl ( version_nums [ 2 ] ) ) ;
clops - > cluster_send_message ( & byebyemsg , sizeof ( byebyemsg ) , our_csid ,
" Error Sending GOAWAY message " ) ;
} else
2005-01-13 16:24:02 +03:00
clops - > add_up_node ( csid ) ;
2014-03-21 15:56:56 +04:00
2004-06-24 12:02:38 +04:00
return ;
}
/* Allocate a default reply buffer */
2014-03-20 12:24:45 +04:00
if ( ( replyargs = dm_malloc ( max_cluster_message - sizeof ( struct clvm_header ) ) ) )
2004-06-24 12:02:38 +04:00
/* Run the command */
2011-10-11 13:23:48 +04:00
/* FIXME: usage of init_test() is unprotected */
status = do_command ( NULL , msg , msglen , & replyargs ,
buflen , & replylen ) ;
2014-03-21 15:56:01 +04:00
else
2005-01-21 14:35:24 +03:00
status = ENOMEM ;
2004-06-24 12:02:38 +04:00
/* If it wasn't a reply, then reply */
if ( msg - > cmd ! = CLVMD_CMD_REPLY ) {
char * aggreply ;
2014-03-20 12:24:45 +04:00
aggreply = dm_realloc ( replyargs , replylen + sizeof ( struct clvm_header ) ) ;
2004-06-24 12:02:38 +04:00
if ( aggreply ) {
struct clvm_header * agghead =
( struct clvm_header * ) aggreply ;
replyargs = aggreply ;
/* Move it up so there's room for a header in front of the data */
memmove ( aggreply + offsetof ( struct clvm_header , args ) ,
replyargs , replylen ) ;
agghead - > xid = msg - > xid ;
2007-06-14 14:16:35 +04:00
agghead - > cmd = CLVMD_CMD_REPLY ;
agghead - > status = status ;
agghead - > flags = 0 ;
agghead - > clientid = msg - > clientid ;
agghead - > arglen = replylen ;
agghead - > node [ 0 ] = ' \0 ' ;
2014-03-21 15:56:56 +04:00
send_message ( aggreply , sizeof ( struct clvm_header ) + replylen ,
csid , fd , " Error sending command reply " ) ;
2004-06-24 12:02:38 +04:00
} else {
/* Return a failure response */
2014-03-21 15:56:56 +04:00
struct clvm_header reply = {
. cmd = CLVMD_CMD_REPLY ,
. status = ENOMEM ,
. clientid = msg - > clientid
} ;
DEBUGLOG ( " Error attempting to realloc return buffer \n " ) ;
send_message ( & reply , sizeof ( reply ) , csid , fd ,
" Error sending ENOMEM command reply " ) ;
2004-06-24 12:02:38 +04:00
}
}
2014-03-20 12:24:45 +04:00
dm_free ( replyargs ) ;
2004-06-24 12:02:38 +04:00
}
/* Add a reply to a command to the list of replies for this client.
If we have got a full set then send them to the waiting client down the local
socket */
static void add_reply_to_list ( struct local_client * client , int status ,
2007-05-02 16:22:40 +04:00
const char * csid , const char * buf , int len )
2004-06-24 12:02:38 +04:00
{
struct node_reply * reply ;
/* Add it to the list of replies */
2014-03-20 17:29:42 +04:00
if ( ! ( reply = dm_zalloc ( sizeof ( * reply ) ) ) ) {
2004-06-24 12:02:38 +04:00
/* It's all gone horribly wrong... */
send_local_reply ( client , ENOMEM , client - > fd ) ;
return ;
}
2014-03-20 17:29:42 +04:00
reply - > status = status ;
clops - > name_from_csid ( csid , reply - > node ) ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Reply from node %s: %d bytes \n " , client , reply - > node , len ) ;
2014-03-20 17:29:42 +04:00
if ( len > 0 ) {
if ( ! ( reply - > replymsg = dm_malloc ( len ) ) )
reply - > status = ENOMEM ;
else
memcpy ( reply - > replymsg , buf , len ) ;
} else
reply - > replymsg = NULL ;
2014-04-09 10:12:13 +04:00
pthread_mutex_lock ( & client - > bits . localsock . mutex ) ;
2014-04-14 14:40:23 +04:00
if ( client - > bits . localsock . finished ) {
dm_free ( reply - > replymsg ) ;
dm_free ( reply ) ;
} else {
/* Hook it onto the reply chain */
reply - > next = client - > bits . localsock . replies ;
client - > bits . localsock . replies = reply ;
/* If we have the whole lot then do the post-process */
2004-06-24 12:02:38 +04:00
/* Post-process the command */
2014-04-14 14:40:23 +04:00
if ( + + client - > bits . localsock . num_replies = =
client - > bits . localsock . expected_replies ) {
2004-06-24 12:02:38 +04:00
client - > bits . localsock . state = POST_COMMAND ;
pthread_cond_signal ( & client - > bits . localsock . cond ) ;
}
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Got %d replies, expecting: %d \n " ,
client , client - > bits . localsock . num_replies ,
2014-04-14 14:40:23 +04:00
client - > bits . localsock . expected_replies ) ;
2004-06-24 12:02:38 +04:00
}
2014-04-09 10:12:13 +04:00
pthread_mutex_unlock ( & client - > bits . localsock . mutex ) ;
2004-06-24 12:02:38 +04:00
}
/* This is the thread that runs the PRE and post commands for a particular connection */
2006-12-02 02:10:26 +03:00
static __attribute__ ( ( noreturn ) ) void * pre_and_post_thread ( void * arg )
2004-06-24 12:02:38 +04:00
{
struct local_client * client = ( struct local_client * ) arg ;
int status ;
2007-03-29 17:59:33 +04:00
int write_status ;
2004-06-24 12:02:38 +04:00
sigset_t ss ;
int pipe_fd = client - > bits . localsock . pipe ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Pre&post thread pipe fd %d \n " , client , pipe_fd ) ;
Fix a deadlock in clvmd.
The signalling code (pthread_cond_signal/pthread_cond_wait) in the
pre_and_post_thread was using the wait mutex (see man pthread_cond_wait)
incorrectly, and this could cause clvmd to deadlock when timing was
right. Detailed explanation of the problem follows.
There is a single mutex around (L for Lock, U for Unlock), a signal (S) and a
wait (W). C for pthread_create. Time flows from left to right, each arrow is a
thread.
So first the "naive" scenario, with no mutex (PPT = pre_and_post_thread, MCT =
main clvmd thread; well actually the thread that does read_from_local_sock). I
will also use X, for a moment when MCT actually waits for something to happen
that PPT was supposed to do.
MCT -----C ------S--X-----S----X----------------------S------XXXXXXXXX
| everything OK up to this --> <-- point...
PPT -----WWW-----WWWW------------------------------WWWWWWWWWWWWW
Ok, so pthread API actually does not let you use W/S like that. It goes out of
its way to tell you that you need a mutex to protect the W so that the above
cannot happen. *But* if you are creative and just lock around the W's and S's,
this happens:
MCT ----C-----LSU----X-----LSU----X------------LSU------XXXXXXX
|
PPT ---LWWWU-------LWWWWU-----------------------LWWWWWWWWW
Ooops. Nothing changed (the above is what actually was done by clvmd before
this satch). So let's do it differently, holding L locked *all* the time in
PPT, unless we are actually in W (this is something that the pthread API does
itself, see the man page).
MCT ----C-----LSU------X---LSU---X-----LLLLLLLSU----X----
| (and they live happily ever after)
PPT L---WWWWW---------WWWW----------------W----------
So W actually ensures that L is unlocked *atomically* together with entering
the wait. That means that unless PPT is actually waiting, it cannot be
signalled by MCT. So if MCT happens to signal it too soon (it wasn't waiting
yet), it (MCT) will be blocked on the mutex (L), until PPT is actually ready to
do something.
2010-10-20 18:46:45 +04:00
pthread_mutex_lock ( & client - > bits . localsock . mutex ) ;
2004-06-24 12:02:38 +04:00
/* Ignore SIGUSR1 (handled by master process) but enable
SIGUSR2 ( kills subthreads ) */
sigemptyset ( & ss ) ;
sigaddset ( & ss , SIGUSR1 ) ;
pthread_sigmask ( SIG_BLOCK , & ss , NULL ) ;
sigdelset ( & ss , SIGUSR1 ) ;
sigaddset ( & ss , SIGUSR2 ) ;
pthread_sigmask ( SIG_UNBLOCK , & ss , NULL ) ;
/* Loop around doing PRE and POST functions until the client goes away */
while ( ! client - > bits . localsock . finished ) {
/* Execute the code */
2011-10-11 13:23:48 +04:00
/* FIXME: usage of init_test() is unprotected as in do_command() */
2014-03-10 15:15:26 +04:00
if ( ( status = do_pre_command ( client ) ) )
2004-06-24 12:02:38 +04:00
client - > bits . localsock . all_success = 0 ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Pre&post thread writes status %d down to pipe fd %d \n " ,
2014-03-20 17:24:06 +04:00
client , status , pipe_fd ) ;
2007-03-29 17:59:33 +04:00
2004-06-24 12:02:38 +04:00
/* Tell the parent process we have finished this bit */
2014-03-10 15:15:41 +04:00
while ( ( write_status = write ( pipe_fd , & status , sizeof ( int ) ) ) ! = sizeof ( int ) )
if ( write_status > = 0 | | ( errno ! = EINTR & & errno ! = EAGAIN ) ) {
2014-03-20 12:21:34 +04:00
log_error ( " Error sending to pipe: %m " ) ;
2007-03-29 17:59:33 +04:00
break ;
2014-03-10 15:15:41 +04:00
}
2007-03-29 17:59:33 +04:00
2007-11-15 13:16:14 +03:00
if ( status ) {
client - > bits . localsock . state = POST_COMMAND ;
goto next_pre ;
}
2004-06-24 12:02:38 +04:00
/* We may need to wait for the condition variable before running the post command */
2010-04-06 19:29:30 +04:00
if ( client - > bits . localsock . state ! = POST_COMMAND & &
2014-03-20 17:24:06 +04:00
! client - > bits . localsock . finished ) {
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Pre&post thread waiting to do post command, state = %d \n " ,
2014-03-20 17:24:06 +04:00
client , client - > bits . localsock . state ) ;
2004-06-24 12:02:38 +04:00
pthread_cond_wait ( & client - > bits . localsock . cond ,
& client - > bits . localsock . mutex ) ;
2014-03-20 17:24:06 +04:00
}
2004-06-24 12:02:38 +04:00
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Pre&post thread got post command condition... \n " , client ) ;
2004-06-24 12:02:38 +04:00
2005-02-08 12:05:58 +03:00
/* POST function must always run, even if the client aborts */
2005-01-07 17:22:49 +03:00
status = 0 ;
2004-06-24 12:02:38 +04:00
do_post_command ( client ) ;
2014-03-10 15:15:41 +04:00
while ( ( write_status = write ( pipe_fd , & status , sizeof ( int ) ) ) ! = sizeof ( int ) )
if ( write_status > = 0 | | ( errno ! = EINTR & & errno ! = EAGAIN ) ) {
2014-03-20 12:21:34 +04:00
log_error ( " Error sending to pipe: %m " ) ;
2007-03-29 17:59:33 +04:00
break ;
2014-03-10 15:15:41 +04:00
}
2007-11-15 13:16:14 +03:00
next_pre :
2008-03-17 12:37:47 +03:00
if ( client - > bits . localsock . state ! = PRE_COMMAND & &
! client - > bits . localsock . finished ) {
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Pre&post thread waiting for next pre command \n " , client ) ;
2004-06-24 12:02:38 +04:00
pthread_cond_wait ( & client - > bits . localsock . cond ,
& client - > bits . localsock . mutex ) ;
}
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Pre&post thread got pre command condition... \n " , client ) ;
2004-06-24 12:02:38 +04:00
}
Fix a deadlock in clvmd.
The signalling code (pthread_cond_signal/pthread_cond_wait) in the
pre_and_post_thread was using the wait mutex (see man pthread_cond_wait)
incorrectly, and this could cause clvmd to deadlock when timing was
right. Detailed explanation of the problem follows.
There is a single mutex around (L for Lock, U for Unlock), a signal (S) and a
wait (W). C for pthread_create. Time flows from left to right, each arrow is a
thread.
So first the "naive" scenario, with no mutex (PPT = pre_and_post_thread, MCT =
main clvmd thread; well actually the thread that does read_from_local_sock). I
will also use X, for a moment when MCT actually waits for something to happen
that PPT was supposed to do.
MCT -----C ------S--X-----S----X----------------------S------XXXXXXXXX
| everything OK up to this --> <-- point...
PPT -----WWW-----WWWW------------------------------WWWWWWWWWWWWW
Ok, so pthread API actually does not let you use W/S like that. It goes out of
its way to tell you that you need a mutex to protect the W so that the above
cannot happen. *But* if you are creative and just lock around the W's and S's,
this happens:
MCT ----C-----LSU----X-----LSU----X------------LSU------XXXXXXX
|
PPT ---LWWWU-------LWWWWU-----------------------LWWWWWWWWW
Ooops. Nothing changed (the above is what actually was done by clvmd before
this satch). So let's do it differently, holding L locked *all* the time in
PPT, unless we are actually in W (this is something that the pthread API does
itself, see the man page).
MCT ----C-----LSU------X---LSU---X-----LLLLLLLSU----X----
| (and they live happily ever after)
PPT L---WWWWW---------WWWW----------------W----------
So W actually ensures that L is unlocked *atomically* together with entering
the wait. That means that unless PPT is actually waiting, it cannot be
signalled by MCT. So if MCT happens to signal it too soon (it wasn't waiting
yet), it (MCT) will be blocked on the mutex (L), until PPT is actually ready to
do something.
2010-10-20 18:46:45 +04:00
pthread_mutex_unlock ( & client - > bits . localsock . mutex ) ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Pre&post thread finished \n " , client ) ;
2014-03-20 17:24:06 +04:00
2014-03-10 15:16:09 +04:00
pthread_exit ( NULL ) ;
2004-06-24 12:02:38 +04:00
}
/* Process a command on the local node and store the result */
static int process_local_command ( struct clvm_header * msg , int msglen ,
struct local_client * client ,
unsigned short xid )
{
2014-03-20 12:24:45 +04:00
char * replybuf ;
2005-01-13 16:24:02 +03:00
int buflen = max_cluster_message - sizeof ( struct clvm_header ) - 1 ;
2004-06-24 12:02:38 +04:00
int replylen = 0 ;
int status ;
2014-03-20 12:24:45 +04:00
if ( ! ( replybuf = dm_malloc ( max_cluster_message ) ) )
return - 1 ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) process_local_command: %s msg=%p, msglen =%d \n " ,
client , decode_cmd ( msg - > cmd ) , msg , msglen ) ;
2008-06-05 18:24:28 +04:00
2012-01-21 09:31:54 +04:00
/* If remote flag is set, just set a successful status code. */
if ( msg - > flags & CLVMD_FLAG_REMOTE )
status = 0 ;
else
status = do_command ( client , msg , msglen , & replybuf , buflen , & replylen ) ;
2004-06-24 12:02:38 +04:00
if ( status )
client - > bits . localsock . all_success = 0 ;
/* If we took too long then discard the reply */
2014-03-10 15:15:26 +04:00
if ( xid = = client - > xid )
2004-06-24 12:02:38 +04:00
add_reply_to_list ( client , status , our_csid , replybuf , replylen ) ;
2014-03-10 15:15:26 +04:00
else
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Local command took too long, discarding xid %d, current is %d \n " ,
client , xid , client - > xid ) ;
2004-06-24 12:02:38 +04:00
2014-03-20 12:24:45 +04:00
dm_free ( replybuf ) ;
2014-03-10 15:15:26 +04:00
2004-06-24 12:02:38 +04:00
return status ;
}
2007-05-02 16:22:40 +04:00
static int process_reply ( const struct clvm_header * msg , int msglen , const char * csid )
2004-06-24 12:02:38 +04:00
{
2014-03-10 15:15:26 +04:00
struct local_client * client ;
2004-06-24 12:02:38 +04:00
2014-03-10 15:15:26 +04:00
if ( ! ( client = find_client ( msg - > clientid ) ) ) {
2004-06-24 12:02:38 +04:00
DEBUGLOG ( " Got message for unknown client 0x%x \n " ,
msg - > clientid ) ;
2014-03-20 12:21:34 +04:00
log_error ( " Got message for unknown client 0x%x. " ,
2004-06-24 12:02:38 +04:00
msg - > clientid ) ;
return - 1 ;
}
if ( msg - > status )
client - > bits . localsock . all_success = 0 ;
/* Gather replies together for this client id */
2014-03-10 15:15:26 +04:00
if ( msg - > xid = = client - > xid )
2004-06-24 12:02:38 +04:00
add_reply_to_list ( client , msg - > status , csid , msg - > args ,
msg - > arglen ) ;
2014-03-10 15:15:26 +04:00
else
2004-06-24 12:02:38 +04:00
DEBUGLOG ( " Discarding reply with old XID %d, current = %d \n " ,
msg - > xid , client - > xid ) ;
2014-03-10 15:15:26 +04:00
2004-06-24 12:02:38 +04:00
return 0 ;
}
/* Send an aggregated reply back to the client */
static void send_local_reply ( struct local_client * client , int status , int fd )
{
struct clvm_header * clientreply ;
struct node_reply * thisreply = client - > bits . localsock . replies ;
char * replybuf ;
char * ptr ;
int message_len = 0 ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Send local reply \n " , client ) ;
2004-06-24 12:02:38 +04:00
/* Work out the total size of the reply */
while ( thisreply ) {
if ( thisreply - > replymsg )
message_len + = strlen ( thisreply - > replymsg ) + 1 ;
else
message_len + + ;
message_len + = strlen ( thisreply - > node ) + 1 + sizeof ( int ) ;
thisreply = thisreply - > next ;
}
/* Add in the size of our header */
2011-03-09 01:48:50 +03:00
message_len = message_len + sizeof ( struct clvm_header ) ;
2014-03-20 12:24:45 +04:00
if ( ! ( replybuf = dm_malloc ( message_len ) ) ) {
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) Memory allocation fails \n " , client ) ;
2011-03-09 01:48:50 +03:00
return ;
}
2004-06-24 12:02:38 +04:00
clientreply = ( struct clvm_header * ) replybuf ;
2005-01-21 14:35:24 +03:00
clientreply - > status = status ;
2004-06-24 12:02:38 +04:00
clientreply - > cmd = CLVMD_CMD_REPLY ;
clientreply - > node [ 0 ] = ' \0 ' ;
2011-03-09 01:48:50 +03:00
clientreply - > xid = 0 ;
clientreply - > clientid = 0 ;
2005-01-21 14:35:24 +03:00
clientreply - > flags = 0 ;
2004-06-24 12:02:38 +04:00
ptr = clientreply - > args ;
/* Add in all the replies, and free them as we go */
thisreply = client - > bits . localsock . replies ;
while ( thisreply ) {
struct node_reply * tempreply = thisreply ;
strcpy ( ptr , thisreply - > node ) ;
ptr + = strlen ( thisreply - > node ) + 1 ;
2005-01-21 14:35:24 +03:00
if ( thisreply - > status )
clientreply - > flags | = CLVMD_FLAG_NODEERRS ;
2006-12-02 02:10:26 +03:00
memcpy ( ptr , & thisreply - > status , sizeof ( int ) ) ;
2004-06-24 12:02:38 +04:00
ptr + = sizeof ( int ) ;
if ( thisreply - > replymsg ) {
strcpy ( ptr , thisreply - > replymsg ) ;
ptr + = strlen ( thisreply - > replymsg ) + 1 ;
} else {
ptr [ 0 ] = ' \0 ' ;
ptr + + ;
}
thisreply = thisreply - > next ;
2014-03-20 12:24:45 +04:00
dm_free ( tempreply - > replymsg ) ;
dm_free ( tempreply ) ;
2004-06-24 12:02:38 +04:00
}
/* Terminate with an empty node name */
* ptr = ' \0 ' ;
2011-03-09 01:48:50 +03:00
clientreply - > arglen = ptr - clientreply - > args ;
2004-06-24 12:02:38 +04:00
/* And send it */
send_message ( replybuf , message_len , our_csid , fd ,
" Error sending REPLY to client " ) ;
2014-03-20 12:24:45 +04:00
dm_free ( replybuf ) ;
2004-06-24 12:02:38 +04:00
/* Reset comms variables */
client - > bits . localsock . replies = NULL ;
client - > bits . localsock . expected_replies = 0 ;
client - > bits . localsock . in_progress = FALSE ;
client - > bits . localsock . sent_out = FALSE ;
}
/* Just free a reply chain baceuse it wasn't used. */
static void free_reply ( struct local_client * client )
{
/* Add in all the replies, and free them as we go */
struct node_reply * thisreply = client - > bits . localsock . replies ;
while ( thisreply ) {
struct node_reply * tempreply = thisreply ;
thisreply = thisreply - > next ;
2014-03-20 12:24:45 +04:00
dm_free ( tempreply - > replymsg ) ;
dm_free ( tempreply ) ;
2004-06-24 12:02:38 +04:00
}
client - > bits . localsock . replies = NULL ;
}
/* Send our version number to the cluster */
2011-03-30 00:30:05 +04:00
static void send_version_message ( void )
2004-06-24 12:02:38 +04:00
{
char message [ sizeof ( struct clvm_header ) + sizeof ( int ) * 3 ] ;
struct clvm_header * msg = ( struct clvm_header * ) message ;
2014-03-09 01:58:37 +04:00
int version_nums [ 3 ] = {
htonl ( CLVMD_MAJOR_VERSION ) ,
htonl ( CLVMD_MINOR_VERSION ) ,
htonl ( CLVMD_PATCH_VERSION )
} ;
2004-06-24 12:02:38 +04:00
msg - > cmd = CLVMD_CMD_VERSION ;
msg - > status = 0 ;
msg - > flags = 0 ;
msg - > clientid = 0 ;
2006-12-02 02:10:26 +03:00
msg - > arglen = sizeof ( version_nums ) ;
2004-06-24 12:02:38 +04:00
2006-12-02 02:10:26 +03:00
memcpy ( & msg - > args , version_nums , sizeof ( version_nums ) ) ;
2005-02-18 18:31:32 +03:00
hton_clvm ( msg ) ;
2006-12-02 02:10:26 +03:00
2005-01-13 16:24:02 +03:00
clops - > cluster_send_message ( message , sizeof ( message ) , NULL ,
2014-03-10 15:15:26 +04:00
" Error Sending version number " ) ;
2004-06-24 12:02:38 +04:00
}
/* Send a message to either a local client or another server */
2007-05-02 16:22:40 +04:00
static int send_message ( void * buf , int msglen , const char * csid , int fd ,
2004-06-24 12:02:38 +04:00
const char * errtext )
{
2014-03-10 15:35:44 +04:00
int len = 0 ;
2014-03-09 02:14:46 +04:00
int ptr ;
2007-03-29 17:59:33 +04:00
struct timespec delay ;
struct timespec remtime ;
int retry_cnt = 0 ;
2004-06-24 12:02:38 +04:00
/* Send remote messages down the cluster socket */
2014-03-10 15:15:26 +04:00
if ( ! csid | | ! ISLOCAL_CSID ( csid ) ) {
2005-02-18 18:31:32 +03:00
hton_clvm ( ( struct clvm_header * ) buf ) ;
2005-01-13 16:24:02 +03:00
return clops - > cluster_send_message ( buf , msglen , csid , errtext ) ;
2014-03-09 02:14:46 +04:00
}
2007-03-29 17:59:33 +04:00
2014-03-09 02:14:46 +04:00
/* Make sure it all goes */
for ( ptr = 0 ; ptr < msglen ; ) {
if ( ( len = write ( fd , ( char * ) buf + ptr , msglen - ptr ) ) < = 0 ) {
if ( errno = = EINTR )
continue ;
if ( ( errno = = EAGAIN | | errno = = EIO | | errno = = ENOSPC ) & &
+ + retry_cnt < MAX_RETRIES ) {
delay . tv_sec = 0 ;
delay . tv_nsec = 100000 ;
remtime . tv_sec = 0 ;
remtime . tv_nsec = 0 ;
( void ) nanosleep ( & delay , & remtime ) ;
continue ;
2004-06-24 12:02:38 +04:00
}
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " %s " , errtext ) ;
2014-03-09 02:14:46 +04:00
log_error ( " %s " , errtext ) ;
break ;
}
ptr + = len ;
2004-06-24 12:02:38 +04:00
}
2014-03-10 15:15:26 +04:00
2004-06-24 12:02:38 +04:00
return len ;
}
static int process_work_item ( struct lvm_thread_cmd * cmd )
{
2005-03-07 20:03:44 +03:00
/* If msg is NULL then this is a cleanup request */
if ( cmd - > msg = = NULL ) {
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) process_work_item: free \n " , cmd - > client ) ;
2005-03-07 20:03:44 +03:00
cmd_client_cleanup ( cmd - > client ) ;
2014-04-09 10:06:16 +04:00
pthread_mutex_destroy ( & cmd - > client - > bits . localsock . mutex ) ;
pthread_cond_destroy ( & cmd - > client - > bits . localsock . cond ) ;
2014-03-20 12:24:45 +04:00
dm_free ( cmd - > client ) ;
2005-03-07 20:03:44 +03:00
return 0 ;
}
2004-06-24 12:02:38 +04:00
if ( ! cmd - > remote ) {
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) process_work_item: local \n " , cmd - > client ) ;
2004-06-24 12:02:38 +04:00
process_local_command ( cmd - > msg , cmd - > msglen , cmd - > client ,
cmd - > xid ) ;
} else {
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) process_work_item: remote \n " , cmd - > client ) ;
2004-06-24 12:02:38 +04:00
process_remote_command ( cmd - > msg , cmd - > msglen , cmd - > client - > fd ,
cmd - > csid ) ;
}
2014-03-21 15:56:01 +04:00
2004-06-24 12:02:38 +04:00
return 0 ;
}
/*
* Routine that runs in the " LVM thread " .
*/
2011-03-30 16:36:19 +04:00
static void * lvm_thread_fn ( void * arg )
2004-06-24 12:02:38 +04:00
{
sigset_t ss ;
2010-04-20 18:07:37 +04:00
struct lvm_startup_params * lvm_params = arg ;
2011-10-11 13:54:39 +04:00
struct lvm_thread_cmd * cmd ;
2004-06-24 12:02:38 +04:00
DEBUGLOG ( " LVM thread function started \n " ) ;
/* Ignore SIGUSR1 & 2 */
sigemptyset ( & ss ) ;
sigaddset ( & ss , SIGUSR1 ) ;
sigaddset ( & ss , SIGUSR2 ) ;
pthread_sigmask ( SIG_BLOCK , & ss , NULL ) ;
/* Initialise the interface to liblvm */
2011-09-25 00:48:34 +04:00
init_clvm ( lvm_params - > excl_uuid ) ;
2004-06-24 12:02:38 +04:00
2005-04-01 17:01:01 +04:00
/* Allow others to get moving */
2011-10-11 13:26:04 +04:00
pthread_barrier_wait ( & lvm_start_barrier ) ;
2014-03-20 17:24:06 +04:00
DEBUGLOG ( " LVM thread ready for work. \n " ) ;
2005-04-01 17:01:01 +04:00
2004-06-24 12:02:38 +04:00
/* Now wait for some actual work */
2011-10-11 13:54:39 +04:00
pthread_mutex_lock ( & lvm_thread_mutex ) ;
2004-06-24 12:02:38 +04:00
2014-03-25 13:50:36 +04:00
for ( ; ; ) {
while ( ! dm_list_empty ( & lvm_cmd_head ) ) {
2011-10-11 13:54:39 +04:00
cmd = dm_list_item ( dm_list_first ( & lvm_cmd_head ) ,
struct lvm_thread_cmd ) ;
2008-11-04 01:14:30 +03:00
dm_list_del ( & cmd - > list ) ;
2004-06-24 12:02:38 +04:00
pthread_mutex_unlock ( & lvm_thread_mutex ) ;
process_work_item ( cmd ) ;
2014-03-20 12:24:45 +04:00
dm_free ( cmd - > msg ) ;
dm_free ( cmd ) ;
2004-06-24 12:02:38 +04:00
pthread_mutex_lock ( & lvm_thread_mutex ) ;
}
2011-03-30 16:36:19 +04:00
2014-03-25 13:50:36 +04:00
if ( lvm_thread_exit )
break ;
DEBUGLOG ( " LVM thread waiting for work \n " ) ;
pthread_cond_wait ( & lvm_thread_cond , & lvm_thread_mutex ) ;
}
2011-10-11 13:54:39 +04:00
pthread_mutex_unlock ( & lvm_thread_mutex ) ;
2014-03-20 17:24:06 +04:00
DEBUGLOG ( " LVM thread exits \n " ) ;
2011-10-11 13:54:39 +04:00
2014-03-20 17:23:03 +04:00
destroy_lvm ( ) ;
2011-03-30 16:36:19 +04:00
pthread_exit ( NULL ) ;
2004-06-24 12:02:38 +04:00
}
/* Pass down some work to the LVM thread */
static int add_to_lvmqueue ( struct local_client * client , struct clvm_header * msg ,
2007-05-02 16:22:40 +04:00
int msglen , const char * csid )
2004-06-24 12:02:38 +04:00
{
struct lvm_thread_cmd * cmd ;
2014-03-20 12:24:45 +04:00
if ( ! ( cmd = dm_malloc ( sizeof ( * cmd ) ) ) )
2005-01-21 14:35:24 +03:00
return ENOMEM ;
2004-06-24 12:02:38 +04:00
2005-03-07 20:03:44 +03:00
if ( msglen ) {
2014-03-20 12:24:45 +04:00
if ( ! ( cmd - > msg = dm_malloc ( msglen ) ) ) {
2014-03-20 12:21:34 +04:00
log_error ( " Unable to allocate buffer space. " ) ;
2014-03-20 12:24:45 +04:00
dm_free ( cmd ) ;
2005-03-07 20:03:44 +03:00
return - 1 ;
}
memcpy ( cmd - > msg , msg , msglen ) ;
}
2014-03-21 15:56:01 +04:00
else
2005-03-07 20:03:44 +03:00
cmd - > msg = NULL ;
2014-03-21 15:56:01 +04:00
2004-06-24 12:02:38 +04:00
cmd - > client = client ;
cmd - > msglen = msglen ;
cmd - > xid = client - > xid ;
2005-03-07 20:03:44 +03:00
2004-06-24 12:02:38 +04:00
if ( csid ) {
2005-01-13 16:24:02 +03:00
memcpy ( cmd - > csid , csid , max_csid_len ) ;
2004-06-24 12:02:38 +04:00
cmd - > remote = 1 ;
2014-03-21 15:56:01 +04:00
} else
2004-06-24 12:02:38 +04:00
cmd - > remote = 0 ;
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) add_to_lvmqueue: cmd=%p, msg=%p, len=%d, csid=%p, xid=%d \n " ,
client , cmd , msg , msglen , csid , cmd - > xid ) ;
2004-06-24 12:02:38 +04:00
pthread_mutex_lock ( & lvm_thread_mutex ) ;
2014-03-25 13:50:36 +04:00
if ( lvm_thread_exit ) {
pthread_mutex_unlock ( & lvm_thread_mutex ) ;
dm_free ( cmd - > msg ) ;
dm_free ( cmd ) ;
return - 1 ; /* We are about to exit */
}
2008-11-04 01:14:30 +03:00
dm_list_add ( & lvm_cmd_head , & cmd - > list ) ;
2004-06-24 12:02:38 +04:00
pthread_cond_signal ( & lvm_thread_cond ) ;
pthread_mutex_unlock ( & lvm_thread_mutex ) ;
return 0 ;
}
2007-08-17 15:51:23 +04:00
/* Return 0 if we can talk to an existing clvmd */
static int check_local_clvmd ( void )
{
int local_socket ;
int ret = 0 ;
2012-06-21 23:19:28 +04:00
struct sockaddr_un sockaddr = { . sun_family = AF_UNIX } ;
if ( ! dm_strncpy ( sockaddr . sun_path , CLVMD_SOCKNAME , sizeof ( sockaddr . sun_path ) ) ) {
log_error ( " %s: clvmd socket name too long. " , CLVMD_SOCKNAME ) ;
return - 1 ;
}
2007-08-17 15:51:23 +04:00
/* Open local socket */
if ( ( local_socket = socket ( PF_UNIX , SOCK_STREAM , 0 ) ) < 0 ) {
2012-06-21 23:19:28 +04:00
log_sys_error ( " socket " , " local socket " ) ;
2007-08-17 15:51:23 +04:00
return - 1 ;
}
if ( connect ( local_socket , ( struct sockaddr * ) & sockaddr ,
sizeof ( sockaddr ) ) ) {
2012-06-21 23:19:28 +04:00
log_sys_error ( " connect " , " local socket " ) ;
2007-08-17 15:51:23 +04:00
ret = - 1 ;
}
2012-03-02 01:12:37 +04:00
if ( close ( local_socket ) )
log_sys_error ( " close " , " local socket " ) ;
2007-08-17 15:51:23 +04:00
return ret ;
}
2010-07-28 17:55:42 +04:00
static void close_local_sock ( int local_socket )
{
if ( local_socket ! = - 1 & & close ( local_socket ) )
2012-02-28 14:14:06 +04:00
log_sys_error ( " close " , CLVMD_SOCKNAME ) ;
2010-07-28 17:55:42 +04:00
if ( CLVMD_SOCKNAME [ 0 ] ! = ' \0 ' & & unlink ( CLVMD_SOCKNAME ) )
stack ;
}
2007-08-17 15:51:23 +04:00
2004-06-24 12:02:38 +04:00
/* Open the local socket, that's the one we talk to libclvm down */
2011-03-30 00:30:05 +04:00
static int open_local_sock ( void )
2004-06-24 12:02:38 +04:00
{
2010-07-28 17:55:42 +04:00
mode_t old_mask ;
2012-06-21 23:19:28 +04:00
int local_socket = - 1 ;
struct sockaddr_un sockaddr = { . sun_family = AF_UNIX } ;
if ( ! dm_strncpy ( sockaddr . sun_path , CLVMD_SOCKNAME , sizeof ( sockaddr . sun_path ) ) ) {
log_error ( " %s: clvmd socket name too long. " , CLVMD_SOCKNAME ) ;
return - 1 ;
}
2010-07-28 17:55:42 +04:00
close_local_sock ( local_socket ) ;
2010-12-13 13:43:56 +03:00
( void ) dm_prepare_selinux_context ( CLVMD_SOCKNAME , S_IFSOCK ) ;
2010-07-28 17:55:42 +04:00
old_mask = umask ( 0077 ) ;
2004-06-24 12:02:38 +04:00
/* Open local socket */
2014-03-21 15:56:01 +04:00
if ( ( local_socket = socket ( PF_UNIX , SOCK_STREAM , 0 ) ) < 0 ) {
2004-06-24 12:02:38 +04:00
log_error ( " Can't create local socket: %m " ) ;
2010-07-28 17:55:42 +04:00
goto error ;
2004-06-24 12:02:38 +04:00
}
2010-04-20 18:07:37 +04:00
2008-03-17 12:37:47 +03:00
/* Set Close-on-exec & non-blocking */
2010-04-20 18:07:37 +04:00
if ( fcntl ( local_socket , F_SETFD , 1 ) )
DEBUGLOG ( " setting CLOEXEC on local_socket failed: %s \n " , strerror ( errno ) ) ;
2012-06-20 12:49:16 +04:00
if ( fcntl ( local_socket , F_SETFL , fcntl ( local_socket , F_GETFL , 0 ) | O_NONBLOCK ) )
DEBUGLOG ( " setting O_NONBLOCK on local_socket failed: %s \n " , strerror ( errno ) ) ;
2004-06-24 12:02:38 +04:00
2010-12-13 13:43:56 +03:00
2004-06-24 12:02:38 +04:00
if ( bind ( local_socket , ( struct sockaddr * ) & sockaddr , sizeof ( sockaddr ) ) ) {
log_error ( " can't bind local socket: %m " ) ;
2010-07-28 17:55:42 +04:00
goto error ;
2004-06-24 12:02:38 +04:00
}
if ( listen ( local_socket , 1 ) ! = 0 ) {
log_error ( " listen local: %m " ) ;
2010-07-28 17:55:42 +04:00
goto error ;
2004-06-24 12:02:38 +04:00
}
2010-07-28 17:55:42 +04:00
umask ( old_mask ) ;
2010-12-13 13:43:56 +03:00
( void ) dm_prepare_selinux_context ( NULL , 0 ) ;
2004-06-24 12:02:38 +04:00
return local_socket ;
2010-07-28 17:55:42 +04:00
error :
close_local_sock ( local_socket ) ;
umask ( old_mask ) ;
2010-12-13 13:43:56 +03:00
( void ) dm_prepare_selinux_context ( NULL , 0 ) ;
2010-07-28 17:55:42 +04:00
return - 1 ;
2004-06-24 12:02:38 +04:00
}
2011-02-18 17:47:28 +03:00
void process_message ( struct local_client * client , char * buf , int len ,
2007-05-02 16:22:40 +04:00
const char * csid )
2004-11-16 13:55:01 +03:00
{
2013-07-30 23:12:33 +04:00
char nodename [ max_cluster_member_name_len ] ;
2014-03-21 15:56:01 +04:00
struct clvm_header * inheader = ( struct clvm_header * ) buf ;
2004-11-16 13:55:01 +03:00
ntoh_clvm ( inheader ) ; /* Byteswap fields */
2013-07-30 23:12:33 +04:00
2014-03-21 15:56:01 +04:00
if ( verify_message ( buf , len ) < 0 ) {
2013-07-30 23:12:33 +04:00
clops - > name_from_csid ( csid , nodename ) ;
2014-03-20 12:21:34 +04:00
log_error ( " process_message from %s len %d bad verify. " , nodename , len ) ;
2013-07-30 23:12:33 +04:00
dump_message ( buf , len ) ;
return ;
}
2004-11-16 13:55:01 +03:00
if ( inheader - > cmd = = CLVMD_CMD_REPLY )
process_reply ( inheader , len , csid ) ;
else
add_to_lvmqueue ( client , inheader , len , csid ) ;
}
2007-05-02 16:22:40 +04:00
static void check_all_callback ( struct local_client * client , const char * csid ,
2004-06-24 12:02:38 +04:00
int node_up )
{
if ( ! node_up )
2014-03-21 15:56:01 +04:00
add_reply_to_list ( client , EHOSTDOWN , csid , " CLVMD not running " , 18 ) ;
2004-06-24 12:02:38 +04:00
}
/* Check to see if all CLVMDs are running (ie one on
every node in the cluster ) .
If not , returns - 1 and prints out a list of errant nodes */
static int check_all_clvmds_running ( struct local_client * client )
{
2017-07-01 03:17:40 +03:00
DEBUGLOG ( " (%p) check_all_clvmds_running \n " , client ) ;
2005-01-13 16:24:02 +03:00
return clops - > cluster_do_node_callback ( client , check_all_callback ) ;
2004-06-24 12:02:38 +04:00
}
/* Return a local_client struct given a client ID.
client IDs are in network byte order */
static struct local_client * find_client ( int clientid )
{
struct local_client * thisfd ;
2014-03-21 15:56:01 +04:00
for ( thisfd = & local_client_head ; thisfd ; thisfd = thisfd - > next )
2011-04-08 18:40:18 +04:00
if ( thisfd - > fd = = ( int ) ntohl ( clientid ) )
2004-06-24 12:02:38 +04:00
return thisfd ;
2014-03-21 15:56:01 +04:00
2004-06-24 12:02:38 +04:00
return NULL ;
}
/* Byte-swapping routines for the header so we
work in a heterogeneous environment */
static void hton_clvm ( struct clvm_header * hdr )
{
hdr - > status = htonl ( hdr - > status ) ;
hdr - > arglen = htonl ( hdr - > arglen ) ;
hdr - > xid = htons ( hdr - > xid ) ;
/* Don't swap clientid as it's only a token as far as
remote nodes are concerned */
}
static void ntoh_clvm ( struct clvm_header * hdr )
{
hdr - > status = ntohl ( hdr - > status ) ;
hdr - > arglen = ntohl ( hdr - > arglen ) ;
hdr - > xid = ntohs ( hdr - > xid ) ;
}
/* Handler for SIGUSR2 - sent to kill subthreads */
static void sigusr2_handler ( int sig )
{
DEBUGLOG ( " SIGUSR2 received \n " ) ;
return ;
}
static void sigterm_handler ( int sig )
{
quit = 1 ;
return ;
}
2005-01-13 16:24:02 +03:00
2005-04-19 14:36:42 +04:00
static void sighup_handler ( int sig )
{
reread_config = 1 ;
}
2005-01-13 16:24:02 +03:00
int sync_lock ( const char * resource , int mode , int flags , int * lockid )
{
return clops - > sync_lock ( resource , mode , flags , lockid ) ;
}
int sync_unlock ( const char * resource , int lockid )
{
return clops - > sync_unlock ( resource , lockid ) ;
}
2009-02-02 17:34:24 +03:00
static if_type_t parse_cluster_interface ( char * ifname )
{
if_type_t iface = IF_AUTO ;
if ( ! strcmp ( ifname , " auto " ) )
iface = IF_AUTO ;
2014-03-09 01:57:31 +04:00
else if ( ! strcmp ( ifname , " cman " ) )
2009-02-02 17:34:24 +03:00
iface = IF_CMAN ;
2014-03-09 01:57:31 +04:00
else if ( ! strcmp ( ifname , " openais " ) )
2009-02-02 17:34:24 +03:00
iface = IF_OPENAIS ;
2014-03-09 01:57:31 +04:00
else if ( ! strcmp ( ifname , " corosync " ) )
2009-02-02 17:34:24 +03:00
iface = IF_COROSYNC ;
2014-03-09 01:57:31 +04:00
else if ( ! strcmp ( ifname , " singlenode " ) )
2010-03-18 12:19:30 +03:00
iface = IF_SINGLENODE ;
2009-02-02 17:34:24 +03:00
return iface ;
}
2009-09-01 13:48:01 +04:00
/*
* Try and find a cluster system in corosync ' s objdb , if it is running . This is
* only called if the command - line option is not present , and if it fails
* we still try the interfaces in order .
*/
2011-03-30 00:30:05 +04:00
static if_type_t get_cluster_type ( void )
2009-09-01 13:48:01 +04:00
{
# ifdef HAVE_COROSYNC_CONFDB_H
confdb_handle_t handle ;
if_type_t type = IF_AUTO ;
int result ;
char buf [ 255 ] ;
size_t namelen = sizeof ( buf ) ;
hdb_handle_t cluster_handle ;
hdb_handle_t clvmd_handle ;
2014-03-09 01:58:37 +04:00
confdb_callbacks_t callbacks = { 0 } ;
2009-09-01 13:48:01 +04:00
result = confdb_initialize ( & handle , & callbacks ) ;
2014-03-21 15:56:01 +04:00
if ( result ! = CS_OK )
2009-09-01 13:48:01 +04:00
return type ;
2014-03-21 15:56:01 +04:00
result = confdb_object_find_start ( handle , OBJECT_PARENT_HANDLE ) ;
2009-09-01 13:48:01 +04:00
if ( result ! = CS_OK )
goto out ;
2014-03-21 15:56:01 +04:00
result = confdb_object_find ( handle , OBJECT_PARENT_HANDLE , ( void * ) " cluster " , strlen ( " cluster " ) , & cluster_handle ) ;
if ( result ! = CS_OK )
2009-09-01 13:48:01 +04:00
goto out ;
2014-03-21 15:56:01 +04:00
result = confdb_object_find_start ( handle , cluster_handle ) ;
2009-09-01 13:48:01 +04:00
if ( result ! = CS_OK )
goto out ;
2014-03-21 15:56:01 +04:00
result = confdb_object_find ( handle , cluster_handle , ( void * ) " clvmd " , strlen ( " clvmd " ) , & clvmd_handle ) ;
if ( result ! = CS_OK )
2009-09-01 13:48:01 +04:00
goto out ;
2014-03-21 15:56:01 +04:00
result = confdb_key_get ( handle , clvmd_handle , ( void * ) " interface " , strlen ( " interface " ) , buf , & namelen ) ;
if ( result ! = CS_OK )
2009-09-01 13:48:01 +04:00
goto out ;
2012-03-23 13:43:44 +04:00
if ( namelen > = sizeof ( buf ) )
namelen = sizeof ( buf ) - 1 ;
2009-09-01 13:48:01 +04:00
buf [ namelen ] = ' \0 ' ;
type = parse_cluster_interface ( buf ) ;
DEBUGLOG ( " got interface type '%s' from confdb \n " , buf ) ;
out :
confdb_finalize ( handle ) ;
return type ;
# else
return IF_AUTO ;
# endif
}