Skip to contentSkip to conversation

lib: fputs() in print_result() is not signal safe

PATCH New Jan Stancek 3 participants 4 messages Feb 21, 2020 – Feb 25, 2020 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: fputs() in print_result() is not signal safe

We have tests that use tst_res() from signal handler and current implementation leads to rare hangs if signal arrives in bad time:
main
tst_run_tcases

    fork_testrun
     testrun
      run_tests
       run
        tst_res_ -> TINFO from main process
         tst_vres_
          print_result
           fputs
            __lll_lock_wait_private
            <signal handler called>
             tst_res_ -> TINFO from signal handler
              tst_vres_
               print_result
                fputs
                 __lll_lock_wait_private -> HANGS

One example is timer_settime01, where we have TPASS from main process and TINFO as response to SIGALRM. SIGALRM happening immediately on older kernels might be a bug, but that is beside the point of this patch.

Replace fputs() with write() to avoid this hang.

---
 lib/tst_test.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
lib/tst_test.c +12 -2
index 9a24cffc5011..220d7fdfc548 100644
@@ -181,7 +181,7 @@ static void print_result(const char *file, const int lineno, int ttype,
 {
 	char buf[1024];
 	char *str = buf;
-	int ret, size = sizeof(buf), ssize, int_errno;
+	int ret, size = sizeof(buf), ssize, int_errno, buflen;
 	const char *str_errno = NULL;
 	const char *res;
 
@@ -255,7 +255,17 @@ static void print_result(const char *file, const int lineno, int ttype,
 
 	snprintf(str, size, "\n");
 
-	fputs(buf, stderr);
+	/* we might be called from signal handler, so use write() */
+	buflen = str - buf + 1;
+	str = buf;
+	while (buflen) {
+		ret = write(STDERR_FILENO, str, buflen);
+		if (ret <= 0)
+			break;
+
+		str += ret;
+		buflen -= ret;
+	}
 }
 
 void tst_vres_(const char *file, const int lineno, int ttype,
Signature · 1 line
2.18.1
#1 lore raw

PATCH 1/1 lib: fputs() in print_result() is not signal safe

Hi!

Quoted 20 lines

We have tests that use tst_res() from signal handler and current implementation leads to rare hangs if signal arrives in bad time:
main
tst_run_tcases

    fork_testrun
     testrun
      run_tests
       run
        tst_res_ -> TINFO from main process
         tst_vres_
          print_result
           fputs
            __lll_lock_wait_private
            <signal handler called>
             tst_res_ -> TINFO from signal handler
              tst_vres_
               print_result
                fputs
                 __lll_lock_wait_private -> HANGS
Quoted 3 lines

One example is timer_settime01, where we have TPASS from main process and TINFO as response to SIGALRM. SIGALRM happening immediately on older kernels might be a bug, but that is beside the point of this patch.

I guess that tst_res() wasn't even supposed to be signal-async-safe but looking at the code I guess that this is the only change needed to make it so.

Signature · 2 lines
Cyril Hrubis
[email protected]