1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

close stdin before calling a background program

The solution with fork() is more general, but it unnecessarily produces two new
process: one in exe_no_stdin(), one in system(). The solution with FD_CLOEXEC
is simpler as it only requires changing and restoring a flag of stdin, but I
suspect it may not be available in all OSes elinks can be compiled for.
This commit is contained in:
sgerwk 2021-03-20 00:49:56 +01:00
parent b6cf921bd3
commit b21dea1ba0
3 changed files with 31 additions and 1 deletions

View File

@ -362,6 +362,30 @@ exe(char *path)
#endif
int
exe_no_stdin(char *path) {
int ret;
#if defined(F_GETFD) && defined(FD_CLOEXEC)
int flags;
flags = fcntl(STDIN_FILENO, F_GETFD, &flags);
fcntl(STDIN_FILENO, F_SETFD, flags | FD_CLOEXEC);
ret = exe(path);
fcntl(STDIN_FILENO, F_SETFD, flags);
#else
pid_t pid;
pid = fork();
if (pid == 0) {
close(STDIN_FILENO);
exit(exe(path));
}
else if (pid > 0)
waitpid(pid, &ret, 0);
#endif
return ret;
}
static char *clipboard;
char *

View File

@ -47,6 +47,7 @@ char *get_window_title(int codepage);
void block_stdin(void);
void unblock_stdin(void);
int exe(char *);
int exe_no_stdin(char *);
int resize_window(int, int, int, int);
int can_resize_window(int);
int can_open_os_shell(int);

View File

@ -226,11 +226,16 @@ void
exec_thread(char *path, int p)
{
int plen = strlen(path + 1) + 2;
pid_t pid;
int flags;
#if defined(HAVE_SETPGID) && !defined(CONFIG_OS_BEOS) && !defined(HAVE_BEGINTHREAD)
if (path[0] == TERM_EXEC_NEWWIN) setpgid(0, 0);
#endif
exe(path + 1);
if (path[0] == TERM_EXEC_BG)
exe_no_stdin(path + 1);
else
exe(path + 1);
if (path[plen]) unlink(path + plen);
}