xref: /xnu-12377.41.6/tests/vfs/fmount_funmount.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
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 fmount_funmount fmount_funmount.c -g -Weverything */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <sys/mount.h>
37 #include <sys/param.h>
38 #include <sys/stat.h>
39 
40 #include <darwintest.h>
41 #include <darwintest/utils.h>
42 
43 #define RUN_TEST     TARGET_OS_OSX
44 
45 #define FSTYPE_APFS "apfs"
46 #define FSTYPE_DEVFS "devfs"
47 
48 static char template[MAXPATHLEN];
49 static char *testdir = NULL;
50 
51 T_GLOBAL_META(
52 	T_META_NAMESPACE("xnu.vfs"),
53 	T_META_RADAR_COMPONENT_NAME("xnu"),
54 	T_META_RADAR_COMPONENT_VERSION("vfs"),
55 	T_META_ASROOT(false),
56 	T_META_ENABLED(RUN_TEST),
57 	T_META_CHECK_LEAKS(false));
58 
59 static int
verify_fstypename(const char * name)60 verify_fstypename(const char *name)
61 {
62 	int error;
63 	struct statfs statfs_buf;;
64 
65 	error = statfs(testdir, &statfs_buf);
66 	if (error) {
67 		return errno;
68 	}
69 
70 	if (strncmp(name, statfs_buf.f_fstypename, MFSNAMELEN)) {
71 		return EINVAL;
72 	}
73 
74 	return 0;
75 }
76 
77 static void
cleanup(void)78 cleanup(void)
79 {
80 	if (testdir) {
81 		rmdir(testdir);
82 	}
83 }
84 
85 T_DECL(fmount_funmount,
86     "Test fmount() and funmount() system calls")
87 {
88 #if (!RUN_TEST)
89 	T_SKIP("Not macOS");
90 #endif
91 
92 	int fd;
93 
94 	T_ATEND(cleanup);
95 
96 	T_SETUPBEGIN;
97 
98 	snprintf(template, sizeof(template), "%s/fmount_funmount-XXXXXX", dt_tmpdir());
99 	T_ASSERT_POSIX_NOTNULL((testdir = mkdtemp(template)), "Creating test root dir");
100 	T_ASSERT_POSIX_ZERO(verify_fstypename(FSTYPE_APFS), "Verifing fstype name equals %s", FSTYPE_APFS);
101 
102 	T_SETUPEND;
103 
104 	/* Mount phase */
105 	T_ASSERT_POSIX_SUCCESS((fd = open(testdir, O_DIRECTORY)), "Open test root dir: %s", testdir);
106 	T_ASSERT_POSIX_SUCCESS(fmount(FSTYPE_DEVFS, fd, MNT_RDONLY, NULL), "Mounting temporary %s mount using fmount(fd = %d)", FSTYPE_DEVFS, fd);
107 	T_ASSERT_POSIX_ZERO(verify_fstypename(FSTYPE_DEVFS), "Verifing fstype name equals %s", FSTYPE_DEVFS);
108 	T_ASSERT_POSIX_SUCCESS(close(fd), "Closing (fd = %d)", fd);
109 
110 	/* Unmount phase */
111 	T_ASSERT_POSIX_SUCCESS((fd = open(testdir, O_DIRECTORY)), "Open test root dir: %s", testdir);
112 	T_ASSERT_POSIX_SUCCESS(funmount(fd, MNT_FORCE), "Unmounting %s using funmount(fd = %d)", testdir, fd);
113 	T_ASSERT_POSIX_ZERO(verify_fstypename(FSTYPE_APFS), "Verifing fstype name equals %s", FSTYPE_APFS);
114 	T_ASSERT_POSIX_SUCCESS(close(fd), "Closing (fd = %d)", fd);
115 }
116