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>
|
|
|
|
|
|
|
|
#include <assert.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 <libgen.h>
|
|
|
|
#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"
|
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)
|
|
|
|
{
|
|
|
|
long maxfd;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close all descriptors.
|
|
|
|
*/
|
|
|
|
maxfd = sysconf(_SC_OPEN_MAX);
|
|
|
|
if (maxfd < 0) {
|
|
|
|
pjdlog_errno(LOG_WARNING, "sysconf(_SC_OPEN_MAX) failed");
|
|
|
|
maxfd = 1024;
|
|
|
|
}
|
2010-08-27 14:35:39 +00:00
|
|
|
for (fd = 0; fd <= maxfd; fd++) {
|
|
|
|
switch (fd) {
|
|
|
|
case STDIN_FILENO:
|
|
|
|
case STDOUT_FILENO:
|
|
|
|
case STDERR_FILENO:
|
|
|
|
if (pjdlog_mode_get() == PJDLOG_MODE_STD)
|
|
|
|
break;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
close(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pjdlog_mode_get() == PJDLOG_MODE_STD)
|
|
|
|
return;
|
2010-02-18 23:16:19 +00:00
|
|
|
/*
|
|
|
|
* Redirect stdin, stdout and stderr to /dev/null.
|
|
|
|
*/
|
|
|
|
fd = open(_PATH_DEVNULL, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to open %s for reading",
|
|
|
|
_PATH_DEVNULL);
|
|
|
|
} else if (fd != STDIN_FILENO) {
|
|
|
|
if (dup2(fd, STDIN_FILENO) < 0) {
|
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to duplicate descriptor for stdin");
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
fd = open(_PATH_DEVNULL, O_WRONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to open %s for writing",
|
|
|
|
_PATH_DEVNULL);
|
|
|
|
} else {
|
|
|
|
if (fd != STDOUT_FILENO && dup2(fd, STDOUT_FILENO) < 0) {
|
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to duplicate descriptor for stdout");
|
|
|
|
}
|
|
|
|
if (fd != STDERR_FILENO && dup2(fd, STDERR_FILENO) < 0) {
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
|
2010-08-29 21:39:49 +00:00
|
|
|
assert(!hooks_initialized);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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++) {
|
|
|
|
(void)strlcat(hp->hp_comm, " ", sizeof(hp->hp_comm));
|
|
|
|
(void)strlcat(hp->hp_comm, args[ii], sizeof(hp->hp_comm));
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(hp->hp_magic == HOOKPROC_MAGIC_ALLOCATED);
|
|
|
|
assert(hp->hp_pid == 0);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
|
|
|
|
assert(hp->hp_pid > 0);
|
|
|
|
assert(mtx_owned(&hookprocs_lock));
|
|
|
|
|
|
|
|
TAILQ_REMOVE(&hookprocs, hp, hp_next);
|
|
|
|
hp->hp_magic = HOOKPROC_MAGIC_ALLOCATED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hook_free(struct hookproc *hp)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(hp->hp_magic == HOOKPROC_MAGIC_ALLOCATED);
|
|
|
|
assert(hp->hp_pid > 0);
|
|
|
|
|
|
|
|
hp->hp_magic = 0;
|
|
|
|
free(hp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct hookproc *
|
|
|
|
hook_find(pid_t pid)
|
|
|
|
{
|
|
|
|
struct hookproc *hp;
|
|
|
|
|
|
|
|
assert(mtx_owned(&hookprocs_lock));
|
|
|
|
|
|
|
|
TAILQ_FOREACH(hp, &hookprocs, hp_next) {
|
|
|
|
assert(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
|
|
|
|
assert(hp->hp_pid > 0);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
assert(hooks_initialized);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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) {
|
|
|
|
assert(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
|
|
|
|
assert(hp->hp_pid > 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
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));
|
|
|
|
args[0] = basename(path);
|
|
|
|
for (ii = 1; ii < sizeof(args) / sizeof(args[0]); ii++) {
|
|
|
|
args[ii] = va_arg(ap, char *);
|
|
|
|
if (args[ii] == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(ii < sizeof(args) / sizeof(args[0]));
|
|
|
|
|
2010-08-27 14:38:12 +00:00
|
|
|
hp = hook_alloc(path, args);
|
|
|
|
if (hp == NULL)
|
|
|
|
return;
|
|
|
|
|
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);
|
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;
|
|
|
|
}
|
|
|
|
}
|