[LTP] [PATCH] [RFC] lib: shell: Fix timeout process races
Cyril Hrubis
chrubis@suse.cz
Mon Sep 20 09:36:36 CEST 2021
Hi!
> > There were actually several races in the shell library timeout handling.
> >
> > This commit fixes hopefully all of them by:
> >
> > * Reimplementing the backgroud timer in C
> I did that once, but at that point it was kinda rejected ;)
> See https://lists.linux.it/pipermail/ltp/2021-May/022445.html
> and https://lists.linux.it/pipermail/ltp/2021-May/022453.html
I guess we found out the hard way that it's impossible to write raceless
timeouts in shell.
> > + tst_timeout_kill $sec $pid &
> >
> > _tst_setup_timer_pid=$!
> > +
> > + while true; do
> > + local state
> > +
> > + state=$(cut -d' ' -f3 "/proc/$_tst_setup_timer_pid/stat")
> Hmm maybe we could use the checkpoint api here instead. Wouldn't this be
> more portable?
Well there are other tests in LTP that depend on the /proc/$PID/stat
already and I do not remmeber any bugreports about it being broken or
not available. Linux generally does not work without /proc/ being
mounted anyways.
I wanted to avoid checkpoints because that requires shared memory and
it's more complex to setup and if we add it here it would be set up by
every shell process, but maybe that's not that bad. Maybe we should
set up the piece of shared memory as we do in C and use it for the
result counter as well. But anyways that is orthogonal from the fixes
here and could be done later on.
> > +
> > + if [ "$state" = "S" ]; then
> > + break;
> > + fi
> > +
> > + tst_sleep 1ms
> > + done
> > }
> >
> > tst_require_root()
> > diff --git a/testcases/lib/tst_timeout_kill.c b/testcases/lib/tst_timeout_kill.c
> > new file mode 100644
> > index 000000000..6e97514f1
> > --- /dev/null
> > +++ b/testcases/lib/tst_timeout_kill.c
> > @@ -0,0 +1,93 @@
> > +// SPDX-License-Identifier: GPL-2.0-or-later
> > +/*
> > + * Copyright (c) 2021 Cyril Hrubis <chrubis@suse.cz>
> > + */
> > +
> > +#include <stdio.h>
> > +#include <signal.h>
> > +#include <stdlib.h>
> > +#include <unistd.h>
> > +#include <tst_res_flags.h>
> > +#include <tst_ansi_color.h>
> > +
> > +static void print_help(const char *name)
> > +{
> > + fprintf(stderr, "usage: %s timeout pid\n", name);
> > +}
> > +
> > +static void print_msg(int type, const char *msg)
> > +{
> > + const char *strtype = "";
> > +
> > + switch (type) {
> > + case TBROK:
> > + strtype = "TBROK";
> > + break;
> > + case TINFO:
> > + strtype = "TINFO";
> > + break;
> > + }
> > +
> > + if (tst_color_enabled(STDERR_FILENO)) {
> > + fprintf(stderr, "%s%s: %s%s\n", tst_ttype2color(type),
> > + strtype, ANSI_COLOR_RESET, msg);
> > + } else {
> > + fprintf(stderr, "%s: %s\n", strtype, msg);
> > + }
> > +}
> Shouldn't this be reused from the library instead of being reimplemented?
I wanted to avoid calling the tst_res() functions to keep things simple,
but I guess that there is no reason not to use it.
> > +
> > +int main(int argc, char *argv[])
> > +{
> > + int timeout, pid, ret, i;
> > +
> > + if (argc != 3) {
> > + print_help(argv[0]);
> > + return 1;
> > + }
> > +
> > + timeout = atoi(argv[1]);
> > + pid = atoi(argv[2]);
> > +
> > + if (timeout < 0) {
> > + fprintf(stderr, "Invalid timeout '%s'", argv[1]);
> > + print_help(argv[0]);
> > + return 1;
> > + }
> > +
> > + if (pid <= 1) {
> > + fprintf(stderr, "Invalid pid '%s'", argv[2]);
> > + print_help(argv[0]);
> > + return 1;
> > + }
> > +
> > + if (timeout)
> > + sleep(timeout);
> > +
> > + print_msg(TBROK, "Test timed out, sending SIGTERM! If you are running on slow machine, try exporting LTP_TIMEOUT_MUL > 1");
> > +
> > + ret = kill(-pid, SIGTERM);
> > + if (ret) {
> > + print_msg(TBROK, "kill() failed");
> > + return 1;
> > + }
> > +
> > + usleep(100000);
> > +
> > + i = 10;
> > +
> > + while (!kill(-pid, 0) && i-- > 0) {
> This was kill(pid, 0) in the original shell code. I am not sure if it is
> important
I think that this is better this way since this should loop until there
is at least one process alive in the process group.
> > + print_msg(TINFO, "Test is still running...");
> > + sleep(1);
> > + }
> > +
> > + if (!kill(-pid, 0)) {
> Here as well
> > + print_msg(TBROK, "Test is still running, sending SIGKILL");
> > + ret = kill(-pid, SIGKILL);
> > + if (ret) {
> > + print_msg(TBROK, "kill() failed");
> > + return 1;
> > + }
> > + }
> > +
> > + return 0;
> > +}
>
> Joerg
--
Cyril Hrubis
chrubis@suse.cz
More information about the ltp
mailing list