1 /* Copyright (C) 2002-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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/>. */
25 #include <support/capture_subprocess.h>
26 #include <support/check.h>
27 #include <support/temp_file.h>
28 #include <support/support.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
);
78 TEST_VERIFY (system (NULL
) != 0);
82 memset (cmd
, 'a', sizeof(cmd
));
83 cmd
[sizeof(cmd
) - 1] = '\0';
85 struct support_capture_subprocess result
;
86 result
= support_capture_subprocess (call_system
,
90 support_capture_subprocess_check (&result
, "system", 0, sc_allow_stderr
);
92 char *returnerr
= xasprintf ("%s: execing %s failed: "
93 "No such file or directory",
94 basename(_PATH_BSHELL
), cmd
);
95 TEST_COMPARE_STRING (result
.err
.buffer
, returnerr
);
100 char cmd
[namemax
+ 1];
101 memset (cmd
, 'a', sizeof(cmd
));
102 cmd
[sizeof(cmd
) - 1] = '\0';
104 struct support_capture_subprocess result
;
105 result
= support_capture_subprocess (call_system
,
109 support_capture_subprocess_check (&result
, "system", 0, sc_allow_stderr
);
111 char *returnerr
= xasprintf ("%s: execing %s failed: "
112 "File name too long",
113 basename(_PATH_BSHELL
), cmd
);
114 TEST_COMPARE_STRING (result
.err
.buffer
, returnerr
);
119 struct support_capture_subprocess result
;
120 result
= support_capture_subprocess (call_system
,
122 "kill $$", 0, SIGTERM
124 support_capture_subprocess_check (&result
, "system", 0, sc_allow_none
);
128 struct support_capture_subprocess result
;
129 result
= support_capture_subprocess (call_system
,
130 &(struct args
) { "echo ...", 0 });
131 support_capture_subprocess_check (&result
, "system", 0, sc_allow_stdout
);
132 TEST_COMPARE_STRING (result
.out
.buffer
, "...\n");
136 struct support_capture_subprocess result
;
137 result
= support_capture_subprocess (call_system
,
138 &(struct args
) { "exit 1", 1 });
139 support_capture_subprocess_check (&result
, "system", 0, sc_allow_none
);
144 xstat (_PATH_BSHELL
, &st
);
145 mode_t mode
= st
.st_mode
;
146 xchmod (_PATH_BSHELL
, mode
& ~(S_IXUSR
| S_IXGRP
| S_IXOTH
));
148 struct support_capture_subprocess result
;
149 result
= support_capture_subprocess (call_system
,
153 support_capture_subprocess_check (&result
, "system", 0, sc_allow_none
);
155 xchmod (_PATH_BSHELL
, st
.st_mode
);
158 TEST_COMPARE (system (""), 0);
163 #include <support/test-driver.c>