~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/core/unshare_test.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 
  3 #define _GNU_SOURCE
  4 #include <errno.h>
  5 #include <fcntl.h>
  6 #include <linux/kernel.h>
  7 #include <limits.h>
  8 #include <stdbool.h>
  9 #include <stdio.h>
 10 #include <stdlib.h>
 11 #include <string.h>
 12 #include <syscall.h>
 13 #include <unistd.h>
 14 #include <sys/resource.h>
 15 #include <linux/close_range.h>
 16 
 17 #include "../kselftest_harness.h"
 18 #include "../clone3/clone3_selftests.h"
 19 
 20 TEST(unshare_EMFILE)
 21 {
 22         pid_t pid;
 23         int status;
 24         struct __clone_args args = {
 25                 .flags = CLONE_FILES,
 26                 .exit_signal = SIGCHLD,
 27         };
 28         int fd;
 29         ssize_t n, n2;
 30         static char buf[512], buf2[512];
 31         struct rlimit rlimit;
 32         int nr_open;
 33 
 34         fd = open("/proc/sys/fs/nr_open", O_RDWR);
 35         ASSERT_GE(fd, 0);
 36 
 37         n = read(fd, buf, sizeof(buf));
 38         ASSERT_GT(n, 0);
 39         ASSERT_EQ(buf[n - 1], '\n');
 40 
 41         ASSERT_EQ(sscanf(buf, "%d", &nr_open), 1);
 42 
 43         ASSERT_EQ(0, getrlimit(RLIMIT_NOFILE, &rlimit));
 44 
 45         /* bump fs.nr_open */
 46         n2 = sprintf(buf2, "%d\n", nr_open + 1024);
 47         lseek(fd, 0, SEEK_SET);
 48         write(fd, buf2, n2);
 49 
 50         /* bump ulimit -n */
 51         rlimit.rlim_cur = nr_open + 1024;
 52         rlimit.rlim_max = nr_open + 1024;
 53         EXPECT_EQ(0, setrlimit(RLIMIT_NOFILE, &rlimit)) {
 54                 lseek(fd, 0, SEEK_SET);
 55                 write(fd, buf, n);
 56                 exit(EXIT_FAILURE);
 57         }
 58 
 59         /* get a descriptor past the old fs.nr_open */
 60         EXPECT_GE(dup2(2, nr_open + 64), 0) {
 61                 lseek(fd, 0, SEEK_SET);
 62                 write(fd, buf, n);
 63                 exit(EXIT_FAILURE);
 64         }
 65 
 66         /* get descriptor table shared */
 67         pid = sys_clone3(&args, sizeof(args));
 68         EXPECT_GE(pid, 0) {
 69                 lseek(fd, 0, SEEK_SET);
 70                 write(fd, buf, n);
 71                 exit(EXIT_FAILURE);
 72         }
 73 
 74         if (pid == 0) {
 75                 int err;
 76 
 77                 /* restore fs.nr_open */
 78                 lseek(fd, 0, SEEK_SET);
 79                 write(fd, buf, n);
 80                 /* ... and now unshare(CLONE_FILES) must fail with EMFILE */
 81                 err = unshare(CLONE_FILES);
 82                 EXPECT_EQ(err, -1)
 83                         exit(EXIT_FAILURE);
 84                 EXPECT_EQ(errno, EMFILE)
 85                         exit(EXIT_FAILURE);
 86                 exit(EXIT_SUCCESS);
 87         }
 88 
 89         EXPECT_EQ(waitpid(pid, &status, 0), pid);
 90         EXPECT_EQ(true, WIFEXITED(status));
 91         EXPECT_EQ(0, WEXITSTATUS(status));
 92 }
 93 
 94 TEST_HARNESS_MAIN
 95 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php