From: Stanislav kholmanskikh <stanislav.kholmansk...@oracle.com>

Added function tst_run_cmd_fds which allows redirection of
stdout/stderr to file descriptors.

Now function tst_run_cmd is a wrapper to tst_run_cmd_fds.

Signed-off-by: Stanislav kholmanskikh <stanislav.kholmansk...@oracle.com>
---
 include/test.h    |   20 +++++++++++++++-
 lib/tst_run_cmd.c |   66 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 83 insertions(+), 3 deletions(-)

diff --git a/include/test.h b/include/test.h
index a76fafc..1957148 100644
--- a/include/test.h
+++ b/include/test.h
@@ -219,8 +219,26 @@ 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_fd: file descriptor where to redirect stdout. Set -1 if
+ * redirection is not needed.
+ * @stderr_fd: file descriptor where to redirect stderr. Set -1 if
+ * redirection is not needed.
  */
-void tst_run_cmd(void (cleanup_fn)(void), char *const argv[]);
+void tst_run_cmd_fds(void (cleanup_fn)(void),
+                       char *const argv[],
+                       int stdout_fd,
+                       int stderr_fd);
+
+/* Executes tst_run_cmd_fds() and redirects its output to a file
+ * @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[],
+               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..714b508 100644
--- a/lib/tst_run_cmd.c
+++ b/lib/tst_run_cmd.c
@@ -19,12 +19,21 @@
  *
  */
 
+#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_fds(void (cleanup_fn)(void),
+               char *const argv[],
+               int stdout_fd,
+               int stderr_fd)
 {
        if (argv == NULL || argv[0] == NULL) {
                tst_brkm(TBROK, cleanup_fn,
@@ -36,8 +45,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_fd != -1) {
+                       close(STDOUT_FILENO);
+                       dup2(stdout_fd, STDOUT_FILENO);
+               }
+
+               if (stderr_fd != -1) {
+                       close(STDERR_FILENO);
+                       dup2(stderr_fd, STDERR_FILENO);
+               }
+
                _exit(execvp(argv[0], argv));
+       }
 
        int ret = -1;
        if (waitpid(pid, &ret, 0) != pid) {
@@ -50,3 +71,44 @@ void tst_run_cmd(void (cleanup_fn)(void), char *const argv[])
                        argv[0], __FILE__, __LINE__);
        }
 }
+
+void tst_run_cmd(void (cleanup_fn)(void),
+               char *const argv[],
+               const char *stdout_path,
+               const char *stderr_path)
+{
+       int stdout_fd = -1;
+       int stderr_fd = -1;
+
+       if (stdout_path != NULL) {
+               stdout_fd = open(stdout_path,
+                               OPEN_FLAGS, OPEN_MODE);
+
+               if (stdout_fd == -1)
+                       tst_resm(TWARN | TERRNO,
+                               "open() on %s failed at %s:%d",
+                               stdout_path, __FILE__, __LINE__);
+       }
+
+       if (stderr_path != NULL) {
+               stderr_fd = open(stderr_path,
+                               OPEN_FLAGS, OPEN_MODE);
+
+               if (stderr_fd == -1)
+                       tst_resm(TWARN | TERRNO,
+                               "open() on %s failed at %s:%d",
+                               stderr_path, __FILE__, __LINE__);
+       }
+
+       tst_run_cmd_fds(cleanup_fn, argv, stdout_fd, stderr_fd);
+
+       if ((stdout_fd != -1) && (close(stdout_fd) == -1))
+               tst_resm(TWARN | TERRNO,
+                       "close() on %s failed at %s:%d",
+                       stdout_path, __FILE__, __LINE__);
+
+       if ((stderr_fd != -1) && (close(stderr_fd) == -1))
+               tst_resm(TWARN | TERRNO,
+                       "close() on %s failed at %s:%d",
+                       stderr_path, __FILE__, __LINE__);
+}
-- 
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=48897031&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to