2012-02-28 22:35:04 +04:00
/*
* Copyright ( C ) 2011 - 2012 Red Hat , Inc .
*
* 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 Lesser General Public License v .2 .1 .
*
* You should have received a copy of the GNU Lesser 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
2012-02-28 22:35:04 +04:00
*/
2018-05-14 12:30:20 +03:00
# include "tools/tool.h"
2015-07-06 19:30:18 +03:00
2018-12-03 20:14:26 +03:00
# include "daemon-io.h"
# include "daemon-client.h"
2018-05-14 14:16:43 +03:00
# include "device_mapper/misc/dm-logging.h"
2012-02-28 22:35:04 +04:00
2011-06-14 06:34:18 +04:00
# include <sys/un.h>
# include <sys/socket.h>
2013-01-05 03:29:59 +04:00
daemon_handle daemon_open ( daemon_info i )
{
2016-03-07 12:20:35 +03:00
daemon_handle h = { . error = 0 } ;
2012-12-14 19:38:07 +04:00
daemon_reply r = { 0 } ;
2012-06-21 23:19:28 +04:00
struct sockaddr_un sockaddr = { . sun_family = AF_UNIX } ;
2011-09-17 18:49:18 +04:00
2013-01-05 03:29:59 +04:00
log_debug ( " %s: Opening daemon socket to %s for protocol %s version %d. " ,
i . socket , i . path , i . protocol , i . protocol_version ) ;
if ( ( h . socket_fd = socket ( PF_UNIX , SOCK_STREAM /* | SOCK_NONBLOCK */ , 0 ) ) < 0 ) {
h . error = errno ;
log_sys_error ( " socket " , i . socket ) ;
2011-06-14 06:34:18 +04:00
goto error ;
2013-01-05 03:29:59 +04:00
}
2012-02-26 12:46:28 +04:00
2024-04-04 19:42:24 +03:00
if ( ! _dm_strncpy ( sockaddr . sun_path , i . socket , sizeof ( sockaddr . sun_path ) ) ) {
2013-01-05 03:29:59 +04:00
log_error ( " %s: Daemon socket path too long. " , i . socket ) ;
2012-04-27 13:52:33 +04:00
goto error ;
}
2012-06-21 23:19:28 +04:00
2013-01-05 03:29:59 +04:00
if ( connect ( h . socket_fd , ( struct sockaddr * ) & sockaddr , sizeof ( sockaddr ) ) ) {
h . error = errno ;
log_sys_error ( " connect " , i . socket ) ;
2011-06-14 06:34:18 +04:00
goto error ;
2013-01-05 03:29:59 +04:00
}
2012-02-24 03:52:11 +04:00
2013-01-05 03:29:59 +04:00
log_debug ( " Sending daemon %s: hello " , i . path ) ;
2012-02-24 04:02:54 +04:00
r = daemon_send_simple ( h , " hello " , NULL ) ;
2013-01-05 03:29:59 +04:00
if ( r . error | | strcmp ( daemon_reply_str ( r , " response " , " unknown " ) , " OK " ) ) {
h . error = r . error ;
log_error ( " Daemon %s returned error %d " , i . path , r . error ) ;
2012-02-24 03:52:11 +04:00
goto error ;
2013-01-05 03:29:59 +04:00
}
2012-02-24 03:52:11 +04:00
2013-01-05 03:29:59 +04:00
/* Check protocol and version matches */
2012-02-24 03:52:11 +04:00
h . protocol = daemon_reply_str ( r , " protocol " , NULL ) ;
if ( h . protocol )
2018-06-08 15:40:53 +03:00
h . protocol = strdup ( h . protocol ) ; /* keep around */
2012-02-24 03:52:11 +04:00
h . protocol_version = daemon_reply_int ( r , " version " , 0 ) ;
2013-01-05 03:29:59 +04:00
if ( i . protocol & & ( ! h . protocol | | strcmp ( h . protocol , i . protocol ) ) ) {
log_error ( " Daemon %s: requested protocol %s != %s " ,
i . path , i . protocol , h . protocol ? : " " ) ;
2012-02-24 03:52:11 +04:00
goto error ;
2013-01-05 03:29:59 +04:00
}
if ( i . protocol_version & & h . protocol_version ! = i . protocol_version ) {
log_error ( " Daemon %s: requested protocol version %d != %d " ,
i . path , i . protocol_version , h . protocol_version ) ;
2012-02-24 03:52:11 +04:00
goto error ;
2013-01-05 03:29:59 +04:00
}
2012-02-24 03:52:11 +04:00
daemon_reply_destroy ( r ) ;
2011-06-14 06:34:18 +04:00
return h ;
2012-02-26 12:46:28 +04:00
2011-06-14 06:34:18 +04:00
error :
2014-11-29 00:31:51 +03:00
if ( h . socket_fd > = 0 & & close ( h . socket_fd ) )
log_sys_error ( " close " , " daemon_open " ) ;
h . socket_fd = - 1 ;
2012-02-24 03:52:11 +04:00
if ( r . cft )
daemon_reply_destroy ( r ) ;
2014-11-29 00:31:51 +03:00
2018-06-08 15:40:53 +03:00
free ( ( char * ) h . protocol ) ;
2014-11-29 00:31:51 +03:00
h . protocol = NULL ;
2011-06-14 06:34:18 +04:00
return h ;
}
daemon_reply daemon_send ( daemon_handle h , daemon_request rq )
{
2012-10-12 12:15:30 +04:00
struct buffer buffer ;
2012-12-14 19:38:07 +04:00
daemon_reply reply = { 0 } ;
2015-03-05 23:00:44 +03:00
if ( h . socket_fd < 0 ) {
log_error ( INTERNAL_ERROR " Daemon send: socket fd cannot be negative %d " , h . socket_fd ) ;
reply . error = EINVAL ;
return reply ;
}
2012-10-12 12:15:30 +04:00
buffer = rq . buffer ;
2011-06-14 06:34:18 +04:00
2012-10-11 16:17:17 +04:00
if ( ! buffer . mem )
2012-12-15 00:36:27 +04:00
if ( ! dm_config_write_node ( rq . cft - > root , buffer_line , & buffer ) ) {
reply . error = ENOMEM ;
return reply ;
}
2011-06-14 06:34:18 +04:00
2015-03-05 23:00:44 +03:00
if ( ! buffer . mem ) {
log_error ( INTERNAL_ERROR " Daemon send: no memory available " ) ;
reply . error = ENOMEM ;
return reply ;
}
2012-10-11 16:17:17 +04:00
if ( ! buffer_write ( h . socket_fd , & buffer ) )
2012-02-26 12:46:28 +04:00
reply . error = errno ;
2012-10-11 16:17:17 +04:00
if ( buffer_read ( h . socket_fd , & reply . buffer ) ) {
2016-09-21 15:59:14 +03:00
reply . cft = config_tree_from_string_without_dup_node_check ( reply . buffer . mem ) ;
2012-09-20 01:30:16 +04:00
if ( ! reply . cft )
reply . error = EPROTO ;
2011-06-14 06:34:18 +04:00
} else
2012-02-26 12:46:28 +04:00
reply . error = errno ;
2011-06-14 06:34:18 +04:00
2012-10-11 16:17:17 +04:00
if ( buffer . mem ! = rq . buffer . mem )
buffer_destroy ( & buffer ) ;
2012-10-08 20:35:30 +04:00
2011-06-14 06:34:18 +04:00
return reply ;
}
2014-11-29 00:31:51 +03:00
void daemon_reply_destroy ( daemon_reply r )
{
2011-06-27 17:58:11 +04:00
if ( r . cft )
2011-08-31 16:18:40 +04:00
dm_config_destroy ( r . cft ) ;
2012-10-11 16:17:17 +04:00
buffer_destroy ( & r . buffer ) ;
2011-06-27 17:58:11 +04:00
}
2012-08-11 12:33:53 +04:00
daemon_reply daemon_send_simple_v ( daemon_handle h , const char * id , va_list ap )
2011-06-27 17:14:53 +04:00
{
2012-12-14 19:38:07 +04:00
static const daemon_reply err = { . error = ENOMEM } ;
2011-10-31 01:58:59 +04:00
daemon_request rq = { . cft = NULL } ;
2011-09-17 18:49:18 +04:00
daemon_reply repl ;
2014-09-15 17:33:56 +04:00
va_list apc ;
2011-09-17 18:49:18 +04:00
2014-09-15 17:33:56 +04:00
va_copy ( apc , ap ) ;
2012-10-13 21:19:50 +04:00
if ( ! buffer_append_f ( & rq . buffer , " request = %s " , id , NULL ) | |
2014-09-15 17:33:56 +04:00
! buffer_append_vf ( & rq . buffer , apc ) ) {
va_end ( apc ) ;
2012-10-13 21:19:50 +04:00
buffer_destroy ( & rq . buffer ) ;
2011-06-27 17:14:53 +04:00
return err ;
2012-10-13 21:19:50 +04:00
}
2014-09-15 17:33:56 +04:00
va_end ( apc ) ;
2011-06-27 17:14:53 +04:00
2011-09-17 18:49:18 +04:00
repl = daemon_send ( h , rq ) ;
2012-10-11 16:17:17 +04:00
buffer_destroy ( & rq . buffer ) ;
2012-02-27 15:49:16 +04:00
2011-06-27 17:14:53 +04:00
return repl ;
}
2012-08-11 12:33:53 +04:00
daemon_reply daemon_send_simple ( daemon_handle h , const char * id , . . . )
{
2012-10-12 12:15:30 +04:00
daemon_reply r ;
2012-08-11 12:33:53 +04:00
va_list ap ;
2012-10-12 12:15:30 +04:00
2012-08-11 12:33:53 +04:00
va_start ( ap , id ) ;
2012-10-12 12:15:30 +04:00
r = daemon_send_simple_v ( h , id , ap ) ;
2012-08-11 12:33:53 +04:00
va_end ( ap ) ;
2012-10-12 12:15:30 +04:00
2012-08-11 12:33:53 +04:00
return r ;
}
2011-06-27 17:14:53 +04:00
void daemon_close ( daemon_handle h )
{
2014-11-29 00:31:51 +03:00
if ( h . socket_fd > = 0 ) {
log_debug ( " Closing daemon socket (fd %d). " , h . socket_fd ) ;
if ( close ( h . socket_fd ) )
log_sys_error ( " close " , " daemon_close " ) ;
}
2018-06-08 15:40:53 +03:00
free ( ( char * ) h . protocol ) ;
2011-06-14 06:34:18 +04:00
}
2012-08-11 12:33:53 +04:00
daemon_request daemon_request_make ( const char * id )
{
daemon_request r ;
2016-03-07 12:20:35 +03:00
2012-10-11 16:17:17 +04:00
buffer_init ( & r . buffer ) ;
2012-08-11 12:33:53 +04:00
if ( ! ( r . cft = dm_config_create ( ) ) )
2016-03-07 12:20:35 +03:00
goto_bad ;
2012-08-11 12:33:53 +04:00
if ( ! ( r . cft - > root = make_text_node ( r . cft , " request " , id , NULL , NULL ) ) )
2016-03-07 12:20:35 +03:00
goto_bad ;
2012-08-11 12:33:53 +04:00
return r ;
bad :
2016-03-07 12:20:35 +03:00
if ( r . cft ) {
2012-08-11 12:33:53 +04:00
dm_config_destroy ( r . cft ) ;
2016-03-07 12:20:35 +03:00
r . cft = NULL ;
}
2012-08-11 12:33:53 +04:00
return r ;
}
int daemon_request_extend_v ( daemon_request r , va_list ap )
{
2014-09-15 17:33:56 +04:00
int res ;
va_list apc ;
2012-08-11 12:33:53 +04:00
if ( ! r . cft )
return 0 ;
2014-09-15 17:33:56 +04:00
va_copy ( apc , ap ) ;
res = config_make_nodes_v ( r . cft , NULL , r . cft - > root , apc ) ? 1 : 0 ;
va_end ( apc ) ;
2012-08-11 12:33:53 +04:00
2014-09-15 17:33:56 +04:00
return res ;
2012-08-11 12:33:53 +04:00
}
int daemon_request_extend ( daemon_request r , . . . )
{
2012-10-12 12:15:30 +04:00
int res ;
2012-08-11 12:33:53 +04:00
va_list ap ;
2012-10-12 12:15:30 +04:00
2012-08-11 12:33:53 +04:00
va_start ( ap , r ) ;
2012-10-12 12:15:30 +04:00
res = daemon_request_extend_v ( r , ap ) ;
2012-08-11 12:33:53 +04:00
va_end ( ap ) ;
2012-10-12 12:15:30 +04:00
2012-08-11 12:33:53 +04:00
return res ;
}
2014-11-29 00:31:51 +03:00
void daemon_request_destroy ( daemon_request r )
{
2012-08-11 12:33:53 +04:00
if ( r . cft )
dm_config_destroy ( r . cft ) ;
2012-10-11 16:17:17 +04:00
buffer_destroy ( & r . buffer ) ;
2012-08-11 12:33:53 +04:00
}