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