4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * ipc.c: Everything about the UNIX domain sockets for IPC
13 #include <sys/types.h>
14 #include <sys/socket.h>
25 #include <yajl/yajl_gen.h>
35 /* Shorter names for all those yajl_gen_* functions */
36 #define y(x, ...) yajl_gen_ ## x (gen, ##__VA_ARGS__)
37 #define ystr(str) yajl_gen_string(gen, (unsigned char*)str, strlen(str))
39 typedef struct ipc_client {
42 TAILQ_ENTRY(ipc_client) clients;
45 TAILQ_HEAD(ipc_client_head, ipc_client) all_clients = TAILQ_HEAD_INITIALIZER(all_clients);
48 * Puts the given socket file descriptor into non-blocking mode or dies if
49 * setting O_NONBLOCK failed. Non-blocking sockets are a good idea for our
50 * IPC model because we should by no means block the window manager.
53 static void set_nonblock(int sockfd) {
54 int flags = fcntl(sockfd, F_GETFL, 0);
56 if (fcntl(sockfd, F_SETFL, flags) < 0)
57 err(-1, "Could not set O_NONBLOCK");
61 void broadcast(EV_P_ struct ev_timer *t, int revents) {
63 TAILQ_FOREACH(current, &all_clients, clients) {
64 write(current->fd, "hi there!\n", strlen("hi there!\n"));
69 static void ipc_send_message(int fd, const unsigned char *payload,
70 int message_type, int message_size) {
71 int buffer_size = strlen("i3-ipc") + sizeof(uint32_t) +
72 sizeof(uint32_t) + message_size;
73 char msg[buffer_size];
76 strcpy(walk, "i3-ipc");
77 walk += strlen("i3-ipc");
78 memcpy(walk, &message_size, sizeof(uint32_t));
79 walk += sizeof(uint32_t);
80 memcpy(walk, &message_type, sizeof(uint32_t));
81 walk += sizeof(uint32_t);
82 memcpy(walk, payload, message_size);
85 int bytes_to_go = buffer_size;
86 while (sent_bytes < bytes_to_go) {
87 int n = write(fd, msg + sent_bytes, bytes_to_go);
89 err(EXIT_FAILURE, "write() failed");
97 * Formats the reply message for a GET_WORKSPACES request and sends it to the
101 static void ipc_send_workspaces(int fd) {
104 yajl_gen gen = yajl_gen_alloc(NULL, NULL);
107 TAILQ_FOREACH(ws, workspaces, workspaces) {
108 if (ws->output == NULL)
121 y(integer, ws->rect.x);
123 y(integer, ws->rect.y);
125 y(integer, ws->rect.width);
127 y(integer, ws->rect.height);
131 ystr(ws->output->name);
138 const unsigned char *payload;
140 y(get_buf, &payload, &length);
142 ipc_send_message(fd, payload, I3_IPC_REPLY_TYPE_WORKSPACES, length);
147 * Decides what to do with the received message.
149 * message is the raw packet, as received from the UNIX domain socket. size
150 * is the remaining size of bytes for this packet.
152 * message_size is the size of the message as the sender specified it.
153 * message_type is the type of the message as the sender specified it.
156 static void ipc_handle_message(int fd, uint8_t *message, int size,
157 uint32_t message_size, uint32_t message_type) {
158 DLOG("handling message of size %d\n", size);
159 DLOG("sender specified size %d\n", message_size);
160 DLOG("sender specified type %d\n", message_type);
161 DLOG("payload as a string = %s\n", message);
163 switch (message_type) {
164 case I3_IPC_MESSAGE_TYPE_COMMAND: {
165 /* To get a properly terminated buffer, we copy
166 * message_size bytes out of the buffer */
167 char *command = scalloc(message_size);
168 strncpy(command, (const char*)message, message_size);
169 parse_command(global_conn, (const char*)command);
174 case I3_IPC_MESSAGE_TYPE_GET_WORKSPACES:
175 ipc_send_workspaces(fd);
178 DLOG("unhandled ipc message\n");
184 * Handler for activity on a client connection, receives a message from a
187 * For now, the maximum message size is 2048. I’m not sure for what the
188 * IPC interface will be used in the future, thus I’m not implementing a
189 * mechanism for arbitrarily long messages, as it seems like overkill
193 static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) {
195 int n = read(w->fd, buf, sizeof(buf));
197 /* On error or an empty message, we close the connection */
200 /* FIXME: I get these when closing a client socket,
201 * therefore we just treat them as an error. Is this
203 if (errno == EAGAIN || errno == EWOULDBLOCK)
207 /* If not, there was some kind of error. We don’t bother
208 * and close the connection */
211 /* Delete the client from the list of clients */
212 struct ipc_client *current;
213 TAILQ_FOREACH(current, &all_clients, clients) {
214 if (current->fd != w->fd)
217 /* We can call TAILQ_REMOVE because we break out of the
218 * TAILQ_FOREACH afterwards */
219 TAILQ_REMOVE(&all_clients, current, clients);
225 DLOG("IPC: client disconnected\n");
229 /* Terminate the message correctly */
232 /* Check if the message starts with the i3 IPC magic code */
233 if (n < strlen(I3_IPC_MAGIC)) {
234 DLOG("IPC: message too short, ignoring\n");
238 if (strncmp(buf, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) {
239 DLOG("IPC: message does not start with the IPC magic\n");
243 uint8_t *message = (uint8_t*)buf;
245 DLOG("IPC: n = %d\n", n);
246 message += strlen(I3_IPC_MAGIC);
247 n -= strlen(I3_IPC_MAGIC);
249 /* The next 32 bit after the magic are the message size */
250 uint32_t message_size = *((uint32_t*)message);
251 message += sizeof(uint32_t);
252 n -= sizeof(uint32_t);
254 if (message_size > n) {
255 DLOG("IPC: Either the message size was wrong or the message was not read completely, dropping\n");
259 /* The last 32 bits of the header are the message type */
260 uint32_t message_type = *((uint32_t*)message);
261 message += sizeof(uint32_t);
262 n -= sizeof(uint32_t);
264 ipc_handle_message(w->fd, message, n, message_size, message_type);
266 message += message_size;
271 * Handler for activity on the listening socket, meaning that a new client
272 * has just connected and we should accept() him. Sets up the event handler
273 * for activity on the new connection and inserts the file descriptor into
274 * the list of clients.
277 void ipc_new_client(EV_P_ struct ev_io *w, int revents) {
278 struct sockaddr_un peer;
279 socklen_t len = sizeof(struct sockaddr_un);
281 if ((client = accept(w->fd, (struct sockaddr*)&peer, &len)) < 0) {
284 else perror("accept()");
288 set_nonblock(client);
290 struct ev_io *package = scalloc(sizeof(struct ev_io));
291 ev_io_init(package, ipc_receive_message, client, EV_READ);
292 ev_io_start(EV_A_ package);
294 DLOG("IPC: new client connected\n");
296 struct ipc_client *new = scalloc(sizeof(struct ipc_client));
299 TAILQ_INSERT_TAIL(&all_clients, new, clients);
303 * Creates the UNIX domain socket at the given path, sets it to non-blocking
304 * mode, bind()s and listen()s on it.
307 int ipc_create_socket(const char *filename) {
310 /* Unlink the unix domain socket before */
313 if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
318 (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
320 struct sockaddr_un addr;
321 memset(&addr, 0, sizeof(struct sockaddr_un));
322 addr.sun_family = AF_LOCAL;
323 strcpy(addr.sun_path, filename);
324 if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) {
329 set_nonblock(sockfd);
331 if (listen(sockfd, 5) < 0) {