From patchwork Wed Nov 20 14:15:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 36060 Received: (qmail 19819 invoked by alias); 20 Nov 2019 14:15:45 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 19808 invoked by uid 89); 20 Nov 2019 14:15:44 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=HX-HELO:sk:mail-qk, strategy, sleeping, tracing X-HELO: mail-qk1-f196.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id; bh=tlaTlqCbXE/BUolfKuqTqddRl4M/yT0FZbMcbe4e1ro=; b=EYFAGmSVLAtTHxo0XKNYXo8RhUO0k4XUeyadbSDJ7ZaR8YCrCOVTPauyL/HRBtywax vjRGqyOUIKxZBDCLSXOYu7mLS6AIFRmMKzltdLGLJxIQ+dHQXXqXgRaUDG4jAen/BOsg ao1XFB7rDv4XLBgIbJdoYzMSesaVjYQMBr61rHphUAl7BhGz7Lsq60UczEN+135k8nU6 OXEZ+SOQzpyctbh29/HUTQzRVSKOeCpFtXGHnCIrvD1hHTTVwiZAvoVLmJP6L199pNPP 7rTHUBP8Tgnu1SdPATvFNW6emGVIQ0zVP3dWUPY0q0ZefTZprLMViMRAep+qe3cPu9fC rmFw== Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 1/3] support: Add support_process_state_wait Date: Wed, 20 Nov 2019 11:15:33 -0300 Message-Id: <20191120141535.5303-1-adhemerval.zanella@linaro.org> It allows parent process to wait for children state using a polling strategy over procfs on Linux. The polling is used over ptrace to avoid the need to handle signals on the target pid and to handle some system specific limitations (such as YAMA). The polling has some limitations, such as resource consumption due the procfs read in a loop and the lack of synchronization after the state is obtained. The interface idea is to simplify some sleep synchronization in waitid tests. --- support/Makefile | 2 + support/process_state.h | 41 +++++++++++ support/support_process_state.c | 89 ++++++++++++++++++++++++ support/tst-support-process_state.c | 101 ++++++++++++++++++++++++++++ 4 files changed, 233 insertions(+) create mode 100644 support/process_state.h create mode 100644 support/support_process_state.c create mode 100644 support/tst-support-process_state.c diff --git a/support/Makefile b/support/Makefile index 23c6d74627..b0d28200af 100644 --- a/support/Makefile +++ b/support/Makefile @@ -56,6 +56,7 @@ libsupport-routines = \ support_format_hostent \ support_format_netent \ support_isolate_in_subprocess \ + support_process_state \ support_ptrace \ support_openpty \ support_paths \ @@ -223,6 +224,7 @@ tests = \ tst-support_capture_subprocess \ tst-support_descriptors \ tst-support_format_dns_packet \ + tst-support-process_state \ tst-support_quote_blob \ tst-support_quote_string \ tst-support_record_failure \ diff --git a/support/process_state.h b/support/process_state.h new file mode 100644 index 0000000000..d616f0e3e6 --- /dev/null +++ b/support/process_state.h @@ -0,0 +1,41 @@ +/* Wait for process state. + Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef SUPPORT_PROCESS_STATE_H +#define SUPPORT_PROCESS_STATE_H + +#include + +enum process_state_t +{ + process_state_running = 0x01, /* R (running) */ + process_state_sleeping = 0x02, /* S (sleeping) */ + process_state_disk_sleep = 0x04, /* D (disk sleep) */ + process_state_stopped = 0x08, /* T (stopped) */ + process_state_tracing_stop = 0x10, /* t (tracing stop) */ + process_state_dead = 0x20, /* X (dead) */ + process_state_zombie = 0x40, /* Z (zombie) */ + process_state_parked = 0x80, /* P (parked) */ +}; + +/* Wait for process PID to reach state STATE. It can be a combination of + multiple possible states ('process_state_running | process_state_sleeping') + where the function return when any of these state are observed. */ +void support_process_state_wait (pid_t pid, enum process_state_t state); + +#endif diff --git a/support/support_process_state.c b/support/support_process_state.c new file mode 100644 index 0000000000..77c684c49c --- /dev/null +++ b/support/support_process_state.c @@ -0,0 +1,89 @@ +/* Wait for process state. + Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +#include + +#include +#include +#include + +void +support_process_state_wait (pid_t pid, enum process_state_t state) +{ +#ifdef __linux__ + /* For Linux it does a polling check on /proc//status checking on + third field. */ + + /* It mimics the kernel states from fs/proc/array.c */ + static struct process_states_t + { + enum process_state_t s; + char v; + } process_states[] = { + { process_state_running, 'R' }, + { process_state_sleeping, 'S' }, + { process_state_disk_sleep, 'D' }, + { process_state_stopped, 'T' }, + { process_state_tracing_stop, 't' }, + { process_state_dead, 'X' }, + { process_state_zombie, 'Z' }, + { process_state_parked, 'P' }, + }; + + char proc_path[sizeof ("/proc/" + 3) * sizeof (pid_t) + sizeof ("/stat") + + 1]; + snprintf (proc_path, sizeof (proc_path), "/proc/%i/stat", pid); + + int fd = xopen (proc_path, O_RDONLY, 0600); + + char statbuf[3 * sizeof (pid_t) /* pid. */ + + 2 /* ' (' */ + + 64 /* task name. */ + + 2 /* ' )' */ + + 1 /* , 1 char. */ + + 1]; /* Final \0. */ + for (;;) + { + ssize_t ret = read (fd, statbuf, sizeof (statbuf) - 1); + TEST_VERIFY (ret >= 0); + statbuf[ret] = '\0'; + + char cur_state; + int sret = sscanf (statbuf, "%*i %*s %c", &cur_state); + TEST_VERIFY (sret == 1); + + for (size_t i = 0; i < array_length (process_states); ++i) + if (state & process_states[i].s && cur_state == process_states[i].v) + { + close (fd); + return; + } + + xlseek (fd, 0, SEEK_SET); + nanosleep (&(struct timespec) { 0, 10000000 }, NULL); + } + + close (fd); +#else + sleep (2); +#endif +} diff --git a/support/tst-support-process_state.c b/support/tst-support-process_state.c new file mode 100644 index 0000000000..3482f12fa5 --- /dev/null +++ b/support/tst-support-process_state.c @@ -0,0 +1,101 @@ +/* Wait for process state tests. + Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +static void +sigusr1_handler (int signo) +{ +} + +static void +test_child (void) +{ + xsignal (SIGUSR1, sigusr1_handler); + + raise (SIGSTOP); + + TEST_COMPARE (pause (), -1); + TEST_COMPARE (errno, EINTR); + + while (1) + asm (""); +} + +static int +do_test (void) +{ + pid_t pid = xfork (); + if (pid == 0) + { + test_child (); + _exit (127); + } + + /* Adding process_state_tracing_stop ('t') allows the test to work under + trace programs such as ptrace. */ + enum process_state_t stop_state = process_state_stopped + | process_state_tracing_stop; + + if (test_verbose) + printf ("info: waiting pid %d, state_stopped/state_tracing_stop\n", + (int) pid); + support_process_state_wait (pid, stop_state); + + if (kill (pid, SIGCONT) != 0) + FAIL_RET ("kill (%d, SIGCONT): %m\n", pid); + + if (test_verbose) + printf ("info: waiting pid %d, state_sleeping\n", (int) pid); + support_process_state_wait (pid, process_state_sleeping); + + if (kill (pid, SIGUSR1) != 0) + FAIL_RET ("kill (%d, SIGUSR1): %m\n", pid); + + if (test_verbose) + printf ("info: waiting pid %d, state_running\n", (int) pid); + support_process_state_wait (pid, process_state_running); + + if (kill (pid, SIGKILL) != 0) + FAIL_RET ("kill (%d, SIGKILL): %m\n", pid); + + if (test_verbose) + printf ("info: waiting pid %d, state_zombie\n", (int) pid); + support_process_state_wait (pid, process_state_zombie); + + siginfo_t info; + int r = waitid (P_PID, pid, &info, WEXITED); + TEST_COMPARE (r, 0); + TEST_COMPARE (info.si_signo, SIGCHLD); + TEST_COMPARE (info.si_code, CLD_KILLED); + TEST_COMPARE (info.si_status, SIGKILL); + TEST_COMPARE (info.si_pid, pid); + + return 0; +} + +#include