<div dir="ltr"><div class="gmail_quote"><div><div class="gmail_default" style="font-size:small">Hi Cyril,</div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+++ b/testcases/lib/<span class="gmail_default" style="font-size:small"></span>tst_timeout_kill.c<br>
@@ -0,0 +1,75 @@<br>
+// SPDX-License-Identifier: GPL-2.0-or-later<br>
+/*<br>
+ * Copyright (c) 2021 Cyril Hrubis <<a href="mailto:chrubis@suse.cz" target="_blank">chrubis@suse.cz</a>><br>
+ */<br>
+<br>
+#include <stdio.h><br>
+#include <signal.h><br>
+#include <stdlib.h><br>
+#include <unistd.h><br>
+#include <errno.h><br>
+#include <string.h><br>
+<br>
+static void print_help(const char *name)<br>
+{<br>
+       fprintf(stderr, "usage: %s timeout pid\n", name);<br>
+}<br>
+<br>
+#define print_msg(fmt, ...) fprintf(stderr, fmt "\n", ##__VA_ARGS__)<br>
+<br>
+int main(int argc, char *argv[])<br>
+{<br>
+       int timeout, pid, ret, i;<br>
+<br>
+       if (argc != 3) {<br>
+               print_help(argv[0]);<br>
+               return 1;<br>
+       }<br>
+<br>
+       timeout = atoi(argv[1]);<br>
+       pid = atoi(argv[2]);<br>
+<br>
+       if (timeout < 0) {<br>
+               fprintf(stderr, "Invalid timeout '%s'\n", argv[1]);<br>
+               print_help(argv[0]);<br>
+               return 1;<br>
+       }<br>
+<br>
+       if (pid <= 1) {<br>
+               fprintf(stderr, "Invalid pid '%s'\n", argv[2]);<br>
+               print_help(argv[0]);<br>
+               return 1;<br>
+       }<br>
+<br>
+       if (timeout)<br>
+               sleep(timeout);<br>
+<br>
+       print_msg("Test timed out, sending SIGTERM!");<br>
+       print_msg("If you are running on slow machine, try exporting LTP_TIMEOUT_MUL > 1");<br>
+<br>
+       ret = kill(-pid, SIGTERM);<br></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">This 'pid' is the parent shell process id, so it obviously that <span class="gmail_default"></span>tst_timeout_kill</div><div class="gmail_default" style="font-size:small">process would get signal SIGTERM as well.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">I'm thinking maybe we should let <span class="gmail_default"></span>tst_timeout_kill itself ignore SIGTERM</div><div class="gmail_default" style="font-size:small">otherwise we have no chance to perform the following double-check code?</div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+       if (ret) {<br>
+               print_msg("kill(%i) failed: %s", -pid, strerror(errno));<br>
+               return 1;<br>
+       }<br>
+<br>
+       usleep(100000);<br>
+<br>
+       i = 10;<br>
+<br>
+       while (!kill(-pid, 0) && i-- > 0) {<br></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">And just check 'pid' but not '-pid' as original shell behavior?</div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+               print_msg("Test is still running...");<br>
+               sleep(1);<br>
+       }<br>
+<br>
+       if (!kill(-pid, 0)) {<br></blockquote><div><span class="gmail_default" style="font-size:small"><br></span></div><div><span class="gmail_default" style="font-size:small">check pid, if so like that.</span></div><div><span class="gmail_default" style="font-size:small"></span> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+               print_msg("Test is still running, sending SIGKILL");<br>
+               ret = kill(-pid, SIGKILL);<br>
+               if (ret) {<br>
+                       print_msg("kill(%i) failed: %s", -pid, strerror(errno));<br>
+                       return 1;<br>
+               }<br>
+       }<br>
+<br>
+       return 0;<br>
+}<br>
-- <br>
2.32.0<br>
<br>
<br>
-- <br>
Mailing list info: <a href="https://lists.linux.it/listinfo/ltp" rel="noreferrer" target="_blank">https://lists.linux.it/listinfo/ltp</a><br>
<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div>Regards,<br></div><div>Li Wang<br></div></div></div></div>