Basic skeleton code for RPC daemon loop. This is shared by both the guest-side RPC server as well as the host-side one (the advertised RPCs for each by guest/host-specific arrays).
Signed-off-by: Michael Roth <mdr...@linux.vnet.ibm.com> --- virtagent-daemon.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++ virtagent-daemon.h | 20 +++++++++ 2 files changed, 138 insertions(+), 0 deletions(-) create mode 100644 virtagent-daemon.c create mode 100644 virtagent-daemon.h diff --git a/virtagent-daemon.c b/virtagent-daemon.c new file mode 100644 index 0000000..998b025 --- /dev/null +++ b/virtagent-daemon.c @@ -0,0 +1,118 @@ +/* + * virt-agent - host/guest RPC daemon functions + * + * Copyright IBM Corp. 2010 + * + * Authors: + * Adam Litke <agli...@linux.vnet.ibm.com> + * Michael Roth <mdr...@linux.vnet.ibm.com> + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <xmlrpc-c/base.h> +#include <xmlrpc-c/server.h> +#include <xmlrpc-c/server_abyss.h> +#include "qemu_socket.h" +#include "virtagent-daemon.h" +#include "virtagent-common.h" + +static int va_accept(int listen_fd) { + struct sockaddr_in saddr; + struct sockaddr *addr; + socklen_t len; + int fd; + + while (1) { + len = sizeof(saddr); + addr = (struct sockaddr *)&saddr; + fd = qemu_accept(listen_fd, addr, &len); + if (fd < 0 && errno != EINTR) { + LOG("accept() failed"); + break; + } else if (fd >= 0) { + TRACE("accepted connection"); + break; + } + } + return fd; +} + +typedef struct RPCFunction { + xmlrpc_value *(*func)(xmlrpc_env *env, xmlrpc_value *param, void *unused); + const char *func_name; +} RPCFunction; + +static RPCFunction guest_functions[] = { + { NULL, NULL } +}; +static RPCFunction host_functions[] = { + { NULL, NULL } +}; + +static void va_register_functions(xmlrpc_env *env, xmlrpc_registry *registry, + RPCFunction *list) +{ + int i; + for (i = 0; list[i].func != NULL; ++i) { + TRACE("adding func: %s", list[i].func_name); + xmlrpc_registry_add_method(env, registry, NULL, list[i].func_name, + list[i].func, NULL); + } +} + +int va_server_loop(int listen_fd, bool is_host) +{ + xmlrpc_registry *registryP; + xmlrpc_env env; + int ret, fd; + char *rpc_request; + int rpc_request_len; + xmlrpc_mem_block *rpc_response; + RPCFunction *func_list = is_host ? host_functions : guest_functions; + + xmlrpc_env_init(&env); + registryP = xmlrpc_registry_new(&env); + va_register_functions(&env, registryP, func_list); + + while (1) { + TRACE("waiting for connection from RPC client"); + fd = va_accept(listen_fd); + if (fd < 0) { + TRACE("connection error: %s", strerror(errno)); + continue; + } + TRACE("RPC client connected, fetching RPC..."); + ret = va_get_rpc_request(fd, &rpc_request, &rpc_request_len); + if (ret != 0 || rpc_request == NULL) { + LOG("error retrieving rpc request"); + goto out; + } + TRACE("handling RPC request"); + rpc_response = xmlrpc_registry_process_call(&env, registryP, NULL, + rpc_request, + rpc_request_len); + if (rpc_response == NULL) { + LOG("error handling rpc request"); + goto out; + } + + qemu_free(rpc_request); + TRACE("sending RPC response"); + ret = va_send_rpc_response(fd, rpc_response); + if (ret != 0) { + LOG("error sending rpc response"); + goto out; + } + TRACE("RPC completed"); + XMLRPC_MEMBLOCK_FREE(char, rpc_response); +out: + closesocket(fd); + } + + return 0; +} diff --git a/virtagent-daemon.h b/virtagent-daemon.h new file mode 100644 index 0000000..bb197d0 --- /dev/null +++ b/virtagent-daemon.h @@ -0,0 +1,20 @@ +/* + * virt-agent - host/guest RPC daemon functions + * + * Copyright IBM Corp. 2010 + * + * Authors: + * Michael Roth <mdr...@linux.vnet.ibm.com> + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ +#define GUEST_AGENT_SERVICE_ID "virtagent" +#define GUEST_AGENT_PATH "/tmp/virtagent-guest.sock" +#define HOST_AGENT_SERVICE_ID "virtagent-host" +#define HOST_AGENT_PATH "/tmp/virtagent-host.sock" +#define VA_GETFILE_MAX 1 << 30 +#define VA_FILEBUF_LEN 16384 + +int va_server_loop(int listen_fd, bool is_host); -- 1.7.0.4