1 /* Check posix_spawn set controlling terminal extension.
2 Copyright (C) 2022 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>
34 handle_restart (const char *argv1
)
36 int fd
= xopen (_PATH_TTY
, O_RDONLY
, 0600);
38 /* If process group is not changed (POSIX_SPAWN_SETPGROUP), then check
39 the creating process one, otherwise check against the process group
42 if (strcmp (argv1
, "setgrpr") != 0)
43 TEST_COMPARE (sscanf (argv1
, "%d", &pgrp
), 1);
47 /* Check if a new process group was actually created. */
48 pid_t ppid
= getppid ();
49 pid_t pgid
= getpgid (ppid
);
50 TEST_VERIFY (pgid
!= pgrp
);
53 TEST_COMPARE (tcgetpgrp (fd
), pgrp
);
60 #define CMDLINE_OPTIONS \
61 { "restart", no_argument, &restart, 1 },
64 run_subprogram (int argc
, char *argv
[], const posix_spawnattr_t
*attr
,
68 TEST_COMPARE (posix_spawnattr_getflags (attr
, &flags
), 0);
69 bool setpgrp
= flags
& POSIX_SPAWN_SETPGROUP
;
72 char pgrp
[INT_STRLEN_BOUND (pid_t
)];
75 for (; i
< argc
- 1; i
++)
76 spargv
[i
] = argv
[i
+ 1];
77 spargv
[i
++] = (char *) "--direct";
78 spargv
[i
++] = (char *) "--restart";
80 spargv
[i
++] = (char *) "setgrpr";
83 snprintf (pgrp
, sizeof pgrp
, "%d", getpgrp ());
87 TEST_VERIFY_EXIT (i
< array_length (spargv
));
90 TEST_COMPARE (posix_spawn (&pid
, argv
[1], NULL
, attr
, spargv
, environ
),
96 TEST_COMPARE (xwaitpid (pid
, &status
, WUNTRACED
), pid
);
97 TEST_VERIFY (WIFEXITED (status
));
98 TEST_VERIFY (!WIFSTOPPED (status
));
99 TEST_VERIFY (!WIFSIGNALED (status
));
100 TEST_COMPARE (WEXITSTATUS (status
), 0);
104 do_test (int argc
, char *argv
[])
106 /* We must have either:
107 - four parameters left if called initially:
108 + path to ld.so optional
109 + "--library-path" optional
110 + the library path optional
111 + the application name
112 - six parameters left if called through re-execution:
117 return handle_restart (argv
[1]);
119 int tcfd
= xopen (_PATH_TTY
, O_RDONLY
, 0600);
121 /* Check getters and setters. */
123 posix_spawnattr_t attr
;
124 TEST_COMPARE (posix_spawnattr_init (&attr
), 0);
125 TEST_COMPARE (posix_spawnattr_tcsetpgrp_np (&attr
, tcfd
), 0);
128 TEST_COMPARE (posix_spawnattr_tcgetpgrp_np (&attr
, &fd
), 0);
129 TEST_COMPARE (tcfd
, fd
);
132 /* Check setting the controlling terminal without changing the group. */
134 posix_spawnattr_t attr
;
135 TEST_COMPARE (posix_spawnattr_init (&attr
), 0);
136 TEST_COMPARE (posix_spawnattr_setflags (&attr
, POSIX_SPAWN_TCSETPGROUP
),
138 TEST_COMPARE (posix_spawnattr_tcsetpgrp_np (&attr
, tcfd
), 0);
140 run_subprogram (argc
, argv
, &attr
, 0);
143 /* Check setting both the controlling terminal and the create a new process
146 posix_spawnattr_t attr
;
147 TEST_COMPARE (posix_spawnattr_init (&attr
), 0);
148 TEST_COMPARE (posix_spawnattr_setflags (&attr
, POSIX_SPAWN_TCSETPGROUP
149 | POSIX_SPAWN_SETPGROUP
),
151 TEST_COMPARE (posix_spawnattr_setpgroup (&attr
, 0), 0);
152 TEST_COMPARE (posix_spawnattr_tcsetpgrp_np (&attr
, tcfd
), 0);
154 run_subprogram (argc
, argv
, &attr
, 0);
157 /* Trying to set the controlling terminal after a setsid incurs in a ENOTTY
160 posix_spawnattr_t attr
;
161 TEST_COMPARE (posix_spawnattr_init (&attr
), 0);
162 TEST_COMPARE (posix_spawnattr_setflags (&attr
, POSIX_SPAWN_TCSETPGROUP
163 | POSIX_SPAWN_SETSID
), 0);
164 TEST_COMPARE (posix_spawnattr_tcsetpgrp_np (&attr
, tcfd
), 0);
166 run_subprogram (argc
, argv
, &attr
, ENOTTY
);
174 #define TEST_FUNCTION_ARGV do_test
175 #include <support/test-driver.c>