summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-06-06 17:23:50 -0600
committercflip <cflip@cflip.net>2023-06-06 17:23:50 -0600
commitc90a2f7a68e3d1054bceed8b67acb7bd0af8d14f (patch)
tree4a16d3e564c5f4b432647cc8f3623c6db4650533
parent9239ee0dbee152f8ebbd12726d8bdc6d5720e2f4 (diff)
Ignore SIGPIPE signals when writing to a closed socket
-rw-r--r--cfws.c5
-rw-r--r--net.c8
2 files changed, 10 insertions, 3 deletions
diff --git a/cfws.c b/cfws.c
index 19a8d0b..5776b49 100644
--- a/cfws.c
+++ b/cfws.c
@@ -1,3 +1,4 @@
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -18,6 +19,10 @@ int main(int argc, char *argv[])
int serverfd, clientfd;
struct http_request request;
+ /* Prevent the program from quitting if it attempts to write to a closed
+ * socket. */
+ signal(SIGPIPE, SIG_IGN);
+
serverfd = net_init_server(port);
if (serverfd == -1)
return 1;
diff --git a/net.c b/net.c
index bbf80d1..84d713b 100644
--- a/net.c
+++ b/net.c
@@ -23,9 +23,11 @@ int net_init_server(int port)
return -1;
}
- /* Allow the port to be reused, prevents errors when quickly starting
- * and restarting the server. */
- /* TODO: Also use SO_REUSEPORT? */
+ /*
+ * Set SO_REUSEADDR to allow the port to be reused, preventing errors
+ * when quickly starting and restarting the server. SO_REUSEPORT and
+ * SO_NOSIGPIPE would also be nice, but they are not supported on Linux.
+ */
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopts, sizeof(int));
memset(&addr, 0, sizeof(struct sockaddr_in));