xref: /xnu-12377.61.12/tests/vfs/openbyid_stress.c (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 /*
2  * Copyright (c) 2024 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /* compile: xcrun -sdk macosx.internal clang -ldarwintest -o openbyid_stress openbyid_stress.c -g -Weverything */
30 /* sign: codesign --force --sign - --timestamp=none --entitlements openbyid_stress.entitlements openbyid_stress */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <sys/mount.h>
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <sys/fsgetpath_private.h>
41 #include <dispatch/dispatch.h>
42 
43 #include <darwintest.h>
44 #include <darwintest/utils.h>
45 
46 #define TEST_DURATION 10 /* seconds */
47 
48 static char template[MAXPATHLEN];
49 static char *testdir = NULL;
50 static char dir1[PATH_MAX], dir2[PATH_MAX];
51 static char file1[PATH_MAX], file2[PATH_MAX];
52 
53 T_GLOBAL_META(
54 	T_META_NAMESPACE("xnu.vfs"),
55 	T_META_RADAR_COMPONENT_NAME("xnu"),
56 	T_META_RADAR_COMPONENT_VERSION("vfs"),
57 	T_META_ASROOT(false),
58 	T_META_CHECK_LEAKS(false));
59 
60 static void
cleanup(void)61 cleanup(void)
62 {
63 	if (file1[0] != '\0') {
64 		unlink(file1);
65 	}
66 	if (file2[0] != '\0') {
67 		unlink(file2);
68 	}
69 	if (dir1[0] != '\0') {
70 		rmdir(dir1);
71 	}
72 	if (dir2[0] != '\0') {
73 		rmdir(dir2);
74 	}
75 	if (testdir) {
76 		rmdir(testdir);
77 	}
78 }
79 
80 T_DECL(openbyid_stress,
81     "Test that openbyid_np does not open the wrong file")
82 {
83 	int fd;
84 	struct stat buf_stat;
85 	struct statfs buf_statfs;
86 	__block int timeout = 0;
87 	__block int error = 0;
88 	int64_t interval = TEST_DURATION * NSEC_PER_SEC;
89 	dispatch_queue_t queue;
90 	dispatch_source_t timeout_source;
91 
92 	dir1[0] = dir2[0] = '\0';
93 	file2[0] = file2[0] = '\0';
94 
95 	T_ATEND(cleanup);
96 
97 	T_SETUPBEGIN;
98 
99 	T_ASSERT_NOTNULL((queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)), "Getting global queue");
100 	T_ASSERT_NOTNULL((timeout_source =  dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue)), "Creating dispatch source");
101 
102 	dispatch_source_set_timer(timeout_source, dispatch_time(DISPATCH_TIME_NOW, interval), DISPATCH_TIME_FOREVER, 0);
103 	dispatch_source_set_event_handler(timeout_source, ^{
104 		timeout = 1;
105 		T_LOG("%d seconds timeout expired", TEST_DURATION);
106 	});
107 
108 	snprintf(template, sizeof(template), "%s/openbyid_stress-XXXXXX", dt_tmpdir());
109 	T_ASSERT_POSIX_NOTNULL((testdir = mkdtemp(template)), "Creating test root dir");
110 
111 	snprintf(dir1, sizeof(dir1), "%s/%s", testdir, "dir1");
112 	snprintf(dir2, sizeof(dir2), "%s/%s", testdir, "dir2");
113 
114 	T_ASSERT_POSIX_SUCCESS(mkdir(dir1, 0777), "Creating dir1");
115 	T_ASSERT_POSIX_SUCCESS(mkdir(dir2, 0777), "Creating dir2");
116 
117 	snprintf(file1, sizeof(file1), "%s/%s", dir1, "file");
118 	snprintf(file2, sizeof(file2), "%s/%s", dir2, "file");
119 
120 	T_ASSERT_POSIX_SUCCESS((fd = open(file1, O_CREAT | O_RDWR, 0777)), "Creating %s", file1);
121 	T_ASSERT_POSIX_SUCCESS(close(fd), "Closing %s", file1);
122 
123 	T_ASSERT_POSIX_SUCCESS((fd = open(file2, O_CREAT | O_RDWR, 0777)), "Creating %s", file2);
124 	T_ASSERT_POSIX_SUCCESS(close(fd), "Closing %s", file2);
125 
126 	T_ASSERT_POSIX_SUCCESS(stat(file1, &buf_stat), "Calling stat() on %s", file1);
127 	T_ASSERT_POSIX_SUCCESS(statfs(file1, &buf_statfs), "Calling statfs() on %s", file1);
128 
129 	T_LOG("File successfully opened: fsid {%d, %d}, inode %llu", buf_statfs.f_fsid.val[0], buf_statfs.f_fsid.val[1], buf_stat.st_ino);
130 
131 	T_SETUPEND;
132 
133 	T_LOG("Running for %d seconds", TEST_DURATION);
134 	dispatch_resume(timeout_source);
135 
136 	/* Replace between dir1 and dir2 */
137 	dispatch_async(queue, ^(void) {
138 		while (!timeout && !error) {
139 		        renamex_np(dir1, dir2, RENAME_SWAP);
140 		}
141 	});
142 
143 	/* Query openbyid_np */
144 	while (!timeout && !error) {
145 		int fd2;
146 		struct stat buf_stat2;
147 		struct statfs buf_statfs2;
148 
149 		if ((fd2 = openbyid_np(&buf_statfs.f_fsid, (fsobj_id_t *)&buf_stat.st_ino, 0)) < 0) {
150 			T_FAIL("openbyid_np() failed %d", errno);
151 			error = errno;
152 			break;
153 		}
154 
155 		if ((error = fstatfs(fd2, &buf_statfs2)) < 0) {
156 			T_FAIL("fstatfs() failed");
157 			error = errno;
158 			close(fd2);
159 			break;
160 		}
161 
162 		if ((error = fstat(fd2, &buf_stat2)) < 0) {
163 			T_FAIL("fstat() failed");
164 			error = errno;
165 			close(fd2);
166 			break;
167 		}
168 
169 		if (buf_statfs.f_fsid.val[0] != buf_statfs2.f_fsid.val[0] ||
170 		    buf_statfs.f_fsid.val[1] != buf_statfs2.f_fsid.val[1] ||
171 		    buf_stat2.st_ino != buf_stat.st_ino) {
172 			T_FAIL("Wrong file opened! fsid {%d, %d}, inode %llu", buf_statfs2.f_fsid.val[0], buf_statfs2.f_fsid.val[1], buf_stat2.st_ino);
173 			error = EINVAL;
174 			close(fd2);
175 			break;
176 		}
177 
178 		close(fd2);
179 	}
180 
181 	T_ASSERT_POSIX_ZERO(error, "Test completed without error(s)");
182 }
183