1 /* Check posix_spawn set controlling terminal extension.
2 Copyright (C) 2022-2023 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>
29 #include <support/check.h>
30 #include <support/xunistd.h>
32 #include <sys/ioctl.h>
35 #include <tst-spawn.h>
38 # define PATH_MAX 1024
40 static char ptmxpath
[PATH_MAX
];
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
49 if (strcmp (argv1
, "setgrpr") != 0)
50 TEST_COMPARE (sscanf (argv1
, "%d", &pgrp
), 1);
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
);
61 long int tcfd
= strtol (argv2
, &endptr
, 10);
62 if (*endptr
!= '\0' || tcfd
> INT_MAX
)
63 FAIL_EXIT1 ("invalid file descriptor name: %s", argv2
);
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
);
78 #define CMDLINE_OPTIONS \
79 { "restart", no_argument, &restart, 1 },
82 run_subprogram (int argc
, char *argv
[], const posix_spawnattr_t
*attr
,
83 const posix_spawn_file_actions_t
*actions
, int tcfd
,
87 TEST_COMPARE (posix_spawnattr_getflags (attr
, &flags
), 0);
88 bool setpgrp
= flags
& POSIX_SPAWN_SETPGROUP
;
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)];
96 for (; i
< argc
- 1; i
++)
97 spargv
[i
] = argv
[i
+ 1];
98 spargv
[i
++] = (char *) "--direct";
99 spargv
[i
++] = (char *) "--restart";
101 spargv
[i
++] = (char *) "setgrpr";
104 snprintf (pgrp
, sizeof pgrp
, "%d", getpgrp ());
107 snprintf (tcfdstr
, sizeof tcfdstr
, "%d", tcfd
);
108 spargv
[i
++] = tcfdstr
;
112 TEST_COMPARE (POSIX_SPAWN (&pid
, argv
[1], actions
, attr
, spargv
, environ
),
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);
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:
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
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
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
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
);
199 do_test (int argc
, char *argv
[])
202 return handle_restart (argv
[1], argv
[2]);
204 pid_t pid
= xfork ();
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);
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);
231 run_test (argc
, argv
);
235 xwaitpid (pid
, &status
, 0);
236 TEST_VERIFY (WIFEXITED (status
));
240 #define TEST_FUNCTION_ARGV do_test
241 #include <support/test-driver.c>