From b553ed2c9ca63a2ef834026cfe5469aa874dca89 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 17 Sep 2015 03:43:08 +0200 Subject: implemented children policies --- src/dropnroll.c | 24 ++++++++++++------------ src/sig_handler.c | 2 -- src/sysexec.c | 17 +++++++++++++++-- src/sysexec.h | 1 + 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/dropnroll.c b/src/dropnroll.c index 210a416..b70bbc5 100644 --- a/src/dropnroll.c +++ b/src/dropnroll.c @@ -113,18 +113,18 @@ int process_watch(int inotify_fd, read_buffer_t* buffer, watch_list_t* watch_lst char* const argv[] = { opt->script_, path, event->len > 0 ? event->name : "", buf, NULL }; char* const evp[] = { NULL }; - rh_exec(opt->script_, argv, evp, child_lst, opt); - - snprintf(buf, 100, "new file in '%s', name='%s'", path, event->len > 0 ? event->name : ""); - log_printf(NOTICE, "%s, executing script %s", buf, opt->script_); - client_t* client; - int listener_cnt = 0; - for(client = client_lst; client; client = client->next) - if(client->status_listener) { - send_response(client->fd, buf); - listener_cnt++; - } - log_printf(DEBUG, "sent status to %d additional listeners", listener_cnt); + if(!rh_exec(opt->script_, argv, evp, child_lst, opt)) { + snprintf(buf, 100, "new file in '%s', name='%s'", path, event->len > 0 ? event->name : ""); + log_printf(NOTICE, "%s, executing script %s", buf, opt->script_); + client_t* client; + int listener_cnt = 0; + for(client = client_lst; client; client = client->next) + if(client->status_listener) { + send_response(client->fd, buf); + listener_cnt++; + } + log_printf(DEBUG, "sent status to %d additional listeners", listener_cnt); + } } if(buffer->offset > len) { diff --git a/src/sig_handler.c b/src/sig_handler.c index 9e9e3e7..7287320 100644 --- a/src/sig_handler.c +++ b/src/sig_handler.c @@ -83,7 +83,6 @@ int signal_init() (sigaction(SIGHUP, &act, NULL) < 0) || (sigaction(SIGUSR1, &act, NULL) < 0) || (sigaction(SIGUSR2, &act, NULL) < 0) || - (sigaction(SIGCHLD, &ign, NULL) < 0) || (sigaction(SIGPIPE, &ign, NULL) < 0)) { log_printf(ERROR, "signal handling init failed (sigaction error: %s)", strerror(errno)); @@ -145,7 +144,6 @@ void signal_stop() sigaction(SIGHUP, &act, NULL); sigaction(SIGUSR1, &act, NULL); sigaction(SIGUSR2, &act, NULL); - sigaction(SIGCHLD, &act, NULL); sigaction(SIGPIPE, &act, NULL); close(sig_pipe_fds[0]); diff --git a/src/sysexec.c b/src/sysexec.c index abfd6f8..ae9a40c 100644 --- a/src/sysexec.c +++ b/src/sysexec.c @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -252,6 +253,14 @@ int child_list_num_running(child_list_t* list) return num; } +void child_list_kill_oldest(child_list_t* list) +{ + if(!list || !list->first_) + return; + + kill(list->first_->pid_, SIGKILL); +} + int rh_exec(const char* script, char* const argv[], char* const evp[], child_list_t* child_lst, options_t* opt) { if(!script) @@ -262,8 +271,12 @@ int rh_exec(const char* script, char* const argv[], char* const evp[], child_lis return -2; if(child_list_num_running(child_lst) >= opt->max_children_) { - log_printf(INFO, "deferring script execution '%s'", script); - return 0; + switch(opt->children_policy_) { + case DEFER: log_printf(INFO, "children limit reached: deferring script execution '%s'", script); break; + case DROP: log_printf(INFO, "children limit reached: not calling '%s'", script); child_list_rm(child_lst, child); break; + case KILL_OLDEST: log_printf(INFO, "children limit reached: killing oldest child"); child_list_kill_oldest(child_lst); break; + } + return -3; } int ret = rh_exec_child(child); diff --git a/src/sysexec.h b/src/sysexec.h index 67dbda2..560a70f 100644 --- a/src/sysexec.h +++ b/src/sysexec.h @@ -49,6 +49,7 @@ void child_list_rm(child_list_t* list, child_list_element_t* child); void child_list_rm_pid(child_list_t* list, pid_t pid); child_list_element_t* child_list_find(child_list_t* list, pid_t pid); int child_list_num_running(child_list_t* list); +void child_list_kill_oldest(child_list_t* list); int rh_exec(const char* script, char* const argv[], char* const evp[], child_list_t* child_lst, options_t* opt); int rh_exec_child(child_list_element_t* child); -- cgit v1.2.3