1 /* $NetBSD: t_fcntl.c,v 1.4 2023/08/05 08:05:16 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2019 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 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 #include <sys/param.h> 33 #include <sys/types.h> 34 #include <sys/mman.h> 35 #include <atf-c.h> 36 #include <fcntl.h> 37 #include <errno.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 42 ATF_TC(getpath_vnode); 43 ATF_TC_HEAD(getpath_vnode, tc) 44 { 45 46 atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) F_GETPATH for vnodes"); 47 } 48 49 static const struct { 50 const char *name; 51 int rv; 52 } files[] = { 53 { "/bin/ls", 0 }, 54 { "/bin/sh", 0 }, 55 { "/dev/zero", 0 }, 56 { "/dev/null", 0 }, 57 { "/sbin/chown", 0 }, 58 { "/", ENOENT }, 59 }; 60 61 ATF_TC_BODY(getpath_vnode, tc) 62 { 63 char path[MAXPATHLEN]; 64 int fd, rv; 65 66 for (size_t i = 0; i < __arraycount(files); i++) { 67 fd = open(files[i].name, O_RDONLY|O_NOFOLLOW); 68 ATF_REQUIRE_MSG(fd != -1, "Cannot open `%s'", files[i].name); 69 rv = fcntl(fd, F_GETPATH, path); 70 if (files[i].rv) { 71 ATF_REQUIRE_MSG(errno == files[i].rv, 72 "Unexpected error %d != %d for `%s'", errno, 73 files[i].rv, files[i].name); 74 } else { 75 ATF_REQUIRE_MSG(rv != -1, 76 "Can't get path for `%s' (%s)", files[i].name, 77 strerror(errno)); 78 ATF_REQUIRE_MSG(strcmp(files[i].name, path) == 0, 79 "Bad name `%s' != `%s'", path, files[i].name); 80 close(fd); 81 } 82 } 83 } 84 85 ATF_TC(getpath_memfd); 86 ATF_TC_HEAD(getpath_memfd, tc) 87 { 88 89 atf_tc_set_md_var(tc, "descr", 90 "Checks fcntl(2) F_GETPATH for fds created by memfd_create"); 91 } 92 93 #define MEMFD_NAME(name) { name, "memfd:" name } 94 static const struct { 95 const char *bare; 96 const char *prefixed; 97 } memfd_names[] = { 98 MEMFD_NAME(""), 99 MEMFD_NAME("some text"), 100 MEMFD_NAME("memfd:"), 101 MEMFD_NAME("../\\"), 102 }; 103 104 ATF_TC_BODY(getpath_memfd, tc) 105 { 106 char path[MAXPATHLEN]; 107 int fd, rv; 108 109 for (size_t i = 0; i < __arraycount(memfd_names); i++) { 110 fd = memfd_create(memfd_names[i].bare, 0); 111 ATF_CHECK_MSG(fd != -1, "Failed to create memfd (%s)", 112 strerror(errno)); 113 if (fd == -1) 114 continue; 115 rv = fcntl(fd, F_GETPATH, path); 116 ATF_CHECK_MSG(rv != -1, "Can't get path `%s' (%s)", 117 memfd_names[i].bare, strerror(errno)); 118 if (rv == -1) 119 goto next; 120 ATF_CHECK_MSG(strcmp(memfd_names[i].prefixed, path) == 0, 121 "Bad name `%s' != `%s'", path, memfd_names[i].prefixed); 122 next: close(fd); 123 } 124 } 125 126 ATF_TP_ADD_TCS(tp) 127 { 128 ATF_TP_ADD_TC(tp, getpath_vnode); 129 ATF_TP_ADD_TC(tp, getpath_memfd); 130 131 return atf_no_error(); 132 } 133