unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* open-pipe fd duplications
@ 2003-07-28 23:13 Kevin Ryde
  2003-09-15 12:45 ` Marius Vollmer
  0 siblings, 1 reply; 3+ messages in thread
From: Kevin Ryde @ 2003-07-28 23:13 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 1101 bytes --]

I think open-pipe leaves an extra copy of its input or output fd in
the forked child.  For instance, using showfds.c below and

	(use-modules (ice-9 popen))

	(let ((p (open-input-pipe "./showfds")))
	  (sleep 1)
	  (close-pipe p))

	(let ((p (open-output-pipe "./showfds")))
	  (sleep 1)
	  (close-pipe p))

I take it the intention is to leave the child clean except for the
normal 0, 1 and 2.

        * popen.scm (open-process): Close input-fdes, output-fdes and
        error-fdes after duping them to 0, 1 and 2.

I think this is worth putting in the 1.6 branch too.

The only likely ill effect I could spot from the current code was that
for an output pipe the extra copy of the read side means that if the
spawned program closes stdin, a write from the parent (ie. guile)
won't produce a SIGPIPE.  For instance the following contrivance
currently gets a SIGPIPE only when the sleep finishes and the shell
exits, whereas with the change above it happens immediately.

	(use-modules (ice-9 popen))
	(let ((p (open-output-pipe "exec 0>/dev/null; sleep 10")))
	  (while #t
	    (display "x" p)))



[-- Attachment #2: popen.scm.close.diff --]
[-- Type: text/plain, Size: 1087 bytes --]

--- popen.scm.~1.11.~	2003-04-07 08:04:58.000000000 +1000
+++ popen.scm	2003-07-27 14:08:21.000000000 +1000
@@ -1,6 +1,6 @@
 ;; popen emulation, for non-stdio based ports.
 
-;;;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+;;;; Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
 ;;;; 
 ;;;; This library is free software; you can redistribute it and/or
 ;;;; modify it under the terms of the GNU Lesser General Public
@@ -90,14 +90,18 @@
 			  (set! output-fdes (dup->fdes 0)))
 		      (if (= error-fdes 0)
 			  (set! error-fdes (dup->fdes 0)))
-		      (dup2 input-fdes 0)))
+		      (dup2 input-fdes 0)
+		      (close-fdes input-fdes)))
 
 	       (cond ((not (= output-fdes 1))
 		      (if (= error-fdes 1)
 			  (set! error-fdes (dup->fdes 1)))
-		      (dup2 output-fdes 1)))
+		      (dup2 output-fdes 1)
+		      (close-fdes output-fdes)))
 
-	       (dup2 error-fdes 2)
+	       (cond ((not (= error-fdes 2))
+		      (dup2 error-fdes 2)
+		      (close-fdes error-fdes)))
 		     
 	       (apply execlp prog prog args)))
 

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: showfds.c --]
[-- Type: text/x-csrc, Size: 604 bytes --]

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>

int
main (void)
{
  int  i, found = 0;
  struct stat  st;
  fprintf (stderr, "file descriptors open:\n");

  for (i = 0; i < 20; i++)
    {
      if (fstat (i, &st) != -1)
        {
          found = 1;
          fprintf (stderr, "%d: %X %s\n",
                   i, st.st_mode,
                   S_ISREG (st.st_mode) ? "reg"
                   : S_ISCHR (st.st_mode) ? "chr"
                   : S_ISFIFO (st.st_mode) ? "fifo"
                   : "unknown");

        }
    }

  if (! found)
    fprintf (stderr, " none\n");

  return 0;
}

[-- Attachment #4: Type: text/plain, Size: 142 bytes --]

_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel

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

end of thread, other threads:[~2003-09-19  1:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-07-28 23:13 open-pipe fd duplications Kevin Ryde
2003-09-15 12:45 ` Marius Vollmer
2003-09-19  1:06   ` Kevin Ryde

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).