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 */ From patchwork Tue Feb 18 14:02:04 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: 183598 Delivered-To: patch@linaro.org Received: by 2002:a92:1f12:0:0:0:0:0 with SMTP id i18csp6154534ile; Tue, 18 Feb 2020 06:02:51 -0800 (PST) X-Google-Smtp-Source: APXvYqzI4riRj9m7fNdHjRP4shopGSOoqFpmbafC27V9z2mm9LSQUmw3ua749H7v6Yx7STIKBSQ2 X-Received: by 2002:a05:6830:22e2:: with SMTP id t2mr16192987otc.129.1582034571433; Tue, 18 Feb 2020 06:02:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582034571; cv=none; d=google.com; s=arc-20160816; b=m9cdMC7JXCwfluMy1/C0aEYWU1KoJ1XgGDI2i7IY28gIlx0ALokc7kR597IZDNO6mH eUJFXzoCJVwoxRzPM0ufNAiKR4YG6X16KPNpR1kE/ixCCHXptV8nPyqOcd6tiPABliyE +oIzoxfWYUG38x6AwBNTVz0xeCaXJhJ6waH69KBhnnfjrckmrLqPlEG7G4EDhik4TUaI Cg+ZqzI1gJY8hMp6AhkDIjQf8uEl6r3xtbXkh5Rog+72lM+qbNW2ox1n6BhCJxvWC7he +x8mJFaEyvgdk1vQWYIz0AKPoy/6uLos8LowCgjMrPuh+g2yHOXivtJV7Ct+/Z5FeAX2 6m5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to: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=8v4matkkl2xNCzGnXOjVnZ9R+czKwVvdV5sV3QiBX3Y=; b=ibC+Z0KMqS5tkCcGRqJh9sS0eSO2OrKsu+mMrkzd9SeGgZ4Kj77u6oSE9oZXhr8Ecs oHIXOzI1hhtySij3AKDPkwpYo1U/d/wxOpDSH4t5vQemYO6Dc9Hl8myMyJ9/3s66Qgn9 9pkPtA4EhR0A29gaWYJspBrWtyvmwtpIswtFtRvXvafaE4RFQuxglBBeAqspNcyg0kYl B24iOpddj1BJqVitt5g3Flbe9+TrzELLqKH3W6ehjWrRSQb0hP7HYhKJPynnWKgRJPBy 0G8VmCL1SrLBu8SM3D6LwUPcIETXv/KW9DNUHb0k57y0JJALGTYAn5DemJXALrtGiszH jzZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=SV0hrAnk; dkim=pass header.i=@linaro.org header.s=google header.b=vhJH0v1a; spf=pass (google.com: domain of libc-alpha-return-109880-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-109880-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 s20si7933459oie.210.2020.02.18.06.02.51 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Feb 2020 06:02:51 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-109880-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=SV0hrAnk; dkim=pass header.i=@linaro.org header.s=google header.b=vhJH0v1a; spf=pass (google.com: domain of libc-alpha-return-109880-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-109880-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:in-reply-to :references; q=dns; s=default; b=U9D6BT7KTyHkrj7CmXASHlbqixN+aYY 8T5cEpY3GdB5fbYbZ/xXIYN+Kj2EOVvr5lGVFUCtJhi+e+EgJ+ti5jkX/mSod4+T i8eOhsRWE9hntWScyVoscX1ZN4obfyxgpAgxzURUSE3zAM9deM7hOtCaO6Nnom5U g8A2L8X8Lb+8= 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:in-reply-to :references; s=default; bh=UGLknzIXm/ZBAIdM2D2TxF2p2lw=; b=SV0hr Ank5neohzSMVzaLn/NBvEUAA2ypCZlnyDbSZx74MtbfZ1UFlz8RTarfBSmygTfmD 7HStqIBHmFBU3VRwDX5zh3r/NZitG566PxqxIZwGLM/zVwjEe0Ycxxj+IBzeYf9m wO+MPKrGtcCnrZmY6aG2XigvHyMYl5aYVtL1zg= Received: (qmail 58492 invoked by alias); 18 Feb 2020 14:02:19 -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 58328 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, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=succeed, Die, him X-HELO: mail-qv1-f65.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references; bh=8v4matkkl2xNCzGnXOjVnZ9R+czKwVvdV5sV3QiBX3Y=; b=vhJH0v1auWIAihKbwtW6Skzq9VLiUO3hTsc7dA4QZ61LK3OBfcYQHWfoVRuCO/IMMy RCEr2G7H9fkv9BxzhL9/9Y1lqmmgvT2Pfsk+yJkcWqlY3EOaIxPlUcffdN04YerfRomn dfi/Dkrhg74OTHf5jwmfLoKlFcWkyJyh9lMhOYxY2CMzlw4OdvygQLUmF8OCzV65XSNz Z4Qc36ktlbLJ+8YwCRBoabdPnE2/9hYniY5J2X3+FE5YMV/elVnKJyv1ARAF457xn6hl vo/aDV1TSkm0Ta3IZkExY6tg5rMJ69IUrFXT3j+Kcx3YkqntvZ/fv0FkFg3q5WRQov2V CArQ== Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2 2/3] posix: Refactor tst-waitid (BZ #14666) Date: Tue, 18 Feb 2020 11:02:04 -0300 Message-Id: <20200218140205.18263-2-adhemerval.zanella@linaro.org> In-Reply-To: <20200218140205.18263-1-adhemerval.zanella@linaro.org> References: <20200218140205.18263-1-adhemerval.zanella@linaro.org> Changes from previous version: - Use sigwaitinfo instead of global variables set by the signal handler. -- The main changes are: - Adapt to libsupport. - Synchronize the signal handler using atomics. - Replace waitpid by waitid calls. - Use support_process_state_wait to wait for child state. - Add tests for P_PGID and P_ALL. - Use sigwaitinfo instead of global state set by the signal handler. Checked on x86_64-linux-gnu and i686-linux-gnu. --- posix/tst-waitid.c | 561 ++++++++++++++------------------------------- 1 file changed, 166 insertions(+), 395 deletions(-) -- 2.17.1 diff --git a/posix/tst-waitid.c b/posix/tst-waitid.c index c4725489ea..2331c42e66 100644 --- a/posix/tst-waitid.c +++ b/posix/tst-waitid.c @@ -22,13 +22,20 @@ #include #include #include +#include +#include +#include + +#include +#include +#include +#include static void -test_child (void) +test_child (bool setgroup) { - /* Wait a second to be sure the parent set his variables before we - produce a SIGCHLD. */ - sleep (1); + if (setgroup) + TEST_COMPARE (setpgid (0, 0), 0); /* First thing, we stop ourselves. */ raise (SIGSTOP); @@ -44,475 +51,239 @@ test_child (void) # define WSTOPPED WUNTRACED #endif -static sig_atomic_t expecting_sigchld, spurious_sigchld; -#ifdef SA_SIGINFO -static siginfo_t sigchld_info; +/* Set with only SIGCHLD on do_test_waitid. */ +static sigset_t chldset; +#ifdef SA_SIGINFO static void sigchld (int signo, siginfo_t *info, void *ctx) { - if (signo != SIGCHLD) - { - printf ("SIGCHLD handler got signal %d instead!\n", signo); - _exit (EXIT_FAILURE); - } - - if (! expecting_sigchld) - { - spurious_sigchld = 1; - printf ("spurious SIGCHLD: signo %d code %d status %d pid %d\n", - info->si_signo, info->si_code, info->si_status, info->si_pid); - } - else - { - sigchld_info = *info; - expecting_sigchld = 0; - } } +#endif static void -check_sigchld (const char *phase, int *ok, int code, int status, pid_t pid) +check_sigchld (int code, int status, pid_t pid) { - if (expecting_sigchld) - { - printf ("missing SIGCHLD on %s\n", phase); - *ok = EXIT_FAILURE; - expecting_sigchld = 0; - return; - } +#ifdef SA_SIGINFO + siginfo_t siginfo; + TEST_COMPARE (sigwaitinfo (&chldset, &siginfo), SIGCHLD); - if (sigchld_info.si_signo != SIGCHLD) - { - printf ("SIGCHLD for %s signal %d\n", phase, sigchld_info.si_signo); - *ok = EXIT_FAILURE; - } - if (sigchld_info.si_code != code) - { - printf ("SIGCHLD for %s code %d\n", phase, sigchld_info.si_code); - *ok = EXIT_FAILURE; - } - if (sigchld_info.si_status != status) - { - printf ("SIGCHLD for %s status %d\n", phase, sigchld_info.si_status); - *ok = EXIT_FAILURE; - } - if (sigchld_info.si_pid != pid) - { - printf ("SIGCHLD for %s pid %d\n", phase, sigchld_info.si_pid); - *ok = EXIT_FAILURE; - } -} -# define CHECK_SIGCHLD(phase, code_check, status_check) \ - check_sigchld ((phase), &status, (code_check), (status_check), pid) -#else -# define CHECK_SIGCHLD(phase, code, status) ((void) 0) + TEST_COMPARE (siginfo.si_signo, SIGCHLD); + TEST_COMPARE (siginfo.si_code, code); + TEST_COMPARE (siginfo.si_status, status); + TEST_COMPARE (siginfo.si_pid, pid); #endif +} static int -do_test (int argc, char *argv[]) +do_test_waitd_common (idtype_t type, pid_t pid) { -#ifdef SA_SIGINFO - struct sigaction sa; - sa.sa_flags = SA_SIGINFO|SA_RESTART; - sa.sa_sigaction = &sigchld; - if (sigemptyset (&sa.sa_mask) < 0 || sigaction (SIGCHLD, &sa, NULL) < 0) - { - printf ("setting SIGCHLD handler: %m\n"); - return EXIT_FAILURE; - } -#endif + /* 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; - expecting_sigchld = 1; + support_process_state_wait (pid, stop_state); - pid_t pid = fork (); - if (pid < 0) - { - printf ("fork: %m\n"); - return EXIT_FAILURE; - } - else if (pid == 0) - { - test_child (); - _exit (127); - } - - int status = EXIT_SUCCESS; -#define RETURN(ok) \ - do { if (status == EXIT_SUCCESS) status = (ok); goto out; } while (0) - - /* Give the child a chance to stop. */ - sleep (3); - - CHECK_SIGCHLD ("stopped (before waitid)", CLD_STOPPED, SIGSTOP); + check_sigchld (CLD_STOPPED, SIGSTOP, pid); /* Now try a wait that should not succeed. */ siginfo_t info; + int fail; + info.si_signo = 0; /* A successful call sets it to SIGCHLD. */ - int fail = waitid (P_PID, pid, &info, WEXITED|WCONTINUED|WNOHANG); - switch (fail) - { - default: - printf ("waitid returned bogus value %d\n", fail); - RETURN (EXIT_FAILURE); - case -1: - printf ("waitid WNOHANG on stopped: %m\n"); - RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE); - case 0: - if (info.si_signo == 0) - break; - if (info.si_signo == SIGCHLD) - printf ("waitid WNOHANG on stopped status %d\n", info.si_status); - else - printf ("waitid WNOHANG on stopped signal %d\n", info.si_signo); - RETURN (EXIT_FAILURE); - } + fail = waitid (P_PID, pid, &info, WEXITED|WCONTINUED|WNOHANG); + if (fail == -1 && errno == ENOTSUP) + FAIL_RET ("waitid WNOHANG on stopped: %m"); + TEST_COMPARE (fail, 0); + TEST_COMPARE (info.si_signo, 0); /* Next the wait that should succeed right away. */ info.si_signo = 0; /* A successful call sets it to SIGCHLD. */ info.si_pid = -1; info.si_status = -1; fail = waitid (P_PID, pid, &info, WSTOPPED|WNOHANG); - switch (fail) - { - default: - printf ("waitid WSTOPPED|WNOHANG returned bogus value %d\n", fail); - RETURN (EXIT_FAILURE); - case -1: - printf ("waitid WSTOPPED|WNOHANG on stopped: %m\n"); - RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE); - case 0: - if (info.si_signo != SIGCHLD) - { - printf ("waitid WSTOPPED|WNOHANG on stopped signal %d\n", - info.si_signo); - RETURN (EXIT_FAILURE); - } - if (info.si_code != CLD_STOPPED) - { - printf ("waitid WSTOPPED|WNOHANG on stopped code %d\n", - info.si_code); - RETURN (EXIT_FAILURE); - } - if (info.si_status != SIGSTOP) - { - printf ("waitid WSTOPPED|WNOHANG on stopped status %d\n", - info.si_status); - RETURN (EXIT_FAILURE); - } - if (info.si_pid != pid) - { - printf ("waitid WSTOPPED|WNOHANG on stopped pid %d != %d\n", - info.si_pid, pid); - RETURN (EXIT_FAILURE); - } - } + if (fail == -1 && errno == ENOTSUP) + FAIL_RET ("waitid WNOHANG on stopped: %m"); + TEST_COMPARE (fail, 0); + TEST_COMPARE (info.si_signo, SIGCHLD); + TEST_COMPARE (info.si_code, CLD_STOPPED); + TEST_COMPARE (info.si_status, SIGSTOP); + TEST_COMPARE (info.si_pid, pid); - expecting_sigchld = WCONTINUED != 0; + //expecting_sigchld = WCONTINUED != 0; if (kill (pid, SIGCONT) != 0) - { - printf ("kill (%d, SIGCONT): %m\n", pid); - RETURN (EXIT_FAILURE); - } + FAIL_RET ("kill (%d, SIGCONT): %m\n", pid); /* Wait for the child to have continued. */ - sleep (2); + support_process_state_wait (pid, process_state_sleeping); #if WCONTINUED != 0 - if (expecting_sigchld) - { - printf ("no SIGCHLD seen for SIGCONT (optional)\n"); - expecting_sigchld = 0; - } - else - CHECK_SIGCHLD ("continued (before waitid)", CLD_CONTINUED, SIGCONT); + { + sigset_t set; + sigemptyset (&set); + sigaddset (&set, SIGCHLD); + siginfo_t info; + TEST_COMPARE (sigwaitinfo (&set, &info), SIGCHLD); + + TEST_COMPARE (info.si_signo, SIGCHLD); + TEST_COMPARE (info.si_code, CLD_CONTINUED); + TEST_COMPARE (info.si_status, SIGCONT); + TEST_COMPARE (info.si_pid, pid); + } info.si_signo = 0; /* A successful call sets it to SIGCHLD. */ info.si_pid = -1; info.si_status = -1; fail = waitid (P_PID, pid, &info, WCONTINUED|WNOWAIT); - switch (fail) - { - default: - printf ("waitid WCONTINUED|WNOWAIT returned bogus value %d\n", fail); - RETURN (EXIT_FAILURE); - case -1: - printf ("waitid WCONTINUED|WNOWAIT on continued: %m\n"); - RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE); - case 0: - if (info.si_signo != SIGCHLD) - { - printf ("waitid WCONTINUED|WNOWAIT on continued signal %d\n", - info.si_signo); - RETURN (EXIT_FAILURE); - } - if (info.si_code != CLD_CONTINUED) - { - printf ("waitid WCONTINUED|WNOWAIT on continued code %d\n", - info.si_code); - RETURN (EXIT_FAILURE); - } - if (info.si_status != SIGCONT) - { - printf ("waitid WCONTINUED|WNOWAIT on continued status %d\n", - info.si_status); - RETURN (EXIT_FAILURE); - } - if (info.si_pid != pid) - { - printf ("waitid WCONTINUED|WNOWAIT on continued pid %d != %d\n", - info.si_pid, pid); - RETURN (EXIT_FAILURE); - } - } + if (fail == -1 && errno == ENOTSUP) + FAIL_RET ("waitid WCONTINUED|WNOWAIT on continued: %m"); + TEST_COMPARE (fail, 0); + TEST_COMPARE (info.si_signo, SIGCHLD); + TEST_COMPARE (info.si_code, CLD_CONTINUED); + TEST_COMPARE (info.si_status, SIGCONT); + TEST_COMPARE (info.si_pid, pid); /* That should leave the CLD_CONTINUED state waiting to be seen again. */ info.si_signo = 0; /* A successful call sets it to SIGCHLD. */ info.si_pid = -1; info.si_status = -1; fail = waitid (P_PID, pid, &info, WCONTINUED); - switch (fail) - { - default: - printf ("waitid WCONTINUED returned bogus value %d\n", fail); - RETURN (EXIT_FAILURE); - case -1: - printf ("waitid WCONTINUED on continued: %m\n"); - RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE); - case 0: - if (info.si_signo != SIGCHLD) - { - printf ("waitid WCONTINUED on continued signal %d\n", info.si_signo); - RETURN (EXIT_FAILURE); - } - if (info.si_code != CLD_CONTINUED) - { - printf ("waitid WCONTINUED on continued code %d\n", info.si_code); - RETURN (EXIT_FAILURE); - } - if (info.si_status != SIGCONT) - { - printf ("waitid WCONTINUED on continued status %d\n", - info.si_status); - RETURN (EXIT_FAILURE); - } - if (info.si_pid != pid) - { - printf ("waitid WCONTINUED on continued pid %d != %d\n", - info.si_pid, pid); - RETURN (EXIT_FAILURE); - } - } + if (fail == -1 && errno == ENOTSUP) + FAIL_RET ("waitid WCONTINUED on continued: %m"); + TEST_COMPARE (fail, 0); + TEST_COMPARE (info.si_signo, SIGCHLD); + TEST_COMPARE (info.si_code, CLD_CONTINUED); + TEST_COMPARE (info.si_status, SIGCONT); + TEST_COMPARE (info.si_pid, pid); /* Now try a wait that should not succeed. */ info.si_signo = 0; /* A successful call sets it to SIGCHLD. */ fail = waitid (P_PID, pid, &info, WCONTINUED|WNOHANG); - switch (fail) - { - default: - printf ("waitid returned bogus value %d\n", fail); - RETURN (EXIT_FAILURE); - case -1: - printf ("waitid WCONTINUED|WNOHANG on waited continued: %m\n"); - RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE); - case 0: - if (info.si_signo == 0) - break; - if (info.si_signo == SIGCHLD) - printf ("waitid WCONTINUED|WNOHANG on waited continued status %d\n", - info.si_status); - else - printf ("waitid WCONTINUED|WNOHANG on waited continued signal %d\n", - info.si_signo); - RETURN (EXIT_FAILURE); - } + if (fail == -1 && errno == ENOTSUP) + FAIL_RET ("waitid WCONTINUED|WNOHANG on waited continued: %m"); + TEST_COMPARE (fail, 0); + TEST_COMPARE (info.si_signo, 0); /* Now stop him again and test waitpid with WCONTINUED. */ - expecting_sigchld = 1; + //expecting_sigchld = 1; if (kill (pid, SIGSTOP) != 0) - { - printf ("kill (%d, SIGSTOP): %m\n", pid); - RETURN (EXIT_FAILURE); - } + FAIL_RET ("kill (%d, SIGSTOP): %m\n", pid); - /* Give the child a chance to stop. The waitpid call below will block - until it has stopped, but if we are real quick and enter the waitpid - system call before the SIGCHLD has been generated, then it will be - discarded and never delivered. */ - sleep (3); + /* Wait the child stop. The waitid call below will block until it has + stopped, but if we are real quick and enter the waitid system call + before the SIGCHLD has been generated, then it will be discarded and + never delivered. */ + support_process_state_wait (pid, stop_state); - pid_t wpid = waitpid (pid, &fail, WUNTRACED); - if (wpid < 0) - { - printf ("waitpid WUNTRACED on stopped: %m\n"); - RETURN (EXIT_FAILURE); - } - else if (wpid != pid) - { - printf ("waitpid WUNTRACED on stopped returned %d != %d (status %x)\n", - wpid, pid, fail); - RETURN (EXIT_FAILURE); - } - else if (!WIFSTOPPED (fail) || WIFSIGNALED (fail) || WIFEXITED (fail) - || WIFCONTINUED (fail) || WSTOPSIG (fail) != SIGSTOP) - { - printf ("waitpid WUNTRACED on stopped: status %x\n", fail); - RETURN (EXIT_FAILURE); - } - CHECK_SIGCHLD ("stopped (after waitpid)", CLD_STOPPED, SIGSTOP); + fail = waitid (type, pid, &info, WEXITED|WSTOPPED); + TEST_COMPARE (fail, 0); + TEST_COMPARE (info.si_signo, SIGCHLD); + TEST_COMPARE (info.si_code, CLD_STOPPED); + TEST_COMPARE (info.si_status, SIGSTOP); + TEST_COMPARE (info.si_pid, pid); + + check_sigchld (CLD_STOPPED, SIGSTOP, pid); - expecting_sigchld = 1; if (kill (pid, SIGCONT) != 0) - { - printf ("kill (%d, SIGCONT): %m\n", pid); - RETURN (EXIT_FAILURE); - } + FAIL_RET ("kill (%d, SIGCONT): %m\n", pid); /* Wait for the child to have continued. */ - sleep (2); + support_process_state_wait (pid, process_state_sleeping); - if (expecting_sigchld) - { - printf ("no SIGCHLD seen for SIGCONT (optional)\n"); - expecting_sigchld = 0; - } - else - CHECK_SIGCHLD ("continued (before waitpid)", CLD_CONTINUED, SIGCONT); + check_sigchld (CLD_CONTINUED, SIGCONT, pid); - wpid = waitpid (pid, &fail, WCONTINUED); - if (wpid < 0) - { - if (errno == EINVAL) - printf ("waitpid does not support WCONTINUED\n"); - else - { - printf ("waitpid WCONTINUED on continued: %m\n"); - RETURN (EXIT_FAILURE); - } - } - else if (wpid != pid) - { - printf ("\ -waitpid WCONTINUED on continued returned %d != %d (status %x)\n", - wpid, pid, fail); - RETURN (EXIT_FAILURE); - } - else if (WIFSTOPPED (fail) || WIFSIGNALED (fail) || WIFEXITED (fail) - || !WIFCONTINUED (fail)) - { - printf ("waitpid WCONTINUED on continued: status %x\n", fail); - RETURN (EXIT_FAILURE); - } + fail = waitid (type, pid, &info, WCONTINUED); + TEST_COMPARE (fail, 0); + TEST_COMPARE (info.si_signo, SIGCHLD); + TEST_COMPARE (info.si_code, CLD_CONTINUED); + TEST_COMPARE (info.si_status, SIGCONT); + TEST_COMPARE (info.si_pid, pid); #endif - expecting_sigchld = 1; - /* Die, child, die! */ if (kill (pid, SIGKILL) != 0) - { - printf ("kill (%d, SIGKILL): %m\n", pid); - RETURN (EXIT_FAILURE); - } + FAIL_RET ("kill (%d, SIGKILL): %m\n", pid); #ifdef WNOWAIT info.si_signo = 0; /* A successful call sets it to SIGCHLD. */ info.si_pid = -1; info.si_status = -1; - fail = waitid (P_PID, pid, &info, WEXITED|WNOWAIT); - switch (fail) - { - default: - printf ("waitid WNOWAIT returned bogus value %d\n", fail); - RETURN (EXIT_FAILURE); - case -1: - printf ("waitid WNOWAIT on killed: %m\n"); - RETURN (errno == ENOTSUP ? EXIT_SUCCESS : EXIT_FAILURE); - case 0: - if (info.si_signo != SIGCHLD) - { - printf ("waitid WNOWAIT on killed signal %d\n", info.si_signo); - RETURN (EXIT_FAILURE); - } - if (info.si_code != CLD_KILLED) - { - printf ("waitid WNOWAIT on killed code %d\n", info.si_code); - RETURN (EXIT_FAILURE); - } - if (info.si_status != SIGKILL) - { - printf ("waitid WNOWAIT on killed status %d\n", info.si_status); - RETURN (EXIT_FAILURE); - } - if (info.si_pid != pid) - { - printf ("waitid WNOWAIT on killed pid %d != %d\n", info.si_pid, pid); - RETURN (EXIT_FAILURE); - } - } + fail = waitid (type, pid, &info, WEXITED|WNOWAIT); + if (fail == -1 && errno == ENOTSUP) + FAIL_RET ("waitid WNOHANG on killed: %m"); + TEST_COMPARE (fail, 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); #else - /* Allow enough time to be sure the child died; we didn't synchronize. */ - sleep (2); + support_process_state_wait (pid, process_state_zombie); #endif - - CHECK_SIGCHLD ("killed", CLD_KILLED, SIGKILL); + check_sigchld (CLD_KILLED, SIGKILL, pid); info.si_signo = 0; /* A successful call sets it to SIGCHLD. */ info.si_pid = -1; info.si_status = -1; - fail = waitid (P_PID, pid, &info, WEXITED|WNOHANG); - switch (fail) - { - default: - printf ("waitid WNOHANG returned bogus value %d\n", fail); - RETURN (EXIT_FAILURE); - case -1: - printf ("waitid WNOHANG on killed: %m\n"); - RETURN (EXIT_FAILURE); - case 0: - if (info.si_signo != SIGCHLD) - { - printf ("waitid WNOHANG on killed signal %d\n", info.si_signo); - RETURN (EXIT_FAILURE); - } - if (info.si_code != CLD_KILLED) - { - printf ("waitid WNOHANG on killed code %d\n", info.si_code); - RETURN (EXIT_FAILURE); - } - if (info.si_status != SIGKILL) - { - printf ("waitid WNOHANG on killed status %d\n", info.si_status); - RETURN (EXIT_FAILURE); - } - if (info.si_pid != pid) - { - printf ("waitid WNOHANG on killed pid %d != %d\n", info.si_pid, pid); - RETURN (EXIT_FAILURE); - } - } + fail = waitid (type, pid, &info, WEXITED | WNOHANG); + TEST_COMPARE (fail, 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); fail = waitid (P_PID, pid, &info, WEXITED); - if (fail == -1) - { - if (errno != ECHILD) - { - printf ("waitid WEXITED on killed: %m\n"); - RETURN (EXIT_FAILURE); - } - } - else + TEST_COMPARE (fail, -1); + TEST_COMPARE (errno, ECHILD); + + return 0; +} + +static int +do_test_waitid (idtype_t type) +{ +#ifdef SA_SIGINFO + { + struct sigaction sa; + sa.sa_flags = SA_SIGINFO | SA_RESTART; + sa.sa_sigaction = sigchld; + sigemptyset (&sa.sa_mask); + xsigaction (SIGCHLD, &sa, NULL); + } +#endif + + sigemptyset (&chldset); + sigaddset (&chldset, SIGCHLD); + + /* The SIGCHLD shall has blocked at the time of the call to sigwait; + otherwise, the behavior is undefined. */ + sigprocmask (SIG_BLOCK, &chldset, NULL); + + pid_t pid = xfork (); + if (pid == 0) { - printf ("waitid WEXITED returned bogus value %d\n", fail); - RETURN (EXIT_FAILURE); + test_child (type == P_PGID || type == P_ALL); + _exit (127); } -#undef RETURN - out: - if (spurious_sigchld) - status = EXIT_FAILURE; - signal (SIGCHLD, SIG_IGN); + int ret = do_test_waitd_common (type, pid); + + xsignal (SIGCHLD, SIG_IGN); kill (pid, SIGKILL); /* Make sure it's dead if we bailed early. */ - return status; + return ret; +} + +static int +do_test (void) +{ + int ret = 0; + + ret |= do_test_waitid (P_PID); + ret |= do_test_waitid (P_PGID); + ret |= do_test_waitid (P_ALL); + + return ret; } -#include "../test-skeleton.c" +#include From patchwork Tue Feb 18 14:02:05 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: 183596 Delivered-To: patch@linaro.org Received: by 2002:a92:1f12:0:0:0:0:0 with SMTP id i18csp6154020ile; Tue, 18 Feb 2020 06:02:29 -0800 (PST) X-Google-Smtp-Source: APXvYqw2d+WycvTogMQkK4sjdsoTPpa6zDzNp1kQORucOlm+LQsnvyospwQnsUquqnaXC6LzMsIh X-Received: by 2002:a9d:7e90:: with SMTP id m16mr14933564otp.227.1582034549325; Tue, 18 Feb 2020 06:02:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582034549; cv=none; d=google.com; s=arc-20160816; b=FwM9qxd7Kh3tApTYZu4N1kkQ8tjZLSXjhHBurF3zceIctObfJJ+Ew59dEJGOL+9ctr o2cW1SLIt+6aLsdlp4In1TeV5S/JP92fyzUX82J/VVIrGcoFIctxYUJ8AzqD7TZkmXst JzcrN/9p02Pi+nkaiQWzDJawms6C6cCwU8eY7RQutvv/tt94rA7+QyE491q/k9lkBpza rJdGad5J1t+ZDtlU5BAlyyWvu0hHFc3rkjDd2AwfgbjHyW8wrWSP0DHPo6WfLXgTOYBW klk2ieKB9EfRRD4J1rQXmoPiEQyA/J2k1+bzPIqvs6ajBmgVLa41IB05Ll5p3w+eQn6I cQAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to: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=Q4Auv/JTCNxpp3lyrWs4HbCN58+/QiP52jt1MovJZGg=; b=KLLJ5ryXBGKQJVXq3VFNZorj4mbJA/+OVI0ZDVOqokrzT7aY1LWIyLEUaXfqBswA00 J8cxXM1fHGQ+MdOS//Lt1rK2MtqGK2tkc/DxC6KL+iBfuUIurZ/rF1q0dVTTHIipVLYS d4DxD5UKZvmAztk2GzwwqOK/119pdWl97GHVQkMCncIy/YFVYrNx8j7XnbVfTZhbZ9kY 9Pw8oM9SiPUo41cAq0s9/E1VVZ5Xdq+1j6DufBUyySV0Zy0uOliVy/CnMKJ1eopkD6bY 2nq3aTtpacWq2FIJXb4WJ8MWnUaLhEPkm/8ss0sWhc1/WXRSioZdgh+z1iLz6lcndURW 6jIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=OY+serfv; dkim=pass header.i=@linaro.org header.s=google header.b="s/GtJ/HB"; spf=pass (google.com: domain of libc-alpha-return-109878-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-109878-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 z15si1812190otm.168.2020.02.18.06.02.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Feb 2020 06:02:28 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-109878-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=OY+serfv; dkim=pass header.i=@linaro.org header.s=google header.b="s/GtJ/HB"; spf=pass (google.com: domain of libc-alpha-return-109878-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-109878-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:in-reply-to :references; q=dns; s=default; b=DXq802KNTFuT6KcET6fV5ONRb+1WJba /pEmsqnbJg639moFxDnm7QDMjx0wfNE91+MToRbPnzETWnCL9yoz62tDEUC4Ohny aclIvgtVB+s48sW78OTRK0VrY/wlnClezFdYETj2blGuJIp8uTtvOUUoSpIIuii5 r/eBqhPpmQ9Q= 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:in-reply-to :references; s=default; bh=lrdqokhHdRNBvt7/mCpqyoVDIJk=; b=OY+se rfvbBwcx7OyIIM7TIOomG4TEDkmOt41btQIKoI/Atw35zHIraPARPR0FXnz3FSFS 7oQn4EkulGbgBgNClKc1hy1z5GF5v9m3EXGWoeBD+7ndhUY7xydOeD6Zx2nbH9Q7 pdjfuRJ/EPkCvWI/iF5VNnYyhixtHxwJgcUAlI= Received: (qmail 58313 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 58302 invoked by uid 89); 18 Feb 2020 14:02:17 -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=Technical X-HELO: mail-qt1-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:in-reply-to:references; bh=Q4Auv/JTCNxpp3lyrWs4HbCN58+/QiP52jt1MovJZGg=; b=s/GtJ/HBFH53kjFNfjmAaj+zfgem0F0WUZ8PioEONVZTGnjLKPr7eX9AlXXKZkVOW6 i9FIMZwsHuJQyuHpOQlSHQN4ir89VlnzDMhUTAzd8Zf7eSHboPEcaZhPsDuoNQ/ncYgw rY6bfXyL0ZMIBwgWJRLaUKmCe1KO94NAKmMhNl7R4ZZtbsgAihJH9iDAAAEFOf6eiDGw Tq5KWe+ZWVF0RIb5M/oxZ8XaM8iwH6Hn1yaldZ1D7HU4waeSGEThVBFgqhD/4Na6ehU1 ELDeHjOmKWTZUAHva1TayPMCHa3B14SwwxjP5muyMuypEzv+z4fXHuZAv5aFaftDpuQk yZhQ== Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2 3/3] posix: Remove posix waitid Date: Tue, 18 Feb 2020 11:02:05 -0300 Message-Id: <20200218140205.18263-3-adhemerval.zanella@linaro.org> In-Reply-To: <20200218140205.18263-1-adhemerval.zanella@linaro.org> References: <20200218140205.18263-1-adhemerval.zanella@linaro.org> The POSIX waitid implementation is problematic in some ways: - It emulates using waitpid, which default implementation calls wait4 and wait4 returns ENOSYS as default. - Also by using waitpid it does not allod support the WNOWAIT, WEXITED, WSTOPPED, or WCONTINUED flag. With current POSIX specification the flags are no longer marked as optional. Also due BZ#23091 Hurd still uses the implementation, so it is moved to as a Hurd arch-specific folder (with some minor cleanups). Checked against a i686-gnu (run-built-tests=no) --- posix/tst-waitid.c | 14 +++--- sysdeps/{posix => mach/hurd}/waitid.c | 70 ++++----------------------- 2 files changed, 16 insertions(+), 68 deletions(-) rename sysdeps/{posix => mach/hurd}/waitid.c (65%) -- 2.17.1 diff --git a/posix/tst-waitid.c b/posix/tst-waitid.c index 2331c42e66..380d807498 100644 --- a/posix/tst-waitid.c +++ b/posix/tst-waitid.c @@ -80,10 +80,10 @@ do_test_waitd_common (idtype_t type, pid_t pid) { /* 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; + enum support_process_state stop_state + = support_process_state_stopped | support_process_state_tracing_stop; - support_process_state_wait (pid, stop_state); + support_process_state_wait (pid, stop_state, NULL); check_sigchld (CLD_STOPPED, SIGSTOP, pid); @@ -117,7 +117,7 @@ do_test_waitd_common (idtype_t type, pid_t pid) FAIL_RET ("kill (%d, SIGCONT): %m\n", pid); /* Wait for the child to have continued. */ - support_process_state_wait (pid, process_state_sleeping); + support_process_state_wait (pid, support_process_state_sleeping, NULL); #if WCONTINUED != 0 { @@ -175,7 +175,7 @@ do_test_waitd_common (idtype_t type, pid_t pid) stopped, but if we are real quick and enter the waitid system call before the SIGCHLD has been generated, then it will be discarded and never delivered. */ - support_process_state_wait (pid, stop_state); + support_process_state_wait (pid, stop_state, NULL); fail = waitid (type, pid, &info, WEXITED|WSTOPPED); TEST_COMPARE (fail, 0); @@ -190,7 +190,7 @@ do_test_waitd_common (idtype_t type, pid_t pid) FAIL_RET ("kill (%d, SIGCONT): %m\n", pid); /* Wait for the child to have continued. */ - support_process_state_wait (pid, process_state_sleeping); + support_process_state_wait (pid, support_process_state_sleeping, NULL); check_sigchld (CLD_CONTINUED, SIGCONT, pid); @@ -219,7 +219,7 @@ do_test_waitd_common (idtype_t type, pid_t pid) TEST_COMPARE (info.si_status, SIGKILL); TEST_COMPARE (info.si_pid, pid); #else - support_process_state_wait (pid, process_state_zombie); + support_process_state_wait (pid, support_process_state_zombie, NULL); #endif check_sigchld (CLD_KILLED, SIGKILL, pid); diff --git a/sysdeps/posix/waitid.c b/sysdeps/mach/hurd/waitid.c similarity index 65% rename from sysdeps/posix/waitid.c rename to sysdeps/mach/hurd/waitid.c index f752076735..dce72339dd 100644 --- a/sysdeps/posix/waitid.c +++ b/sysdeps/mach/hurd/waitid.c @@ -17,25 +17,11 @@ License along with the GNU C Library; if not, see . */ -#include #include -#include -#define __need_NULL -#include #include -#include -#include - -#ifdef DO_WAITID -# define OUR_WAITID DO_WAITID -#elif !defined NO_DO_WAITID -# define OUR_WAITID do_waitid -#endif - -#ifdef OUR_WAITID -static int -OUR_WAITID (idtype_t idtype, id_t id, siginfo_t *infop, int options) +int +__waitid (idtype_t idtype, id_t id, siginfo_t *infop, int options) { pid_t pid, child; int status; @@ -43,7 +29,7 @@ OUR_WAITID (idtype_t idtype, id_t id, siginfo_t *infop, int options) switch (idtype) { case P_PID: - if(id <= 0) + if (id <= 0) goto invalid; pid = (pid_t) id; break; @@ -72,31 +58,10 @@ OUR_WAITID (idtype_t idtype, id_t id, siginfo_t *infop, int options) return -1; } - /* This emulation using waitpid cannot support the waitid modes in which - we do not reap the child, or match only stopped and not dead children. */ - if (0 -#ifdef WNOWAIT - || (options & WNOWAIT) -#endif -#ifdef WEXITED - || ((options & (WEXITED|WSTOPPED|WCONTINUED)) - != (WEXITED | (options & WSTOPPED))) -#endif - ) - { - __set_errno (ENOTSUP); - return -1; - } - /* Note the waitid() is a cancellation point. But since we call waitpid() which itself is a cancellation point we do not have to do anything here. */ - child = __waitpid (pid, &status, - options -#ifdef WEXITED - &~ WEXITED -#endif - ); + child = __waitpid (pid, &status, options); if (child == -1) /* `waitpid' set `errno' for us. */ @@ -104,9 +69,11 @@ OUR_WAITID (idtype_t idtype, id_t id, siginfo_t *infop, int options) if (child == 0) { - /* The WHOHANG bit in OPTIONS is set and there are children available - but none has a status for us. The XPG docs do not mention this - case so we clear the `siginfo_t' struct and return successfully. */ + /* POSIX.1-2008, Technical Corrigendum 1 XSH/TC1-2008/0713 [153] states + that if waitid returns because WNOHANG was specified and status is + not available for any process specified by idtype and id, then the + si_signo and si_pid members of the structure pointed to by infop + shall be set to zero. */ infop->si_signo = 0; infop->si_code = 0; return 0; @@ -132,27 +99,8 @@ OUR_WAITID (idtype_t idtype, id_t id, siginfo_t *infop, int options) infop->si_code = CLD_STOPPED; infop->si_status = WSTOPSIG (status); } -#ifdef WIFCONTINUED - else if (WIFCONTINUED (status)) - { - infop->si_code = CLD_CONTINUED; - infop->si_status = SIGCONT; - } -#endif - else - /* Can't happen. */ - assert (! "What?"); return 0; } -#endif - - -int -__waitid (idtype_t idtype, id_t id, siginfo_t *infop, int options) -{ - /* __waitpid should be a cancellation point. */ - return do_waitid (idtype, id, infop, options); -} weak_alias (__waitid, waitid) strong_alias (__waitid, __libc_waitid)