Bug in system()?

I’ve just hit a problem where the system() method to call an external
program failed in a fairly unpredictable way, and I couldn’t get any
clues from within ruby to diagnose the problem, so I ended up debugging
process.c to work out what the problem was.

My code looks something like:

system("/bin/tar --extract --directory=#{dir} --file=#{file}")

if $?.exitstatus !=0
raise “tar unpack failed with exitstatus: #{$?.exitstatus}”
end

But it fails on the if because ‘$?’ isn’t being set by the failing
system() method and so ‘$?’ is nil.

The problem ultimately boiled down to the fork() system call failing
with ENOMEM (badly configured system, but that’s another matter).

Am I missing something here? Should there be a way to get errno from
ruby, or am I missing other diagnostic information I could have used?

Is there a bug in system()? Shouldn’t it always set ‘$?’ ?

thanks,

Anthony W.

On Wed, Feb 2, 2011 at 5:49 PM, Anthony W. [email protected]
wrote:

But it fails on the if because ‘$?’ isn’t being set by the failing system()
method and so ‘$?’ is nil.

The problem ultimately boiled down to the fork() system call failing with ENOMEM
(badly configured system, but that’s another matter).

Am I missing something here? Should there be a way to get errno from ruby, or am
I missing other diagnostic information I could have used?

Is there a bug in system()? Shouldn’t it always set ‘$?’ ?

You can use system’s return value

irb(main):006:0> system ‘/bin/sh -c “exit 1”’
=> false
irb(main):007:0> system ‘/bin/sh -c “exit 0”’
=> true

system("/bin/tar --extract --directory=#{dir} --file=#{file}") or
raise “tar unpack failed”

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs