2010-05-21 06:37:41 +04:00
/*
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 11:24:08 +04:00
# include "libctdb_private.h"
2010-05-21 06:37:41 +04:00
2010-05-24 07:47:36 +04:00
/* On failure, frees req and returns NULL. */
2010-06-04 11:24:08 +04:00
static struct ctdb_request * synchronous ( struct ctdb_connection * ctdb ,
struct ctdb_request * req ,
bool * done )
2010-05-21 06:37:41 +04:00
{
struct pollfd fds ;
2010-05-24 07:47:36 +04:00
/* Pass through allocation failures. */
if ( ! req )
return NULL ;
2010-05-21 06:37:41 +04:00
fds . fd = ctdb_get_fd ( ctdb ) ;
2010-05-24 07:47:36 +04:00
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-04 11:24:08 +04:00
ctdb_request_free ( ctdb , req ) ;
2010-06-04 14:57:06 +04:00
DEBUG ( ctdb , LOG_ERR , " ctdb_synchronous: poll failed " ) ;
2010-05-24 07:47:36 +04:00
return NULL ;
}
if ( ctdb_service ( ctdb , fds . revents ) < 0 ) {
2010-06-04 11:24:08 +04:00
ctdb_request_free ( ctdb , req ) ;
2010-05-24 07:47:36 +04:00
return NULL ;
}
2010-05-21 06:37:41 +04:00
}
2010-05-24 07:47:36 +04:00
return req ;
2010-05-21 06:37:41 +04:00
}
2010-05-24 07:47:36 +04:00
static void set ( struct ctdb_connection * ctdb ,
struct ctdb_request * req , bool * done )
2010-05-21 06:37:41 +04:00
{
2010-05-24 07:47:36 +04:00
* done = true ;
2010-05-21 06:37:41 +04:00
}
int ctdb_getrecmaster ( struct ctdb_connection * ctdb ,
uint32_t destnode , uint32_t * recmaster )
{
struct ctdb_request * req ;
2010-05-24 07:47:36 +04:00
bool done = false ;
int ret = - 1 ;
2010-06-04 11:24:08 +04:00
req = synchronous ( ctdb ,
ctdb_getrecmaster_send ( ctdb , destnode , set , & done ) ,
& done ) ;
2010-05-24 07:47:36 +04:00
if ( req ! = NULL ) {
2010-06-04 11:24:08 +04:00
ret = ctdb_getrecmaster_recv ( ctdb , req , recmaster ) ;
ctdb_request_free ( ctdb , req ) ;
2010-05-21 06:37:41 +04:00
}
2010-05-24 07:47:36 +04:00
return ret ;
2010-05-21 06:37:41 +04:00
}
struct ctdb_db * ctdb_attachdb ( struct ctdb_connection * ctdb ,
const char * name , int persistent ,
uint32_t tdb_flags )
{
struct ctdb_request * req ;
2010-05-24 07:47:36 +04:00
bool done = false ;
struct ctdb_db * ret = NULL ;
2010-06-04 11:24:08 +04:00
req = synchronous ( ctdb ,
ctdb_attachdb_send ( ctdb , name , persistent , tdb_flags ,
set , & done ) ,
& done ) ;
2010-05-24 07:47:36 +04:00
if ( req ! = NULL ) {
2010-06-04 11:24:08 +04:00
ret = ctdb_attachdb_recv ( ctdb , req ) ;
ctdb_request_free ( ctdb , req ) ;
2010-05-21 06:37:41 +04:00
}
2010-05-24 07:47:36 +04:00
return ret ;
2010-05-21 06:37:41 +04:00
}
2010-06-02 04:36:19 +04:00
int ctdb_getpnn ( struct ctdb_connection * ctdb ,
uint32_t destnode , uint32_t * pnn )
{
struct ctdb_request * req ;
bool done = false ;
int ret = - 1 ;
2010-06-04 11:24:08 +04:00
req = synchronous ( ctdb ,
ctdb_getpnn_send ( ctdb , destnode , set , & done ) ,
& done ) ;
2010-06-02 04:36:19 +04:00
if ( req ! = NULL ) {
2010-06-04 11:24:08 +04:00
ret = ctdb_getpnn_recv ( ctdb , req , pnn ) ;
ctdb_request_free ( ctdb , req ) ;
2010-06-02 04:36:19 +04:00
}
return ret ;
}