2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 The FreeBSD Foundation
5 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
8 * This software was developed by Pawel Jakub Dawidek under sponsorship from
9 * the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/sysctl.h>
58 /* Report processes that are running for too long not often than this value. */
59 #define REPORT_INTERVAL 60
61 /* Are we initialized? */
62 static bool hooks_initialized = false;
65 * Keep all processes we forked on a global queue, so we can report nicely
66 * when they finish or report that they are running for a long time.
68 #define HOOKPROC_MAGIC_ALLOCATED 0x80090ca
69 #define HOOKPROC_MAGIC_ONLIST 0x80090c0
73 /* PID of a forked child. */
75 /* When process were forked? */
77 /* When we logged previous reported? */
79 /* Path to executable and all the arguments we passed. */
80 char hp_comm[PATH_MAX];
81 TAILQ_ENTRY(hookproc) hp_next;
83 static TAILQ_HEAD(, hookproc) hookprocs;
84 static pthread_mutex_t hookprocs_lock;
86 static void hook_remove(struct hookproc *hp);
87 static void hook_free(struct hookproc *hp);
95 * Close all (or almost all) descriptors.
97 if (pjdlog_mode_get() == PJDLOG_MODE_STD) {
98 closefrom(MAX(MAX(STDIN_FILENO, STDOUT_FILENO),
106 * Redirect stdin, stdout and stderr to /dev/null.
108 fd = open(_PATH_DEVNULL, O_RDONLY);
110 pjdlog_errno(LOG_WARNING, "Unable to open %s for reading",
112 } else if (fd != STDIN_FILENO) {
113 if (dup2(fd, STDIN_FILENO) == -1) {
114 pjdlog_errno(LOG_WARNING,
115 "Unable to duplicate descriptor for stdin");
119 fd = open(_PATH_DEVNULL, O_WRONLY);
121 pjdlog_errno(LOG_WARNING, "Unable to open %s for writing",
124 if (fd != STDOUT_FILENO && dup2(fd, STDOUT_FILENO) == -1) {
125 pjdlog_errno(LOG_WARNING,
126 "Unable to duplicate descriptor for stdout");
128 if (fd != STDERR_FILENO && dup2(fd, STDERR_FILENO) == -1) {
129 pjdlog_errno(LOG_WARNING,
130 "Unable to duplicate descriptor for stderr");
132 if (fd != STDOUT_FILENO && fd != STDERR_FILENO)
141 PJDLOG_ASSERT(!hooks_initialized);
143 mtx_init(&hookprocs_lock);
144 TAILQ_INIT(&hookprocs);
145 hooks_initialized = true;
153 PJDLOG_ASSERT(hooks_initialized);
155 mtx_lock(&hookprocs_lock);
156 while ((hp = TAILQ_FIRST(&hookprocs)) != NULL) {
157 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
158 PJDLOG_ASSERT(hp->hp_pid > 0);
163 mtx_unlock(&hookprocs_lock);
165 mtx_destroy(&hookprocs_lock);
166 TAILQ_INIT(&hookprocs);
167 hooks_initialized = false;
170 static struct hookproc *
171 hook_alloc(const char *path, char **args)
176 hp = malloc(sizeof(*hp));
178 pjdlog_error("Unable to allocate %zu bytes of memory for a hook.",
184 hp->hp_birthtime = hp->hp_lastreport = time(NULL);
185 (void)strlcpy(hp->hp_comm, path, sizeof(hp->hp_comm));
186 /* We start at 2nd argument as we don't want to have exec name twice. */
187 for (ii = 1; args[ii] != NULL; ii++) {
188 (void)snprlcat(hp->hp_comm, sizeof(hp->hp_comm), " %s",
191 if (strlen(hp->hp_comm) >= sizeof(hp->hp_comm) - 1) {
192 pjdlog_error("Exec path too long, correct configuration file.");
196 hp->hp_magic = HOOKPROC_MAGIC_ALLOCATED;
201 hook_add(struct hookproc *hp, pid_t pid)
204 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ALLOCATED);
205 PJDLOG_ASSERT(hp->hp_pid == 0);
208 mtx_lock(&hookprocs_lock);
209 hp->hp_magic = HOOKPROC_MAGIC_ONLIST;
210 TAILQ_INSERT_TAIL(&hookprocs, hp, hp_next);
211 mtx_unlock(&hookprocs_lock);
215 hook_remove(struct hookproc *hp)
218 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
219 PJDLOG_ASSERT(hp->hp_pid > 0);
220 PJDLOG_ASSERT(mtx_owned(&hookprocs_lock));
222 TAILQ_REMOVE(&hookprocs, hp, hp_next);
223 hp->hp_magic = HOOKPROC_MAGIC_ALLOCATED;
227 hook_free(struct hookproc *hp)
230 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ALLOCATED);
231 PJDLOG_ASSERT(hp->hp_pid > 0);
237 static struct hookproc *
242 PJDLOG_ASSERT(mtx_owned(&hookprocs_lock));
244 TAILQ_FOREACH(hp, &hookprocs, hp_next) {
245 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
246 PJDLOG_ASSERT(hp->hp_pid > 0);
248 if (hp->hp_pid == pid)
256 hook_check_one(pid_t pid, int status)
260 mtx_lock(&hookprocs_lock);
263 mtx_unlock(&hookprocs_lock);
264 pjdlog_debug(1, "Unknown process pid=%u", pid);
268 mtx_unlock(&hookprocs_lock);
269 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
270 pjdlog_debug(1, "Hook exited gracefully (pid=%u, cmd=[%s]).",
272 } else if (WIFSIGNALED(status)) {
273 pjdlog_error("Hook was killed (pid=%u, signal=%d, cmd=[%s]).",
274 pid, WTERMSIG(status), hp->hp_comm);
276 pjdlog_error("Hook exited ungracefully (pid=%u, exitcode=%d, cmd=[%s]).",
277 pid, WIFEXITED(status) ? WEXITSTATUS(status) : -1,
286 struct hookproc *hp, *hp2;
289 PJDLOG_ASSERT(hooks_initialized);
291 pjdlog_debug(2, "Checking hooks.");
294 * Report about processes that are running for a long time.
297 mtx_lock(&hookprocs_lock);
298 TAILQ_FOREACH_SAFE(hp, &hookprocs, hp_next, hp2) {
299 PJDLOG_ASSERT(hp->hp_magic == HOOKPROC_MAGIC_ONLIST);
300 PJDLOG_ASSERT(hp->hp_pid > 0);
303 * If process doesn't exists we somehow missed it.
304 * Not much can be done expect for logging this situation.
306 if (kill(hp->hp_pid, 0) == -1 && errno == ESRCH) {
307 pjdlog_warning("Hook disappeared (pid=%u, cmd=[%s]).",
308 hp->hp_pid, hp->hp_comm);
315 * Skip proccesses younger than 1 minute.
317 if (now - hp->hp_lastreport < REPORT_INTERVAL)
321 * Hook is running for too long, report it.
323 pjdlog_warning("Hook is running for %ju seconds (pid=%u, cmd=[%s]).",
324 (uintmax_t)(now - hp->hp_birthtime), hp->hp_pid,
326 hp->hp_lastreport = now;
328 mtx_unlock(&hookprocs_lock);
332 hook_exec(const char *path, ...)
337 hook_execv(path, ap);
342 hook_execv(const char *path, va_list ap)
350 PJDLOG_ASSERT(hooks_initialized);
352 if (path == NULL || path[0] == '\0')
355 memset(args, 0, sizeof(args));
356 args[0] = __DECONST(char *, path);
357 for (ii = 1; ii < sizeof(args) / sizeof(args[0]); ii++) {
358 args[ii] = va_arg(ap, char *);
359 if (args[ii] == NULL)
362 PJDLOG_ASSERT(ii < sizeof(args) / sizeof(args[0]));
364 hp = hook_alloc(path, args);
368 pjdlog_debug(1, "Executing hook: %s", hp->hp_comm);
372 case -1: /* Error. */
373 pjdlog_errno(LOG_ERR, "Unable to fork to execute %s", path);
378 PJDLOG_VERIFY(sigemptyset(&mask) == 0);
379 PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
381 * Dummy handler set for SIGCHLD in the parent will be restored
382 * to SIG_IGN on execv(3) below, so there is no need to do
386 pjdlog_errno(LOG_ERR, "Unable to execute %s", path);
388 default: /* Parent. */