1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions * Copyright (c) 2025 Apple Computer, Inc. All rights reserved.
3*043036a2SApple OSS Distributions *
4*043036a2SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions *
6*043036a2SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions *
15*043036a2SApple OSS Distributions * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions *
18*043036a2SApple OSS Distributions * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions * limitations under the License.
25*043036a2SApple OSS Distributions *
26*043036a2SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions */
28*043036a2SApple OSS Distributions
29*043036a2SApple OSS Distributions /* compile: xcrun -sdk macosx.internal clang -ldarwintest -o devfs_nested_dir devfs_nested_dirs.c -g -Weverything */
30*043036a2SApple OSS Distributions
31*043036a2SApple OSS Distributions #include <stdio.h>
32*043036a2SApple OSS Distributions #include <stdlib.h>
33*043036a2SApple OSS Distributions #include <fcntl.h>
34*043036a2SApple OSS Distributions #include <unistd.h>
35*043036a2SApple OSS Distributions #include <errno.h>
36*043036a2SApple OSS Distributions #include <sys/mount.h>
37*043036a2SApple OSS Distributions #include <sys/param.h>
38*043036a2SApple OSS Distributions #include <sys/stat.h>
39*043036a2SApple OSS Distributions #include <darwintest.h>
40*043036a2SApple OSS Distributions #include <darwintest/utils.h>
41*043036a2SApple OSS Distributions
42*043036a2SApple OSS Distributions T_GLOBAL_META(
43*043036a2SApple OSS Distributions T_META_NAMESPACE("xnu.vfs"),
44*043036a2SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
45*043036a2SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("vfs"),
46*043036a2SApple OSS Distributions T_META_ASROOT(false),
47*043036a2SApple OSS Distributions T_META_ENABLED(TARGET_OS_OSX),
48*043036a2SApple OSS Distributions T_META_CHECK_LEAKS(false));
49*043036a2SApple OSS Distributions
50*043036a2SApple OSS Distributions
51*043036a2SApple OSS Distributions #define MAX_NESTED_SUBDIRS 16
52*043036a2SApple OSS Distributions
53*043036a2SApple OSS Distributions static char template[MAXPATHLEN];
54*043036a2SApple OSS Distributions static char *mount_path;
55*043036a2SApple OSS Distributions
56*043036a2SApple OSS Distributions static void
cleanup(void)57*043036a2SApple OSS Distributions cleanup(void)
58*043036a2SApple OSS Distributions {
59*043036a2SApple OSS Distributions if (mount_path) {
60*043036a2SApple OSS Distributions unmount(mount_path, MNT_FORCE);
61*043036a2SApple OSS Distributions rmdir(mount_path);
62*043036a2SApple OSS Distributions }
63*043036a2SApple OSS Distributions }
64*043036a2SApple OSS Distributions
65*043036a2SApple OSS Distributions /* Create n-level nested subdirs and Return the last parent dir's fd. */
66*043036a2SApple OSS Distributions static int
create_nested_subdirs(char * dir,int levels)67*043036a2SApple OSS Distributions create_nested_subdirs(char *dir, int levels)
68*043036a2SApple OSS Distributions {
69*043036a2SApple OSS Distributions int parentdirfd = -1;
70*043036a2SApple OSS Distributions
71*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS((parentdirfd = open(mount_path, O_SEARCH, 0777)),
72*043036a2SApple OSS Distributions "Opening mount root diretory");
73*043036a2SApple OSS Distributions
74*043036a2SApple OSS Distributions for (int i = 0; i <= levels; i++) {
75*043036a2SApple OSS Distributions int dirfd;
76*043036a2SApple OSS Distributions
77*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(mkdirat(parentdirfd, dir, 0777),
78*043036a2SApple OSS Distributions "Verifying that creating subdir at level %d should pass", i);
79*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS((dirfd = openat(parentdirfd, dir, O_SEARCH)),
80*043036a2SApple OSS Distributions "Verifying that opening subdir at level %d should pass", i);
81*043036a2SApple OSS Distributions
82*043036a2SApple OSS Distributions close(parentdirfd);
83*043036a2SApple OSS Distributions parentdirfd = dirfd;
84*043036a2SApple OSS Distributions }
85*043036a2SApple OSS Distributions
86*043036a2SApple OSS Distributions return parentdirfd;
87*043036a2SApple OSS Distributions }
88*043036a2SApple OSS Distributions
89*043036a2SApple OSS Distributions T_DECL(nested_subdirs_limit,
90*043036a2SApple OSS Distributions "Test creating and renaming nested subdirs in devfs mount",
91*043036a2SApple OSS Distributions T_META_ENABLED(true))
92*043036a2SApple OSS Distributions {
93*043036a2SApple OSS Distributions char todirpath[PATH_MAX];
94*043036a2SApple OSS Distributions int lastdirfd, rootdirfd, todirfd;
95*043036a2SApple OSS Distributions
96*043036a2SApple OSS Distributions #if (!TARGET_OS_OSX)
97*043036a2SApple OSS Distributions T_SKIP("Not macOS");
98*043036a2SApple OSS Distributions #endif
99*043036a2SApple OSS Distributions
100*043036a2SApple OSS Distributions T_ATEND(cleanup);
101*043036a2SApple OSS Distributions
102*043036a2SApple OSS Distributions T_SETUPBEGIN;
103*043036a2SApple OSS Distributions
104*043036a2SApple OSS Distributions snprintf(template, sizeof(template), "%s/devfs", dt_tmpdir());
105*043036a2SApple OSS Distributions T_ASSERT_POSIX_NOTNULL((mount_path = mkdtemp(template)),
106*043036a2SApple OSS Distributions "Creating mount path %s", template);
107*043036a2SApple OSS Distributions
108*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(mount("devfs", mount_path, MNT_IGNORE_OWNERSHIP, NULL),
109*043036a2SApple OSS Distributions "Mounting devfs mount using path %s", mount_path);
110*043036a2SApple OSS Distributions
111*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(chmod(mount_path, 0777),
112*043036a2SApple OSS Distributions "Changing permission on root diretory");
113*043036a2SApple OSS Distributions
114*043036a2SApple OSS Distributions T_SETUPEND;
115*043036a2SApple OSS Distributions
116*043036a2SApple OSS Distributions lastdirfd = create_nested_subdirs("aaa", MAX_NESTED_SUBDIRS);
117*043036a2SApple OSS Distributions T_ASSERT_NE_INT(lastdirfd, -1, "create_nested_subdirs(aaa, 16)");
118*043036a2SApple OSS Distributions
119*043036a2SApple OSS Distributions T_ASSERT_POSIX_FAILURE(mkdirat(lastdirfd, "aaa", 0777), EMLINK,
120*043036a2SApple OSS Distributions "Verifying that creating subdir at max level (>16) should fail");
121*043036a2SApple OSS Distributions close(lastdirfd);
122*043036a2SApple OSS Distributions
123*043036a2SApple OSS Distributions /* Create a nested subdirs of 8 levels deep.*/
124*043036a2SApple OSS Distributions lastdirfd = create_nested_subdirs("bbb", 8);
125*043036a2SApple OSS Distributions T_ASSERT_NE_INT(lastdirfd, -1, "create_nested_subdirs(bbb, 8)");
126*043036a2SApple OSS Distributions close(lastdirfd);
127*043036a2SApple OSS Distributions
128*043036a2SApple OSS Distributions /* Create a nested subdirs of 10 levels deep.*/
129*043036a2SApple OSS Distributions lastdirfd = create_nested_subdirs("ccc", 10);
130*043036a2SApple OSS Distributions T_ASSERT_NE_INT(lastdirfd, -1, "create_nested_subdirs(ccc, 10)");
131*043036a2SApple OSS Distributions close(lastdirfd);
132*043036a2SApple OSS Distributions
133*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS((rootdirfd = open(mount_path, O_SEARCH, 0777)),
134*043036a2SApple OSS Distributions "Opening mount root diretory");
135*043036a2SApple OSS Distributions
136*043036a2SApple OSS Distributions snprintf(todirpath, sizeof(todirpath),
137*043036a2SApple OSS Distributions "%s/bbb/bbb/bbb/bbb/bbb/bbb/bbb/bbb/bbb", mount_path);
138*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS((todirfd = open(todirpath, O_SEARCH, 0777)),
139*043036a2SApple OSS Distributions "Opening target diretory");
140*043036a2SApple OSS Distributions
141*043036a2SApple OSS Distributions /*
142*043036a2SApple OSS Distributions * Verify that rename should fail if the deepest subdir (after rename)
143*043036a2SApple OSS Distributions * would exceed the max nested subdirs limit.
144*043036a2SApple OSS Distributions */
145*043036a2SApple OSS Distributions T_ASSERT_POSIX_FAILURE(renameat(rootdirfd, "ccc", todirfd, "ccc"), EMLINK,
146*043036a2SApple OSS Distributions "Verifying that rename a dir with deep nested subdirs that exceed limit should fail");
147*043036a2SApple OSS Distributions
148*043036a2SApple OSS Distributions /* Create a nested subdirs of 4 levels deep.*/
149*043036a2SApple OSS Distributions lastdirfd = create_nested_subdirs("ddd", 4);
150*043036a2SApple OSS Distributions T_ASSERT_NE_INT(lastdirfd, -1, "create_nested_subdirs(ddd, 4)");
151*043036a2SApple OSS Distributions close(lastdirfd);
152*043036a2SApple OSS Distributions
153*043036a2SApple OSS Distributions /*
154*043036a2SApple OSS Distributions * Verify that rename should succeed if the deepest subdir (after rename)
155*043036a2SApple OSS Distributions * doesn't exceed the max nested subdirs limit.
156*043036a2SApple OSS Distributions */
157*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(renameat(rootdirfd, "ddd", todirfd, "ddd"),
158*043036a2SApple OSS Distributions "Verifying that rename a dir with nested subdirs that doesn't exceed limit should succeed");
159*043036a2SApple OSS Distributions
160*043036a2SApple OSS Distributions close(todirfd);
161*043036a2SApple OSS Distributions close(rootdirfd);
162*043036a2SApple OSS Distributions }
163