2
* Copyright (C) 2011-2012 Red Hat, Inc.
4
* This file is part of LVM2.
6
* This copyrighted material is made available to anyone wishing to use,
7
* modify, copy, or redistribute it subject to the terms and conditions
8
* of the GNU Lesser General Public License v.2.1.
10
* You should have received a copy of the GNU Lesser General Public License
11
* along with this program; if not, write to the Free Software Foundation,
12
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
#include "daemon-shared.h"
16
#include "daemon-client.h"
19
#include <sys/socket.h>
24
#include <errno.h> // ENOMEM
26
daemon_handle daemon_open(daemon_info i) {
27
daemon_handle h = { .protocol_version = 0, .error = 0 };
28
daemon_reply r = { .cft = NULL };
29
struct sockaddr_un sockaddr;
31
if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0)
34
memset(&sockaddr, 0, sizeof(sockaddr));
35
strcpy(sockaddr.sun_path, i.socket);
36
sockaddr.sun_family = AF_UNIX;
37
if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr)))
40
r = daemon_send_simple(h, "hello", NULL);
41
if (r.error || strcmp(daemon_reply_str(r, "response", "unknown"), "OK"))
44
h.protocol = daemon_reply_str(r, "protocol", NULL);
46
h.protocol = dm_strdup(h.protocol); /* keep around */
47
h.protocol_version = daemon_reply_int(r, "version", 0);
49
if (i.protocol && (!h.protocol || strcmp(h.protocol, i.protocol)))
51
if (i.protocol_version && h.protocol_version != i.protocol_version)
54
daemon_reply_destroy(r);
60
if (close(h.socket_fd))
63
daemon_reply_destroy(r);
68
daemon_reply daemon_send(daemon_handle h, daemon_request rq)
70
daemon_reply reply = { .cft = NULL, .error = 0 };
71
assert(h.socket_fd >= 0);
74
/* TODO: build the buffer from rq.cft */
78
if (!write_buffer(h.socket_fd, rq.buffer, strlen(rq.buffer)))
81
if (read_buffer(h.socket_fd, &reply.buffer)) {
82
reply.cft = dm_config_from_string(reply.buffer);
89
void daemon_reply_destroy(daemon_reply r) {
91
dm_config_destroy(r.cft);
95
daemon_reply daemon_send_simple(daemon_handle h, const char *id, ...)
97
static const daemon_reply err = { .error = ENOMEM, .buffer = NULL, .cft = NULL };
98
daemon_request rq = { .cft = NULL };
103
rq.buffer = format_buffer("request", id, ap);
109
repl = daemon_send(h, rq);
115
void daemon_close(daemon_handle h)
117
dm_free((char *)h.protocol);