Avoid SOCK_CLOEXEC on Darwin

Signed-off-by: Torrekie Gen <me@torrekie.dev>
This commit is contained in:
Torrekie 2024-04-20 18:27:23 +08:00
parent 91484e380f
commit 3c6a88253c
8 changed files with 155 additions and 8 deletions

View file

@ -97,7 +97,13 @@ TEST(client_destroy_listener)
bool user_data_destroyed = false;
int s[2];
#ifdef SOCK_CLOEXEC
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
#else
assert(socketpair(AF_UNIX, SOCK_STREAM, 0, s) == 0);
assert(set_cloexec_or_close(s[0]) != -1);
assert(set_cloexec_or_close(s[1]) != -1);
#endif
display = wl_display_create();
assert(display);
client = wl_client_create(display, s[0]);
@ -184,7 +190,13 @@ TEST(client_destroy_removes_link)
struct client_destroy_listener destroy_listener;
int s[2];
#ifdef SOCK_CLOEXEC
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
#else
assert(socketpair(AF_UNIX, SOCK_STREAM, 0, s) == 0);
assert(set_cloexec_or_close(s[0]) != -1);
assert(set_cloexec_or_close(s[1]) != -1);
#endif
display = wl_display_create();
assert(display);
client = wl_client_create(display, s[0]);

View file

@ -48,7 +48,13 @@ setup(int *s)
{
struct wl_connection *connection;
#ifdef SOCK_CLOEXEC
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
#else
assert(socketpair(AF_UNIX, SOCK_STREAM, 0, s) == 0);
assert(set_cloexec_or_close(s[0]) != -1);
assert(set_cloexec_or_close(s[1]) != -1);
#endif
connection = wl_connection_create(s[0], WL_BUFFER_DEFAULT_MAX_SIZE);
assert(connection);
@ -181,8 +187,14 @@ struct marshal_data {
static void
setup_marshal_data(struct marshal_data *data)
{
#ifdef SOCK_CLOEXEC
assert(socketpair(AF_UNIX,
SOCK_STREAM | SOCK_CLOEXEC, 0, data->s) == 0);
#else
assert(socketpair(AF_UNIX, SOCK_STREAM , 0, data->s) == 0);
assert(set_cloexec_or_close(data->s[0]) != -1);
assert(set_cloexec_or_close(data->s[1]) != -1);
#endif
data->read_connection = wl_connection_create(data->s[0],
WL_BUFFER_DEFAULT_MAX_SIZE);
assert(data->read_connection);
@ -885,7 +897,13 @@ TEST(request_bogus_size)
for (bogus_size = 11; bogus_size >= 0; bogus_size--) {
fprintf(stderr, "* bogus size %d\n", bogus_size);
#ifdef SOCK_CLOEXEC
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
#else
assert(socketpair(AF_UNIX, SOCK_STREAM, 0, s) == 0);
assert(set_cloexec_or_close(s[0]) != -1);
assert(set_cloexec_or_close(s[1]) != -1);
#endif
display = wl_display_create();
assert(display);
client = wl_client_create(display, s[0]);

View file

@ -23,6 +23,8 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/* This test should be skipped on Darwin (as of 2024) */
#ifndef __APPLE__
#include "../config.h"
#define _GNU_SOURCE
@ -387,5 +389,5 @@ TEST(os_wrappers_epoll_create_cloexec_fallback)
init_fallbacks(1);
do_os_wrappers_epoll_create_cloexec(2);
}
#endif /* __APPLE__ */
/* FIXME: add tests for wl_os_accept_cloexec() */

View file

@ -40,7 +40,14 @@ TEST(create_resource_tst)
int s[2];
uint32_t id;
#ifdef SOCK_CLOEXEC
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
#else
assert(socketpair(AF_UNIX, SOCK_STREAM, 0, s) == 0);
assert(set_cloexec_or_close(s[0]) != -1);
assert(set_cloexec_or_close(s[1]) != -1);
#endif
display = wl_display_create();
assert(display);
client = wl_client_create(display, s[0]);
@ -111,7 +118,13 @@ TEST(destroy_res_tst)
.notify = &destroy_notify
};
#ifdef SOCK_CLOEXEC
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
#else
assert(socketpair(AF_UNIX, SOCK_STREAM, 0, s) == 0);
assert(set_cloexec_or_close(s[0]) != -1);
assert(set_cloexec_or_close(s[1]) != -1);
#endif
display = wl_display_create();
assert(display);
client = wl_client_create(display, s[0]);
@ -159,7 +172,13 @@ TEST(create_resource_with_same_id)
int s[2];
uint32_t id;
#ifdef SOCK_CLOEXEC
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
#else
assert(socketpair(AF_UNIX, SOCK_STREAM, 0, s) == 0);
assert(set_cloexec_or_close(s[0]) != -1);
assert(set_cloexec_or_close(s[1]) != -1);
#endif
display = wl_display_create();
assert(display);
client = wl_client_create(display, s[0]);
@ -243,7 +262,13 @@ TEST(resource_destroy_iteration)
.notify = &resource_destroy_notify
};
#ifdef SOCK_CLOEXEC
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
#else
assert(socketpair(AF_UNIX, SOCK_STREAM, 0, s) == 0);
assert(set_cloexec_or_close(s[0]) != -1);
assert(set_cloexec_or_close(s[1]) != -1);
#endif
display = wl_display_create();
assert(display);
client = wl_client_create(display, s[0]);

View file

@ -88,6 +88,30 @@ count_open_fds(void)
/* return the current number of entries */
return size / sizeof(struct kinfo_file);
}
#elif defined(__APPLE__)
#include <libproc.h>
/*
* On Darwin, use libproc API to get fds of a PID
*/
int
count_open_fds(void)
{
int buffer_size, buffer_used;
pid_t pid = getpid();
int nfds;
struct proc_fdinfo *fdinfo;
buffer_size = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, 0, 0);
fdinfo = malloc(buffer_size);
buffer_used = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, fdinfo, buffer_size);
assert(buffer_used > 0 && "proc_pidinfo PROC_PIDLISTFDS failed.");
nfds = buffer_used / PROC_PIDLISTFD_SIZE;
free(fdinfo);
return nfds;
}
#else
int
count_open_fds(void)

