2010-02-18 23:16:19 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2010-02-18 23:16:19 +00:00
|
|
|
* Copyright (c) 2010 The FreeBSD Foundation
|
2010-08-27 14:38:12 +00:00
|
|
|
* Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
|
2010-02-18 23:16:19 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by Pawel Jakub Dawidek under sponsorship from
|
|
|
|
* the FreeBSD Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2010-08-27 14:38:12 +00:00
|
|
|
#include <sys/sysctl.h>
|
2010-02-18 23:16:19 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2010-08-27 14:38:12 +00:00
|
|
|
#include <errno.h>
|
2010-02-18 23:16:19 +00:00
|
|
|
#include <fcntl.h>
|
2010-08-27 14:38:12 +00:00
|
|
|
#include <paths.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2010-02-18 23:16:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
2010-08-27 14:38:12 +00:00
|
|
|
#include <unistd.h>
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
#include <pjdlog.h>
|
|
|
|
|
|
|
|
#include "hooks.h"
|
2011-03-21 08:37:50 +00:00
|
|
|
#include "subr.h"
|
2010-08-27 14:38:12 +00:00
|
|
|
#include "synch.h"
|
|
|
|
|
|
|
|
/* Report processes that are running for too long not often than this value. */
|
|
|
|
#define REPORT_INTERVAL 60
|
|
|
|
|
|
|
|
/* Are we initialized? */
|
|
|
|
static bool hooks_initialized = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Keep all processes we forked on a global queue, so we can report nicely
|
|
|
|
* when they finish or report that they are running for a long time.
|
|
|
|
*/
|
|
|
|
#define HOOKPROC_MAGIC_ALLOCATED 0x80090ca
|
|
|
|
#define HOOKPROC_MAGIC_ONLIST 0x80090c0
|
|
|
|
struct hookproc {
|
|
|
|
/* Magic. */
|
|
|
|
int hp_magic;
|
|
|
|
/* PID of a forked child. */
|
|
|
|
pid_t hp_pid;
|
|
|
|
/* When process were forked? */
|
|
|
|
time_t hp_birthtime;
|
|
|
|
/* When we logged previous reported? */
|
|
|
|
time_t hp_lastreport;
|
|
|
|
/* Path to executable and all the arguments we passed. */
|
|
|
|
char hp_comm[PATH_MAX];
|
|
|
|
TAILQ_ENTRY(hookproc) hp_next;
|
|
|
|
};
|
|
|
|
static TAILQ_HEAD(, hookproc) hookprocs;
|
|
|
|
static pthread_mutex_t hookprocs_lock;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2010-08-29 21:39:49 +00:00
|
|
|
static void hook_remove(struct hookproc *hp);
|
|
|
|
static void hook_free(struct hookproc *hp);
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
static void
|
|
|
|
descriptors(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/*
|
2010-10-20 21:10:01 +00:00
|
|
|
* Close all (or almost all) descriptors.
|
2010-02-18 23:16:19 +00:00
|
|
|
*/
|
2010-10-20 21:10:01 +00:00
|
|
|
if (pjdlog_mode_get() == PJDLOG_MODE_STD) {
|
|
|
|
closefrom(MAX(MAX(STDIN_FILENO, STDOUT_FILENO),
|
|
|
|
STDERR_FILENO) + 1);
|
2010-08-27 14:35:39 +00:00
|
|
|
return;
|
2010-10-20 21:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
closefrom(0);
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* Redirect stdin, stdout and stderr to /dev/null.
|
|
|
|
*/
|
|
|
|
fd = open(_PATH_DEVNULL, O_RDONLY);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (fd == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to open %s for reading",
|
|
|
|
_PATH_DEVNULL);
|
|
|
|
} else if (fd != STDIN_FILENO) {
|
2012-01-10 22:39:07 +00:00
|
|
|
if (dup2(fd, STDIN_FILENO) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to duplicate descriptor for stdin");
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
fd = open(_PATH_DEVNULL, O_WRONLY);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (fd == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to open %s for writing",
|
|
|
|
_PATH_DEVNULL);
|
|
|
|
} else {
|
2012-01-10 22:39:07 +00:00
|
|
|
if (fd != STDOUT_FILENO && dup2(fd, STDOUT_FILENO) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to duplicate descriptor for stdout");
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
if (fd != STDERR_FILENO && dup2(fd, STDERR_FILENO) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to duplicate descriptor for stderr");
|
|
|
|
}
|
|
|
|
if (fd != STDOUT_FILENO && fd != STDERR_FILENO)
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-27 14:38:12 +00:00
|
|
|
void
|
|
|
|
hook_init(void)
|
|
|
|
{
|
|
|
|
|
2011-09-27 08:50:37 +00:00
|
|
|
PJDLOG_ASSERT(!hooks_initialized);
|
2010-08-29 21:39:49 +00:00
|
|
|
|
2010-08-27 14:38:12 +00:00
|
|
|
mtx_init(&hookprocs_lock);
|
|
|
|
TAILQ_INIT(&hookprocs);
|
|
|
|
hooks_initialized = true;
|
|
|
|
}
|
|
|
|
|
2010-08-29 21:39:49 +00:00
|
|
|
void
|
|
|
|
hook_fini(void)
|
|
|
|
{
|
|
|
|
struct hookproc *hp;
|
|
|
|
|
2011-09-27 08:50:37 +00:00
|
|
|
PJDLOG_ASSERT(hooks_initialized);
|
2010-08-29 21:39:49 +00:00
|
|
|
|
|
|
|
mtx_lock(&hookprocs_lock);
|
|
|
|
while ((hp = TAILQ_FIRST(&hookprocs)) != NULL) {
|
2011-09-27 08:50:37 +00:00
|
|
|
PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
|
|
|
|
PJDLOG_ASSERT(hp->hp_pid > 0);
|
2010-08-29 21:39:49 +00:00
|
|
|
|
|
|
|
hook_remove(hp);
|
|
|
|
hook_free(hp);
|
|
|
|
}
|
|
|
|
mtx_unlock(&hookprocs_lock);
|
|
|
|
|
|
|
|
mtx_destroy(&hookprocs_lock);
|
|
|
|
TAILQ_INIT(&hookprocs);
|
|
|
|
hooks_initialized = false;
|
|
|
|
}
|
|
|
|
|
2010-08-27 14:38:12 +00:00
|
|
|
static struct hookproc *
|
|
|
|
hook_alloc(const char *path, char **args)
|
|
|
|
{
|
|
|
|
struct hookproc *hp;
|
|
|
|
unsigned int ii;
|
|
|
|
|
|
|
|
hp = malloc(sizeof(*hp));
|
|
|
|
if (hp == NULL) {
|
|
|
|
pjdlog_error("Unable to allocate %zu bytes of memory for a hook.",
|
|
|
|
sizeof(*hp));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
hp->hp_pid = 0;
|
|
|
|
hp->hp_birthtime = hp->hp_lastreport = time(NULL);
|
|
|
|
(void)strlcpy(hp->hp_comm, path, sizeof(hp->hp_comm));
|
|
|
|
/* We start at 2nd argument as we don't want to have exec name twice. */
|
|
|
|
for (ii = 1; args[ii] != NULL; ii++) {
|
2011-03-21 08:37:50 +00:00
|
|
|
(void)snprlcat(hp->hp_comm, sizeof(hp->hp_comm), " %s",
|
|
|
|
args[ii]);
|
2010-08-27 14:38:12 +00:00
|
|
|
}
|
|
|
|
if (strlen(hp->hp_comm) >= sizeof(hp->hp_comm) - 1) {
|
|
|
|
pjdlog_error("Exec path too long, correct configuration file.");
|
|
|
|
free(hp);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
hp->hp_magic = HOOKPROC_MAGIC_ALLOCATED;
|
|
|
|
return (hp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hook_add(struct hookproc *hp, pid_t pid)
|
|
|
|
{
|
|
|
|
|
2011-09-27 08:50:37 +00:00
|
|
|
PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ALLOCATED);
|
|
|
|
PJDLOG_ASSERT(hp->hp_pid == 0);
|
2010-08-27 14:38:12 +00:00
|
|
|
|
|
|
|
hp->hp_pid = pid;
|
|
|
|
mtx_lock(&hookprocs_lock);
|
|
|
|
hp->hp_magic = HOOKPROC_MAGIC_ONLIST;
|
|
|
|
TAILQ_INSERT_TAIL(&hookprocs, hp, hp_next);
|
|
|
|
mtx_unlock(&hookprocs_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hook_remove(struct hookproc *hp)
|
|
|
|
{
|
|
|
|
|
2011-09-27 08:50:37 +00:00
|
|
|
PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
|
|
|
|
PJDLOG_ASSERT(hp->hp_pid > 0);
|
|
|
|
PJDLOG_ASSERT(mtx_owned(&hookprocs_lock));
|
2010-08-27 14:38:12 +00:00
|
|
|
|
|
|
|
TAILQ_REMOVE(&hookprocs, hp, hp_next);
|
|
|
|
hp->hp_magic = HOOKPROC_MAGIC_ALLOCATED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hook_free(struct hookproc *hp)
|
|
|
|
{
|
|
|
|
|
2011-09-27 08:50:37 +00:00
|
|
|
PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ALLOCATED);
|
|
|
|
PJDLOG_ASSERT(hp->hp_pid > 0);
|
2010-08-27 14:38:12 +00:00
|
|
|
|
|
|
|
hp->hp_magic = 0;
|
|
|
|
free(hp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct hookproc *
|
|
|
|
hook_find(pid_t pid)
|
|
|
|
{
|
|
|
|
struct hookproc *hp;
|
|
|
|
|
2011-09-27 08:50:37 +00:00
|
|
|
PJDLOG_ASSERT(mtx_owned(&hookprocs_lock));
|
2010-08-27 14:38:12 +00:00
|
|
|
|
|
|
|
TAILQ_FOREACH(hp, &hookprocs, hp_next) {
|
2011-09-27 08:50:37 +00:00
|
|
|
PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
|
|
|
|
PJDLOG_ASSERT(hp->hp_pid > 0);
|
2010-08-27 14:38:12 +00:00
|
|
|
|
|
|
|
if (hp->hp_pid == pid)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (hp);
|
|
|
|
}
|
|
|
|
|
2010-08-29 21:39:49 +00:00
|
|
|
void
|
|
|
|
hook_check_one(pid_t pid, int status)
|
|
|
|
{
|
|
|
|
struct hookproc *hp;
|
|
|
|
|
|
|
|
mtx_lock(&hookprocs_lock);
|
|
|
|
hp = hook_find(pid);
|
|
|
|
if (hp == NULL) {
|
|
|
|
mtx_unlock(&hookprocs_lock);
|
|
|
|
pjdlog_debug(1, "Unknown process pid=%u", pid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hook_remove(hp);
|
|
|
|
mtx_unlock(&hookprocs_lock);
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
|
|
|
|
pjdlog_debug(1, "Hook exited gracefully (pid=%u, cmd=[%s]).",
|
|
|
|
pid, hp->hp_comm);
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
pjdlog_error("Hook was killed (pid=%u, signal=%d, cmd=[%s]).",
|
|
|
|
pid, WTERMSIG(status), hp->hp_comm);
|
|
|
|
} else {
|
|
|
|
pjdlog_error("Hook exited ungracefully (pid=%u, exitcode=%d, cmd=[%s]).",
|
|
|
|
pid, WIFEXITED(status) ? WEXITSTATUS(status) : -1,
|
|
|
|
hp->hp_comm);
|
|
|
|
}
|
|
|
|
hook_free(hp);
|
|
|
|
}
|
|
|
|
|
2010-08-27 14:38:12 +00:00
|
|
|
void
|
2010-10-04 21:43:06 +00:00
|
|
|
hook_check(void)
|
2010-08-27 14:38:12 +00:00
|
|
|
{
|
|
|
|
struct hookproc *hp, *hp2;
|
|
|
|
time_t now;
|
|
|
|
|
2011-09-27 08:50:37 +00:00
|
|
|
PJDLOG_ASSERT(hooks_initialized);
|
2010-08-27 14:38:12 +00:00
|
|
|
|
2011-03-21 14:53:27 +00:00
|
|
|
pjdlog_debug(2, "Checking hooks.");
|
2011-03-21 08:38:24 +00:00
|
|
|
|
2010-08-27 14:38:12 +00:00
|
|
|
/*
|
|
|
|
* Report about processes that are running for a long time.
|
|
|
|
*/
|
|
|
|
now = time(NULL);
|
|
|
|
mtx_lock(&hookprocs_lock);
|
|
|
|
TAILQ_FOREACH_SAFE(hp, &hookprocs, hp_next, hp2) {
|
2011-09-27 08:50:37 +00:00
|
|
|
PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
|
|
|
|
PJDLOG_ASSERT(hp->hp_pid > 0);
|
2010-08-27 14:38:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If process doesn't exists we somehow missed it.
|
|
|
|
* Not much can be done expect for logging this situation.
|
|
|
|
*/
|
|
|
|
if (kill(hp->hp_pid, 0) == -1 && errno == ESRCH) {
|
|
|
|
pjdlog_warning("Hook disappeared (pid=%u, cmd=[%s]).",
|
|
|
|
hp->hp_pid, hp->hp_comm);
|
|
|
|
hook_remove(hp);
|
|
|
|
hook_free(hp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip proccesses younger than 1 minute.
|
|
|
|
*/
|
|
|
|
if (now - hp->hp_lastreport < REPORT_INTERVAL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hook is running for too long, report it.
|
|
|
|
*/
|
|
|
|
pjdlog_warning("Hook is running for %ju seconds (pid=%u, cmd=[%s]).",
|
|
|
|
(uintmax_t)(now - hp->hp_birthtime), hp->hp_pid,
|
|
|
|
hp->hp_comm);
|
|
|
|
hp->hp_lastreport = now;
|
|
|
|
}
|
|
|
|
mtx_unlock(&hookprocs_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-02-18 23:16:19 +00:00
|
|
|
hook_exec(const char *path, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, path);
|
2010-08-27 14:38:12 +00:00
|
|
|
hook_execv(path, ap);
|
2010-02-18 23:16:19 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2010-08-27 14:38:12 +00:00
|
|
|
void
|
2010-02-18 23:16:19 +00:00
|
|
|
hook_execv(const char *path, va_list ap)
|
|
|
|
{
|
2010-08-27 14:38:12 +00:00
|
|
|
struct hookproc *hp;
|
2010-02-18 23:16:19 +00:00
|
|
|
char *args[64];
|
|
|
|
unsigned int ii;
|
2010-10-16 22:48:48 +00:00
|
|
|
sigset_t mask;
|
2010-08-27 14:38:12 +00:00
|
|
|
pid_t pid;
|
|
|
|
|
2011-09-27 08:50:37 +00:00
|
|
|
PJDLOG_ASSERT(hooks_initialized);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
if (path == NULL || path[0] == '\0')
|
2010-08-27 14:38:12 +00:00
|
|
|
return;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
memset(args, 0, sizeof(args));
|
2016-05-01 08:22:11 +00:00
|
|
|
args[0] = __DECONST(char *, path);
|
2010-02-18 23:16:19 +00:00
|
|
|
for (ii = 1; ii < sizeof(args) / sizeof(args[0]); ii++) {
|
|
|
|
args[ii] = va_arg(ap, char *);
|
|
|
|
if (args[ii] == NULL)
|
|
|
|
break;
|
|
|
|
}
|
2011-09-27 08:50:37 +00:00
|
|
|
PJDLOG_ASSERT(ii < sizeof(args) / sizeof(args[0]));
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2010-08-27 14:38:12 +00:00
|
|
|
hp = hook_alloc(path, args);
|
|
|
|
if (hp == NULL)
|
|
|
|
return;
|
|
|
|
|
2011-03-21 08:38:24 +00:00
|
|
|
pjdlog_debug(1, "Executing hook: %s", hp->hp_comm);
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
pid = fork();
|
|
|
|
switch (pid) {
|
|
|
|
case -1: /* Error. */
|
2010-08-27 14:38:12 +00:00
|
|
|
pjdlog_errno(LOG_ERR, "Unable to fork to execute %s", path);
|
2010-09-26 10:39:01 +00:00
|
|
|
hook_free(hp);
|
2010-08-27 14:38:12 +00:00
|
|
|
return;
|
2010-02-18 23:16:19 +00:00
|
|
|
case 0: /* Child. */
|
|
|
|
descriptors();
|
2010-10-16 22:48:48 +00:00
|
|
|
PJDLOG_VERIFY(sigemptyset(&mask) == 0);
|
|
|
|
PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
|
2011-01-12 14:38:17 +00:00
|
|
|
/*
|
2011-01-12 16:16:54 +00:00
|
|
|
* Dummy handler set for SIGCHLD in the parent will be restored
|
|
|
|
* to SIG_IGN on execv(3) below, so there is no need to do
|
|
|
|
* anything with it.
|
2011-01-12 14:38:17 +00:00
|
|
|
*/
|
2010-02-18 23:16:19 +00:00
|
|
|
execv(path, args);
|
|
|
|
pjdlog_errno(LOG_ERR, "Unable to execute %s", path);
|
|
|
|
exit(EX_SOFTWARE);
|
|
|
|
default: /* Parent. */
|
2010-08-27 14:38:12 +00:00
|
|
|
hook_add(hp, pid);
|
2010-02-18 23:16:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|