diff options
author | minus <minus@mnus.de> | 2015-08-16 20:24:18 +0200 |
---|---|---|
committer | minus <minus@mnus.de> | 2015-08-20 15:24:33 +0200 |
commit | 8981b48cd2cdd0a59c1546f2816587b303538d77 (patch) | |
tree | c764bd013e9e368418adee8e3e833fb5b948c796 /sway/ipc.c | |
parent | Add command line parsing (diff) | |
download | sway-8981b48cd2cdd0a59c1546f2816587b303538d77.tar.gz sway-8981b48cd2cdd0a59c1546f2816587b303538d77.tar.zst sway-8981b48cd2cdd0a59c1546f2816587b303538d77.zip |
very basic IPC implementation
simply executes the received data as command
Diffstat (limited to 'sway/ipc.c')
-rw-r--r-- | sway/ipc.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/sway/ipc.c b/sway/ipc.c new file mode 100644 index 00000000..ac5246d2 --- /dev/null +++ b/sway/ipc.c | |||
@@ -0,0 +1,70 @@ | |||
1 | #include <errno.h> | ||
2 | #include <string.h> | ||
3 | #include <sys/socket.h> | ||
4 | #include <sys/un.h> | ||
5 | #include <wlc/wlc.h> | ||
6 | #include <unistd.h> | ||
7 | #include "log.h" | ||
8 | #include "config.h" | ||
9 | #include "commands.h" | ||
10 | |||
11 | static int ipc_socket = -1; | ||
12 | |||
13 | int ipc_handle_connection(int fd, uint32_t mask, void *data); | ||
14 | |||
15 | void init_ipc() { | ||
16 | ipc_socket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); | ||
17 | if (ipc_socket == -1) { | ||
18 | sway_abort("Unable to create IPC socket"); | ||
19 | } | ||
20 | |||
21 | struct sockaddr_un ipc_sockaddr = { | ||
22 | .sun_family = AF_UNIX, | ||
23 | // TODO: use a proper socket path | ||
24 | .sun_path = "/tmp/sway.sock" | ||
25 | }; | ||
26 | |||
27 | unlink(ipc_sockaddr.sun_path); | ||
28 | if (bind(ipc_socket, (struct sockaddr *)&ipc_sockaddr, sizeof(ipc_sockaddr)) == -1) { | ||
29 | sway_abort("Unable to bind IPC socket"); | ||
30 | } | ||
31 | |||
32 | if (listen(ipc_socket, 3) == -1) { | ||
33 | sway_abort("Unable to listen on IPC socket"); | ||
34 | } | ||
35 | |||
36 | wlc_event_loop_add_fd(ipc_socket, WLC_EVENT_READABLE, ipc_handle_connection, NULL); | ||
37 | } | ||
38 | |||
39 | int ipc_handle_connection(int /*fd*/, uint32_t /*mask*/, void */*data*/) { | ||
40 | int client_socket = accept(ipc_socket, NULL, NULL); | ||
41 | if (client_socket == -1) { | ||
42 | char error[256]; | ||
43 | strerror_r(errno, error, sizeof(error)); | ||
44 | sway_log(L_INFO, "Unable to accept IPC client connection: %s", error); | ||
45 | return 0; | ||
46 | } | ||
47 | |||
48 | char buf[1024]; | ||
49 | if (recv(client_socket, buf, sizeof(buf), 0) == -1) { | ||
50 | char error[256]; | ||
51 | strerror_r(errno, error, sizeof(error)); | ||
52 | sway_log(L_INFO, "Unable to receive from IPC client: %s", error); | ||
53 | close(client_socket); | ||
54 | return 0; | ||
55 | } | ||
56 | |||
57 | sway_log(L_INFO, "Executing IPC command: %s", buf); | ||
58 | |||
59 | bool success = handle_command(config, buf); | ||
60 | snprintf(buf, sizeof(buf), "{\"success\":%s}\n", success ? "true" : "false"); | ||
61 | |||
62 | if (send(client_socket, buf, strlen(buf), 0) == -1) { | ||
63 | char error[256]; | ||
64 | strerror_r(errno, error, sizeof(error)); | ||
65 | sway_log(L_INFO, "Unable to send to IPC client: %s", error); | ||
66 | } | ||
67 | |||
68 | close(client_socket); | ||
69 | return 0; | ||
70 | } | ||