Add AT_RSEQ_* from Linux 6.3 to elf.h
[glibc.git] / posix / test-errno.c
blob305bc429387fa450f85f61a68bbb4f192f67d57e
1 /* Test that failing system calls do set errno to the correct value.
3 Copyright (C) 2017-2023 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #include <errno.h>
21 #include <limits.h>
22 #include <grp.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30 #include <sys/resource.h>
31 #include <sys/types.h>
32 #include <sys/statfs.h>
33 #include <sys/mman.h>
34 #include <sys/uio.h>
35 #include <unistd.h>
36 #include <netinet/in.h>
37 #include <libc-diag.h>
39 /* This is not an exhaustive test: only system calls that can be
40 persuaded to fail with a consistent error code and no side effects
41 are included. Usually these are failures due to invalid arguments,
42 with errno code EBADF or EINVAL. The order of argument checks is
43 unspecified, so we must take care to provide arguments that only
44 allow _one_ failure mode.
46 Note that all system calls that can fail with EFAULT are permitted
47 to deliver a SIGSEGV signal instead, so we avoid supplying invalid
48 pointers in general, and we do not attempt to test system calls
49 that can only fail with EFAULT (e.g. gettimeofday, gethostname).
51 Also note that root-only system calls (e.g. acct, reboot) may, when
52 the test is run as an unprivileged user, fail due to insufficient
53 privileges before bothering to do argument checks, so those are not
54 tested either.
56 Also, system calls that take enum or a set of flags as argument is
57 not tested if POSIX doesn't specify exact binary values for all
58 flags, and so any value passed to flags may become valid.
60 Some tests assume "/bin/sh" names a file that exists and is not a
61 directory. */
63 #define test_wrp_rv(rtype, prtype, experr, syscall, ...) \
64 (__extension__ ({ \
65 errno = 0xdead; \
66 rtype ret = syscall (__VA_ARGS__); \
67 int err = errno; \
68 int fail; \
69 if (ret == (rtype) -1 && err == experr) \
70 fail = 0; \
71 else \
72 { \
73 fail = 1; \
74 if (ret != (rtype) -1) \
75 printf ("FAIL: " #syscall ": didn't fail as expected" \
76 " (return "prtype")\n", ret); \
77 else if (err == 0xdead) \
78 puts("FAIL: " #syscall ": didn't update errno\n"); \
79 else if (err != experr) \
80 printf ("FAIL: " #syscall \
81 ": errno is: %d (%s) expected: %d (%s)\n", \
82 err, strerror (err), experr, strerror (experr)); \
83 } \
84 fail; \
85 }))
87 #define test_wrp(experr, syscall, ...) \
88 test_wrp_rv(int, "%d", experr, syscall, __VA_ARGS__)
90 static int
91 do_test (void)
93 size_t pagesize = sysconf (_SC_PAGESIZE);
94 struct statfs sfs;
95 struct sockaddr sa;
96 socklen_t sl;
97 char buf[1];
98 struct iovec iov[1] = { { buf, 1 } };
99 struct sockaddr_in sin;
100 sin.sin_family = AF_INET;
101 sin.sin_port = htons (1026);
102 sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
103 struct msghdr msg;
104 memset(&msg, 0, sizeof msg);
105 msg.msg_iov = iov;
106 msg.msg_iovlen = 1;
108 int fails = 0;
109 fails |= test_wrp (EBADF, accept, -1, &sa, &sl);
110 fails |= test_wrp (EINVAL, access, "/", -1);
111 fails |= test_wrp (EBADF, bind, -1, (struct sockaddr *)&sin, sizeof sin);
112 fails |= test_wrp (ENOTDIR, chdir, "/bin/sh");
113 fails |= test_wrp (EBADF, close, -1);
114 fails |= test_wrp (EBADF, connect, -1, (struct sockaddr *)&sin, sizeof sin);
115 fails |= test_wrp (EBADF, dup, -1);
116 fails |= test_wrp (EBADF, dup2, -1, -1);
117 fails |= test_wrp (EBADF, fchdir, -1);
118 fails |= test_wrp (EBADF, fchmod, -1, 0);
119 fails |= test_wrp (EBADF, fcntl, -1, 0);
120 fails |= test_wrp (EBADF, fstatfs, -1, &sfs);
121 fails |= test_wrp (EBADF, fsync, -1);
122 fails |= test_wrp (EBADF, ftruncate, -1, 0);
124 #if __GNUC_PREREQ (7, 0)
125 DIAG_PUSH_NEEDS_COMMENT;
126 /* Avoid warnings about the second (size) argument being negative. */
127 DIAG_IGNORE_NEEDS_COMMENT (10.1, "-Wstringop-overflow");
128 #endif
129 fails |= test_wrp (EINVAL, getgroups, -1, 0);
130 #if __GNUC_PREREQ (7, 0)
131 DIAG_POP_NEEDS_COMMENT;
132 #endif
133 fails |= test_wrp (EBADF, getpeername, -1, &sa, &sl);
134 fails |= test_wrp (EBADF, getsockname, -1, &sa, &sl);
135 fails |= test_wrp (EBADF, getsockopt, -1, 0, 0, buf, &sl);
136 fails |= test_wrp (EBADF, ioctl, -1, TIOCNOTTY);
137 fails |= test_wrp (EBADF, listen, -1, 1);
138 fails |= test_wrp (EBADF, lseek, -1, 0, 0);
139 fails |= test_wrp (EINVAL, madvise, (void *) -1, -1, 0);
140 fails |= test_wrp_rv (void *, "%p", EBADF,
141 mmap, 0, pagesize, PROT_READ, MAP_PRIVATE, -1, 0);
142 fails |= test_wrp (EINVAL, mprotect, (void *) -1, pagesize, -1);
143 fails |= test_wrp (EINVAL, msync, (void *) -1, pagesize, -1);
144 fails |= test_wrp (EINVAL, munmap, (void *) -1, 0);
145 fails |= test_wrp (EISDIR, open, "/bin", EISDIR, O_WRONLY);
146 fails |= test_wrp (EBADF, read, -1, buf, 1);
147 fails |= test_wrp (EINVAL, readlink, "/", buf, sizeof buf);
148 fails |= test_wrp (EBADF, readv, -1, iov, 1);
149 fails |= test_wrp (EBADF, recv, -1, buf, 1, 0);
150 fails |= test_wrp (EBADF, recvfrom, -1, buf, 1, 0, &sa, &sl);
151 fails |= test_wrp (EBADF, recvmsg, -1, &msg, 0);
152 fails |= test_wrp (EINVAL, select, -1, 0, 0, 0, 0);
153 fails |= test_wrp (EBADF, send, -1, buf, 1, 0);
154 fails |= test_wrp (EBADF, sendmsg, -1, &msg, 0);
155 fails |= test_wrp (EBADF, sendto, -1, buf, 1, 0, &sa, sl);
156 fails |= test_wrp (EBADF, setsockopt, -1, 0, 0, buf, sizeof (*buf));
157 fails |= test_wrp (EBADF, shutdown, -1, SHUT_RD);
158 fails |= test_wrp (EBADF, write, -1, "Hello", sizeof ("Hello") );
159 fails |= test_wrp (EBADF, writev, -1, iov, 1 );
161 return fails;
164 #include "support/test-driver.c"