]> git.sur5r.net Git - i3/i3/blob - libi3/ipc_connect.c
normalize file headers across **/*.{h,c}
[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-2011 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
15 #include "libi3.h"
16
17 /*
18  * Connects to the i3 IPC socket and returns the file descriptor for the
19  * socket. die()s if anything goes wrong.
20  *
21  */
22 int ipc_connect(const char *socket_path) {
23     int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
24     if (sockfd == -1)
25         err(EXIT_FAILURE, "Could not create socket");
26
27     struct sockaddr_un addr;
28     memset(&addr, 0, sizeof(struct sockaddr_un));
29     addr.sun_family = AF_LOCAL;
30     strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
31     if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
32         err(EXIT_FAILURE, "Could not connect to i3");
33
34     return sockfd;
35 }