|
|
446cf2 |
commit dfe9aa91564c1bf2a23b5589a5db42f9da5d29b5
|
|
|
446cf2 |
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
|
|
|
446cf2 |
Date: Tue Nov 19 17:17:05 2019 -0300
|
|
|
446cf2 |
|
|
|
446cf2 |
support: Add support_process_state_wait
|
|
|
446cf2 |
|
|
|
446cf2 |
It allows parent process to wait for child state using a polling
|
|
|
446cf2 |
strategy over procfs on Linux. The polling is used over ptrace to
|
|
|
446cf2 |
avoid the need to handle signals on the target pid and to handle some
|
|
|
446cf2 |
system specific limitation (such as YAMA).
|
|
|
446cf2 |
|
|
|
446cf2 |
The polling has some limitations, such as resource consumption due
|
|
|
446cf2 |
the procfs read in a loop and the lack of synchronization after the
|
|
|
446cf2 |
state is obtained.
|
|
|
446cf2 |
|
|
|
446cf2 |
The interface idea is to simplify some sleep synchronization waitid
|
|
|
446cf2 |
tests and is to reduce timeouts by replacing arbitrary waits.
|
|
|
446cf2 |
|
|
|
446cf2 |
diff --git a/support/Makefile b/support/Makefile
|
|
|
446cf2 |
index 79d03bd6bfe02540..117cfdd4f22fc405 100644
|
|
|
446cf2 |
--- a/support/Makefile
|
|
|
446cf2 |
+++ b/support/Makefile
|
|
|
446cf2 |
@@ -58,6 +58,7 @@ libsupport-routines = \
|
|
|
446cf2 |
support_format_hostent \
|
|
|
446cf2 |
support_format_netent \
|
|
|
446cf2 |
support_isolate_in_subprocess \
|
|
|
446cf2 |
+ support_process_state \
|
|
|
446cf2 |
support_ptrace \
|
|
|
446cf2 |
support_openpty \
|
|
|
446cf2 |
support_paths \
|
|
|
446cf2 |
@@ -90,6 +91,7 @@ libsupport-routines = \
|
|
|
446cf2 |
xfopen \
|
|
|
446cf2 |
xfork \
|
|
|
446cf2 |
xftruncate \
|
|
|
446cf2 |
+ xgetline \
|
|
|
446cf2 |
xgetsockname \
|
|
|
446cf2 |
xlisten \
|
|
|
446cf2 |
xlseek \
|
|
|
446cf2 |
@@ -217,6 +219,7 @@ tests = \
|
|
|
446cf2 |
tst-support_capture_subprocess \
|
|
|
446cf2 |
tst-support_descriptors \
|
|
|
446cf2 |
tst-support_format_dns_packet \
|
|
|
446cf2 |
+ tst-support-process_state \
|
|
|
446cf2 |
tst-support_quote_blob \
|
|
|
446cf2 |
tst-support_quote_string \
|
|
|
446cf2 |
tst-support_record_failure \
|
|
|
446cf2 |
diff --git a/support/process_state.h b/support/process_state.h
|
|
|
446cf2 |
new file mode 100644
|
|
|
446cf2 |
index 0000000000000000..6c19afdbb7462277
|
|
|
446cf2 |
--- /dev/null
|
|
|
446cf2 |
+++ b/support/process_state.h
|
|
|
446cf2 |
@@ -0,0 +1,43 @@
|
|
|
446cf2 |
+/* Wait for process state.
|
|
|
446cf2 |
+ Copyright (C) 2020 Free Software Foundation, Inc.
|
|
|
446cf2 |
+ This file is part of the GNU C Library.
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
446cf2 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
446cf2 |
+ License as published by the Free Software Foundation; either
|
|
|
446cf2 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
446cf2 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
446cf2 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
446cf2 |
+ Lesser General Public License for more details.
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
446cf2 |
+ License along with the GNU C Library; if not, see
|
|
|
446cf2 |
+ <https://www.gnu.org/licenses/>. */
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+#ifndef SUPPORT_PROCESS_STATE_H
|
|
|
446cf2 |
+#define SUPPORT_PROCESS_STATE_H
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+#include <sys/types.h>
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+enum support_process_state
|
|
|
446cf2 |
+{
|
|
|
446cf2 |
+ support_process_state_running = 0x01, /* R (running). */
|
|
|
446cf2 |
+ support_process_state_sleeping = 0x02, /* S (sleeping). */
|
|
|
446cf2 |
+ support_process_state_disk_sleep = 0x04, /* D (disk sleep). */
|
|
|
446cf2 |
+ support_process_state_stopped = 0x08, /* T (stopped). */
|
|
|
446cf2 |
+ support_process_state_tracing_stop = 0x10, /* t (tracing stop). */
|
|
|
446cf2 |
+ support_process_state_dead = 0x20, /* X (dead). */
|
|
|
446cf2 |
+ support_process_state_zombie = 0x40, /* Z (zombie). */
|
|
|
446cf2 |
+ support_process_state_parked = 0x80, /* P (parked). */
|
|
|
446cf2 |
+};
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+/* Wait for process PID to reach state STATE. It can be a combination of
|
|
|
446cf2 |
+ multiple possible states ('process_state_running | process_state_sleeping')
|
|
|
446cf2 |
+ where the function return when any of these state are observed.
|
|
|
446cf2 |
+ For an invalid state not represented by SUPPORT_PROCESS_STATE, it fallbacks
|
|
|
446cf2 |
+ to a 2 second sleep. */
|
|
|
446cf2 |
+void support_process_state_wait (pid_t pid, enum support_process_state state);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+#endif
|
|
|
446cf2 |
diff --git a/support/support_process_state.c b/support/support_process_state.c
|
|
|
446cf2 |
new file mode 100644
|
|
|
446cf2 |
index 0000000000000000..76dc798728ece0d9
|
|
|
446cf2 |
--- /dev/null
|
|
|
446cf2 |
+++ b/support/support_process_state.c
|
|
|
446cf2 |
@@ -0,0 +1,92 @@
|
|
|
446cf2 |
+/* Wait for process state.
|
|
|
446cf2 |
+ Copyright (C) 2020 Free Software Foundation, Inc.
|
|
|
446cf2 |
+ This file is part of the GNU C Library.
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
446cf2 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
446cf2 |
+ License as published by the Free Software Foundation; either
|
|
|
446cf2 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
446cf2 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
446cf2 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
446cf2 |
+ Lesser General Public License for more details.
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
446cf2 |
+ License along with the GNU C Library; if not, see
|
|
|
446cf2 |
+ <https://www.gnu.org/licenses/>. */
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+#include <stdlib.h>
|
|
|
446cf2 |
+#include <time.h>
|
|
|
446cf2 |
+#include <string.h>
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+#include <array_length.h>
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+#include <support/process_state.h>
|
|
|
446cf2 |
+#include <support/xstdio.h>
|
|
|
446cf2 |
+#include <support/check.h>
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+void
|
|
|
446cf2 |
+support_process_state_wait (pid_t pid, enum support_process_state state)
|
|
|
446cf2 |
+{
|
|
|
446cf2 |
+#ifdef __linux__
|
|
|
446cf2 |
+ /* For Linux it does a polling check on /proc/<pid>/status checking on
|
|
|
446cf2 |
+ third field. */
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ /* It mimics the kernel states from fs/proc/array.c */
|
|
|
446cf2 |
+ static const struct process_states
|
|
|
446cf2 |
+ {
|
|
|
446cf2 |
+ enum support_process_state s;
|
|
|
446cf2 |
+ char v;
|
|
|
446cf2 |
+ } process_states[] = {
|
|
|
446cf2 |
+ { support_process_state_running, 'R' },
|
|
|
446cf2 |
+ { support_process_state_sleeping, 'S' },
|
|
|
446cf2 |
+ { support_process_state_disk_sleep, 'D' },
|
|
|
446cf2 |
+ { support_process_state_stopped, 'T' },
|
|
|
446cf2 |
+ { support_process_state_tracing_stop, 't' },
|
|
|
446cf2 |
+ { support_process_state_dead, 'X' },
|
|
|
446cf2 |
+ { support_process_state_zombie, 'Z' },
|
|
|
446cf2 |
+ { support_process_state_parked, 'P' },
|
|
|
446cf2 |
+ };
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ char spath[sizeof ("/proc/" + 3) * sizeof (pid_t) + sizeof ("/status") + 1];
|
|
|
446cf2 |
+ snprintf (spath, sizeof (spath), "/proc/%i/status", pid);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ FILE *fstatus = xfopen (spath, "r");
|
|
|
446cf2 |
+ char *line = NULL;
|
|
|
446cf2 |
+ size_t linesiz = 0;
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ for (;;)
|
|
|
446cf2 |
+ {
|
|
|
446cf2 |
+ char cur_state = -1;
|
|
|
446cf2 |
+ while (xgetline (&line, &linesiz, fstatus) != -1)
|
|
|
446cf2 |
+ if (strncmp (line, "State:", strlen ("State:")) == 0)
|
|
|
446cf2 |
+ {
|
|
|
446cf2 |
+ TEST_COMPARE (sscanf (line, "%*s %c", &cur_state), 1);
|
|
|
446cf2 |
+ break;
|
|
|
446cf2 |
+ }
|
|
|
446cf2 |
+ /* Fallback to nanosleep for invalid state. */
|
|
|
446cf2 |
+ if (cur_state == -1)
|
|
|
446cf2 |
+ break;
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ for (size_t i = 0; i < array_length (process_states); ++i)
|
|
|
446cf2 |
+ if (state & process_states[i].s && cur_state == process_states[i].v)
|
|
|
446cf2 |
+ {
|
|
|
446cf2 |
+ free (line);
|
|
|
446cf2 |
+ xfclose (fstatus);
|
|
|
446cf2 |
+ return;
|
|
|
446cf2 |
+ }
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ rewind (fstatus);
|
|
|
446cf2 |
+ fflush (fstatus);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ if (nanosleep (&(struct timespec) { 0, 10000000 }, NULL) != 0)
|
|
|
446cf2 |
+ FAIL_EXIT1 ("nanosleep: %m");
|
|
|
446cf2 |
+ }
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ free (line);
|
|
|
446cf2 |
+ xfclose (fstatus);
|
|
|
446cf2 |
+ /* Fallback to nanosleep if an invalid state is found. */
|
|
|
446cf2 |
+#endif
|
|
|
446cf2 |
+ nanosleep (&(struct timespec) { 2, 0 }, NULL);
|
|
|
446cf2 |
+}
|
|
|
446cf2 |
diff --git a/support/tst-support-process_state.c b/support/tst-support-process_state.c
|
|
|
446cf2 |
new file mode 100644
|
|
|
446cf2 |
index 0000000000000000..3fc103ab9205ddb0
|
|
|
446cf2 |
--- /dev/null
|
|
|
446cf2 |
+++ b/support/tst-support-process_state.c
|
|
|
446cf2 |
@@ -0,0 +1,105 @@
|
|
|
446cf2 |
+/* Wait for process state tests.
|
|
|
446cf2 |
+ Copyright (C) 2020 Free Software Foundation, Inc.
|
|
|
446cf2 |
+ This file is part of the GNU C Library.
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
446cf2 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
446cf2 |
+ License as published by the Free Software Foundation; either
|
|
|
446cf2 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
446cf2 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
446cf2 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
446cf2 |
+ Lesser General Public License for more details.
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
446cf2 |
+ License along with the GNU C Library; if not, see
|
|
|
446cf2 |
+ <https://www.gnu.org/licenses/>. */
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+#include <stdio.h>
|
|
|
446cf2 |
+#include <sys/wait.h>
|
|
|
446cf2 |
+#include <unistd.h>
|
|
|
446cf2 |
+#include <errno.h>
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+#include <support/test-driver.h>
|
|
|
446cf2 |
+#include <support/process_state.h>
|
|
|
446cf2 |
+#include <support/check.h>
|
|
|
446cf2 |
+#include <support/xsignal.h>
|
|
|
446cf2 |
+#include <support/xunistd.h>
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+#ifndef WEXITED
|
|
|
446cf2 |
+# define WEXITED 0
|
|
|
446cf2 |
+#endif
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+static void
|
|
|
446cf2 |
+sigusr1_handler (int signo)
|
|
|
446cf2 |
+{
|
|
|
446cf2 |
+}
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+static void
|
|
|
446cf2 |
+test_child (void)
|
|
|
446cf2 |
+{
|
|
|
446cf2 |
+ xsignal (SIGUSR1, sigusr1_handler);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ raise (SIGSTOP);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ TEST_COMPARE (pause (), -1);
|
|
|
446cf2 |
+ TEST_COMPARE (errno, EINTR);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ while (1)
|
|
|
446cf2 |
+ asm ("");
|
|
|
446cf2 |
+}
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+static int
|
|
|
446cf2 |
+do_test (void)
|
|
|
446cf2 |
+{
|
|
|
446cf2 |
+ pid_t pid = xfork ();
|
|
|
446cf2 |
+ if (pid == 0)
|
|
|
446cf2 |
+ {
|
|
|
446cf2 |
+ test_child ();
|
|
|
446cf2 |
+ _exit (127);
|
|
|
446cf2 |
+ }
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ /* Adding process_state_tracing_stop ('t') allows the test to work under
|
|
|
446cf2 |
+ trace programs such as ptrace. */
|
|
|
446cf2 |
+ enum support_process_state stop_state = support_process_state_stopped
|
|
|
446cf2 |
+ | support_process_state_tracing_stop;
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ if (test_verbose)
|
|
|
446cf2 |
+ printf ("info: waiting pid %d, state_stopped/state_tracing_stop\n",
|
|
|
446cf2 |
+ (int) pid);
|
|
|
446cf2 |
+ support_process_state_wait (pid, stop_state);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ if (kill (pid, SIGCONT) != 0)
|
|
|
446cf2 |
+ FAIL_RET ("kill (%d, SIGCONT): %m\n", pid);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ if (test_verbose)
|
|
|
446cf2 |
+ printf ("info: waiting pid %d, state_sleeping\n", (int) pid);
|
|
|
446cf2 |
+ support_process_state_wait (pid, support_process_state_sleeping);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ if (kill (pid, SIGUSR1) != 0)
|
|
|
446cf2 |
+ FAIL_RET ("kill (%d, SIGUSR1): %m\n", pid);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ if (test_verbose)
|
|
|
446cf2 |
+ printf ("info: waiting pid %d, state_running\n", (int) pid);
|
|
|
446cf2 |
+ support_process_state_wait (pid, support_process_state_running);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ if (kill (pid, SIGKILL) != 0)
|
|
|
446cf2 |
+ FAIL_RET ("kill (%d, SIGKILL): %m\n", pid);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ if (test_verbose)
|
|
|
446cf2 |
+ printf ("info: waiting pid %d, state_zombie\n", (int) pid);
|
|
|
446cf2 |
+ support_process_state_wait (pid, support_process_state_zombie);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ siginfo_t info;
|
|
|
446cf2 |
+ int r = waitid (P_PID, pid, &info, WEXITED);
|
|
|
446cf2 |
+ TEST_COMPARE (r, 0);
|
|
|
446cf2 |
+ TEST_COMPARE (info.si_signo, SIGCHLD);
|
|
|
446cf2 |
+ TEST_COMPARE (info.si_code, CLD_KILLED);
|
|
|
446cf2 |
+ TEST_COMPARE (info.si_status, SIGKILL);
|
|
|
446cf2 |
+ TEST_COMPARE (info.si_pid, pid);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ return 0;
|
|
|
446cf2 |
+}
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+#include <support/test-driver.c>
|
|
|
446cf2 |
diff --git a/support/xgetline.c b/support/xgetline.c
|
|
|
446cf2 |
new file mode 100644
|
|
|
446cf2 |
index 0000000000000000..180bc2db95a9c5d4
|
|
|
446cf2 |
--- /dev/null
|
|
|
446cf2 |
+++ b/support/xgetline.c
|
|
|
446cf2 |
@@ -0,0 +1,33 @@
|
|
|
446cf2 |
+/* fopen with error checking.
|
|
|
446cf2 |
+ Copyright (C) 2020 Free Software Foundation, Inc.
|
|
|
446cf2 |
+ This file is part of the GNU C Library.
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
446cf2 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
446cf2 |
+ License as published by the Free Software Foundation; either
|
|
|
446cf2 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
446cf2 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
446cf2 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
446cf2 |
+ Lesser General Public License for more details.
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
446cf2 |
+ License along with the GNU C Library; if not, see
|
|
|
446cf2 |
+ <https://www.gnu.org/licenses/>. */
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+#include <support/xstdio.h>
|
|
|
446cf2 |
+#include <support/check.h>
|
|
|
446cf2 |
+#include <errno.h>
|
|
|
446cf2 |
+
|
|
|
446cf2 |
+ssize_t
|
|
|
446cf2 |
+xgetline (char **lineptr, size_t *n, FILE *stream)
|
|
|
446cf2 |
+{
|
|
|
446cf2 |
+ int old_errno = errno;
|
|
|
446cf2 |
+ errno = 0;
|
|
|
446cf2 |
+ size_t ret = getline (lineptr, n, stream);
|
|
|
446cf2 |
+ if (!feof (stream) && ferror (stream))
|
|
|
446cf2 |
+ FAIL_EXIT1 ("getline failed: %m");
|
|
|
446cf2 |
+ errno = old_errno;
|
|
|
446cf2 |
+ return ret;
|
|
|
446cf2 |
+}
|
|
|
446cf2 |
diff --git a/support/xstdio.h b/support/xstdio.h
|
|
|
446cf2 |
index e7d0274474706380..9446b1f27b0f881e 100644
|
|
|
446cf2 |
--- a/support/xstdio.h
|
|
|
446cf2 |
+++ b/support/xstdio.h
|
|
|
446cf2 |
@@ -27,6 +27,8 @@ __BEGIN_DECLS
|
|
|
446cf2 |
FILE *xfopen (const char *path, const char *mode);
|
|
|
446cf2 |
void xfclose (FILE *);
|
|
|
446cf2 |
|
|
|
446cf2 |
+ssize_t xgetline (char **lineptr, size_t *n, FILE *stream);
|
|
|
446cf2 |
+
|
|
|
446cf2 |
__END_DECLS
|
|
|
446cf2 |
|
|
|
446cf2 |
#endif /* SUPPORT_XSTDIO_H */
|