Mercurial > gnulib
view tests/test-fcntl.c @ 19595:beb2ad957aca
Simplify code. Drop support for Borland C++ on Windows.
Reported by Gisle Vanem <gisle.vanem@gmail.com>.
* lib/accept4.c: Simplify 'defined _WIN32 || defined __WIN32__' to just
'defined _WIN32'.
* lib/canonicalize-lgpl.c: Likewise.
* lib/classpath.c: Likewise.
* lib/clean-temp.c: Likewise.
* lib/csharpexec.c: Likewise.
* lib/ctime.c: Likewise.
* lib/dosname.h: Likewise.
* lib/dup2.c: Likewise.
* lib/errno.in.h: Likewise.
* lib/error.c: Likewise.
* lib/euidaccess.c: Likewise.
* lib/execute.c: Likewise.
* lib/fcntl.in.h: Likewise.
* lib/fcntl.c: Likewise.
* lib/filename.h: Likewise.
* lib/findprog.c: Likewise.
* lib/flock.c: Likewise.
* lib/fopen.c: Likewise.
* lib/freopen.c: Likewise.
* lib/fstat.c: Likewise.
* lib/fsync.c: Likewise.
* lib/gc-gnulib.c: Likewise.
* lib/get-rusage-data.c: Likewise.
* lib/getaddrinfo.c: Likewise.
* lib/getdelim.c: Likewise.
* lib/getdtablesize.c: Likewise.
* lib/gethostname.c: Likewise.
* lib/getlogin.c: Likewise.
* lib/getlogin_r.c: Likewise.
* lib/getopt.c: Likewise.
* lib/getpagesize.c: Likewise.
* lib/getpass.c: Likewise.
* lib/getrusage.c: Likewise.
* lib/gettimeofday.c: Likewise.
* lib/glob.c: Likewise.
* lib/inttypes.in.h: Likewise.
* lib/isapipe.c: Likewise.
* lib/javaexec.c: Likewise.
* lib/link.c: Likewise.
* lib/localcharset.c: Likewise.
* lib/localename.h: Likewise.
* lib/localename.c: Likewise.
* lib/localtime.c: Likewise.
* lib/lseek.c: Likewise.
* lib/mbsinit.c: Likewise.
* lib/mkdir.c: Likewise.
* lib/msvc-nothrow.h: Likewise.
* lib/nanosleep.c: Likewise.
* lib/nl_langinfo.c: Likewise.
* lib/nonblocking.c: Likewise.
* lib/nproc.c: Likewise.
* lib/open.c: Likewise.
* lib/openpty.c: Likewise.
* lib/pathmax.h: Likewise.
* lib/pipe-filter-aux.c: Likewise.
* lib/pipe-filter-gi.c: Likewise.
* lib/pipe-filter-ii.c: Likewise.
* lib/pipe.c: Likewise.
* lib/pipe2.c: Likewise.
* lib/poll.c: Likewise.
* lib/popen.c: Likewise.
* lib/posix_openpt.c: Likewise.
* lib/printf-parse.c: Likewise.
* lib/progreloc.c: Likewise.
* lib/putenv.c: Likewise.
* lib/read.c: Likewise.
* lib/relocatable.c: Likewise.
* lib/rename.c: Likewise.
* lib/same-inode.h: Likewise.
* lib/secure_getenv.c: Likewise.
* lib/select.c: Likewise.
* lib/sethostname.c: Likewise.
* lib/setlocale.c: Likewise.
* lib/sigaction.c: Likewise.
* lib/sigprocmask.c: Likewise.
* lib/sleep.c: Likewise.
* lib/spawn-pipe.h: Likewise.
* lib/spawn-pipe.c: Likewise.
* lib/spawni.c: Likewise.
* lib/stat-time.h: Likewise.
* lib/stat-w32.c: Likewise.
* lib/stat.c: Likewise.
* lib/stdio.in.h: Likewise.
* lib/stdio-impl.h: Likewise.
* lib/stdio-read.c: Likewise.
* lib/stdio-write.c: Likewise.
* lib/stdlib.in.h: Likewise.
* lib/strerror_r.c: Likewise.
* lib/strftime-fixes.c: Likewise.
* lib/sys_stat.in.h: Likewise.
* lib/sys_types.in.h: Likewise.
* lib/sys_wait.in.h : Likewise.
* lib/system-quote.h: Likewise.
* lib/system-quote.c: Likewise.
* lib/tmpdir.c: Likewise.
* lib/tzset.c: Likewise.
* lib/uname.c: Likewise.
* lib/unistd.in.h: Likewise.
* lib/utime.in.h: Likewise.
* lib/utime.c: Likewise.
* lib/utimecmp.c: Likewise.
* lib/utimens.c: Likewise.
* lib/vasnprintf.c: Likewise.
* lib/vma-iter.h: Likewise.
* lib/vma-iter.c: Likewise.
* lib/wait-process.c: Likewise.
* lib/wcsftime.c: Likewise.
* lib/wctype.in.h: Likewise.
* lib/write.c: Likewise.
* tests/nap.h: Likewise.
* tests/test-cloexec.c: Likewise.
* tests/test-dup-safer.c: Likewise.
* tests/test-dup2.c: Likewise.
* tests/test-dup3.c: Likewise.
* tests/test-fcntl.c: Likewise.
* tests/test-get-rusage-data.c: Likewise.
* tests/test-getaddrinfo.c: Likewise.
* tests/test-getlogin.h: Likewise.
* tests/test-isatty.c: Likewise.
* tests/test-localename.c: Likewise.
* tests/test-mbrtowc-w32.c: Likewise.
* tests/test-nonblocking.c: Likewise.
* tests/test-nonblocking-pipe-main.c: Likewise.
* tests/test-nonblocking-socket-main.c: Likewise.
* tests/test-nonblocking-socket.h: Likewise.
* tests/test-pipe.c: Likewise.
* tests/test-pipe2.c: Likewise.
* tests/test-poll.c: Likewise.
* tests/test-pthread_sigmask1.c: Likewise.
* tests/test-select.h: Likewise.
* tests/test-sethostname2.c: Likewise.
* tests/test-sigprocmask.c: Likewise.
* tests/test-spawn-pipe-child.c: Likewise.
* tests/test-stat-time.c: Likewise.
* tests/test-system-quote-main.c: Likewise.
* tests/test-utimens-common.h: Likewise.
* tests/test-wcrtomb-w32.c: Likewise.
* m4/csharpexec.m4 (gt_CSHARPEXEC): Likewise.
* m4/getcwd.m4 (gl_FUNC_GETCWD_NULL): Likewise.
* m4/javacomp.m4 (gt_JAVACOMP): Likewise.
* m4/javaexec.m4 (gt_JAVAEXEC): Likewise.
* m4/locale-ar.m4 (gt_LOCALE_AR): Likewise.
* m4/locale-fr.m4 (gt_LOCALE_FR, gt_LOCALE_FR_UTF8): Likewise.
* m4/locale-ja.m4 (gt_LOCALE_JA): Likewise.
* m4/locale-tr.m4 (gt_LOCALE_TR_UTF8): Likewise.
* m4/locale-zh.m4 (gt_LOCALE_ZH_CN): Likewise.
* m4/malloc.m4 (gl_CHECK_MALLOC_POSIX): Likewise.
* m4/nocrash.m4 (GL_NOCRASH): Likewise.
* m4/pathmax.m4 (gl_PATHMAX_SNIPPET): Likewise.
* m4/ptsname_r.m4 (gl_PREREQ_PTSNAME_R): Likewise.
* m4/stdio_h.m4 (gl_STDIO_H): Likewise.
* m4/tmpfile.m4 (gl_FUNC_TMPFILE): Likewise.
* m4/unistd_h.m4 (gl_UNISTD_H): Likewise.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 03 May 2018 23:09:42 +0200 |
parents | 87191c3593a6 |
children | b06060465f09 |
line wrap: on
line source
/* Test of fcntl(2). Copyright (C) 2009-2018 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ /* Written by Eric Blake <ebb9@byu.net>, 2009. */ #include <config.h> /* Specification. */ #include <fcntl.h> #include "signature.h" SIGNATURE_CHECK (fcntl, int, (int, int, ...)); /* Helpers. */ #include <errno.h> #include <stdarg.h> #include <stdbool.h> #include <unistd.h> #if defined _WIN32 && ! defined __CYGWIN__ /* Get declarations of the native Windows API functions. */ # define WIN32_LEAN_AND_MEAN # include <windows.h> /* Get _get_osfhandle. */ # if GNULIB_MSVC_NOTHROW # include "msvc-nothrow.h" # else # include <io.h> # endif #endif #include "binary-io.h" #include "macros.h" #if !O_BINARY # define setmode(f,m) zero () static int zero (void) { return 0; } #endif /* Return true if FD is open. */ static bool is_open (int fd) { #if defined _WIN32 && ! defined __CYGWIN__ /* On native Windows, the initial state of unassigned standard file descriptors is that they are open but point to an INVALID_HANDLE_VALUE, and there is no fcntl. */ return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE; #else # ifndef F_GETFL # error Please port fcntl to your platform # endif return 0 <= fcntl (fd, F_GETFL); #endif } /* Return true if FD is open and inheritable across exec/spawn. */ static bool is_inheritable (int fd) { #if defined _WIN32 && ! defined __CYGWIN__ /* On native Windows, the initial state of unassigned standard file descriptors is that they are open but point to an INVALID_HANDLE_VALUE, and there is no fcntl. */ HANDLE h = (HANDLE) _get_osfhandle (fd); DWORD flags; if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0) return false; return (flags & HANDLE_FLAG_INHERIT) != 0; #else # ifndef F_GETFD # error Please port fcntl to your platform # endif int i = fcntl (fd, F_GETFD); return 0 <= i && (i & FD_CLOEXEC) == 0; #endif } /* Return non-zero if FD is open in the given MODE, which is either O_TEXT or O_BINARY. */ static bool is_mode (int fd, int mode) { int value = setmode (fd, O_BINARY); setmode (fd, value); return mode == value; } /* Since native fcntl can have more supported operations than our replacement is aware of, and since various operations assign different types to the vararg argument, a wrapper around fcntl must be able to pass a vararg of unknown type on through to the original fcntl. Make sure that this works properly: func1 behaves like the original fcntl interpreting the vararg as an int or a pointer to a struct, and func2 behaves like rpl_fcntl that doesn't know what type to forward. */ struct dummy_struct { long filler; int value; }; static int func1 (int a, ...) { va_list arg; int i; va_start (arg, a); if (a < 4) i = va_arg (arg, int); else { struct dummy_struct *s = va_arg (arg, struct dummy_struct *); i = s->value; } va_end (arg); return i; } static int func2 (int a, ...) { va_list arg; void *p; va_start (arg, a); p = va_arg (arg, void *); va_end (arg); return func1 (a, p); } /* Ensure that all supported fcntl actions are distinct, and usable in preprocessor expressions. */ static void check_flags (void) { switch (0) { case F_DUPFD: #if F_DUPFD #endif case F_DUPFD_CLOEXEC: #if F_DUPFD_CLOEXEC #endif case F_GETFD: #if F_GETFD #endif #ifdef F_SETFD case F_SETFD: # if F_SETFD # endif #endif #ifdef F_GETFL case F_GETFL: # if F_GETFL # endif #endif #ifdef F_SETFL case F_SETFL: # if F_SETFL # endif #endif #ifdef F_GETOWN case F_GETOWN: # if F_GETOWN # endif #endif #ifdef F_SETOWN case F_SETOWN: # if F_SETOWN # endif #endif #ifdef F_GETLK case F_GETLK: # if F_GETLK # endif #endif #ifdef F_SETLK case F_SETLK: # if F_SETLK # endif #endif #ifdef F_SETLKW case F_SETLKW: # if F_SETLKW # endif #endif ; } } int main (void) { const char *file = "test-fcntl.tmp"; int fd; int bad_fd = getdtablesize (); /* Sanity check that rpl_fcntl is likely to work. */ ASSERT (func2 (1, 2) == 2); ASSERT (func2 (2, -2) == -2); ASSERT (func2 (3, 0x80000000) == 0x80000000); { struct dummy_struct s = { 0L, 4 }; ASSERT (func2 (4, &s) == 4); } check_flags (); /* Assume std descriptors were provided by invoker, and ignore fds that might have been inherited. */ fd = creat (file, 0600); ASSERT (STDERR_FILENO < fd); close (fd + 1); close (fd + 2); /* For F_DUPFD*, the source must be valid. */ errno = 0; ASSERT (fcntl (-1, F_DUPFD, 0) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (fd + 1, F_DUPFD, 0) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (bad_fd, F_DUPFD, 0) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (-1, F_DUPFD_CLOEXEC, 0) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (fd + 1, F_DUPFD_CLOEXEC, 0) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (bad_fd, F_DUPFD_CLOEXEC, 0) == -1); ASSERT (errno == EBADF); /* For F_DUPFD*, the destination must be valid. */ errno = 0; ASSERT (fcntl (fd, F_DUPFD, -1) == -1); ASSERT (errno == EINVAL); errno = 0; ASSERT (fcntl (fd, F_DUPFD, bad_fd) == -1); ASSERT (errno == EINVAL); errno = 0; ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, -1) == -1); ASSERT (errno == EINVAL); errno = 0; ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, bad_fd) == -1); ASSERT (errno == EINVAL || errno == EMFILE /* WSL */); /* For F_DUPFD*, check for correct inheritance, as well as preservation of text vs. binary. */ setmode (fd, O_BINARY); ASSERT (is_open (fd)); ASSERT (!is_open (fd + 1)); ASSERT (!is_open (fd + 2)); ASSERT (is_inheritable (fd)); ASSERT (is_mode (fd, O_BINARY)); ASSERT (fcntl (fd, F_DUPFD, fd) == fd + 1); ASSERT (is_open (fd)); ASSERT (is_open (fd + 1)); ASSERT (!is_open (fd + 2)); ASSERT (is_inheritable (fd + 1)); ASSERT (is_mode (fd, O_BINARY)); ASSERT (is_mode (fd + 1, O_BINARY)); ASSERT (close (fd + 1) == 0); ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, fd + 2) == fd + 2); ASSERT (is_open (fd)); ASSERT (!is_open (fd + 1)); ASSERT (is_open (fd + 2)); ASSERT (is_inheritable (fd)); ASSERT (!is_inheritable (fd + 2)); ASSERT (is_mode (fd, O_BINARY)); ASSERT (is_mode (fd + 2, O_BINARY)); ASSERT (close (fd) == 0); setmode (fd + 2, O_TEXT); ASSERT (fcntl (fd + 2, F_DUPFD, fd + 1) == fd + 1); ASSERT (!is_open (fd)); ASSERT (is_open (fd + 1)); ASSERT (is_open (fd + 2)); ASSERT (is_inheritable (fd + 1)); ASSERT (!is_inheritable (fd + 2)); ASSERT (is_mode (fd + 1, O_TEXT)); ASSERT (is_mode (fd + 2, O_TEXT)); ASSERT (close (fd + 1) == 0); ASSERT (fcntl (fd + 2, F_DUPFD_CLOEXEC, 0) == fd); ASSERT (is_open (fd)); ASSERT (!is_open (fd + 1)); ASSERT (is_open (fd + 2)); ASSERT (!is_inheritable (fd)); ASSERT (!is_inheritable (fd + 2)); ASSERT (is_mode (fd, O_TEXT)); ASSERT (is_mode (fd + 2, O_TEXT)); ASSERT (close (fd + 2) == 0); /* Test F_GETFD on invalid file descriptors. */ errno = 0; ASSERT (fcntl (-1, F_GETFD) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (fd + 1, F_GETFD) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (bad_fd, F_GETFD) == -1); ASSERT (errno == EBADF); /* Test F_GETFD, the FD_CLOEXEC bit. */ { int result = fcntl (fd, F_GETFD); ASSERT (0 <= result); ASSERT ((result & FD_CLOEXEC) == FD_CLOEXEC); ASSERT (dup (fd) == fd + 1); result = fcntl (fd + 1, F_GETFD); ASSERT (0 <= result); ASSERT ((result & FD_CLOEXEC) == 0); ASSERT (close (fd + 1) == 0); } #ifdef F_SETFD /* Test F_SETFD on invalid file descriptors. */ errno = 0; ASSERT (fcntl (-1, F_SETFD, 0) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (fd + 1, F_SETFD, 0) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (bad_fd, F_SETFD, 0) == -1); ASSERT (errno == EBADF); #endif #ifdef F_GETFL /* Test F_GETFL on invalid file descriptors. */ errno = 0; ASSERT (fcntl (-1, F_GETFL) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (fd + 1, F_GETFL) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (bad_fd, F_GETFL) == -1); ASSERT (errno == EBADF); #endif #ifdef F_SETFL /* Test F_SETFL on invalid file descriptors. */ errno = 0; ASSERT (fcntl (-1, F_SETFL, 0) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (fd + 1, F_SETFL, 0) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (bad_fd, F_SETFL, 0) == -1); ASSERT (errno == EBADF); #endif #ifdef F_GETOWN /* Test F_GETOWN on invalid file descriptors. */ errno = 0; ASSERT (fcntl (-1, F_GETOWN) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (fd + 1, F_GETOWN) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (bad_fd, F_GETOWN) == -1); ASSERT (errno == EBADF); #endif #ifdef F_SETOWN /* Test F_SETFL on invalid file descriptors. */ errno = 0; ASSERT (fcntl (-1, F_SETOWN, 0) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (fd + 1, F_SETOWN, 0) == -1); ASSERT (errno == EBADF); errno = 0; ASSERT (fcntl (bad_fd, F_SETOWN, 0) == -1); ASSERT (errno == EBADF); #endif /* Cleanup. */ ASSERT (close (fd) == 0); ASSERT (unlink (file) == 0); return 0; }