elf/Makefile: Split and sort PIE tests
[glibc.git] / sysdeps / unix / sysv / linux / tst-ttyname-namespace.c
blob8d7a037fe4dc22d9bb47f13da56017cbb9e9bf09
1 /* Tests for ttyname/ttyname_r with namespaces.
2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <https://www.gnu.org/licenses/>. */
19 #include <sched.h>
20 #include <sys/prctl.h>
21 #include <sys/wait.h>
23 #include <support/namespace.h>
25 #include "tst-ttyname-common.c"
27 static int
28 do_in_chroot_2 (int (*cb)(const char *, int))
30 printf ("info: entering chroot 2\n");
32 int pid_pipe[2];
33 xpipe (pid_pipe);
34 int exit_pipe[2];
35 xpipe (exit_pipe);
37 /* Open the PTS that we'll be testing on. */
38 int master;
39 char *slavename;
40 VERIFY ((master = posix_openpt (O_RDWR|O_NOCTTY|O_NONBLOCK)) >= 0);
41 VERIFY ((slavename = ptsname (master)));
42 VERIFY (unlockpt (master) == 0);
43 if (strncmp (slavename, "/dev/pts/", 9) != 0)
44 FAIL_UNSUPPORTED ("slave pseudo-terminal is not under /dev/pts/: %s",
45 slavename);
46 adjust_file_limit (slavename);
47 /* wait until in a new mount ns to open the slave */
49 /* enable `wait`ing on grandchildren */
50 VERIFY (prctl (PR_SET_CHILD_SUBREAPER, 1) == 0);
52 pid_t pid = xfork (); /* outer child */
53 if (pid == 0)
55 xclose (master);
56 xclose (pid_pipe[0]);
57 xclose (exit_pipe[1]);
59 if (!support_enter_mount_namespace ())
60 FAIL_UNSUPPORTED ("could not enter new mount namespace");
62 int slave = xopen (slavename, O_RDWR, 0);
63 if (!doit (slave, "basic smoketest",
64 (struct result_r){.name=slavename, .ret=0, .err=0}))
65 _exit (1);
67 VERIFY (mount ("tmpfs", chrootdir, "tmpfs", 0, "mode=755") == 0);
68 VERIFY (chdir (chrootdir) == 0);
70 xmkdir ("proc", 0755);
71 xmkdir ("dev", 0755);
72 xmkdir ("dev/pts", 0755);
74 VERIFY (mount ("devpts", "dev/pts", "devpts",
75 MS_NOSUID|MS_NOEXEC,
76 "newinstance,ptmxmode=0666,mode=620") == 0);
77 VERIFY (symlink ("pts/ptmx", "dev/ptmx") == 0);
79 touch ("console", 0);
80 touch ("dev/console", 0);
81 VERIFY (mount (slavename, "console", NULL, MS_BIND, NULL) == 0);
83 xchroot (".");
85 if (unshare (CLONE_NEWNS | CLONE_NEWPID) < 0)
86 FAIL_UNSUPPORTED ("could not enter new PID namespace");
87 pid = xfork (); /* inner child */
88 if (pid == 0)
90 xclose (pid_pipe[1]);
92 /* wait until the outer child has exited */
93 char c;
94 VERIFY (read (exit_pipe[0], &c, 1) == 0);
95 xclose (exit_pipe[0]);
97 if (mount ("proc", "/proc", "proc",
98 MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL) != 0)
100 /* This happens if we're trying to create a nested container,
101 like if the build is running under podman, and we lack
102 priviledges. */
103 if (errno == EPERM)
104 _exit (EXIT_UNSUPPORTED);
105 else
106 _exit (EXIT_FAILURE);
109 char *linkname = xasprintf ("/proc/self/fd/%d", slave);
110 char *target = proc_fd_readlink (linkname);
111 VERIFY (strcmp (target, strrchr (slavename, '/')) == 0);
112 free (linkname);
114 _exit (cb (slavename, slave));
116 int status;
117 xwaitpid (pid, &status, 0);
118 _exit (WEXITSTATUS (status));
120 xclose (pid_pipe[1]);
121 xclose (exit_pipe[0]);
122 xclose (exit_pipe[1]);
124 /* wait for the outer child */
125 int status;
126 xwaitpid (pid, &status, 0);
127 VERIFY (WIFEXITED (status));
128 int ret = WEXITSTATUS (status);
129 if (ret != 0)
130 FAIL_UNSUPPORTED ("unable to mount /proc on inner child process");
132 xclose (pid_pipe[0]);
134 return 0;
137 static int
138 do_test (void)
140 support_become_root ();
142 do_in_chroot_2 (run_chroot_tests);
144 return 0;
147 #include <support/test-driver.c>