Files
.github
AnyEvent-I3
contrib
debian
docs
etc
i3-config-wizard
i3-dump-log
i3-input
i3-msg
i3-nagbar
i3bar
include
libi3
README
dpi.c
draw_util.c
fake_configure_notify.c
font.c
format_placeholders.c
g_utf8_make_valid.c
get_colorpixel.c
get_config_path.c
get_exe_path.c
get_mod_mask.c
get_process_filename.c
get_visualtype.c
ipc_connect.c
ipc_recv_message.c
ipc_send_message.c
is_debug_build.c
mkdirp.c
resolve_tilde.c
root_atom_contents.c
safewrappers.c
string.c
strndup.c
ucs2_conversion.c
m4
man
parser-specs
share
src
testcases
travis
.clang-format
.editorconfig
.gitignore
.travis.yml
DEPENDS
I3_VERSION
LICENSE
Makefile.am
PACKAGE-MAINTAINER
README.md
RELEASE-NOTES-4.16
configure.ac
generate-command-parser.pl
i3-dmenu-desktop
i3-migrate-config-to-v4
i3-save-tree
i3-sensible-editor
i3-sensible-pager
i3-sensible-terminal
logo.svg
pseudo-doc.doxygen
release.sh
i3/libi3/ipc_connect.c
2017-11-26 16:41:59 +01:00

59 lines
1.4 KiB
C

/*
* vim:ts=4:sw=4:expandtab
*
* i3 - an improved dynamic tiling window manager
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
*
*/
#include "libi3.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <err.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
/*
* 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) {
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");
}
int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (sockfd == -1)
err(EXIT_FAILURE, "Could not create socket");
(void)fcntl(sockfd, F_SETFD, FD_CLOEXEC);
struct sockaddr_un addr;
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_LOCAL;
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
err(EXIT_FAILURE, "Could not connect to i3 on socket %s", path);
free(path);
return sockfd;
}