* [LTP] [PATCH] lib: fputs() in print_result() is not signal safe
@ 2020-02-21 16:12 Jan Stancek
2020-02-21 16:29 ` Cyril Hrubis
2020-02-25 7:35 ` Petr Vorel
0 siblings, 2 replies; 4+ messages in thread
From: Jan Stancek @ 2020-02-21 16:12 UTC (permalink / raw)
To: ltp
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.
Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
lib/tst_test.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/lib/tst_test.c b/lib/tst_test.c
index 9a24cffc5011..220d7fdfc548 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -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,
--
2.18.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-02-25 8:40 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-02-21 16:12 [LTP] [PATCH] lib: fputs() in print_result() is not signal safe Jan Stancek
2020-02-21 16:29 ` Cyril Hrubis
2020-02-25 7:35 ` Petr Vorel
2020-02-25 8:40 ` Jan Stancek
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox