Forum: Ruby execle ruby process from C ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Zouplaz (Guest)
on 2009-03-15 21:35
(Received via mailing list)
Hello, I'm playing with a small http server written in C & Ruby CGI and
there is something wrong

if(execle("/usr/bin/ruby", "/usr/bin/ruby" " test.rb", NULL, NULL) ==
-1)
{
  wlog("error execle");
  exit(EXIT_FAILURE);
}

- the C program did a chdir to the directory where test.rb can be found.
- ps ae gives
   18133 pts/5    S+     0:00 /usr/bin/ruby test.rb

test.rb contains just one line -> print "hello"

But when execle, ruby never returns... I mean, the server can be
interrupted with a CONTROL/C and gives /usr/bin/ruby test.rb: Interrupt

I don't understand why...

I have to say that stdout and stdin are redirected to pipes - I tried
with some unix executables like 'ls' and it works

What is the difference with ruby executable ?

Thanks
Zouplaz (Guest)
on 2009-03-15 21:55
(Received via mailing list)
I forgot to say that print 'hello' has not effect...
Michael L. (Guest)
on 2009-03-16 01:04
Zouplaz wrote:
> Hello, I'm playing with a small http server written in C & Ruby CGI and
> there is something wrong
>
> if(execle("/usr/bin/ruby", "/usr/bin/ruby" " test.rb", NULL, NULL) ==
> -1)
> {
>   wlog("error execle");
>   exit(EXIT_FAILURE);
> }

> What is the difference with ruby executable ?

I may be mistaken because 1. I don't have your full piece of code or 2.
I'm not very up-to-date with C.

But simply running ../ruby test.rb and test.rb only has print "hello" in
it, the output would be to your shell/terminal...not your http server
o_O

Correct me if I'm wrong and I'll try to help the best I can. Post the
code to your http server and that would probably help alot.

Regards,

- Mac
Nobuyoshi N. (Guest)
on 2009-03-16 05:53
(Received via mailing list)
Hi,

At Mon, 16 Mar 2009 04:32:42 +0900,
Zouplaz wrote in [ruby-talk:331208]:
> if(execle("/usr/bin/ruby", "/usr/bin/ruby" " test.rb", NULL, NULL) == -1)

The second string is concatenated at compile time into
"/usr/bin/ruby test.rb", so you execute ruby with only that one
argument, which is counted as the program name.  Therefore ruby
waits script from stdin.
Michael L. (Guest)
on 2009-03-16 06:02
> The second string is concatenated at compile time into
> "/usr/bin/ruby test.rb", so you execute ruby with only that one
> argument, which is counted as the program name.  Therefore ruby
> waits script from stdin.

The HTTP server shouldn't be waiting for stdin because test.rb never
calls for it. The server isn't returning a value because it isn't being
sent to the server, it's being sent to the shell. If it was waiting for
input from stdin then hitting enter would return an input
value...nil...and it would return as such.

- Mac
Nobuyoshi N. (Guest)
on 2009-03-16 06:53
(Received via mailing list)
Hi,

At Mon, 16 Mar 2009 12:59:51 +0900,
Michael L. wrote in [ruby-talk:331241]:
> > The second string is concatenated at compile time into
> > "/usr/bin/ruby test.rb", so you execute ruby with only that one
> > argument, which is counted as the program name.  Therefore ruby
> > waits script from stdin.
>
> The HTTP server shouldn't be waiting for stdin because test.rb never
> calls for it.

Correct, but OP's code doesn't any arguments to /usr/bin/ruby.
So ruby expects that the script will be given from stdin.

That is, I meant a bug about string literal concatenation.  It
should be:

  if(execle("/usr/bin/ruby", "/usr/bin/ruby", "test.rb", NULL, NULL) ==
-1)

Note that a comma between "/usr/bin/ruby" and "test.rb", and
stripping a leading space from " test.rb", also.
This topic is locked and can not be replied to.