Skip to content

Commit

Permalink
Merge pull request swiftlang#408 from adierking/fork
Browse files Browse the repository at this point in the history
tests: fall back to fork() if posix_spawnp() is not available
  • Loading branch information
ktopley-apple authored Nov 6, 2018
2 parents 30eeb14 + e5fd9cc commit 6a5c6d8
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ check_function_exists(mach_approximate_time HAVE_MACH_APPROXIMATE_TIME)
check_function_exists(mach_port_construct HAVE_MACH_PORT_CONSTRUCT)
check_function_exists(malloc_create_zone HAVE_MALLOC_CREATE_ZONE)
check_function_exists(posix_fadvise HAVE_POSIX_FADVISE)
check_function_exists(posix_spawnp HAVE_POSIX_SPAWNP)
check_function_exists(pthread_key_init_np HAVE_PTHREAD_KEY_INIT_NP)
check_function_exists(pthread_main_np HAVE_PTHREAD_MAIN_NP)
check_function_exists(pthread_workqueue_setdispatch_np HAVE_PTHREAD_WORKQUEUE_SETDISPATCH_NP)
Expand Down
3 changes: 3 additions & 0 deletions cmake/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@
/* Define to 1 if you have the `posix_fadvise' function. */
#cmakedefine HAVE_POSIX_FADVISE

/* Define to 1 if you have the `posix_spawnp' function. */
#cmakedefine HAVE_POSIX_SPAWNP

/* Define to 1 if you have the `pthread_key_init_np' function. */
#cmakedefine HAVE_PTHREAD_KEY_INIT_NP

Expand Down
5 changes: 4 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ target_include_directories(bsdtests
PRIVATE
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
${PROJECT_SOURCE_DIR})
${PROJECT_SOURCE_DIR}
PUBLIC
# bsdtests.h needs config_ac.h
${PROJECT_BINARY_DIR})
if(BSD_OVERLAY_FOUND)
target_compile_options(bsdtests
PRIVATE
Expand Down
27 changes: 23 additions & 4 deletions tests/bsdtestharness.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ main(int argc, char *argv[])
exit(1);
}

#ifdef HAVE_POSIX_SPAWNP
short spawnflags = 0;
#ifdef __APPLE__
short spawnflags = POSIX_SPAWN_START_SUSPENDED;
spawnflags |= POSIX_SPAWN_START_SUSPENDED;
#if TARGET_OS_EMBEDDED
spawnflags |= POSIX_SPAWN_SETEXEC;
#endif
#else
#define POSIX_SPAWN_SETEXEC 0 /* ignore... */
short spawnflags = 0;
#endif

posix_spawnattr_t attr;
res = posix_spawnattr_init(&attr);
assert(res == 0);
res = posix_spawnattr_setflags(&attr, spawnflags);
assert(res == 0);
#endif

uint64_t to = 0;
char *tos = getenv("BSDTEST_TIMEOUT");
Expand Down Expand Up @@ -104,9 +104,12 @@ main(int argc, char *argv[])
struct timeval tv_start;
gettimeofday(&tv_start, NULL);

#ifdef HAVE_POSIX_SPAWNP
#ifdef __APPLE__
if (spawnflags & POSIX_SPAWN_SETEXEC) {
pid = fork();
}
#endif
if (!pid) {
res = posix_spawnp(&pid, newargv[0], NULL, &attr, newargv, environ);
if (res) {
Expand All @@ -115,6 +118,22 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
}
#elif defined(__unix__)
(void)res;
pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
if (execve(newargv[0], newargv, environ) == -1) {
perror(newargv[0]);
_Exit(EXIT_FAILURE);
}
}
#else
#error "bsdtestharness not implemented on this platform"
#endif

//fprintf(stderr, "pid = %d\n", pid);
assert(pid > 0);
Expand Down
8 changes: 8 additions & 0 deletions tests/bsdtests.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
#ifndef __BSD_TEST_H__
#define __BSD_TEST_H__

#if defined(HAVE_CONFIG_H)
#if __has_include(<config/config_ac.h>)
#include <config/config_ac.h>
#else
#include <config/config.h>
#endif
#endif


#if !HAVE_PRINTFLIKE
#ifndef __printflike
Expand Down
16 changes: 16 additions & 0 deletions tests/dispatch_io_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,28 @@ main(int argc, char** argv)
arguments[1] = port_str;
arguments[2] = NULL;

#ifdef HAVE_POSIX_SPAWNP
int error;
if ((error = posix_spawnp(&clientid, exec_filename, NULL, NULL,
arguments, environ)) != 0) {
test_errno("Server-posix_spawnp()", error, 0);
test_stop();
}
#elif defined(__unix__)
clientid = fork();
if (clientid == -1) {
test_errno("Server-fork()", errno, 0);
test_stop();
} else if (clientid == 0) {
// Child process
if (execve(exec_filename, arguments, environ) == -1) {
perror(exec_filename);
_Exit(EXIT_FAILURE);
}
}
#else
#error "dispatch_io_net not implemented on this platform"
#endif

addr2len = sizeof(struct sockaddr_in);
clientfd = accept(sockfd, (struct sockaddr *)&addr2, &addr2len);
Expand Down

0 comments on commit 6a5c6d8

Please sign in to comment.