]> git.sur5r.net Git - i3/i3/blob - src/ipc.c
Merge branch 'master' into next
[i3/i3] / src / ipc.c
1 #undef I3__FILE__
2 #define I3__FILE__ "ipc.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * ipc.c: UNIX domain socket IPC (initialization, client handling, protocol).
10  *
11  */
12 #include "all.h"
13 #include "yajl_utils.h"
14
15 #include <sys/socket.h>
16 #include <sys/un.h>
17 #include <fcntl.h>
18 #include <libgen.h>
19 #include <ev.h>
20 #include <yajl/yajl_gen.h>
21 #include <yajl/yajl_parse.h>
22
23 char *current_socketpath = NULL;
24
25 TAILQ_HEAD(ipc_client_head, ipc_client) all_clients = TAILQ_HEAD_INITIALIZER(all_clients);
26
27 /*
28  * Puts the given socket file descriptor into non-blocking mode or dies if
29  * setting O_NONBLOCK failed. Non-blocking sockets are a good idea for our
30  * IPC model because we should by no means block the window manager.
31  *
32  */
33 static void set_nonblock(int sockfd) {
34     int flags = fcntl(sockfd, F_GETFL, 0);
35     flags |= O_NONBLOCK;
36     if (fcntl(sockfd, F_SETFL, flags) < 0)
37         err(-1, "Could not set O_NONBLOCK");
38 }
39
40 /*
41  * Emulates mkdir -p (creates any missing folders)
42  *
43  */
44 static bool mkdirp(const char *path) {
45     if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0)
46         return true;
47     if (errno != ENOENT) {
48         ELOG("mkdir(%s) failed: %s\n", path, strerror(errno));
49         return false;
50     }
51     char *copy = sstrdup(path);
52     /* strip trailing slashes, if any */
53     while (copy[strlen(copy)-1] == '/')
54         copy[strlen(copy)-1] = '\0';
55
56     char *sep = strrchr(copy, '/');
57     if (sep == NULL) {
58         FREE(copy);
59         return false;
60     }
61     *sep = '\0';
62     bool result = false;
63     if (mkdirp(copy))
64         result = mkdirp(path);
65     free(copy);
66
67     return result;
68 }
69
70 /*
71  * Sends the specified event to all IPC clients which are currently connected
72  * and subscribed to this kind of event.
73  *
74  */
75 void ipc_send_event(const char *event, uint32_t message_type, const char *payload) {
76     ipc_client *current;
77     TAILQ_FOREACH(current, &all_clients, clients) {
78         /* see if this client is interested in this event */
79         bool interested = false;
80         for (int i = 0; i < current->num_events; i++) {
81             if (strcasecmp(current->events[i], event) != 0)
82                 continue;
83             interested = true;
84             break;
85         }
86         if (!interested)
87             continue;
88
89         ipc_send_message(current->fd, strlen(payload), message_type, (const uint8_t*)payload);
90     }
91 }
92
93 /*
94  * Calls shutdown() on each socket and closes it. This function to be called
95  * when exiting or restarting only!
96  *
97  */
98 void ipc_shutdown(void) {
99     ipc_client *current;
100     while (!TAILQ_EMPTY(&all_clients)) {
101         current = TAILQ_FIRST(&all_clients);
102         shutdown(current->fd, SHUT_RDWR);
103         close(current->fd);
104         TAILQ_REMOVE(&all_clients, current, clients);
105         free(current);
106     }
107 }
108
109 /*
110  * Executes the command and returns whether it could be successfully parsed
111  * or not (at the moment, always returns true).
112  *
113  */
114 IPC_HANDLER(command) {
115     /* To get a properly terminated buffer, we copy
116      * message_size bytes out of the buffer */
117     char *command = scalloc(message_size + 1);
118     strncpy(command, (const char*)message, message_size);
119     LOG("IPC: received: *%s*\n", command);
120     struct CommandResult *command_output = parse_command((const char*)command);
121     free(command);
122
123     if (command_output->needs_tree_render)
124         tree_render();
125
126     const unsigned char *reply;
127     ylength length;
128     yajl_gen_get_buf(command_output->json_gen, &reply, &length);
129
130     ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_COMMAND,
131                      (const uint8_t*)reply);
132
133     yajl_gen_free(command_output->json_gen);
134 }
135
136 static void dump_rect(yajl_gen gen, const char *name, Rect r) {
137     ystr(name);
138     y(map_open);
139     ystr("x");
140     y(integer, r.x);
141     ystr("y");
142     y(integer, r.y);
143     ystr("width");
144     y(integer, r.width);
145     ystr("height");
146     y(integer, r.height);
147     y(map_close);
148 }
149
150 void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart) {
151     y(map_open);
152     ystr("id");
153     y(integer, (long int)con);
154
155     ystr("type");
156     y(integer, con->type);
157
158     /* provided for backwards compatibility only. */
159     ystr("orientation");
160     if (!con_is_split(con))
161         ystr("none");
162     else {
163         if (con_orientation(con) == HORIZ)
164             ystr("horizontal");
165         else ystr("vertical");
166     }
167
168     ystr("scratchpad_state");
169     switch (con->scratchpad_state) {
170         case SCRATCHPAD_NONE:
171             ystr("none");
172             break;
173         case SCRATCHPAD_FRESH:
174             ystr("fresh");
175             break;
176         case SCRATCHPAD_CHANGED:
177             ystr("changed");
178             break;
179     }
180
181     ystr("percent");
182     if (con->percent == 0.0)
183         y(null);
184     else y(double, con->percent);
185
186     ystr("urgent");
187     y(bool, con->urgent);
188
189     if (con->mark != NULL) {
190         ystr("mark");
191         ystr(con->mark);
192     }
193
194     ystr("focused");
195     y(bool, (con == focused));
196
197     ystr("layout");
198     switch (con->layout) {
199         case L_DEFAULT:
200             DLOG("About to dump layout=default, this is a bug in the code.\n");
201             assert(false);
202             break;
203         case L_SPLITV:
204             ystr("splitv");
205             break;
206         case L_SPLITH:
207             ystr("splith");
208             break;
209         case L_STACKED:
210             ystr("stacked");
211             break;
212         case L_TABBED:
213             ystr("tabbed");
214             break;
215         case L_DOCKAREA:
216             ystr("dockarea");
217             break;
218         case L_OUTPUT:
219             ystr("output");
220             break;
221     }
222
223     ystr("workspace_layout");
224     switch (con->workspace_layout) {
225         case L_DEFAULT:
226             ystr("default");
227             break;
228         case L_STACKED:
229             ystr("stacked");
230             break;
231         case L_TABBED:
232             ystr("tabbed");
233             break;
234         default:
235             DLOG("About to dump workspace_layout=%d (none of default/stacked/tabbed), this is a bug.\n", con->workspace_layout);
236             assert(false);
237             break;
238     }
239
240     ystr("last_split_layout");
241     switch (con->layout) {
242         case L_SPLITV:
243             ystr("splitv");
244             break;
245         default:
246             ystr("splith");
247             break;
248     }
249
250     ystr("border");
251     switch (con->border_style) {
252         case BS_NORMAL:
253             ystr("normal");
254             break;
255         case BS_NONE:
256             ystr("none");
257             break;
258         case BS_PIXEL:
259             ystr("pixel");
260             break;
261     }
262
263     ystr("current_border_width");
264     y(integer, con->current_border_width);
265
266     dump_rect(gen, "rect", con->rect);
267     dump_rect(gen, "window_rect", con->window_rect);
268     dump_rect(gen, "geometry", con->geometry);
269
270     ystr("name");
271     if (con->window && con->window->name)
272         ystr(i3string_as_utf8(con->window->name));
273     else
274         ystr(con->name);
275
276     if (con->type == CT_WORKSPACE) {
277         ystr("num");
278         y(integer, con->num);
279     }
280
281     ystr("window");
282     if (con->window)
283         y(integer, con->window->id);
284     else y(null);
285
286     ystr("nodes");
287     y(array_open);
288     Con *node;
289     if (con->type != CT_DOCKAREA || !inplace_restart) {
290         TAILQ_FOREACH(node, &(con->nodes_head), nodes) {
291             dump_node(gen, node, inplace_restart);
292         }
293     }
294     y(array_close);
295
296     ystr("floating_nodes");
297     y(array_open);
298     TAILQ_FOREACH(node, &(con->floating_head), floating_windows) {
299         dump_node(gen, node, inplace_restart);
300     }
301     y(array_close);
302
303     ystr("focus");
304     y(array_open);
305     TAILQ_FOREACH(node, &(con->focus_head), focused) {
306         y(integer, (long int)node);
307     }
308     y(array_close);
309
310     ystr("fullscreen_mode");
311     y(integer, con->fullscreen_mode);
312
313     ystr("floating");
314     switch (con->floating) {
315         case FLOATING_AUTO_OFF:
316             ystr("auto_off");
317             break;
318         case FLOATING_AUTO_ON:
319             ystr("auto_on");
320             break;
321         case FLOATING_USER_OFF:
322             ystr("user_off");
323             break;
324         case FLOATING_USER_ON:
325             ystr("user_on");
326             break;
327     }
328
329     ystr("swallows");
330     y(array_open);
331     Match *match;
332     TAILQ_FOREACH(match, &(con->swallow_head), matches) {
333         if (match->dock != -1) {
334             y(map_open);
335             ystr("dock");
336             y(integer, match->dock);
337             ystr("insert_where");
338             y(integer, match->insert_where);
339             y(map_close);
340         }
341
342         /* TODO: the other swallow keys */
343     }
344
345     if (inplace_restart) {
346         if (con->window != NULL) {
347             y(map_open);
348             ystr("id");
349             y(integer, con->window->id);
350             ystr("restart_mode");
351             y(bool, true);
352             y(map_close);
353         }
354     }
355     y(array_close);
356
357     y(map_close);
358 }
359
360 IPC_HANDLER(tree) {
361     setlocale(LC_NUMERIC, "C");
362     yajl_gen gen = ygenalloc();
363     dump_node(gen, croot, false);
364     setlocale(LC_NUMERIC, "");
365
366     const unsigned char *payload;
367     ylength length;
368     y(get_buf, &payload, &length);
369
370     ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_TREE, payload);
371     y(free);
372 }
373
374
375 /*
376  * Formats the reply message for a GET_WORKSPACES request and sends it to the
377  * client
378  *
379  */
380 IPC_HANDLER(get_workspaces) {
381     yajl_gen gen = ygenalloc();
382     y(array_open);
383
384     Con *focused_ws = con_get_workspace(focused);
385
386     Con *output;
387     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
388         if (con_is_internal(output))
389             continue;
390         Con *ws;
391         TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
392             assert(ws->type == CT_WORKSPACE);
393             y(map_open);
394
395             ystr("num");
396             if (ws->num == -1)
397                 y(null);
398             else y(integer, ws->num);
399
400             ystr("name");
401             ystr(ws->name);
402
403             ystr("visible");
404             y(bool, workspace_is_visible(ws));
405
406             ystr("focused");
407             y(bool, ws == focused_ws);
408
409             ystr("rect");
410             y(map_open);
411             ystr("x");
412             y(integer, ws->rect.x);
413             ystr("y");
414             y(integer, ws->rect.y);
415             ystr("width");
416             y(integer, ws->rect.width);
417             ystr("height");
418             y(integer, ws->rect.height);
419             y(map_close);
420
421             ystr("output");
422             ystr(output->name);
423
424             ystr("urgent");
425             y(bool, ws->urgent);
426
427             y(map_close);
428         }
429     }
430
431     y(array_close);
432
433     const unsigned char *payload;
434     ylength length;
435     y(get_buf, &payload, &length);
436
437     ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_WORKSPACES, payload);
438     y(free);
439 }
440
441 /*
442  * Formats the reply message for a GET_OUTPUTS request and sends it to the
443  * client
444  *
445  */
446 IPC_HANDLER(get_outputs) {
447     yajl_gen gen = ygenalloc();
448     y(array_open);
449
450     Output *output;
451     TAILQ_FOREACH(output, &outputs, outputs) {
452         y(map_open);
453
454         ystr("name");
455         ystr(output->name);
456
457         ystr("active");
458         y(bool, output->active);
459
460         ystr("primary");
461         y(bool, output->primary);
462
463         ystr("rect");
464         y(map_open);
465         ystr("x");
466         y(integer, output->rect.x);
467         ystr("y");
468         y(integer, output->rect.y);
469         ystr("width");
470         y(integer, output->rect.width);
471         ystr("height");
472         y(integer, output->rect.height);
473         y(map_close);
474
475         ystr("current_workspace");
476         Con *ws = NULL;
477         if (output->con && (ws = con_get_fullscreen_con(output->con, CF_OUTPUT)))
478             ystr(ws->name);
479         else y(null);
480
481         y(map_close);
482     }
483
484     y(array_close);
485
486     const unsigned char *payload;
487     ylength length;
488     y(get_buf, &payload, &length);
489
490     ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_OUTPUTS, payload);
491     y(free);
492 }
493
494 /*
495  * Formats the reply message for a GET_MARKS request and sends it to the
496  * client
497  *
498  */
499 IPC_HANDLER(get_marks) {
500     yajl_gen gen = ygenalloc();
501     y(array_open);
502
503     Con *con;
504     TAILQ_FOREACH(con, &all_cons, all_cons)
505         if (con->mark != NULL)
506             ystr(con->mark);
507
508     y(array_close);
509
510     const unsigned char *payload;
511     ylength length;
512     y(get_buf, &payload, &length);
513
514     ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_MARKS, payload);
515     y(free);
516 }
517
518 /*
519  * Returns the version of i3
520  *
521  */
522 IPC_HANDLER(get_version) {
523     yajl_gen gen = ygenalloc();
524     y(map_open);
525
526     ystr("major");
527     y(integer, MAJOR_VERSION);
528
529     ystr("minor");
530     y(integer, MINOR_VERSION);
531
532     ystr("patch");
533     y(integer, PATCH_VERSION);
534
535     ystr("human_readable");
536     ystr(I3_VERSION);
537
538     y(map_close);
539
540     const unsigned char *payload;
541     ylength length;
542     y(get_buf, &payload, &length);
543
544     ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_VERSION, payload);
545     y(free);
546 }
547
548 /*
549  * Formats the reply message for a GET_BAR_CONFIG request and sends it to the
550  * client.
551  *
552  */
553 IPC_HANDLER(get_bar_config) {
554     yajl_gen gen = ygenalloc();
555
556     /* If no ID was passed, we return a JSON array with all IDs */
557     if (message_size == 0) {
558         y(array_open);
559         Barconfig *current;
560         TAILQ_FOREACH(current, &barconfigs, configs) {
561             ystr(current->id);
562         }
563         y(array_close);
564
565         const unsigned char *payload;
566         ylength length;
567         y(get_buf, &payload, &length);
568
569         ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
570         y(free);
571         return;
572     }
573
574     /* To get a properly terminated buffer, we copy
575      * message_size bytes out of the buffer */
576     char *bar_id = scalloc(message_size + 1);
577     strncpy(bar_id, (const char*)message, message_size);
578     LOG("IPC: looking for config for bar ID \"%s\"\n", bar_id);
579     Barconfig *current, *config = NULL;
580     TAILQ_FOREACH(current, &barconfigs, configs) {
581         if (strcmp(current->id, bar_id) != 0)
582             continue;
583
584         config = current;
585         break;
586     }
587
588     y(map_open);
589
590     if (!config) {
591         /* If we did not find a config for the given ID, the reply will contain
592          * a null 'id' field. */
593         ystr("id");
594         y(null);
595     } else {
596         ystr("id");
597         ystr(config->id);
598
599         if (config->num_outputs > 0) {
600             ystr("outputs");
601             y(array_open);
602             for (int c = 0; c < config->num_outputs; c++)
603                 ystr(config->outputs[c]);
604             y(array_close);
605         }
606
607 #define YSTR_IF_SET(name) \
608         do { \
609             if (config->name) { \
610                 ystr( # name); \
611                 ystr(config->name); \
612             } \
613         } while (0)
614
615         YSTR_IF_SET(tray_output);
616         YSTR_IF_SET(socket_path);
617
618         ystr("mode");
619         if (config->mode == M_HIDE)
620             ystr("hide");
621         else ystr("dock");
622
623         ystr("modifier");
624         switch (config->modifier) {
625             case M_CONTROL:
626                 ystr("ctrl");
627                 break;
628             case M_SHIFT:
629                 ystr("shift");
630                 break;
631             case M_MOD1:
632                 ystr("Mod1");
633                 break;
634             case M_MOD2:
635                 ystr("Mod2");
636                 break;
637             case M_MOD3:
638                 ystr("Mod3");
639                 break;
640             /*
641             case M_MOD4:
642                 ystr("Mod4");
643                 break;
644             */
645             case M_MOD5:
646                 ystr("Mod5");
647                 break;
648             default:
649                 ystr("Mod4");
650                 break;
651         }
652
653         ystr("position");
654         if (config->position == P_BOTTOM)
655             ystr("bottom");
656         else ystr("top");
657
658         YSTR_IF_SET(status_command);
659         YSTR_IF_SET(font);
660
661         ystr("workspace_buttons");
662         y(bool, !config->hide_workspace_buttons);
663
664         ystr("verbose");
665         y(bool, config->verbose);
666
667 #undef YSTR_IF_SET
668 #define YSTR_IF_SET(name) \
669         do { \
670             if (config->colors.name) { \
671                 ystr( # name); \
672                 ystr(config->colors.name); \
673             } \
674         } while (0)
675
676         ystr("colors");
677         y(map_open);
678         YSTR_IF_SET(background);
679         YSTR_IF_SET(statusline);
680         YSTR_IF_SET(focused_workspace_border);
681         YSTR_IF_SET(focused_workspace_bg);
682         YSTR_IF_SET(focused_workspace_text);
683         YSTR_IF_SET(active_workspace_border);
684         YSTR_IF_SET(active_workspace_bg);
685         YSTR_IF_SET(active_workspace_text);
686         YSTR_IF_SET(inactive_workspace_border);
687         YSTR_IF_SET(inactive_workspace_bg);
688         YSTR_IF_SET(inactive_workspace_text);
689         YSTR_IF_SET(urgent_workspace_border);
690         YSTR_IF_SET(urgent_workspace_bg);
691         YSTR_IF_SET(urgent_workspace_text);
692         y(map_close);
693
694 #undef YSTR_IF_SET
695     }
696
697     y(map_close);
698
699     const unsigned char *payload;
700     ylength length;
701     y(get_buf, &payload, &length);
702
703     ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
704     y(free);
705 }
706
707 /*
708  * Callback for the YAJL parser (will be called when a string is parsed).
709  *
710  */
711 static int add_subscription(void *extra, const unsigned char *s,
712                             ylength len) {
713     ipc_client *client = extra;
714
715     DLOG("should add subscription to extra %p, sub %.*s\n", client, (int)len, s);
716     int event = client->num_events;
717
718     client->num_events++;
719     client->events = realloc(client->events, client->num_events * sizeof(char*));
720     /* We copy the string because it is not null-terminated and strndup()
721      * is missing on some BSD systems */
722     client->events[event] = scalloc(len+1);
723     memcpy(client->events[event], s, len);
724
725     DLOG("client is now subscribed to:\n");
726     for (int i = 0; i < client->num_events; i++)
727         DLOG("event %s\n", client->events[i]);
728     DLOG("(done)\n");
729
730     return 1;
731 }
732
733 /*
734  * Subscribes this connection to the event types which were given as a JSON
735  * serialized array in the payload field of the message.
736  *
737  */
738 IPC_HANDLER(subscribe) {
739     yajl_handle p;
740     yajl_callbacks callbacks;
741     yajl_status stat;
742     ipc_client *current, *client = NULL;
743
744     /* Search the ipc_client structure for this connection */
745     TAILQ_FOREACH(current, &all_clients, clients) {
746         if (current->fd != fd)
747             continue;
748
749         client = current;
750         break;
751     }
752
753     if (client == NULL) {
754         ELOG("Could not find ipc_client data structure for fd %d\n", fd);
755         return;
756     }
757
758     /* Setup the JSON parser */
759     memset(&callbacks, 0, sizeof(yajl_callbacks));
760     callbacks.yajl_string = add_subscription;
761
762     p = yalloc(&callbacks, (void*)client);
763     stat = yajl_parse(p, (const unsigned char*)message, message_size);
764     if (stat != yajl_status_ok) {
765         unsigned char *err;
766         err = yajl_get_error(p, true, (const unsigned char*)message,
767                              message_size);
768         ELOG("YAJL parse error: %s\n", err);
769         yajl_free_error(p, err);
770
771         const char *reply = "{\"success\":false}";
772         ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t*)reply);
773         yajl_free(p);
774         return;
775     }
776     yajl_free(p);
777     const char *reply = "{\"success\":true}";
778     ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t*)reply);
779 }
780
781 /* The index of each callback function corresponds to the numeric
782  * value of the message type (see include/i3/ipc.h) */
783 handler_t handlers[8] = {
784     handle_command,
785     handle_get_workspaces,
786     handle_subscribe,
787     handle_get_outputs,
788     handle_tree,
789     handle_get_marks,
790     handle_get_bar_config,
791     handle_get_version,
792 };
793
794 /*
795  * Handler for activity on a client connection, receives a message from a
796  * client.
797  *
798  * For now, the maximum message size is 2048. I’m not sure for what the
799  * IPC interface will be used in the future, thus I’m not implementing a
800  * mechanism for arbitrarily long messages, as it seems like overkill
801  * at the moment.
802  *
803  */
804 static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) {
805     uint32_t message_type;
806     uint32_t message_length;
807     uint8_t *message;
808
809     int ret = ipc_recv_message(w->fd, &message_type, &message_length, &message);
810     /* EOF or other error */
811     if (ret < 0) {
812         /* Was this a spurious read? See ev(3) */
813         if (ret == -1 && errno == EAGAIN)
814             return;
815
816         /* If not, there was some kind of error. We don’t bother
817          * and close the connection */
818         close(w->fd);
819
820         /* Delete the client from the list of clients */
821         ipc_client *current;
822         TAILQ_FOREACH(current, &all_clients, clients) {
823             if (current->fd != w->fd)
824                 continue;
825
826             for (int i = 0; i < current->num_events; i++)
827                 free(current->events[i]);
828             /* We can call TAILQ_REMOVE because we break out of the
829              * TAILQ_FOREACH afterwards */
830             TAILQ_REMOVE(&all_clients, current, clients);
831             free(current);
832             break;
833         }
834
835         ev_io_stop(EV_A_ w);
836         free(w);
837
838         DLOG("IPC: client disconnected\n");
839         return;
840     }
841
842     if (message_type >= (sizeof(handlers) / sizeof(handler_t)))
843         DLOG("Unhandled message type: %d\n", message_type);
844     else {
845         handler_t h = handlers[message_type];
846         h(w->fd, message, 0, message_length, message_type);
847     }
848 }
849
850 /*
851  * Handler for activity on the listening socket, meaning that a new client
852  * has just connected and we should accept() him. Sets up the event handler
853  * for activity on the new connection and inserts the file descriptor into
854  * the list of clients.
855  *
856  */
857 void ipc_new_client(EV_P_ struct ev_io *w, int revents) {
858     struct sockaddr_un peer;
859     socklen_t len = sizeof(struct sockaddr_un);
860     int client;
861     if ((client = accept(w->fd, (struct sockaddr*)&peer, &len)) < 0) {
862         if (errno == EINTR)
863             return;
864         else perror("accept()");
865         return;
866     }
867
868     /* Close this file descriptor on exec() */
869     (void)fcntl(client, F_SETFD, FD_CLOEXEC);
870
871     set_nonblock(client);
872
873     struct ev_io *package = scalloc(sizeof(struct ev_io));
874     ev_io_init(package, ipc_receive_message, client, EV_READ);
875     ev_io_start(EV_A_ package);
876
877     DLOG("IPC: new client connected on fd %d\n", w->fd);
878
879     ipc_client *new = scalloc(sizeof(ipc_client));
880     new->fd = client;
881
882     TAILQ_INSERT_TAIL(&all_clients, new, clients);
883 }
884
885 /*
886  * Creates the UNIX domain socket at the given path, sets it to non-blocking
887  * mode, bind()s and listen()s on it.
888  *
889  */
890 int ipc_create_socket(const char *filename) {
891     int sockfd;
892
893     FREE(current_socketpath);
894
895     char *resolved = resolve_tilde(filename);
896     DLOG("Creating IPC-socket at %s\n", resolved);
897     char *copy = sstrdup(resolved);
898     const char *dir = dirname(copy);
899     if (!path_exists(dir))
900         mkdirp(dir);
901     free(copy);
902
903     /* Unlink the unix domain socket before */
904     unlink(resolved);
905
906     if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
907         perror("socket()");
908         free(resolved);
909         return -1;
910     }
911
912     (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
913
914     struct sockaddr_un addr;
915     memset(&addr, 0, sizeof(struct sockaddr_un));
916     addr.sun_family = AF_LOCAL;
917     strncpy(addr.sun_path, resolved, sizeof(addr.sun_path) - 1);
918     if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) {
919         perror("bind()");
920         free(resolved);
921         return -1;
922     }
923
924     set_nonblock(sockfd);
925
926     if (listen(sockfd, 5) < 0) {
927         perror("listen()");
928         free(resolved);
929         return -1;
930     }
931
932     current_socketpath = resolved;
933     return sockfd;
934 }