diff --git a/libexec/rtld-elf/tests/common.c b/libexec/rtld-elf/tests/common.c index 6eb3edfbc14c..82dd5180b582 100644 --- a/libexec/rtld-elf/tests/common.c +++ b/libexec/rtld-elf/tests/common.c @@ -1,81 +1,81 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * Copyright 2014 Jonathan Anderson. * Copyright 2021 Mariusz Zaborski * * 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 AUTHOR ``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 AUTHOR 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. * * $FreeBSD$ */ #include #include #include #include "common.h" void expect_success(int binary, char *senv) { char * const env[] = { senv, NULL }; try_to_run(binary, 0, env, "the hypotenuse of 3 and 4 is 5\n", ""); } void expect_missing_library(int binary, char *senv) { char * const env[] = { senv, NULL }; try_to_run(binary, 1, env, "", "ld-elf.so.1: Shared object \"libpythagoras.so.0\" not found," " required by \"target\"\n"); } void try_to_run(int binary, int exit_status, char * const *env, const char *expected_out, const char *expected_err) { pid_t child = atf_utils_fork(); if (child == 0) { char * const args[] = { "target", NULL }; fexecve(binary, args, env); atf_tc_fail("fexecve() failed"); } atf_utils_wait(child, exit_status, expected_out, expected_err); } int -opendir(const char *name) +opendir_fd(const char *name) { return open(name, O_RDONLY | O_DIRECTORY); } int opendirat(int parent, const char *name) { return openat(parent, name, O_RDONLY | O_DIRECTORY); } diff --git a/libexec/rtld-elf/tests/common.h b/libexec/rtld-elf/tests/common.h index d5bf2050dcfd..12a9eec36eb5 100644 --- a/libexec/rtld-elf/tests/common.h +++ b/libexec/rtld-elf/tests/common.h @@ -1,43 +1,43 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * Copyright 2014 Jonathan Anderson. * Copyright 2021 Mariusz Zaborski * * 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 AUTHOR ``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 AUTHOR 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. * * $FreeBSD$ */ #ifndef _LD_COMMON_H_ #define _LD_COMMON_H_ #define TARGET_ELF_NAME "target" #define TARGET_LIBRARY "libpythagoras.so.0" void expect_success(int binary, char *senv); void expect_missing_library(int binary, char *senv); void try_to_run(int binary, int expected_exit_status, char * const *env, const char *expected_out, const char *expected_err); -int opendir(const char *name); +int opendir_fd(const char *name); int opendirat(int parent, const char *name); #endif /* _LD_COMMON_H_ */ diff --git a/libexec/rtld-elf/tests/ld_library_pathfds.c b/libexec/rtld-elf/tests/ld_library_pathfds.c index bc0627d4c3d5..ed9f51c175fa 100644 --- a/libexec/rtld-elf/tests/ld_library_pathfds.c +++ b/libexec/rtld-elf/tests/ld_library_pathfds.c @@ -1,171 +1,171 @@ /*- * Copyright 2014 Jonathan Anderson. * All rights reserved. * * 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 AUTHOR ``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 AUTHOR 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. * * $FreeBSD$ */ #include #include #include #include "common.h" struct descriptors { int binary; int testdir; int root; int etc; int usr; }; static void setup(struct descriptors *, const atf_tc_t *); ATF_TC_WITHOUT_HEAD(missing_library); ATF_TC_BODY(missing_library, tc) { struct descriptors files; setup(&files, tc); expect_missing_library(files.binary, NULL); } ATF_TC_WITHOUT_HEAD(wrong_library_directories); ATF_TC_BODY(wrong_library_directories, tc) { struct descriptors files; char *pathfds; setup(&files, tc); ATF_REQUIRE( asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.etc) > 0); expect_missing_library(files.binary, pathfds); } ATF_TC_WITHOUT_HEAD(bad_library_directories); ATF_TC_BODY(bad_library_directories, tc) { struct descriptors files; char *pathfds; setup(&files, tc); ATF_REQUIRE(asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=::") > 0); expect_missing_library(files.binary, pathfds); } ATF_TC_WITHOUT_HEAD(single_library_directory); ATF_TC_BODY(single_library_directory, tc) { struct descriptors files; char *pathfds; setup(&files, tc); ATF_REQUIRE( asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d", files.testdir) > 0); expect_success(files.binary, pathfds); } ATF_TC_WITHOUT_HEAD(first_library_directory); ATF_TC_BODY(first_library_directory, tc) { struct descriptors files; char *pathfds; setup(&files, tc); ATF_REQUIRE( asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d", files.testdir, files.etc) > 0); expect_success(files.binary, pathfds); } ATF_TC_WITHOUT_HEAD(middle_library_directory); ATF_TC_BODY(middle_library_directory, tc) { struct descriptors files; char *pathfds; setup(&files, tc); ATF_REQUIRE( asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d:%d", files.root, files.testdir, files.usr) > 0); expect_success(files.binary, pathfds); } ATF_TC_WITHOUT_HEAD(last_library_directory); ATF_TC_BODY(last_library_directory, tc) { struct descriptors files; char *pathfds; setup(&files, tc); ATF_REQUIRE( asprintf(&pathfds, "LD_LIBRARY_PATH_FDS=%d:%d", files.root, files.testdir) > 0); expect_success(files.binary, pathfds); } /* Register test cases with ATF. */ ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, missing_library); ATF_TP_ADD_TC(tp, wrong_library_directories); ATF_TP_ADD_TC(tp, bad_library_directories); ATF_TP_ADD_TC(tp, single_library_directory); ATF_TP_ADD_TC(tp, first_library_directory); ATF_TP_ADD_TC(tp, middle_library_directory); ATF_TP_ADD_TC(tp, last_library_directory); return atf_no_error(); } static void setup(struct descriptors *dp, const atf_tc_t *tc) { - dp->testdir = opendir(atf_tc_get_config_var(tc, "srcdir")); + dp->testdir = opendir_fd(atf_tc_get_config_var(tc, "srcdir")); ATF_REQUIRE(dp->testdir >= 0); ATF_REQUIRE( (dp->binary = openat(dp->testdir, TARGET_ELF_NAME, O_RDONLY)) >= 0); - ATF_REQUIRE((dp->root = opendir("/")) >= 0); + ATF_REQUIRE((dp->root = opendir_fd("/")) >= 0); ATF_REQUIRE((dp->etc = opendirat(dp->root, "etc")) >= 0); ATF_REQUIRE((dp->usr = opendirat(dp->root, "usr")) >= 0); } diff --git a/libexec/rtld-elf/tests/ld_preload_fds.c b/libexec/rtld-elf/tests/ld_preload_fds.c index 3a220b009bb6..99378b549de5 100644 --- a/libexec/rtld-elf/tests/ld_preload_fds.c +++ b/libexec/rtld-elf/tests/ld_preload_fds.c @@ -1,108 +1,108 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * Copyright 2021 Mariusz Zaborski * * 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 AUTHOR ``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 AUTHOR 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. * * $FreeBSD$ */ #include #include #include #include "common.h" int binaryfd; int libraryfd; static void setup(const atf_tc_t *tc) { int testdir; - testdir = opendir(atf_tc_get_config_var(tc, "srcdir")); + testdir = opendir_fd(atf_tc_get_config_var(tc, "srcdir")); ATF_REQUIRE(testdir >= 0); binaryfd = openat(testdir, TARGET_ELF_NAME, O_RDONLY); ATF_REQUIRE(binaryfd >= 0); libraryfd = openat(testdir, TARGET_LIBRARY, O_RDONLY); ATF_REQUIRE(libraryfd >= 0); close(testdir); } ATF_TC_WITHOUT_HEAD(missing_library); ATF_TC_BODY(missing_library, tc) { setup(tc); expect_missing_library(binaryfd, NULL); } ATF_TC_WITHOUT_HEAD(bad_librarys); ATF_TC_BODY(bad_librarys, tc) { char *senv; ATF_REQUIRE(asprintf(&senv, "LD_PRELOAD_FDS=::") > 0); setup(tc); expect_missing_library(binaryfd, senv); } ATF_TC_WITHOUT_HEAD(single_library); ATF_TC_BODY(single_library, tc) { char *senv; setup(tc); ATF_REQUIRE( asprintf(&senv, "LD_PRELOAD_FDS=%d", libraryfd) > 0); expect_success(binaryfd, senv); } ATF_TC_WITHOUT_HEAD(two_librarys); ATF_TC_BODY(two_librarys, tc) { char *senv; setup(tc); ATF_REQUIRE( asprintf(&senv, "LD_PRELOAD_FDS=%d:%d", libraryfd, libraryfd) > 0); expect_success(binaryfd, senv); } /* Register test cases with ATF. */ ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, missing_library); ATF_TP_ADD_TC(tp, bad_librarys); ATF_TP_ADD_TC(tp, single_library); ATF_TP_ADD_TC(tp, two_librarys); return atf_no_error(); }