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