Fixes to improve support for chdir in spawn functions#6226
Fixes to improve support for chdir in spawn functions#6226headius merged 10 commits intojruby:masterfrom
Conversation
|
The first fix here utilizes the following pattern, suggested by @mrnoname1000, to create a multi-argument process launch that safely does a chdir followed by an exec. execlp("sh", "/bin/sh", "-c", "cd \"$0\"; exec \"$@\"", path_to_cd, arguments, NULL);This fixes the original case provided in #6153, since the embedded Ruby script is passed along to the requested Ruby interpreter without shell intervention. I have done a somewhat "brute force" implementation of this change. When we determine that a chdir is necessary and we have multiple arguments (indicating an execl-style process launch), the additional elements from the C call example above are prepended to the collection of program and arguments and the chdir flag is cleared. This avoids falling back on existing logic to launch with There are a few items missing here:
|
7b682aa to
8f7aeb4
Compare
This change helps make our sh-based chdir work properly without us mashing the command + arguments together into a string that may parse badly when passed to sh -c. The primary trick here is to use sh to do the chdir followed by an exec of the explicit argument list passed in. The chdir will run in the intermediate shell, as with a fork/exec, and then the exec will receive the actual target program and its arguments unpolluted. With this change, the example from jruby#6153 works properly, and the Ruby script is passed along without sh seeing it as part of a shell script.
8f7aeb4 to
2b07d27
Compare
* Avoid having user values in $0 for sh. * Use -- to terminate chdir argument list.
2b07d27 to
01a8828
Compare
We'll do a CRuby exclude sweep soon.
3a6b4aa to
2ff3738
Compare
This uncomments some paths that are not yet used but will be once Windows support comes along.
4acdb4c to
1daa8a5
Compare
1daa8a5 to
21c9ac3
Compare
|
As a note for future folks, a recap of what has been added:
There are a few possible improvemements in the future:
Thanks to @mrnoname1000 for helping with these arcane shell oddities. |
This PR will be an aggregate of fixes to make process launching with chdir work more like it does in CRuby.
The main issue here is in our native process launching, which uses
posix_spawn. There's no ability in this function to do a chdir before launching the new process, and we have worked around this in a number of ways::chdirtoKernel.systemdoesn't work reliably #6153, the keyword argumentchdiroption is not yet supported along this path, but it does already handle our virtual CWD.cd somedirto that script. This may be sufficient, since the expectation is that the remainder of the script is already valid shell. We may wish to pass the requested dir in using argument passing as mentioned in Passing:chdirtoKernel.systemdoesn't work reliably #6153.system, we must construct a new command line using sh and chdir without the actual program and arguments getting interpreted as part of a shell script. The current JRuby code just naively joins the command and its arguments into a single string, which is the cause of Passing:chdirtoKernel.systemdoesn't work reliably #6153.The bulk of work in this PR will make use of the examples provided by @mrnoname1000 to use exec within our intermediate shell script, ensuring the actual command and arguments are handled without the shell attempting to treat them as part of a shell script.
There may be other combinations not described above that become apparent while working on these fixes.
TO DO: