From patchwork Tue Feb 18 14:02:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 183597 Delivered-To: patch@linaro.org Received: by 2002:a92:1f12:0:0:0:0:0 with SMTP id i18csp6154256ile; Tue, 18 Feb 2020 06:02:40 -0800 (PST) X-Google-Smtp-Source: APXvYqyAhP8899pR2V2IcbO7McZUnB6S7J3b0h9FStSBfrvQExBC6w28VUotvkU+JqCc7ZH/BFfy X-Received: by 2002:aca:cdd5:: with SMTP id d204mr1292342oig.134.1582034558006; Tue, 18 Feb 2020 06:02:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582034558; cv=none; d=google.com; s=arc-20160816; b=oa0s7VfLAMtcHin7bbl2m5AsK8YezWujaV52jiwrrHHYLLo8pPgO82CPU78N1RWONQ zoBzLna8oEbqsyeE3UOHHsCYSMIasT0IMiphw/MQc+wwh9x1zQR6g0oOLguI8Ldpd21d HP3unPyuGSxxUqx3r/d1n91Sr3yrFDJmPxaehKdn3UOL/UNmNUe8+KkoWRvHqpjL/cy2 mqHWNKcjfmw3avuHn1bVkoUiZLMkoRNFZPnz90tjk9tHEQFUBP0FPdn87Y5E2bLKBNXB hK3Nt26EEbZYsTXmrD/2zSO92bCbsnsykRQvORPvjT/bi9PTuycP85sE8Ek3RZR4/J1r /Gyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:to:from:dkim-signature:delivered-to:sender :list-help:list-post:list-archive:list-subscribe:list-unsubscribe :list-id:precedence:mailing-list:dkim-signature:domainkey-signature; bh=dNnYqpByrbW1fKXlyB83EG//bAX+B2Y8H4W2OeZ2pSE=; b=Hls/OYrDGTgh1bbw7HlMVGbpmHoy6wmylBmBmMwUbtAGlR2k74erruBJ2knAODkAMk iTdyse3wmztGVLmkrsvdBPzYhxvrtnPHQfC6IveDlw5z27tLgnIRTqCRqyalM4hva8Cl ph+punGfTJ7DXapV2g9EaSMtQR4nWEcuxAibwh8/N4CYtwTU8fOH0RrTvnCt6GhG5GLF V82LV9kYevgxFC6YMP/CefRhYlzcnrmIGBeZwSvHwOSu2cFSAzUsh9NImvyrAU6aQwjE FziOXvaN6R9O50eYoeExXKj7QogVDe77N+sTAwLwxEQJrGLNe1LjHRnl49xlBa+I3wDX Mmjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=ngn0rC97; dkim=pass header.i=@linaro.org header.s=google header.b=iOnjZsdT; spf=pass (google.com: domain of libc-alpha-return-109879-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-109879-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id y73si7514828oia.246.2020.02.18.06.02.37 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Feb 2020 06:02:37 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-109879-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=ngn0rC97; dkim=pass header.i=@linaro.org header.s=google header.b=iOnjZsdT; spf=pass (google.com: domain of libc-alpha-return-109879-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-109879-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; q=dns; s= default; b=KX3rdtlRdFw5C1VVfsvK5/QBEbL/fQGoN8tKXZ3NCy1Qgb94s5wgJ hQ9Jh9+o0y5NL947TKG7H31bGCSwzEoSm5T1YqkX05AorRZsom5cvEuQpJBdjIOa 5PAeQn6LCmfKftYN7IPyydFcA2zL16jXROVBEMGfsEJA6ULxyrPlNw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; s=default; bh=ihLC9jkIsGE+Rh25p3WHijNsFxE=; b=ngn0rC972HfLwzaH6Oh7PmIZ14+i Q+bIT7DKL26bX1ktbBKXLD9GRr58a4b9cPwsvGay5mq1cYqut0bM2m8eIzJJLdrW eT5g8JObuj73UtjzwAjiUl/sYhZh8h3wnEVZUn2RmsANpybfgdXc94Va+jS+Z7Vn eQ3yPqIQUc1f3dU= Received: (qmail 58322 invoked by alias); 18 Feb 2020 14:02:18 -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 58304 invoked by uid 89); 18 Feb 2020 14:02:18 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.0 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=STATE, sleeping X-HELO: mail-qv1-f67.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=dNnYqpByrbW1fKXlyB83EG//bAX+B2Y8H4W2OeZ2pSE=; b=iOnjZsdTSRftz0lgXCIIoT1OstLggy/fTjAMQs7j14vG4Htu74gplEK4WpF9gBM0AA kQL2/FBvgqPMJQPC3h0sHW4yScs/0yGLG/JvAETO7vIpwry8hyb4y0RZYCMG/lbBfgAQ vbS6r/7vEen4zz8e6wsu8OowCk7ZmMFlYncDuLvpDEWxeFJfaqcJOzv4ZmuNskf4fhB4 VYD9D4Xrw9LsBFviLyD5EQDqOawt5fNIOxC+GZkZX9czK0IX32SpHfy0ICMZPnJyrrg5 E52fdrbV+YCT5xHbNiU4Z3TNYkaadAmXgFPY2i1o3Owhc3tRhZ4m1ktQ4+peutyWwbkX Wfew== Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2 1/3] support: Add support_process_state_wait Date: Tue, 18 Feb 2020 11:02:03 -0300 Message-Id: <20200218140205.18263-1-adhemerval.zanella@linaro.org> Changes from previous version: - Handle process name with spaces. - Fallback to nanosleep for invalid states. - Set process_states_t as const object. -- 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 limitation (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 waitid tests and is to reduce timeouts by replacing arbitrary waits. --- support/Makefile | 3 + support/process_state.h | 47 +++++++++++++ support/support_process_state.c | 92 ++++++++++++++++++++++++ support/tst-support-process_state.c | 105 ++++++++++++++++++++++++++++ support/xgetline.c | 34 +++++++++ support/xstdio.h | 2 + 6 files changed, 283 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 create mode 100644 support/xgetline.c -- 2.17.1 diff --git a/support/Makefile b/support/Makefile index a0304e6def..6e38b87ebe 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 \ @@ -95,6 +96,7 @@ libsupport-routines = \ xfopen \ xfork \ xftruncate \ + xgetline \ xgetsockname \ xlisten \ xlseek \ @@ -224,6 +226,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..90c4a1eea4 --- /dev/null +++ b/support/process_state.h @@ -0,0 +1,47 @@ +/* Wait for process state. + Copyright (C) 2020 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 support_process_state +{ + support_process_state_running = 0x01, /* R (running). */ + support_process_state_sleeping = 0x02, /* S (sleeping). */ + support_process_state_disk_sleep = 0x04, /* D (disk sleep). */ + support_process_state_stopped = 0x08, /* T (stopped). */ + support_process_state_tracing_stop = 0x10, /* t (tracing stop). */ + support_process_state_dead = 0x20, /* X (dead). */ + support_process_state_zombie = 0x40, /* Z (zombie). */ + support_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. + The timeout POLL_TS might be used to wait before polling the PID status + information from the kernel. A NULL value will set to use a default + value (10000000 ns). + For an invalid state not represented by SUPPORT_PROCESS_STATE, it fallbacks + to a 2 second sleep. */ +void support_process_state_wait (pid_t pid, enum support_process_state state, + struct timespec *poll_ts); + +#endif diff --git a/support/support_process_state.c b/support/support_process_state.c new file mode 100644 index 0000000000..29ba79a5d2 --- /dev/null +++ b/support/support_process_state.c @@ -0,0 +1,92 @@ +/* Wait for process state. + Copyright (C) 2020 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 support_process_state state, + struct timespec *poll_ts) +{ +#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 const struct process_states + { + enum support_process_state s; + char v; + } process_states[] = { + { support_process_state_running, 'R' }, + { support_process_state_sleeping, 'S' }, + { support_process_state_disk_sleep, 'D' }, + { support_process_state_stopped, 'T' }, + { support_process_state_tracing_stop, 't' }, + { support_process_state_dead, 'X' }, + { support_process_state_zombie, 'Z' }, + { support_process_state_parked, 'P' }, + }; + + char spath[sizeof ("/proc/" + 3) * sizeof (pid_t) + sizeof ("/status") + 1]; + snprintf (spath, sizeof (spath), "/proc/%i/status", pid); + + FILE *fstatus = xfopen (spath, "r"); + char *line = NULL; + size_t linesiz = 0; + + for (;;) + { + char cur_state = -1; + while (xgetline (&line, &linesiz, fstatus) != -1) + if (strncmp (line, "State:", strlen ("State:")) == 0) + { + TEST_COMPARE (sscanf (line, "%*s %c", &cur_state), 1); + break; + } + /* Fallback to nanosleep for invalid state. */ + if (cur_state == -1) + break; + + for (size_t i = 0; i < array_length (process_states); ++i) + if (state & process_states[i].s && cur_state == process_states[i].v) + { + free (line); + xfclose (fstatus); + return; + } + + rewind (fstatus); + fflush (fstatus); + + nanosleep (poll_ts ? poll_ts : &(struct timespec) { 0, 10000000 }, NULL); + } + + free (line); + xfclose (fstatus); + /* Fallback to nanosleep if an invalid state is found. */ +#endif + nanosleep (&(struct timespec) { 2, 0 }, NULL); +} diff --git a/support/tst-support-process_state.c b/support/tst-support-process_state.c new file mode 100644 index 0000000000..4f85f571fb --- /dev/null +++ b/support/tst-support-process_state.c @@ -0,0 +1,105 @@ +/* Wait for process state tests. + Copyright (C) 2020 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 + +#ifndef WEXITED +# define WEXITED 0 +#endif + +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 support_process_state stop_state = support_process_state_stopped + | support_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, NULL); + + 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, support_process_state_sleeping, NULL); + + 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, support_process_state_running, NULL); + + 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, support_process_state_zombie, NULL); + + 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 diff --git a/support/xgetline.c b/support/xgetline.c new file mode 100644 index 0000000000..5b010bcf56 --- /dev/null +++ b/support/xgetline.c @@ -0,0 +1,34 @@ +/* fopen with error checking. + Copyright (C) 2016-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 + +ssize_t +xgetline (char **lineptr, size_t *n, FILE *stream) +{ + int old_errno = errno; + errno = 0; + ssize_t ret = getline (lineptr, n, stream); + if (ret == -1 && errno != 0) + FAIL_EXIT1 ("getline failed: %m"); + errno = old_errno; + return ret; +} diff --git a/support/xstdio.h b/support/xstdio.h index b62267a2a2..143957b6a8 100644 --- a/support/xstdio.h +++ b/support/xstdio.h @@ -27,6 +27,8 @@ __BEGIN_DECLS FILE *xfopen (const char *path, const char *mode); void xfclose (FILE *); +ssize_t xgetline (char **lineptr, size_t *n, FILE *stream); + __END_DECLS #endif /* SUPPORT_XSTDIO_H */