Re: Calling system(3) when SIGCHLD is SIG_IGN'd

Rhialto <[email protected]> Sat, 12 Aug 2023 17:43:24 +0200
Newsgroups gmane.comp.window-managers.ctwm
Message-ID <[email protected]>

--OcwYQ/JcyG0cIa07
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sat 12 Aug 2023 at 16:05:34 +0200, Rhialto wrote:
> On Sat 12 Aug 2023 at 15:22:56 +0200, Rhialto wrote:
> > Or, alternatively, a proper signal handler for SIGCHLD could be set up.
>=20
> I propose something like this.
> It worked for me in a quick test, also when I changed the style of my
> .xinitrc file from my usual
>=20
>     xterm &
>     exec ctwm
>=20
> to
>=20
>     ctwm &
>     exec xterm

I meant that the other way around. The bug shows in the first case.
I've also updated it slightly to save and restore errno.

=3D=3D=3D modified file 'signals.c'
--- old/signals.c	2018-11-18 22:08:49 +0000
+++ new/signals.c	2023-08-12 15:03:42 +0000
@@ -8,6 +8,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <sys/wait.h>
+#include <errno.h>
=20
 #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);
=20
=20
 // Internal flags for which signals have called us
@@ -46,9 +49,8 @@
 	// die...
 	signal(SIGALRM, SIG_IGN);
=20
-	// 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);
=20
 	return;
 }
@@ -123,3 +125,18 @@
 	SignalFlag =3D sig_shutdown =3D true;
 }
=20
+/**
+ * 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 =3D errno;
+
+	while((pid =3D waitpid(-1, NULL, WNOHANG)) > 0)
+		;
+
+	errno =3D old_errno;
+}

-Olaf.
--=20
___ Olaf 'Rhialto' Seibert                            <rhialto/at/falu.nl>
\X/ There is no AI. There is just someone else's work.           --I. Rose

--OcwYQ/JcyG0cIa07
Content-Type: application/pgp-signature; name="signature.asc"

-----BEGIN PGP SIGNATURE-----

iQEzBAEBCAAdFiEEFVAhiiWjqgwBVdQAmYnGRWHD+9MFAmTXqJgACgkQmYnGRWHD
+9MJfwf8DBpPOrCt8jpOqR6X1B4Z21Vq9ytfgX0wQjZCin1FVQbBSHIss+VwS3LH
U6N9FieIN8CVDqQYpXz5GDRtHYDRXXOqDfQWLtjNZq7kI5RVbX5Z80xtIYZYw4if
9Ia64S1Z7ZhmmGDqyOAdippGHiloikyV5BOKewzgKSLBWhq6qszuET/EhgCVLFRs
wppTEYmIiOi1RuDxwnSgwIbv2/z3sHgHC+yyraQFPm2puFVVzSFcaNMouyz/0/OQ
UCnZT9wio979nlNEOSMwDxd3V2VscIfq4UyOAaUcmKOyNmyxN+3T9odOPFfDnHu9
8H80Wvrgfk7qeF3irhC6eI+C8Mygog==
=uVpr
-----END PGP SIGNATURE-----

--OcwYQ/JcyG0cIa07--