2011-06-14 06:34:18 +04:00
# include "daemon-client.h"
# include "daemon-shared.h"
# include <sys/un.h>
# include <sys/socket.h>
# include <string.h>
# include <stdio.h>
2011-08-31 16:18:40 +04:00
# include <unistd.h>
2011-06-14 06:34:18 +04:00
# include <assert.h>
2011-06-27 17:14:53 +04:00
# include <errno.h> // ENOMEM
2011-06-14 06:34:18 +04:00
daemon_handle daemon_open ( daemon_info i ) {
daemon_handle h ;
struct sockaddr_un sockaddr ;
2011-06-27 17:14:53 +04:00
if ( ( h . socket_fd = socket ( PF_UNIX , SOCK_STREAM /* | SOCK_NONBLOCK */ , 0 ) ) < 0 ) {
2011-06-14 06:34:18 +04:00
perror ( " socket " ) ;
goto error ;
}
memset ( & sockaddr , 0 , sizeof ( sockaddr ) ) ;
2011-06-27 17:46:45 +04:00
fprintf ( stderr , " [C] connecting to %s \n " , i . socket ) ;
2011-06-14 06:34:18 +04:00
strcpy ( sockaddr . sun_path , i . socket ) ;
sockaddr . sun_family = AF_UNIX ;
if ( connect ( h . socket_fd , ( struct sockaddr * ) & sockaddr , sizeof ( sockaddr ) ) ) {
perror ( " connect " ) ;
goto error ;
}
h . protocol = 0 ;
return h ;
error :
if ( h . socket_fd > = 0 )
close ( h . socket_fd ) ;
h . socket_fd = - 1 ;
return h ;
}
daemon_reply daemon_send ( daemon_handle h , daemon_request rq )
{
daemon_reply reply ;
assert ( h . socket_fd > = 0 ) ;
if ( ! rq . buffer ) {
/* TODO: build the buffer from rq.cft */
}
assert ( rq . buffer ) ;
write_buffer ( h . socket_fd , rq . buffer , strlen ( rq . buffer ) ) ;
if ( read_buffer ( h . socket_fd , & reply . buffer ) ) {
2011-08-30 19:42:56 +04:00
reply . cft = dm_config_from_string ( reply . buffer ) ;
2011-06-14 06:34:18 +04:00
} else
reply . error = 1 ;
return reply ;
}
2011-06-27 17:58:11 +04:00
void daemon_reply_destroy ( daemon_reply r ) {
if ( r . cft )
2011-08-31 16:18:40 +04:00
dm_config_destroy ( r . cft ) ;
2011-06-27 17:58:11 +04:00
}
2011-06-27 17:14:53 +04:00
daemon_reply daemon_send_simple ( daemon_handle h , char * id , . . . )
{
va_list ap ;
va_start ( ap , id ) ;
2011-06-30 02:20:14 +04:00
daemon_request rq = { . buffer = format_buffer ( " request " , id , ap ) } ;
2011-06-27 17:14:53 +04:00
if ( ! rq . buffer ) {
daemon_reply err = { . error = ENOMEM , . buffer = NULL , . cft = NULL } ;
return err ;
}
daemon_reply repl = daemon_send ( h , rq ) ;
dm_free ( rq . buffer ) ;
return repl ;
}
void daemon_close ( daemon_handle h )
{
2011-06-14 06:34:18 +04:00
}