From: Arran Cudbard-Bell Date: Sat, 28 Aug 2021 16:43:34 +0000 (-0500) Subject: Add pid reaping function X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7e7c59279a786a6ad9f2c4470ea4cf6f41807d2e;p=thirdparty%2Ffreeradius-server.git Add pid reaping function --- diff --git a/src/lib/util/event.c b/src/lib/util/event.c index b37a48a928e..5dfc3ae54e0 100644 --- a/src/lib/util/event.c +++ b/src/lib/util/event.c @@ -1679,6 +1679,32 @@ int _fr_event_pid_wait(NDEBUG_LOCATION_ARGS return 0; } +/** Does the actual reaping of PIDs + * + */ +static void _fr_event_pid_reap_cb(UNUSED fr_event_list_t *el, pid_t pid, int status, UNUSED void *uctx) +{ + waitpid(pid, &status, WNOHANG); /* Don't block the process if there's a logic error somewhere */ +} + +/** Asynchronously wait for a PID to exit, then reap it + * + * This is intended to be used when we no longer care about a process + * exiting, but we still want to clean up its state so we don't have + * zombie processes sticking around. + * + * @param[in] el to use to reap the process. + * @param[in] pid to reap. + * @return + * - -1 if we couldn't find the process or it has already exited/been reaped. + * - 0 on success (we setup a process handler). + */ +int _fr_event_pid_reap(NDEBUG_LOCATION_ARGS fr_event_list_t *el, pid_t pid) +{ + return _fr_event_pid_wait(NDEBUG_LOCATION_VALS NULL, el, NULL, pid, _fr_event_pid_reap_cb, NULL); +} + + /** Add a user callback to the event list. * * @param[in] el Containing the timer events. diff --git a/src/lib/util/event.h b/src/lib/util/event.h index babb65b5d11..03ea604a532 100644 --- a/src/lib/util/event.h +++ b/src/lib/util/event.h @@ -257,6 +257,10 @@ int _fr_event_pid_wait(NDEBUG_LOCATION_ARGS CC_HINT(nonnull(NDEBUG_LOCATION_NONNULL(2))); #define fr_event_pid_wait(...) _fr_event_pid_wait(NDEBUG_LOCATION_EXP __VA_ARGS__) +int _fr_event_pid_reap(NDEBUG_LOCATION_ARGS fr_event_list_t *el, pid_t pid) + CC_HINT(nonnull(NDEBUG_LOCATION_NONNULL(1))); +#define fr_event_pid_reap(...) _fr_event_pid_reap(NDEBUG_LOCATION_EXP __VA_ARGS__) + int fr_event_timer_run(fr_event_list_t *el, fr_time_t *when); uintptr_t fr_event_user_insert(fr_event_list_t *el, fr_event_user_handler_t user, void *uctx) CC_HINT(nonnull(1,2));