summaryrefslogtreecommitdiff
path: root/libio
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2008-07-28 23:34:19 +0000
committerJakub Jelinek <jakub@redhat.com>2008-07-28 23:34:19 +0000
commitc7045198ca8f4ff5b97205340d51127f8503c2bd (patch)
tree387ee7a78760f683df6035af28d665e3972aa30b /libio
parentc83494a925f4b4b716f9ba3abcb5e695d3e2a8a9 (diff)
Updated to fedora-glibc-20080728T2320cvs/fedora-glibc-2_8_90-10
Diffstat (limited to 'libio')
-rw-r--r--libio/Makefile4
-rw-r--r--libio/fileops.c4
-rw-r--r--libio/iopopen.c120
-rw-r--r--libio/tst-popen1.c49
4 files changed, 150 insertions, 27 deletions
diff --git a/libio/Makefile b/libio/Makefile
index 31fac70cfd..385040fb96 100644
--- a/libio/Makefile
+++ b/libio/Makefile
@@ -1,4 +1,4 @@
-# Copyright (C) 1995-2002,2003,2004,2006,2007 Free Software Foundation, Inc.
+# Copyright (C) 1995-2004,2006,2007,2008 Free Software Foundation, Inc.
# This file is part of the GNU C Library.
# The GNU C Library is free software; you can redistribute it and/or
@@ -58,7 +58,7 @@ tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc \
tst-memstream1 tst-memstream2 \
tst-wmemstream1 tst-wmemstream2 \
bug-memstream1 bug-wmemstream1 \
- tst-setvbuf1
+ tst-setvbuf1 tst-popen1
test-srcs = test-freopen
all: # Make this the default target; it will be defined in Rules.
diff --git a/libio/fileops.c b/libio/fileops.c
index 95cd2d64c9..cf47c915a7 100644
--- a/libio/fileops.c
+++ b/libio/fileops.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1993, 1995, 1997-2005, 2006, 2007
+/* Copyright (C) 1993, 1995, 1997-2005, 2006, 2007, 2008
Free Software Foundation, Inc.
This file is part of the GNU C Library.
Written by Per Bothner <bothner@cygnus.com>.
@@ -428,9 +428,11 @@ _IO_new_file_attach (fp, fd)
/* Get the current position of the file. */
/* We have to do that since that may be junk. */
fp->_offset = _IO_pos_BAD;
+ int save_errno = errno;
if (_IO_SEEKOFF (fp, (_IO_off64_t)0, _IO_seek_cur, _IOS_INPUT|_IOS_OUTPUT)
== _IO_pos_BAD && errno != ESPIPE)
return NULL;
+ __set_errno (save_errno);
return fp;
}
INTDEF2(_IO_new_file_attach, _IO_file_attach)
diff --git a/libio/iopopen.c b/libio/iopopen.c
index d5c6305b09..1a5cc0f592 100644
--- a/libio/iopopen.c
+++ b/libio/iopopen.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1993, 1997-2002, 2003, 2004, 2007
+/* Copyright (C) 1993, 1997-2002, 2003, 2004, 2007, 2008
Free Software Foundation, Inc.
This file is part of the GNU C Library.
Written by Per Bothner <bothner@cygnus.com>.
@@ -44,6 +44,7 @@
#endif
#include <sys/types.h>
#include <sys/wait.h>
+#include <kernel-features.h>
#ifndef _IO_fork
#ifdef _LIBC
@@ -56,15 +57,6 @@ extern _IO_pid_t _IO_fork (void) __THROW;
#endif /* _IO_HAVE_SYS_WAIT */
-#ifndef _IO_pipe
-#ifdef _LIBC
-#define _IO_pipe __pipe
-#else
-#define _IO_pipe pipe
-#endif
-extern int _IO_pipe (int des[2]) __THROW;
-#endif
-
#ifndef _IO_dup2
#ifdef _LIBC
#define _IO_dup2 __dup2
@@ -127,45 +119,103 @@ _IO_new_proc_open (fp, command, mode)
const char *mode;
{
#if _IO_HAVE_SYS_WAIT
- volatile int read_or_write;
- volatile int parent_end, child_end;
+ int read_or_write;
+ int parent_end, child_end;
int pipe_fds[2];
_IO_pid_t child_pid;
+
+ int do_read = 0;
+ int do_write = 0;
+ int do_cloexec = 0;
+ while (*mode != '\0')
+ switch (*mode++)
+ {
+ case 'r':
+ do_read = 1;
+ break;
+ case 'w':
+ do_write = 1;
+ break;
+ case 'e':
+ do_cloexec = 1;
+ break;
+ default:
+ errout:
+ __set_errno (EINVAL);
+ return NULL;
+ }
+
+ if ((do_read ^ do_write) == 0)
+ goto errout;
+
if (_IO_file_is_open (fp))
return NULL;
- if (_IO_pipe (pipe_fds) < 0)
- return NULL;
- if (mode[0] == 'r' && mode[1] == '\0')
+
+#ifdef O_CLOEXEC
+# ifndef __ASSUME_PIPE2
+ if (__have_pipe2 >= 0)
+# endif
+ {
+ int r = __pipe2 (pipe_fds, O_CLOEXEC);
+# ifndef __ASSUME_PIPE2
+ if (__have_pipe2 == 0)
+ __have_pipe2 = r != -1 || errno != ENOSYS ? 1 : -1;
+
+ if (__have_pipe2 > 0)
+# endif
+ if (r < 0)
+ return NULL;
+ }
+#endif
+#ifndef __ASSUME_PIPE2
+# ifdef O_CLOEXEC
+ if (__have_pipe2 < 0)
+# endif
+ if (__pipe (pipe_fds) < 0)
+ return NULL;
+#endif
+
+ if (do_read)
{
parent_end = pipe_fds[0];
child_end = pipe_fds[1];
read_or_write = _IO_NO_WRITES;
}
- else if (mode[0] == 'w' && mode[1] == '\0')
+ else
{
parent_end = pipe_fds[1];
child_end = pipe_fds[0];
read_or_write = _IO_NO_READS;
}
- else
- {
- _IO_close (pipe_fds[0]);
- _IO_close (pipe_fds[1]);
- __set_errno (EINVAL);
- return NULL;
- }
+
((_IO_proc_file *) fp)->pid = child_pid = _IO_fork ();
if (child_pid == 0)
{
- int child_std_end = mode[0] == 'r' ? 1 : 0;
+ int child_std_end = do_read ? 1 : 0;
struct _IO_proc_file *p;
+#ifndef __ASSUME_PIPE2
+ /* If we have pipe2 the descriptor is marked for close-on-exec. */
_IO_close (parent_end);
+#endif
if (child_end != child_std_end)
{
_IO_dup2 (child_end, child_std_end);
+#ifndef __ASSUME_PIPE2
_IO_close (child_end);
+#endif
+ }
+#ifdef O_CLOEXEC
+ else
+ {
+ /* The descriptor is already the one we will use. But it must
+ not be marked close-on-exec. Undo the effects. */
+# ifndef __ASSUME_PIPE2
+ if (__have_pipe2 > 0)
+# endif
+ __fcntl (child_end, F_SETFD, 0);
}
+#endif
/* POSIX.2: "popen() shall ensure that any streams from previous
popen() calls that remain open in the parent process are closed
in the new child process." */
@@ -189,6 +239,28 @@ _IO_new_proc_open (fp, command, mode)
_IO_close (parent_end);
return NULL;
}
+
+ if (do_cloexec)
+ {
+#ifndef __ASSUME_PIPE2
+# ifdef O_CLOEXEC
+ if (__have_pipe2 < 0)
+# endif
+ __fcntl (parent_end, F_SETFD, FD_CLOEXEC);
+#endif
+ }
+ else
+ {
+#ifdef O_CLOEXEC
+ /* Undo the effects of the pipe2 call which set the
+ close-on-exec flag. */
+# ifndef __ASSUME_PIPE2
+ if (__have_pipe2 > 0)
+# endif
+ __fcntl (parent_end, F_SETFD, 0);
+#endif
+ }
+
_IO_fileno (fp) = parent_end;
/* Link into proc_file_chain. */
diff --git a/libio/tst-popen1.c b/libio/tst-popen1.c
new file mode 100644
index 0000000000..bae6615b9b
--- /dev/null
+++ b/libio/tst-popen1.c
@@ -0,0 +1,49 @@
+#include <fcntl.h>
+#include <stdio.h>
+
+static int
+do_test (void)
+{
+ int res = 0;
+
+ FILE *fp = popen ("echo hello", "r");
+ if (fp == NULL)
+ {
+ puts ("first popen failed");
+ res = 1;
+ }
+ else
+ {
+ int fd = fileno (fp);
+ if (fcntl (fd, F_GETFD) == FD_CLOEXEC)
+ {
+ puts ("first popen(\"r\") set FD_CLOEXEC");
+ res = 1;
+ }
+
+ fclose (fp);
+ }
+
+ fp = popen ("echo hello", "re");
+ if (fp == NULL)
+ {
+ puts ("second popen failed");
+ res = 1;
+ }
+ else
+ {
+ int fd = fileno (fp);
+ if (fcntl (fd, F_GETFD) != FD_CLOEXEC)
+ {
+ puts ("second popen(\"r\") did not set FD_CLOEXEC");
+ res = 1;
+ }
+
+ fclose (fp);
+ }
+
+ return res;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"