xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/tests/functional/tmpfile/tmpfile_stat_mode.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2019 by Tomohiro Kusumi. All rights reserved.
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <err.h>
33 
34 /* backward compat in case it's not defined */
35 #ifndef O_TMPFILE
36 #define	O_TMPFILE	(020000000|O_DIRECTORY)
37 #endif
38 
39 /*
40  * DESCRIPTION:
41  *	Verify fstat(2) for O_TMPFILE file considers umask.
42  *
43  * STRATEGY:
44  *	1. open(2) with O_TMPFILE.
45  *	2. linkat(2).
46  *	3. fstat(2) and verify .st_mode value.
47  */
48 
49 static void
test_stat_mode(mode_t mask)50 test_stat_mode(mode_t mask)
51 {
52 	struct stat fst;
53 	int i, fd;
54 	char spath[1024], dpath[1024];
55 	const char *penv[] = {"TESTDIR", "TESTFILE0"};
56 	mode_t masked = 0777 & ~mask;
57 	mode_t mode;
58 
59 	/*
60 	 * Get the environment variable values.
61 	 */
62 	for (i = 0; i < ARRAY_SIZE(penv); i++)
63 		if ((penv[i] = getenv(penv[i])) == NULL)
64 			errx(1, "getenv(penv[%d])", i);
65 
66 	umask(mask);
67 	fd = open(penv[0], O_RDWR|O_TMPFILE, 0777);
68 	if (fd == -1)
69 		err(2, "open(%s)", penv[0]);
70 
71 	if (fstat(fd, &fst) == -1)
72 		err(3, "fstat(%s)", penv[0]);
73 
74 	snprintf(spath, sizeof (spath), "/proc/self/fd/%d", fd);
75 	snprintf(dpath, sizeof (dpath), "%s/%s", penv[0], penv[1]);
76 
77 	unlink(dpath);
78 	if (linkat(AT_FDCWD, spath, AT_FDCWD, dpath, AT_SYMLINK_FOLLOW) == -1)
79 		err(4, "linkat");
80 	close(fd);
81 
82 	/* Verify fstat(2) result at old path */
83 	mode = fst.st_mode & 0777;
84 	if (mode != masked)
85 		errx(5, "fstat(2) %o != %o\n", mode, masked);
86 
87 	fd = open(dpath, O_RDWR);
88 	if (fd == -1)
89 		err(6, "open(%s)", dpath);
90 
91 	if (fstat(fd, &fst) == -1)
92 		err(7, "fstat(%s)", dpath);
93 
94 	/* Verify fstat(2) result at new path */
95 	mode = fst.st_mode & 0777;
96 	if (mode != masked)
97 		errx(8, "fstat(2) %o != %o\n", mode, masked);
98 	close(fd);
99 }
100 
101 int
main(void)102 main(void)
103 {
104 	fprintf(stdout, "Verify stat(2) for O_TMPFILE file considers umask.\n");
105 
106 	test_stat_mode(0022);
107 	test_stat_mode(0077);
108 
109 	return (0);
110 }
111