unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Kevin Ryde <user42@zip.com.au>
Subject: open-pipe fd duplications
Date: Tue, 29 Jul 2003 09:13:18 +1000	[thread overview]
Message-ID: <87lluikzxt.fsf@zip.com.au> (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

             reply	other threads:[~2003-07-28 23:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-07-28 23:13 Kevin Ryde [this message]
2003-09-15 12:45 ` open-pipe fd duplications Marius Vollmer
2003-09-19  1:06   ` Kevin Ryde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87lluikzxt.fsf@zip.com.au \
    --to=user42@zip.com.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).