4 * i3 - an improved dynamic tiling window manager
6 * © 2009 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>
32 typedef struct ipc_client {
35 TAILQ_ENTRY(ipc_client) clients;
38 TAILQ_HEAD(ipc_client_head, ipc_client) all_clients = TAILQ_HEAD_INITIALIZER(all_clients);
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.
46 static void set_nonblock(int sockfd) {
47 int flags = fcntl(sockfd, F_GETFL, 0);
49 if (fcntl(sockfd, F_SETFL, flags) < 0)
50 err(-1, "Could not set O_NONBLOCK");
54 void broadcast(EV_P_ struct ev_timer *t, int revents) {
56 TAILQ_FOREACH(current, &all_clients, clients) {
57 write(current->fd, "hi there!\n", strlen("hi there!\n"));
63 * Decides what to do with the received message.
65 * message is the raw packet, as received from the UNIX domain socket. size
66 * is the remaining size of bytes for this packet.
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.
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);
79 switch (message_type) {
80 case I3_IPC_MESSAGE_TYPE_COMMAND: {
81 /* To get a properly terminated buffer, we copy
82 * message_size bytes out of the buffer */
83 char *command = scalloc(message_size);
84 strncpy(command, (const char*)message, message_size);
85 parse_command(global_conn, (const char*)command);
91 LOG("unhandled ipc message\n");
97 * Handler for activity on a client connection, receives a message from a
100 * For now, the maximum message size is 2048. I’m not sure for what the
101 * IPC interface will be used in the future, thus I’m not implementing a
102 * mechanism for arbitrarily long messages, as it seems like overkill
106 static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) {
108 int n = read(w->fd, buf, sizeof(buf));
110 /* On error or an empty message, we close the connection */
113 /* FIXME: I get these when closing a client socket,
114 * therefore we just treat them as an error. Is this
116 if (errno == EAGAIN || errno == EWOULDBLOCK)
120 /* If not, there was some kind of error. We don’t bother
121 * and close the connection */
124 /* Delete the client from the list of clients */
125 struct ipc_client *current;
126 TAILQ_FOREACH(current, &all_clients, clients) {
127 if (current->fd != w->fd)
130 /* We can call TAILQ_REMOVE because we break out of the
131 * TAILQ_FOREACH afterwards */
132 TAILQ_REMOVE(&all_clients, current, clients);
138 LOG("IPC: client disconnected\n");
142 /* Terminate the message correctly */
145 /* Check if the message starts with the i3 IPC magic code */
146 if (n < strlen(I3_IPC_MAGIC)) {
147 LOG("IPC: message too short, ignoring\n");
151 if (strncmp(buf, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) {
152 LOG("IPC: message does not start with the IPC magic\n");
156 uint8_t *message = (uint8_t*)buf;
158 LOG("IPC: n = %d\n", n);
159 message += strlen(I3_IPC_MAGIC);
160 n -= strlen(I3_IPC_MAGIC);
162 /* The next 32 bit after the magic are the message size */
163 uint32_t message_size = *((uint32_t*)message);
164 message += sizeof(uint32_t);
165 n -= sizeof(uint32_t);
167 if (message_size > n) {
168 LOG("IPC: Either the message size was wrong or the message was not read completely, dropping\n");
172 /* The last 32 bits of the header are the message type */
173 uint32_t message_type = *((uint32_t*)message);
174 message += sizeof(uint32_t);
175 n -= sizeof(uint32_t);
177 ipc_handle_message(message, n, message_size, message_type);
179 message += message_size;
184 * Handler for activity on the listening socket, meaning that a new client
185 * has just connected and we should accept() him. Sets up the event handler
186 * for activity on the new connection and inserts the file descriptor into
187 * the list of clients.
190 void ipc_new_client(EV_P_ struct ev_io *w, int revents) {
191 struct sockaddr_un peer;
192 socklen_t len = sizeof(struct sockaddr_un);
194 if ((client = accept(w->fd, (struct sockaddr*)&peer, &len)) < 0) {
197 else perror("accept()");
201 set_nonblock(client);
203 struct ev_io *package = calloc(sizeof(struct ev_io), 1);
204 ev_io_init(package, ipc_receive_message, client, EV_READ);
205 ev_io_start(EV_A_ package);
207 LOG("IPC: new client connected\n");
209 struct ipc_client *new = calloc(sizeof(struct ipc_client), 1);
212 TAILQ_INSERT_TAIL(&all_clients, new, clients);
216 * Creates the UNIX domain socket at the given path, sets it to non-blocking
217 * mode, bind()s and listen()s on it.
220 int ipc_create_socket(const char *filename) {
223 /* Unlink the unix domain socket before */
226 if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
231 struct sockaddr_un addr;
232 memset(&addr, 0, sizeof(struct sockaddr_un));
233 addr.sun_family = AF_LOCAL;
234 strcpy(addr.sun_path, filename);
235 if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) {
240 set_nonblock(sockfd);
242 if (listen(sockfd, 5) < 0) {