forked from Airblader/i3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move set_nonblock, create_socket and path_exists to libi3
- Loading branch information
1 parent
e4f12ac
commit 131a615
Showing
8 changed files
with
139 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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 <unistd.h> | ||
#include <libgen.h> | ||
#include <err.h> | ||
#include <fcntl.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/socket.h> | ||
#include <sys/un.h> | ||
|
||
/* | ||
* Creates the UNIX domain socket at the given path, sets it to non-blocking | ||
* mode, bind()s and listen()s on it. | ||
* | ||
* The full path to the socket is stored in the char* that out_socketpath points | ||
* to. | ||
* | ||
*/ | ||
int create_socket(const char *filename, char **out_socketpath) { | ||
int sockfd; | ||
|
||
char *resolved = resolve_tilde(filename); | ||
DLOG("Creating UNIX socket at %s\n", resolved); | ||
char *copy = sstrdup(resolved); | ||
const char *dir = dirname(copy); | ||
if (!path_exists(dir)) | ||
mkdirp(dir, DEFAULT_DIR_MODE); | ||
free(copy); | ||
|
||
/* Unlink the unix domain socket before */ | ||
unlink(resolved); | ||
|
||
if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) { | ||
perror("socket()"); | ||
free(resolved); | ||
return -1; | ||
} | ||
|
||
(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, resolved, sizeof(addr.sun_path) - 1); | ||
if (bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) { | ||
perror("bind()"); | ||
free(resolved); | ||
return -1; | ||
} | ||
|
||
set_nonblock(sockfd); | ||
|
||
if (listen(sockfd, 5) < 0) { | ||
perror("listen()"); | ||
free(resolved); | ||
return -1; | ||
} | ||
|
||
free(*out_socketpath); | ||
*out_socketpath = resolved; | ||
return sockfd; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include "libi3.h" | ||
|
||
#include <err.h> | ||
#include <fcntl.h> | ||
|
||
/* | ||
* Puts the given socket file descriptor into non-blocking mode or dies if | ||
* setting O_NONBLOCK failed. Non-blocking sockets are a good idea for our | ||
* IPC model because we should by no means block the window manager. | ||
* | ||
*/ | ||
void set_nonblock(int sockfd) { | ||
int flags = fcntl(sockfd, F_GETFL, 0); | ||
if (flags & O_NONBLOCK) { | ||
return; | ||
} | ||
flags |= O_NONBLOCK; | ||
if (fcntl(sockfd, F_SETFL, flags) < 0) | ||
err(-1, "Could not set O_NONBLOCK"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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/stat.h> | ||
#include <unistd.h> | ||
|
||
/* | ||
* Checks if the given path exists by calling stat(). | ||
* | ||
*/ | ||
bool path_exists(const char *path) { | ||
struct stat buf; | ||
return (stat(path, &buf) == 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters