1 /* Copyright (C) 2002-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
24 #include <support/capture_subprocess.h>
25 #include <support/check.h>
26 #include <support/temp_file.h>
27 #include <support/support.h>
28 #include <support/xthread.h>
29 #include <support/xunistd.h>
32 static long int namemax
;
35 do_prepare (int argc
, char *argv
[])
37 tmpdir
= support_create_temp_directory ("tst-system-");
38 /* Include the last '/0'. */
39 namemax
= pathconf (tmpdir
, _PC_NAME_MAX
) + 1;
40 TEST_VERIFY_EXIT (namemax
!= -1);
42 #define PREPARE do_prepare
53 call_system (void *closure
)
55 struct args
*args
= (struct args
*) closure
;
58 if (args
->path
!= NULL
)
59 TEST_COMPARE (setenv ("PATH", args
->path
, 1), 0);
60 ret
= system (args
->command
);
61 if (args
->term_sig
== 0)
63 /* Expect regular termination. */
64 TEST_VERIFY (WIFEXITED (ret
) != 0);
65 TEST_COMPARE (WEXITSTATUS (ret
), args
->exit_status
);
69 /* status_or_signal < 0. Expect termination by signal. */
70 TEST_VERIFY (WIFSIGNALED (ret
) != 0);
71 TEST_COMPARE (WTERMSIG (ret
), args
->term_sig
);
76 sleep_and_check_sigchld (void *closure
)
78 double *seconds
= (double *) closure
;
80 sprintf (cmd
, "sleep %lf" , *seconds
);
81 TEST_COMPARE (system (cmd
), 0);
83 sigset_t blocked
= {0};
84 TEST_COMPARE (sigprocmask (SIG_BLOCK
, NULL
, &blocked
), 0);
85 TEST_COMPARE (sigismember (&blocked
, SIGCHLD
), 0);
92 TEST_VERIFY (system (NULL
) != 0);
96 memset (cmd
, 'a', sizeof(cmd
));
97 cmd
[sizeof(cmd
) - 1] = '\0';
99 struct support_capture_subprocess result
;
100 result
= support_capture_subprocess (call_system
,
104 support_capture_subprocess_check (&result
, "system", 0, sc_allow_stderr
);
106 char *returnerr
= xasprintf ("%s: execing %s failed: "
107 "No such file or directory",
108 basename(_PATH_BSHELL
), cmd
);
109 TEST_COMPARE_STRING (result
.err
.buffer
, returnerr
);
114 char cmd
[namemax
+ 1];
115 memset (cmd
, 'a', sizeof(cmd
));
116 cmd
[sizeof(cmd
) - 1] = '\0';
118 struct support_capture_subprocess result
;
119 result
= support_capture_subprocess (call_system
,
123 support_capture_subprocess_check (&result
, "system", 0, sc_allow_stderr
);
125 char *returnerr
= xasprintf ("%s: execing %s failed: "
126 "File name too long",
127 basename(_PATH_BSHELL
), cmd
);
128 TEST_COMPARE_STRING (result
.err
.buffer
, returnerr
);
133 struct support_capture_subprocess result
;
134 result
= support_capture_subprocess (call_system
,
136 "kill $$", 0, SIGTERM
138 support_capture_subprocess_check (&result
, "system", 0, sc_allow_none
);
142 struct support_capture_subprocess result
;
143 result
= support_capture_subprocess (call_system
,
144 &(struct args
) { "echo ...", 0 });
145 support_capture_subprocess_check (&result
, "system", 0, sc_allow_stdout
);
146 TEST_COMPARE_STRING (result
.out
.buffer
, "...\n");
150 struct support_capture_subprocess result
;
151 const char *cmd
= "-echo";
152 result
= support_capture_subprocess (call_system
,
153 &(struct args
) { cmd
, 127 });
154 support_capture_subprocess_check (&result
, "system", 0, sc_allow_stderr
|
156 char *returnerr
= xasprintf ("%s: execing -echo failed: "
157 "No such file or directory",
158 basename(_PATH_BSHELL
));
159 TEST_COMPARE_STRING (result
.err
.buffer
, returnerr
);
164 struct support_capture_subprocess result
;
165 result
= support_capture_subprocess (call_system
,
166 &(struct args
) { "exit 1", 1 });
167 support_capture_subprocess_check (&result
, "system", 0, sc_allow_none
);
172 xstat (_PATH_BSHELL
, &st
);
173 mode_t mode
= st
.st_mode
;
174 xchmod (_PATH_BSHELL
, mode
& ~(S_IXUSR
| S_IXGRP
| S_IXOTH
));
176 struct support_capture_subprocess result
;
177 result
= support_capture_subprocess (call_system
,
181 support_capture_subprocess_check (&result
, "system", 0, sc_allow_none
);
183 xchmod (_PATH_BSHELL
, st
.st_mode
);
187 pthread_t long_sleep_thread
= xpthread_create (NULL
,
188 sleep_and_check_sigchld
,
190 pthread_t short_sleep_thread
= xpthread_create (NULL
,
191 sleep_and_check_sigchld
,
193 xpthread_join (short_sleep_thread
);
194 xpthread_join (long_sleep_thread
);
197 TEST_COMPARE (system (""), 0);
202 #include <support/test-driver.c>