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