ZyklonB: factor out try_reap_plugin()

This commit is contained in:
Přemysl Eric Janouch 2015-06-15 00:58:56 +02:00
parent e34ecd6bb9
commit f2998db30b
1 changed files with 30 additions and 25 deletions

View File

@ -1787,6 +1787,32 @@ on_plugin_death (struct plugin *plugin, int status)
try_finish_quit (ctx); try_finish_quit (ctx);
} }
static bool
try_reap_plugin (struct bot_context *ctx)
{
int status;
pid_t zombie = waitpid (-1, &status, WNOHANG);
if (zombie == -1)
{
// No children to wait on
if (errno == ECHILD)
return false;
hard_assert (errno == EINTR);
return true;
}
if (zombie == 0)
return false;
struct plugin *plugin = plugin_find_by_pid (ctx, zombie);
// XXX: re-exec if something has died that we don't recognize?
if (soft_assert (plugin != NULL))
on_plugin_death (plugin, status);
return true;
}
static void static void
on_signal_pipe_readable (const struct pollfd *fd, struct bot_context *ctx) on_signal_pipe_readable (const struct pollfd *fd, struct bot_context *ctx)
{ {
@ -1803,31 +1829,10 @@ on_signal_pipe_readable (const struct pollfd *fd, struct bot_context *ctx)
initiate_quit (ctx); initiate_quit (ctx);
} }
// Reap all dead children (since the pipe may overflow, we ask waitpid() // Reap all dead children (since the signal pipe may overflow etc. we run
// to return all the zombies it knows about). // waitpid() in a loop to return all the zombies it knows about).
while (true) while (try_reap_plugin (ctx))
{ ;
int status;
pid_t zombie = waitpid (-1, &status, WNOHANG);
if (zombie == -1)
{
// No children to wait on
if (errno == ECHILD)
break;
hard_assert (errno == EINTR);
continue;
}
if (zombie == 0)
break;
struct plugin *plugin = plugin_find_by_pid (ctx, zombie);
// XXX: re-exec if something has died that we don't recognize?
if (soft_assert (plugin != NULL))
on_plugin_death (plugin, status);
}
} }
int int