]> sourceware.org Git - lvm2.git/blob - libdaemon/client/daemon-client.c
Reflect new file locations, include file updates etc.
[lvm2.git] / libdaemon / client / daemon-client.c
1 /*
2 * Copyright (C) 2011-2012 Red Hat, Inc.
3 *
4 * This file is part of LVM2.
5 *
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.
9 *
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
13 */
14
15 #include "daemon-shared.h"
16 #include "daemon-client.h"
17
18 #include <sys/un.h>
19 #include <sys/socket.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <assert.h>
24 #include <errno.h> // ENOMEM
25
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;
30
31 if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0)
32 goto error;
33
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)))
38 goto error;
39
40 r = daemon_send_simple(h, "hello", NULL);
41 if (r.error || strcmp(daemon_reply_str(r, "response", "unknown"), "OK"))
42 goto error;
43
44 h.protocol = daemon_reply_str(r, "protocol", NULL);
45 if (h.protocol)
46 h.protocol = dm_strdup(h.protocol); /* keep around */
47 h.protocol_version = daemon_reply_int(r, "version", 0);
48
49 if (i.protocol && (!h.protocol || strcmp(h.protocol, i.protocol)))
50 goto error;
51 if (i.protocol_version && h.protocol_version != i.protocol_version)
52 goto error;
53
54 daemon_reply_destroy(r);
55 return h;
56
57 error:
58 h.error = errno;
59 if (h.socket_fd >= 0)
60 close(h.socket_fd);
61 if (r.cft)
62 daemon_reply_destroy(r);
63 h.socket_fd = -1;
64 return h;
65 }
66
67 daemon_reply daemon_send(daemon_handle h, daemon_request rq)
68 {
69 daemon_reply reply = { .cft = NULL, .error = 0 };
70 assert(h.socket_fd >= 0);
71
72 if (!rq.buffer) {
73 /* TODO: build the buffer from rq.cft */
74 }
75
76 assert(rq.buffer);
77 if (!write_buffer(h.socket_fd, rq.buffer, strlen(rq.buffer)))
78 reply.error = errno;
79
80 if (read_buffer(h.socket_fd, &reply.buffer)) {
81 reply.cft = dm_config_from_string(reply.buffer);
82 } else
83 reply.error = errno;
84
85 return reply;
86 }
87
88 void daemon_reply_destroy(daemon_reply r) {
89 if (r.cft)
90 dm_config_destroy(r.cft);
91 dm_free(r.buffer);
92 }
93
94 daemon_reply daemon_send_simple(daemon_handle h, const char *id, ...)
95 {
96 static const daemon_reply err = { .error = ENOMEM, .buffer = NULL, .cft = NULL };
97 daemon_request rq = { .cft = NULL };
98 daemon_reply repl;
99 va_list ap;
100
101 va_start(ap, id);
102 rq.buffer = format_buffer("request", id, ap);
103 va_end(ap);
104
105 if (!rq.buffer)
106 return err;
107
108 repl = daemon_send(h, rq);
109 dm_free(rq.buffer);
110
111 return repl;
112 }
113
114 void daemon_close(daemon_handle h)
115 {
116 dm_free((char *)h.protocol);
117 }
This page took 0.039879 seconds and 6 git commands to generate.