View file

@ -63,7 +63,12 @@ static int timeouts_enabled = 1;
/* set to one if the output goes to the terminal */
static int is_atty = 0;
#if __APPLE__
extern const struct test __start_test_section __asm("section$start$__DATA$test_section");
extern const struct test __stop_test_section __asm("section$end$__DATA$test_section");
#else
extern const struct test __start_test_section, __stop_test_section;
#endif
static const struct test *
find_test(const char *name)
@ -308,6 +313,23 @@ is_debugger_attached(void)
return rc;
}
#elif defined(__APPLE__)
#include <sys/sysctl.h>
/* https://stackoverflow.com/a/2200786 */
static int
is_debugger_attached(void)
{
int ret;
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
struct kinfo_proc info;
size_t size;
info.kp_proc.p_flag = 0;
ret = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
assert(ret == 0);
return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
}
#else
static int
is_debugger_attached(void)

View file

@ -37,11 +37,17 @@ struct test {
int must_fail;
} __attribute__ ((aligned (16)));
#if __APPLE__
#define TEST_SECTION "__DATA,test_section"
#else
#define TEST_SECTION "test_section"
#endif
#define TEST(name) \
static void name(void); \
\
const struct test test##name \
__attribute__ ((used, section ("test_section"))) = { \
__attribute__ ((used, section (TEST_SECTION))) = { \
#name, name, 0 \
}; \
\
@ -51,7 +57,7 @@ struct test {
static void name(void); \
\
const struct test test##name \
__attribute__ ((used, section ("test_section"))) = { \
__attribute__ ((used, section (TEST_SECTION))) = { \
#name, name, 1 \
}; \
\
@ -93,3 +99,28 @@ test_disable_coredumps(void);
} while (0);
#endif
/* For systems without SOCK_CLOEXEC */
#include <fcntl.h>
__attribute__((used))
static int
set_cloexec_or_close(int fd)
{
long flags;
if (fd == -1)
return -1;
flags = fcntl(fd, F_GETFD);
if (flags == -1)
goto err;
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
goto err;
return fd;
err:
close(fd);
return -1;
}