Skip to content

Commit c102d20

Browse files
committed
android: add a modified FreeBSD posix_spawn
1 parent c7883a2 commit c102d20

File tree

5 files changed

+25
-40
lines changed

5 files changed

+25
-40
lines changed

stdlib/private/SwiftPrivateDarwinExtras/Subprocess.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Darwin
1717
import Glibc
1818
#endif
1919

20-
#if !os(Android)
20+
// FIXME: Android: prefix with swift_ in Android version
2121

2222
// swift_posix_spawn isn't available in the public watchOS SDK, we sneak by the
2323
// unavailable attribute declaration here of the APIs that we need.
@@ -49,7 +49,6 @@ func swift_posix_spawn(
4949
_ argv: UnsafePointer<UnsafeMutablePointer<Int8>>,
5050
_ envp: UnsafePointer<UnsafeMutablePointer<Int8>>) -> CInt
5151

52-
#endif
5352

5453
/// Calls POSIX `pipe()`.
5554
func posixPipe() -> (readFD: CInt, writeFD: CInt) {
@@ -68,9 +67,6 @@ func posixPipe() -> (readFD: CInt, writeFD: CInt) {
6867
/// stderr.
6968
public func spawnChild(args: [String])
7069
-> (pid: pid_t, stdinFD: CInt, stdoutFD: CInt, stderrFD: CInt) {
71-
#if os(Android)
72-
preconditionFailure("posix_spawn doesn't exist on Android")
73-
#else
7470
var fileActions = posix_spawn_file_actions_t()
7571
if swift_posix_spawn_file_actions_init(&fileActions) != 0 {
7672
preconditionFailure("swift_posix_spawn_file_actions_init() failed")
@@ -145,7 +141,6 @@ public func spawnChild(args: [String])
145141
}
146142

147143
return (pid, childStdin.writeFD, childStdout.readFD, childStderr.readFD)
148-
#endif
149144
}
150145

151146
internal func _readAll(fd: CInt) -> String {

stdlib/public/Bionic/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set(sources
22
module.map
3+
spawn.h
34
)
45
set(output_dir "${SWIFTLIB_DIR}/bionic")
56

@@ -35,6 +36,7 @@ swift_install_in_component(stdlib
3536

3637
add_swift_library(swiftGlibc IS_SDK_OVERLAY
3738
Glibc.swift
39+
posix_spawn.c
3840
FILE_DEPENDS copy_bionic_module "${SWIFTLIB_DIR}/bionic"
3941
TARGET_SDKS ANDROID
4042
INSTALL_IN_COMPONENT stdlib-experimental)

stdlib/public/Bionic/module.map

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,5 +369,9 @@ module SwiftBionic [system] {
369369
// header "SYSROOT/usr/include/wordexp.h"
370370
// export *
371371
//}
372+
module spawn {
373+
header "spawn.h"
374+
export *
375+
}
372376
}
373377
}

stdlib/public/Bionic/posix_spawn.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,17 @@
2727
#include <sys/cdefs.h>
2828
__FBSDID("$FreeBSD$");
2929

30-
#include "namespace.h"
3130
#include <sys/queue.h>
3231
#include <sys/wait.h>
3332

3433
#include <errno.h>
3534
#include <fcntl.h>
3635
#include <sched.h>
37-
#include <spawn.h>
36+
#include "spawn.h"
3837
#include <signal.h>
3938
#include <stdlib.h>
4039
#include <string.h>
4140
#include <unistd.h>
42-
#include "un-namespace.h"
43-
#include "libc_private.h"
4441

4542
extern char **environ;
4643

@@ -85,6 +82,7 @@ typedef struct __posix_spawn_file_actions_entry {
8582
static int
8683
process_spawnattr(const posix_spawnattr_t sa)
8784
{
85+
#if 0
8886
struct sigaction sigact = { .sa_flags = 0, .sa_handler = SIG_DFL };
8987
int i;
9088

@@ -133,6 +131,7 @@ process_spawnattr(const posix_spawnattr_t sa)
133131
return (errno);
134132
}
135133
}
134+
#endif
136135

137136
return (0);
138137
}
@@ -145,30 +144,30 @@ process_file_actions_entry(posix_spawn_file_actions_entry_t *fae)
145144
switch (fae->fae_action) {
146145
case FAE_OPEN:
147146
/* Perform an open(), make it use the right fd */
148-
fd = _open(fae->fae_path, fae->fae_oflag, fae->fae_mode);
147+
fd = open(fae->fae_path, fae->fae_oflag, fae->fae_mode);
149148
if (fd < 0)
150149
return (errno);
151150
if (fd != fae->fae_fildes) {
152-
if (_dup2(fd, fae->fae_fildes) == -1)
151+
if (dup2(fd, fae->fae_fildes) == -1)
153152
return (errno);
154-
if (_close(fd) != 0) {
153+
if (close(fd) != 0) {
155154
if (errno == EBADF)
156155
return (EBADF);
157156
}
158157
}
159-
if (_fcntl(fae->fae_fildes, F_SETFD, 0) == -1)
158+
if (fcntl(fae->fae_fildes, F_SETFD, 0) == -1)
160159
return (errno);
161160
break;
162161
case FAE_DUP2:
163162
/* Perform a dup2() */
164-
if (_dup2(fae->fae_fildes, fae->fae_newfildes) == -1)
163+
if (dup2(fae->fae_fildes, fae->fae_newfildes) == -1)
165164
return (errno);
166-
if (_fcntl(fae->fae_newfildes, F_SETFD, 0) == -1)
165+
if (fcntl(fae->fae_newfildes, F_SETFD, 0) == -1)
167166
return (errno);
168167
break;
169168
case FAE_CLOSE:
170169
/* Perform a close(), do not fail if already closed */
171-
(void)_close(fae->fae_fildes);
170+
(void)close(fae->fae_fildes);
172171
break;
173172
}
174173
return (0);
@@ -198,7 +197,7 @@ do_posix_spawn(pid_t *pid, const char *path,
198197
pid_t p;
199198
volatile int error = 0;
200199

201-
p = vfork();
200+
p = fork();
202201
switch (p) {
203202
case -1:
204203
return (errno);
@@ -213,15 +212,15 @@ do_posix_spawn(pid_t *pid, const char *path,
213212
if (error)
214213
_exit(127);
215214
}
216-
if (use_env_path)
217-
_execvpe(path, argv, envp != NULL ? envp : environ);
218-
else
219-
_execve(path, argv, envp != NULL ? envp : environ);
215+
//if (use_env_path)
216+
// execvpe(path, argv, envp != NULL ? envp : environ);
217+
//else
218+
execve(path, argv, envp != NULL ? envp : environ);
220219
error = errno;
221220
_exit(127);
222221
default:
223222
if (error != 0)
224-
_waitpid(p, NULL, WNOHANG);
223+
waitpid(p, NULL, WNOHANG);
225224
else if (pid != NULL)
226225
*pid = p;
227226
return (error);

stdlib/public/Bionic/spawn.h

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,8 @@
3030
#define _SPAWN_H_
3131

3232
#include <sys/cdefs.h>
33-
#include <sys/_types.h>
34-
#include <sys/_sigset.h>
35-
36-
#ifndef _MODE_T_DECLARED
37-
typedef __mode_t mode_t;
38-
#define _MODE_T_DECLARED
39-
#endif
40-
41-
#ifndef _PID_T_DECLARED
42-
typedef __pid_t pid_t;
43-
#define _PID_T_DECLARED
44-
#endif
45-
46-
#ifndef _SIGSET_T_DECLARED
47-
#define _SIGSET_T_DECLARED
48-
typedef __sigset_t sigset_t;
49-
#endif
33+
#include <sys/types.h>
34+
#include <signal.h>
5035

5136
struct sched_param;
5237

0 commit comments

Comments
 (0)