Skip to content

Use pipe instead of 'sh -c' to spawn subprocesses #2580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ecnelises
Copy link

Filling the whole command into 'sh -c' may hit length limit of single argument. For non-console subprocesses, use pipe to invoke sh instead.

Fixes #1261

Filling the whole command into 'sh -c' may hit length limit of single
argument. For non-console subprocesses, use pipe to invoke sh instead.
err = posix_spawn(&pid_, "/bin/sh", &action, &attr,
const_cast<char**>(spawned_args), environ);
if (err != 0)
Fatal("posix_spawn: %s", strerror(err));

if (!use_console_) {
ssize_t written = write(input_pipe[1], command.c_str(), command.size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that work for large commands?

Most systems use a default pipe buffer size of 64 kiB, some even smaller (see https://github.com/afborchert/pipebuf) so this write() call will block infinitely if command.size() is larger than that (and the bug referenced by this PR talks about command sizes of 128 kiB).

Linux has fcntl(F_SETPIPE_SZ) to change that the pipe buffer size, but I am not aware of similar features on other systems. Even the Linux pipe manpage tsates that applications should not rely on a specific capacity.

It might be more portable and simple to write long commands to a file that is passed as the first argument to the shell instead. And that would also work for console processes too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Long build line can hit MAX_ARG_STRLEN on Linux
2 participants