Skip to content

Commit aa81141

Browse files
author
Daniel Kroening
committed
run() can now redirect stderr
1 parent a6652d2 commit aa81141

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

src/goto-cc/as_mode.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ int as_modet::run_as()
267267
std::cout << '\n';
268268
#endif
269269

270-
return run(new_argv[0], new_argv, cmdline.stdin_file, "");
270+
return run(new_argv[0], new_argv, cmdline.stdin_file);
271271
}
272272

273273
int as_modet::as_hybrid_binary()
@@ -314,7 +314,7 @@ int as_modet::as_hybrid_binary()
314314
objcopy_argv.push_back("--remove-section=goto-cc");
315315
objcopy_argv.push_back(output_file);
316316

317-
result=run(objcopy_argv[0], objcopy_argv, "", "");
317+
result = run(objcopy_argv[0], objcopy_argv);
318318
}
319319

320320
if(result==0)
@@ -327,7 +327,7 @@ int as_modet::as_hybrid_binary()
327327
objcopy_argv.push_back("goto-cc="+saved);
328328
objcopy_argv.push_back(output_file);
329329

330-
result=run(objcopy_argv[0], objcopy_argv, "", "");
330+
result = run(objcopy_argv[0], objcopy_argv);
331331
}
332332

333333
int remove_result=remove(saved.c_str());
@@ -354,7 +354,7 @@ int as_modet::as_hybrid_binary()
354354
lipo_argv.push_back("-output");
355355
lipo_argv.push_back(output_file);
356356

357-
result=run(lipo_argv[0], lipo_argv, "", "");
357+
result = run(lipo_argv[0], lipo_argv);
358358
}
359359

360360
int remove_result=remove(saved.c_str());

src/goto-cc/gcc_mode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ int gcc_modet::run_gcc(const compilet &compiler)
831831
debug() << " " << new_argv[i];
832832
debug() << eom;
833833

834-
return run(new_argv[0], new_argv, cmdline.stdin_file, "");
834+
return run(new_argv[0], new_argv, cmdline.stdin_file);
835835
}
836836

837837
int gcc_modet::gcc_hybrid_binary(compilet &compiler)

src/util/run.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ Date: August 2012
1010

1111
#include "run.h"
1212

13-
#include <cassert>
14-
1513
#ifdef _WIN32
1614
#include <process.h>
1715
#else
@@ -30,6 +28,7 @@ Date: August 2012
3028

3129
#endif
3230

31+
#include <util/invariant.h>
3332
#include <util/unicode.h>
3433
#include <util/signal_catcher.h>
3534

@@ -39,19 +38,21 @@ int run_shell(const std::string &command)
3938
std::vector<std::string> argv;
4039
argv.push_back(shell);
4140
argv.push_back(command);
42-
return run(shell, argv, "", "");
41+
return run(shell, argv, "", "", "");
4342
}
4443

4544
int run(
4645
const std::string &what,
4746
const std::vector<std::string> &argv,
4847
const std::string &std_input,
49-
const std::string &std_output)
48+
const std::string &std_output,
49+
const std::string &std_error)
5050
{
5151
#ifdef _WIN32
52-
// we don't support stdin/stdout redirection on Windows
53-
assert(std_input.empty());
54-
assert(std_output.empty());
52+
// we don't support stdin/stdout/stderr redirection on Windows
53+
PRECONDITION(std_input.empty());
54+
PRECONDITION(std_output.empty());
55+
PRECONDITION(std_error.empty());
5556

5657
// unicode version of the arguments
5758
std::vector<std::wstring> wargv;
@@ -101,6 +102,18 @@ int run(
101102
}
102103
}
103104

105+
int stderr_fd = STDERR_FILENO;
106+
107+
if(!std_error.empty())
108+
{
109+
stderr_fd = open(std_error.c_str(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
110+
if(stderr_fd == -1)
111+
{
112+
perror("Failed to open stderr copy");
113+
return 1;
114+
}
115+
}
116+
104117
// temporarily suspend all signals
105118
sigset_t new_mask, old_mask;
106119
sigemptyset(&new_mask);
@@ -127,6 +140,8 @@ int run(
127140
dup2(stdin_fd, STDIN_FILENO);
128141
if(stdout_fd!=STDOUT_FILENO)
129142
dup2(stdout_fd, STDOUT_FILENO);
143+
if(stderr_fd != STDERR_FILENO)
144+
dup2(stderr_fd, STDERR_FILENO);
130145

131146
errno=0;
132147
execvp(what.c_str(), _argv.data());
@@ -153,13 +168,17 @@ int run(
153168
close(stdin_fd);
154169
if(stdout_fd!=STDOUT_FILENO)
155170
close(stdout_fd);
171+
if(stderr_fd != STDERR_FILENO)
172+
close(stderr_fd);
156173
return 1;
157174
}
158175

159176
if(stdin_fd!=STDIN_FILENO)
160177
close(stdin_fd);
161178
if(stdout_fd!=STDOUT_FILENO)
162179
close(stdout_fd);
180+
if(stderr_fd != STDERR_FILENO)
181+
close(stderr_fd);
163182

164183
return WEXITSTATUS(status);
165184
}
@@ -173,6 +192,8 @@ int run(
173192
close(stdin_fd);
174193
if(stdout_fd!=STDOUT_FILENO)
175194
close(stdout_fd);
195+
if(stderr_fd != STDERR_FILENO)
196+
close(stderr_fd);
176197

177198
return 1;
178199
}

src/util/run.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ Date: August 2012
1818
int run(
1919
const std::string &what,
2020
const std::vector<std::string> &argv,
21-
const std::string &std_input,
22-
const std::string &std_output);
21+
const std::string &std_input = "",
22+
const std::string &std_output = "",
23+
const std::string &std_error = "");
2324

2425
int run_shell(const std::string &command);
2526

0 commit comments

Comments
 (0)