From patchwork Sun May 19 10:40:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Janne Blomqvist X-Patchwork-Id: 1101519 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-501097-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="BKjYdBJX"; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="jQx+FDTK"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 456JVJ6WwCz9sBb for ; Sun, 19 May 2019 20:41:26 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id; q=dns; s=default; b=i5iXN0KzFoSw XV0d+yArum9+t3Rv7e5t6CaoI2ONBlm8Edn44YkSYRjP6gsj+h5Iq5fZLV0b/vqG 4wOVRFq+h6vNSTTcNyUErLdTm10qzuzeJRAyCCZUodjM4eY/U4DQJnVxl/sGWq1G tnj8MeKzR3e+433u4/dbhCu8+ZKJpLw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id; s=default; bh=3gkzd0Nsx4GKaDBxC/ PeLZsLQ3Q=; b=BKjYdBJXg8VYpAuVeNOWy8Ry4/cI5sOy5yJ+/ZFT4spjZF8Ie0 LMK/smuj/tMNhUCVh35dafATMdKgCzrtE0YHJu8p+TiS7Lp++k3FNuA4VVR8VDOZ q5U+Lo0w3VE5w3Uzt2LvC//wAfGTxGJtkTlqRbQy4Fpgkq1WNC2iHBmds= Received: (qmail 109251 invoked by alias); 19 May 2019 10:41:19 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 109222 invoked by uid 89); 19 May 2019 10:41:18 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, 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=UD:execute_command_line.c, zombie, reap, sk:ac_chec X-HELO: mail-lf1-f68.google.com Received: from mail-lf1-f68.google.com (HELO mail-lf1-f68.google.com) (209.85.167.68) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 19 May 2019 10:41:08 +0000 Received: by mail-lf1-f68.google.com with SMTP id c17so8262216lfi.2; Sun, 19 May 2019 03:41:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=Hu7ZNBJhsAO+cYJaYH+J3MMj+BgjjeCqmIyeVBbt2Aw=; b=jQx+FDTK6TIf8COGOxvXbEGLX7LnqWex487P7ndVsCSPrd9tLfKCXwvBhmZCARs9zY X434JckCeGZg3+SApbRdgBBQAdcC9996pbJicEy81EylfK7lc7yVdW6sVqzbaGokqLn1 MDPwlJWi+hZXF0E8wFtpFwqtgNcWOmPh8/98OBk1dhtXjkBoyvNLdTEBmCdKGg7bOIpU O6NSRz8ID52FLxHubYh5BSm43CkrZdgPfFKp1ydLBWZGUv7tYyTymykfIuE9uomeIG20 7oeiv3RziLQRjk737fLkj0FO+CXtXor+il8dejKzLRYRmbLL7THkhsQksQnlf7UDiKb3 rgfg== Received: from ws.lan (88-114-247-254.elisa-laajakaista.fi. [88.114.247.254]) by smtp.gmail.com with ESMTPSA id e14sm3444884ljk.45.2019.05.19.03.41.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 19 May 2019 03:41:05 -0700 (PDT) From: Janne Blomqvist To: fortran@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Janne Blomqvist Subject: [PATCH] libfortran/90038 Reap dead children when wait=.false. Date: Sun, 19 May 2019 13:40:59 +0300 Message-Id: <20190519104059.19382-1-blomqvist.janne@gmail.com> When using posix_spawn or fork to launch a child process, the parent needs to wait for the child, otherwise the dead child is left as a zombie process. For this purpose one can install a signal handler for SIGCHLD. 2019-05-19 Janne Blomqvist PR libfortran/90038 * intrinsics/execute_command_line (sigchld_handler): New function. (execute_command_line): Install handler for SIGCHLD. * configure.ac: Check for presence of sigaction and waitpid. * config.h.in: Regenerated. * configure: Regenerated. Regtested on x86_64-pc-linux-gnu, Ok for trunk? --- libgfortran/configure.ac | 2 +- libgfortran/intrinsics/execute_command_line.c | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/libgfortran/configure.ac b/libgfortran/configure.ac index 8fd5a1a30a9..7cfce28ab69 100644 --- a/libgfortran/configure.ac +++ b/libgfortran/configure.ac @@ -314,7 +314,7 @@ if test "${hardwire_newlib:-0}" -eq 1; then else AC_CHECK_FUNCS_ONCE(getrusage times mkstemp strtof strtold snprintf \ ftruncate chsize chdir getentropy getlogin gethostname kill link symlink \ - sleep ttyname \ + sleep ttyname sigaction waitpid \ alarm access fork posix_spawn setmode fcntl writev \ gettimeofday stat fstat lstat getpwuid vsnprintf dup \ getcwd localtime_r gmtime_r getpwuid_r ttyname_r clock_gettime \ diff --git a/libgfortran/intrinsics/execute_command_line.c b/libgfortran/intrinsics/execute_command_line.c index 2ef2324b243..1a471632172 100644 --- a/libgfortran/intrinsics/execute_command_line.c +++ b/libgfortran/intrinsics/execute_command_line.c @@ -36,6 +36,9 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #include extern char **environ; #endif +#if defined(HAVE_POSIX_SPAWN) || defined(HAVE_FORK) +#include +#endif enum { EXEC_SYNCHRONOUS = -2, EXEC_NOERROR = 0, EXEC_SYSTEMFAILED, EXEC_CHILDFAILED, EXEC_INVALIDCOMMAND }; @@ -62,6 +65,14 @@ set_cmdstat (int *cmdstat, int value) } +#if defined(HAVE_WAITPID) && defined(HAVE_SIGACTION) +static void +sigchld_handler (int signum __attribute__((unused))) +{ + while (waitpid ((pid_t)(-1), NULL, WNOHANG) > 0) {} +} +#endif + static void execute_command_line (const char *command, bool wait, int *exitstat, int *cmdstat, char *cmdmsg, @@ -82,6 +93,20 @@ execute_command_line (const char *command, bool wait, int *exitstat, set_cmdstat (cmdstat, EXEC_NOERROR); +#if defined(HAVE_SIGACTION) && defined(HAVE_WAITPID) + static bool sig_init_saved; + bool sig_init = __atomic_load_n (&sig_init_saved, __ATOMIC_RELAXED); + if (!sig_init) + { + struct sigaction sa; + sa.sa_handler = &sigchld_handler; + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_RESTART | SA_NOCLDSTOP; + sigaction(SIGCHLD, &sa, 0); + __atomic_store_n (&sig_init_saved, true, __ATOMIC_RELAXED); + } +#endif + #ifdef HAVE_POSIX_SPAWN const char * const argv[] = {"sh", "-c", cmd, NULL}; if (posix_spawn (&pid, "/bin/sh", NULL, NULL,