- Add hook_fini() which should be called after fork() from the main hastd
process, once it start to use hooks. - Add hook_check_one() in case the caller expects different child processes and once it can recognize it, it will pass pid and status to hook_check_one(). MFC after: 2 weeks Obtained from: Wheel Systems Sp. z o.o. http://www.wheelsystems.com
This commit is contained in:
parent
572cdb2216
commit
25ec2e3e2b
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=211976
@ -82,6 +82,9 @@ struct hookproc {
|
||||
static TAILQ_HEAD(, hookproc) hookprocs;
|
||||
static pthread_mutex_t hookprocs_lock;
|
||||
|
||||
static void hook_remove(struct hookproc *hp);
|
||||
static void hook_free(struct hookproc *hp);
|
||||
|
||||
static void
|
||||
descriptors(void)
|
||||
{
|
||||
@ -147,11 +150,35 @@ void
|
||||
hook_init(void)
|
||||
{
|
||||
|
||||
assert(!hooks_initialized);
|
||||
|
||||
mtx_init(&hookprocs_lock);
|
||||
TAILQ_INIT(&hookprocs);
|
||||
hooks_initialized = true;
|
||||
}
|
||||
|
||||
void
|
||||
hook_fini(void)
|
||||
{
|
||||
struct hookproc *hp;
|
||||
|
||||
assert(hooks_initialized);
|
||||
|
||||
mtx_lock(&hookprocs_lock);
|
||||
while ((hp = TAILQ_FIRST(&hookprocs)) != NULL) {
|
||||
assert(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
|
||||
assert(hp->hp_pid > 0);
|
||||
|
||||
hook_remove(hp);
|
||||
hook_free(hp);
|
||||
}
|
||||
mtx_unlock(&hookprocs_lock);
|
||||
|
||||
mtx_destroy(&hookprocs_lock);
|
||||
TAILQ_INIT(&hookprocs);
|
||||
hooks_initialized = false;
|
||||
}
|
||||
|
||||
static struct hookproc *
|
||||
hook_alloc(const char *path, char **args)
|
||||
{
|
||||
@ -238,25 +265,16 @@ hook_find(pid_t pid)
|
||||
}
|
||||
|
||||
void
|
||||
hook_check(bool sigchld)
|
||||
hook_check_one(pid_t pid, int status)
|
||||
{
|
||||
struct hookproc *hp, *hp2;
|
||||
int status;
|
||||
time_t now;
|
||||
pid_t pid;
|
||||
struct hookproc *hp;
|
||||
|
||||
assert(hooks_initialized);
|
||||
|
||||
/*
|
||||
* If SIGCHLD was received, garbage collect finished processes.
|
||||
*/
|
||||
while (sigchld && (pid = wait3(&status, WNOHANG, NULL)) > 0) {
|
||||
mtx_lock(&hookprocs_lock);
|
||||
hp = hook_find(pid);
|
||||
if (hp == NULL) {
|
||||
mtx_unlock(&hookprocs_lock);
|
||||
pjdlog_warning("Unknown process pid=%u", pid);
|
||||
continue;
|
||||
pjdlog_debug(1, "Unknown process pid=%u", pid);
|
||||
return;
|
||||
}
|
||||
hook_remove(hp);
|
||||
mtx_unlock(&hookprocs_lock);
|
||||
@ -274,6 +292,24 @@ hook_check(bool sigchld)
|
||||
hook_free(hp);
|
||||
}
|
||||
|
||||
void
|
||||
hook_check(bool sigchld)
|
||||
{
|
||||
struct hookproc *hp, *hp2;
|
||||
int status;
|
||||
time_t now;
|
||||
pid_t pid;
|
||||
|
||||
assert(hooks_initialized);
|
||||
|
||||
/*
|
||||
* If SIGCHLD was received, garbage collect finished processes.
|
||||
*/
|
||||
if (sigchld) {
|
||||
while ((pid = wait3(&status, WNOHANG, NULL)) > 0)
|
||||
hook_check_one(pid, status);
|
||||
}
|
||||
|
||||
/*
|
||||
* Report about processes that are running for a long time.
|
||||
*/
|
||||
|
@ -33,10 +33,14 @@
|
||||
#ifndef _HOOKS_H_
|
||||
#define _HOOKS_H_
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
void hook_init(void);
|
||||
void hook_fini(void);
|
||||
void hook_check_one(pid_t pid, int status);
|
||||
void hook_check(bool sigchld);
|
||||
void hook_exec(const char *path, ...);
|
||||
void hook_execv(const char *path, va_list ap);
|
||||
|
Loading…
Reference in New Issue
Block a user