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.
4
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.
9
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.
14
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/>. */
18
19#include <sched.h>
20#include <sys/prctl.h>
21#include <sys/wait.h>
22
23#include <support/namespace.h>
24
25#include "tst-ttyname-common.c"
26
27static int
28do_in_chroot_2 (int (*cb)(const char *, int))
29{
30 printf (format: "info: entering chroot 2\n");
31
32 int pid_pipe[2];
33 xpipe (pid_pipe);
34 int exit_pipe[2];
35 xpipe (exit_pipe);
36
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 (pty: slavename);
47 /* wait until in a new mount ns to open the slave */
48
49 /* enable `wait`ing on grandchildren */
50 VERIFY (prctl (PR_SET_CHILD_SUBREAPER, 1) == 0);
51
52 pid_t pid = xfork (); /* outer child */
53 if (pid == 0)
54 {
55 xclose (master);
56 xclose (pid_pipe[0]);
57 xclose (exit_pipe[1]);
58
59 if (!support_enter_mount_namespace ())
60 FAIL_UNSUPPORTED ("could not enter new mount namespace");
61
62 int slave = xopen (path: slavename, O_RDWR, 0);
63 if (!doit (fd: slave, testname: "basic smoketest",
64 expected_r: (struct result_r){.name=slavename, .ret=0, .err=0}))
65 _exit (1);
66
67 VERIFY (mount ("tmpfs", chrootdir, "tmpfs", 0, "mode=755") == 0);
68 VERIFY (chdir (chrootdir) == 0);
69
70 xmkdir (path: "proc", 0755);
71 xmkdir (path: "dev", 0755);
72 xmkdir (path: "dev/pts", 0755);
73
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);
78
79 touch (path: "console", mode: 0);
80 touch (path: "dev/console", mode: 0);
81 VERIFY (mount (slavename, "console", NULL, MS_BIND, NULL) == 0);
82
83 xchroot (path: ".");
84
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)
89 {
90 xclose (pid_pipe[1]);
91
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]);
96
97 if (mount (special_file: "proc", dir: "/proc", fstype: "proc",
98 MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL) != 0)
99 {
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);
107 }
108
109 char *linkname = xasprintf (format: "/proc/self/fd/%d", slave);
110 char *target = proc_fd_readlink (linkname);
111 VERIFY (strcmp (target, strrchr (slavename, '/')) == 0);
112 free (ptr: linkname);
113
114 _exit (cb (slavename, slave));
115 }
116 int status;
117 xwaitpid (pid, status: &status, flags: 0);
118 _exit (WEXITSTATUS (status));
119 }
120 xclose (pid_pipe[1]);
121 xclose (exit_pipe[0]);
122 xclose (exit_pipe[1]);
123
124 /* wait for the outer child */
125 int status;
126 xwaitpid (pid, status: &status, flags: 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");
131
132 xclose (pid_pipe[0]);
133
134 return 0;
135}
136
137static int
138do_test (void)
139{
140 support_become_root ();
141
142 do_in_chroot_2 (cb: run_chroot_tests);
143
144 return 0;
145}
146
147#include <support/test-driver.c>
148

source code of glibc/sysdeps/unix/sysv/linux/tst-ttyname-namespace.c