Skip to contentSkip to conversation

Add tst_flush() library function

PATCH New Michael Moese 2 participants 4 messages Mar 9, 2018 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 Add tst_flush() library function

Add a library function to flush stderr and stdout streams:
void tst_flush(void)

This function flushes stderr and stdout streams. In case of an error, the test is aborted with TBROK and an error message is printed.

---
 doc/test-writing-guidelines.txt |  8 ++++++++
 include/tst_test.h              |  3 +++
 lib/tst_test.c                  | 15 +++++++++++++++
 3 files changed, 26 insertions(+)
doc/test-writing-guidelines.txt +8 -0
index 739b295b8..4c60cd66b 100644
@@ -465,6 +465,14 @@ Allows for setting timeout per test iteration dymanically in the test setup(),
 the timeout is specified in seconds. There are a few testcases whose runtime
 can vary arbitrarily, these can disable timeouts by setting it to -1.
 
+[source,c]
+------------------------------------------------------------------------------
+void tst_flush(void);
+-------------------------------------------------------------------------------
+
+Flush stderr and stdout streams, handling errors appropriately.
+You should not use fflush() for stderr or stdout.
+
 2.2.3 Test temporary directory
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
include/tst_test.h +3 -0
index af97b8983..54ff306d9 100644
@@ -70,6 +70,9 @@ void tst_brk_(const char *file, const int lineno, int ttype,
 #define tst_brk(ttype, arg_fmt, ...) \
 	tst_brk_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__)
 
+/* flush stderr and stdout */
+void tst_flush(void);
+
 pid_t safe_fork(const char *filename, unsigned int lineno);
 #define SAFE_FORK() \
 	safe_fork(__FILE__, __LINE__)
lib/tst_test.c +15 -0
index 2cf35ed66..9b4f43828 100644
@@ -1085,3 +1085,18 @@ void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
 
 	do_exit(ret);
 }
+
+
+void tst_flush(void)
+{
+	int rval;
+
+	rval = fflush(stderr);
+	if (rval != 0)
+		tst_brk(TBROK | TERRNO, "fflush(stderr) failed");
+
+	rval = fflush(stderr);
+	if (rval != 0)
+		tst_brk(TBROK | TERRNO, "fflush(stdout) failed");
+
+}
Signature · 1 line
2.13.6
#1 lore raw

PATCH v2 2/3 remove old tst_flush()

The old tst_flush() was renamed to tst_old_flush(), and tst_flush() was removed from tstapicmd.c.
The new tst_flush() has the same prototype as the old one, so any test using it should still be fine.

---
 lib/tst_res.c             | 2 +-
 tools/apicmds/ltpapicmd.c | 4 ----
 2 files changed, 1 insertion(+), 5 deletions(-)
lib/tst_res.c +1 -1
index b56f37db0..9c88b2923 100644
@@ -245,7 +245,7 @@ static void tst_condense(int tnum, int ttype, const char *tmesg)
 	Buffered = TRUE;
 }
 
-void tst_flush(void)
+void tst_old_flush(void)
 {
 	NO_NEWLIB_ASSERT("Unknown", 0);
 
tools/apicmds/ltpapicmd.c +0 -4
index 9ad68b589..4b66c4226 100644
@@ -34,8 +34,6 @@
  *                          break remaining test cases
  *              tst_res   - Print result message, including file contents
  *              tst_resm  - Print result message
- *              tst_flush - Print any messages pending because of CONDENSE mode,
- *                          and flush output stream
  *              tst_exit  - Exit test with a meaningful exit value
  *
  *              These are the minimum set of functions or commands required to
@@ -332,8 +330,6 @@ int main(int argc, char *argv[])
 		apicmd_resm(argc, argv);
 	} else if (strcmp(cmd_name, "tst_exit") == 0) {
 		tst_exit();
-	} else if (strcmp(cmd_name, "tst_flush") == 0) {
-		tst_flush();
 	} else if (strcmp(cmd_name, "tst_ncpus") == 0) {
 		printf("%li\n", tst_ncpus());
 	} else if (strcmp(cmd_name, "tst_ncpus_conf") == 0) {
Signature · 1 line
2.13.6
#2 lore raw

PATCH v2 2/3 remove old tst_flush()

Hi!

Quoted 4 lines

The old tst_flush() was renamed to tst_old_flush(), and tst_flush() was removed from tstapicmd.c.
The new tst_flush() has the same prototype as the old one, so any test using it should still be fine.

There are calls to tst_flush() in the lib/tst_res.c as well as in the oldlib tests, these have to call the tst_old_flush().

Also this patch should be first in the series since ideally the git tree should be compileable after each patch so that we do not break bisections.

Signature · 2 lines
Cyril Hrubis
[email protected]
#3 lore raw

PATCH v2 3/3 safe_fork() should use tst_flush() instead of fflush()

In reply to Michael Moese

safe_fork() should not use fflush(stdout). This commit makes use of the the newly added tst_flush(), which flushes, in fact, both stdout and stderr.
In addition, tests output their results to stderr, so flushing stdout here is simply wrong.

---
 lib/tst_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/tst_test.c +1 -1
index 9b4f43828..7069bbc5e 100644
@@ -380,7 +380,7 @@ pid_t safe_fork(const char *filename, unsigned int lineno)
 	if (!tst_test->forks_child)
 		tst_brk(TBROK, "test.forks_child must be set!");
 
-	fflush(stdout);
+	tst_flush();
 
 	pid = fork();
 	if (pid < 0)
Signature · 1 line
2.13.6