2011-10-23 12:18:14 -04:00
|
|
|
/*
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2015-04-03 20:17:56 -04:00
|
|
|
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
|
2011-10-23 12:18:14 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdlib.h>
|
2013-04-14 04:12:21 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2011-10-23 12:18:14 -04:00
|
|
|
|
2011-10-23 12:38:21 -04:00
|
|
|
#include "libi3.h"
|
|
|
|
|
2011-10-23 12:18:14 -04:00
|
|
|
/*
|
|
|
|
* Connects to the i3 IPC socket and returns the file descriptor for the
|
|
|
|
* socket. die()s if anything goes wrong.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int ipc_connect(const char *socket_path) {
|
|
|
|
int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
|
|
|
|
if (sockfd == -1)
|
|
|
|
err(EXIT_FAILURE, "Could not create socket");
|
|
|
|
|
2013-04-14 04:12:21 -04:00
|
|
|
(void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
2011-10-23 12:18:14 -04:00
|
|
|
struct sockaddr_un addr;
|
|
|
|
memset(&addr, 0, sizeof(struct sockaddr_un));
|
|
|
|
addr.sun_family = AF_LOCAL;
|
|
|
|
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
|
2014-06-15 13:07:02 -04:00
|
|
|
if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
|
2011-10-23 12:18:14 -04:00
|
|
|
err(EXIT_FAILURE, "Could not connect to i3");
|
|
|
|
|
|
|
|
return sockfd;
|
|
|
|
}
|