#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
+#include <libgen.h>
#include <ev.h>
#include <yajl/yajl_gen.h>
#include <yajl/yajl_parse.h>
err(-1, "Could not set O_NONBLOCK");
}
+/*
+ * Emulates mkdir -p (creates any missing folders)
+ *
+ */
+static bool mkdirp(const char *path) {
+ if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0)
+ return true;
+ if (errno != ENOENT) {
+ ELOG("mkdir(%s) failed: %s\n", path, strerror(errno));
+ return false;
+ }
+ char *copy = strdup(path);
+ /* strip trailing slashes, if any */
+ while (copy[strlen(copy)-1] == '/')
+ copy[strlen(copy)-1] = '\0';
+
+ char *sep = strrchr(copy, '/');
+ if (sep == NULL)
+ return false;
+ *sep = '\0';
+ bool result = false;
+ if (mkdirp(copy))
+ result = mkdirp(path);
+ free(copy);
+
+ return result;
+}
+
static void ipc_send_message(int fd, const unsigned char *payload,
int message_type, int message_size) {
int buffer_size = strlen("i3-ipc") + sizeof(uint32_t) +
int ipc_create_socket(const char *filename) {
int sockfd;
+ char *resolved = resolve_tilde(filename);
+ DLOG("Creating IPC-socket at %s\n", resolved);
+ char *copy = sstrdup(resolved);
+ const char *dir = dirname(copy);
+ if (!path_exists(dir))
+ mkdirp(dir);
+ free(copy);
+
/* Unlink the unix domain socket before */
unlink(filename);
if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
perror("socket()");
+ free(resolved);
return -1;
}
struct sockaddr_un addr;
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_LOCAL;
- strcpy(addr.sun_path, filename);
+ strncpy(addr.sun_path, resolved, sizeof(addr.sun_path) - 1);
if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) {
perror("bind()");
+ free(resolved);
return -1;
}
+ free(resolved);
set_nonblock(sockfd);
if (listen(sockfd, 5) < 0) {