diff options
Diffstat (limited to 'common/ipc-client.c')
-rw-r--r-- | common/ipc-client.c | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/common/ipc-client.c b/common/ipc-client.c index 916676a9..c3a9c9a5 100644 --- a/common/ipc-client.c +++ b/common/ipc-client.c | |||
@@ -24,7 +24,7 @@ char *get_socketpath(void) { | |||
24 | return line; | 24 | return line; |
25 | } | 25 | } |
26 | 26 | ||
27 | char *ipc_single_command(const char *socket_path, uint32_t type, const char *payload, uint32_t len) { | 27 | int ipc_open_socket(const char *socket_path) { |
28 | struct sockaddr_un addr; | 28 | struct sockaddr_un addr; |
29 | int socketfd; | 29 | int socketfd; |
30 | if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { | 30 | if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { |
@@ -36,18 +36,21 @@ char *ipc_single_command(const char *socket_path, uint32_t type, const char *pay | |||
36 | if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) { | 36 | if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) { |
37 | sway_abort("Unable to connect to %s", socket_path); | 37 | sway_abort("Unable to connect to %s", socket_path); |
38 | } | 38 | } |
39 | return socketfd; | ||
40 | } | ||
39 | 41 | ||
42 | char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) { | ||
40 | char data[ipc_header_size]; | 43 | char data[ipc_header_size]; |
41 | uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); | 44 | uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); |
42 | memcpy(data, ipc_magic, sizeof(ipc_magic)); | 45 | memcpy(data, ipc_magic, sizeof(ipc_magic)); |
43 | data32[0] = len; | 46 | data32[0] = *len; |
44 | data32[1] = type; | 47 | data32[1] = type; |
45 | 48 | ||
46 | if (write(socketfd, data, ipc_header_size) == -1) { | 49 | if (write(socketfd, data, ipc_header_size) == -1) { |
47 | sway_abort("Unable to send IPC header"); | 50 | sway_abort("Unable to send IPC header"); |
48 | } | 51 | } |
49 | 52 | ||
50 | if (write(socketfd, payload, len) == -1) { | 53 | if (write(socketfd, payload, *len) == -1) { |
51 | sway_abort("Unable to send IPC payload"); | 54 | sway_abort("Unable to send IPC payload"); |
52 | } | 55 | } |
53 | 56 | ||
@@ -61,18 +64,16 @@ char *ipc_single_command(const char *socket_path, uint32_t type, const char *pay | |||
61 | } | 64 | } |
62 | 65 | ||
63 | total = 0; | 66 | total = 0; |
64 | len = data32[0]; | 67 | *len = data32[0]; |
65 | char *response = malloc(len + 1); | 68 | char *response = malloc(*len + 1); |
66 | while (total < len) { | 69 | while (total < *len) { |
67 | ssize_t received = recv(socketfd, response + total, len - total, 0); | 70 | ssize_t received = recv(socketfd, response + total, *len - total, 0); |
68 | if (received < 0) { | 71 | if (received < 0) { |
69 | sway_abort("Unable to receive IPC response"); | 72 | sway_abort("Unable to receive IPC response"); |
70 | } | 73 | } |
71 | total += received; | 74 | total += received; |
72 | } | 75 | } |
73 | response[len] = '\0'; | 76 | response[*len] = '\0'; |
74 | |||
75 | close(socketfd); | ||
76 | 77 | ||
77 | return response; | 78 | return response; |
78 | } | 79 | } |