Cleanup of code that decides whether or not to fork. Fix for issue where stderr may be output twice.

This commit is contained in:
ridiculousfish 2013-06-16 23:26:43 -07:00
parent c6ec2645dc
commit 640118e781
3 changed files with 134 additions and 114 deletions

119
exec.cpp
View file

@ -149,6 +149,25 @@ int exec_pipe(int fd[2])
return res;
}
/* Returns true if the redirection is a file redirection to a file other than /dev/null */
static bool redirection_is_to_real_file(const io_data_t *io)
{
bool result = false;
if (io != NULL && io->io_mode == IO_FILE)
{
/* It's a file redirection. Compare the path to /dev/null */
CAST_INIT(const io_file_t *, io_file, io);
const char *path = io_file->filename_cstr;
if (strcmp(path, "/dev/null") != 0)
{
/* It's not /dev/null */
result = true;
}
}
return result;
}
void print_open_fds(void)
{
for (size_t i=0; i < open_fds.size(); ++i)
@ -519,22 +538,17 @@ static bool can_use_posix_spawn_for_job(const job_t *job, const process_t *proce
}
}
/* Now see if we have a redirection involving a file. The only one we allow is /dev/null, which we assume will not fail. */
bool result = true;
for (size_t idx = 0; idx < job->io.size(); idx++)
{
const shared_ptr<const io_data_t> &io = job->io.at(idx);
if (io->io_mode == IO_FILE)
{
CAST_INIT(const io_file_t *, io_file, io.get());
const char *path = io_file->filename_cstr;
/* This IO action is a file redirection. Only allow /dev/null, which is a common case we assume won't fail. */
if (strcmp(path, "/dev/null") != 0)
if (redirection_is_to_real_file(io.get()))
{
result = false;
break;
}
}
}
return result;
}
@ -910,7 +924,7 @@ void exec(parser_t &parser, job_t *j)
*/
if (p == j->first_process)
{
const shared_ptr<const io_data_t> &in = io_chain_get(j->io, 0);
const shared_ptr<const io_data_t> in = io_chain_get(j->io, 0);
if (in)
{
@ -1170,54 +1184,59 @@ void exec(parser_t &parser, job_t *j)
case INTERNAL_BUILTIN:
{
bool skip_fork;
/*
Handle output from builtin commands. In the general
case, this means forking of a worker process, that
will write out the contents of the stdout and stderr
buffers to the correct file descriptor. Since
forking is expensive, fish tries to avoid it wehn
forking is expensive, fish tries to avoid it when
possible.
*/
/*
If a builtin didn't produce any output, and it is
not inside a pipeline, there is no need to fork
*/
skip_fork =
get_stdout_buffer().empty() &&
get_stderr_buffer().empty() &&
!p->next;
/*
If the output of a builtin is to be sent to an internal
buffer, there is no need to fork. This helps out the
performance quite a bit in complex completion code.
*/
bool fork_was_skipped = false;
const shared_ptr<io_data_t> stdout_io = io_chain_get(j->io, STDOUT_FILENO);
const shared_ptr<io_data_t> stderr_io = io_chain_get(j->io, STDERR_FILENO);
const bool buffer_stdout = stdout_io && stdout_io->io_mode == IO_BUFFER;
if ((get_stderr_buffer().empty()) &&
(!p->next) &&
(! get_stdout_buffer().empty()) &&
(buffer_stdout))
/* If we are outputting to a file, we have to actually do it, even if we have no output, so that we can truncate the file. Does not apply to /dev/null. */
bool must_fork = redirection_is_to_real_file(stdout_io.get()) || redirection_is_to_real_file(stderr_io.get());
if (! must_fork)
{
CAST_INIT(io_buffer_t *, io_buffer, stdout_io.get());
const std::string res = wcs2string(get_stdout_buffer());
io_buffer->out_buffer_append(res.c_str(), res.size());
skip_fork = true;
}
if (p->next == NULL)
{
const bool stdout_is_to_buffer = stdout_io && stdout_io->io_mode == IO_BUFFER;
const bool no_stdout_output = get_stdout_buffer().empty();
const bool no_stderr_output = get_stderr_buffer().empty();
/* PCA for some reason, fish forks a lot, even for basic builtins like echo just to write out their buffers. I'm certain a lot of this is unnecessary, but I am not sure exactly when. If j->io is NULL, then it means there's no pipes or anything, so we can certainly just write out our data. Beyond that, we may be able to do the same if io_get returns 0 for STDOUT_FILENO and STDERR_FILENO.
*/
if (! skip_fork && stdout_io.get() == NULL && stderr_io.get() == NULL)
if (no_stdout_output && no_stderr_output)
{
/* The builtin produced no output and is not inside of a pipeline. No need to fork or even output anything. */
if (g_log_forks)
{
printf("fork #-: Skipping fork for internal builtin for '%ls'\n", p->argv0());
// This one is very wordy
//printf("fork #-: Skipping fork due to no output for internal builtin for '%ls'\n", p->argv0());
}
fork_was_skipped = true;
}
else if (no_stderr_output && stdout_is_to_buffer)
{
/* The builtin produced no stderr, and its stdout is going to an internal buffer. There is no need to fork. This helps out the performance quite a bit in complex completion code. */
if (g_log_forks)
{
printf("fork #-: Skipping fork due to buffered output for internal builtin for '%ls'\n", p->argv0());
}
CAST_INIT(io_buffer_t *, io_buffer, stdout_io.get());
const std::string res = wcs2string(get_stdout_buffer());
io_buffer->out_buffer_append(res.data(), res.size());
fork_was_skipped = true;
}
else if (stdout_io.get() == NULL && stderr_io.get() == NULL)
{
/* We are writing to normal stdout and stderr. Just do it - no need to fork. */
if (g_log_forks)
{
printf("fork #-: Skipping fork due to ordinary output for internal builtin for '%ls'\n", p->argv0());
}
const wcstring &out = get_stdout_buffer(), &err = get_stderr_buffer();
const std::string outbuff = wcs2string(out);
@ -1227,25 +1246,13 @@ void exec(parser_t &parser, job_t *j)
{
show_stackframe();
}
skip_fork = true;
}
for (io_chain_t::iterator iter = j->io.begin(); iter != j->io.end(); ++iter)
{
const shared_ptr<io_data_t> &tmp_io = *iter;
if (tmp_io->io_mode == IO_FILE)
{
const io_file_t *tmp_file_io = static_cast<const io_file_t *>(tmp_io.get());
if (strcmp(tmp_file_io->filename_cstr, "/dev/null"))
{
skip_fork = false;
break;
fork_was_skipped = true;
}
}
}
if (skip_fork)
if (fork_was_skipped)
{
p->completed=1;
if (p->next == 0)
@ -1255,11 +1262,12 @@ void exec(parser_t &parser, job_t *j)
int status = p->status;
proc_set_last_status(job_get_flag(j, JOB_NEGATE)?(!status):status);
}
break;
}
else
{
/* Ok, unfortunatly, we have to do a real fork. Bummer. We work hard to make sure we don't have to wait for all our threads to exit, by arranging things so that we don't have to allocate memory or do anything except system calls in the child. */
/* Ok, unfortunately, we have to do a real fork. Bummer. We work hard to make sure we don't have to wait for all our threads to exit, by arranging things so that we don't have to allocate memory or do anything except system calls in the child. */
/* Get the strings we'll write before we fork (since they call malloc) */
const wcstring &out = get_stdout_buffer(), &err = get_stderr_buffer();
@ -1304,6 +1312,7 @@ void exec(parser_t &parser, job_t *j)
set_child_group(j, p, 0);
}
}
break;
}

View file

@ -1,5 +1,14 @@
# ensure that builtins that produce no output can still truncate files
# (bug PCA almost reintroduced!)
echo "Testing that builtins can truncate files"
echo abc > /tmp/file_truncation_test.txt
cat /tmp/file_truncation_test.txt
echo -n > /tmp/file_truncation_test.txt
cat /tmp/file_truncation_test.txt
# Test events.
# This pattern caused a crash; github issue #449
set -g var before

View file

@ -1,2 +1,4 @@
Testing that builtins can truncate files
abc
before:test1
received event test3 with args: foo bar