1 /* Copyright (C) 2002-2020 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>
31 static long int namemax
;
34 do_prepare (int argc
, char *argv
[])
36 tmpdir
= support_create_temp_directory ("tst-system-");
37 /* Include the last '/0'. */
38 namemax
= pathconf (tmpdir
, _PC_NAME_MAX
) + 1;
39 TEST_VERIFY_EXIT (namemax
!= -1);
41 #define PREPARE do_prepare
52 call_system (void *closure
)
54 struct args
*args
= (struct args
*) closure
;
57 if (args
->path
!= NULL
)
58 TEST_COMPARE (setenv ("PATH", args
->path
, 1), 0);
59 ret
= system (args
->command
);
60 if (args
->term_sig
== 0)
62 /* Expect regular termination. */
63 TEST_VERIFY (WIFEXITED (ret
) != 0);
64 TEST_COMPARE (WEXITSTATUS (ret
), args
->exit_status
);
68 /* status_or_signal < 0. Expect termination by signal. */
69 TEST_VERIFY (WIFSIGNALED (ret
) != 0);
70 TEST_COMPARE (WTERMSIG (ret
), args
->term_sig
);
77 TEST_VERIFY (system (NULL
) != 0);
81 memset (cmd
, 'a', sizeof(cmd
));
82 cmd
[sizeof(cmd
) - 1] = '\0';
84 struct support_capture_subprocess result
;
85 result
= support_capture_subprocess (call_system
,
89 support_capture_subprocess_check (&result
, "system", 0, sc_allow_stderr
);
91 char *returnerr
= xasprintf ("%s: execing %s failed: "
92 "No such file or directory",
93 basename(_PATH_BSHELL
), cmd
);
94 TEST_COMPARE_STRING (result
.err
.buffer
, returnerr
);
99 char cmd
[namemax
+ 1];
100 memset (cmd
, 'a', sizeof(cmd
));
101 cmd
[sizeof(cmd
) - 1] = '\0';
103 struct support_capture_subprocess result
;
104 result
= support_capture_subprocess (call_system
,
108 support_capture_subprocess_check (&result
, "system", 0, sc_allow_stderr
);
110 char *returnerr
= xasprintf ("%s: execing %s failed: "
111 "File name too long",
112 basename(_PATH_BSHELL
), cmd
);
113 TEST_COMPARE_STRING (result
.err
.buffer
, returnerr
);
118 struct support_capture_subprocess result
;
119 result
= support_capture_subprocess (call_system
,
121 "kill $$", 0, SIGTERM
123 support_capture_subprocess_check (&result
, "system", 0, sc_allow_none
);
127 struct support_capture_subprocess result
;
128 result
= support_capture_subprocess (call_system
,
129 &(struct args
) { "echo ...", 0 });
130 support_capture_subprocess_check (&result
, "system", 0, sc_allow_stdout
);
131 TEST_COMPARE_STRING (result
.out
.buffer
, "...\n");
135 struct support_capture_subprocess result
;
136 result
= support_capture_subprocess (call_system
,
137 &(struct args
) { "exit 1", 1 });
138 support_capture_subprocess_check (&result
, "system", 0, sc_allow_none
);
141 TEST_COMPARE (system (""), 0);
146 #include <support/test-driver.c>