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/>. */
20 #include <sys/prctl.h>
23 #include <support/namespace.h>
25 #include "tst-ttyname-common.c"
28 do_in_chroot_2 (int (*cb
)(const char *, int))
30 printf ("info: entering chroot 2\n");
37 /* Open the PTS that we'll be testing on. */
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",
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 */
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}))
67 VERIFY (mount ("tmpfs", chrootdir
, "tmpfs", 0, "mode=755") == 0);
68 VERIFY (chdir (chrootdir
) == 0);
70 xmkdir ("proc", 0755);
72 xmkdir ("dev/pts", 0755);
74 VERIFY (mount ("devpts", "dev/pts", "devpts",
76 "newinstance,ptmxmode=0666,mode=620") == 0);
77 VERIFY (symlink ("pts/ptmx", "dev/ptmx") == 0);
80 touch ("dev/console", 0);
81 VERIFY (mount (slavename
, "console", NULL
, MS_BIND
, NULL
) == 0);
85 if (unshare (CLONE_NEWNS
| CLONE_NEWPID
) < 0)
86 FAIL_UNSUPPORTED ("could not enter new PID namespace");
87 pid
= xfork (); /* inner child */
92 /* wait until the outer child has exited */
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
104 _exit (EXIT_UNSUPPORTED
);
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);
114 _exit (cb (slavename
, slave
));
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 */
126 xwaitpid (pid
, &status
, 0);
127 VERIFY (WIFEXITED (status
));
128 int ret
= WEXITSTATUS (status
);
130 FAIL_UNSUPPORTED ("unable to mount /proc on inner child process");
132 xclose (pid_pipe
[0]);
140 support_become_root ();
142 do_in_chroot_2 (run_chroot_tests
);
147 #include <support/test-driver.c>