Update libc.pot for 2.35 release.
[glibc.git] / posix / tst-spawn6.c
blob5f95bd1938a69552d180d874aeb247081c444efa
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>
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>
33 static int
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
40 itself. */
41 pid_t pgrp;
42 if (strcmp (argv1, "setgrpr") != 0)
43 TEST_COMPARE (sscanf (argv1, "%d", &pgrp), 1);
44 else
46 pgrp = getpgrp ();
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);
55 xclose (fd);
56 return 0;
59 static int restart;
60 #define CMDLINE_OPTIONS \
61 { "restart", no_argument, &restart, 1 },
63 static void
64 run_subprogram (int argc, char *argv[], const posix_spawnattr_t *attr,
65 int exp_err)
67 short int flags;
68 TEST_COMPARE (posix_spawnattr_getflags (attr, &flags), 0);
69 bool setpgrp = flags & POSIX_SPAWN_SETPGROUP;
71 char *spargv[9];
72 char pgrp[INT_STRLEN_BOUND (pid_t)];
74 int i = 0;
75 for (; i < argc - 1; i++)
76 spargv[i] = argv[i + 1];
77 spargv[i++] = (char *) "--direct";
78 spargv[i++] = (char *) "--restart";
79 if (setpgrp)
80 spargv[i++] = (char *) "setgrpr";
81 else
83 snprintf (pgrp, sizeof pgrp, "%d", getpgrp ());
84 spargv[i++] = pgrp;
86 spargv[i] = NULL;
87 TEST_VERIFY_EXIT (i < array_length (spargv));
89 pid_t pid;
90 TEST_COMPARE (posix_spawn (&pid, argv[1], NULL, attr, spargv, environ),
91 exp_err);
92 if (exp_err != 0)
93 return;
95 int status;
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);
103 static int
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:
113 + --setgrpr optional
116 if (restart)
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);
127 int fd;
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
144 group. */
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
158 from tcsetpgrp. */
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);
169 xclose (tcfd);
171 return 0;
174 #define TEST_FUNCTION_ARGV do_test
175 #include <support/test-driver.c>