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