2013-04-22 09:36:27 +04:00
/*
ctdb lock helper
Copyright ( C ) Amitay Isaacs 2013
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"
2013-04-22 09:36:27 +04:00
# include "system/filesys.h"
2015-10-26 08:50:46 +03:00
# include "system/network.h"
2016-11-29 09:20:45 +03:00
# include "system/wait.h"
2015-10-26 08:50:46 +03:00
# include <talloc.h>
2016-11-29 09:20:45 +03:00
# include <tevent.h>
2016-11-29 04:54:00 +03:00
# include <tdb.h>
2015-10-26 08:50:46 +03:00
2016-11-29 04:55:06 +03:00
# include "lib/util/sys_rw.h"
2016-11-29 09:20:45 +03:00
# include "lib/util/tevent_unix.h"
2016-11-29 04:55:06 +03:00
2016-11-29 04:54:00 +03:00
# include "protocol/protocol.h"
2015-10-26 08:50:46 +03:00
2015-10-23 06:11:53 +03:00
# include "common/system.h"
2013-04-22 09:36:27 +04:00
2016-06-17 11:33:27 +03:00
static bool realtime = true ;
2016-11-29 09:20:45 +03:00
struct lock_state {
struct tdb_context * tdb ;
TDB_DATA key ;
} ;
2016-06-17 11:33:27 +03:00
static void set_priority ( void )
{
const char * ptr ;
ptr = getenv ( " CTDB_NOSETSCHED " ) ;
if ( ptr ! = NULL ) {
realtime = false ;
}
if ( ! realtime ) {
return ;
}
realtime = set_scheduler ( ) ;
if ( ! realtime ) {
fprintf ( stderr ,
2016-11-30 04:22:02 +03:00
" locking: Unable to set real-time scheduler priority \n " ) ;
2016-06-17 11:33:27 +03:00
}
}
static void reset_priority ( void )
{
if ( realtime ) {
reset_scheduler ( ) ;
}
}
2013-04-22 09:36:27 +04:00
static void send_result ( int fd , char result )
{
2014-07-30 15:03:53 +04:00
sys_write ( fd , & result , 1 ) ;
2013-04-22 09:36:27 +04:00
if ( result = = 1 ) {
exit ( 1 ) ;
}
}
2017-02-15 03:56:01 +03:00
static void usage ( const char * progname )
2013-04-22 09:36:27 +04:00
{
fprintf ( stderr , " \n " ) ;
2016-11-30 04:22:02 +03:00
fprintf ( stderr , " Usage: %s <ctdbd-pid> <output-fd> RECORD <db-path> <db-flags> <db-key> \n " , progname ) ;
2016-11-29 09:13:41 +03:00
fprintf ( stderr , " %s <ctdbd-pid> <output-fd> DB <db-path> <db-flags> \n " , progname ) ;
2013-04-22 09:36:27 +04:00
}
2014-08-06 10:36:58 +04:00
static uint8_t * hex_decode_talloc ( TALLOC_CTX * mem_ctx ,
const char * hex_in , size_t * len )
{
int i , num ;
uint8_t * buffer ;
* len = strlen ( hex_in ) / 2 ;
buffer = talloc_array ( mem_ctx , unsigned char , * len ) ;
for ( i = 0 ; i < * len ; i + + ) {
sscanf ( & hex_in [ i * 2 ] , " %02X " , & num ) ;
buffer [ i ] = ( uint8_t ) num ;
}
return buffer ;
}
2013-04-22 09:36:27 +04:00
2016-11-29 09:20:45 +03:00
static int lock_record ( const char * dbpath , const char * dbflags ,
const char * dbkey , struct lock_state * state )
2013-04-22 09:36:27 +04:00
{
2014-12-11 05:16:47 +03:00
int tdb_flags ;
/* No error checking since CTDB always passes sane values */
tdb_flags = strtol ( dbflags , NULL , 0 ) ;
2013-04-22 09:36:27 +04:00
/* Convert hex key to key */
if ( strcmp ( dbkey , " NULL " ) = = 0 ) {
2016-11-29 09:20:45 +03:00
state - > key . dptr = NULL ;
state - > key . dsize = 0 ;
2013-04-22 09:36:27 +04:00
} else {
2016-11-29 09:20:45 +03:00
state - > key . dptr = hex_decode_talloc ( NULL , dbkey ,
& state - > key . dsize ) ;
2013-04-22 09:36:27 +04:00
}
2016-11-29 09:20:45 +03:00
state - > tdb = tdb_open ( dbpath , 0 , tdb_flags , O_RDWR , 0600 ) ;
if ( state - > tdb = = NULL ) {
2016-11-30 04:22:02 +03:00
fprintf ( stderr , " locking: Error opening database %s \n " , dbpath ) ;
2013-04-22 09:36:27 +04:00
return 1 ;
}
2016-06-17 11:33:27 +03:00
set_priority ( ) ;
2016-02-09 03:59:30 +03:00
2016-11-29 09:20:45 +03:00
if ( tdb_chainlock ( state - > tdb , state - > key ) < 0 ) {
2016-11-30 04:22:02 +03:00
fprintf ( stderr , " locking: Error getting record lock (%s) \n " ,
2016-11-29 09:20:45 +03:00
tdb_errorstr ( state - > tdb ) ) ;
2013-04-22 09:36:27 +04:00
return 1 ;
}
2016-06-17 11:33:27 +03:00
reset_priority ( ) ;
2016-02-09 03:59:30 +03:00
2013-04-22 09:36:27 +04:00
return 0 ;
}
2016-11-29 09:20:45 +03:00
static int lock_db ( const char * dbpath , const char * dbflags ,
struct lock_state * state )
2013-04-22 09:36:27 +04:00
{
2014-12-11 05:16:47 +03:00
int tdb_flags ;
/* No error checking since CTDB always passes sane values */
tdb_flags = strtol ( dbflags , NULL , 0 ) ;
2013-04-22 09:36:27 +04:00
2016-11-29 09:20:45 +03:00
state - > tdb = tdb_open ( dbpath , 0 , tdb_flags , O_RDWR , 0600 ) ;
if ( state - > tdb = = NULL ) {
2016-11-30 04:22:02 +03:00
fprintf ( stderr , " locking: Error opening database %s \n " , dbpath ) ;
2013-04-22 09:36:27 +04:00
return 1 ;
}
2016-06-17 11:33:27 +03:00
set_priority ( ) ;
2016-02-09 03:59:30 +03:00
2016-11-29 09:20:45 +03:00
if ( tdb_lockall ( state - > tdb ) < 0 ) {
2016-11-30 04:22:02 +03:00
fprintf ( stderr , " locking: Error getting db lock (%s) \n " ,
2016-11-29 09:20:45 +03:00
tdb_errorstr ( state - > tdb ) ) ;
2013-04-22 09:36:27 +04:00
return 1 ;
}
2016-06-17 11:33:27 +03:00
reset_priority ( ) ;
2016-02-09 03:59:30 +03:00
2013-04-22 09:36:27 +04:00
return 0 ;
}
2016-11-29 09:20:45 +03:00
struct wait_for_parent_state {
struct tevent_context * ev ;
pid_t ppid ;
} ;
static void wait_for_parent_check ( struct tevent_req * subreq ) ;
static struct tevent_req * wait_for_parent_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
pid_t ppid )
{
struct tevent_req * req , * subreq ;
struct wait_for_parent_state * state ;
req = tevent_req_create ( mem_ctx , & state , struct wait_for_parent_state ) ;
if ( req = = NULL ) {
return NULL ;
}
state - > ev = ev ;
state - > ppid = ppid ;
if ( ppid = = 1 ) {
tevent_req_done ( req ) ;
return tevent_req_post ( req , ev ) ;
}
subreq = tevent_wakeup_send ( state , ev ,
tevent_timeval_current_ofs ( 5 , 0 ) ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , wait_for_parent_check , req ) ;
return req ;
}
static void wait_for_parent_check ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct wait_for_parent_state * state = tevent_req_data (
req , struct wait_for_parent_state ) ;
bool status ;
status = tevent_wakeup_recv ( subreq ) ;
TALLOC_FREE ( subreq ) ;
if ( ! status ) {
/* Ignore error */
fprintf ( stderr , " locking: tevent_wakeup_recv() failed \n " ) ;
}
if ( kill ( state - > ppid , 0 ) = = - 1 & & errno = = ESRCH ) {
tevent_req_done ( req ) ;
return ;
}
subreq = tevent_wakeup_send ( state , state - > ev ,
tevent_timeval_current_ofs ( 5 , 0 ) ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
tevent_req_set_callback ( subreq , wait_for_parent_check , req ) ;
}
static bool wait_for_parent_recv ( struct tevent_req * req )
{
if ( tevent_req_is_unix_error ( req , NULL ) ) {
return false ;
}
return true ;
}
static void cleanup ( struct lock_state * state )
{
if ( state - > tdb ! = NULL ) {
if ( state - > key . dsize = = 0 ) {
tdb_unlockall ( state - > tdb ) ;
} else {
tdb_chainunlock ( state - > tdb , state - > key ) ;
}
tdb_close ( state - > tdb ) ;
}
}
static void signal_handler ( struct tevent_context * ev ,
struct tevent_signal * se ,
int signum , int count , void * siginfo ,
void * private_data )
{
struct lock_state * state = ( struct lock_state * ) private_data ;
cleanup ( state ) ;
exit ( 0 ) ;
}
2013-04-22 09:36:27 +04:00
int main ( int argc , char * argv [ ] )
{
2016-11-29 09:20:45 +03:00
struct tevent_context * ev ;
struct tevent_signal * se ;
struct tevent_req * req ;
struct lock_state state = { 0 } ;
2016-11-30 04:22:02 +03:00
int write_fd ;
2013-04-22 09:36:27 +04:00
char result = 0 ;
int ppid ;
const char * lock_type ;
2016-11-29 09:20:45 +03:00
bool status ;
2013-04-22 09:36:27 +04:00
2016-11-17 08:10:51 +03:00
reset_scheduler ( ) ;
2016-11-30 04:22:02 +03:00
if ( argc < 4 ) {
2017-02-15 03:56:01 +03:00
usage ( argv [ 0 ] ) ;
2013-04-22 09:36:27 +04:00
exit ( 1 ) ;
}
2016-11-30 04:22:02 +03:00
ppid = atoi ( argv [ 1 ] ) ;
write_fd = atoi ( argv [ 2 ] ) ;
lock_type = argv [ 3 ] ;
2013-04-22 09:36:27 +04:00
2016-11-29 09:20:45 +03:00
ev = tevent_context_init ( NULL ) ;
if ( ev = = NULL ) {
fprintf ( stderr , " locking: tevent_context_init() failed \n " ) ;
exit ( 1 ) ;
}
se = tevent_add_signal ( ev , ev , SIGTERM , 0 ,
signal_handler , & state ) ;
if ( se = = NULL ) {
fprintf ( stderr , " locking: tevent_add_signal() failed \n " ) ;
talloc_free ( ev ) ;
exit ( 1 ) ;
}
2013-04-22 09:36:27 +04:00
if ( strcmp ( lock_type , " RECORD " ) = = 0 ) {
2016-11-30 04:22:02 +03:00
if ( argc ! = 7 ) {
fprintf ( stderr ,
" locking: Invalid number of arguments (%d) \n " ,
argc ) ;
2017-02-15 03:56:01 +03:00
usage ( argv [ 0 ] ) ;
2013-04-22 09:36:27 +04:00
exit ( 1 ) ;
}
2016-11-29 09:20:45 +03:00
result = lock_record ( argv [ 4 ] , argv [ 5 ] , argv [ 6 ] , & state ) ;
2013-04-22 09:36:27 +04:00
} else if ( strcmp ( lock_type , " DB " ) = = 0 ) {
2016-11-29 09:13:41 +03:00
if ( argc ! = 6 ) {
fprintf ( stderr ,
" locking: Invalid number of arguments (%d) \n " ,
argc ) ;
2017-02-15 03:56:01 +03:00
usage ( argv [ 0 ] ) ;
2016-11-29 09:13:41 +03:00
exit ( 1 ) ;
2013-04-22 09:36:27 +04:00
}
2016-11-29 09:20:45 +03:00
result = lock_db ( argv [ 4 ] , argv [ 5 ] , & state ) ;
2013-04-22 09:36:27 +04:00
} else {
2016-11-30 04:22:02 +03:00
fprintf ( stderr , " locking: Invalid lock-type '%s' \n " , lock_type ) ;
2017-02-15 03:56:01 +03:00
usage ( argv [ 0 ] ) ;
2013-04-22 09:36:27 +04:00
exit ( 1 ) ;
}
send_result ( write_fd , result ) ;
2016-11-29 09:20:45 +03:00
req = wait_for_parent_send ( ev , ev , ppid ) ;
if ( req = = NULL ) {
fprintf ( stderr , " locking: wait_for_parent_send() failed \n " ) ;
cleanup ( & state ) ;
exit ( 1 ) ;
}
tevent_req_poll ( req , ev ) ;
status = wait_for_parent_recv ( req ) ;
if ( ! status ) {
fprintf ( stderr , " locking: wait_for_parent_recv() failed \n " ) ;
}
talloc_free ( ev ) ;
cleanup ( & state ) ;
2013-04-22 09:36:27 +04:00
return 0 ;
}