<p>laforge has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/osmo-uecups/+/17925">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">dameon: Handle multiple coalesced SIGCHLD<br><br>It turns out signalfd is much less useful than one would assume:<br>https://ldpreload.com/blog/signalfd-is-useless<br><br>Change-Id: I16bc5b7de72843c2ae18b982bfd967a7105313e4<br>---<br>M daemon/main.c<br>1 file changed, 29 insertions(+), 10 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/osmo-uecups refs/changes/25/17925/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/daemon/main.c b/daemon/main.c</span><br><span>index a408df4..fec6f84 100644</span><br><span>--- a/daemon/main.c</span><br><span>+++ b/daemon/main.c</span><br><span>@@ -7,6 +7,7 @@</span><br><span> #include <stdio.h></span><br><span> #include <assert.h></span><br><span> #include <sys/types.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <sys/wait.h></span><br><span> #include <sys/signalfd.h></span><br><span> #include <signal.h></span><br><span> #include <errno.h></span><br><span>@@ -334,26 +335,22 @@</span><br><span>         return NULL;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static void sigchild_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi)</span><br><span style="color: hsl(120, 100%, 40%);">+static void child_terminated(struct gtp_daemon *d, int pid, int status)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-     struct gtp_daemon *d = osfd->data;</span><br><span>        struct subprocess *sproc;</span><br><span>    json_t *jterm_ind;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-  OSMO_ASSERT(fdsi->ssi_signo == SIGCHLD);</span><br><span style="color: hsl(120, 100%, 40%);">+   LOGP(DUECUPS, LOGL_DEBUG, "SIGCHLD receive from pid %u; status=%d\n", pid, status);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-       LOGP(DUECUPS, LOGL_DEBUG, "SIGCHLD receive from pid %u; status=%d\n",</span><br><span style="color: hsl(0, 100%, 40%);">-         fdsi->ssi_pid, fdsi->ssi_status);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- sproc = subprocess_by_pid(d, fdsi->ssi_pid);</span><br><span style="color: hsl(120, 100%, 40%);">+       sproc = subprocess_by_pid(d, pid);</span><br><span>   if (!sproc) {</span><br><span>                LOGP(DUECUPS, LOGL_NOTICE, "subprocess %u terminated (status=%d) but we don't know it?\n",</span><br><span style="color: hsl(0, 100%, 40%);">-                        fdsi->ssi_pid, fdsi->ssi_status);</span><br><span style="color: hsl(120, 100%, 40%);">+                       pid, status);</span><br><span>                return;</span><br><span>      }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   /* FIXME: generate prog_term_ind towards control plane */</span><br><span style="color: hsl(0, 100%, 40%);">-       jterm_ind = gen_uecups_term_ind(fdsi->ssi_pid, fdsi->ssi_status);</span><br><span style="color: hsl(120, 100%, 40%);">+       /* generate prog_term_ind towards control plane */</span><br><span style="color: hsl(120, 100%, 40%);">+    jterm_ind = gen_uecups_term_ind(pid, status);</span><br><span>        if (!jterm_ind)</span><br><span>              return;</span><br><span> </span><br><span>@@ -363,6 +360,28 @@</span><br><span>   talloc_free(sproc);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static void sigchild_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     struct gtp_daemon *d = osfd->data;</span><br><span style="color: hsl(120, 100%, 40%);">+ int pid, status;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    OSMO_ASSERT(fdsi->ssi_signo == SIGCHLD);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ child_terminated(d, fdsi->ssi_pid, fdsi->ssi_status);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* it is known that classic signals coalesce: If you get multiple signals of the</span><br><span style="color: hsl(120, 100%, 40%);">+       * same type before a process is scheduled, the subsequent signaals are dropped.  This</span><br><span style="color: hsl(120, 100%, 40%);">+         * makes sense for SIGINT or something like this, but for SIGCHLD carrying the PID of</span><br><span style="color: hsl(120, 100%, 40%);">+  * the terminated process, it doesn't really.  Linux had the chance to fix this when</span><br><span style="color: hsl(120, 100%, 40%);">+       * introducing signalfd() - but the developers decided not to fix it.  So the signalfd_siginfo</span><br><span style="color: hsl(120, 100%, 40%);">+         * contains the PID of one process that terminated - but there may be any number of other</span><br><span style="color: hsl(120, 100%, 40%);">+      * processes that also have terminated, and for which we don't get events this way. */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  while ((pid = waitpid(-1, &status, WNOHANG)) > 0)</span><br><span style="color: hsl(120, 100%, 40%);">+              child_terminated(d, pid, status);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static json_t *gen_uecups_start_res(pid_t pid, const char *result)</span><br><span> {</span><br><span>  json_t *ret = gen_uecups_result("start_program_res", result);</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/osmo-uecups/+/17925">change 17925</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/osmo-uecups/+/17925"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: osmo-uecups </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I16bc5b7de72843c2ae18b982bfd967a7105313e4 </div>
<div style="display:none"> Gerrit-Change-Number: 17925 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>