]>
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) { | |
12 | daemon_handle h; | |
13 | struct sockaddr_un sockaddr; | |
30896326 | 14 | if ((h.socket_fd = socket(PF_UNIX, SOCK_STREAM /* | SOCK_NONBLOCK */, 0)) < 0) { |
92658f56 PR |
15 | perror("socket"); |
16 | goto error; | |
17 | } | |
18 | memset(&sockaddr, 0, sizeof(sockaddr)); | |
55e30071 | 19 | fprintf(stderr, "[C] connecting to %s\n", i.socket); |
92658f56 PR |
20 | strcpy(sockaddr.sun_path, i.socket); |
21 | sockaddr.sun_family = AF_UNIX; | |
22 | if (connect(h.socket_fd,(struct sockaddr *) &sockaddr, sizeof(sockaddr))) { | |
23 | perror("connect"); | |
24 | goto error; | |
25 | } | |
26 | h.protocol = 0; | |
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 | { | |
37 | daemon_reply reply; | |
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 | ||
30896326 PR |
60 | daemon_reply daemon_send_simple(daemon_handle h, char *id, ...) |
61 | { | |
62 | va_list ap; | |
63 | va_start(ap, id); | |
14e01287 | 64 | daemon_request rq = { .buffer = format_buffer("request", id, ap) }; |
30896326 PR |
65 | |
66 | if (!rq.buffer) { | |
67 | daemon_reply err = { .error = ENOMEM, .buffer = NULL, .cft = NULL }; | |
68 | return err; | |
69 | } | |
70 | ||
71 | daemon_reply repl = daemon_send(h, rq); | |
72 | dm_free(rq.buffer); | |
73 | return repl; | |
74 | } | |
75 | ||
76 | void daemon_close(daemon_handle h) | |
77 | { | |
92658f56 | 78 | } |