]> git.sur5r.net Git - i3/i3/blob - libi3/ipc_connect.c
d27e04678e0e50c511addf5b2c8639181227ed04
[i3/i3] / libi3 / ipc_connect.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  */
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <string.h>
12 #include <err.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16
17 #include "libi3.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 ipc_connect(const 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     (void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
30
31     struct sockaddr_un addr;
32     memset(&addr, 0, sizeof(struct sockaddr_un));
33     addr.sun_family = AF_LOCAL;
34     strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
35     if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
36         err(EXIT_FAILURE, "Could not connect to i3");
37
38     return sockfd;
39 }