S390: Move utf8-utf32-z9.c to multiarch folder and use s390_libc_ifunc_expr macro.
[glibc.git] / posix / test-errno.c
blobc2bfd8a80f67541d443a3e18a723f842a8719538
1 /* Test that failing system calls do set errno to the correct value.
3 Copyright (C) 2017 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 <http://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 <unistd.h>
35 #include <netinet/in.h>
37 /* This is not an exhaustive test: only system calls that can be
38 persuaded to fail with a consistent error code and no side effects
39 are included. Usually these are failures due to invalid arguments,
40 with errno code EBADF or EINVAL. The order of argument checks is
41 unspecified, so we must take care to provide arguments that only
42 allow _one_ failure mode.
44 Note that all system calls that can fail with EFAULT are permitted
45 to deliver a SIGSEGV signal instead, so we avoid supplying invalid
46 pointers in general, and we do not attempt to test system calls
47 that can only fail with EFAULT (e.g. gettimeofday, gethostname).
49 Also note that root-only system calls (e.g. acct, reboot) may, when
50 the test is run as an unprivileged user, fail due to insufficient
51 privileges before bothering to do argument checks, so those are not
52 tested either.
54 Also, system calls that take enum or a set of flags as argument is
55 not tested if POSIX doesn't specify exact binary values for all
56 flags, and so any value passed to flags may become valid.
58 Some tests assume "/bin/sh" names a file that exists and is not a
59 directory. */
61 #define test_wrp_rv(rtype, prtype, experr, syscall, ...) \
62 (__extension__ ({ \
63 errno = 0xdead; \
64 rtype ret = syscall (__VA_ARGS__); \
65 int err = errno; \
66 int fail; \
67 if (ret == (rtype) -1 && err == experr) \
68 fail = 0; \
69 else \
70 { \
71 fail = 1; \
72 if (ret != (rtype) -1) \
73 printf ("FAIL: " #syscall ": didn't fail as expected" \
74 " (return "prtype")\n", ret); \
75 else if (err == 0xdead) \
76 puts("FAIL: " #syscall ": didn't update errno\n"); \
77 else if (err != experr) \
78 printf ("FAIL: " #syscall \
79 ": errno is: %d (%s) expected: %d (%s)\n", \
80 err, strerror (err), experr, strerror (experr)); \
81 } \
82 fail; \
83 }))
85 #define test_wrp(experr, syscall, ...) \
86 test_wrp_rv(int, "%d", experr, syscall, __VA_ARGS__)
88 static int
89 do_test (void)
91 size_t pagesize = sysconf (_SC_PAGESIZE);
92 struct statfs sfs;
93 struct sockaddr sa;
94 socklen_t sl;
95 char buf[1];
96 struct iovec iov[1] = { { buf, 1 } };
97 struct sockaddr_in sin;
98 sin.sin_family = AF_INET;
99 sin.sin_port = htons (1026);
100 sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
101 struct msghdr msg;
102 memset(&msg, 0, sizeof msg);
103 msg.msg_iov = iov;
104 msg.msg_iovlen = 1;
106 int fails = 0;
107 fails |= test_wrp (EBADF, accept, -1, &sa, &sl);
108 fails |= test_wrp (EINVAL, access, "/", -1);
109 fails |= test_wrp (EBADF, bind, -1, (struct sockaddr *)&sin, sizeof sin);
110 fails |= test_wrp (ENOTDIR, chdir, "/bin/sh");
111 fails |= test_wrp (EBADF, close, -1);
112 fails |= test_wrp (EBADF, connect, -1, (struct sockaddr *)&sin, sizeof sin);
113 fails |= test_wrp (EBADF, dup, -1);
114 fails |= test_wrp (EBADF, dup2, -1, -1);
115 fails |= test_wrp (EBADF, fchdir, -1);
116 fails |= test_wrp (EBADF, fchmod, -1, 0);
117 fails |= test_wrp (EBADF, fcntl, -1, 0);
118 fails |= test_wrp (EBADF, fstatfs, -1, &sfs);
119 fails |= test_wrp (EBADF, fsync, -1);
120 fails |= test_wrp (EBADF, ftruncate, -1, 0);
121 fails |= test_wrp (EINVAL, getgroups, -1, 0);
122 fails |= test_wrp (EBADF, getpeername, -1, &sa, &sl);
123 fails |= test_wrp (EBADF, getsockname, -1, &sa, &sl);
124 fails |= test_wrp (EBADF, getsockopt, -1, 0, 0, buf, &sl);
125 fails |= test_wrp (EBADF, ioctl, -1, TIOCNOTTY);
126 fails |= test_wrp (EBADF, listen, -1, 1);
127 fails |= test_wrp (EBADF, lseek, -1, 0, 0);
128 fails |= test_wrp (EINVAL, madvise, (void *) -1, -1, 0);
129 fails |= test_wrp_rv (void *, "%p", EBADF,
130 mmap, 0, pagesize, PROT_READ, MAP_PRIVATE, -1, 0);
131 fails |= test_wrp (EINVAL, mprotect, (void *) -1, pagesize, -1);
132 fails |= test_wrp (EINVAL, msync, (void *) -1, pagesize, -1);
133 fails |= test_wrp (EINVAL, munmap, (void *) -1, 0);
134 fails |= test_wrp (EISDIR, open, "/bin", EISDIR, O_WRONLY);
135 fails |= test_wrp (EBADF, read, -1, buf, 1);
136 fails |= test_wrp (EINVAL, readlink, "/", buf, -1);
137 fails |= test_wrp (EBADF, readv, -1, iov, 1);
138 fails |= test_wrp (EBADF, recv, -1, buf, 1, 0);
139 fails |= test_wrp (EBADF, recvfrom, -1, buf, 1, 0, &sa, &sl);
140 fails |= test_wrp (EBADF, recvmsg, -1, &msg, 0);
141 fails |= test_wrp (EINVAL, select, -1, 0, 0, 0, 0);
142 fails |= test_wrp (EBADF, send, -1, buf, 1, 0);
143 fails |= test_wrp (EBADF, sendmsg, -1, &msg, 0);
144 fails |= test_wrp (EBADF, sendto, -1, buf, 1, 0, &sa, sl);
145 fails |= test_wrp (EBADF, setsockopt, -1, 0, 0, buf, sizeof (*buf));
146 fails |= test_wrp (EBADF, shutdown, -1, SHUT_RD);
147 fails |= test_wrp (EBADF, write, -1, "Hello", sizeof ("Hello") );
148 fails |= test_wrp (EBADF, writev, -1, iov, 1 );
150 return fails;
153 #include "support/test-driver.c"