Home | History | Annotate | Line # | Download | only in sys
      1 /* $NetBSD: t_posix_fadvise.c,v 1.4 2025/04/06 19:18:00 riastradh 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.4 2025/04/06 19:18:00 riastradh 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 #define CE(x, exp) \
    107 	do { \
    108 		ATF_CHECK_EQ_MSG(ret = (x), (exp), \
    109 		    "got %d (%s), expected %d (%s)", \
    110 		    ret, strerror(ret), \
    111 		    (exp), strerror(exp)); \
    112 	} while (0)
    113 
    114 	CE(posix_fadvise(fd, 0, 0, -1), EINVAL);
    115 	CE(posix_fadvise(pipe_fds[0], 0, 0, POSIX_FADV_NORMAL), ESPIPE);
    116 	CE(posix_fadvise(badfd, 0, 0, POSIX_FADV_NORMAL), EBADF);
    117 	CE(posix_fadvise(fd, 0, 0, POSIX_FADV_NORMAL), 0);
    118 	CE(posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL), 0);
    119 	CE(posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM), 0);
    120 	CE(posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED), 0);
    121 	CE(posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED), 0);
    122 	CE(posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE), 0);
    123 }
    124 
    125 ATF_TC_BODY(posix_fadvise_reg, tc)
    126 {
    127 	int rfd, ret;
    128 
    129 	rump_init();
    130 	RL(rfd = rump_sys_open("/a_file", O_CREAT, 0666));
    131 
    132 	CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_NORMAL), 0);
    133 	CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_SEQUENTIAL), 0);
    134 	CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_RANDOM), 0);
    135 	CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_WILLNEED), 0);
    136 	CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_NOREUSE), 0);
    137 
    138 	CE(rump_sys_posix_fadvise(rfd,
    139 	    INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_NORMAL), 0);
    140 	CE(rump_sys_posix_fadvise(rfd,
    141 	    INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_SEQUENTIAL), 0);
    142 	CE(rump_sys_posix_fadvise(rfd,
    143 	    INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_RANDOM), 0);
    144 	CE(rump_sys_posix_fadvise(rfd,
    145 	    INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_WILLNEED), 0);
    146 	CE(rump_sys_posix_fadvise(rfd,
    147 	    INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_NOREUSE), 0);
    148 
    149 	//atf_tc_expect_signal(-1, "http://mail-index.netbsd.org/source-changes-d/2010/11/11/msg002508.html");
    150 	CE(rump_sys_posix_fadvise(rfd,
    151 	    INT64_MAX-getpagesize(), getpagesize(), POSIX_FADV_DONTNEED), 0);
    152 	CE(rump_sys_posix_fadvise(rfd, 0, 0, POSIX_FADV_DONTNEED), 0);
    153 #undef CE
    154 }
    155 
    156 ATF_TP_ADD_TCS(tp)
    157 {
    158 	ATF_TP_ADD_TC(tp, posix_fadvise);
    159 	ATF_TP_ADD_TC(tp, posix_fadvise_reg);
    160 
    161 	return atf_no_error();
    162 }
    163