Skip to content
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

env: don't panic when name is empty #2731

Merged
merged 3 commits into from
Nov 2, 2021

Conversation

thomasqueirozb
Copy link
Contributor

No description provided.

@thomasqueirozb
Copy link
Contributor Author

thomasqueirozb commented Oct 25, 2021

While looking at the file I also saw this:

// FIXME: this should just use execvp() (no fork()) on Unix-like systems
match Command::new(&*prog).args(args).status() {

I don't think the FIXME is actually necessary. On unix like systems, Command::spawn does indeed fork here

But it looks like it only forks if self.posix_spawn (which calls libc::posix_spawnp) doesn't pass the necessary requirements to be used.

The forked child process calls self.do_exec (which calls libc::execvp after doing some stuff and performing some checks).

If both methods fail, Command::spawn returns an io error.

All of this is basically to say that the fork only happens in some circumstances and that calling the standard library is a better way to do this than calling execvp directly, since it does a lot of error checking and tries to recover when something fails. The env utility shouldn't have to worry about a fork and we should just keep things as they are.

Copy link
Contributor

@miDeb miDeb left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Nice that you added such a detailed comment and a test :)

src/uu/env/src/env.rs Show resolved Hide resolved
@miDeb
Copy link
Contributor

miDeb commented Nov 1, 2021

All of this is basically to say that the fork only happens in some circumstances

Indeed, on my system it calls clone (which is called by posix_spawnp) according to strace . However, I think the main point of this comment is that it would not be necessary to spawn a child process and that we could keep using the current process. I think it's probably not worth changing, as you pointed out using the standard library is much simpler.

@thomasqueirozb
Copy link
Contributor Author

Thanks for the PR! Nice that you added such a detailed comment and a test :)

I spent a lot of time researching what was the correct way to do this. Might as well document it and possibly save someone else's time in the future :)

All of this is basically to say that the fork only happens in some circumstances

Indeed, on my system it calls clone (which is called by posix_spawnp) according to strace . However, I think the main point of this comment is that it would not be necessary to spawn a child process and that we could keep using the current process. I think it's probably not worth changing, as you pointed out using the standard library is much simpler.

Will remove the FIXME and change the comment to specify why fork/clone is called.

Copy link
Contributor

@miDeb miDeb left a comment

Choose a reason for hiding this comment

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

Thanks again!

@miDeb miDeb merged commit a05628f into uutils:master Nov 2, 2021
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.

2 participants