Project

General

Profile

Actions

Feature #4046

closed

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

Added by rocky (Rocky Bernstein) over 13 years ago. Updated about 6 years ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-core:33146]

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::OS_ARGV and RubyVM::OS_STARTUP_DIR. With this,
if a Ruby program wants to restart itself, it can run:

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


Files

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

Related issues 2 (1 open1 closed)

Has duplicate Ruby master - Feature #6648: Provide a standard API for retrieving all command-line flags passed to RubyAssignedmatz (Yukihiro Matsumoto)Actions
Has duplicate Ruby master - Feature #6793: easier ability to "run as the currently running ruby"FeedbackActions
Actions #1

Updated by kstephens (Kurt Stephens) over 13 years ago

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

Actions #2

Updated by mame (Yusuke Endoh) over 13 years ago

=begin
Hi,

2010/11/12 Rocky Bernstein :

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::OS_STARTUP_DIR
puts ""
puts "
exec: %p" % RubyVM::OS_ARGV.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::OS_STARTUP_DIR
puts ""
puts "
exec: %p" % RubyVM::OS_ARGV.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

=end

Actions #3

Updated by rocky (Rocky Bernstein) over 13 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

Updated by naruse (Yui NARUSE) about 12 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.

Updated by ko1 (Koichi Sasada) over 11 years ago

  • Target version changed from 2.0.0 to 2.6

I changed target to next minor because no feedback.

Actions #6

Updated by naruse (Yui NARUSE) about 6 years ago

  • Target version deleted (2.6)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0