xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_posix_fadvise.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
1 /* $NetBSD: t_posix_fadvise.c,v 1.1 2011/10/15 06:10:26 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by YAMAMOTO Takashi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c)2005 YAMAMOTO Takashi,
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57 
58 #include <sys/cdefs.h>
59 __COPYRIGHT("@(#) Copyright (c) 2008\
60  The NetBSD Foundation, inc. All rights reserved.");
61 __RCSID("$NetBSD: t_posix_fadvise.c,v 1.1 2011/10/15 06:10:26 jruoho Exp $");
62 
63 #include <sys/fcntl.h>
64 
65 #include <errno.h>
66 #include <string.h>
67 #include <unistd.h>
68 
69 #include <atf-c.h>
70 
71 #include "../../../h_macros.h"
72 
73 #include <rump/rump.h>
74 #include <rump/rump_syscalls.h>
75 
76 ATF_TC(posix_fadvise);
77 ATF_TC_HEAD(posix_fadvise, tc)
78 {
79 	atf_tc_set_md_var(tc, "descr", "Checks posix_fadvise(2)");
80 }
81 
82 ATF_TC(posix_fadvise_reg);
83 ATF_TC_HEAD(posix_fadvise_reg, tc)
84 {
85 	atf_tc_set_md_var(tc, "descr", "Checks posix_fadvise(2) "
86 	    "for regular files");
87 }
88 
89 ATF_TC_BODY(posix_fadvise, tc)
90 {
91 	int fd;
92 	int pipe_fds[2];
93 	int badfd = 10;
94 	int ret;
95 
96 	RL(fd = open("/dev/null", O_RDWR));
97 
98 	(void)close(badfd);
99 	RL(pipe(pipe_fds));
100 
101 	/*
102 	 * it's hard to check if posix_fadvise is working properly.
103 	 * only check return values here.
104 	 */
105 
106 	/* posix_fadvise shouldn't affect errno. */
107 
108 #define CE(x, exp) \
109 	do { \
110 		int save = errno; \
111 		errno = 999; \
112 		ATF_CHECK_EQ_MSG(ret = (x), exp, "got: %d", ret); \
113 		ATF_CHECK_EQ_MSG(errno, 999, "got: %s", strerror(errno)); \
114 		errno = save; \
115 	} while (0);
116 
117 	CE(posix_fadvise(fd, 0, 0, -1), EINVAL);
118 	CE(posix_fadvise(pipe_fds[0], 0, 0, POSIX_FADV_NORMAL), ESPIPE);
119 	CE(posix_fadvise(badfd, 0, 0, POSIX_FADV_NORMAL), EBADF);
120 	CE(posix_fadvise(fd, 0, 0, POSIX_FADV_NORMAL), 0);
121 	CE(posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL), 0);
122 	CE(posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM), 0);
123 	CE(posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED), 0);
124 	CE(posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED), 0);
125 	CE(posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE), 0);
126 }
127 
128 ATF_TC_BODY(posix_fadvise_reg, tc)
129 {
130 	int rfd, ret;
131 
132 	rump_init();
133 	RL(rfd = rump_sys_open("/a_file", O_CREAT, 0666));
134 
135 	CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_NORMAL), 0);
136 	CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_SEQUENTIAL), 0);
137 	CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_RANDOM), 0);
138 	CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_WILLNEED), 0);
139 	CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_NOREUSE), 0);
140 
141 	CE(rump_sys_posix_fadvise(rfd,
142 	    INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_NORMAL), 0);
143 	CE(rump_sys_posix_fadvise(rfd,
144 	    INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_SEQUENTIAL), 0);
145 	CE(rump_sys_posix_fadvise(rfd,
146 	    INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_RANDOM), 0);
147 	CE(rump_sys_posix_fadvise(rfd,
148 	    INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_WILLNEED), 0);
149 	CE(rump_sys_posix_fadvise(rfd,
150 	    INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_NOREUSE), 0);
151 
152 	//atf_tc_expect_signal(-1, "http://mail-index.netbsd.org/source-changes-d/2010/11/11/msg002508.html");
153 	CE(rump_sys_posix_fadvise(rfd,
154 	    INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_DONTNEED), 0);
155 	CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_DONTNEED), 0);
156 #undef CE
157 }
158 
159 ATF_TP_ADD_TCS(tp)
160 {
161 	ATF_TP_ADD_TC(tp, posix_fadvise);
162 	ATF_TP_ADD_TC(tp, posix_fadvise_reg);
163 
164 	return atf_no_error();
165 }
166