glibc/posix/test-errno.c

171 lines
6.3 KiB
C
Raw Permalink Normal View History

/* Test that failing system calls do set errno to the correct value.
Copyright (C) 2017-2024 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
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 13:40:42 +08:00
<https://www.gnu.org/licenses/>. */
#include <sys/cdefs.h>
#include <libc-diag.h>
#if __GNUC_PREREQ (7, 0)
/* Triggered by getgroup fortify wrapper. */
DIAG_IGNORE_NEEDS_COMMENT (7, "-Wstringop-overflow");
#endif
#include <errno.h>
#include <limits.h>
#include <grp.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/statfs.h>
#include <sys/mman.h>
Fix sys/socket.h namespace issues from sys/uio.h inclusion (bug 21426). sys/socket.h includes sys/uio.h to get the definition of the iovec structure. POSIX allows sys/socket.h to make all sys/uio.h symbols visible. However, all of sys/uio.h is XSI-shaded, so for non-XSI POSIX this results in conformtest failures (for sys/socket.h and other headers that include it): Namespace violation: "UIO_MAXIOV" Namespace violation: "readv" Namespace violation: "writev" Now, there is some ambiguity in POSIX about what namespace reservations apply in this case - see http://austingroupbugs.net/view.php?id=1127 - but glibc convention would still avoid declaring readv and writev, for example, for feature test macros that don't include them (if only headers from the relevant standard are included), even if such declarations are permitted, so there is a bug here according to glibc conventions. This patch moves the struct iovec definition to a new bits/types/struct_iovec.h header and includes that from sys/socket.h instead of including the whole of sys/uio.h. This fixes the namespace issue; however, three files in glibc that were relying on the implicit inclusion needed to be updated to include sys/uio.h explicitly. So there is a question of whether sys/socket.h should continue to include sys/uio.h under some conditions, such as __USE_XOPEN or __USE_MISC or __USE_XOPEN || __USE_MISC, for greater compatibility with code that (wrongly) expects this optional inclusion to be present there. (I think the three affected files in glibc should still have explicit sys/uio.h inclusions added in any case, however.) Tested for x86_64. [BZ #21426] * misc/bits/types/struct_iovec.h: New file. * misc/Makefile (headers): Add bits/types/struct_iovec.h. * include/bits/types/struct_iovec.h: New file. * bits/uio.h (struct iovec): Replace by inclusion of <bits/types/struct_iovec.h>. * sysdeps/unix/sysv/linux/bits/uio.h (struct iovec): Likewise. * socket/sys/socket.h: Include <bits/types/struct_iovec.h> instead of <sys/uio.h>. * nptl/tst-cancel4.c: Include <sys/uio.h> * posix/test-errno.c: Likewise. * support/resolv_test.c: Likewise. * conform/Makefile (test-xfail-POSIX2008/arpa/inet.h/conform): Remove. (test-xfail-POSIX2008/netdb.h/conform): Likewise. (test-xfail-POSIX2008/netinet/in.h/conform): Likewise. (test-xfail-POSIX2008/sys/socket.h/conform): Likewise.
2017-04-26 01:52:47 +08:00
#include <sys/uio.h>
#include <unistd.h>
#include <netinet/in.h>
/* This is not an exhaustive test: only system calls that can be
persuaded to fail with a consistent error code and no side effects
are included. Usually these are failures due to invalid arguments,
with errno code EBADF or EINVAL. The order of argument checks is
unspecified, so we must take care to provide arguments that only
allow _one_ failure mode.
Note that all system calls that can fail with EFAULT are permitted
to deliver a SIGSEGV signal instead, so we avoid supplying invalid
pointers in general, and we do not attempt to test system calls
that can only fail with EFAULT (e.g. gettimeofday, gethostname).
Also note that root-only system calls (e.g. acct, reboot) may, when
the test is run as an unprivileged user, fail due to insufficient
privileges before bothering to do argument checks, so those are not
tested either.
Also, system calls that take enum or a set of flags as argument is
not tested if POSIX doesn't specify exact binary values for all
flags, and so any value passed to flags may become valid.
Some tests assume "/bin/sh" names a file that exists and is not a
directory. */
#define test_wrp_rv(rtype, prtype, experr, syscall, ...) \
(__extension__ ({ \
errno = 0xdead; \
rtype ret = syscall (__VA_ARGS__); \
int err = errno; \
int fail; \
if (ret == (rtype) -1 && err == experr) \
fail = 0; \
else \
{ \
fail = 1; \
if (ret != (rtype) -1) \
printf ("FAIL: " #syscall ": didn't fail as expected" \
" (return "prtype")\n", ret); \
else if (err == 0xdead) \
puts("FAIL: " #syscall ": didn't update errno\n"); \
else if (err != experr) \
printf ("FAIL: " #syscall \
": errno is: %d (%s) expected: %d (%s)\n", \
err, strerror (err), experr, strerror (experr)); \
} \
fail; \
}))
#define test_wrp(experr, syscall, ...) \
test_wrp_rv(int, "%d", experr, syscall, __VA_ARGS__)
static int
do_test (void)
{
size_t pagesize = sysconf (_SC_PAGESIZE);
struct statfs sfs;
struct sockaddr sa;
socklen_t sl;
char buf[1];
struct iovec iov[1] = { { buf, 1 } };
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons (1026);
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
struct msghdr msg;
memset(&msg, 0, sizeof msg);
msg.msg_iov = iov;
msg.msg_iovlen = 1;
int fails = 0;
fails |= test_wrp (EBADF, accept, -1, &sa, &sl);
fails |= test_wrp (EINVAL, access, "/", -1);
fails |= test_wrp (EBADF, bind, -1, (struct sockaddr *)&sin, sizeof sin);
fails |= test_wrp (ENOTDIR, chdir, "/bin/sh");
fails |= test_wrp (EBADF, close, -1);
fails |= test_wrp (EBADF, connect, -1, (struct sockaddr *)&sin, sizeof sin);
fails |= test_wrp (EBADF, dup, -1);
fails |= test_wrp (EBADF, dup2, -1, -1);
fails |= test_wrp (EBADF, fchdir, -1);
fails |= test_wrp (EBADF, fchmod, -1, 0);
fails |= test_wrp (EBADF, fcntl, -1, 0);
fails |= test_wrp (EBADF, fstatfs, -1, &sfs);
fails |= test_wrp (EBADF, fsync, -1);
fails |= test_wrp (EBADF, ftruncate, -1, 0);
#if __GNUC_PREREQ (7, 0)
DIAG_PUSH_NEEDS_COMMENT;
/* Avoid warnings about the second (size) argument being negative. */
DIAG_IGNORE_NEEDS_COMMENT (10.1, "-Wstringop-overflow");
#endif
fails |= test_wrp (EINVAL, getgroups, -1, 0);
#if __GNUC_PREREQ (7, 0)
DIAG_POP_NEEDS_COMMENT;
#endif
fails |= test_wrp (EBADF, getpeername, -1, &sa, &sl);
fails |= test_wrp (EBADF, getsockname, -1, &sa, &sl);
fails |= test_wrp (EBADF, getsockopt, -1, 0, 0, buf, &sl);
fails |= test_wrp (EBADF, ioctl, -1, TIOCNOTTY);
fails |= test_wrp (EBADF, listen, -1, 1);
fails |= test_wrp (EBADF, lseek, -1, 0, 0);
fails |= test_wrp (EINVAL, madvise, (void *) -1, -1, 0);
fails |= test_wrp_rv (void *, "%p", EBADF,
mmap, 0, pagesize, PROT_READ, MAP_PRIVATE, -1, 0);
fails |= test_wrp (EINVAL, mprotect, (void *) -1, pagesize, -1);
fails |= test_wrp (EINVAL, msync, (void *) -1, pagesize, -1);
fails |= test_wrp (EINVAL, munmap, (void *) -1, 0);
fails |= test_wrp (EISDIR, open, "/bin", EISDIR, O_WRONLY);
fails |= test_wrp (EBADF, read, -1, buf, 1);
fails |= test_wrp (EINVAL, readlink, "/", buf, sizeof buf);
fails |= test_wrp (EBADF, readv, -1, iov, 1);
fails |= test_wrp (EBADF, recv, -1, buf, 1, 0);
fails |= test_wrp (EBADF, recvfrom, -1, buf, 1, 0, &sa, &sl);
fails |= test_wrp (EBADF, recvmsg, -1, &msg, 0);
fails |= test_wrp (EINVAL, select, -1, 0, 0, 0, 0);
fails |= test_wrp (EBADF, send, -1, buf, 1, 0);
fails |= test_wrp (EBADF, sendmsg, -1, &msg, 0);
fails |= test_wrp (EBADF, sendto, -1, buf, 1, 0, &sa, sl);
fails |= test_wrp (EBADF, setsockopt, -1, 0, 0, buf, sizeof (*buf));
fails |= test_wrp (EBADF, shutdown, -1, SHUT_RD);
fails |= test_wrp (EBADF, write, -1, "Hello", sizeof ("Hello") );
fails |= test_wrp (EBADF, writev, -1, iov, 1 );
return fails;
}
#include "support/test-driver.c"