2014-04-09 19:45:27 +02:00
|
|
|
/* Copyright (c) 2003-2004, Roger Dingledine
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2018-06-20 14:13:28 +02:00
|
|
|
* Copyright (c) 2007-2018, The Tor Project, Inc. */
|
2014-04-09 19:45:27 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
2018-07-10 18:22:01 +02:00
|
|
|
* \file waitpid.c
|
|
|
|
* \brief Convenience structures for handlers for handling waitpid().
|
2014-04-09 19:45:27 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
|
2018-06-28 16:53:34 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
#include "lib/process/waitpid.h"
|
|
|
|
#include "lib/log/torlog.h"
|
|
|
|
#include "lib/log/util_bug.h"
|
2018-07-10 21:16:57 +02:00
|
|
|
#include "lib/malloc/malloc.h"
|
2018-06-28 16:53:34 +02:00
|
|
|
#include "ht.h"
|
|
|
|
|
2014-04-09 19:45:27 +02:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
|
|
|
|
2018-06-28 16:53:34 +02:00
|
|
|
#include <string.h>
|
2014-04-09 19:45:27 +02:00
|
|
|
|
|
|
|
/* ================================================== */
|
|
|
|
/* Convenience structures for handlers for waitpid().
|
|
|
|
*
|
|
|
|
* The tor_process_monitor*() code above doesn't use them, since it is for
|
|
|
|
* monitoring a non-child process.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Mapping from a PID to a userfn/userdata pair. */
|
|
|
|
struct waitpid_callback_t {
|
|
|
|
HT_ENTRY(waitpid_callback_t) node;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
void (*userfn)(int, void *userdata);
|
|
|
|
void *userdata;
|
|
|
|
|
|
|
|
unsigned running;
|
|
|
|
};
|
|
|
|
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline unsigned int
|
2014-04-09 19:45:27 +02:00
|
|
|
process_map_entry_hash_(const waitpid_callback_t *ent)
|
|
|
|
{
|
|
|
|
return (unsigned) ent->pid;
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline unsigned int
|
2014-06-16 21:18:02 +02:00
|
|
|
process_map_entries_eq_(const waitpid_callback_t *a,
|
|
|
|
const waitpid_callback_t *b)
|
2014-04-09 19:45:27 +02:00
|
|
|
{
|
|
|
|
return a->pid == b->pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HT_HEAD(process_map, waitpid_callback_t) process_map = HT_INITIALIZER();
|
|
|
|
|
|
|
|
HT_PROTOTYPE(process_map, waitpid_callback_t, node, process_map_entry_hash_,
|
2016-06-02 15:09:37 +02:00
|
|
|
process_map_entries_eq_)
|
2014-09-02 18:48:34 +02:00
|
|
|
HT_GENERATE2(process_map, waitpid_callback_t, node, process_map_entry_hash_,
|
2016-06-02 15:09:37 +02:00
|
|
|
process_map_entries_eq_, 0.6, tor_reallocarray_, tor_free_)
|
2014-04-09 19:45:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Begin monitoring the child pid <b>pid</b> to see if we get a SIGCHLD for
|
|
|
|
* it. If we eventually do, call <b>fn</b>, passing it the exit status (as
|
|
|
|
* yielded by waitpid) and the pointer <b>arg</b>.
|
|
|
|
*
|
|
|
|
* To cancel this, or clean up after it has triggered, call
|
|
|
|
* clear_waitpid_callback().
|
|
|
|
*/
|
|
|
|
waitpid_callback_t *
|
|
|
|
set_waitpid_callback(pid_t pid, void (*fn)(int, void *), void *arg)
|
|
|
|
{
|
|
|
|
waitpid_callback_t *old_ent;
|
|
|
|
waitpid_callback_t *ent = tor_malloc_zero(sizeof(waitpid_callback_t));
|
|
|
|
ent->pid = pid;
|
|
|
|
ent->userfn = fn;
|
|
|
|
ent->userdata = arg;
|
|
|
|
ent->running = 1;
|
|
|
|
|
|
|
|
old_ent = HT_REPLACE(process_map, &process_map, ent);
|
|
|
|
if (old_ent) {
|
|
|
|
log_warn(LD_BUG, "Replaced a waitpid monitor on pid %u. That should be "
|
|
|
|
"impossible.", (unsigned) pid);
|
|
|
|
old_ent->running = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel a waitpid_callback_t, or clean up after one has triggered. Releases
|
|
|
|
* all storage held by <b>ent</b>.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
clear_waitpid_callback(waitpid_callback_t *ent)
|
|
|
|
{
|
|
|
|
waitpid_callback_t *old_ent;
|
|
|
|
if (ent == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ent->running) {
|
|
|
|
old_ent = HT_REMOVE(process_map, &process_map, ent);
|
|
|
|
if (old_ent != ent) {
|
|
|
|
log_warn(LD_BUG, "Couldn't remove waitpid monitor for pid %u.",
|
|
|
|
(unsigned) ent->pid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_free(ent);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper: find the callack for <b>pid</b>; if there is one, run it,
|
|
|
|
* reporting the exit status as <b>status</b>. */
|
|
|
|
static void
|
|
|
|
notify_waitpid_callback_by_pid(pid_t pid, int status)
|
|
|
|
{
|
|
|
|
waitpid_callback_t search, *ent;
|
|
|
|
|
|
|
|
search.pid = pid;
|
|
|
|
ent = HT_REMOVE(process_map, &process_map, &search);
|
|
|
|
if (!ent || !ent->running) {
|
|
|
|
log_info(LD_GENERAL, "Child process %u has exited; no callback was "
|
|
|
|
"registered", (unsigned)pid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_info(LD_GENERAL, "Child process %u has exited; running callback.",
|
|
|
|
(unsigned)pid);
|
|
|
|
|
|
|
|
ent->running = 0;
|
|
|
|
ent->userfn(status, ent->userdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Use waitpid() to wait for all children that have exited, and invoke any
|
|
|
|
* callbacks registered for them. */
|
|
|
|
void
|
|
|
|
notify_pending_waitpid_callbacks(void)
|
|
|
|
{
|
|
|
|
/* I was going to call this function reap_zombie_children(), but
|
|
|
|
* that makes it sound way more exciting than it really is. */
|
|
|
|
pid_t child;
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
while ((child = waitpid(-1, &status, WNOHANG)) > 0) {
|
|
|
|
notify_waitpid_callback_by_pid(child, status);
|
|
|
|
status = 0; /* should be needless */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(_WIN32) */
|