2011-10-23 18:18:14 +02:00
|
|
|
/*
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2015-04-04 02:17:56 +02:00
|
|
|
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
|
2011-10-23 18:18:14 +02:00
|
|
|
*
|
|
|
|
*/
|
2016-10-11 09:13:35 +02:00
|
|
|
#include "libi3.h"
|
|
|
|
|
2011-10-23 18:18:14 +02:00
|
|
|
#include <err.h>
|
2020-04-19 09:43:48 +02:00
|
|
|
#include <fcntl.h>
|
2011-10-23 18:18:14 +02:00
|
|
|
#include <stdlib.h>
|
2020-04-19 09:43:48 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
2013-04-14 10:12:21 +02:00
|
|
|
#include <unistd.h>
|
2011-10-23 18:18:14 +02: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) {
|
2017-11-26 16:41:59 +01:00
|
|
|
char *path = NULL;
|
|
|
|
if (socket_path != NULL) {
|
|
|
|
path = sstrdup(socket_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path == NULL) {
|
|
|
|
if ((path = getenv("I3SOCK")) != NULL) {
|
|
|
|
path = sstrdup(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path == NULL) {
|
|
|
|
path = root_atom_contents("I3_SOCKET_PATH", NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path == NULL) {
|
|
|
|
path = sstrdup("/tmp/i3-ipc.sock");
|
|
|
|
}
|
|
|
|
|
2011-10-23 18:18:14 +02:00
|
|
|
int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
|
|
|
|
if (sockfd == -1)
|
|
|
|
err(EXIT_FAILURE, "Could not create socket");
|
|
|
|
|
2013-04-14 10:12:21 +02:00
|
|
|
(void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
2011-10-23 18:18:14 +02:00
|
|
|
struct sockaddr_un addr;
|
|
|
|
memset(&addr, 0, sizeof(struct sockaddr_un));
|
|
|
|
addr.sun_family = AF_LOCAL;
|
2017-11-26 16:41:59 +01:00
|
|
|
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
|
2014-06-15 19:07:02 +02:00
|
|
|
if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
|
2017-11-26 16:41:59 +01:00
|
|
|
err(EXIT_FAILURE, "Could not connect to i3 on socket %s", path);
|
|
|
|
free(path);
|
2011-10-23 18:18:14 +02:00
|
|
|
return sockfd;
|
|
|
|
}
|