math: Remove bogus math implementations
[glibc.git] / posix / tst-spawn6.c
blobbf70cf2b9ac746292bd3e9bb73ef98cffd717c58
1 /* Check posix_spawn set controlling terminal extension.
2 Copyright (C) 2022-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
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <array_length.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <intprops.h>
24 #include <paths.h>
25 #include <spawn.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <support/check.h>
30 #include <support/xunistd.h>
31 #include <sys/wait.h>
32 #include <sys/ioctl.h>
33 #include <stdlib.h>
34 #include <termios.h>
35 #include <tst-spawn.h>
37 #ifndef PATH_MAX
38 # define PATH_MAX 1024
39 #endif
40 static char ptmxpath[PATH_MAX];
42 static int
43 handle_restart (const char *argv1, const char *argv2)
45 /* If process group is not changed (POSIX_SPAWN_SETPGROUP), then check
46 the creating process one, otherwise check against the process group
47 itself. */
48 pid_t pgrp;
49 if (strcmp (argv1, "setgrpr") != 0)
50 TEST_COMPARE (sscanf (argv1, "%d", &pgrp), 1);
51 else
53 pgrp = getpgrp ();
54 /* Check if a new process group was actually created. */
55 pid_t ppid = getppid ();
56 pid_t pgid = getpgid (ppid);
57 TEST_VERIFY (pgid != pgrp);
60 char *endptr;
61 long int tcfd = strtol (argv2, &endptr, 10);
62 if (*endptr != '\0' || tcfd > INT_MAX)
63 FAIL_EXIT1 ("invalid file descriptor name: %s", argv2);
64 if (tcfd != -1)
66 TEST_COMPARE (fcntl (tcfd, F_GETFD), -1);
67 TEST_COMPARE (errno, EBADF);
70 int fd = xopen (_PATH_TTY, O_RDONLY, 0600);
71 TEST_COMPARE (tcgetpgrp (fd), pgrp);
72 xclose (fd);
74 return 0;
77 static int restart;
78 #define CMDLINE_OPTIONS \
79 { "restart", no_argument, &restart, 1 },
81 static void
82 run_subprogram (int argc, char *argv[], const posix_spawnattr_t *attr,
83 const posix_spawn_file_actions_t *actions, int tcfd,
84 int exp_err)
86 short int flags;
87 TEST_COMPARE (posix_spawnattr_getflags (attr, &flags), 0);
88 bool setpgrp = flags & POSIX_SPAWN_SETPGROUP;
90 char *spargv[9];
91 TEST_VERIFY_EXIT (((argc - 1) + 4) < array_length (spargv));
92 char pgrp[INT_STRLEN_BOUND (pid_t)];
93 char tcfdstr[INT_STRLEN_BOUND (int)];
95 int i = 0;
96 for (; i < argc - 1; i++)
97 spargv[i] = argv[i + 1];
98 spargv[i++] = (char *) "--direct";
99 spargv[i++] = (char *) "--restart";
100 if (setpgrp)
101 spargv[i++] = (char *) "setgrpr";
102 else
104 snprintf (pgrp, sizeof pgrp, "%d", getpgrp ());
105 spargv[i++] = pgrp;
107 snprintf (tcfdstr, sizeof tcfdstr, "%d", tcfd);
108 spargv[i++] = tcfdstr;
109 spargv[i] = NULL;
111 pid_t pid;
112 TEST_COMPARE (POSIX_SPAWN (&pid, argv[1], actions, attr, spargv, environ),
113 exp_err);
114 if (exp_err != 0)
115 return;
117 siginfo_t sinfo;
118 TEST_COMPARE (WAITID (P_ALL, 0, &sinfo, WEXITED), 0);
119 TEST_COMPARE (sinfo.si_code, CLD_EXITED);
120 TEST_COMPARE (sinfo.si_status, 0);
123 static int
124 run_test (int argc, char *argv[])
126 /* We must have either:
127 - four parameters left if called initially:
128 + path to ld.so optional
129 + "--library-path" optional
130 + the library path optional
131 + the application name
132 - six parameters left if called through re-execution:
133 + --setgrpr optional
136 int tcfd = xopen (ptmxpath, O_RDONLY, 0600);
138 /* Check setting the controlling terminal without changing the group. */
140 posix_spawnattr_t attr;
141 TEST_COMPARE (posix_spawnattr_init (&attr), 0);
142 posix_spawn_file_actions_t actions;
143 TEST_COMPARE (posix_spawn_file_actions_init (&actions), 0);
144 TEST_COMPARE (posix_spawn_file_actions_addtcsetpgrp_np (&actions, tcfd),
147 run_subprogram (argc, argv, &attr, &actions, -1, 0);
150 /* Check setting both the controlling terminal and the create a new process
151 group. */
153 posix_spawnattr_t attr;
154 TEST_COMPARE (posix_spawnattr_init (&attr), 0);
155 TEST_COMPARE (posix_spawnattr_setflags (&attr, POSIX_SPAWN_SETPGROUP), 0);
156 posix_spawn_file_actions_t actions;
157 TEST_COMPARE (posix_spawn_file_actions_init (&actions), 0);
158 TEST_COMPARE (posix_spawn_file_actions_addtcsetpgrp_np (&actions, tcfd),
161 run_subprogram (argc, argv, &attr, &actions, -1, 0);
164 /* Same as before, but check if the addclose file actions closes the terminal
165 file descriptor. */
167 posix_spawnattr_t attr;
168 TEST_COMPARE (posix_spawnattr_init (&attr), 0);
169 TEST_COMPARE (posix_spawnattr_setflags (&attr, POSIX_SPAWN_SETPGROUP), 0);
170 posix_spawn_file_actions_t actions;
171 TEST_COMPARE (posix_spawn_file_actions_init (&actions), 0);
172 TEST_COMPARE (posix_spawn_file_actions_addtcsetpgrp_np (&actions, tcfd),
174 TEST_COMPARE (posix_spawn_file_actions_addclose (&actions, tcfd), 0);
176 run_subprogram (argc, argv, &attr, &actions, tcfd, 0);
179 /* Trying to set the controlling terminal after a setsid incurs in a ENOTTY
180 from tcsetpgrp. */
182 posix_spawnattr_t attr;
183 TEST_COMPARE (posix_spawnattr_init (&attr), 0);
184 TEST_COMPARE (posix_spawnattr_setflags (&attr, POSIX_SPAWN_SETSID), 0);
185 posix_spawn_file_actions_t actions;
186 TEST_COMPARE (posix_spawn_file_actions_init (&actions), 0);
187 TEST_COMPARE (posix_spawn_file_actions_addtcsetpgrp_np (&actions, tcfd),
190 run_subprogram (argc, argv, &attr, &actions, -1, ENOTTY);
193 xclose (tcfd);
195 return 0;
198 static int
199 do_test (int argc, char *argv[])
201 if (restart)
202 return handle_restart (argv[1], argv[2]);
204 pid_t pid = xfork ();
205 if (pid == 0)
207 /* Create a pseudo-terminal to avoid interfering with the one using by
208 test itself, creates a new session (so there is no controlling
209 terminal), and set the pseudo-terminal as the controlling one. */
210 int ptmx = posix_openpt (0);
211 if (ptmx == -1)
213 if (errno == ENXIO)
214 FAIL_UNSUPPORTED ("terminal not available, skipping test");
215 FAIL_EXIT1 ("posix_openpt (0): %m");
217 TEST_VERIFY_EXIT (grantpt (ptmx) == 0);
218 TEST_VERIFY_EXIT (unlockpt (ptmx) == 0);
220 TEST_VERIFY_EXIT (setsid () != -1);
221 TEST_VERIFY_EXIT (ioctl (ptmx, TIOCSCTTY, NULL) == 0);
222 while (dup2 (ptmx, STDIN_FILENO) == -1 && errno == EBUSY)
224 while (dup2 (ptmx, STDOUT_FILENO) == -1 && errno == EBUSY)
226 while (dup2 (ptmx, STDERR_FILENO) == -1 && errno == EBUSY)
228 TEST_VERIFY_EXIT (ptsname_r (ptmx, ptmxpath, sizeof ptmxpath) == 0);
229 xclose (ptmx);
231 run_test (argc, argv);
232 _exit (0);
234 int status;
235 xwaitpid (pid, &status, 0);
236 TEST_VERIFY (WIFEXITED (status));
237 exit (0);
240 #define TEST_FUNCTION_ARGV do_test
241 #include <support/test-driver.c>