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 Netto X-Patchwork-Id: 179852 Delivered-To: patch@linaro.org Received: by 2002:a92:38d5:0:0:0:0:0 with SMTP id g82csp2162463ilf; Wed, 20 Nov 2019 06:15:55 -0800 (PST) X-Google-Smtp-Source: APXvYqxFiSc6xGe5cW3xA5tpnWVft9S7Y6HBJXltMRYpL945CKmDZvdbINGX/qpIr0yZ2e3uDR7W X-Received: by 2002:a1c:7d95:: with SMTP id y143mr3594197wmc.143.1574259355863; Wed, 20 Nov 2019 06:15:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574259355; cv=none; d=google.com; s=arc-20160816; b=ft76+YIdeScns2nOEBzola01QFdSdJqjDseVinqdOOykPAuMfF+pLm4hyO/e8PIZ52 GE9AoD4vSxIq48pZFCTRiTh3JJEA/qpi7akrdB/iRJ/SiP4Fn3qatsru4s/96mr0PGEU wkBFoLWwyK7f1VHvNqfai3hUsu0dqBE8BQFAOJUYAoX5/log61PGLlXa6zN9YcKC2bvf sDq+/7FH+Xo3ru8nfVKWlKVKZ9DzwyxQgxkW5ubyTPVsbC6L28ka/zB29PW8mCj42LnR 73QwsEWglvFlRK8gBdnBVITO0bh8Mj8RmBfb24ZupVYcjBLhCsK9gN8a8KsUaeAxJqf4 8TKQ== 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=tlaTlqCbXE/BUolfKuqTqddRl4M/yT0FZbMcbe4e1ro=; b=BovmxTC20nt5t3Bwzt9PLSYCefl1lpKbhMINjTz02G6/Mp0HQPchaRbsYxDqtHkIPX ClbbYQUZBQampS7iWlURQ6VoAbMrmepM5QQaiu1ysNWUR670GS8ZBatJYeiRta7UI+JM PCj61qK+y5RkJGEV6fb8Rs8AoLOzifHMAf8N7Sacn99xwClEDETWNJZilztaHPt2j3PN mU+ZqMOlcWfaswJIUZ9vp9vtPFfPynIZHJi60msKNSwrZirloX4hBwfgpY5gSK+5c/9h plBJzbQ9NCrgjYDu8IWUNWxNjLweoAmWHcbvU2/ZAzgjCzcD2mcEuRkqPW0GolURwFA1 YtXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=B1bUsuqW; dkim=pass header.i=@linaro.org header.s=google header.b=EYFAGmSV; spf=pass (google.com: domain of libc-alpha-return-107208-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-107208-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 j1si183508eds.39.2019.11.20.06.15.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Nov 2019 06:15:55 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-107208-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=B1bUsuqW; dkim=pass header.i=@linaro.org header.s=google header.b=EYFAGmSV; spf=pass (google.com: domain of libc-alpha-return-107208-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-107208-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=dsfJPFc8rYeLcU+daxDKCMCwPW+Qa1j38pEAq00YcWetMhynrQDIu E6xym8R16xMn7Fkv33HLy0+/MS77pbFTcP8aAB11h4PVEC04afCzJf4ACjmGUs45 XYRgdItigI+oaw3/oP1CtZOvMRIIswwtM2a18GW2QHqLUDlZnG0XBc= 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=crJUcSQklgntn07+tHhzSXzKOxk=; b=B1bUsuqWinyQ8+iqgKXxYNL08bdo 27OtKxfHmBYTPToVGW2vdn/iXxnpp0X3kHAO/2Ukeyi5hxZLf8lH/4rHJb+WVbLW cEunfh66B8nXl0BpOzvQbjPJvN47CMUxHqjoX6s8azgQelj8/bQ9JBw6ZMS3ggL2 9+QZ/N6M1nWwVCg= 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 -- 2.17.1 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 From patchwork Wed Nov 20 14:15:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 179854 Delivered-To: patch@linaro.org Received: by 2002:a92:38d5:0:0:0:0:0 with SMTP id g82csp2162985ilf; Wed, 20 Nov 2019 06:16:20 -0800 (PST) X-Google-Smtp-Source: APXvYqy44OTSrPOumFKaOJ+MJjd/y7dmoyM17yQ6dw2L0udDyyyQW+PKh+S4ab0RRmtXEecsnwqc X-Received: by 2002:a19:ca03:: with SMTP id a3mr3178028lfg.20.1574259379399; Wed, 20 Nov 2019 06:16:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574259379; cv=none; d=google.com; s=arc-20160816; b=DwN2NSpQh+cSe9cqZEQc3ajpYD8h1QpKhSiyn5fbk0zvMNjLIXuuNe/me9JoZ0vrUt RhkdbhXmlUGmG3TNT5dEF5Ft/CdBxbSM+2fvzxQ2l50hnw5+jYZ4/L3b+QBFQQiScC7a grbrASLbay0FnTZR4vr6sS6QmvW21GyYdDV57n6TlhTVtQDUuYk5EN8emkEEJOR2Gl4X b9Mt+pPkfwoVZDXPCK+kMN5ycD+Yjj4LdOWYgCqI9CzRDmlFZgbicIhAEjUJNnb1l81e YdfJ7Zo70fOZpZDcY9IhKjrgSB3eShflJ+OATfJTDgQhj3qQFIMwiP3jQOaVmvHvnYg9 fcDw== 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=0DIElEKx93V53y5V4vttZ8xgf5LGpnf7T9X8fJpJPCg=; b=zIew+3fGFTS127SJVcizPHzErzCLE28hYDxj8yv1IFEZ3GcKPjgwaBR0a6EfSa2tTt p+g/E8DAsxF3frRfsG9BXE/7KFjLEIHPtKwWou6LNj/kCYgC+ja6Bo7J59VE1iQHrnHa 3X7a5QL+5jitnBC3Eg1W7ScwRkSHBJcBYnNerFnvjgtVUIdFiWYiu7pPZUYJ+nq5PXRs Ngd6T95Qqgpq+naKuL/FrdJ/bgFbRObDHx3Lwi+3kqCw6xmumSZif3lriM9nh6PYbHlK TkVrgTvl0DU7nWvNj/A6oZqcaydumlGGHde6f7ZB0TgyQ2zdAhxIpJtEm3Wf9RK1MuI/ uenw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=p3OpyEEX; dkim=pass header.i=@linaro.org header.s=google header.b=DwEIsh31; spf=pass (google.com: domain of libc-alpha-return-107210-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-107210-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 t52si18567073edd.3.2019.11.20.06.16.18 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Nov 2019 06:16:19 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-107210-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=p3OpyEEX; dkim=pass header.i=@linaro.org header.s=google header.b=DwEIsh31; spf=pass (google.com: domain of libc-alpha-return-107210-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-107210-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=KZ2lZM2QrRt6C9Z6XF8SU5r814CO6/R PlM51MDSqgKRxWxgV486wjHNWYZMkLenYMZQmpQ/smMjeDTbmZOOv3k3JA7Hicpm 0CJW+7SreqQKPCH5iCXLaZBToObZYyeZC1aFeIkQwYfBIXlY2Rn3B1CAzr1Bb6Qa 8uhOu0606Aqw= 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=5JxHQroGabelCFX4dWTsIT3AawA=; b=p3Opy EEXxWy1J6ScLkdLAPSmJM2Ykngum3eHe6dWmWmxQrDkvheXzvXscxy1Xy0ymzm6o rKPSoFoFahMejOQnxLPDQYshgDQoZkYPkgblHpnGbf6j4rTjsNIsX7/o98vIx3D2 MEj8Ypsw5s5R5dtmcD7MJTSwQfjur/5WbL92j8= Received: (qmail 20168 invoked by alias); 20 Nov 2019 14:15:47 -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 20093 invoked by uid 89); 20 Nov 2019 14:15:47 -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, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=him, his, act X-HELO: mail-qt1-f194.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=0DIElEKx93V53y5V4vttZ8xgf5LGpnf7T9X8fJpJPCg=; b=DwEIsh31GbywVB91vhwAx06fNbdAB4hhzSk6Qbl6BzXtgs/S6UUdxporVLsBjIPzV5 Wnh/K3SUzwTAFfyqS8+L4SRLS+dFMN2Qr/imqsawtO1MQ2XEWzRoTcX1WxOHfxySOxSg 34SPYbQTVFxqfrYB399P7/s40qoElLCSLFXKPHyV5G2ZemmuDikLFg/eTl/q/tbrJocM fKBJ+OUipyCIcpl0u+Nfcp1dKsz+O3Cpd4Rp9hx0OAMCMO5F4QGA1EnNTFilrcUOc1wX 0M6EgbuFuh/p6LpGNlLb9khuF3srV6v5S7D6cuKEJzBjxx3Lg9+Rr62iPtts1quEuAT1 TygA== Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 2/3] posix: Refactor tst-waitid Date: Wed, 20 Nov 2019 11:15:34 -0300 Message-Id: <20191120141535.5303-2-adhemerval.zanella@linaro.org> In-Reply-To: <20191120141535.5303-1-adhemerval.zanella@linaro.org> References: <20191120141535.5303-1-adhemerval.zanella@linaro.org> 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. Checked on x86_64-linux-gnu and i686-linux-gnu. --- posix/tst-waitid.c | 519 +++++++++++++-------------------------------- 1 file changed, 153 insertions(+), 366 deletions(-) -- 2.17.1 diff --git a/posix/tst-waitid.c b/posix/tst-waitid.c index c82ec2b566..1134d92bb3 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,20 +51,16 @@ test_child (void) # define WSTOPPED WUNTRACED #endif -static sig_atomic_t expecting_sigchld, spurious_sigchld; +static atomic_int expecting_sigchld, spurious_sigchld; #ifdef SA_SIGINFO static siginfo_t sigchld_info; 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); - } + TEST_COMPARE (signo, SIGCHLD); - if (! expecting_sigchld) + if (expecting_sigchld == 0) { spurious_sigchld = 1; printf ("spurious SIGCHLD: signo %d code %d status %d pid %d\n", @@ -69,154 +72,66 @@ sigchld (int signo, siginfo_t *info, void *ctx) 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; - } - - 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) +#ifdef SA_SIGINFO + TEST_COMPARE (expecting_sigchld, 0); + TEST_COMPARE (sigchld_info.si_signo, SIGCHLD); + TEST_COMPARE (sigchld_info.si_code, code); + TEST_COMPARE (sigchld_info.si_status, status); + TEST_COMPARE (sigchld_info.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 - - expecting_sigchld = 1; - - pid_t pid = fork (); - if (pid < 0) - { - printf ("fork: %m\n"); - return EXIT_FAILURE; - } - else 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; - int status = EXIT_SUCCESS; -#define RETURN(ok) \ - do { if (status == EXIT_SUCCESS) status = (ok); goto out; } while (0) + support_process_state_wait (pid, stop_state); - /* Give the child a chance to stop. */ - sleep (3); + /* Wait the SIGCHLD handler to act. */ + while (expecting_sigchld == 1); - 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; 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) @@ -224,152 +139,68 @@ do_test (int argc, char *argv[]) printf ("no SIGCHLD seen for SIGCONT (optional)\n"); expecting_sigchld = 0; } - else - CHECK_SIGCHLD ("continued (before waitid)", CLD_CONTINUED, SIGCONT); + check_sigchld (CLD_CONTINUED, SIGCONT, 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; 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) { @@ -377,142 +208,98 @@ do_test (int argc, char *argv[]) expecting_sigchld = 0; } else - CHECK_SIGCHLD ("continued (before waitpid)", CLD_CONTINUED, SIGCONT); - - 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); - } + check_sigchld (CLD_CONTINUED, SIGCONT, pid); + + 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) +{ + expecting_sigchld = 1; + spurious_sigchld = 0; + +#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 + + 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: + int ret = do_test_waitd_common (type, pid); + if (spurious_sigchld) - status = EXIT_FAILURE; - signal (SIGCHLD, SIG_IGN); + ret = EXIT_FAILURE; + 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 Wed Nov 20 14:15:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 179853 Delivered-To: patch@linaro.org Received: by 2002:a92:38d5:0:0:0:0:0 with SMTP id g82csp2162669ilf; Wed, 20 Nov 2019 06:16:05 -0800 (PST) X-Google-Smtp-Source: APXvYqzRpt4Q79TRF3X5yBFtQSxVdsBFzWLzQ+I2VTAPQ278mbz5WR1UuxaqaY7bF3gFtHA/Xl51 X-Received: by 2002:a5d:50ce:: with SMTP id f14mr3905623wrt.219.1574259365007; Wed, 20 Nov 2019 06:16:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574259365; cv=none; d=google.com; s=arc-20160816; b=aJ1Mu7FDmBs/KZ+5ymoxr4p2/FZvojW2/PJrzihVnSxu8unhr40yf2IqxeGj1bBIjg qpkZDgtOuUEgkJQCtNL1s1PQP6IdNXrvOwm++PzyEmW9+FXwabW0DEKB+hAyxsWxY/HI wHU1ahbIiOOzGEDt5g5h5tPR3SMi3bHsey//2faGdYT5Y0tLdwGj9Y5hjKhlZiTmSJgm mZFPwWsUHCN9PF16iCg1lo4mJwS7iYcnUKPUP8zDjWjUWehjLk0Inqwlpyj3lSJyypth kM2IBZmHH217ljXEcQW8h+sEK4iu9lQdaugWDs9sR9QwlDgBVSTpxnOx+dn+0dyYvcz9 taZw== 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=48a1o4aMKtGnx5Ek7Ic2Xb0gL89NmqbXeOGxxXb1FyU=; b=Cu9yAlprG0m9KLETeH5gWbYWWmqF1Ge14JhCuuXtOmllFWensxWec+pYZaTK2b/pls 8k2XPqgIzy80FUX6LSfTrK3khZJi+W9tyMJu5PSC+Cb+Qcw8eKT46YIe6N46rFLZ2QKz rt5mQHibBaGunq09Kt4HxxUIztLB2/FrL2A+O6VWfnXCKLYmPFXiQ9UpNOCxjYy7SLfM dOUQlD2uPcFo+CHj8PE3yji7piWaYEgzj5zIX2iHv2AP3cLGnLRiDFi3N87G00gu13jH H5W6PaP9A8g6aCVpLJaeAjh2KhXSQsRjqmRFCzjEbIXwawezrIJbMtLd42sWZMSkYqKn 1hVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=mMhphYaB; dkim=pass header.i=@linaro.org header.s=google header.b=sySO6yde; spf=pass (google.com: domain of libc-alpha-return-107209-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-107209-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 d10si14220488edo.298.2019.11.20.06.16.04 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Nov 2019 06:16:04 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-107209-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=mMhphYaB; dkim=pass header.i=@linaro.org header.s=google header.b=sySO6yde; spf=pass (google.com: domain of libc-alpha-return-107209-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-107209-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=bBMH0sKPtdfEY+XNa8dqsoDZOnzbm8S Rjgc5Nxen6HtL+AVgssH5nQZX2nUnbr0TYv5r4nVPlKEe3uvOhF6xTRmgGq0JDh5 WFHj1EJFqKKjp8KMDCB1HR1TbSPbdcF3c5qLEl8JyO6M1HF7NAWU+zkUCnVbGmeM IZn0Sc7wOHbA= 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=3mPgAvoJKvLikHMdvOTGHqBOqDU=; b=mMhph YaBo51CRX+s+fXS3ACHnykdTg99ErFs75UeGoYg/yEeo7+Dr60qeUfKeESGdaB6/ 7dVSexAvcKA+TIVqWs7JIYZ6jGjKnEP0F+KqMfXYWAIex6YKdDR7a/QHFXqfIOfK Z0uP27lCFuMXHyW8Fgih7E4c/xdHCsPX+0UQ+4= Received: (qmail 20032 invoked by alias); 20 Nov 2019 14:15:47 -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 19987 invoked by uid 89); 20 Nov 2019 14:15:46 -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=folder, Technical X-HELO: mail-qt1-f194.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=48a1o4aMKtGnx5Ek7Ic2Xb0gL89NmqbXeOGxxXb1FyU=; b=sySO6ydeodjXYUwNbaBeZfr1JzafehEX0NAsOhie2Bdmc1A6r5FX2r6ESBKuxiY8Ye dO0p7TO0H6Kq3jehuSNThTPrzNqXOyrK6rpjjYIrYpY9RQeSpDqENBaOUhclyq5HtMfr NyN7oZOYC85pbUN2vsnlQuRJpjARCj9bwWYAntbUz2Y4OU2SPoyKVLO9ErXgcDJpy0qH Z6uqFgOqwdhpkuIYwjlmrXePzuWEnKrVlMZi0RU0my0+hLov5qGIK1nSxDGyNjd6VXW3 BvYT/womKVvpb0T9yZgQ5b+8TPi+8FAx4nBZCjidHl5vCCtX4IZOQDzNEzZVcGrq56HM DYTg== Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 3/3] posix: Remove posix waitid Date: Wed, 20 Nov 2019 11:15:35 -0300 Message-Id: <20191120141535.5303-3-adhemerval.zanella@linaro.org> In-Reply-To: <20191120141535.5303-1-adhemerval.zanella@linaro.org> References: <20191120141535.5303-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) --- sysdeps/{posix => mach/hurd}/waitid.c | 70 ++++----------------------- 1 file changed, 9 insertions(+), 61 deletions(-) rename sysdeps/{posix => mach/hurd}/waitid.c (65%) -- 2.17.1 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 0942623553..d28d0034da 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)