1 /*
2 * Copyright (c) 2019 Alexey Dobriyan <[email protected]>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16 /* Test that open(O_TMPFILE), linkat() doesn't screw accounting. */
17 #include <errno.h>
18 #include <sched.h>
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <sys/mount.h>
24 #include <unistd.h>
25
26 #include "../kselftest.h"
27
main(void)28 int main(void)
29 {
30 int fd;
31
32 // Setting up kselftest framework
33 ksft_print_header();
34 ksft_set_plan(1);
35
36 // Check if test is run as root
37 if (geteuid()) {
38 ksft_exit_skip("This test needs root to run!\n");
39 return 1;
40 }
41
42 if (unshare(CLONE_NEWNS) == -1) {
43 if (errno == ENOSYS || errno == EPERM) {
44 ksft_exit_skip("unshare() error: unshare, errno %d\n", errno);
45 } else {
46 ksft_exit_fail_msg("unshare() error: unshare, errno %d\n", errno);
47 }
48 }
49
50 if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) == -1) {
51 ksft_exit_fail_msg("mount() error: Root filesystem private mount: Fail %d\n", errno);
52 }
53
54 /* Our heroes: 1 root inode, 1 O_TMPFILE inode, 1 permanent inode. */
55 if (mount(NULL, "/tmp", "tmpfs", 0, "nr_inodes=3") == -1) {
56 ksft_exit_fail_msg("mount() error: Mounting tmpfs on /tmp: Fail %d\n", errno);
57 }
58
59 fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_TMPFILE, 0600);
60 if (fd == -1) {
61 ksft_exit_fail_msg("openat() error: Open first temporary file: Fail %d\n", errno);
62 }
63
64 if (linkat(fd, "", AT_FDCWD, "/tmp/1", AT_EMPTY_PATH) == -1) {
65 ksft_exit_fail_msg("linkat() error: Linking the temporary file: Fail %d\n", errno);
66 /* Ensure fd is closed on failure */
67 close(fd);
68 }
69 close(fd);
70
71 fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_TMPFILE, 0600);
72 if (fd == -1) {
73 ksft_exit_fail_msg("openat() error: Opening the second temporary file: Fail %d\n", errno);
74 }
75 ksft_test_result_pass(" ");
76 ksft_exit_pass();
77 return 0;
78 }
79