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>
56 /* Report processes that are running for too long not often than this value. */
57 #define REPORT_INTERVAL 60
59 /* Are we initialized? */
60 static bool hooks_initialized
= false;
63 * Keep all processes we forked on a global queue, so we can report nicely
64 * when they finish or report that they are running for a long time.
66 #define HOOKPROC_MAGIC_ALLOCATED 0x80090ca
67 #define HOOKPROC_MAGIC_ONLIST 0x80090c0
71 /* PID of a forked child. */
73 /* When process were forked? */
75 /* When we logged previous reported? */
77 /* Path to executable and all the arguments we passed. */
78 char hp_comm
[PATH_MAX
];
79 TAILQ_ENTRY(hookproc
) hp_next
;
81 static TAILQ_HEAD(, hookproc
) hookprocs
;
82 static pthread_mutex_t hookprocs_lock
;
84 static void hook_remove(struct hookproc
*hp
);
85 static void hook_free(struct hookproc
*hp
);
93 * Close all (or almost all) descriptors.
95 if (pjdlog_mode_get() == PJDLOG_MODE_STD
) {
96 closefrom(MAX(MAX(STDIN_FILENO
, STDOUT_FILENO
),
104 * Redirect stdin, stdout and stderr to /dev/null.
106 fd
= open(_PATH_DEVNULL
, O_RDONLY
);
108 pjdlog_errno(LOG_WARNING
, "Unable to open %s for reading",
110 } else if (fd
!= STDIN_FILENO
) {
111 if (dup2(fd
, STDIN_FILENO
) == -1) {
112 pjdlog_errno(LOG_WARNING
,
113 "Unable to duplicate descriptor for stdin");
117 fd
= open(_PATH_DEVNULL
, O_WRONLY
);
119 pjdlog_errno(LOG_WARNING
, "Unable to open %s for writing",
122 if (fd
!= STDOUT_FILENO
&& dup2(fd
, STDOUT_FILENO
) == -1) {
123 pjdlog_errno(LOG_WARNING
,
124 "Unable to duplicate descriptor for stdout");
126 if (fd
!= STDERR_FILENO
&& dup2(fd
, STDERR_FILENO
) == -1) {
127 pjdlog_errno(LOG_WARNING
,
128 "Unable to duplicate descriptor for stderr");
130 if (fd
!= STDOUT_FILENO
&& fd
!= STDERR_FILENO
)
139 PJDLOG_ASSERT(!hooks_initialized
);
141 mtx_init(&hookprocs_lock
);
142 TAILQ_INIT(&hookprocs
);
143 hooks_initialized
= true;
151 PJDLOG_ASSERT(hooks_initialized
);
153 mtx_lock(&hookprocs_lock
);
154 while ((hp
= TAILQ_FIRST(&hookprocs
)) != NULL
) {
155 PJDLOG_ASSERT(hp
->hp_magic
== HOOKPROC_MAGIC_ONLIST
);
156 PJDLOG_ASSERT(hp
->hp_pid
> 0);
161 mtx_unlock(&hookprocs_lock
);
163 mtx_destroy(&hookprocs_lock
);
164 TAILQ_INIT(&hookprocs
);
165 hooks_initialized
= false;
168 static struct hookproc
*
169 hook_alloc(const char *path
, char **args
)
174 hp
= malloc(sizeof(*hp
));
176 pjdlog_error("Unable to allocate %zu bytes of memory for a hook.",
182 hp
->hp_birthtime
= hp
->hp_lastreport
= time(NULL
);
183 (void)strlcpy(hp
->hp_comm
, path
, sizeof(hp
->hp_comm
));
184 /* We start at 2nd argument as we don't want to have exec name twice. */
185 for (ii
= 1; args
[ii
] != NULL
; ii
++) {
186 (void)snprlcat(hp
->hp_comm
, sizeof(hp
->hp_comm
), " %s",
189 if (strlen(hp
->hp_comm
) >= sizeof(hp
->hp_comm
) - 1) {
190 pjdlog_error("Exec path too long, correct configuration file.");
194 hp
->hp_magic
= HOOKPROC_MAGIC_ALLOCATED
;
199 hook_add(struct hookproc
*hp
, pid_t pid
)
202 PJDLOG_ASSERT(hp
->hp_magic
== HOOKPROC_MAGIC_ALLOCATED
);
203 PJDLOG_ASSERT(hp
->hp_pid
== 0);
206 mtx_lock(&hookprocs_lock
);
207 hp
->hp_magic
= HOOKPROC_MAGIC_ONLIST
;
208 TAILQ_INSERT_TAIL(&hookprocs
, hp
, hp_next
);
209 mtx_unlock(&hookprocs_lock
);
213 hook_remove(struct hookproc
*hp
)
216 PJDLOG_ASSERT(hp
->hp_magic
== HOOKPROC_MAGIC_ONLIST
);
217 PJDLOG_ASSERT(hp
->hp_pid
> 0);
218 PJDLOG_ASSERT(mtx_owned(&hookprocs_lock
));
220 TAILQ_REMOVE(&hookprocs
, hp
, hp_next
);
221 hp
->hp_magic
= HOOKPROC_MAGIC_ALLOCATED
;
225 hook_free(struct hookproc
*hp
)
228 PJDLOG_ASSERT(hp
->hp_magic
== HOOKPROC_MAGIC_ALLOCATED
);
229 PJDLOG_ASSERT(hp
->hp_pid
> 0);
235 static struct hookproc
*
240 PJDLOG_ASSERT(mtx_owned(&hookprocs_lock
));
242 TAILQ_FOREACH(hp
, &hookprocs
, hp_next
) {
243 PJDLOG_ASSERT(hp
->hp_magic
== HOOKPROC_MAGIC_ONLIST
);
244 PJDLOG_ASSERT(hp
->hp_pid
> 0);
246 if (hp
->hp_pid
== pid
)
254 hook_check_one(pid_t pid
, int status
)
258 mtx_lock(&hookprocs_lock
);
261 mtx_unlock(&hookprocs_lock
);
262 pjdlog_debug(1, "Unknown process pid=%u", pid
);
266 mtx_unlock(&hookprocs_lock
);
267 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
268 pjdlog_debug(1, "Hook exited gracefully (pid=%u, cmd=[%s]).",
270 } else if (WIFSIGNALED(status
)) {
271 pjdlog_error("Hook was killed (pid=%u, signal=%d, cmd=[%s]).",
272 pid
, WTERMSIG(status
), hp
->hp_comm
);
274 pjdlog_error("Hook exited ungracefully (pid=%u, exitcode=%d, cmd=[%s]).",
275 pid
, WIFEXITED(status
) ? WEXITSTATUS(status
) : -1,
284 struct hookproc
*hp
, *hp2
;
287 PJDLOG_ASSERT(hooks_initialized
);
289 pjdlog_debug(2, "Checking hooks.");
292 * Report about processes that are running for a long time.
295 mtx_lock(&hookprocs_lock
);
296 TAILQ_FOREACH_SAFE(hp
, &hookprocs
, hp_next
, hp2
) {
297 PJDLOG_ASSERT(hp
->hp_magic
== HOOKPROC_MAGIC_ONLIST
);
298 PJDLOG_ASSERT(hp
->hp_pid
> 0);
301 * If process doesn't exists we somehow missed it.
302 * Not much can be done expect for logging this situation.
304 if (kill(hp
->hp_pid
, 0) == -1 && errno
== ESRCH
) {
305 pjdlog_warning("Hook disappeared (pid=%u, cmd=[%s]).",
306 hp
->hp_pid
, hp
->hp_comm
);
313 * Skip proccesses younger than 1 minute.
315 if (now
- hp
->hp_lastreport
< REPORT_INTERVAL
)
319 * Hook is running for too long, report it.
321 pjdlog_warning("Hook is running for %ju seconds (pid=%u, cmd=[%s]).",
322 (uintmax_t)(now
- hp
->hp_birthtime
), hp
->hp_pid
,
324 hp
->hp_lastreport
= now
;
326 mtx_unlock(&hookprocs_lock
);
330 hook_exec(const char *path
, ...)
335 hook_execv(path
, ap
);
340 hook_execv(const char *path
, va_list ap
)
348 PJDLOG_ASSERT(hooks_initialized
);
350 if (path
== NULL
|| path
[0] == '\0')
353 memset(args
, 0, sizeof(args
));
354 args
[0] = __DECONST(char *, path
);
355 for (ii
= 1; ii
< sizeof(args
) / sizeof(args
[0]); ii
++) {
356 args
[ii
] = va_arg(ap
, char *);
357 if (args
[ii
] == NULL
)
360 PJDLOG_ASSERT(ii
< sizeof(args
) / sizeof(args
[0]));
362 hp
= hook_alloc(path
, args
);
366 pjdlog_debug(1, "Executing hook: %s", hp
->hp_comm
);
370 case -1: /* Error. */
371 pjdlog_errno(LOG_ERR
, "Unable to fork to execute %s", path
);
376 PJDLOG_VERIFY(sigemptyset(&mask
) == 0);
377 PJDLOG_VERIFY(sigprocmask(SIG_SETMASK
, &mask
, NULL
) == 0);
379 * Dummy handler set for SIGCHLD in the parent will be restored
380 * to SIG_IGN on execv(3) below, so there is no need to do
384 pjdlog_errno(LOG_ERR
, "Unable to execute %s", path
);
386 default: /* Parent. */