diff --git a/distrib/sets/lists/tests/mi b/distrib/sets/lists/tests/mi index ec895ec0028d..553f65cd83b0 100644 --- a/distrib/sets/lists/tests/mi +++ b/distrib/sets/lists/tests/mi @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.342 2011/06/01 19:45:08 tron Exp $ +# $NetBSD: mi,v 1.343 2011/06/02 10:48:55 jruoho Exp $ # # Note: don't delete entries from here - mark them as "obsolete" instead. # @@ -617,6 +617,7 @@ ./usr/libdata/debug/usr/tests/syscall/t_gettimeofday.debug tests-syscall-debug debug,atf ./usr/libdata/debug/usr/tests/syscall/t_itimer.debug tests-syscall-debug debug,atf ./usr/libdata/debug/usr/tests/syscall/t_kill.debug tests-syscall-debug debug,atf +./usr/libdata/debug/usr/tests/syscall/t_mincore.debug tests-syscall-debug debug,atf ./usr/libdata/debug/usr/tests/syscall/t_mmap.debug tests-syscall-debug debug,atf ./usr/libdata/debug/usr/tests/syscall/t_mprotect.debug tests-syscall-debug debug,atf ./usr/libdata/debug/usr/tests/syscall/t_msync.debug tests-syscall-debug debug,atf @@ -2323,6 +2324,7 @@ ./usr/tests/syscall/t_gettimeofday tests-syscall-tests atf ./usr/tests/syscall/t_itimer tests-syscall-tests atf ./usr/tests/syscall/t_kill tests-syscall-tests atf +./usr/tests/syscall/t_mincore tests-syscall-tests atf ./usr/tests/syscall/t_mmap tests-syscall-tests atf ./usr/tests/syscall/t_mprotect tests-syscall-tests atf ./usr/tests/syscall/t_msync tests-syscall-tests atf diff --git a/tests/syscall/Makefile b/tests/syscall/Makefile index 0823fb37b0a4..7c27719940dc 100644 --- a/tests/syscall/Makefile +++ b/tests/syscall/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.29 2011/06/01 19:43:10 tron Exp $ +# $NetBSD: Makefile,v 1.30 2011/06/02 10:48:56 jruoho Exp $ .include @@ -6,8 +6,10 @@ TESTSDIR= ${TESTSBASE}/syscall TESTS_C+= t_access t_cmsg t_dup t_fsync TESTS_C+= t_getgroups t_getpid t_getrusage t_getsid t_gettimeofday -TESTS_C+= t_itimer t_kill t_mmap t_mprotect t_msync t_nanosleep t_poll -TESTS_C+= t_pollts t_pselect t_setrlimit t_setuid t_timer t_umask +TESTS_C+= t_itimer t_kill +TESTS_C+= t_mincore t_mmap t_mprotect t_msync t_nanosleep +TESTS_C+= t_poll t_pollts t_pselect +TESTS_C+= t_setrlimit t_setuid t_timer t_umask LDADD.t_getpid+= -lpthread LDADD.t_timer+= -lpthread diff --git a/tests/syscall/t_mincore.c b/tests/syscall/t_mincore.c new file mode 100644 index 000000000000..9fe69a74673c --- /dev/null +++ b/tests/syscall/t_mincore.c @@ -0,0 +1,189 @@ +/* $NetBSD: t_mincore.c,v 1.1 2011/06/02 10:48:55 jruoho Exp $ */ + +/*- + * Copyright (c) 2011 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jukka Ruohonen. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include +__RCSID("$NetBSD: t_mincore.c,v 1.1 2011/06/02 10:48:55 jruoho Exp $"); + +#include + +#include +#include +#include +#include +#include + +static long page = 0; +static const char path[] = "mincore"; + +ATF_TC(mincore_err); +ATF_TC_HEAD(mincore_err, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test errors from mincore(2)"); +} + +ATF_TC_BODY(mincore_err, tc) +{ + char *map, *vec; + + map = mmap(NULL, page, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); + vec = malloc(page); + + ATF_REQUIRE(vec != NULL); + ATF_REQUIRE(map != MAP_FAILED); + + errno = 0; + ATF_REQUIRE_ERRNO(EINVAL, mincore(map, 0, vec) == -1); + + errno = 0; + ATF_REQUIRE_ERRNO(ENOMEM, mincore(0, page, vec) == -1); + + errno = 0; + ATF_REQUIRE_ERRNO(EFAULT, mincore(map, page, (void *)-1) == -1); + + free(vec); + ATF_REQUIRE(munmap(map, page) == 0); +} + +ATF_TC_WITH_CLEANUP(mincore_incore); +ATF_TC_HEAD(mincore_incore, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test that mincore(2) works"); +} + +ATF_TC_BODY(mincore_incore, tc) +{ + char *buf, *vec, *map = MAP_FAILED; + const char *str = NULL; + const size_t n = 3; + ssize_t tot; + int fd, rv; + size_t i, j; + + /* + * Create a temporary file, write + * few pages to it, and map the file. + */ + buf = calloc(n, page); + vec = calloc(n, page); + + if (buf == NULL || vec == NULL) + return; + + for (i = 0; i < (size_t)page * n; i++) + buf[i] = 'x'; + + fd = open(path, O_RDWR | O_CREAT, 0600); + + if (fd < 0) { + str = "failed to open"; + goto out; + } + + tot = 0; + + while (tot < page * (long)n) { + + rv = write(fd, buf, sizeof(buf)); + + if (rv < 0) { + str = "failed to write"; + goto out; + } + + tot += rv; + } + + map = mmap(NULL, page * n, PROT_READ | PROT_WRITE, + MAP_FILE | MAP_PRIVATE, fd, 0); + + if (map == MAP_FAILED) { + str = "failed to map"; + goto out; + } + + /* + * Lock the mapping such that only + * in-core page status is returned. + */ + if (mlock(map, page * n) != 0) { + str = "failed to lock"; + goto out; + } + + if (mincore(map, page * n, vec) != 0) { + str = "mincore failed"; + goto out; + } + + /* + * Check that the in-core pages + * match the locked pages. + */ + for (i = j = 0; i < (size_t)page * n; i++) { + + if (vec[i] != 0) + j++; + } + + if (j != n) + str = "mismatch of in-core pages"; + +out: + free(buf); + free(vec); + + (void)close(fd); + (void)unlink(path); + + if (map != MAP_FAILED) { + (void)munlock(map, page); + (void)munmap(map, page); + } + + if (str != NULL) + atf_tc_fail(str); +} + +ATF_TC_CLEANUP(mincore_incore, tc) +{ + (void)unlink(path); +} + +ATF_TP_ADD_TCS(tp) +{ + + page = sysconf(_SC_PAGESIZE); + ATF_REQUIRE(page >= 0); + + ATF_TP_ADD_TC(tp, mincore_err); + ATF_TP_ADD_TC(tp, mincore_incore); + + return atf_no_error(); +}