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>
33 typedef struct ipc_client {
36 TAILQ_ENTRY(ipc_client) clients;
39 TAILQ_HEAD(ipc_client_head, ipc_client) all_clients = TAILQ_HEAD_INITIALIZER(all_clients);
42 * Puts the given socket file descriptor into non-blocking mode or dies if
43 * setting O_NONBLOCK failed. Non-blocking sockets are a good idea for our
44 * IPC model because we should by no means block the window manager.
47 static void set_nonblock(int sockfd) {
48 int flags = fcntl(sockfd, F_GETFL, 0);
50 if (fcntl(sockfd, F_SETFL, flags) < 0)
51 err(-1, "Could not set O_NONBLOCK");
55 void broadcast(EV_P_ struct ev_timer *t, int revents) {
57 TAILQ_FOREACH(current, &all_clients, clients) {
58 write(current->fd, "hi there!\n", strlen("hi there!\n"));
64 * Decides what to do with the received message.
66 * message is the raw packet, as received from the UNIX domain socket. size
67 * is the remaining size of bytes for this packet.
69 * message_size is the size of the message as the sender specified it.
70 * message_type is the type of the message as the sender specified it.
73 static void ipc_handle_message(uint8_t *message, int size,
74 uint32_t message_size, uint32_t message_type) {
75 DLOG("handling message of size %d\n", size);
76 DLOG("sender specified size %d\n", message_size);
77 DLOG("sender specified type %d\n", message_type);
78 DLOG("payload as a string = %s\n", message);
80 switch (message_type) {
81 case I3_IPC_MESSAGE_TYPE_COMMAND: {
82 /* To get a properly terminated buffer, we copy
83 * message_size bytes out of the buffer */
84 char *command = scalloc(message_size);
85 strncpy(command, (const char*)message, message_size);
86 parse_command(global_conn, (const char*)command);
92 DLOG("unhandled ipc message\n");
98 * Handler for activity on a client connection, receives a message from a
101 * For now, the maximum message size is 2048. I’m not sure for what the
102 * IPC interface will be used in the future, thus I’m not implementing a
103 * mechanism for arbitrarily long messages, as it seems like overkill
107 static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) {
109 int n = read(w->fd, buf, sizeof(buf));
111 /* On error or an empty message, we close the connection */
114 /* FIXME: I get these when closing a client socket,
115 * therefore we just treat them as an error. Is this
117 if (errno == EAGAIN || errno == EWOULDBLOCK)
121 /* If not, there was some kind of error. We don’t bother
122 * and close the connection */
125 /* Delete the client from the list of clients */
126 struct ipc_client *current;
127 TAILQ_FOREACH(current, &all_clients, clients) {
128 if (current->fd != w->fd)
131 /* We can call TAILQ_REMOVE because we break out of the
132 * TAILQ_FOREACH afterwards */
133 TAILQ_REMOVE(&all_clients, current, clients);
139 DLOG("IPC: client disconnected\n");
143 /* Terminate the message correctly */
146 /* Check if the message starts with the i3 IPC magic code */
147 if (n < strlen(I3_IPC_MAGIC)) {
148 DLOG("IPC: message too short, ignoring\n");
152 if (strncmp(buf, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) {
153 DLOG("IPC: message does not start with the IPC magic\n");
157 uint8_t *message = (uint8_t*)buf;
159 DLOG("IPC: n = %d\n", n);
160 message += strlen(I3_IPC_MAGIC);
161 n -= strlen(I3_IPC_MAGIC);
163 /* The next 32 bit after the magic are the message size */
164 uint32_t message_size = *((uint32_t*)message);
165 message += sizeof(uint32_t);
166 n -= sizeof(uint32_t);
168 if (message_size > n) {
169 DLOG("IPC: Either the message size was wrong or the message was not read completely, dropping\n");
173 /* The last 32 bits of the header are the message type */
174 uint32_t message_type = *((uint32_t*)message);
175 message += sizeof(uint32_t);
176 n -= sizeof(uint32_t);
178 ipc_handle_message(message, n, message_size, message_type);
180 message += message_size;
185 * Handler for activity on the listening socket, meaning that a new client
186 * has just connected and we should accept() him. Sets up the event handler
187 * for activity on the new connection and inserts the file descriptor into
188 * the list of clients.
191 void ipc_new_client(EV_P_ struct ev_io *w, int revents) {
192 struct sockaddr_un peer;
193 socklen_t len = sizeof(struct sockaddr_un);
195 if ((client = accept(w->fd, (struct sockaddr*)&peer, &len)) < 0) {
198 else perror("accept()");
202 set_nonblock(client);
204 struct ev_io *package = scalloc(sizeof(struct ev_io));
205 ev_io_init(package, ipc_receive_message, client, EV_READ);
206 ev_io_start(EV_A_ package);
208 DLOG("IPC: new client connected\n");
210 struct ipc_client *new = scalloc(sizeof(struct ipc_client));
213 TAILQ_INSERT_TAIL(&all_clients, new, clients);
217 * Creates the UNIX domain socket at the given path, sets it to non-blocking
218 * mode, bind()s and listen()s on it.
221 int ipc_create_socket(const char *filename) {
224 /* Unlink the unix domain socket before */
227 if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
232 (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
234 struct sockaddr_un addr;
235 memset(&addr, 0, sizeof(struct sockaddr_un));
236 addr.sun_family = AF_LOCAL;
237 strcpy(addr.sun_path, filename);
238 if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) {
243 set_nonblock(sockfd);
245 if (listen(sockfd, 5) < 0) {