t_fcntl.c revision 1.3 1 1.3 christos /* $NetBSD: t_fcntl.c,v 1.3 2023/07/29 12:16:34 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos #include <sys/param.h>
33 1.1 christos #include <sys/types.h>
34 1.3 christos #include <sys/mman.h>
35 1.1 christos #include <atf-c.h>
36 1.1 christos #include <fcntl.h>
37 1.1 christos #include <errno.h>
38 1.1 christos #include <stdlib.h>
39 1.1 christos #include <string.h>
40 1.1 christos #include <unistd.h>
41 1.1 christos
42 1.3 christos ATF_TC(getpath_vnode);
43 1.3 christos ATF_TC_HEAD(getpath_vnode, tc)
44 1.1 christos {
45 1.1 christos
46 1.3 christos atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) F_GETPATH for vnodes");
47 1.1 christos }
48 1.1 christos
49 1.1 christos static const struct {
50 1.1 christos const char *name;
51 1.1 christos int rv;
52 1.1 christos } files[] = {
53 1.1 christos { "/bin/ls", 0 },
54 1.1 christos { "/bin/sh", 0 },
55 1.1 christos { "/dev/zero", 0 },
56 1.1 christos { "/dev/null", 0 },
57 1.2 christos { "/sbin/chown", 0 },
58 1.1 christos { "/", ENOENT },
59 1.1 christos };
60 1.1 christos
61 1.3 christos ATF_TC_BODY(getpath_vnode, tc)
62 1.1 christos {
63 1.1 christos char path[MAXPATHLEN];
64 1.1 christos int fd, rv;
65 1.1 christos
66 1.1 christos for (size_t i = 0; i < __arraycount(files); i++) {
67 1.1 christos fd = open(files[i].name, O_RDONLY|O_NOFOLLOW);
68 1.2 christos ATF_REQUIRE_MSG(fd != -1, "Cannot open `%s'", files[i].name);
69 1.1 christos rv = fcntl(fd, F_GETPATH, path);
70 1.1 christos if (files[i].rv) {
71 1.1 christos ATF_REQUIRE_MSG(errno == files[i].rv,
72 1.1 christos "Unexpected error %d != %d for `%s'", errno,
73 1.1 christos files[i].rv, files[i].name);
74 1.1 christos } else {
75 1.1 christos ATF_REQUIRE_MSG(rv != -1,
76 1.1 christos "Can't get path for `%s' (%s)", files[i].name,
77 1.1 christos strerror(errno));
78 1.1 christos ATF_REQUIRE_MSG(strcmp(files[i].name, path) == 0,
79 1.1 christos "Bad name `%s' != `%s'", path, files[i].name);
80 1.1 christos close(fd);
81 1.1 christos }
82 1.1 christos }
83 1.1 christos }
84 1.1 christos
85 1.3 christos ATF_TC(getpath_memfd);
86 1.3 christos ATF_TC_HEAD(getpath_memfd, tc)
87 1.3 christos {
88 1.3 christos
89 1.3 christos atf_tc_set_md_var(tc, "descr",
90 1.3 christos "Checks fcntl(2) F_GETPATH for fds created by memfd_create");
91 1.3 christos }
92 1.3 christos
93 1.3 christos #define MEMFD_NAME(name) { name, "memfd:" name }
94 1.3 christos static const struct {
95 1.3 christos const char *bare;
96 1.3 christos const char *prefixed;
97 1.3 christos } memfd_names[] = {
98 1.3 christos MEMFD_NAME(""),
99 1.3 christos MEMFD_NAME("some text"),
100 1.3 christos MEMFD_NAME("memfd:"),
101 1.3 christos MEMFD_NAME("../\\"),
102 1.3 christos };
103 1.3 christos
104 1.3 christos ATF_TC_BODY(getpath_memfd, tc)
105 1.3 christos {
106 1.3 christos char path[MAXPATHLEN];
107 1.3 christos int fd, rv;
108 1.3 christos
109 1.3 christos for (size_t i = 0; i < __arraycount(memfd_names); i++) {
110 1.3 christos fd = memfd_create(memfd_names[i].bare, 0);
111 1.3 christos ATF_REQUIRE_MSG(fd != -1, "Failed to create memfd (%s)",
112 1.3 christos strerror(errno));
113 1.3 christos rv = fcntl(fd, F_GETPATH, path);
114 1.3 christos ATF_REQUIRE_MSG(rv != -1, "Can't get path `%s' (%s)",
115 1.3 christos memfd_names[i].bare, strerror(errno));
116 1.3 christos ATF_REQUIRE_MSG(strcmp(memfd_names[i].prefixed, path) == 0,
117 1.3 christos "Bad name `%s' != `%s'", path, memfd_names[i].prefixed);
118 1.3 christos close(fd);
119 1.3 christos }
120 1.3 christos }
121 1.3 christos
122 1.1 christos ATF_TP_ADD_TCS(tp)
123 1.1 christos {
124 1.3 christos ATF_TP_ADD_TC(tp, getpath_vnode);
125 1.3 christos ATF_TP_ADD_TC(tp, getpath_memfd);
126 1.1 christos
127 1.1 christos return atf_no_error();
128 1.1 christos }
129