mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-06-05 08:11:15 -03:00
Merge branch 'split-io' of git://github.com/xiaq/fish-shell into xiaq-split-io
This commit is contained in:
64
postfork.cpp
64
postfork.cpp
@@ -121,15 +121,16 @@ static void free_redirected_fds_from_pipes(io_chain_t &io_chain)
|
||||
for (size_t j = 0; j < max; j++)
|
||||
{
|
||||
/* We're only interested in pipes */
|
||||
io_data_t *possible_conflict = io_chain.at(j).get();
|
||||
if (possible_conflict->io_mode != IO_PIPE && possible_conflict->io_mode != IO_BUFFER)
|
||||
io_data_t *io = io_chain.at(j).get();
|
||||
if (io->io_mode != IO_PIPE && io->io_mode != IO_BUFFER)
|
||||
continue;
|
||||
|
||||
CAST_INIT(io_pipe_t *, possible_conflict, io);
|
||||
/* If the pipe is a conflict, dup it to some other value */
|
||||
for (int k=0; k<2; k++)
|
||||
{
|
||||
/* If it's not a conflict, we don't care */
|
||||
if (possible_conflict->param1.pipe_fd[k] != fd_to_free)
|
||||
if (possible_conflict->pipe_fd[k] != fd_to_free)
|
||||
continue;
|
||||
|
||||
/* Repeat until we have a replacement fd */
|
||||
@@ -144,7 +145,7 @@ static void free_redirected_fds_from_pipes(io_chain_t &io_chain)
|
||||
FATAL_EXIT();
|
||||
}
|
||||
}
|
||||
possible_conflict->param1.pipe_fd[k] = replacement_fd;
|
||||
possible_conflict->pipe_fd[k] = replacement_fd;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -173,7 +174,7 @@ static int handle_child_io(io_chain_t &io_chain)
|
||||
io_data_t *io = io_chain.at(idx).get();
|
||||
int tmp;
|
||||
|
||||
if (io->io_mode == IO_FD && io->fd == io->param1.old_fd)
|
||||
if (io->io_mode == IO_FD && io->fd == static_cast<io_fd_t*>(io)->old_fd)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -193,17 +194,18 @@ static int handle_child_io(io_chain_t &io_chain)
|
||||
case IO_FILE:
|
||||
{
|
||||
// Here we definitely do not want to set CLO_EXEC because our child needs access
|
||||
if ((tmp=open(io->filename_cstr,
|
||||
io->param2.flags, OPEN_MASK))==-1)
|
||||
CAST_INIT(io_file_t *, io_file, io);
|
||||
if ((tmp=open(io_file->filename_cstr,
|
||||
io_file->flags, OPEN_MASK))==-1)
|
||||
{
|
||||
if ((io->param2.flags & O_EXCL) &&
|
||||
if ((io_file->flags & O_EXCL) &&
|
||||
(errno ==EEXIST))
|
||||
{
|
||||
debug_safe(1, NOCLOB_ERROR, io->filename_cstr);
|
||||
debug_safe(1, NOCLOB_ERROR, io_file->filename_cstr);
|
||||
}
|
||||
else
|
||||
{
|
||||
debug_safe(1, FILE_ERROR, io->filename_cstr);
|
||||
debug_safe(1, FILE_ERROR, io_file->filename_cstr);
|
||||
safe_perror("open");
|
||||
}
|
||||
|
||||
@@ -236,7 +238,7 @@ static int handle_child_io(io_chain_t &io_chain)
|
||||
*/
|
||||
close(io->fd);
|
||||
|
||||
if (dup2(io->param1.old_fd, io->fd) == -1)
|
||||
if (dup2(static_cast<const io_fd_t *>(io)->old_fd, io->fd) == -1)
|
||||
{
|
||||
debug_safe_int(1, FD_ERROR, io->fd);
|
||||
safe_perror("dup2");
|
||||
@@ -248,28 +250,29 @@ static int handle_child_io(io_chain_t &io_chain)
|
||||
case IO_BUFFER:
|
||||
case IO_PIPE:
|
||||
{
|
||||
CAST_INIT(io_pipe_t *, io_pipe, io);
|
||||
/* If write_pipe_idx is 0, it means we're connecting to the read end (first pipe fd). If it's 1, we're connecting to the write end (second pipe fd). */
|
||||
unsigned int write_pipe_idx = (io->is_input ? 0 : 1);
|
||||
unsigned int write_pipe_idx = (io_pipe->is_input ? 0 : 1);
|
||||
/*
|
||||
debug( 0,
|
||||
L"%ls %ls on fd %d (%d %d)",
|
||||
write_pipe?L"write":L"read",
|
||||
(io->io_mode == IO_BUFFER)?L"buffer":L"pipe",
|
||||
io->fd,
|
||||
io->param1.pipe_fd[0],
|
||||
io->param1.pipe_fd[1]);
|
||||
io->pipe_fd[0],
|
||||
io->pipe_fd[1]);
|
||||
*/
|
||||
if (dup2(io->param1.pipe_fd[write_pipe_idx], io->fd) != io->fd)
|
||||
if (dup2(io_pipe->pipe_fd[write_pipe_idx], io->fd) != io->fd)
|
||||
{
|
||||
debug_safe(1, LOCAL_PIPE_ERROR);
|
||||
safe_perror("dup2");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (io->param1.pipe_fd[0] >= 0)
|
||||
exec_close(io->param1.pipe_fd[0]);
|
||||
if (io->param1.pipe_fd[1] >= 0)
|
||||
exec_close(io->param1.pipe_fd[1]);
|
||||
if (io_pipe->pipe_fd[0] >= 0)
|
||||
exec_close(io_pipe->pipe_fd[0]);
|
||||
if (io_pipe->pipe_fd[1] >= 0)
|
||||
exec_close(io_pipe->pipe_fd[1]);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -447,9 +450,11 @@ bool fork_actions_make_spawn_properties(posix_spawnattr_t *attr, posix_spawn_fil
|
||||
{
|
||||
shared_ptr<const io_data_t> io = j->io.at(idx);
|
||||
|
||||
if (io->io_mode == IO_FD && io->fd == io->param1.old_fd)
|
||||
if (io->io_mode == IO_FD)
|
||||
{
|
||||
continue;
|
||||
CAST_INIT(const io_fd_t *, io_fd, io.get());
|
||||
if (io->fd == io_fd->old_fd)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (io->fd > 2)
|
||||
@@ -469,23 +474,26 @@ bool fork_actions_make_spawn_properties(posix_spawnattr_t *attr, posix_spawn_fil
|
||||
|
||||
case IO_FILE:
|
||||
{
|
||||
CAST_INIT(const io_file_t *, io_file, io.get());
|
||||
if (! err)
|
||||
err = posix_spawn_file_actions_addopen(actions, io->fd, io->filename_cstr, io->param2.flags /* mode */, OPEN_MASK);
|
||||
err = posix_spawn_file_actions_addopen(actions, io->fd, io_file->filename_cstr, io_file->flags /* mode */, OPEN_MASK);
|
||||
break;
|
||||
}
|
||||
|
||||
case IO_FD:
|
||||
{
|
||||
CAST_INIT(const io_fd_t *, io_fd, io.get());
|
||||
if (! err)
|
||||
err = posix_spawn_file_actions_adddup2(actions, io->param1.old_fd /* from */, io->fd /* to */);
|
||||
err = posix_spawn_file_actions_adddup2(actions, io_fd->old_fd /* from */, io->fd /* to */);
|
||||
break;
|
||||
}
|
||||
|
||||
case IO_BUFFER:
|
||||
case IO_PIPE:
|
||||
{
|
||||
unsigned int write_pipe_idx = (io->is_input ? 0 : 1);
|
||||
int from_fd = io->param1.pipe_fd[write_pipe_idx];
|
||||
CAST_INIT(const io_pipe_t *, io_pipe, io.get());
|
||||
unsigned int write_pipe_idx = (io_pipe->is_input ? 0 : 1);
|
||||
int from_fd = io_pipe->pipe_fd[write_pipe_idx];
|
||||
int to_fd = io->fd;
|
||||
if (! err)
|
||||
err = posix_spawn_file_actions_adddup2(actions, from_fd, to_fd);
|
||||
@@ -494,14 +502,14 @@ bool fork_actions_make_spawn_properties(posix_spawnattr_t *attr, posix_spawn_fil
|
||||
if (write_pipe_idx > 0)
|
||||
{
|
||||
if (! err)
|
||||
err = posix_spawn_file_actions_addclose(actions, io->param1.pipe_fd[0]);
|
||||
err = posix_spawn_file_actions_addclose(actions, io_pipe->pipe_fd[0]);
|
||||
if (! err)
|
||||
err = posix_spawn_file_actions_addclose(actions, io->param1.pipe_fd[1]);
|
||||
err = posix_spawn_file_actions_addclose(actions, io_pipe->pipe_fd[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (! err)
|
||||
err = posix_spawn_file_actions_addclose(actions, io->param1.pipe_fd[0]);
|
||||
err = posix_spawn_file_actions_addclose(actions, io_pipe->pipe_fd[0]);
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user