]> git.sur5r.net Git - i3/i3/blob - src/ipc.c
Initial implementation of IPC via UNIX domain sockets
[i3/i3] / src / ipc.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * ipc.c: Everything about the UNIX domain sockets for IPC
11  *
12  */
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <sys/un.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <err.h>
21 #include <stdlib.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <ev.h>
25
26 #include "queue.h"
27 #include "i3/ipc.h"
28 #include "i3.h"
29 #include "util.h"
30 #include "commands.h"
31
32 typedef struct ipc_client {
33         int fd;
34
35         TAILQ_ENTRY(ipc_client) clients;
36 } ipc_client;
37
38 TAILQ_HEAD(ipc_client_head, ipc_client) all_clients = TAILQ_HEAD_INITIALIZER(all_clients);
39
40 /*
41  * Puts the given socket file descriptor into non-blocking mode or dies if
42  * setting O_NONBLOCK failed. Non-blocking sockets are a good idea for our
43  * IPC model because we should by no means block the window manager.
44  *
45  */
46 static void set_nonblock(int sockfd) {
47         int flags = fcntl(sockfd, F_GETFL, 0);
48         flags |= O_NONBLOCK;
49         if (fcntl(sockfd, F_SETFL, flags) < 0)
50                 err(-1, "Could not set O_NONBLOCK");
51 }
52
53 #if 0
54 void broadcast(EV_P_ struct ev_timer *t, int revents) {
55         ipc_client *current;
56         TAILQ_FOREACH(current, &all_clients, clients) {
57                 write(current->fd, "hi there!\n", strlen("hi there!\n"));
58         }
59 }
60 #endif
61
62 /*
63  * Decides what to do with the received message.
64  *
65  * message is the raw packet, as received from the UNIX domain socket. size
66  * is the remaining size of bytes for this packet.
67  *
68  * message_size is the size of the message as the sender specified it.
69  * message_type is the type of the message as the sender specified it.
70  *
71  */
72 static void ipc_handle_message(uint8_t *message, int size,
73                                 uint32_t message_size, uint32_t message_type) {
74         LOG("handling message of size %d\n", size);
75         LOG("sender specified size %d\n", message_size);
76         LOG("sender specified type %d\n", message_type);
77         LOG("payload as a string = %s\n", message);
78
79         switch (message_type) {
80                 case I3_IPC_MESSAGE_TYPE_COMMAND:
81                         parse_command(global_conn, (const char*)message);
82
83                         break;
84                 default:
85                         LOG("unhandled ipc message\n");
86                         break;
87         }
88 }
89
90 /*
91  * Handler for activity on a client connection, receives a message from a
92  * client.
93  *
94  * For now, the maximum message size is 2048. I’m not sure for what the
95  * IPC interface will be used in the future, thus I’m not implementing a
96  * mechanism for arbitrarily long messages, as it seems like overkill
97  * at the moment.
98  *
99  */
100 static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) {
101         char buf[2048];
102         int n = read(w->fd, buf, sizeof(buf));
103
104         /* On error or an empty message, we close the connection */
105         if (n <= 0) {
106 #if 0
107                 /* FIXME: I get these when closing a client socket,
108                  * therefore we just treat them as an error. Is this
109                  * correct? */
110                 if (errno == EAGAIN || errno == EWOULDBLOCK)
111                         return;
112 #endif
113
114                 /* If not, there was some kind of error. We don’t bother
115                  * and close the connection */
116                 close(w->fd);
117
118                 /* Delete the client from the list of clients */
119                 struct ipc_client *current;
120                 TAILQ_FOREACH(current, &all_clients, clients) {
121                         if (current->fd != w->fd)
122                                 continue;
123
124                         /* We can call TAILQ_REMOVE because we break out of the
125                          * TAILQ_FOREACH afterwards */
126                         TAILQ_REMOVE(&all_clients, current, clients);
127                         break;
128                 }
129
130                 ev_io_stop(EV_A_ w);
131
132                 LOG("IPC: client disconnected\n");
133                 return;
134         }
135
136         /* Terminate the message correctly */
137         buf[n] = '\0';
138
139         /* Check if the message starts with the i3 IPC magic code */
140         if (n < strlen(I3_IPC_MAGIC)) {
141                 LOG("IPC: message too short, ignoring\n");
142                 return;
143         }
144
145         if (strncmp(buf, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) {
146                 LOG("IPC: message does not start with the IPC magic\n");
147                 return;
148         }
149
150         uint8_t *message = (uint8_t*)buf;
151         message += strlen(I3_IPC_MAGIC);
152         n -= strlen(I3_IPC_MAGIC);
153
154         /* The next 32 bit after the magic are the message size */
155         uint32_t message_size = *((uint32_t*)message);
156         message += sizeof(uint32_t);
157         n -= sizeof(uint32_t);
158
159         /* The last 32 bits of the header are the message type */
160         uint32_t message_type = *((uint32_t*)message);
161         message += sizeof(uint32_t);
162         n -= sizeof(uint32_t);
163
164         ipc_handle_message(message, n, message_size, message_type);
165 }
166
167 /*
168  * Handler for activity on the listening socket, meaning that a new client
169  * has just connected and we should accept() him. Sets up the event handler
170  * for activity on the new connection and inserts the file descriptor into
171  * the list of clients.
172  *
173  */
174 void ipc_new_client(EV_P_ struct ev_io *w, int revents) {
175         struct sockaddr_un peer;
176         socklen_t len = sizeof(struct sockaddr_un);
177         int client;
178         if ((client = accept(w->fd, (struct sockaddr*)&peer, &len)) < 0) {
179                 if (errno == EINTR)
180                         return;
181                 else perror("accept()");
182                 return;
183         }
184
185         set_nonblock(client);
186
187         struct ev_io *package = calloc(sizeof(struct ev_io), 1);
188         ev_io_init(package, ipc_receive_message, client, EV_READ);
189         ev_io_start(EV_A_ package);
190
191         LOG("IPC: new client connected\n");
192
193         struct ipc_client *new = calloc(sizeof(struct ipc_client), 1);
194         new->fd = client;
195
196         TAILQ_INSERT_TAIL(&all_clients, new, clients);
197 }
198
199 /*
200  * Creates the UNIX domain socket at the given path, sets it to non-blocking
201  * mode, bind()s and listen()s on it.
202  *
203  */
204 int ipc_create_socket(const char *filename) {
205         int sockfd;
206
207         /* Unlink the unix domain socket before */
208         unlink(filename);
209
210         if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
211                 perror("socket()");
212                 return -1;
213         }
214
215         struct sockaddr_un addr;
216         memset(&addr, 0, sizeof(struct sockaddr_un));
217         addr.sun_family = AF_LOCAL;
218         strcpy(addr.sun_path, filename);
219         if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) {
220                 perror("bind()");
221                 return -1;
222         }
223
224         set_nonblock(sockfd);
225
226         if (listen(sockfd, 5) < 0) {
227                 perror("listen()");
228                 return -1;
229         }
230
231         return sockfd;
232 }