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