hooks.c (211885) | hooks.c (211976) |
---|---|
1/*- 2 * Copyright (c) 2010 The FreeBSD Foundation 3 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org> 4 * All rights reserved. 5 * 6 * This software was developed by Pawel Jakub Dawidek under sponsorship from 7 * the FreeBSD Foundation. 8 * --- 15 unchanged lines hidden (view full) --- 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 28 * SUCH DAMAGE. 29 */ 30 31#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 2010 The FreeBSD Foundation 3 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org> 4 * All rights reserved. 5 * 6 * This software was developed by Pawel Jakub Dawidek under sponsorship from 7 * the FreeBSD Foundation. 8 * --- 15 unchanged lines hidden (view full) --- 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 28 * SUCH DAMAGE. 29 */ 30 31#include <sys/cdefs.h> |
32__FBSDID("$FreeBSD: head/sbin/hastd/hooks.c 211885 2010-08-27 14:38:12Z pjd $"); | 32__FBSDID("$FreeBSD: head/sbin/hastd/hooks.c 211976 2010-08-29 21:39:49Z pjd $"); |
33 34#include <sys/types.h> 35#include <sys/sysctl.h> 36#include <sys/wait.h> 37 38#include <assert.h> 39#include <errno.h> 40#include <fcntl.h> --- 36 unchanged lines hidden (view full) --- 77 time_t hp_lastreport; 78 /* Path to executable and all the arguments we passed. */ 79 char hp_comm[PATH_MAX]; 80 TAILQ_ENTRY(hookproc) hp_next; 81}; 82static TAILQ_HEAD(, hookproc) hookprocs; 83static pthread_mutex_t hookprocs_lock; 84 | 33 34#include <sys/types.h> 35#include <sys/sysctl.h> 36#include <sys/wait.h> 37 38#include <assert.h> 39#include <errno.h> 40#include <fcntl.h> --- 36 unchanged lines hidden (view full) --- 77 time_t hp_lastreport; 78 /* Path to executable and all the arguments we passed. */ 79 char hp_comm[PATH_MAX]; 80 TAILQ_ENTRY(hookproc) hp_next; 81}; 82static TAILQ_HEAD(, hookproc) hookprocs; 83static pthread_mutex_t hookprocs_lock; 84 |
85static void hook_remove(struct hookproc *hp); 86static void hook_free(struct hookproc *hp); 87 |
|
85static void 86descriptors(void) 87{ 88 long maxfd; 89 int fd; 90 91 /* 92 * Close all descriptors. --- 49 unchanged lines hidden (view full) --- 142 close(fd); 143 } 144} 145 146void 147hook_init(void) 148{ 149 | 88static void 89descriptors(void) 90{ 91 long maxfd; 92 int fd; 93 94 /* 95 * Close all descriptors. --- 49 unchanged lines hidden (view full) --- 145 close(fd); 146 } 147} 148 149void 150hook_init(void) 151{ 152 |
153 assert(!hooks_initialized); 154 |
|
150 mtx_init(&hookprocs_lock); 151 TAILQ_INIT(&hookprocs); 152 hooks_initialized = true; 153} 154 | 155 mtx_init(&hookprocs_lock); 156 TAILQ_INIT(&hookprocs); 157 hooks_initialized = true; 158} 159 |
160void 161hook_fini(void) 162{ 163 struct hookproc *hp; 164 165 assert(hooks_initialized); 166 167 mtx_lock(&hookprocs_lock); 168 while ((hp = TAILQ_FIRST(&hookprocs)) != NULL) { 169 assert(hp->hp_magic == HOOKPROC_MAGIC_ONLIST); 170 assert(hp->hp_pid > 0); 171 172 hook_remove(hp); 173 hook_free(hp); 174 } 175 mtx_unlock(&hookprocs_lock); 176 177 mtx_destroy(&hookprocs_lock); 178 TAILQ_INIT(&hookprocs); 179 hooks_initialized = false; 180} 181 |
|
155static struct hookproc * 156hook_alloc(const char *path, char **args) 157{ 158 struct hookproc *hp; 159 unsigned int ii; 160 161 hp = malloc(sizeof(*hp)); 162 if (hp == NULL) { --- 70 unchanged lines hidden (view full) --- 233 if (hp->hp_pid == pid) 234 break; 235 } 236 237 return (hp); 238} 239 240void | 182static struct hookproc * 183hook_alloc(const char *path, char **args) 184{ 185 struct hookproc *hp; 186 unsigned int ii; 187 188 hp = malloc(sizeof(*hp)); 189 if (hp == NULL) { --- 70 unchanged lines hidden (view full) --- 260 if (hp->hp_pid == pid) 261 break; 262 } 263 264 return (hp); 265} 266 267void |
268hook_check_one(pid_t pid, int status) 269{ 270 struct hookproc *hp; 271 272 mtx_lock(&hookprocs_lock); 273 hp = hook_find(pid); 274 if (hp == NULL) { 275 mtx_unlock(&hookprocs_lock); 276 pjdlog_debug(1, "Unknown process pid=%u", pid); 277 return; 278 } 279 hook_remove(hp); 280 mtx_unlock(&hookprocs_lock); 281 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 282 pjdlog_debug(1, "Hook exited gracefully (pid=%u, cmd=[%s]).", 283 pid, hp->hp_comm); 284 } else if (WIFSIGNALED(status)) { 285 pjdlog_error("Hook was killed (pid=%u, signal=%d, cmd=[%s]).", 286 pid, WTERMSIG(status), hp->hp_comm); 287 } else { 288 pjdlog_error("Hook exited ungracefully (pid=%u, exitcode=%d, cmd=[%s]).", 289 pid, WIFEXITED(status) ? WEXITSTATUS(status) : -1, 290 hp->hp_comm); 291 } 292 hook_free(hp); 293} 294 295void |
|
241hook_check(bool sigchld) 242{ 243 struct hookproc *hp, *hp2; 244 int status; 245 time_t now; 246 pid_t pid; 247 248 assert(hooks_initialized); 249 250 /* 251 * If SIGCHLD was received, garbage collect finished processes. 252 */ | 296hook_check(bool sigchld) 297{ 298 struct hookproc *hp, *hp2; 299 int status; 300 time_t now; 301 pid_t pid; 302 303 assert(hooks_initialized); 304 305 /* 306 * If SIGCHLD was received, garbage collect finished processes. 307 */ |
253 while (sigchld && (pid = wait3(&status, WNOHANG, NULL)) > 0) { 254 mtx_lock(&hookprocs_lock); 255 hp = hook_find(pid); 256 if (hp == NULL) { 257 mtx_unlock(&hookprocs_lock); 258 pjdlog_warning("Unknown process pid=%u", pid); 259 continue; 260 } 261 hook_remove(hp); 262 mtx_unlock(&hookprocs_lock); 263 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 264 pjdlog_debug(1, "Hook exited gracefully (pid=%u, cmd=[%s]).", 265 pid, hp->hp_comm); 266 } else if (WIFSIGNALED(status)) { 267 pjdlog_error("Hook was killed (pid=%u, signal=%d, cmd=[%s]).", 268 pid, WTERMSIG(status), hp->hp_comm); 269 } else { 270 pjdlog_error("Hook exited ungracefully (pid=%u, exitcode=%d, cmd=[%s]).", 271 pid, WIFEXITED(status) ? WEXITSTATUS(status) : -1, 272 hp->hp_comm); 273 } 274 hook_free(hp); | 308 if (sigchld) { 309 while ((pid = wait3(&status, WNOHANG, NULL)) > 0) 310 hook_check_one(pid, status); |
275 } 276 277 /* 278 * Report about processes that are running for a long time. 279 */ 280 now = time(NULL); 281 mtx_lock(&hookprocs_lock); 282 TAILQ_FOREACH_SAFE(hp, &hookprocs, hp_next, hp2) { --- 83 unchanged lines hidden --- | 311 } 312 313 /* 314 * Report about processes that are running for a long time. 315 */ 316 now = time(NULL); 317 mtx_lock(&hookprocs_lock); 318 TAILQ_FOREACH_SAFE(hp, &hookprocs, hp_next, hp2) { --- 83 unchanged lines hidden --- |