2014-06-06 06:55:56 +04:00
/*
common system utilities
Copyright ( C ) Amitay Isaacs 2014
Copyright ( C ) Martin Schwenke 2014
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , see < http : //www.gnu.org/licenses/>.
*/
2015-10-26 08:50:46 +03:00
# include "replace.h"
2014-06-06 06:55:56 +04:00
# include "system/filesys.h"
2014-06-10 11:04:34 +04:00
# include "system/shmem.h"
2015-10-26 08:50:46 +03:00
# include "system/network.h"
2014-06-06 06:55:56 +04:00
2016-03-11 03:44:12 +03:00
# include <talloc.h>
2014-06-06 06:55:56 +04:00
# include <libgen.h>
2015-10-26 08:50:46 +03:00
# include "lib/util/debug.h"
2015-11-02 08:03:29 +03:00
# include "protocol/protocol.h"
2015-10-23 06:11:53 +03:00
2015-11-11 06:43:56 +03:00
# include "common/logging.h"
2015-10-23 06:11:53 +03:00
# include "common/system.h"
2014-06-06 07:52:15 +04:00
# if HAVE_SCHED_H
# include <sched.h>
# endif
# if HAVE_PROCINFO_H
# include <procinfo.h>
# endif
2017-03-08 18:33:56 +03:00
# include "lib/util/mkdir_p.h"
2014-06-06 07:52:15 +04:00
/*
if possible , make this task real time
*/
2014-09-12 05:22:36 +04:00
bool set_scheduler ( void )
2014-06-06 07:52:15 +04:00
{
# ifdef _AIX_
# if HAVE_THREAD_SETSCHED
struct thrdentry64 te ;
tid64_t ti ;
ti = 0ULL ;
if ( getthrds64 ( getpid ( ) , & te , sizeof ( te ) , & ti , 1 ) ! = 1 ) {
DEBUG ( DEBUG_ERR , ( " Unable to get thread information \n " ) ) ;
2014-09-12 05:22:36 +04:00
return false ;
2014-06-06 07:52:15 +04:00
}
if ( thread_setsched ( te . ti_tid , 0 , SCHED_RR ) = = - 1 ) {
DEBUG ( DEBUG_ERR , ( " Unable to set scheduler to SCHED_RR (%s) \n " ,
strerror ( errno ) ) ) ;
2014-09-12 05:22:36 +04:00
return false ;
2014-06-06 07:52:15 +04:00
} else {
2014-09-12 05:22:36 +04:00
return true ;
2014-06-06 07:52:15 +04:00
}
# endif
# else /* no AIX */
# if HAVE_SCHED_SETSCHEDULER
struct sched_param p ;
p . sched_priority = 1 ;
2016-10-24 10:24:54 +03:00
if ( sched_setscheduler ( 0 , SCHED_FIFO , & p ) = = - 1 ) {
2014-06-06 07:52:15 +04:00
DEBUG ( DEBUG_CRIT , ( " Unable to set scheduler to SCHED_FIFO (%s) \n " ,
strerror ( errno ) ) ) ;
2014-09-12 05:22:36 +04:00
return false ;
2014-06-06 07:52:15 +04:00
} else {
2014-09-12 05:22:36 +04:00
return true ;
2014-06-06 07:52:15 +04:00
}
# endif
# endif
2014-09-15 05:48:33 +04:00
DEBUG ( DEBUG_CRIT , ( " No way to set real-time priority. \n " ) ) ;
2014-09-12 05:22:36 +04:00
return false ;
2014-06-06 07:52:15 +04:00
}
/*
reset scheduler from real - time to normal scheduling
*/
void reset_scheduler ( void )
{
# ifdef _AIX_
# if HAVE_THREAD_SETSCHED
struct thrdentry64 te ;
tid64_t ti ;
ti = 0ULL ;
if ( getthrds64 ( getpid ( ) , & te , sizeof ( te ) , & ti , 1 ) ! = 1 ) {
DEBUG ( DEBUG_ERR , ( " Unable to get thread information \n " ) ) ;
}
if ( thread_setsched ( te . ti_tid , 0 , SCHED_OTHER ) = = - 1 ) {
DEBUG ( DEBUG_ERR , ( " Unable to set scheduler to SCHED_OTHER \n " ) ) ;
}
# endif
# else /* no AIX */
# if HAVE_SCHED_SETSCHEDULER
struct sched_param p ;
p . sched_priority = 0 ;
if ( sched_setscheduler ( 0 , SCHED_OTHER , & p ) = = - 1 ) {
DEBUG ( DEBUG_ERR , ( " Unable to set scheduler to SCHED_OTHER \n " ) ) ;
}
# endif
# endif
}
2014-06-10 11:04:34 +04:00
/* we don't lock future pages here; it would increase the chance that
* we ' d fail to mmap later on . */
void lockdown_memory ( bool valgrinding )
{
# if defined(HAVE_MLOCKALL) && !defined(_AIX_)
/* Extra stack, please! */
char dummy [ 10000 ] ;
memset ( dummy , 0 , sizeof ( dummy ) ) ;
if ( valgrinding ) {
return ;
}
/* Ignore when running in local daemons mode */
if ( getuid ( ) ! = 0 ) {
return ;
}
/* Avoid compiler optimizing out dummy. */
mlock ( dummy , sizeof ( dummy ) ) ;
if ( mlockall ( MCL_CURRENT ) ! = 0 ) {
DEBUG ( DEBUG_WARNING , ( " Failed to lockdown memory: %s' \n " ,
strerror ( errno ) ) ) ;
}
# endif
}
2015-12-08 06:12:46 +03:00
void ctdb_wait_for_process_to_exit ( pid_t pid )
{
while ( kill ( pid , 0 ) = = 0 | | errno ! = ESRCH ) {
sleep ( 5 ) ;
}
}
2018-06-28 13:30:32 +03:00
# ifdef HAVE_AF_PACKET
bool ctdb_sys_check_iface_exists ( const char * iface )
{
int s ;
struct ifreq ifr ;
s = socket ( AF_PACKET , SOCK_RAW , 0 ) ;
if ( s = = - 1 ) {
/* We don't know if the interface exists, so assume yes */
DBG_ERR ( " Failed to open raw socket \n " ) ;
return true ;
}
strlcpy ( ifr . ifr_name , iface , sizeof ( ifr . ifr_name ) ) ;
if ( ioctl ( s , SIOCGIFINDEX , & ifr ) < 0 & & errno = = ENODEV ) {
DBG_ERR ( " Interface '%s' not found \n " , iface ) ;
close ( s ) ;
return false ;
}
close ( s ) ;
return true ;
}
# else /* HAVE_AF_PACKET */
bool ctdb_sys_check_iface_exists ( const char * iface )
{
/* Not implemented: Interface always considered present */
return true ;
}
# endif /* HAVE_AF_PACKET */
2018-06-28 13:35:56 +03:00
# ifdef HAVE_PEERCRED
int ctdb_get_peer_pid ( const int fd , pid_t * peer_pid )
{
struct ucred cr ;
socklen_t crl = sizeof ( struct ucred ) ;
int ret ;
ret = getsockopt ( fd , SOL_SOCKET , SO_PEERCRED , & cr , & crl ) ;
if ( ret = = 0 ) {
* peer_pid = cr . pid ;
} else {
* peer_pid = - 1 ;
}
return ret ;
}
# else /* HAVE_PEERCRED */
# ifdef _AIX_
int ctdb_get_peer_pid ( const int fd , pid_t * peer_pid )
{
struct peercred_struct cr ;
socklen_t crl = sizeof ( struct peercred_struct ) ;
int ret ;
ret = getsockopt ( fd , SOL_SOCKET , SO_PEERID , & cr , & crl ) ;
if ( ret = = 0 ) {
* peer_pid = cr . pid ;
} else {
* peer_pid = - 1 ;
}
return ret ;
}
# else /* _AIX_ */
int ctdb_get_peer_pid ( const int fd , pid_t * peer_pid )
{
/* Not implemented */
* peer_pid = - 1 ;
return ENOSYS ;
}
# endif /* _AIX_ */
# endif /* HAVE_PEERCRED */