Feature #4046

Saving C's **argv and cwd allows Ruby programs to reliably restart themselves

Added by Rocky Bernstein over 3 years ago. Updated over 1 year ago.

[ruby-core:33146]
Status:Feedback
Priority:Normal
Assignee:-
Category:-
Target version:next minor

Description

=begin
In a debugger often one gets to a state where one just wants to restart everything exactly the way the program was previously invoked.

It would helpful if Ruby saved **argv and cwd.

The attached patch saves these in RubyVM::OSARGV and RubyVM::OSSTARTUP_DIR. With this,
if a Ruby program wants to restart itself, it can run:

chdir RubyVM::OSSTARTUPDIR
exec(*RubyVM::OS_ARGV)
=end

OS_ARGV_and_STARTUP_DIR.patch Magnifier - Save C's **argv and cwd as RubyVM::OS_ARGV and RubyVM::OS_STARTUP_DIR (3.02 KB) Rocky Bernstein, 11/12/2010 12:32 PM


Related issues

Duplicated by ruby-trunk - Feature #6648: Provide a standard API for retrieving all command-line fl... Assigned 06/26/2012
Duplicated by ruby-trunk - Feature #6793: easier ability to "run as the currently running ruby" Feedback 07/26/2012

History

#1 Updated by Kurt Stephens over 3 years ago

=begin
bump. This would be very helpful in Rakefiles; rake mutates ARGV rather than making a copy.
=end

#2 Updated by Yusuke Endoh over 3 years ago

=begin
Hi,

2010/11/12 Rocky Bernstein redmine@ruby-lang.org:

In a debugger often one gets to a state where one just wants to restart everything exactly the way the program was previously invoked.

exec(3) allows us to fake argv[0].
Unfortunately, there is no truely reliable way to re-invoke process,
at least, in Unix.

$ cat test.rb
#
# test.rb
#
Dir.chdir RubyVM::OSSTARTUPDIR
puts ""
puts "
exec: %p" % RubyVM::OSARGV.join(" ")
puts "**"
exec(
RubyVM::OS
ARGV)

# http://cr.yp.to/ucspi-tcp/argv0.html
$ argv0 ./ruby cat test.rb


*** exec: "cat test.rb"


#
# test.rb
#
Dir.chdir RubyVM::OSSTARTUPDIR
puts ""
puts "
exec: %p" % RubyVM::OSARGV.join(" ")
puts "**"
exec(
RubyVM::OS
ARGV)

# You can do the same with perl, ruby, etc.
$ perl -e 'exec { "./ruby" } ("cat", "test.rb")'
$ ruby -e 'exec(["./ruby", "cat"], "test.rb")'

--
Yusuke Endoh mame@tsg.ne.jp

=end

#3 Updated by Rocky Bernstein over 3 years ago

=begin
I am sorry if this duplicated on ruby-core. I wanted the redmine ticket updated. It also gives me an opportunity to correct some grammar.

This is a valid point and I now understand that this isn't 100% reliable. Just "most-of-the-time" reliable.

Furthermore cwd one can't fake. And the other argv values one can't fake. So as with other things in life, I propose we say, "yes there are exceptions, so programmer beware" and then try to be helpful. This is nicer than taking an approach that because we can't get it to work in all conditions, we're not going to do anything. Programmers will still have to solve their problems, and without this will do so even less reliably but with more effort.

I think that "faking" $0 beforehand is more of the exceptional case than the normal case. And as you suggest, it can't be done on all operating systems.

But in my particular application, it doesn't really matter.

In the ruby debuggers I work on, you can see what the debugger has recorded to restart. That command is called "show args". So if you've run argv0, the programmer can change the settings presented with "set args". If it makes a difference.

I put this all in the context of a debugger to make things concrete. But one may imagine other situations in writing a large system where one may just want a simple way to restart the program or suggest how the program was invoked, keeping in mind argv[0] might be different.
=end

#4 Updated by Yui NARUSE about 2 years ago

  • Description updated (diff)
  • Status changed from Open to Feedback

argv[0] is not reliable on some environment.

see "#! - the Unix truth as far as I know it."
http://homepages.cwi.nl/~aeb/std/hashexclam.html

I tried to implement with vendor specific functions.
https://gist.github.com/1129437

I gave up it because I can't find the way on AIX.
Therefore I concluded people should use RbConfig.

#5 Updated by Koichi Sasada over 1 year ago

  • Target version changed from 2.0.0 to next minor

I changed target to next minor because no feedback.

Also available in: Atom PDF