]>
Commit | Line | Data |
---|---|---|
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 | |
11 | daemon_handle daemon_open(daemon_info i) { | |
6d404585 | 12 | daemon_handle h = { .protocol = 0 }; |
92658f56 | 13 | struct sockaddr_un sockaddr; |
6d404585 | 14 | |
30896326 | 15 | if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0) { |
92658f56 PR |
16 | perror("socket"); |
17 | goto error; | |
18 | } | |
19 | memset(&sockaddr, 0, sizeof(sockaddr)); | |
55e30071 | 20 | fprintf(stderr, "[C] connecting to %s\n", i.socket); |
92658f56 PR |
21 | strcpy(sockaddr.sun_path, i.socket); |
22 | sockaddr.sun_family = AF_UNIX; | |
23 | if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr))) { | |
24 | perror("connect"); | |
25 | goto error; | |
26 | } | |
92658f56 PR |
27 | return h; |
28 | error: | |
29 | if (h.socket_fd >= 0) | |
30 | close(h.socket_fd); | |
31 | h.socket_fd = -1; | |
32 | return h; | |
33 | } | |
34 | ||
35 | daemon_reply daemon_send(daemon_handle h, daemon_request rq) | |
36 | { | |
351aefc8 | 37 | daemon_reply reply = { .cft = NULL, .error = 0 }; |
92658f56 PR |
38 | assert(h.socket_fd >= 0); |
39 | ||
40 | if (!rq.buffer) { | |
41 | /* TODO: build the buffer from rq.cft */ | |
42 | } | |
43 | ||
44 | assert(rq.buffer); | |
45 | write_buffer(h.socket_fd, rq.buffer, strlen(rq.buffer)); | |
46 | ||
47 | if (read_buffer(h.socket_fd, &reply.buffer)) { | |
c033ea01 | 48 | reply.cft = dm_config_from_string(reply.buffer); |
92658f56 PR |
49 | } else |
50 | reply.error = 1; | |
51 | ||
52 | return reply; | |
53 | } | |
54 | ||
aaca7f11 PR |
55 | void daemon_reply_destroy(daemon_reply r) { |
56 | if (r.cft) | |
6e4e3082 | 57 | dm_config_destroy(r.cft); |
aaca7f11 PR |
58 | } |
59 | ||
d528658f | 60 | daemon_reply daemon_send_simple(daemon_handle h, const char *id, ...) |
30896326 | 61 | { |
6d404585 | 62 | static const daemon_reply err = { .error = ENOMEM, .buffer = NULL, .cft = NULL }; |
ade2b934 | 63 | daemon_request rq = { .cft = NULL }; |
6d404585 | 64 | daemon_reply repl; |
30896326 | 65 | va_list ap; |
6d404585 | 66 | |
30896326 | 67 | va_start(ap, id); |
6d404585 ZK |
68 | rq.buffer = format_buffer("request", id, ap); |
69 | va_end(ap); | |
30896326 | 70 | |
6d404585 | 71 | if (!rq.buffer) |
30896326 | 72 | return err; |
30896326 | 73 | |
6d404585 | 74 | repl = daemon_send(h, rq); |
30896326 PR |
75 | dm_free(rq.buffer); |
76 | return repl; | |
77 | } | |
78 | ||
79 | void daemon_close(daemon_handle h) | |
80 | { | |
92658f56 | 81 | } |