From: Stanislav kholmanskikh <[email protected]>
Signed-off-by: Stanislav kholmanskikh <[email protected]>
---
include/test.h | 9 ++++++++-
lib/tst_run_cmd.c | 20 ++++++++++++++++++--
2 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/include/test.h b/include/test.h
index a76fafc..f294b16 100644
--- a/include/test.h
+++ b/include/test.h
@@ -219,8 +219,15 @@ long tst_ncpus_max(void);
* @argv: a list of two (at least program name + NULL) or more pointers that
* represent the argument list to the new program. The array of pointers
* must be terminated by a NULL pointer.
+ * @stdout_path: path where to redirect stdout. Set NULL if redirection is
+ * not needed.
+ * @stderr_path: path where to redirect stderr. Set NULL if redirection is
+ * not needed.
*/
-void tst_run_cmd(void (cleanup_fn)(void), char *const argv[]);
+void tst_run_cmd(void (cleanup_fn)(void),
+ char *const argv[],
+ const char *stdout_path,
+ const char *stderr_path);
#ifdef TST_USE_COMPAT16_SYSCALL
#define TCID_BIT_SUFFIX "_16"
diff --git a/lib/tst_run_cmd.c b/lib/tst_run_cmd.c
index 93fe2d6..c54c650 100644
--- a/lib/tst_run_cmd.c
+++ b/lib/tst_run_cmd.c
@@ -19,12 +19,16 @@
*
*/
+#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "test.h"
-void tst_run_cmd(void (cleanup_fn)(void), char *const argv[])
+void tst_run_cmd(void (cleanup_fn)(void),
+ char *const argv[],
+ const char *stdout_path,
+ const char *stderr_path)
{
if (argv == NULL || argv[0] == NULL) {
tst_brkm(TBROK, cleanup_fn,
@@ -36,8 +40,20 @@ void tst_run_cmd(void (cleanup_fn)(void), char *const argv[])
tst_brkm(TBROK | TERRNO, cleanup_fn, "vfork failed at %s:%d",
__FILE__, __LINE__);
}
- if (!pid)
+ if (!pid) {
+ /* redirecting stdout and stderr if needed */
+ if ((stdout_path != NULL) &&
+ (freopen(stdout_path, "a", stdout) == NULL))
+ tst_resm(TWARN | TERRNO, "freopen failed at %s:%d",
+ __FILE__, __LINE__);
+
+ if ((stderr_path != NULL) &&
+ (freopen(stderr_path, "a", stderr) == NULL))
+ tst_resm(TWARN | TERRNO, "freopen failed at %s:%d",
+ __FILE__, __LINE__);
+
_exit(execvp(argv[0], argv));
+ }
int ret = -1;
if (waitpid(pid, &ret, 0) != pid) {
--
1.7.1
------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list