2010-05-21 12:07:41 +09:30
/*
synchronous wrappers for libctdb
Copyright ( C ) Rusty Russell 2010
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/>.
*/
# include <ctdb.h>
# include <stdbool.h>
# include <poll.h>
# include <errno.h>
# include <stdlib.h>
2010-06-04 16:54:08 +09:30
# include "libctdb_private.h"
2010-05-21 12:07:41 +09:30
2010-08-09 15:41:32 +09:30
/* Remove type-safety macros. */
# undef ctdb_set_message_handler
2010-05-24 13:17:36 +09:30
/* On failure, frees req and returns NULL. */
2010-06-04 16:54:08 +09:30
static struct ctdb_request * synchronous ( struct ctdb_connection * ctdb ,
struct ctdb_request * req ,
bool * done )
2010-05-21 12:07:41 +09:30
{
struct pollfd fds ;
2010-05-24 13:17:36 +09:30
/* Pass through allocation failures. */
if ( ! req )
return NULL ;
2010-05-21 12:07:41 +09:30
fds . fd = ctdb_get_fd ( ctdb ) ;
2010-05-24 13:17:36 +09:30
while ( ! * done ) {
fds . events = ctdb_which_events ( ctdb ) ;
if ( poll ( & fds , 1 , - 1 ) < 0 ) {
/* Signalled is OK, other error is bad. */
if ( errno = = EINTR )
continue ;
2010-06-18 15:15:27 +09:30
ctdb_cancel ( ctdb , req ) ;
2010-06-04 20:27:06 +09:30
DEBUG ( ctdb , LOG_ERR , " ctdb_synchronous: poll failed " ) ;
2010-05-24 13:17:36 +09:30
return NULL ;
}
2010-06-18 15:13:54 +09:30
if ( ! ctdb_service ( ctdb , fds . revents ) ) {
2010-06-18 15:15:27 +09:30
/* It can have failed after it completed request. */
if ( ! * done )
ctdb_cancel ( ctdb , req ) ;
else
2011-08-19 17:05:36 +02:00
ctdb_request_free ( req ) ;
2010-05-24 13:17:36 +09:30
return NULL ;
}
2010-05-21 12:07:41 +09:30
}
2010-05-24 13:17:36 +09:30
return req ;
2010-05-21 12:07:41 +09:30
}
2010-05-24 13:17:36 +09:30
static void set ( struct ctdb_connection * ctdb ,
struct ctdb_request * req , bool * done )
2010-05-21 12:07:41 +09:30
{
2010-05-24 13:17:36 +09:30
* done = true ;
2010-05-21 12:07:41 +09:30
}
2010-06-04 20:19:25 +09:30
bool ctdb_getrecmaster ( struct ctdb_connection * ctdb ,
uint32_t destnode , uint32_t * recmaster )
2010-05-21 12:07:41 +09:30
{
struct ctdb_request * req ;
2010-05-24 13:17:36 +09:30
bool done = false ;
2010-06-04 20:19:25 +09:30
bool ret = false ;
2010-05-24 13:17:36 +09:30
2010-06-04 16:54:08 +09:30
req = synchronous ( ctdb ,
ctdb_getrecmaster_send ( ctdb , destnode , set , & done ) ,
& done ) ;
2010-05-24 13:17:36 +09:30
if ( req ! = NULL ) {
2010-06-04 16:54:08 +09:30
ret = ctdb_getrecmaster_recv ( ctdb , req , recmaster ) ;
2011-08-19 17:05:36 +02:00
ctdb_request_free ( req ) ;
2010-05-21 12:07:41 +09:30
}
2010-05-24 13:17:36 +09:30
return ret ;
2010-05-21 12:07:41 +09:30
}
2011-08-23 15:00:27 +10:00
bool ctdb_getrecmode ( struct ctdb_connection * ctdb ,
uint32_t destnode , uint32_t * recmode )
{
struct ctdb_request * req ;
bool done = false ;
bool ret = false ;
req = synchronous ( ctdb ,
ctdb_getrecmode_send ( ctdb , destnode , set , & done ) ,
& done ) ;
if ( req ! = NULL ) {
ret = ctdb_getrecmode_recv ( ctdb , req , recmode ) ;
ctdb_request_free ( req ) ;
}
return ret ;
}
2010-05-21 12:07:41 +09:30
struct ctdb_db * ctdb_attachdb ( struct ctdb_connection * ctdb ,
2010-06-08 17:11:40 +09:30
const char * name , bool persistent ,
2010-05-21 12:07:41 +09:30
uint32_t tdb_flags )
{
struct ctdb_request * req ;
2010-05-24 13:17:36 +09:30
bool done = false ;
struct ctdb_db * ret = NULL ;
2010-06-04 16:54:08 +09:30
req = synchronous ( ctdb ,
ctdb_attachdb_send ( ctdb , name , persistent , tdb_flags ,
set , & done ) ,
& done ) ;
2010-05-24 13:17:36 +09:30
if ( req ! = NULL ) {
2010-06-04 16:54:08 +09:30
ret = ctdb_attachdb_recv ( ctdb , req ) ;
2011-08-19 17:05:36 +02:00
ctdb_request_free ( req ) ;
2010-05-21 12:07:41 +09:30
}
2010-05-24 13:17:36 +09:30
return ret ;
2010-05-21 12:07:41 +09:30
}
2010-06-02 10:36:19 +10:00
2010-06-04 20:19:25 +09:30
bool ctdb_getpnn ( struct ctdb_connection * ctdb ,
uint32_t destnode , uint32_t * pnn )
2010-06-02 10:36:19 +10:00
{
struct ctdb_request * req ;
bool done = false ;
2010-06-04 20:19:25 +09:30
bool ret = false ;
2010-06-02 10:36:19 +10:00
2010-06-04 16:54:08 +09:30
req = synchronous ( ctdb ,
ctdb_getpnn_send ( ctdb , destnode , set , & done ) ,
& done ) ;
2010-06-02 10:36:19 +10:00
if ( req ! = NULL ) {
2010-06-04 16:54:08 +09:30
ret = ctdb_getpnn_recv ( ctdb , req , pnn ) ;
2011-08-19 17:05:36 +02:00
ctdb_request_free ( req ) ;
2010-06-02 10:36:19 +10:00
}
return ret ;
}
2010-06-21 14:47:34 +09:30
2011-11-30 10:00:07 +11:00
bool ctdb_check_message_handlers ( struct ctdb_connection * ctdb ,
uint32_t destnode , uint32_t num ,
uint64_t * mhs , uint8_t * result )
{
struct ctdb_request * req ;
bool done = false ;
bool ret = false ;
req = synchronous ( ctdb ,
ctdb_check_message_handlers_send ( ctdb , destnode , num , mhs , set , & done ) ,
& done ) ;
if ( req ! = NULL ) {
ret = ctdb_check_message_handlers_recv ( ctdb , req , num , result ) ;
ctdb_request_free ( req ) ;
}
return ret ;
}
2010-09-13 14:28:11 +10:00
bool ctdb_getnodemap ( struct ctdb_connection * ctdb ,
uint32_t destnode , struct ctdb_node_map * * nodemap )
{
struct ctdb_request * req ;
bool done = false ;
bool ret = false ;
* nodemap = NULL ;
req = synchronous ( ctdb ,
ctdb_getnodemap_send ( ctdb , destnode , set , & done ) ,
& done ) ;
if ( req ! = NULL ) {
ret = ctdb_getnodemap_recv ( ctdb , req , nodemap ) ;
2011-08-19 17:05:36 +02:00
ctdb_request_free ( req ) ;
2010-09-13 14:28:11 +10:00
}
return ret ;
}
2010-09-15 14:56:57 +10:00
bool ctdb_getpublicips ( struct ctdb_connection * ctdb ,
uint32_t destnode , struct ctdb_all_public_ips * * ips )
{
struct ctdb_request * req ;
bool done = false ;
bool ret = false ;
* ips = NULL ;
req = synchronous ( ctdb ,
ctdb_getpublicips_send ( ctdb , destnode , set , & done ) ,
& done ) ;
if ( req ! = NULL ) {
ret = ctdb_getpublicips_recv ( ctdb , req , ips ) ;
2011-08-19 17:05:36 +02:00
ctdb_request_free ( req ) ;
2010-09-15 14:56:57 +10:00
}
return ret ;
}
2010-08-09 15:41:32 +09:30
bool ctdb_set_message_handler ( struct ctdb_connection * ctdb , uint64_t srvid ,
ctdb_message_fn_t handler , void * cbdata )
{
struct ctdb_request * req ;
bool done = false ;
bool ret = false ;
req = synchronous ( ctdb ,
ctdb_set_message_handler_send ( ctdb , srvid , handler ,
cbdata , set , & done ) ,
& done ) ;
if ( req ! = NULL ) {
ret = ctdb_set_message_handler_recv ( ctdb , req ) ;
2011-08-19 17:05:36 +02:00
ctdb_request_free ( req ) ;
2010-08-09 15:41:32 +09:30
}
return ret ;
}
2010-06-21 14:47:34 +09:30
struct rrl_info {
bool done ;
struct ctdb_lock * lock ;
TDB_DATA * data ;
} ;
static void rrl_callback ( struct ctdb_db * ctdb_db ,
struct ctdb_lock * lock ,
TDB_DATA data ,
struct rrl_info * rrl )
{
rrl - > done = true ;
rrl - > lock = lock ;
* rrl - > data = data ;
}
struct ctdb_lock * ctdb_readrecordlock ( struct ctdb_connection * ctdb ,
struct ctdb_db * ctdb_db , TDB_DATA key ,
TDB_DATA * data )
{
struct pollfd fds ;
struct rrl_info rrl ;
rrl . done = false ;
rrl . lock = NULL ;
rrl . data = data ;
/* Immediate failure is easy. */
if ( ! ctdb_readrecordlock_async ( ctdb_db , key , rrl_callback , & rrl ) )
return NULL ;
/* Immediate success is easy. */
if ( ! rrl . done ) {
/* Otherwise wait until callback called. */
fds . fd = ctdb_get_fd ( ctdb ) ;
while ( ! rrl . done ) {
fds . events = ctdb_which_events ( ctdb ) ;
if ( poll ( & fds , 1 , - 1 ) < 0 ) {
/* Signalled is OK, other error is bad. */
if ( errno = = EINTR )
continue ;
DEBUG ( ctdb , LOG_ERR ,
" ctdb_readrecordlock: poll failed " ) ;
return NULL ;
}
if ( ! ctdb_service ( ctdb , fds . revents ) ) {
break ;
}
}
}
return rrl . lock ;
}
2011-11-28 16:30:46 +11:00
bool ctdb_getdbseqnum ( struct ctdb_connection * ctdb ,
uint32_t destnode , uint32_t dbid ,
uint64_t * seqnum )
{
struct ctdb_request * req ;
bool done = false ;
bool ret = false ;
req = synchronous ( ctdb ,
ctdb_getdbseqnum_send ( ctdb , destnode , dbid , set , & done ) ,
& done ) ;
if ( req ! = NULL ) {
ret = ctdb_getdbseqnum_recv ( ctdb , req , seqnum ) ;
ctdb_request_free ( req ) ;
}
return ret ;
}