i3
ipc.c
Go to the documentation of this file.
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  if (inplace_restart && con->window != NULL) {
358  ystr("depth");
359  y(integer, con->depth);
360  }
361 
362  y(map_close);
363 }
364 
365 IPC_HANDLER(tree) {
366  setlocale(LC_NUMERIC, "C");
367  yajl_gen gen = ygenalloc();
368  dump_node(gen, croot, false);
369  setlocale(LC_NUMERIC, "");
370 
371  const unsigned char *payload;
372  ylength length;
373  y(get_buf, &payload, &length);
374 
375  ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_TREE, payload);
376  y(free);
377 }
378 
379 
380 /*
381  * Formats the reply message for a GET_WORKSPACES request and sends it to the
382  * client
383  *
384  */
385 IPC_HANDLER(get_workspaces) {
386  yajl_gen gen = ygenalloc();
387  y(array_open);
388 
389  Con *focused_ws = con_get_workspace(focused);
390 
391  Con *output;
392  TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
393  if (con_is_internal(output))
394  continue;
395  Con *ws;
396  TAILQ_FOREACH(ws, &(output_get_content(output)->nodes_head), nodes) {
397  assert(ws->type == CT_WORKSPACE);
398  y(map_open);
399 
400  ystr("num");
401  if (ws->num == -1)
402  y(null);
403  else y(integer, ws->num);
404 
405  ystr("name");
406  ystr(ws->name);
407 
408  ystr("visible");
409  y(bool, workspace_is_visible(ws));
410 
411  ystr("focused");
412  y(bool, ws == focused_ws);
413 
414  ystr("rect");
415  y(map_open);
416  ystr("x");
417  y(integer, ws->rect.x);
418  ystr("y");
419  y(integer, ws->rect.y);
420  ystr("width");
421  y(integer, ws->rect.width);
422  ystr("height");
423  y(integer, ws->rect.height);
424  y(map_close);
425 
426  ystr("output");
427  ystr(output->name);
428 
429  ystr("urgent");
430  y(bool, ws->urgent);
431 
432  y(map_close);
433  }
434  }
435 
436  y(array_close);
437 
438  const unsigned char *payload;
439  ylength length;
440  y(get_buf, &payload, &length);
441 
442  ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_WORKSPACES, payload);
443  y(free);
444 }
445 
446 /*
447  * Formats the reply message for a GET_OUTPUTS request and sends it to the
448  * client
449  *
450  */
451 IPC_HANDLER(get_outputs) {
452  yajl_gen gen = ygenalloc();
453  y(array_open);
454 
455  Output *output;
456  TAILQ_FOREACH(output, &outputs, outputs) {
457  y(map_open);
458 
459  ystr("name");
460  ystr(output->name);
461 
462  ystr("active");
463  y(bool, output->active);
464 
465  ystr("primary");
466  y(bool, output->primary);
467 
468  ystr("rect");
469  y(map_open);
470  ystr("x");
471  y(integer, output->rect.x);
472  ystr("y");
473  y(integer, output->rect.y);
474  ystr("width");
475  y(integer, output->rect.width);
476  ystr("height");
477  y(integer, output->rect.height);
478  y(map_close);
479 
480  ystr("current_workspace");
481  Con *ws = NULL;
482  if (output->con && (ws = con_get_fullscreen_con(output->con, CF_OUTPUT)))
483  ystr(ws->name);
484  else y(null);
485 
486  y(map_close);
487  }
488 
489  y(array_close);
490 
491  const unsigned char *payload;
492  ylength length;
493  y(get_buf, &payload, &length);
494 
495  ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_OUTPUTS, payload);
496  y(free);
497 }
498 
499 /*
500  * Formats the reply message for a GET_MARKS request and sends it to the
501  * client
502  *
503  */
504 IPC_HANDLER(get_marks) {
505  yajl_gen gen = ygenalloc();
506  y(array_open);
507 
508  Con *con;
510  if (con->mark != NULL)
511  ystr(con->mark);
512 
513  y(array_close);
514 
515  const unsigned char *payload;
516  ylength length;
517  y(get_buf, &payload, &length);
518 
519  ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_MARKS, payload);
520  y(free);
521 }
522 
523 /*
524  * Returns the version of i3
525  *
526  */
527 IPC_HANDLER(get_version) {
528  yajl_gen gen = ygenalloc();
529  y(map_open);
530 
531  ystr("major");
532  y(integer, MAJOR_VERSION);
533 
534  ystr("minor");
535  y(integer, MINOR_VERSION);
536 
537  ystr("patch");
538  y(integer, PATCH_VERSION);
539 
540  ystr("human_readable");
541  ystr(I3_VERSION);
542 
543  y(map_close);
544 
545  const unsigned char *payload;
546  ylength length;
547  y(get_buf, &payload, &length);
548 
549  ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_VERSION, payload);
550  y(free);
551 }
552 
553 /*
554  * Formats the reply message for a GET_BAR_CONFIG request and sends it to the
555  * client.
556  *
557  */
558 IPC_HANDLER(get_bar_config) {
559  yajl_gen gen = ygenalloc();
560 
561  /* If no ID was passed, we return a JSON array with all IDs */
562  if (message_size == 0) {
563  y(array_open);
564  Barconfig *current;
565  TAILQ_FOREACH(current, &barconfigs, configs) {
566  ystr(current->id);
567  }
568  y(array_close);
569 
570  const unsigned char *payload;
571  ylength length;
572  y(get_buf, &payload, &length);
573 
574  ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
575  y(free);
576  return;
577  }
578 
579  /* To get a properly terminated buffer, we copy
580  * message_size bytes out of the buffer */
581  char *bar_id = scalloc(message_size + 1);
582  strncpy(bar_id, (const char*)message, message_size);
583  LOG("IPC: looking for config for bar ID \"%s\"\n", bar_id);
584  Barconfig *current, *config = NULL;
585  TAILQ_FOREACH(current, &barconfigs, configs) {
586  if (strcmp(current->id, bar_id) != 0)
587  continue;
588 
589  config = current;
590  break;
591  }
592 
593  y(map_open);
594 
595  if (!config) {
596  /* If we did not find a config for the given ID, the reply will contain
597  * a null 'id' field. */
598  ystr("id");
599  y(null);
600  } else {
601  ystr("id");
602  ystr(config->id);
603 
604  if (config->num_outputs > 0) {
605  ystr("outputs");
606  y(array_open);
607  for (int c = 0; c < config->num_outputs; c++)
608  ystr(config->outputs[c]);
609  y(array_close);
610  }
611 
612 #define YSTR_IF_SET(name) \
613  do { \
614  if (config->name) { \
615  ystr( # name); \
616  ystr(config->name); \
617  } \
618  } while (0)
619 
620  YSTR_IF_SET(tray_output);
621  YSTR_IF_SET(socket_path);
622 
623  ystr("mode");
624  switch (config->mode) {
625  case M_HIDE:
626  ystr("hide");
627  break;
628  case M_INVISIBLE:
629  ystr("invisible");
630  break;
631  case M_DOCK:
632  default:
633  ystr("dock");
634  break;
635  }
636 
637  ystr("hidden_state");
638  switch (config->hidden_state) {
639  case S_SHOW:
640  ystr("show");
641  break;
642  case S_HIDE:
643  default:
644  ystr("hide");
645  break;
646  }
647 
648  ystr("modifier");
649  switch (config->modifier) {
650  case M_CONTROL:
651  ystr("ctrl");
652  break;
653  case M_SHIFT:
654  ystr("shift");
655  break;
656  case M_MOD1:
657  ystr("Mod1");
658  break;
659  case M_MOD2:
660  ystr("Mod2");
661  break;
662  case M_MOD3:
663  ystr("Mod3");
664  break;
665  /*
666  case M_MOD4:
667  ystr("Mod4");
668  break;
669  */
670  case M_MOD5:
671  ystr("Mod5");
672  break;
673  default:
674  ystr("Mod4");
675  break;
676  }
677 
678  ystr("position");
679  if (config->position == P_BOTTOM)
680  ystr("bottom");
681  else ystr("top");
682 
683  YSTR_IF_SET(status_command);
684  YSTR_IF_SET(font);
685 
686  ystr("workspace_buttons");
687  y(bool, !config->hide_workspace_buttons);
688 
689  ystr("binding_mode_indicator");
690  y(bool, !config->hide_binding_mode_indicator);
691 
692  ystr("verbose");
693  y(bool, config->verbose);
694 
695 #undef YSTR_IF_SET
696 #define YSTR_IF_SET(name) \
697  do { \
698  if (config->colors.name) { \
699  ystr( # name); \
700  ystr(config->colors.name); \
701  } \
702  } while (0)
703 
704  ystr("colors");
705  y(map_open);
706  YSTR_IF_SET(background);
707  YSTR_IF_SET(statusline);
708  YSTR_IF_SET(separator);
709  YSTR_IF_SET(focused_workspace_border);
710  YSTR_IF_SET(focused_workspace_bg);
711  YSTR_IF_SET(focused_workspace_text);
712  YSTR_IF_SET(active_workspace_border);
713  YSTR_IF_SET(active_workspace_bg);
714  YSTR_IF_SET(active_workspace_text);
715  YSTR_IF_SET(inactive_workspace_border);
716  YSTR_IF_SET(inactive_workspace_bg);
717  YSTR_IF_SET(inactive_workspace_text);
718  YSTR_IF_SET(urgent_workspace_border);
719  YSTR_IF_SET(urgent_workspace_bg);
720  YSTR_IF_SET(urgent_workspace_text);
721  y(map_close);
722 
723 #undef YSTR_IF_SET
724  }
725 
726  y(map_close);
727 
728  const unsigned char *payload;
729  ylength length;
730  y(get_buf, &payload, &length);
731 
732  ipc_send_message(fd, length, I3_IPC_REPLY_TYPE_BAR_CONFIG, payload);
733  y(free);
734 }
735 
736 /*
737  * Callback for the YAJL parser (will be called when a string is parsed).
738  *
739  */
740 static int add_subscription(void *extra, const unsigned char *s,
741  ylength len) {
742  ipc_client *client = extra;
743 
744  DLOG("should add subscription to extra %p, sub %.*s\n", client, (int)len, s);
745  int event = client->num_events;
746 
747  client->num_events++;
748  client->events = realloc(client->events, client->num_events * sizeof(char*));
749  /* We copy the string because it is not null-terminated and strndup()
750  * is missing on some BSD systems */
751  client->events[event] = scalloc(len+1);
752  memcpy(client->events[event], s, len);
753 
754  DLOG("client is now subscribed to:\n");
755  for (int i = 0; i < client->num_events; i++)
756  DLOG("event %s\n", client->events[i]);
757  DLOG("(done)\n");
758 
759  return 1;
760 }
761 
762 /*
763  * Subscribes this connection to the event types which were given as a JSON
764  * serialized array in the payload field of the message.
765  *
766  */
767 IPC_HANDLER(subscribe) {
768  yajl_handle p;
769  yajl_callbacks callbacks;
770  yajl_status stat;
771  ipc_client *current, *client = NULL;
772 
773  /* Search the ipc_client structure for this connection */
774  TAILQ_FOREACH(current, &all_clients, clients) {
775  if (current->fd != fd)
776  continue;
777 
778  client = current;
779  break;
780  }
781 
782  if (client == NULL) {
783  ELOG("Could not find ipc_client data structure for fd %d\n", fd);
784  return;
785  }
786 
787  /* Setup the JSON parser */
788  memset(&callbacks, 0, sizeof(yajl_callbacks));
789  callbacks.yajl_string = add_subscription;
790 
791  p = yalloc(&callbacks, (void*)client);
792  stat = yajl_parse(p, (const unsigned char*)message, message_size);
793  if (stat != yajl_status_ok) {
794  unsigned char *err;
795  err = yajl_get_error(p, true, (const unsigned char*)message,
796  message_size);
797  ELOG("YAJL parse error: %s\n", err);
798  yajl_free_error(p, err);
799 
800  const char *reply = "{\"success\":false}";
801  ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t*)reply);
802  yajl_free(p);
803  return;
804  }
805  yajl_free(p);
806  const char *reply = "{\"success\":true}";
807  ipc_send_message(fd, strlen(reply), I3_IPC_REPLY_TYPE_SUBSCRIBE, (const uint8_t*)reply);
808 }
809 
810 /* The index of each callback function corresponds to the numeric
811  * value of the message type (see include/i3/ipc.h) */
813  handle_command,
814  handle_get_workspaces,
815  handle_subscribe,
816  handle_get_outputs,
817  handle_tree,
818  handle_get_marks,
819  handle_get_bar_config,
820  handle_get_version,
821 };
822 
823 /*
824  * Handler for activity on a client connection, receives a message from a
825  * client.
826  *
827  * For now, the maximum message size is 2048. I’m not sure for what the
828  * IPC interface will be used in the future, thus I’m not implementing a
829  * mechanism for arbitrarily long messages, as it seems like overkill
830  * at the moment.
831  *
832  */
833 static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) {
834  uint32_t message_type;
835  uint32_t message_length;
836  uint8_t *message;
837 
838  int ret = ipc_recv_message(w->fd, &message_type, &message_length, &message);
839  /* EOF or other error */
840  if (ret < 0) {
841  /* Was this a spurious read? See ev(3) */
842  if (ret == -1 && errno == EAGAIN)
843  return;
844 
845  /* If not, there was some kind of error. We don’t bother
846  * and close the connection */
847  close(w->fd);
848 
849  /* Delete the client from the list of clients */
850  ipc_client *current;
851  TAILQ_FOREACH(current, &all_clients, clients) {
852  if (current->fd != w->fd)
853  continue;
854 
855  for (int i = 0; i < current->num_events; i++)
856  free(current->events[i]);
857  /* We can call TAILQ_REMOVE because we break out of the
858  * TAILQ_FOREACH afterwards */
859  TAILQ_REMOVE(&all_clients, current, clients);
860  free(current);
861  break;
862  }
863 
864  ev_io_stop(EV_A_ w);
865  free(w);
866 
867  DLOG("IPC: client disconnected\n");
868  return;
869  }
870 
871  if (message_type >= (sizeof(handlers) / sizeof(handler_t)))
872  DLOG("Unhandled message type: %d\n", message_type);
873  else {
874  handler_t h = handlers[message_type];
875  h(w->fd, message, 0, message_length, message_type);
876  }
877 }
878 
879 /*
880  * Handler for activity on the listening socket, meaning that a new client
881  * has just connected and we should accept() him. Sets up the event handler
882  * for activity on the new connection and inserts the file descriptor into
883  * the list of clients.
884  *
885  */
886 void ipc_new_client(EV_P_ struct ev_io *w, int revents) {
887  struct sockaddr_un peer;
888  socklen_t len = sizeof(struct sockaddr_un);
889  int client;
890  if ((client = accept(w->fd, (struct sockaddr*)&peer, &len)) < 0) {
891  if (errno == EINTR)
892  return;
893  else perror("accept()");
894  return;
895  }
896 
897  /* Close this file descriptor on exec() */
898  (void)fcntl(client, F_SETFD, FD_CLOEXEC);
899 
900  set_nonblock(client);
901 
902  struct ev_io *package = scalloc(sizeof(struct ev_io));
903  ev_io_init(package, ipc_receive_message, client, EV_READ);
904  ev_io_start(EV_A_ package);
905 
906  DLOG("IPC: new client connected on fd %d\n", w->fd);
907 
908  ipc_client *new = scalloc(sizeof(ipc_client));
909  new->fd = client;
910 
911  TAILQ_INSERT_TAIL(&all_clients, new, clients);
912 }
913 
914 /*
915  * Creates the UNIX domain socket at the given path, sets it to non-blocking
916  * mode, bind()s and listen()s on it.
917  *
918  */
919 int ipc_create_socket(const char *filename) {
920  int sockfd;
921 
923 
924  char *resolved = resolve_tilde(filename);
925  DLOG("Creating IPC-socket at %s\n", resolved);
926  char *copy = sstrdup(resolved);
927  const char *dir = dirname(copy);
928  if (!path_exists(dir))
929  mkdirp(dir);
930  free(copy);
931 
932  /* Unlink the unix domain socket before */
933  unlink(resolved);
934 
935  if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
936  perror("socket()");
937  free(resolved);
938  return -1;
939  }
940 
941  (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
942 
943  struct sockaddr_un addr;
944  memset(&addr, 0, sizeof(struct sockaddr_un));
945  addr.sun_family = AF_LOCAL;
946  strncpy(addr.sun_path, resolved, sizeof(addr.sun_path) - 1);
947  if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) {
948  perror("bind()");
949  free(resolved);
950  return -1;
951  }
952 
953  set_nonblock(sockfd);
954 
955  if (listen(sockfd, 5) < 0) {
956  perror("listen()");
957  free(resolved);
958  return -1;
959  }
960 
961  current_socketpath = resolved;
962  return sockfd;
963 }
964 
965 /*
966  * For the workspace "focus" event we send, along the usual "change" field,
967  * also the current and previous workspace, in "current" and "old"
968  * respectively.
969  */
970 void ipc_send_workspace_focus_event(Con *current, Con *old) {
971  setlocale(LC_NUMERIC, "C");
972  yajl_gen gen = ygenalloc();
973 
974  y(map_open);
975 
976  ystr("change");
977  ystr("focus");
978 
979  ystr("current");
980  dump_node(gen, current, false);
981 
982  ystr("old");
983  if (old == NULL)
984  y(null);
985  else
986  dump_node(gen, old, false);
987 
988  y(map_close);
989 
990  const unsigned char *payload;
991  ylength length;
992  y(get_buf, &payload, &length);
993 
994  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
995  y(free);
996  setlocale(LC_NUMERIC, "");
997 }