Skip to content

Commit f7db03b

Browse files
committed
Processing (*nix): use traditional fork-exec on Android and OpenBSD
Fix termux/termux-packages#25369
1 parent 8647e36 commit f7db03b

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/common/io/io.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ static inline bool ffSearchUserConfigFile(const FFlist* configDirs, const char*
222222
return false;
223223
}
224224

225+
FFNativeFD ffGetNullFD(void);
226+
225227
#ifdef _WIN32
226228
// Only O_RDONLY is supported
227229
HANDLE openat(HANDLE dfd, const char* fileName, bool directory);

src/common/io/io_unix.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,12 @@ void ffListFilesRecursively(const char* path, bool pretty)
358358
ffStrbufEnsureEndsWithC(&folder, '/');
359359
listFilesRecursively(folder.length, &folder, 0, NULL, pretty);
360360
}
361+
362+
FFNativeFD ffGetNullFD(void)
363+
{
364+
static FFNativeFD hNullFile = -1;
365+
if (hNullFile != -1)
366+
return hNullFile;
367+
hNullFile = open("/dev/null", O_WRONLY | O_CLOEXEC);
368+
return hNullFile;
369+
}

src/common/processing_linux.c

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,21 @@ const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool use
6363
if(ffPipe2(pipes, O_CLOEXEC) == -1)
6464
return "pipe() failed";
6565

66+
pid_t childPid = -1;
67+
int nullFile = ffGetNullFD();
68+
69+
#if !(__ANDROID__ || __OpenBSD__)
70+
71+
// NetBSD / Darwin: native syscall
72+
// Linux (glibc): clone3-execve
73+
// FreeBSD: vfork-execve
74+
// illumos: vforkx-execve
75+
// OpenBSD / Android (bionic): fork-execve
76+
6677
posix_spawn_file_actions_t file_actions;
6778
posix_spawn_file_actions_init(&file_actions);
6879
posix_spawn_file_actions_adddup2(&file_actions, pipes[1], useStdErr ? STDERR_FILENO : STDOUT_FILENO);
69-
posix_spawn_file_actions_addopen(&file_actions, useStdErr ? STDOUT_FILENO : STDERR_FILENO, "/dev/null", O_WRONLY, 0);
70-
pid_t childPid = -1;
80+
posix_spawn_file_actions_adddup2(&file_actions, nullFile, useStdErr ? STDOUT_FILENO : STDERR_FILENO);
7181

7282
static char* oldLang = NULL;
7383
static int langIndex = -1;
@@ -113,13 +123,38 @@ const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool use
113123

114124
posix_spawn_file_actions_destroy(&file_actions);
115125

116-
close(pipes[1]);
117126
if (ret != 0)
118127
{
119128
close(pipes[0]);
129+
close(pipes[1]);
120130
return "posix_spawnp() failed";
121131
}
122132

133+
#else
134+
135+
// https://github.com/termux/termux-packages/issues/25369
136+
childPid = fork();
137+
if(childPid == -1)
138+
{
139+
close(pipes[0]);
140+
close(pipes[1]);
141+
return "fork() failed";
142+
}
143+
144+
if(childPid == 0)
145+
{
146+
//Child
147+
dup2(pipes[1], useStdErr ? STDERR_FILENO : STDOUT_FILENO);
148+
dup2(nullFile, useStdErr ? STDOUT_FILENO : STDERR_FILENO);
149+
putenv("LANG=C.UTF-8");
150+
execvp(argv[0], argv);
151+
_exit(127);
152+
}
153+
154+
#endif
155+
156+
close(pipes[1]);
157+
123158
int FF_AUTO_CLOSE_FD childPipeFd = pipes[0];
124159
char str[FF_PIPE_BUFSIZ];
125160

0 commit comments

Comments
 (0)