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 parse_command(global_conn, (const char*)message);
85 LOG("unhandled ipc message\n");
91 * Handler for activity on a client connection, receives a message from a
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
100 static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) {
102 int n = read(w->fd, buf, sizeof(buf));
104 /* On error or an empty message, we close the connection */
107 /* FIXME: I get these when closing a client socket,
108 * therefore we just treat them as an error. Is this
110 if (errno == EAGAIN || errno == EWOULDBLOCK)
114 /* If not, there was some kind of error. We don’t bother
115 * and close the connection */
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)
124 /* We can call TAILQ_REMOVE because we break out of the
125 * TAILQ_FOREACH afterwards */
126 TAILQ_REMOVE(&all_clients, current, clients);
132 LOG("IPC: client disconnected\n");
136 /* Terminate the message correctly */
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");
145 if (strncmp(buf, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) {
146 LOG("IPC: message does not start with the IPC magic\n");
150 uint8_t *message = (uint8_t*)buf;
151 message += strlen(I3_IPC_MAGIC);
152 n -= strlen(I3_IPC_MAGIC);
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);
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);
164 ipc_handle_message(message, n, message_size, message_type);
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.
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);
178 if ((client = accept(w->fd, (struct sockaddr*)&peer, &len)) < 0) {
181 else perror("accept()");
185 set_nonblock(client);
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);
191 LOG("IPC: new client connected\n");
193 struct ipc_client *new = calloc(sizeof(struct ipc_client), 1);
196 TAILQ_INSERT_TAIL(&all_clients, new, clients);
200 * Creates the UNIX domain socket at the given path, sets it to non-blocking
201 * mode, bind()s and listen()s on it.
204 int ipc_create_socket(const char *filename) {
207 /* Unlink the unix domain socket before */
210 if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
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) {
224 set_nonblock(sockfd);
226 if (listen(sockfd, 5) < 0) {