Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/user/testbin/badcall/common_fds.c
734 views
1
/*
2
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3
* The President and Fellows of Harvard College.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the University nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*/
29
30
/*
31
* Calls with invalid fds
32
*/
33
34
#include <sys/types.h>
35
#include <sys/stat.h>
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
#include <unistd.h>
40
#include <limits.h>
41
#include <errno.h>
42
#include <err.h>
43
44
#include "config.h"
45
#include "test.h"
46
47
48
static
49
int
50
read_badfd(int fd)
51
{
52
char buf[128];
53
return read(fd, buf, sizeof(buf));
54
}
55
56
static
57
int
58
write_badfd(int fd)
59
{
60
char buf[128];
61
memset(buf, 'a', sizeof(buf));
62
return write(fd, buf, sizeof(buf));
63
}
64
65
66
static
67
int
68
close_badfd(int fd)
69
{
70
return close(fd);
71
}
72
73
static
74
int
75
ioctl_badfd(int fd)
76
{
77
return ioctl(fd, 0, NULL);
78
}
79
80
static
81
int
82
lseek_badfd(int fd)
83
{
84
return lseek(fd, 0, SEEK_SET);
85
}
86
87
static
88
int
89
fsync_badfd(int fd)
90
{
91
return fsync(fd);
92
}
93
94
static
95
int
96
ftruncate_badfd(int fd)
97
{
98
return ftruncate(fd, 60);
99
}
100
101
static
102
int
103
fstat_badfd(int fd)
104
{
105
struct stat sb;
106
return fstat(fd, &sb);
107
}
108
109
static
110
int
111
getdirentry_badfd(int fd)
112
{
113
char buf[32];
114
return getdirentry(fd, buf, sizeof(buf));
115
}
116
117
static
118
int
119
dup2_badfd(int fd)
120
{
121
/* use the +1 to avoid doing dup2(CLOSED_FD, CLOSED_FD) */
122
return dup2(fd, CLOSED_FD+1);
123
}
124
125
static
126
void
127
dup2_cleanup(void)
128
{
129
close(CLOSED_FD+1);
130
}
131
132
////////////////////////////////////////////////////////////
133
134
static
135
void
136
any_badfd(int (*func)(int fd), void (*cleanup)(void), const char *callname,
137
int fd, const char *fddesc)
138
{
139
char fulldesc[128];
140
int rv;
141
142
snprintf(fulldesc, sizeof(fulldesc), "%s using %s", callname, fddesc);
143
rv = func(fd);
144
report_test(rv, errno, EBADF, fulldesc);
145
if (cleanup) {
146
cleanup();
147
}
148
}
149
150
static
151
void
152
runtest(int (*func)(int fd), void (*cleanup)(void), const char *callname)
153
{
154
/*
155
* If adding cases, also see bad_dup2.c
156
*/
157
158
/* basic invalid case: fd -1 */
159
any_badfd(func, cleanup, callname, -1, "fd -1");
160
161
/* also try -5 in case -1 is special somehow */
162
any_badfd(func, cleanup, callname, -5, "fd -5");
163
164
/* try a fd we know is closed */
165
any_badfd(func, cleanup, callname, CLOSED_FD, "closed fd");
166
167
/* try a positive fd we know is out of range */
168
any_badfd(func, cleanup, callname, IMPOSSIBLE_FD, "impossible fd");
169
170
/* test for off-by-one errors */
171
#ifdef OPEN_MAX
172
any_badfd(func, cleanup, callname, OPEN_MAX, "fd OPEN_MAX");
173
#else
174
warnx("Warning: OPEN_MAX not defined, test skipped");
175
#endif
176
}
177
178
////////////////////////////////////////////////////////////
179
180
#define T(call) \
181
void \
182
test_##call##_fd(void) \
183
{ \
184
runtest(call##_badfd, NULL, #call); \
185
}
186
187
#define TC(call) \
188
void \
189
test_##call##_fd(void) \
190
{ \
191
runtest(call##_badfd, call##_cleanup, #call);\
192
}
193
194
T(read);
195
T(write);
196
T(close);
197
T(ioctl);
198
T(lseek);
199
T(fsync);
200
T(ftruncate);
201
T(fstat);
202
T(getdirentry);
203
TC(dup2);
204
205