Commit Diff
Diff:
8307a7c8a20d43ed32440d3a15197e4f5a804022
ee0aac2f009a841246854bc189070f07803d0d2e
Commit:
ee0aac2f009a841246854bc189070f07803d0d2e
Tree:
af67d21052326e73d1dfe93e98c515380165b96c
Author:
Omar Polo <op@omarpolo.com>
Date:
Wed May 25 18:03:07 2022 UTC
Message:
work around macos lack of SOCK_CLOEXEC / SOCK_NONBLOCK

rework mark_nonblock so it sets the cloexec flag too and use it in
control.c to avoid these flags. (which are expected to become available
on a future revision of POSIX and are already widely available.)

It's not an issue for telescope to do the socket/accept + fcntl dance
because there aren't threads that can fork(2) (there are no threads at
all!)

reported by @sikmir at github

https://github.com/omar-polo/telescope/commit/59ef79dd19611c7846b00427e6f2267c748ae290#r74498414
commit - 8307a7c8a20d43ed32440d3a15197e4f5a804022
commit + ee0aac2f009a841246854bc189070f07803d0d2e
blob - 10acae1b1d3ecb0e2c6fac34c7fd1f50b483b38b
blob + b2cba8137f48f2e155021927aea8685178e2d619
--- control.c
+++ control.c
@@ -61,12 +61,16 @@ control_init(char *path)
int fd;
mode_t old_umask;
- if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
- 0)) == -1) {
+ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
warn("%s: socket", __func__);
return (-1);
}
+ if (!mark_nonblock_cloexec(fd)) {
+ close(fd);
+ return (-1);
+ }
+
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
@@ -127,8 +131,7 @@ control_accept(int listenfd, short event, void *bula)
return;
len = sizeof(sun);
- if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
- SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
+ if ((connfd = accept(listenfd, (struct sockaddr *)&sun, &len)) == -1) {
/*
* Pause accept if we are out of file descriptors, or
* libevent will haunt us here too.
@@ -144,6 +147,13 @@ control_accept(int listenfd, short event, void *bula)
return;
}
+ if (!mark_nonblock_cloexec(connfd)) {
+ message("%s: mark_nonblock_cloexec: %s", __func__,
+ strerror(errno));
+ close(connfd);
+ return;
+ }
+
if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
message("%s: calloc: %s", __func__, strerror(errno));
close(connfd);
blob - faa6df63aa90f545848a3d44d2f14cdba3239fd0
blob + 588673966051ca334bb6556f4e9054308ee8bc64
--- include/utils.h
+++ include/utils.h
@@ -17,7 +17,7 @@ int mark_nonblock(int);
#ifndef UTILS_H
#define UTILS_H
-int mark_nonblock(int);
+int mark_nonblock_cloexec(int);
int has_suffix(const char *, const char *);
int unicode_isspace(uint32_t);
blob - 35e3674d618b8b5ad01dd54d45f883e9fde01359
blob + 48ce206255dcef21d57c0556dcd570f965041aea
--- net.c
+++ net.c
@@ -173,7 +173,7 @@ again:
req->p = req->p->ai_next;
goto again;
} else {
- mark_nonblock(req->fd);
+ mark_nonblock_cloexec(req->fd);
if (connect(req->fd, req->p->ai_addr, req->p->ai_addrlen) == 0)
goto done;
yield_w(req, try_to_connect, NULL);
blob - d36b92c69e0f2fb8092ed347f7bc3ccab891c9c3
blob + a78de2d3db7f7b2d79d10c46adc1229c634d5ca7
--- utils.c
+++ utils.c
@@ -27,7 +27,7 @@ mark_nonblock(int fd)
#include "utils.h"
int
-mark_nonblock(int fd)
+mark_nonblock_cloexec(int fd)
{
int flags;
@@ -35,6 +35,8 @@ mark_nonblock(int fd)
return 0;
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
return 0;
+ if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
+ return 0;
return 1;
}
Omar Polo