From: Stanislav kholmanskikh <[email protected]>

Signed-off-by: Stanislav kholmanskikh <[email protected]>
---
 include/test.h    |    9 ++++++++-
 lib/tst_run_cmd.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 56 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..044adc7 100644
--- a/lib/tst_run_cmd.c
+++ b/lib/tst_run_cmd.c
@@ -19,25 +19,71 @@
  *
  */
 
+#include <errno.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <sys/wait.h>
+#include <fcntl.h>
 #include <unistd.h>
 #include "test.h"
 
-void tst_run_cmd(void (cleanup_fn)(void), char *const argv[])
+#define OPEN_MODE      (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
+#define OPEN_FLAGS     (O_WRONLY | O_APPEND | O_CREAT)
+
+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,
                        "argument list is empty at %s:%d", __FILE__, __LINE__);
        }
 
+       /* descriptors for child (if needed) */
+       int child_stdout;
+       int child_stderr;
+
        pid_t pid = vfork();
        if (pid == -1) {
                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) {
+                       child_stdout = open(stdout_path,
+                                       OPEN_FLAGS, OPEN_MODE);
+
+                       if (child_stdout == -1) {
+                               tst_resm(TWARN | TERRNO,
+                                       "open() on %s failed at %s:%d: %s",
+                                       stdout_path, __FILE__, __LINE__,
+                                       strerror(errno));
+                       } else {
+                               close(STDOUT_FILENO);
+                               dup2(child_stdout, STDOUT_FILENO);
+                       }
+
+               }
+
+               if (stderr_path != NULL) {
+                       child_stderr = open(stderr_path,
+                                       OPEN_FLAGS, OPEN_MODE);
+
+                       if (child_stderr == -1) {
+                               tst_resm(TWARN | TERRNO,
+                                       "open() on %s failed at %s:%d: %s",
+                                       stderr_path, __FILE__, __LINE__,
+                                       strerror(errno));
+                       } else {
+                               close(STDERR_FILENO);
+                               dup2(child_stderr, STDERR_FILENO);
+                       }
+               }
+
                _exit(execvp(argv[0], argv));
+       }
 
        int ret = -1;
        if (waitpid(pid, &ret, 0) != pid) {
-- 
1.7.1


------------------------------------------------------------------------------
Get your SQL database under version control now!
Version control is standard for application code, but databases havent 
caught up. So what steps can you take to put your SQL databases under 
version control? Why should you start doing it? Read more to find out.
http://pubads.g.doubleclick.net/gampad/clk?id=49501711&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to