1*63d1fd59SEnji Cooper /* $NetBSD: t_posix_fadvise.c,v 1.2 2017/01/13 21:30:41 christos Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*-
457718be8SEnji Cooper * Copyright (c) 2008 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper * by YAMAMOTO Takashi.
957718be8SEnji Cooper *
1057718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper * are met:
1357718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper *
1957718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper */
3157718be8SEnji Cooper
3257718be8SEnji Cooper /*-
3357718be8SEnji Cooper * Copyright (c)2005 YAMAMOTO Takashi,
3457718be8SEnji Cooper * All rights reserved.
3557718be8SEnji Cooper *
3657718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
3757718be8SEnji Cooper * modification, are permitted provided that the following conditions
3857718be8SEnji Cooper * are met:
3957718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
4057718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
4157718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
4257718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
4357718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
4457718be8SEnji Cooper *
4557718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
4657718be8SEnji Cooper * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4757718be8SEnji Cooper * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4857718be8SEnji Cooper * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4957718be8SEnji Cooper * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5057718be8SEnji Cooper * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5157718be8SEnji Cooper * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5257718be8SEnji Cooper * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5357718be8SEnji Cooper * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5457718be8SEnji Cooper * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5557718be8SEnji Cooper * SUCH DAMAGE.
5657718be8SEnji Cooper */
5757718be8SEnji Cooper
5857718be8SEnji Cooper #include <sys/cdefs.h>
5957718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2008\
6057718be8SEnji Cooper The NetBSD Foundation, inc. All rights reserved.");
61*63d1fd59SEnji Cooper __RCSID("$NetBSD: t_posix_fadvise.c,v 1.2 2017/01/13 21:30:41 christos Exp $");
6257718be8SEnji Cooper
6357718be8SEnji Cooper #include <sys/fcntl.h>
6457718be8SEnji Cooper
6557718be8SEnji Cooper #include <errno.h>
6657718be8SEnji Cooper #include <string.h>
6757718be8SEnji Cooper #include <unistd.h>
6857718be8SEnji Cooper
6957718be8SEnji Cooper #include <atf-c.h>
7057718be8SEnji Cooper
71*63d1fd59SEnji Cooper #include "h_macros.h"
7257718be8SEnji Cooper
7357718be8SEnji Cooper #include <rump/rump.h>
7457718be8SEnji Cooper #include <rump/rump_syscalls.h>
7557718be8SEnji Cooper
7657718be8SEnji Cooper ATF_TC(posix_fadvise);
ATF_TC_HEAD(posix_fadvise,tc)7757718be8SEnji Cooper ATF_TC_HEAD(posix_fadvise, tc)
7857718be8SEnji Cooper {
7957718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Checks posix_fadvise(2)");
8057718be8SEnji Cooper }
8157718be8SEnji Cooper
8257718be8SEnji Cooper ATF_TC(posix_fadvise_reg);
ATF_TC_HEAD(posix_fadvise_reg,tc)8357718be8SEnji Cooper ATF_TC_HEAD(posix_fadvise_reg, tc)
8457718be8SEnji Cooper {
8557718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Checks posix_fadvise(2) "
8657718be8SEnji Cooper "for regular files");
8757718be8SEnji Cooper }
8857718be8SEnji Cooper
ATF_TC_BODY(posix_fadvise,tc)8957718be8SEnji Cooper ATF_TC_BODY(posix_fadvise, tc)
9057718be8SEnji Cooper {
9157718be8SEnji Cooper int fd;
9257718be8SEnji Cooper int pipe_fds[2];
9357718be8SEnji Cooper int badfd = 10;
9457718be8SEnji Cooper int ret;
9557718be8SEnji Cooper
9657718be8SEnji Cooper RL(fd = open("/dev/null", O_RDWR));
9757718be8SEnji Cooper
9857718be8SEnji Cooper (void)close(badfd);
9957718be8SEnji Cooper RL(pipe(pipe_fds));
10057718be8SEnji Cooper
10157718be8SEnji Cooper /*
10257718be8SEnji Cooper * it's hard to check if posix_fadvise is working properly.
10357718be8SEnji Cooper * only check return values here.
10457718be8SEnji Cooper */
10557718be8SEnji Cooper
10657718be8SEnji Cooper /* posix_fadvise shouldn't affect errno. */
10757718be8SEnji Cooper
10857718be8SEnji Cooper #define CE(x, exp) \
10957718be8SEnji Cooper do { \
11057718be8SEnji Cooper int save = errno; \
11157718be8SEnji Cooper errno = 999; \
11257718be8SEnji Cooper ATF_CHECK_EQ_MSG(ret = (x), exp, "got: %d", ret); \
11357718be8SEnji Cooper ATF_CHECK_EQ_MSG(errno, 999, "got: %s", strerror(errno)); \
11457718be8SEnji Cooper errno = save; \
11557718be8SEnji Cooper } while (0);
11657718be8SEnji Cooper
11757718be8SEnji Cooper CE(posix_fadvise(fd, 0, 0, -1), EINVAL);
11857718be8SEnji Cooper CE(posix_fadvise(pipe_fds[0], 0, 0, POSIX_FADV_NORMAL), ESPIPE);
11957718be8SEnji Cooper CE(posix_fadvise(badfd, 0, 0, POSIX_FADV_NORMAL), EBADF);
12057718be8SEnji Cooper CE(posix_fadvise(fd, 0, 0, POSIX_FADV_NORMAL), 0);
12157718be8SEnji Cooper CE(posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL), 0);
12257718be8SEnji Cooper CE(posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM), 0);
12357718be8SEnji Cooper CE(posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED), 0);
12457718be8SEnji Cooper CE(posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED), 0);
12557718be8SEnji Cooper CE(posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE), 0);
12657718be8SEnji Cooper }
12757718be8SEnji Cooper
ATF_TC_BODY(posix_fadvise_reg,tc)12857718be8SEnji Cooper ATF_TC_BODY(posix_fadvise_reg, tc)
12957718be8SEnji Cooper {
13057718be8SEnji Cooper int rfd, ret;
13157718be8SEnji Cooper
13257718be8SEnji Cooper rump_init();
13357718be8SEnji Cooper RL(rfd = rump_sys_open("/a_file", O_CREAT, 0666));
13457718be8SEnji Cooper
13557718be8SEnji Cooper CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_NORMAL), 0);
13657718be8SEnji Cooper CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_SEQUENTIAL), 0);
13757718be8SEnji Cooper CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_RANDOM), 0);
13857718be8SEnji Cooper CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_WILLNEED), 0);
13957718be8SEnji Cooper CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_NOREUSE), 0);
14057718be8SEnji Cooper
14157718be8SEnji Cooper CE(rump_sys_posix_fadvise(rfd,
14257718be8SEnji Cooper INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_NORMAL), 0);
14357718be8SEnji Cooper CE(rump_sys_posix_fadvise(rfd,
14457718be8SEnji Cooper INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_SEQUENTIAL), 0);
14557718be8SEnji Cooper CE(rump_sys_posix_fadvise(rfd,
14657718be8SEnji Cooper INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_RANDOM), 0);
14757718be8SEnji Cooper CE(rump_sys_posix_fadvise(rfd,
14857718be8SEnji Cooper INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_WILLNEED), 0);
14957718be8SEnji Cooper CE(rump_sys_posix_fadvise(rfd,
15057718be8SEnji Cooper INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_NOREUSE), 0);
15157718be8SEnji Cooper
15257718be8SEnji Cooper //atf_tc_expect_signal(-1, "http://mail-index.netbsd.org/source-changes-d/2010/11/11/msg002508.html");
15357718be8SEnji Cooper CE(rump_sys_posix_fadvise(rfd,
15457718be8SEnji Cooper INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_DONTNEED), 0);
15557718be8SEnji Cooper CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_DONTNEED), 0);
15657718be8SEnji Cooper #undef CE
15757718be8SEnji Cooper }
15857718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)15957718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
16057718be8SEnji Cooper {
16157718be8SEnji Cooper ATF_TP_ADD_TC(tp, posix_fadvise);
16257718be8SEnji Cooper ATF_TP_ADD_TC(tp, posix_fadvise_reg);
16357718be8SEnji Cooper
16457718be8SEnji Cooper return atf_no_error();
16557718be8SEnji Cooper }
166