aboutsummaryrefslogtreecommitdiff
path: root/libseat
diff options
context:
space:
mode:
authorKenny Levinsen <kl@kl.wtf>2022-02-09 23:21:18 +0100
committerKenny Levinsen <kl@kl.wtf>2022-02-09 23:22:29 +0100
commitb47c79d73123052b20bc853b544c10274ab29fa6 (patch)
tree2350f7eb88f282a6357fad054512cc7a2345ec27 /libseat
parent96a5de88595cb8ddc41a4beb72d213d1f8bfdc61 (diff)
libseat: Use SOCK_CLOEXEC and SOCK_NONBLOCK
This both simplifies our code and fixes an exec fd leak when using builtin or noop backends.
Diffstat (limited to 'libseat')
-rw-r--r--libseat/backend/noop.c2
-rw-r--r--libseat/backend/seatd.c20
2 files changed, 3 insertions, 19 deletions
diff --git a/libseat/backend/noop.c b/libseat/backend/noop.c
index 7436c48..f69aac6 100644
--- a/libseat/backend/noop.c
+++ b/libseat/backend/noop.c
@@ -109,7 +109,7 @@ static struct libseat *noop_open_seat(const struct libseat_seat_listener *listen
return NULL;
}
- if (socketpair(AF_UNIX, SOCK_STREAM, 0, backend->sockets) != 0) {
+ if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, backend->sockets) != 0) {
log_errorf("socketpair() failed: %s", strerror(errno));
free(backend);
return NULL;
diff --git a/libseat/backend/seatd.c b/libseat/backend/seatd.c
index 26308d1..6e90713 100644
--- a/libseat/backend/seatd.c
+++ b/libseat/backend/seatd.c
@@ -42,32 +42,16 @@ struct backend_seatd {
char seat_name[MAX_SEAT_LEN];
};
-static int set_nonblock(int fd) {
- int flags;
- if ((flags = fcntl(fd, F_GETFD)) == -1 || fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
- return -1;
- }
- if ((flags = fcntl(fd, F_GETFL)) == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
- return -1;
- }
- return 0;
-}
-
static int seatd_connect(void) {
union {
struct sockaddr_un unix;
struct sockaddr generic;
} addr = {{0}};
- int fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (fd == -1) {
log_errorf("Could not create socket: %s", strerror(errno));
return -1;
}
- if (set_nonblock(fd) == -1) {
- log_errorf("Could not make socket non-blocking: %s", strerror(errno));
- close(fd);
- return -1;
- }
const char *path = getenv("SEATD_SOCK");
if (path == NULL) {
path = SEATD_DEFAULTPATH;
@@ -638,7 +622,7 @@ static int set_deathsig(int signal) {
static struct libseat *builtin_open_seat(const struct libseat_seat_listener *listener, void *data) {
int fds[2];
- if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) {
+ if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, fds) == -1) {
log_errorf("Could not create socket pair: %s", strerror(errno));
return NULL;
}