Archive of RubyForge sup-talk mailing list
 help / color / mirror / Atom feed
* [sup-talk] [PATCH] shell commands are now run in a child process
@ 2008-02-03  8:11 Christopher Warrington
  2008-02-05 17:34 ` William Morgan
  0 siblings, 1 reply; 9+ messages in thread
From: Christopher Warrington @ 2008-02-03  8:11 UTC (permalink / raw)


When shelling out, the external command is now run in a child process.
Before the external command is run, all (I hope) of sup's open files are
closed first.

This should fix the ferret "Permission denied" errors on Windows.
---
 lib/sup/buffer.rb |   27 ++++++++++++++++++++++++++-
 1 files changed, 26 insertions(+), 1 deletions(-)

diff --git a/lib/sup/buffer.rb b/lib/sup/buffer.rb
index 4374fa8..7768011 100644
--- a/lib/sup/buffer.rb
+++ b/lib/sup/buffer.rb
@@ -696,11 +696,36 @@ EOS
     draw_screen :refresh => true
   end
 
+  ## There is no good way that I can find the get the maximum fd allowed.
+  ## On the POSIX systems I have played with, 500 seems to be the maximum. On Windows,
+  ## 2048 is the documented maximum (for the C Runtime Library).
+  MAX_FD = 2048
+
   def shell_out command
     @shelled = true
     Ncurses.sync do
       Ncurses.endwin
-      system command
+
+      child_pid = fork
+      if child_pid == nil
+        ## child process
+
+        start_fd = 1 + [STDIN.fileno, STDOUT.fileno, STDERR.fileno].max # don't close these
+	start_fd.upto(MAX_FD) do |fd|
+          begin
+            IO.for_fd(fd).close
+          rescue Errno::EBADF
+            ## fd is not open: ignore and move on
+          end
+        end
+
+        exec(command)
+        ## never gets here
+      else
+        ## sup process
+        Process.waitpid(child_pid, Process::WUNTRACED) # catch an already dead child
+      end
+
       Ncurses.refresh
       Ncurses.curs_set 0
     end
-- 
1.5.3.8



^ permalink raw reply	[flat|nested] 9+ messages in thread
* [sup-talk] [PATCH] shell commands are now run in a child process
@ 2008-02-25 17:48 William Morgan
  2008-03-01  4:42 ` Christopher Warrington
  0 siblings, 1 reply; 9+ messages in thread
From: William Morgan @ 2008-02-25 17:48 UTC (permalink / raw)


Reformatted excerpts from Christopher Warrington's message of 2008-02-20:
> I wouldn't right now. I was getting ruby seg. faults with the code I
> submitted and ruby 1.8.6 (ruby bug? I don't know). Changing to code
> that closes open files descriptors until the first invalid file
> descriptor is encountered closes enough that I don't get the crash,
> but also doesn't cause a seg. fault.

Well, this wouldn't be the most cargo-cultish code that's in Sup. (See
previous message about ncurses.)

I'll keep this patch on the back burner, then, until we have a little
more experience. Is anyone else out there using Cygwin?

-- 
William <wmorgan-sup at masanjin.net>


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2008-03-01 20:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-03  8:11 [sup-talk] [PATCH] shell commands are now run in a child process Christopher Warrington
2008-02-05 17:34 ` William Morgan
2008-02-05 19:29   ` Christopher Warrington
2008-02-05 20:12     ` William Morgan
2008-02-08  9:40       ` Christopher Warrington
2008-02-19 17:48         ` William Morgan
2008-02-25 17:48 William Morgan
2008-03-01  4:42 ` Christopher Warrington
2008-03-01 20:37   ` William Morgan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox