]> sourceware.org Git - lvm2.git/blame - libdaemon/client/daemon-client.c
remove old makefile
[lvm2.git] / libdaemon / client / 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) {
98b4241b 12 daemon_handle h = { .protocol_version = 0, .error = 0 };
dc5ba5c3 13 daemon_reply r = { .cft = NULL };
92658f56 14 struct sockaddr_un sockaddr;
6d404585 15
98b4241b 16 if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0)
92658f56 17 goto error;
98b4241b 18
92658f56 19 memset(&sockaddr, 0, sizeof(sockaddr));
92658f56
PR
20 strcpy(sockaddr.sun_path, i.socket);
21 sockaddr.sun_family = AF_UNIX;
98b4241b 22 if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr)))
92658f56 23 goto error;
3f694b12 24
dc5ba5c3 25 r = daemon_send_simple(h, "hello", NULL);
3f694b12
PR
26 if (r.error || strcmp(daemon_reply_str(r, "response", "unknown"), "OK"))
27 goto error;
28
29 h.protocol = daemon_reply_str(r, "protocol", NULL);
30 if (h.protocol)
31 h.protocol = dm_strdup(h.protocol); /* keep around */
32 h.protocol_version = daemon_reply_int(r, "version", 0);
33
34 if (i.protocol && (!h.protocol || strcmp(h.protocol, i.protocol)))
35 goto error;
36 if (i.protocol_version && h.protocol_version != i.protocol_version)
37 goto error;
38
39 daemon_reply_destroy(r);
92658f56 40 return h;
98b4241b 41
92658f56 42error:
98b4241b 43 h.error = errno;
92658f56
PR
44 if (h.socket_fd >= 0)
45 close(h.socket_fd);
3f694b12
PR
46 if (r.cft)
47 daemon_reply_destroy(r);
92658f56
PR
48 h.socket_fd = -1;
49 return h;
50}
51
52daemon_reply daemon_send(daemon_handle h, daemon_request rq)
53{
351aefc8 54 daemon_reply reply = { .cft = NULL, .error = 0 };
92658f56
PR
55 assert(h.socket_fd >= 0);
56
57 if (!rq.buffer) {
58 /* TODO: build the buffer from rq.cft */
59 }
60
61 assert(rq.buffer);
98b4241b
PR
62 if (!write_buffer(h.socket_fd, rq.buffer, strlen(rq.buffer)))
63 reply.error = errno;
64
92658f56 65 if (read_buffer(h.socket_fd, &reply.buffer)) {
c033ea01 66 reply.cft = dm_config_from_string(reply.buffer);
92658f56 67 } else
98b4241b 68 reply.error = errno;
92658f56
PR
69
70 return reply;
71}
72
aaca7f11
PR
73void daemon_reply_destroy(daemon_reply r) {
74 if (r.cft)
6e4e3082 75 dm_config_destroy(r.cft);
3f694b12 76 dm_free(r.buffer);
aaca7f11
PR
77}
78
d528658f 79daemon_reply daemon_send_simple(daemon_handle h, const char *id, ...)
30896326 80{
6d404585 81 static const daemon_reply err = { .error = ENOMEM, .buffer = NULL, .cft = NULL };
ade2b934 82 daemon_request rq = { .cft = NULL };
6d404585 83 daemon_reply repl;
30896326 84 va_list ap;
6d404585 85
30896326 86 va_start(ap, id);
6d404585
ZK
87 rq.buffer = format_buffer("request", id, ap);
88 va_end(ap);
30896326 89
6d404585 90 if (!rq.buffer)
30896326 91 return err;
30896326 92
6d404585 93 repl = daemon_send(h, rq);
5627fc8e
ZK
94 dm_free(rq.buffer);
95
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.042818 seconds and 5 git commands to generate.