summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeonardo Hernández Hernández <leohdz172@proton.me>2024-06-27 13:19:16 -0600
committerLeonardo Hernández Hernández <leohdz172@proton.me>2024-07-01 20:40:54 -0600
commit71f11e6cf63289d51f152469a0da81a85fe2608c (patch)
treec5b63e1836cfaf5289bcdc2938cac15d2e09e3f4
parent2b4893a0ad57fb5234c48615a2e531401efcf69c (diff)
set O_NONBLOCK flag to stdout
-rw-r--r--dwl.c6
-rw-r--r--util.c16
-rw-r--r--util.h1
3 files changed, 23 insertions, 0 deletions
diff --git a/dwl.c b/dwl.c
index 9fb50a7..3175e79 100644
--- a/dwl.c
+++ b/dwl.c
@@ -2190,6 +2190,12 @@ run(char *startup_cmd)
close(piperw[1]);
close(piperw[0]);
}
+
+ /* Mark stdout as non-blocking to avoid people who does not close stdin
+ * nor consumes it in their startup script getting dwl frozen */
+ if (fd_set_nonblock(STDOUT_FILENO) < 0)
+ close(STDOUT_FILENO);
+
printstatus();
/* At this point the outputs are initialized, choose initial selmon based on
diff --git a/util.c b/util.c
index cca7c19..51130af 100644
--- a/util.c
+++ b/util.c
@@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <fcntl.h>
#include "util.h"
@@ -33,3 +34,18 @@ ecalloc(size_t nmemb, size_t size)
die("calloc:");
return p;
}
+
+int
+fd_set_nonblock(int fd) {
+ int flags = fcntl(fd, F_GETFL);
+ if (flags < 0) {
+ perror("fcntl(F_GETFL):");
+ return -1;
+ }
+ if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
+ perror("fcntl(F_SETFL):");
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/util.h b/util.h
index 4c94117..226980d 100644
--- a/util.h
+++ b/util.h
@@ -2,3 +2,4 @@
void die(const char *fmt, ...);
void *ecalloc(size_t nmemb, size_t size);
+int fd_set_nonblock(int fd);