]> git.sur5r.net Git - i3/i3/blob - i3-config-wizard/ipc.c
i3-config-wizard: use IPC functions from libi3
[i3/i3] / i3-config-wizard / ipc.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <stdint.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <sys/socket.h>
15 #include <sys/un.h>
16 #include <unistd.h>
17 #include <err.h>
18
19 /*
20  * Connects to the i3 IPC socket and returns the file descriptor for the
21  * socket. die()s if anything goes wrong.
22  *
23  */
24 int connect_ipc(char *socket_path) {
25         int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
26         if (sockfd == -1)
27                 err(EXIT_FAILURE, "Could not create socket");
28
29         struct sockaddr_un addr;
30         memset(&addr, 0, sizeof(struct sockaddr_un));
31         addr.sun_family = AF_LOCAL;
32         strcpy(addr.sun_path, socket_path);
33         if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
34                 err(EXIT_FAILURE, "Could not connect to i3");
35
36         return sockfd;
37 }