2 * Copyright (c) 2010 The FreeBSD Foundation
3 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
35 #include <sys/sysctl.h>
57 /* Report processes that are running for too long not often than this value. */
58 #define REPORT_INTERVAL 60
60 /* Are we initialized? */
61 static bool hooks_initialized = false;
64 * Keep all processes we forked on a global queue, so we can report nicely
65 * when they finish or report that they are running for a long time.
67 #define HOOKPROC_MAGIC_ALLOCATED 0x80090ca
68 #define HOOKPROC_MAGIC_ONLIST 0x80090c0
72 /* PID of a forked child. */
74 /* When process were forked? */
76 /* When we logged previous reported? */
78 /* Path to executable and all the arguments we passed. */
79 char hp_comm[PATH_MAX];
80 TAILQ_ENTRY(hookproc) hp_next;
82 static TAILQ_HEAD(, hookproc) hookprocs;
83 static pthread_mutex_t hookprocs_lock;
85 static void hook_remove(struct hookproc *hp);
86 static void hook_free(struct hookproc *hp);
94 * Close all (or almost all) descriptors.
96 if (pjdlog_mode_get() == PJDLOG_MODE_STD) {
97 closefrom(MAX(MAX(STDIN_FILENO, STDOUT_FILENO),
105 * Redirect stdin, stdout and stderr to /dev/null.
107 fd = open(_PATH_DEVNULL, O_RDONLY);
109 pjdlog_errno(LOG_WARNING, "Unable to open %s for reading",
111 } else if (fd != STDIN_FILENO) {
112 if (dup2(fd, STDIN_FILENO) == -1) {
113 pjdlog_errno(LOG_WARNING,
114 "Unable to duplicate descriptor for stdin");
118 fd = open(_PATH_DEVNULL, O_WRONLY);
120 pjdlog_errno(LOG_WARNING, "Unable to open %s for writing",
123 if (fd != STDOUT_FILENO && dup2(fd, STDOUT_FILENO) == -1) {
124 pjdlog_errno(LOG_WARNING,
125 "Unable to duplicate descriptor for stdout");
127 if (fd != STDERR_FILENO && dup2(fd, STDERR_FILENO) == -1) {
128 pjdlog_errno(LOG_WARNING,
129 "Unable to duplicate descriptor for stderr");
131 if (fd != STDOUT_FILENO && fd != STDERR_FILENO)
140 PJDLOG_ASSERT(!hooks_initialized);
142 mtx_init(&hookprocs_lock);
143 TAILQ_INIT(&hookprocs);
144 hooks_initialized = true;
152 PJDLOG_ASSERT(hooks_initialized);
154 mtx_lock(&hookprocs_lock);
155 while ((hp = TAILQ_FIRST(&hookprocs)) != NULL) {
156 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
157 PJDLOG_ASSERT(hp->hp_pid > 0);
162 mtx_unlock(&hookprocs_lock);
164 mtx_destroy(&hookprocs_lock);
165 TAILQ_INIT(&hookprocs);
166 hooks_initialized = false;
169 static struct hookproc *
170 hook_alloc(const char *path, char **args)
175 hp = malloc(sizeof(*hp));
177 pjdlog_error("Unable to allocate %zu bytes of memory for a hook.",
183 hp->hp_birthtime = hp->hp_lastreport = time(NULL);
184 (void)strlcpy(hp->hp_comm, path, sizeof(hp->hp_comm));
185 /* We start at 2nd argument as we don't want to have exec name twice. */
186 for (ii = 1; args[ii] != NULL; ii++) {
187 (void)snprlcat(hp->hp_comm, sizeof(hp->hp_comm), " %s",
190 if (strlen(hp->hp_comm) >= sizeof(hp->hp_comm) - 1) {
191 pjdlog_error("Exec path too long, correct configuration file.");
195 hp->hp_magic = HOOKPROC_MAGIC_ALLOCATED;
200 hook_add(struct hookproc *hp, pid_t pid)
203 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ALLOCATED);
204 PJDLOG_ASSERT(hp->hp_pid == 0);
207 mtx_lock(&hookprocs_lock);
208 hp->hp_magic = HOOKPROC_MAGIC_ONLIST;
209 TAILQ_INSERT_TAIL(&hookprocs, hp, hp_next);
210 mtx_unlock(&hookprocs_lock);
214 hook_remove(struct hookproc *hp)
217 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
218 PJDLOG_ASSERT(hp->hp_pid > 0);
219 PJDLOG_ASSERT(mtx_owned(&hookprocs_lock));
221 TAILQ_REMOVE(&hookprocs, hp, hp_next);
222 hp->hp_magic = HOOKPROC_MAGIC_ALLOCATED;
226 hook_free(struct hookproc *hp)
229 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ALLOCATED);
230 PJDLOG_ASSERT(hp->hp_pid > 0);
236 static struct hookproc *
241 PJDLOG_ASSERT(mtx_owned(&hookprocs_lock));
243 TAILQ_FOREACH(hp, &hookprocs, hp_next) {
244 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
245 PJDLOG_ASSERT(hp->hp_pid > 0);
247 if (hp->hp_pid == pid)
255 hook_check_one(pid_t pid, int status)
259 mtx_lock(&hookprocs_lock);
262 mtx_unlock(&hookprocs_lock);
263 pjdlog_debug(1, "Unknown process pid=%u", pid);
267 mtx_unlock(&hookprocs_lock);
268 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
269 pjdlog_debug(1, "Hook exited gracefully (pid=%u, cmd=[%s]).",
271 } else if (WIFSIGNALED(status)) {
272 pjdlog_error("Hook was killed (pid=%u, signal=%d, cmd=[%s]).",
273 pid, WTERMSIG(status), hp->hp_comm);
275 pjdlog_error("Hook exited ungracefully (pid=%u, exitcode=%d, cmd=[%s]).",
276 pid, WIFEXITED(status) ? WEXITSTATUS(status) : -1,
285 struct hookproc *hp, *hp2;
288 PJDLOG_ASSERT(hooks_initialized);
290 pjdlog_debug(2, "Checking hooks.");
293 * Report about processes that are running for a long time.
296 mtx_lock(&hookprocs_lock);
297 TAILQ_FOREACH_SAFE(hp, &hookprocs, hp_next, hp2) {
298 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
299 PJDLOG_ASSERT(hp->hp_pid > 0);
302 * If process doesn't exists we somehow missed it.
303 * Not much can be done expect for logging this situation.
305 if (kill(hp->hp_pid, 0) == -1 && errno == ESRCH) {
306 pjdlog_warning("Hook disappeared (pid=%u, cmd=[%s]).",
307 hp->hp_pid, hp->hp_comm);
314 * Skip proccesses younger than 1 minute.
316 if (now - hp->hp_lastreport < REPORT_INTERVAL)
320 * Hook is running for too long, report it.
322 pjdlog_warning("Hook is running for %ju seconds (pid=%u, cmd=[%s]).",
323 (uintmax_t)(now - hp->hp_birthtime), hp->hp_pid,
325 hp->hp_lastreport = now;
327 mtx_unlock(&hookprocs_lock);
331 hook_exec(const char *path, ...)
336 hook_execv(path, ap);
341 hook_execv(const char *path, va_list ap)
349 PJDLOG_ASSERT(hooks_initialized);
351 if (path == NULL || path[0] == '\0')
354 memset(args, 0, sizeof(args));
355 args[0] = basename(path);
356 for (ii = 1; ii < sizeof(args) / sizeof(args[0]); ii++) {
357 args[ii] = va_arg(ap, char *);
358 if (args[ii] == NULL)
361 PJDLOG_ASSERT(ii < sizeof(args) / sizeof(args[0]));
363 hp = hook_alloc(path, args);
367 pjdlog_debug(1, "Executing hook: %s", hp->hp_comm);
371 case -1: /* Error. */
372 pjdlog_errno(LOG_ERR, "Unable to fork to execute %s", path);
377 PJDLOG_VERIFY(sigemptyset(&mask) == 0);
378 PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
380 * Dummy handler set for SIGCHLD in the parent will be restored
381 * to SIG_IGN on execv(3) below, so there is no need to do
385 pjdlog_errno(LOG_ERR, "Unable to execute %s", path);
387 default: /* Parent. */