[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] fcntl-h: define O_CLOEXEC and O_EXEC if not defined; use new def
From: |
Paul Eggert |
Subject: |
[PATCH] fcntl-h: define O_CLOEXEC and O_EXEC if not defined; use new defines |
Date: |
Fri, 10 Sep 2010 11:59:57 -0700 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.12) Gecko/20100826 Thunderbird/3.0.7 |
The motivation for this is that I'm putting GNU tar on a file-descriptor
diet, and I needed O_CLOEXEC, which gnulib sometimes doesn't define.
It's more convenient for tar if gnulib defines O_CLOEXEC to 0 on platforms
that lack it, which is what gnulib already does for the other O_* flags.
This patch also adds O_EXEC. GNU tar doesn't need that, but might as well
add it while I'm in the neighborhood.
* doc/posix-headers/fcntl.texi (fcntl.h): Document that
O_CLOEXEC is now defined to 0 if it is not defined, like other flags.
Also, O_EXEC is now defined to be O_RDONLY if O_EXEC is not defined.
Similarly for O_SEARCH; this last was already true, but not documented.
* lib/fcntl.in.h (O_CLOEXEC): Define to 0 if not defined.
* lib/dup-safer-flag.c (O_CLOEXEC): Remove now-useless #define.
* lib/dup3.c, lib/pipe2.c, tests/test-dup-safer.c, tests/test-fcntl.c:
Likewise.
* lib/popen-safer.c (open_noinherit): Check whether O_CLOEXEC
is zero, not whether it is defined.
* tests/test-dup3.c, tests/test-pipe2.c (main): Likewise.
* lib/progreloc.c (find_executable): Use O_EXEC rather than O_RDONLY.
* lib/open.c (open): Check for O_SEARCH as well as for O_RDONLY.
---
ChangeLog | 17 +++++++++++++++++
doc/posix-headers/fcntl.texi | 19 ++++++++-----------
lib/dup-safer-flag.c | 4 ----
lib/dup3.c | 7 -------
lib/fcntl.in.h | 8 ++++++++
lib/open.c | 3 ++-
lib/pipe2.c | 7 -------
lib/popen-safer.c | 2 +-
lib/progreloc.c | 4 ++--
tests/test-dup-safer.c | 3 ---
tests/test-dup3.c | 4 ++--
tests/test-fcntl.c | 5 -----
tests/test-pipe2.c | 4 ++--
13 files changed, 42 insertions(+), 45 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 16f4a21..48a4d32 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2010-09-10 Paul Eggert <address@hidden>
+
+ fcntl-h: define O_CLOEXEC and O_EXEC if not defined; use new defines
+ * doc/posix-headers/fcntl.texi (fcntl.h): Document that
+ O_CLOEXEC is now defined to 0 if it is not defined, like other flags.
+ Also, O_EXEC is now defined to be O_RDONLY if O_EXEC is not defined.
+ Similarly for O_SEARCH; this last was already true, but not documented.
+ * lib/fcntl.in.h (O_CLOEXEC): Define to 0 if not defined.
+ * lib/dup-safer-flag.c (O_CLOEXEC): Remove now-useless #define.
+ * lib/dup3.c, lib/pipe2.c, tests/test-dup-safer.c, tests/test-fcntl.c:
+ Likewise.
+ * lib/popen-safer.c (open_noinherit): Check whether O_CLOEXEC
+ is zero, not whether it is defined.
+ * tests/test-dup3.c, tests/test-pipe2.c (main): Likewise.
+ * lib/progreloc.c (find_executable): Use O_EXEC rather than O_RDONLY.
+ * lib/open.c (open): Check for O_SEARCH as well as for O_RDONLY.
+
2010-09-10 Bruno Haible <address@hidden>
langinfo, nl_langinfo: Fix for IRIX 5.3.
diff --git a/doc/posix-headers/fcntl.texi b/doc/posix-headers/fcntl.texi
index 993db2d..1e3f977 100644
--- a/doc/posix-headers/fcntl.texi
+++ b/doc/posix-headers/fcntl.texi
@@ -8,9 +8,14 @@ Gnulib module: fcntl-h
Portability problems fixed by Gnulib:
@itemize
@item
address@hidden, @samp{O_DSYNC}, @samp{O_NONBLOCK}, @samp{O_RSYNC},
address@hidden, @samp{O_DIRECTORY}, @samp{O_NOFOLLOW}, and
address@hidden are not defined on some platforms.
address@hidden, @samp{O_DIRECTORY}, @samp{O_DSYNC}, @samp{O_NOCTTY},
address@hidden, @samp{O_NONBLOCK}, @samp{O_RSYNC}, @samp{O_SYNC},
+and @samp{O_TTY_INIT} are not defined on some platforms. Gnulib defines
+these macros to 0.
+
address@hidden
address@hidden and @samp{O_SEARCH} are not defined on some platforms.
+Gnulib defines these macros to @samp{O_RDONLY}, which is typically 0.
@item
@samp{O_BINARY}, @samp{O_TEXT} (not specified by POSIX, but essential for
@@ -49,14 +54,6 @@ Solaris 10.
Portability problems not fixed by Gnulib:
@itemize
@item
address@hidden is not defined on some platforms. The gnulib
-replacement is not atomic on these platforms.
-
address@hidden
address@hidden and @samp{O_EXEC} are not defined
-on some platforms.
-
address@hidden
@samp{F_SETFD}, @samp{F_GETFL}, @samp{F_SETFL}, @samp{F_GETLK},
@samp{F_SETLK}, @samp{F_SETLOKW}, @samp{F_GETOWN}, and @samp{F_SETOWN}
are not defined on some platforms:
diff --git a/lib/dup-safer-flag.c b/lib/dup-safer-flag.c
index 10d6a1b..e4c0597 100644
--- a/lib/dup-safer-flag.c
+++ b/lib/dup-safer-flag.c
@@ -28,10 +28,6 @@
#include "cloexec.h"
-#ifndef O_CLOEXEC
-# define O_CLOEXEC 0
-#endif
-
/* Like dup, but do not return STDIN_FILENO, STDOUT_FILENO, or
STDERR_FILENO. If FLAG contains O_CLOEXEC, behave like
fcntl(F_DUPFD_CLOEXEC) rather than fcntl(F_DUPFD). */
diff --git a/lib/dup3.c b/lib/dup3.c
index d44aa92..5ae8b44 100644
--- a/lib/dup3.c
+++ b/lib/dup3.c
@@ -38,13 +38,6 @@
/* Upper bound on getdtablesize(). See lib/getdtablesize.c. */
# define OPEN_MAX_MAX 0x10000
-#else
-/* Unix API. */
-
-# ifndef O_CLOEXEC
-# define O_CLOEXEC 0
-# endif
-
#endif
int
diff --git a/lib/fcntl.in.h b/lib/fcntl.in.h
index 42fb81b..a7e4a950 100644
--- a/lib/fcntl.in.h
+++ b/lib/fcntl.in.h
@@ -170,6 +170,10 @@ _GL_WARN_ON_USE (openat, "openat is not portable - "
# define O_CLOEXEC O_NOINHERIT
#endif
+#ifndef O_CLOEXEC
+# define O_CLOEXEC 0
+#endif
+
#ifndef O_DIRECT
# define O_DIRECT 0
#endif
@@ -182,6 +186,10 @@ _GL_WARN_ON_USE (openat, "openat is not portable - "
# define O_DSYNC 0
#endif
+#ifndef O_EXEC
+# define O_EXEC O_RDONLY /* This is often close enough in older systems. */
+#endif
+
#ifndef O_NDELAY
# define O_NDELAY 0
#endif
diff --git a/lib/open.c b/lib/open.c
index f612b80..4e44936 100644
--- a/lib/open.c
+++ b/lib/open.c
@@ -111,7 +111,8 @@ open (const char *filename, int flags, ...)
override fstat() in fchdir.c to hide the fact that we have a
dummy. */
if (REPLACE_OPEN_DIRECTORY && fd < 0 && errno == EACCES
- && (flags & O_ACCMODE) == O_RDONLY)
+ && ((flags & O_ACCMODE) == O_RDONLY
+ || (O_SEARCH != O_RDONLY && (flags & O_ACCMODE) == O_SEARCH)))
{
struct stat statbuf;
if (stat (filename, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
diff --git a/lib/pipe2.c b/lib/pipe2.c
index 2d973dc..8257fcf 100644
--- a/lib/pipe2.c
+++ b/lib/pipe2.c
@@ -30,13 +30,6 @@
# include <io.h>
-#else
-/* Unix API. */
-
-# ifndef O_CLOEXEC
-# define O_CLOEXEC 0
-# endif
-
#endif
int
diff --git a/lib/popen-safer.c b/lib/popen-safer.c
index 1791bc3..be67c76 100644
--- a/lib/popen-safer.c
+++ b/lib/popen-safer.c
@@ -34,7 +34,7 @@ static int
open_noinherit (char const *name, int flags)
{
int fd;
-#ifdef O_CLOEXEC
+#if O_CLOEXEC
/* 0 = unknown, 1 = yes, -1 = no. */
static int have_cloexec;
if (have_cloexec >= 0)
diff --git a/lib/progreloc.c b/lib/progreloc.c
index 1e27de7..19bbc12 100644
--- a/lib/progreloc.c
+++ b/lib/progreloc.c
@@ -189,7 +189,7 @@ find_executable (const char *argv0)
if (link != NULL && link[0] != '[')
return link;
if (executable_fd < 0)
- executable_fd = open ("/proc/self/exe", O_RDONLY, 0);
+ executable_fd = open ("/proc/self/exe", O_EXEC, 0);
{
char buf[6+10+5];
@@ -198,7 +198,7 @@ find_executable (const char *argv0)
if (link != NULL && link[0] != '[')
return link;
if (executable_fd < 0)
- executable_fd = open (buf, O_RDONLY, 0);
+ executable_fd = open (buf, O_EXEC, 0);
}
}
#endif
diff --git a/tests/test-dup-safer.c b/tests/test-dup-safer.c
index 28f1317..b71b6c3 100644
--- a/tests/test-dup-safer.c
+++ b/tests/test-dup-safer.c
@@ -38,9 +38,6 @@
# define setmode(f,m) zero ()
static int zero (void) { return 0; }
#endif
-#ifndef O_CLOEXEC
-# define O_CLOEXEC 0
-#endif
/* This test intentionally closes stderr. So, we arrange to have fd 10
(outside the range of interesting fd's during the test) set up to
diff --git a/tests/test-dup3.c b/tests/test-dup3.c
index a719234..f1e978b 100644
--- a/tests/test-dup3.c
+++ b/tests/test-dup3.c
@@ -75,7 +75,7 @@ main ()
{
int use_cloexec;
-#if defined O_CLOEXEC
+#if O_CLOEXEC
for (use_cloexec = 0; use_cloexec <= 1; use_cloexec++)
#else
use_cloexec = 0;
@@ -87,7 +87,7 @@ main ()
char buffer[1];
o_flags = 0;
-#if defined O_CLOEXEC
+#if O_CLOEXEC
if (use_cloexec)
o_flags |= O_CLOEXEC;
#endif
diff --git a/tests/test-fcntl.c b/tests/test-fcntl.c
index 71dfb98..a181da3 100644
--- a/tests/test-fcntl.c
+++ b/tests/test-fcntl.c
@@ -39,11 +39,6 @@ SIGNATURE_CHECK (fcntl, int, (int, int, ...));
#include "binary-io.h"
#include "macros.h"
-/* Use O_CLOEXEC if available, but test works without it. */
-#ifndef O_CLOEXEC
-# define O_CLOEXEC 0
-#endif
-
#if !O_BINARY
# define setmode(f,m) zero ()
static int zero (void) { return 0; }
diff --git a/tests/test-pipe2.c b/tests/test-pipe2.c
index bd6df7c..e8e5798 100644
--- a/tests/test-pipe2.c
+++ b/tests/test-pipe2.c
@@ -92,7 +92,7 @@ main ()
#else
use_nonblocking = 0;
#endif
-#if defined O_CLOEXEC
+#if O_CLOEXEC
for (use_cloexec = 0; use_cloexec <= 1; use_cloexec++)
#else
use_cloexec = 0;
@@ -106,7 +106,7 @@ main ()
if (use_nonblocking)
o_flags |= O_NONBLOCK;
#endif
-#if defined O_CLOEXEC
+#if O_CLOEXEC
if (use_cloexec)
o_flags |= O_CLOEXEC;
#endif
--
1.7.2
- [PATCH] fcntl-h: define O_CLOEXEC and O_EXEC if not defined; use new defines,
Paul Eggert <=