aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert2017-08-16 13:55:46 -0700
committerPaul Eggert2017-08-16 14:04:33 -0700
commit794c3cd3a2dfcedc829ccb5dc413c99fb670f4a1 (patch)
tree728839628834cbd80dc3bd8f795e9a5598fdffaf
parent69f2b755f44a5e447b3ad482ce0b409764fa10e6 (diff)
downloademacs-794c3cd3a2dfcedc829ccb5dc413c99fb670f4a1.tar.gz
emacs-794c3cd3a2dfcedc829ccb5dc413c99fb670f4a1.zip
Merge from Gnulib; use ‘open’ for O_CLOEXEC
This incorporates: 2017-08-15 renameat: ensure declaration in <stdio.h> on NetBSD 2017-08-15 extensions: enable NetBSD specific extensions 2017-08-14 open: support O_CLOEXEC 2017-08-13 reallocarray: new module * admin/merge-gnulib (AVOIDED_MODULES): Remove ‘open’, since it now supports O_CLOEXEC and this simplifies Emacs. * build-aux/config.guess, lib/fcntl.in.h, lib/stdio.in.h: * lib/stdlib.in.h, m4/extensions.m4, m4/stdlib_h.m4: Copy from Gnulib. * lib/cloexec.c, lib/cloexec.h, lib/open.c: * m4/mode_t.m4, m4/open-cloexec.m4, m4/open.m4: New files, copied from Gnulib. * lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate. * lib-src/etags.c (O_CLOEXEC) [WINDOWSNT]: Remove, as Gnulib does this for us. * src/filelock.c (create_lock_file): * src/sysdep.c (emacs_open, emacs_pipe): Don’t worry about O_CLOEXEC == 0, as Gnulib no longer sets it to 0.
-rwxr-xr-xadmin/merge-gnulib2
-rwxr-xr-xbuild-aux/config.guess5
-rw-r--r--lib-src/etags.c1
-rw-r--r--lib/cloexec.c83
-rw-r--r--lib/cloexec.h38
-rw-r--r--lib/fcntl.in.h5
-rw-r--r--lib/gnulib.mk.in33
-rw-r--r--lib/open.c208
-rw-r--r--lib/stdio.in.h4
-rw-r--r--lib/stdlib.in.h17
-rw-r--r--m4/extensions.m47
-rw-r--r--m4/gnulib-comp.m435
-rw-r--r--m4/mode_t.m426
-rw-r--r--m4/open-cloexec.m421
-rw-r--r--m4/open.m495
-rw-r--r--m4/stdlib_h.m48
-rw-r--r--src/filelock.c2
-rw-r--r--src/sysdep.c10
18 files changed, 578 insertions, 22 deletions
diff --git a/admin/merge-gnulib b/admin/merge-gnulib
index 7eca64305de..e7b304a264d 100755
--- a/admin/merge-gnulib
+++ b/admin/merge-gnulib
@@ -48,7 +48,7 @@ GNULIB_MODULES='
48AVOIDED_MODULES=' 48AVOIDED_MODULES='
49 close dup fchdir fstat 49 close dup fchdir fstat
50 malloc-posix msvc-inval msvc-nothrow 50 malloc-posix msvc-inval msvc-nothrow
51 open openat-die opendir raise 51 openat-die opendir raise
52 save-cwd select setenv sigprocmask stat stdarg stdbool 52 save-cwd select setenv sigprocmask stat stdarg stdbool
53 threadlib tzset unsetenv utime utime-h 53 threadlib tzset unsetenv utime utime-h
54' 54'
diff --git a/build-aux/config.guess b/build-aux/config.guess
index 07785f54515..a7448442748 100755
--- a/build-aux/config.guess
+++ b/build-aux/config.guess
@@ -2,7 +2,7 @@
2# Attempt to guess a canonical system name. 2# Attempt to guess a canonical system name.
3# Copyright 1992-2017 Free Software Foundation, Inc. 3# Copyright 1992-2017 Free Software Foundation, Inc.
4 4
5timestamp='2017-07-19' 5timestamp='2017-08-08'
6 6
7# This file is free software; you can redistribute it and/or modify it 7# This file is free software; you can redistribute it and/or modify it
8# under the terms of the GNU General Public License as published by 8# under the terms of the GNU General Public License as published by
@@ -259,6 +259,9 @@ case "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" in
259 *:Sortix:*:*) 259 *:Sortix:*:*)
260 echo ${UNAME_MACHINE}-unknown-sortix 260 echo ${UNAME_MACHINE}-unknown-sortix
261 exit ;; 261 exit ;;
262 *:Redox:*:*)
263 echo ${UNAME_MACHINE}-unknown-redox
264 exit ;;
262 alpha:OSF1:*:*) 265 alpha:OSF1:*:*)
263 case $UNAME_RELEASE in 266 case $UNAME_RELEASE in
264 *4.0) 267 *4.0)
diff --git a/lib-src/etags.c b/lib-src/etags.c
index 7b1a7fc1851..bec61a8a230 100644
--- a/lib-src/etags.c
+++ b/lib-src/etags.c
@@ -111,7 +111,6 @@ char pot_etags_version[] = "@(#) pot revision number is 17.38.1.4";
111# undef HAVE_NTGUI 111# undef HAVE_NTGUI
112# undef DOS_NT 112# undef DOS_NT
113# define DOS_NT 113# define DOS_NT
114# define O_CLOEXEC O_NOINHERIT
115#endif /* WINDOWSNT */ 114#endif /* WINDOWSNT */
116 115
117#include <limits.h> 116#include <limits.h>
diff --git a/lib/cloexec.c b/lib/cloexec.c
new file mode 100644
index 00000000000..e34aef87976
--- /dev/null
+++ b/lib/cloexec.c
@@ -0,0 +1,83 @@
1/* cloexec.c - set or clear the close-on-exec descriptor flag
2
3 Copyright (C) 1991, 2004-2006, 2009-2017 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 The code is taken from glibc/manual/llio.texi */
19
20#include <config.h>
21
22#include "cloexec.h"
23
24#include <errno.h>
25#include <fcntl.h>
26#include <unistd.h>
27
28/* Set the 'FD_CLOEXEC' flag of DESC if VALUE is true,
29 or clear the flag if VALUE is false.
30 Return 0 on success, or -1 on error with 'errno' set.
31
32 Note that on MingW, this function does NOT protect DESC from being
33 inherited into spawned children. Instead, either use dup_cloexec
34 followed by closing the original DESC, or use interfaces such as
35 open or pipe2 that accept flags like O_CLOEXEC to create DESC
36 non-inheritable in the first place. */
37
38int
39set_cloexec_flag (int desc, bool value)
40{
41#ifdef F_SETFD
42
43 int flags = fcntl (desc, F_GETFD, 0);
44
45 if (0 <= flags)
46 {
47 int newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC);
48
49 if (flags == newflags
50 || fcntl (desc, F_SETFD, newflags) != -1)
51 return 0;
52 }
53
54 return -1;
55
56#else /* !F_SETFD */
57
58 /* Use dup2 to reject invalid file descriptors; the cloexec flag
59 will be unaffected. */
60 if (desc < 0)
61 {
62 errno = EBADF;
63 return -1;
64 }
65 if (dup2 (desc, desc) < 0)
66 /* errno is EBADF here. */
67 return -1;
68
69 /* There is nothing we can do on this kind of platform. Punt. */
70 return 0;
71#endif /* !F_SETFD */
72}
73
74
75/* Duplicates a file handle FD, while marking the copy to be closed
76 prior to exec or spawn. Returns -1 and sets errno if FD could not
77 be duplicated. */
78
79int
80dup_cloexec (int fd)
81{
82 return fcntl (fd, F_DUPFD_CLOEXEC, 0);
83}
diff --git a/lib/cloexec.h b/lib/cloexec.h
new file mode 100644
index 00000000000..cdaf422226d
--- /dev/null
+++ b/lib/cloexec.h
@@ -0,0 +1,38 @@
1/* cloexec.c - set or clear the close-on-exec descriptor flag
2
3 Copyright (C) 2004, 2009-2017 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18*/
19
20#include <stdbool.h>
21
22/* Set the 'FD_CLOEXEC' flag of DESC if VALUE is true,
23 or clear the flag if VALUE is false.
24 Return 0 on success, or -1 on error with 'errno' set.
25
26 Note that on MingW, this function does NOT protect DESC from being
27 inherited into spawned children. Instead, either use dup_cloexec
28 followed by closing the original DESC, or use interfaces such as
29 open or pipe2 that accept flags like O_CLOEXEC to create DESC
30 non-inheritable in the first place. */
31
32int set_cloexec_flag (int desc, bool value);
33
34/* Duplicates a file handle FD, while marking the copy to be closed
35 prior to exec or spawn. Returns -1 and sets errno if FD could not
36 be duplicated. */
37
38int dup_cloexec (int fd);
diff --git a/lib/fcntl.in.h b/lib/fcntl.in.h
index 4a1d40af6df..076d1ac34a7 100644
--- a/lib/fcntl.in.h
+++ b/lib/fcntl.in.h
@@ -213,7 +213,10 @@ _GL_WARN_ON_USE (openat, "openat is not portable - "
213#endif 213#endif
214 214
215#ifndef O_CLOEXEC 215#ifndef O_CLOEXEC
216# define O_CLOEXEC 0 216# define O_CLOEXEC 0x40000000 /* Try to not collide with system O_* flags. */
217# define GNULIB_defined_O_CLOEXEC 1
218#else
219# define GNULIB_defined_O_CLOEXEC 0
217#endif 220#endif
218 221
219#ifndef O_DIRECT 222#ifndef O_DIRECT
diff --git a/lib/gnulib.mk.in b/lib/gnulib.mk.in
index 30986b4ed7f..b6eb0f69539 100644
--- a/lib/gnulib.mk.in
+++ b/lib/gnulib.mk.in
@@ -21,7 +21,7 @@
21# the same distribution terms as the rest of that program. 21# the same distribution terms as the rest of that program.
22# 22#
23# Generated by gnulib-tool. 23# Generated by gnulib-tool.
24# Reproduce by: gnulib-tool --import --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=close --avoid=dup --avoid=fchdir --avoid=fstat --avoid=malloc-posix --avoid=msvc-inval --avoid=msvc-nothrow --avoid=open --avoid=openat-die --avoid=opendir --avoid=raise --avoid=save-cwd --avoid=select --avoid=setenv --avoid=sigprocmask --avoid=stat --avoid=stdarg --avoid=stdbool --avoid=threadlib --avoid=tzset --avoid=unsetenv --avoid=utime --avoid=utime-h --gnu-make --makefile-name=gnulib.mk.in --conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files alloca-opt binary-io byteswap c-ctype c-strcase careadlinkat close-stream count-leading-zeros count-one-bits count-trailing-zeros crypto/md5 crypto/sha1 crypto/sha256 crypto/sha512 d-type diffseq dtoastr dtotimespec dup2 environ execinfo explicit_bzero faccessat fcntl fcntl-h fdatasync fdopendir filemode filevercmp flexmember fstatat fsync getloadavg getopt-gnu gettime gettimeofday gitlog-to-changelog ignore-value intprops largefile lstat manywarnings memrchr minmax mkostemp mktime nstrftime pipe2 pselect pthread_sigmask putenv qcopy-acl readlink readlinkat sig2str socklen stat-time std-gnu11 stdalign stddef stdio stpcpy strtoimax symlink sys_stat sys_time tempname time time_r time_rz timegm timer-time timespec-add timespec-sub unlocked-io update-copyright utimens vla warnings 24# Reproduce by: gnulib-tool --import --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=close --avoid=dup --avoid=fchdir --avoid=fstat --avoid=malloc-posix --avoid=msvc-inval --avoid=msvc-nothrow --avoid=openat-die --avoid=opendir --avoid=raise --avoid=save-cwd --avoid=select --avoid=setenv --avoid=sigprocmask --avoid=stat --avoid=stdarg --avoid=stdbool --avoid=threadlib --avoid=tzset --avoid=unsetenv --avoid=utime --avoid=utime-h --gnu-make --makefile-name=gnulib.mk.in --conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files alloca-opt binary-io byteswap c-ctype c-strcase careadlinkat close-stream count-leading-zeros count-one-bits count-trailing-zeros crypto/md5 crypto/sha1 crypto/sha256 crypto/sha512 d-type diffseq dtoastr dtotimespec dup2 environ execinfo explicit_bzero faccessat fcntl fcntl-h fdatasync fdopendir filemode filevercmp flexmember fstatat fsync getloadavg getopt-gnu gettime gettimeofday gitlog-to-changelog ignore-value intprops largefile lstat manywarnings memrchr minmax mkostemp mktime nstrftime pipe2 pselect pthread_sigmask putenv qcopy-acl readlink readlinkat sig2str socklen stat-time std-gnu11 stdalign stddef stdio stpcpy strtoimax symlink sys_stat sys_time tempname time time_r time_rz timegm timer-time timespec-add timespec-sub unlocked-io update-copyright utimens vla warnings
25 25
26 26
27MOSTLYCLEANFILES += core *.stackdump 27MOSTLYCLEANFILES += core *.stackdump
@@ -255,6 +255,7 @@ GNULIB_READ = @GNULIB_READ@
255GNULIB_READDIR = @GNULIB_READDIR@ 255GNULIB_READDIR = @GNULIB_READDIR@
256GNULIB_READLINK = @GNULIB_READLINK@ 256GNULIB_READLINK = @GNULIB_READLINK@
257GNULIB_READLINKAT = @GNULIB_READLINKAT@ 257GNULIB_READLINKAT = @GNULIB_READLINKAT@
258GNULIB_REALLOCARRAY = @GNULIB_REALLOCARRAY@
258GNULIB_REALLOC_POSIX = @GNULIB_REALLOC_POSIX@ 259GNULIB_REALLOC_POSIX = @GNULIB_REALLOC_POSIX@
259GNULIB_REALPATH = @GNULIB_REALPATH@ 260GNULIB_REALPATH = @GNULIB_REALPATH@
260GNULIB_REMOVE = @GNULIB_REMOVE@ 261GNULIB_REMOVE = @GNULIB_REMOVE@
@@ -465,6 +466,7 @@ HAVE_RAWMEMCHR = @HAVE_RAWMEMCHR@
465HAVE_READDIR = @HAVE_READDIR@ 466HAVE_READDIR = @HAVE_READDIR@
466HAVE_READLINK = @HAVE_READLINK@ 467HAVE_READLINK = @HAVE_READLINK@
467HAVE_READLINKAT = @HAVE_READLINKAT@ 468HAVE_READLINKAT = @HAVE_READLINKAT@
469HAVE_REALLOCARRAY = @HAVE_REALLOCARRAY@
468HAVE_REALPATH = @HAVE_REALPATH@ 470HAVE_REALPATH = @HAVE_REALPATH@
469HAVE_RENAMEAT = @HAVE_RENAMEAT@ 471HAVE_RENAMEAT = @HAVE_RENAMEAT@
470HAVE_REWINDDIR = @HAVE_REWINDDIR@ 472HAVE_REWINDDIR = @HAVE_REWINDDIR@
@@ -898,11 +900,13 @@ gl_GNULIB_ENABLED_6099e9737f757db36c47fa9d9f02e88c = @gl_GNULIB_ENABLED_6099e973
898gl_GNULIB_ENABLED_682e609604ccaac6be382e4ee3a4eaec = @gl_GNULIB_ENABLED_682e609604ccaac6be382e4ee3a4eaec@ 900gl_GNULIB_ENABLED_682e609604ccaac6be382e4ee3a4eaec = @gl_GNULIB_ENABLED_682e609604ccaac6be382e4ee3a4eaec@
899gl_GNULIB_ENABLED_a9786850e999ae65a836a6041e8e5ed1 = @gl_GNULIB_ENABLED_a9786850e999ae65a836a6041e8e5ed1@ 901gl_GNULIB_ENABLED_a9786850e999ae65a836a6041e8e5ed1 = @gl_GNULIB_ENABLED_a9786850e999ae65a836a6041e8e5ed1@
900gl_GNULIB_ENABLED_be453cec5eecf5731a274f2de7f2db36 = @gl_GNULIB_ENABLED_be453cec5eecf5731a274f2de7f2db36@ 902gl_GNULIB_ENABLED_be453cec5eecf5731a274f2de7f2db36 = @gl_GNULIB_ENABLED_be453cec5eecf5731a274f2de7f2db36@
903gl_GNULIB_ENABLED_cloexec = @gl_GNULIB_ENABLED_cloexec@
901gl_GNULIB_ENABLED_dirfd = @gl_GNULIB_ENABLED_dirfd@ 904gl_GNULIB_ENABLED_dirfd = @gl_GNULIB_ENABLED_dirfd@
902gl_GNULIB_ENABLED_dosname = @gl_GNULIB_ENABLED_dosname@ 905gl_GNULIB_ENABLED_dosname = @gl_GNULIB_ENABLED_dosname@
903gl_GNULIB_ENABLED_euidaccess = @gl_GNULIB_ENABLED_euidaccess@ 906gl_GNULIB_ENABLED_euidaccess = @gl_GNULIB_ENABLED_euidaccess@
904gl_GNULIB_ENABLED_getdtablesize = @gl_GNULIB_ENABLED_getdtablesize@ 907gl_GNULIB_ENABLED_getdtablesize = @gl_GNULIB_ENABLED_getdtablesize@
905gl_GNULIB_ENABLED_getgroups = @gl_GNULIB_ENABLED_getgroups@ 908gl_GNULIB_ENABLED_getgroups = @gl_GNULIB_ENABLED_getgroups@
909gl_GNULIB_ENABLED_open = @gl_GNULIB_ENABLED_open@
906gl_GNULIB_ENABLED_strtoll = @gl_GNULIB_ENABLED_strtoll@ 910gl_GNULIB_ENABLED_strtoll = @gl_GNULIB_ENABLED_strtoll@
907gl_LIBOBJS = @gl_LIBOBJS@ 911gl_LIBOBJS = @gl_LIBOBJS@
908gl_LTLIBOBJS = @gl_LTLIBOBJS@ 912gl_LTLIBOBJS = @gl_LTLIBOBJS@
@@ -1085,6 +1089,18 @@ EXTRA_DIST += careadlinkat.h
1085endif 1089endif
1086## end gnulib module careadlinkat 1090## end gnulib module careadlinkat
1087 1091
1092## begin gnulib module cloexec
1093ifeq (,$(OMIT_GNULIB_MODULE_cloexec))
1094
1095ifneq (,$(gl_GNULIB_ENABLED_cloexec))
1096libgnu_a_SOURCES += cloexec.c
1097
1098endif
1099EXTRA_DIST += cloexec.h
1100
1101endif
1102## end gnulib module cloexec
1103
1088## begin gnulib module close-stream 1104## begin gnulib module close-stream
1089ifeq (,$(OMIT_GNULIB_MODULE_close-stream)) 1105ifeq (,$(OMIT_GNULIB_MODULE_close-stream))
1090 1106
@@ -1817,6 +1833,19 @@ EXTRA_DIST += strftime.h
1817endif 1833endif
1818## end gnulib module nstrftime 1834## end gnulib module nstrftime
1819 1835
1836## begin gnulib module open
1837ifeq (,$(OMIT_GNULIB_MODULE_open))
1838
1839ifneq (,$(gl_GNULIB_ENABLED_open))
1840
1841endif
1842EXTRA_DIST += open.c
1843
1844EXTRA_libgnu_a_SOURCES += open.c
1845
1846endif
1847## end gnulib module open
1848
1820## begin gnulib module openat-h 1849## begin gnulib module openat-h
1821ifeq (,$(OMIT_GNULIB_MODULE_openat-h)) 1850ifeq (,$(OMIT_GNULIB_MODULE_openat-h))
1822 1851
@@ -2311,6 +2340,7 @@ stdlib.h: stdlib.in.h $(top_builddir)/config.status $(CXXDEFS_H) \
2311 -e 's/@''GNULIB_RANDOM''@/$(GNULIB_RANDOM)/g' \ 2340 -e 's/@''GNULIB_RANDOM''@/$(GNULIB_RANDOM)/g' \
2312 -e 's/@''GNULIB_RANDOM_R''@/$(GNULIB_RANDOM_R)/g' \ 2341 -e 's/@''GNULIB_RANDOM_R''@/$(GNULIB_RANDOM_R)/g' \
2313 -e 's/@''GNULIB_REALLOC_POSIX''@/$(GNULIB_REALLOC_POSIX)/g' \ 2342 -e 's/@''GNULIB_REALLOC_POSIX''@/$(GNULIB_REALLOC_POSIX)/g' \
2343 -e 's/@''GNULIB_REALLOCARRAY''@/$(GNULIB_REALLOCARRAY)/g' \
2314 -e 's/@''GNULIB_REALPATH''@/$(GNULIB_REALPATH)/g' \ 2344 -e 's/@''GNULIB_REALPATH''@/$(GNULIB_REALPATH)/g' \
2315 -e 's/@''GNULIB_RPMATCH''@/$(GNULIB_RPMATCH)/g' \ 2345 -e 's/@''GNULIB_RPMATCH''@/$(GNULIB_RPMATCH)/g' \
2316 -e 's/@''GNULIB_SECURE_GETENV''@/$(GNULIB_SECURE_GETENV)/g' \ 2346 -e 's/@''GNULIB_SECURE_GETENV''@/$(GNULIB_SECURE_GETENV)/g' \
@@ -2341,6 +2371,7 @@ stdlib.h: stdlib.in.h $(top_builddir)/config.status $(CXXDEFS_H) \
2341 -e 's|@''HAVE_RANDOM''@|$(HAVE_RANDOM)|g' \ 2371 -e 's|@''HAVE_RANDOM''@|$(HAVE_RANDOM)|g' \
2342 -e 's|@''HAVE_RANDOM_H''@|$(HAVE_RANDOM_H)|g' \ 2372 -e 's|@''HAVE_RANDOM_H''@|$(HAVE_RANDOM_H)|g' \
2343 -e 's|@''HAVE_RANDOM_R''@|$(HAVE_RANDOM_R)|g' \ 2373 -e 's|@''HAVE_RANDOM_R''@|$(HAVE_RANDOM_R)|g' \
2374 -e 's|@''HAVE_REALLOCARRAY''@|$(HAVE_REALLOCARRAY)|g' \
2344 -e 's|@''HAVE_REALPATH''@|$(HAVE_REALPATH)|g' \ 2375 -e 's|@''HAVE_REALPATH''@|$(HAVE_REALPATH)|g' \
2345 -e 's|@''HAVE_RPMATCH''@|$(HAVE_RPMATCH)|g' \ 2376 -e 's|@''HAVE_RPMATCH''@|$(HAVE_RPMATCH)|g' \
2346 -e 's|@''HAVE_SECURE_GETENV''@|$(HAVE_SECURE_GETENV)|g' \ 2377 -e 's|@''HAVE_SECURE_GETENV''@|$(HAVE_SECURE_GETENV)|g' \
diff --git a/lib/open.c b/lib/open.c
new file mode 100644
index 00000000000..c62f02b1452
--- /dev/null
+++ b/lib/open.c
@@ -0,0 +1,208 @@
1/* Open a descriptor to a file.
2 Copyright (C) 2007-2017 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
17/* Written by Bruno Haible <bruno@clisp.org>, 2007. */
18
19/* If the user's config.h happens to include <fcntl.h>, let it include only
20 the system's <fcntl.h> here, so that orig_open doesn't recurse to
21 rpl_open. */
22#define __need_system_fcntl_h
23#include <config.h>
24
25/* Get the original definition of open. It might be defined as a macro. */
26#include <fcntl.h>
27#include <sys/types.h>
28#undef __need_system_fcntl_h
29
30static int
31orig_open (const char *filename, int flags, mode_t mode)
32{
33 return open (filename, flags, mode);
34}
35
36/* Specification. */
37/* Write "fcntl.h" here, not <fcntl.h>, otherwise OSF/1 5.1 DTK cc eliminates
38 this include because of the preliminary #include <fcntl.h> above. */
39#include "fcntl.h"
40
41#include "cloexec.h"
42
43#include <errno.h>
44#include <stdarg.h>
45#include <string.h>
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <unistd.h>
49
50#ifndef REPLACE_OPEN_DIRECTORY
51# define REPLACE_OPEN_DIRECTORY 0
52#endif
53
54int
55open (const char *filename, int flags, ...)
56{
57 /* 0 = unknown, 1 = yes, -1 = no. */
58#if GNULIB_defined_O_CLOEXEC
59 int have_cloexec = -1;
60#else
61 static int have_cloexec;
62#endif
63
64 mode_t mode;
65 int fd;
66
67 mode = 0;
68 if (flags & O_CREAT)
69 {
70 va_list arg;
71 va_start (arg, flags);
72
73 /* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4
74 creates crashing code when 'mode_t' is smaller than 'int'. */
75 mode = va_arg (arg, PROMOTED_MODE_T);
76
77 va_end (arg);
78 }
79
80#if GNULIB_defined_O_NONBLOCK
81 /* The only known platform that lacks O_NONBLOCK is mingw, but it
82 also lacks named pipes and Unix sockets, which are the only two
83 file types that require non-blocking handling in open().
84 Therefore, it is safe to ignore O_NONBLOCK here. It is handy
85 that mingw also lacks openat(), so that is also covered here. */
86 flags &= ~O_NONBLOCK;
87#endif
88
89#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
90 if (strcmp (filename, "/dev/null") == 0)
91 filename = "NUL";
92#endif
93
94#if OPEN_TRAILING_SLASH_BUG
95 /* If the filename ends in a slash and one of O_CREAT, O_WRONLY, O_RDWR
96 is specified, then fail.
97 Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
98 says that
99 "A pathname that contains at least one non-slash character and that
100 ends with one or more trailing slashes shall be resolved as if a
101 single dot character ( '.' ) were appended to the pathname."
102 and
103 "The special filename dot shall refer to the directory specified by
104 its predecessor."
105 If the named file already exists as a directory, then
106 - if O_CREAT is specified, open() must fail because of the semantics
107 of O_CREAT,
108 - if O_WRONLY or O_RDWR is specified, open() must fail because POSIX
109 <http://www.opengroup.org/susv3/functions/open.html> says that it
110 fails with errno = EISDIR in this case.
111 If the named file does not exist or does not name a directory, then
112 - if O_CREAT is specified, open() must fail since open() cannot create
113 directories,
114 - if O_WRONLY or O_RDWR is specified, open() must fail because the
115 file does not contain a '.' directory. */
116 if (flags & (O_CREAT | O_WRONLY | O_RDWR))
117 {
118 size_t len = strlen (filename);
119 if (len > 0 && filename[len - 1] == '/')
120 {
121 errno = EISDIR;
122 return -1;
123 }
124 }
125#endif
126
127 fd = orig_open (filename,
128 flags & ~(have_cloexec <= 0 ? O_CLOEXEC : 0), mode);
129
130 if (flags & O_CLOEXEC)
131 {
132 if (! have_cloexec)
133 {
134 if (0 <= fd)
135 have_cloexec = 1;
136 else if (errno == EINVAL)
137 {
138 fd = orig_open (filename, flags & ~O_CLOEXEC, mode);
139 have_cloexec = -1;
140 }
141 }
142 if (have_cloexec < 0 && 0 <= fd)
143 set_cloexec_flag (fd, true);
144 }
145
146
147#if REPLACE_FCHDIR
148 /* Implementing fchdir and fdopendir requires the ability to open a
149 directory file descriptor. If open doesn't support that (as on
150 mingw), we use a dummy file that behaves the same as directories
151 on Linux (ie. always reports EOF on attempts to read()), and
152 override fstat() in fchdir.c to hide the fact that we have a
153 dummy. */
154 if (REPLACE_OPEN_DIRECTORY && fd < 0 && errno == EACCES
155 && ((flags & O_ACCMODE) == O_RDONLY
156 || (O_SEARCH != O_RDONLY && (flags & O_ACCMODE) == O_SEARCH)))
157 {
158 struct stat statbuf;
159 if (stat (filename, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
160 {
161 /* Maximum recursion depth of 1. */
162 fd = open ("/dev/null", flags, mode);
163 if (0 <= fd)
164 fd = _gl_register_fd (fd, filename);
165 }
166 else
167 errno = EACCES;
168 }
169#endif
170
171#if OPEN_TRAILING_SLASH_BUG
172 /* If the filename ends in a slash and fd does not refer to a directory,
173 then fail.
174 Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
175 says that
176 "A pathname that contains at least one non-slash character and that
177 ends with one or more trailing slashes shall be resolved as if a
178 single dot character ( '.' ) were appended to the pathname."
179 and
180 "The special filename dot shall refer to the directory specified by
181 its predecessor."
182 If the named file without the slash is not a directory, open() must fail
183 with ENOTDIR. */
184 if (fd >= 0)
185 {
186 /* We know len is positive, since open did not fail with ENOENT. */
187 size_t len = strlen (filename);
188 if (filename[len - 1] == '/')
189 {
190 struct stat statbuf;
191
192 if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
193 {
194 close (fd);
195 errno = ENOTDIR;
196 return -1;
197 }
198 }
199 }
200#endif
201
202#if REPLACE_FCHDIR
203 if (!REPLACE_OPEN_DIRECTORY && 0 <= fd)
204 fd = _gl_register_fd (fd, filename);
205#endif
206
207 return fd;
208}
diff --git a/lib/stdio.in.h b/lib/stdio.in.h
index d706377f984..b714c54a544 100644
--- a/lib/stdio.in.h
+++ b/lib/stdio.in.h
@@ -111,9 +111,9 @@
111#define _GL_ATTRIBUTE_FORMAT_SCANF_SYSTEM(formatstring_parameter, first_argument) \ 111#define _GL_ATTRIBUTE_FORMAT_SCANF_SYSTEM(formatstring_parameter, first_argument) \
112 _GL_ATTRIBUTE_FORMAT ((__scanf__, formatstring_parameter, first_argument)) 112 _GL_ATTRIBUTE_FORMAT ((__scanf__, formatstring_parameter, first_argument))
113 113
114/* Solaris 10 declares renameat in <unistd.h>, not in <stdio.h>. */ 114/* Solaris 10 and NetBSD 7.0 declare renameat in <unistd.h>, not in <stdio.h>. */
115/* But in any case avoid namespace pollution on glibc systems. */ 115/* But in any case avoid namespace pollution on glibc systems. */
116#if (@GNULIB_RENAMEAT@ || defined GNULIB_POSIXCHECK) && defined __sun \ 116#if (@GNULIB_RENAMEAT@ || defined GNULIB_POSIXCHECK) && (defined __sun || defined __NetBSD__) \
117 && ! defined __GLIBC__ 117 && ! defined __GLIBC__
118# include <unistd.h> 118# include <unistd.h>
119#endif 119#endif
diff --git a/lib/stdlib.in.h b/lib/stdlib.in.h
index b5cf9d36958..c6e68fddc48 100644
--- a/lib/stdlib.in.h
+++ b/lib/stdlib.in.h
@@ -765,6 +765,23 @@ _GL_WARN_ON_USE (realloc, "realloc is not POSIX compliant everywhere - "
765 "use gnulib module realloc-posix for portability"); 765 "use gnulib module realloc-posix for portability");
766#endif 766#endif
767 767
768
769#if @GNULIB_REALLOCARRAY@
770# if ! @HAVE_REALLOCARRAY@
771_GL_FUNCDECL_SYS (reallocarray, void *,
772 (void *ptr, size_t nmemb, size_t size));
773# endif
774_GL_CXXALIAS_SYS (reallocarray, void *,
775 (void *ptr, size_t nmemb, size_t size));
776_GL_CXXALIASWARN (reallocarray);
777#elif defined GNULIB_POSIXCHECK
778# undef reallocarray
779# if HAVE_RAW_DECL_REALLOCARRAY
780_GL_WARN_ON_USE (reallocarray, "reallocarray is not portable - "
781 "use gnulib module reallocarray for portability");
782# endif
783#endif
784
768#if @GNULIB_REALPATH@ 785#if @GNULIB_REALPATH@
769# if @REPLACE_REALPATH@ 786# if @REPLACE_REALPATH@
770# if !(defined __cplusplus && defined GNULIB_NAMESPACE) 787# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
diff --git a/m4/extensions.m4 b/m4/extensions.m4
index 0c16bb832f8..f8543386795 100644
--- a/m4/extensions.m4
+++ b/m4/extensions.m4
@@ -1,4 +1,4 @@
1# serial 16 -*- Autoconf -*- 1# serial 17 -*- Autoconf -*-
2# Enable extensions on systems that normally disable them. 2# Enable extensions on systems that normally disable them.
3 3
4# Copyright (C) 2003, 2006-2017 Free Software Foundation, Inc. 4# Copyright (C) 2003, 2006-2017 Free Software Foundation, Inc.
@@ -68,6 +68,10 @@ dnl configure.ac when using autoheader 2.62.
68#ifndef _GNU_SOURCE 68#ifndef _GNU_SOURCE
69# undef _GNU_SOURCE 69# undef _GNU_SOURCE
70#endif 70#endif
71/* Enable NetBSD extensions on NetBSD. */
72#ifndef _NETBSD_SOURCE
73# undef _NETBSD_SOURCE
74#endif
71/* Enable OpenBSD extensions on NetBSD. */ 75/* Enable OpenBSD extensions on NetBSD. */
72#ifndef _OPENBSD_SOURCE 76#ifndef _OPENBSD_SOURCE
73# undef _OPENBSD_SOURCE 77# undef _OPENBSD_SOURCE
@@ -132,6 +136,7 @@ dnl configure.ac when using autoheader 2.62.
132 AC_DEFINE([_ALL_SOURCE]) 136 AC_DEFINE([_ALL_SOURCE])
133 AC_DEFINE([_DARWIN_C_SOURCE]) 137 AC_DEFINE([_DARWIN_C_SOURCE])
134 AC_DEFINE([_GNU_SOURCE]) 138 AC_DEFINE([_GNU_SOURCE])
139 AC_DEFINE([_NETBSD_SOURCE])
135 AC_DEFINE([_OPENBSD_SOURCE]) 140 AC_DEFINE([_OPENBSD_SOURCE])
136 AC_DEFINE([_POSIX_PTHREAD_SEMANTICS]) 141 AC_DEFINE([_POSIX_PTHREAD_SEMANTICS])
137 AC_DEFINE([__STDC_WANT_IEC_60559_ATTRIBS_EXT__]) 142 AC_DEFINE([__STDC_WANT_IEC_60559_ATTRIBS_EXT__])
diff --git a/m4/gnulib-comp.m4 b/m4/gnulib-comp.m4
index d1089860e19..13504a8ca20 100644
--- a/m4/gnulib-comp.m4
+++ b/m4/gnulib-comp.m4
@@ -53,6 +53,7 @@ AC_DEFUN([gl_EARLY],
53 # Code from module c-strcase: 53 # Code from module c-strcase:
54 # Code from module careadlinkat: 54 # Code from module careadlinkat:
55 # Code from module clock-time: 55 # Code from module clock-time:
56 # Code from module cloexec:
56 # Code from module close-stream: 57 # Code from module close-stream:
57 # Code from module count-leading-zeros: 58 # Code from module count-leading-zeros:
58 # Code from module count-one-bits: 59 # Code from module count-one-bits:
@@ -115,6 +116,7 @@ AC_DEFUN([gl_EARLY],
115 # Code from module multiarch: 116 # Code from module multiarch:
116 # Code from module nocrash: 117 # Code from module nocrash:
117 # Code from module nstrftime: 118 # Code from module nstrftime:
119 # Code from module open:
118 # Code from module openat-h: 120 # Code from module openat-h:
119 # Code from module pipe2: 121 # Code from module pipe2:
120 # Code from module pselect: 122 # Code from module pselect:
@@ -413,6 +415,7 @@ AC_DEFUN([gl_INIT],
413 gl_UTIMENS 415 gl_UTIMENS
414 AC_C_VARARRAYS 416 AC_C_VARARRAYS
415 gl_gnulib_enabled_260941c0e5dc67ec9e87d1fb321c300b=false 417 gl_gnulib_enabled_260941c0e5dc67ec9e87d1fb321c300b=false
418 gl_gnulib_enabled_cloexec=false
416 gl_gnulib_enabled_dirfd=false 419 gl_gnulib_enabled_dirfd=false
417 gl_gnulib_enabled_dosname=false 420 gl_gnulib_enabled_dosname=false
418 gl_gnulib_enabled_euidaccess=false 421 gl_gnulib_enabled_euidaccess=false
@@ -422,6 +425,7 @@ AC_DEFUN([gl_INIT],
422 gl_gnulib_enabled_a9786850e999ae65a836a6041e8e5ed1=false 425 gl_gnulib_enabled_a9786850e999ae65a836a6041e8e5ed1=false
423 gl_gnulib_enabled_2049e887c7e5308faad27b3f894bb8c9=false 426 gl_gnulib_enabled_2049e887c7e5308faad27b3f894bb8c9=false
424 gl_gnulib_enabled_5264294aa0a5557541b53c8c741f7f31=false 427 gl_gnulib_enabled_5264294aa0a5557541b53c8c741f7f31=false
428 gl_gnulib_enabled_open=false
425 gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7=false 429 gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7=false
426 gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c=false 430 gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c=false
427 gl_gnulib_enabled_strtoll=false 431 gl_gnulib_enabled_strtoll=false
@@ -431,6 +435,14 @@ AC_DEFUN([gl_INIT],
431 if ! $gl_gnulib_enabled_260941c0e5dc67ec9e87d1fb321c300b; then 435 if ! $gl_gnulib_enabled_260941c0e5dc67ec9e87d1fb321c300b; then
432 AC_LIBOBJ([openat-proc]) 436 AC_LIBOBJ([openat-proc])
433 gl_gnulib_enabled_260941c0e5dc67ec9e87d1fb321c300b=true 437 gl_gnulib_enabled_260941c0e5dc67ec9e87d1fb321c300b=true
438 func_gl_gnulib_m4code_open
439 fi
440 }
441 func_gl_gnulib_m4code_cloexec ()
442 {
443 if ! $gl_gnulib_enabled_cloexec; then
444 gl_MODULE_INDICATOR_FOR_TESTS([cloexec])
445 gl_gnulib_enabled_cloexec=true
434 fi 446 fi
435 } 447 }
436 func_gl_gnulib_m4code_dirfd () 448 func_gl_gnulib_m4code_dirfd ()
@@ -536,6 +548,21 @@ AC_DEFUN([gl_INIT],
536 gl_gnulib_enabled_5264294aa0a5557541b53c8c741f7f31=true 548 gl_gnulib_enabled_5264294aa0a5557541b53c8c741f7f31=true
537 fi 549 fi
538 } 550 }
551 func_gl_gnulib_m4code_open ()
552 {
553 if ! $gl_gnulib_enabled_open; then
554 gl_FUNC_OPEN
555 if test $REPLACE_OPEN = 1; then
556 AC_LIBOBJ([open])
557 gl_PREREQ_OPEN
558 fi
559 gl_FCNTL_MODULE_INDICATOR([open])
560 gl_gnulib_enabled_open=true
561 if test $REPLACE_OPEN = 1; then
562 func_gl_gnulib_m4code_cloexec
563 fi
564 fi
565 }
539 func_gl_gnulib_m4code_03e0aaad4cb89ca757653bd367a6ccb7 () 566 func_gl_gnulib_m4code_03e0aaad4cb89ca757653bd367a6ccb7 ()
540 { 567 {
541 if ! $gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7; then 568 if ! $gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7; then
@@ -622,6 +649,7 @@ AC_DEFUN([gl_INIT],
622 fi 649 fi
623 m4_pattern_allow([^gl_GNULIB_ENABLED_]) 650 m4_pattern_allow([^gl_GNULIB_ENABLED_])
624 AM_CONDITIONAL([gl_GNULIB_ENABLED_260941c0e5dc67ec9e87d1fb321c300b], [$gl_gnulib_enabled_260941c0e5dc67ec9e87d1fb321c300b]) 651 AM_CONDITIONAL([gl_GNULIB_ENABLED_260941c0e5dc67ec9e87d1fb321c300b], [$gl_gnulib_enabled_260941c0e5dc67ec9e87d1fb321c300b])
652 AM_CONDITIONAL([gl_GNULIB_ENABLED_cloexec], [$gl_gnulib_enabled_cloexec])
625 AM_CONDITIONAL([gl_GNULIB_ENABLED_dirfd], [$gl_gnulib_enabled_dirfd]) 653 AM_CONDITIONAL([gl_GNULIB_ENABLED_dirfd], [$gl_gnulib_enabled_dirfd])
626 AM_CONDITIONAL([gl_GNULIB_ENABLED_dosname], [$gl_gnulib_enabled_dosname]) 654 AM_CONDITIONAL([gl_GNULIB_ENABLED_dosname], [$gl_gnulib_enabled_dosname])
627 AM_CONDITIONAL([gl_GNULIB_ENABLED_euidaccess], [$gl_gnulib_enabled_euidaccess]) 655 AM_CONDITIONAL([gl_GNULIB_ENABLED_euidaccess], [$gl_gnulib_enabled_euidaccess])
@@ -631,6 +659,7 @@ AC_DEFUN([gl_INIT],
631 AM_CONDITIONAL([gl_GNULIB_ENABLED_a9786850e999ae65a836a6041e8e5ed1], [$gl_gnulib_enabled_a9786850e999ae65a836a6041e8e5ed1]) 659 AM_CONDITIONAL([gl_GNULIB_ENABLED_a9786850e999ae65a836a6041e8e5ed1], [$gl_gnulib_enabled_a9786850e999ae65a836a6041e8e5ed1])
632 AM_CONDITIONAL([gl_GNULIB_ENABLED_2049e887c7e5308faad27b3f894bb8c9], [$gl_gnulib_enabled_2049e887c7e5308faad27b3f894bb8c9]) 660 AM_CONDITIONAL([gl_GNULIB_ENABLED_2049e887c7e5308faad27b3f894bb8c9], [$gl_gnulib_enabled_2049e887c7e5308faad27b3f894bb8c9])
633 AM_CONDITIONAL([gl_GNULIB_ENABLED_5264294aa0a5557541b53c8c741f7f31], [$gl_gnulib_enabled_5264294aa0a5557541b53c8c741f7f31]) 661 AM_CONDITIONAL([gl_GNULIB_ENABLED_5264294aa0a5557541b53c8c741f7f31], [$gl_gnulib_enabled_5264294aa0a5557541b53c8c741f7f31])
662 AM_CONDITIONAL([gl_GNULIB_ENABLED_open], [$gl_gnulib_enabled_open])
634 AM_CONDITIONAL([gl_GNULIB_ENABLED_03e0aaad4cb89ca757653bd367a6ccb7], [$gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7]) 663 AM_CONDITIONAL([gl_GNULIB_ENABLED_03e0aaad4cb89ca757653bd367a6ccb7], [$gl_gnulib_enabled_03e0aaad4cb89ca757653bd367a6ccb7])
635 AM_CONDITIONAL([gl_GNULIB_ENABLED_6099e9737f757db36c47fa9d9f02e88c], [$gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c]) 664 AM_CONDITIONAL([gl_GNULIB_ENABLED_6099e9737f757db36c47fa9d9f02e88c], [$gl_gnulib_enabled_6099e9737f757db36c47fa9d9f02e88c])
636 AM_CONDITIONAL([gl_GNULIB_ENABLED_strtoll], [$gl_gnulib_enabled_strtoll]) 665 AM_CONDITIONAL([gl_GNULIB_ENABLED_strtoll], [$gl_gnulib_enabled_strtoll])
@@ -800,6 +829,8 @@ AC_DEFUN([gl_FILE_LIST], [
800 lib/c-strncasecmp.c 829 lib/c-strncasecmp.c
801 lib/careadlinkat.c 830 lib/careadlinkat.c
802 lib/careadlinkat.h 831 lib/careadlinkat.h
832 lib/cloexec.c
833 lib/cloexec.h
803 lib/close-stream.c 834 lib/close-stream.c
804 lib/close-stream.h 835 lib/close-stream.h
805 lib/count-leading-zeros.c 836 lib/count-leading-zeros.c
@@ -869,6 +900,7 @@ AC_DEFUN([gl_FILE_LIST], [
869 lib/mktime-internal.h 900 lib/mktime-internal.h
870 lib/mktime.c 901 lib/mktime.c
871 lib/nstrftime.c 902 lib/nstrftime.c
903 lib/open.c
872 lib/openat-priv.h 904 lib/openat-priv.h
873 lib/openat-proc.c 905 lib/openat-proc.c
874 lib/openat.h 906 lib/openat.h
@@ -987,10 +1019,13 @@ AC_DEFUN([gl_FILE_LIST], [
987 m4/minmax.m4 1019 m4/minmax.m4
988 m4/mkostemp.m4 1020 m4/mkostemp.m4
989 m4/mktime.m4 1021 m4/mktime.m4
1022 m4/mode_t.m4
990 m4/multiarch.m4 1023 m4/multiarch.m4
991 m4/nocrash.m4 1024 m4/nocrash.m4
992 m4/nstrftime.m4 1025 m4/nstrftime.m4
993 m4/off_t.m4 1026 m4/off_t.m4
1027 m4/open-cloexec.m4
1028 m4/open.m4
994 m4/pipe2.m4 1029 m4/pipe2.m4
995 m4/pselect.m4 1030 m4/pselect.m4
996 m4/pthread_sigmask.m4 1031 m4/pthread_sigmask.m4
diff --git a/m4/mode_t.m4 b/m4/mode_t.m4
new file mode 100644
index 00000000000..75d372a4a8a
--- /dev/null
+++ b/m4/mode_t.m4
@@ -0,0 +1,26 @@
1# mode_t.m4 serial 2
2dnl Copyright (C) 2009-2017 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7# For using mode_t, it's sufficient to use AC_TYPE_MODE_T and
8# include <sys/types.h>.
9
10# Define PROMOTED_MODE_T to the type that is the result of "default argument
11# promotion" (ISO C 6.5.2.2.(6)) of the type mode_t.
12AC_DEFUN([gl_PROMOTED_TYPE_MODE_T],
13[
14 AC_REQUIRE([AC_TYPE_MODE_T])
15 AC_CACHE_CHECK([for promoted mode_t type], [gl_cv_promoted_mode_t], [
16 dnl Assume mode_t promotes to 'int' if and only if it is smaller than 'int',
17 dnl and to itself otherwise. This assumption is not guaranteed by the ISO C
18 dnl standard, but we don't know of any real-world counterexamples.
19 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>]],
20 [[typedef int array[2 * (sizeof (mode_t) < sizeof (int)) - 1];]])],
21 [gl_cv_promoted_mode_t='int'],
22 [gl_cv_promoted_mode_t='mode_t'])
23 ])
24 AC_DEFINE_UNQUOTED([PROMOTED_MODE_T], [$gl_cv_promoted_mode_t],
25 [Define to the type that is the result of default argument promotions of type mode_t.])
26])
diff --git a/m4/open-cloexec.m4 b/m4/open-cloexec.m4
new file mode 100644
index 00000000000..897af66910f
--- /dev/null
+++ b/m4/open-cloexec.m4
@@ -0,0 +1,21 @@
1# Test whether O_CLOEXEC is defined.
2
3dnl Copyright 2017 Free Software Foundation, Inc.
4dnl This file is free software; the Free Software Foundation
5dnl gives unlimited permission to copy and/or distribute it,
6dnl with or without modifications, as long as this notice is preserved.
7
8AC_DEFUN([gl_PREPROC_O_CLOEXEC],
9[
10 AC_CACHE_CHECK([for O_CLOEXEC],
11 [gl_cv_macro_O_CLOEXEC],
12 [AC_COMPILE_IFELSE(
13 [AC_LANG_PROGRAM([[#include <fcntl.h>
14 #ifndef O_CLOEXEC
15 choke me;
16 #endif
17 ]],
18 [[return O_CLOEXEC;]])],
19 [gl_cv_macro_O_CLOEXEC=yes],
20 [gl_cv_macro_O_CLOEXEC=no])])
21])
diff --git a/m4/open.m4 b/m4/open.m4
new file mode 100644
index 00000000000..68253e15ffd
--- /dev/null
+++ b/m4/open.m4
@@ -0,0 +1,95 @@
1# open.m4 serial 15
2dnl Copyright (C) 2007-2017 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7AC_DEFUN([gl_FUNC_OPEN],
8[
9 AC_REQUIRE([AC_CANONICAL_HOST])
10 AC_REQUIRE([gl_PREPROC_O_CLOEXEC])
11 case "$host_os" in
12 mingw* | pw*)
13 REPLACE_OPEN=1
14 ;;
15 *)
16 dnl open("foo/") should not create a file when the file name has a
17 dnl trailing slash. FreeBSD only has the problem on symlinks.
18 AC_CHECK_FUNCS_ONCE([lstat])
19 if test "$gl_cv_macro_O_CLOEXEC" != yes; then
20 REPLACE_OPEN=1
21 fi
22 AC_CACHE_CHECK([whether open recognizes a trailing slash],
23 [gl_cv_func_open_slash],
24 [# Assume that if we have lstat, we can also check symlinks.
25 if test $ac_cv_func_lstat = yes; then
26 touch conftest.tmp
27 ln -s conftest.tmp conftest.lnk
28 fi
29 AC_RUN_IFELSE(
30 [AC_LANG_SOURCE([[
31#include <fcntl.h>
32#if HAVE_UNISTD_H
33# include <unistd.h>
34#endif
35int main ()
36{
37 int result = 0;
38#if HAVE_LSTAT
39 if (open ("conftest.lnk/", O_RDONLY) != -1)
40 result |= 1;
41#endif
42 if (open ("conftest.sl/", O_CREAT, 0600) >= 0)
43 result |= 2;
44 return result;
45}]])],
46 [gl_cv_func_open_slash=yes],
47 [gl_cv_func_open_slash=no],
48 [
49changequote(,)dnl
50 case "$host_os" in
51 freebsd* | aix* | hpux* | solaris2.[0-9] | solaris2.[0-9].*)
52 gl_cv_func_open_slash="guessing no" ;;
53 *)
54 gl_cv_func_open_slash="guessing yes" ;;
55 esac
56changequote([,])dnl
57 ])
58 rm -f conftest.sl conftest.tmp conftest.lnk
59 ])
60 case "$gl_cv_func_open_slash" in
61 *no)
62 AC_DEFINE([OPEN_TRAILING_SLASH_BUG], [1],
63 [Define to 1 if open() fails to recognize a trailing slash.])
64 REPLACE_OPEN=1
65 ;;
66 esac
67 ;;
68 esac
69 dnl Replace open() for supporting the gnulib-defined fchdir() function,
70 dnl to keep fchdir's bookkeeping up-to-date.
71 m4_ifdef([gl_FUNC_FCHDIR], [
72 if test $REPLACE_OPEN = 0; then
73 gl_TEST_FCHDIR
74 if test $HAVE_FCHDIR = 0; then
75 REPLACE_OPEN=1
76 fi
77 fi
78 ])
79 dnl Replace open() for supporting the gnulib-defined O_NONBLOCK flag.
80 m4_ifdef([gl_NONBLOCKING_IO], [
81 if test $REPLACE_OPEN = 0; then
82 gl_NONBLOCKING_IO
83 if test $gl_cv_have_open_O_NONBLOCK != yes; then
84 REPLACE_OPEN=1
85 fi
86 fi
87 ])
88])
89
90# Prerequisites of lib/open.c.
91AC_DEFUN([gl_PREREQ_OPEN],
92[
93 AC_REQUIRE([gl_PROMOTED_TYPE_MODE_T])
94 :
95])
diff --git a/m4/stdlib_h.m4 b/m4/stdlib_h.m4
index 110fe2d1a9f..ec4a058154b 100644
--- a/m4/stdlib_h.m4
+++ b/m4/stdlib_h.m4
@@ -21,9 +21,9 @@ AC_DEFUN([gl_STDLIB_H],
21#endif 21#endif
22 ]], [_Exit atoll canonicalize_file_name getloadavg getsubopt grantpt 22 ]], [_Exit atoll canonicalize_file_name getloadavg getsubopt grantpt
23 initstate initstate_r mkdtemp mkostemp mkostemps mkstemp mkstemps 23 initstate initstate_r mkdtemp mkostemp mkostemps mkstemp mkstemps
24 posix_openpt ptsname ptsname_r qsort_r random random_r realpath rpmatch 24 posix_openpt ptsname ptsname_r qsort_r random random_r reallocarray
25 secure_getenv setenv setstate setstate_r srandom srandom_r 25 realpath rpmatch secure_getenv setenv setstate setstate_r srandom
26 strtod strtoll strtoull unlockpt unsetenv]) 26 srandom_r strtod strtoll strtoull unlockpt unsetenv])
27]) 27])
28 28
29AC_DEFUN([gl_STDLIB_MODULE_INDICATOR], 29AC_DEFUN([gl_STDLIB_MODULE_INDICATOR],
@@ -58,6 +58,7 @@ AC_DEFUN([gl_STDLIB_H_DEFAULTS],
58 GNULIB_QSORT_R=0; AC_SUBST([GNULIB_QSORT_R]) 58 GNULIB_QSORT_R=0; AC_SUBST([GNULIB_QSORT_R])
59 GNULIB_RANDOM=0; AC_SUBST([GNULIB_RANDOM]) 59 GNULIB_RANDOM=0; AC_SUBST([GNULIB_RANDOM])
60 GNULIB_RANDOM_R=0; AC_SUBST([GNULIB_RANDOM_R]) 60 GNULIB_RANDOM_R=0; AC_SUBST([GNULIB_RANDOM_R])
61 GNULIB_REALLOCARRAY=0; AC_SUBST([GNULIB_REALLOCARRAY])
61 GNULIB_REALLOC_POSIX=0; AC_SUBST([GNULIB_REALLOC_POSIX]) 62 GNULIB_REALLOC_POSIX=0; AC_SUBST([GNULIB_REALLOC_POSIX])
62 GNULIB_REALPATH=0; AC_SUBST([GNULIB_REALPATH]) 63 GNULIB_REALPATH=0; AC_SUBST([GNULIB_REALPATH])
63 GNULIB_RPMATCH=0; AC_SUBST([GNULIB_RPMATCH]) 64 GNULIB_RPMATCH=0; AC_SUBST([GNULIB_RPMATCH])
@@ -89,6 +90,7 @@ AC_DEFUN([gl_STDLIB_H_DEFAULTS],
89 HAVE_RANDOM=1; AC_SUBST([HAVE_RANDOM]) 90 HAVE_RANDOM=1; AC_SUBST([HAVE_RANDOM])
90 HAVE_RANDOM_H=1; AC_SUBST([HAVE_RANDOM_H]) 91 HAVE_RANDOM_H=1; AC_SUBST([HAVE_RANDOM_H])
91 HAVE_RANDOM_R=1; AC_SUBST([HAVE_RANDOM_R]) 92 HAVE_RANDOM_R=1; AC_SUBST([HAVE_RANDOM_R])
93 HAVE_REALLOCARRAY=1; AC_SUBST([HAVE_REALLOCARRAY])
92 HAVE_REALPATH=1; AC_SUBST([HAVE_REALPATH]) 94 HAVE_REALPATH=1; AC_SUBST([HAVE_REALPATH])
93 HAVE_RPMATCH=1; AC_SUBST([HAVE_RPMATCH]) 95 HAVE_RPMATCH=1; AC_SUBST([HAVE_RPMATCH])
94 HAVE_SECURE_GETENV=1; AC_SUBST([HAVE_SECURE_GETENV]) 96 HAVE_SECURE_GETENV=1; AC_SUBST([HAVE_SECURE_GETENV])
diff --git a/src/filelock.c b/src/filelock.c
index 3d6941695ae..fec9bc044ae 100644
--- a/src/filelock.c
+++ b/src/filelock.c
@@ -403,8 +403,6 @@ create_lock_file (char *lfname, char *lock_info_str, bool force)
403 else 403 else
404 { 404 {
405 ptrdiff_t lock_info_len; 405 ptrdiff_t lock_info_len;
406 if (! O_CLOEXEC)
407 fcntl (fd, F_SETFD, FD_CLOEXEC);
408 lock_info_len = strlen (lock_info_str); 406 lock_info_len = strlen (lock_info_str);
409 err = 0; 407 err = 0;
410 if (emacs_write (fd, lock_info_str, lock_info_len) != lock_info_len 408 if (emacs_write (fd, lock_info_str, lock_info_len) != lock_info_len
diff --git a/src/sysdep.c b/src/sysdep.c
index 2e18a419e3f..12e9c83ee90 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -2393,8 +2393,6 @@ emacs_open (const char *file, int oflags, int mode)
2393 oflags |= O_CLOEXEC; 2393 oflags |= O_CLOEXEC;
2394 while ((fd = open (file, oflags, mode)) < 0 && errno == EINTR) 2394 while ((fd = open (file, oflags, mode)) < 0 && errno == EINTR)
2395 maybe_quit (); 2395 maybe_quit ();
2396 if (! O_CLOEXEC && 0 <= fd)
2397 fcntl (fd, F_SETFD, FD_CLOEXEC);
2398 return fd; 2396 return fd;
2399} 2397}
2400 2398
@@ -2436,13 +2434,7 @@ emacs_pipe (int fd[2])
2436#ifdef MSDOS 2434#ifdef MSDOS
2437 return pipe (fd); 2435 return pipe (fd);
2438#else /* !MSDOS */ 2436#else /* !MSDOS */
2439 int result = pipe2 (fd, O_BINARY | O_CLOEXEC); 2437 return pipe2 (fd, O_BINARY | O_CLOEXEC);
2440 if (! O_CLOEXEC && result == 0)
2441 {
2442 fcntl (fd[0], F_SETFD, FD_CLOEXEC);
2443 fcntl (fd[1], F_SETFD, FD_CLOEXEC);
2444 }
2445 return result;
2446#endif /* !MSDOS */ 2438#endif /* !MSDOS */
2447} 2439}
2448 2440