NetBSD Problem Report #57579

From www@netbsd.org  Sat Aug 12 13:21:54 2023
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 73F271A923A
	for <gnats-bugs@gnats.NetBSD.org>; Sat, 12 Aug 2023 13:21:54 +0000 (UTC)
Message-Id: <20230812132152.C763D1A923C@mollari.NetBSD.org>
Date: Sat, 12 Aug 2023 13:21:52 +0000 (UTC)
From: campbell+netbsd@mumble.net
Reply-To: campbell+netbsd@mumble.net
To: gnats-bugs@NetBSD.org
Subject: ctwm application menu hangs because of SIGCHLD/SIG_IGN/system(3) interaction
X-Send-Pr-Version: www-1.0

>Number:         57579
>Category:       xsrc
>Synopsis:       ctwm application menu hangs because of SIGCHLD/SIG_IGN/system(3) interaction
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    xsrc-manager
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Aug 12 13:25:00 +0000 2023
>Last-Modified:  Sat Aug 12 15:10:01 +0000 2023
>Originator:     Taylor R Campbell
>Release:        current
>Organization:
The NetBSD SIGCHLDation
>Environment:
>Description:
The recent ctwm-4.1.0 update switched from setting SIGCHLD to a handler that calls waitpid, to setting SIGCHLD to SIG_IGN.

Under POSIX semantics, having SIGCHLD handled by SIG_IGN -- or having the SA_NOCLDWAIT flag set -- means that wait(2) will not return until _all_ children have exited, and then will fail with ECHILD.

POSIX does not require system(3) to cope with SIG_IGN or SA_NOCLDWAIT in the SIGCHLD action, and NetBSD's system(3) does not.  (See https://gnats.netbsd.org/57527 for further discussion of that question.)

Suppose you have an .xsession or .xinitrc file like this:

xterm &
xclock &
exec ctwm

ctwm will inherit the xterm and xclock processes as children of the shell via exec.  If you then try to use the application menu, ctwm will try to run a subprocess with system(3).  system(3) relies on waitpid to wait for _the child it created_ to exit, but because ctwm has set SIGCHLD to SIG_IGN, the call to waitpid in system(3) will -- under POSIX semantics -- wait for _all children of the process_ to exit.  That means ctwm will keep hanging until not just the application it started has exited, but also the xterm and xclock processes.
>How-To-Repeat:
1. Put `xterm & xclock & exec ctwm' in .xsession or .xinitrc.
2. Start ctwm.
3. Launch a program from the applications menu.
>Fix:
Go back to installing a signal handler for SIGCHLD that calls waitpid, instead of setting it to SIG_IGN.

>Audit-Trail:
From: Rhialto <rhialto@falu.nl>
To: gnats-admin@NetBSD.org
Cc: 
Subject: Re: pkg/57579: ctwm application menu hangs because of
 SIGCHLD/SIG_IGN/system(3) interaction
Date: Sat, 12 Aug 2023 16:18:38 +0200

 This should fix it; but I didn't test this instance of the patch.
 It is a copy of the change I proposed to the current upstream ctwm code,
 which I did test (before and after). I changed my .xsession setup for it
 since in my normal version I end it with starting an xterm instead of
 the window manager, and in that case the problem doesn't show.

 Index: signals.c
 ===================================================================
 RCS file: /cvsroot/xsrc/external/mit/ctwm/dist/signals.c,v
 retrieving revision 1.1.1.1
 diff -u -r1.1.1.1 signals.c
 --- signals.c	5 Jul 2023 07:36:07 -0000	1.1.1.1
 +++ signals.c	12 Aug 2023 14:12:23 -0000
 @@ -8,6 +8,7 @@
  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
 +#include <sys/wait.h>

  #include "ctwm_shutdown.h"
  #include "signals.h"
 @@ -16,6 +17,7 @@
  /* Our backends */
  static void sh_restart(int signum);
  static void sh_shutdown(int signum);
 +static void sh_sigchld(int signum);


  // Internal flags for which signals have called us
 @@ -46,9 +48,8 @@
  	// die...
  	signal(SIGALRM, SIG_IGN);

 -	// This should be set by default, but just in case; explicitly don't
 -	// leave zombies.
 -	signal(SIGCHLD, SIG_IGN);
 +	// Explicitly don't leave zombies.
 +	signal(SIGCHLD, sh_sigchld);

  	return;
  }
 @@ -123,3 +124,15 @@
  	SignalFlag = sig_shutdown = true;
  }

 +/**
 + * Handle SIGCHLD so we don't leave zombie child processes.
 + */
 +static void
 +sh_sigchld(int signum)
 +{
 +	pid_t pid;
 +	int status;
 +
 +	while((pid = waitpid(-1, &status, WNOHANG)) > 0)
 +		;
 +}

From: Rhialto <rhialto@falu.nl>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: pkg/57579: ctwm application menu hangs because of
 SIGCHLD/SIG_IGN/system(3) interaction
Date: Sat, 12 Aug 2023 17:06:17 +0200

 Second version of the patch. Restores errno.

 Index: signals.c
 ===================================================================
 RCS file: /cvsroot/xsrc/external/mit/ctwm/dist/signals.c,v
 retrieving revision 1.1.1.1
 diff -u -r1.1.1.1 signals.c
 --- signals.c	5 Jul 2023 07:36:07 -0000	1.1.1.1
 +++ signals.c	12 Aug 2023 15:04:33 -0000
 @@ -8,6 +8,8 @@
  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
 +#include <sys/wait.h>
 +#include <errno.h>

  #include "ctwm_shutdown.h"
  #include "signals.h"
 @@ -16,6 +18,7 @@
  /* Our backends */
  static void sh_restart(int signum);
  static void sh_shutdown(int signum);
 +static void sh_sigchld(int signum);


  // Internal flags for which signals have called us
 @@ -46,9 +49,8 @@
  	// die...
  	signal(SIGALRM, SIG_IGN);

 -	// This should be set by default, but just in case; explicitly don't
 -	// leave zombies.
 -	signal(SIGCHLD, SIG_IGN);
 +	// Explicitly don't leave zombies.
 +	signal(SIGCHLD, sh_sigchld);

  	return;
  }
 @@ -123,3 +125,18 @@
  	SignalFlag = sig_shutdown = true;
  }

 +/**
 + * Handle SIGCHLD so we don't leave zombie child processes.
 + * SIG_IGN'ing it would cause system(3) to malfunction.
 + */
 +static void
 +sh_sigchld(int signum)
 +{
 +	pid_t pid;
 +	int old_errno = errno;
 +
 +	while((pid = waitpid(-1, NULL, WNOHANG)) > 0)
 +		;
 +
 +	errno = old_errno;
 +}

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.47 2022/09/11 19:34:41 kim Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2023 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.