]> sourceware.org Git - lvm2.git/blame - daemons/common/daemon-client.c
Add tentative documentation of pvscan --lvmetad to the pvscan manpage.
[lvm2.git] / daemons / common / daemon-client.c
CommitLineData
92658f56
PR
1#include "daemon-client.h"
2#include "daemon-shared.h"
3#include <sys/un.h>
4#include <sys/socket.h>
5#include <string.h>
6#include <stdio.h>
6e4e3082 7#include <unistd.h>
92658f56 8#include <assert.h>
30896326 9#include <errno.h> // ENOMEM
92658f56
PR
10
11daemon_handle daemon_open(daemon_info i) {
3f694b12 12 daemon_handle h = { .protocol_version = 0 };
dc5ba5c3 13 daemon_reply r = { .cft = NULL };
92658f56 14 struct sockaddr_un sockaddr;
6d404585 15
30896326 16 if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0) {
92658f56
PR
17 perror("socket");
18 goto error;
19 }
20 memset(&sockaddr, 0, sizeof(sockaddr));
55e30071 21 fprintf(stderr, "[C] connecting to %s\n", i.socket);
92658f56
PR
22 strcpy(sockaddr.sun_path, i.socket);
23 sockaddr.sun_family = AF_UNIX;
24 if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr))) {
25 perror("connect");
26 goto error;
27 }
3f694b12 28
dc5ba5c3 29 r = daemon_send_simple(h, "hello", NULL);
3f694b12
PR
30 if (r.error || strcmp(daemon_reply_str(r, "response", "unknown"), "OK"))
31 goto error;
32
33 h.protocol = daemon_reply_str(r, "protocol", NULL);
34 if (h.protocol)
35 h.protocol = dm_strdup(h.protocol); /* keep around */
36 h.protocol_version = daemon_reply_int(r, "version", 0);
37
38 if (i.protocol && (!h.protocol || strcmp(h.protocol, i.protocol)))
39 goto error;
40 if (i.protocol_version && h.protocol_version != i.protocol_version)
41 goto error;
42
43 daemon_reply_destroy(r);
92658f56
PR
44 return h;
45error:
46 if (h.socket_fd >= 0)
47 close(h.socket_fd);
3f694b12
PR
48 if (r.cft)
49 daemon_reply_destroy(r);
92658f56
PR
50 h.socket_fd = -1;
51 return h;
52}
53
54daemon_reply daemon_send(daemon_handle h, daemon_request rq)
55{
351aefc8 56 daemon_reply reply = { .cft = NULL, .error = 0 };
92658f56
PR
57 assert(h.socket_fd >= 0);
58
59 if (!rq.buffer) {
60 /* TODO: build the buffer from rq.cft */
61 }
62
63 assert(rq.buffer);
64 write_buffer(h.socket_fd, rq.buffer, strlen(rq.buffer));
3f694b12 65 dm_free(rq.buffer);
92658f56
PR
66
67 if (read_buffer(h.socket_fd, &reply.buffer)) {
c033ea01 68 reply.cft = dm_config_from_string(reply.buffer);
92658f56
PR
69 } else
70 reply.error = 1;
71
72 return reply;
73}
74
aaca7f11
PR
75void daemon_reply_destroy(daemon_reply r) {
76 if (r.cft)
6e4e3082 77 dm_config_destroy(r.cft);
3f694b12 78 dm_free(r.buffer);
aaca7f11
PR
79}
80
d528658f 81daemon_reply daemon_send_simple(daemon_handle h, const char *id, ...)
30896326 82{
6d404585 83 static const daemon_reply err = { .error = ENOMEM, .buffer = NULL, .cft = NULL };
ade2b934 84 daemon_request rq = { .cft = NULL };
6d404585 85 daemon_reply repl;
30896326 86 va_list ap;
6d404585 87
30896326 88 va_start(ap, id);
6d404585
ZK
89 rq.buffer = format_buffer("request", id, ap);
90 va_end(ap);
30896326 91
6d404585 92 if (!rq.buffer)
30896326 93 return err;
30896326 94
6d404585 95 repl = daemon_send(h, rq);
30896326
PR
96 return repl;
97}
98
99void daemon_close(daemon_handle h)
100{
3f694b12 101 dm_free((char *)h.protocol);
92658f56 102}
This page took 0.045557 seconds and 5 git commands to generate.