/* * dropnroll * * Copyright (C) 2009-2015 Christian Pointner * * This file is part of dropnroll. * * dropnroll is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * dropnroll is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with dropnroll. If not, see . */ #include "datatypes.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "sysexec.h" #include "log.h" char** dup_ptrptr(char* const ptrptr[]) { if(!ptrptr) return NULL; int n = 0; while(ptrptr[n]) n++; char** my_ptrptr; my_ptrptr = malloc((n+1)*sizeof(char*)); if(!my_ptrptr) return NULL; int i; for(i = 0; i < n; ++i) { my_ptrptr[i] = strdup(ptrptr[i]); if(!my_ptrptr[i]) { i--; for(; i >= 0; --i) free(my_ptrptr[i]); free(my_ptrptr); return NULL; } } my_ptrptr[n] = NULL; return my_ptrptr; } void free_ptrptr(char** ptrptr) { if(!ptrptr) return; int i; for(i = 0; ptrptr[i]; ++i) free(ptrptr[i]); free(ptrptr); } void child_list_init(child_list_t* list) { if(!list) return; list->first_ = NULL; } void child_list_clear(child_list_t* list) { if(!list) return; while(list->first_) { child_list_element_t* tmp; tmp = list->first_; list->first_ = tmp->next_; if(tmp->script_) free(tmp->script_); if(tmp->state_ == RUNNING) { pid_t pgid = getpgid(tmp->pid_); kill(pgid * -1, SIGKILL); } free(tmp); } } inline static void timespec2timeval(struct timeval* dest, const struct timespec* src) { dest->tv_sec = src->tv_sec; dest->tv_usec = src->tv_nsec/1000; } child_list_element_t* child_list_new(const char* script, char* const argv[], char* const evp[]) { child_list_element_t* new_child; struct timespec now; if(clock_gettime(CLOCK_MONOTONIC, &now)) { log_printf(ERROR, "new child: clock_gettime() error: %s", strerror(errno)); return NULL; } new_child = malloc(sizeof(child_list_element_t)); if(!new_child) return NULL; new_child->next_ = 0; new_child->pid_ = -1; new_child->err_fd_ = -1; new_child->state_ = NEW; timespec2timeval(&(new_child->last_state_change_), &now); new_child->script_ = strdup(script); if(!new_child->script_) { free(new_child); return NULL; } new_child->argv_ = dup_ptrptr(argv); if(!new_child->argv_) { free(new_child->script_); free(new_child); return NULL; } new_child->evp_ = dup_ptrptr(evp); if(!new_child->evp_) { free_ptrptr(new_child->argv_); free(new_child->script_); free(new_child); return NULL; } log_printf(DEBUG, "new child: added (at %lld.%06ld)", (long long)new_child->last_state_change_.tv_sec, new_child->last_state_change_.tv_usec); return new_child; } child_list_element_t* child_list_add(child_list_t* list, const char* script, char* const argv[], char* const evp[]) { if(!list) return NULL; if(!list->first_) { list->first_ = child_list_new(script, argv, evp); return list->first_; } else { child_list_element_t* tmp = list->first_; while(tmp->next_) tmp = tmp->next_; tmp->next_ = child_list_new(script, argv, evp); return tmp->next_; } } void child_list_rm(child_list_t* list, child_list_element_t* child) { if(!list || !child) return; if(child == list->first_) { list->first_ = list->first_->next_; free_ptrptr(child->argv_); free_ptrptr(child->evp_); if(child->script_) free(child->script_); free(child); return; } else { child_list_element_t* tmp = list->first_; while(tmp) { if(tmp->next_ == child) { tmp->next_ = tmp->next_->next_; free_ptrptr(child->argv_); free_ptrptr(child->evp_); if(child->script_) free(child->script_); free(child); return; } tmp = tmp->next_; } } } void child_list_rm_pid(child_list_t* list, pid_t pid) { if(!list) return; child_list_element_t* tmp = NULL; if(list->first_->pid_ == pid) { tmp = list->first_; list->first_ = list->first_->next_; free_ptrptr(tmp->argv_); free_ptrptr(tmp->evp_); if(tmp->script_) free(tmp->script_); free(tmp); return; } child_list_element_t* prev = list->first_; tmp = list->first_->next_; while(tmp) { if(tmp->pid_ == pid) { prev->next_ = tmp->next_; free_ptrptr(tmp->argv_); free_ptrptr(tmp->evp_); if(tmp->script_) free(tmp->script_); free(tmp); return; } prev = tmp; tmp = tmp->next_; } } child_list_element_t* child_list_find(child_list_t* list, pid_t pid) { if(!list) return NULL; child_list_element_t* tmp = list->first_; while(tmp) { if(tmp->pid_ == pid) return tmp; tmp = tmp->next_; } return NULL; } int child_list_num_state(child_list_t* list, child_state_t state) { int num = 0; if(!list) return 0; child_list_element_t* tmp = list->first_; for(;tmp;tmp=tmp->next_) if(tmp->state_ == state) num++; return num; } void child_list_kill_oldest(child_list_t* list, int min_runtime) { if(!list || !list->first_) return; int ignore_timeout = (min_runtime >= 0) ? 0 : 1; struct timeval now = { 0, 0 }; struct timespec now_nsec; if(clock_gettime(CLOCK_MONOTONIC, &now_nsec)) { log_printf(ERROR, "kill oldest child: clock_gettime() error: %s", strerror(errno)); ignore_timeout = 1; } else { timespec2timeval(&now, &now_nsec); } child_list_element_t* tmp = list->first_; for(;tmp;tmp=tmp->next_) { if(tmp->state_ == RUNNING) { struct timeval diff; timersub(&now, &(tmp->last_state_change_), &diff); if(ignore_timeout || diff.tv_sec >= min_runtime) { pid_t pgid = getpgid(tmp->pid_); log_printf(DEBUG, "sending KILL signal to child %d with process group id %d (at %lld.%06ld, was running for %lld.%06ld s)", tmp->pid_, pgid, (long long)tmp->last_state_change_.tv_sec, tmp->last_state_change_.tv_usec, (long long)diff.tv_sec, diff.tv_usec); kill(pgid * -1, SIGKILL); tmp->state_ = KILLED; } else { log_printf(DEBUG, "not yet killing child %d since it only ran for %lld.%06ld s", tmp->pid_, (long long)diff.tv_sec, diff.tv_usec); } break; } } } int dnr_exec(const char* script, char* const argv[], char* const evp[], child_list_t* child_lst, options_t* opt) { if(!script) return -1; child_list_element_t* child = child_list_add(child_lst, script, argv, evp); if(!child) return -2; if(child_list_num_state(child_lst, RUNNING) >= opt->max_children_) { 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, opt->min_child_runtime_); break; } return -3; } int ret = dnr_exec_child(child); if(ret) child_list_rm(child_lst, child); return ret; } int dnr_exec_child(child_list_element_t* child) { if(!child || child->pid_ != -1) return -1; int pipefd[2]; if(pipe(pipefd) == -1) { log_printf(ERROR, "executing script '%s' failed: pipe() error: %s", child->script_, strerror(errno)); return -1; } pid_t pid = fork(); if(pid == -1) { log_printf(ERROR, "executing script '%s' failed: fork() error: %s", child->script_, strerror(errno)); return -1; } if(!pid) { pid_t pgid = setsid(); if(pgid == -1) { log_printf(ERROR, "executing script '%s' failed: setsid() error: %s", child->script_, strerror(errno)); exit(-1); } int fd; for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors if(fd != pipefd[1]) close(fd); fd = open("/dev/null",O_RDWR); // stdin if(fd == -1) log_printf(WARNING, "can't open stdin"); else { if(dup(fd) == -1) // stdout log_printf(WARNING, "can't open stdout"); if(dup(fd) == -1) // stderr log_printf(WARNING, "can't open stderr"); } execve(child->script_, child->argv_, child->evp_); // if execve returns, an error occurred, but logging doesn't work // because we closed all file descriptors, so just write errno to // pipe and call exit int ret = write(pipefd[1], (void*)(&errno), sizeof(errno)); if(ret == -1) exit(-1); exit(-1); } close(pipefd[1]); child->pid_ = pid; child->err_fd_ = pipefd[0]; child->state_ = RUNNING; struct timespec now; if(clock_gettime(CLOCK_MONOTONIC, &now)) log_printf(ERROR, "exec child: clock_gettime() error: %s", strerror(errno)); else timespec2timeval(&(child->last_state_change_), &now); log_printf(INFO, "called script '%s' with pid %d (at %lld.%06ld)", child->script_, child->pid_, (long long)child->last_state_change_.tv_sec, child->last_state_change_.tv_usec); return 0; } void dnr_check_runtime(child_list_t* list, options_t* opt) { if(!list || !list->first_ || opt->min_child_runtime_ < 0) return; struct timeval now = { 0, 0 }; struct timespec now_nsec; if(clock_gettime(CLOCK_MONOTONIC, &now_nsec)) { log_printf(ERROR, "check runtime: clock_gettime() error: %s", strerror(errno)); return; } else { timespec2timeval(&now, &now_nsec); } if(!child_list_num_state(list, NEW)) return; child_list_element_t* tmp = list->first_; for(;tmp;tmp=tmp->next_) { if(tmp->state_ == RUNNING) { struct timeval diff; timersub(&now, &(tmp->last_state_change_), &diff); if(diff.tv_sec >= opt->min_child_runtime_) { pid_t pgid = getpgid(tmp->pid_); log_printf(DEBUG, "sending KILL signal to child %d with process group id %d (at %lld.%06ld, was running for %lld.%06ld s)", tmp->pid_, pgid, (long long)tmp->last_state_change_.tv_sec, tmp->last_state_change_.tv_usec, (long long)diff.tv_sec, diff.tv_usec); kill(pgid * -1, SIGKILL); tmp->state_ = KILLED; } break; } } } int dnr_waitpid(child_list_t* child_lst, options_t* opt) { int status = 0; pid_t pid = waitpid(-1, &status, WNOHANG); if(!pid || (pid < 0 && errno == ECHILD)) return 0; if(pid < 0) { log_printf(ERROR, "waitpid returned with error: %s", strerror(errno)); return pid; } child_list_element_t* child = child_list_find(child_lst, pid); if(!child) { log_printf(ERROR, "waitpid returned unknown child pid (%d)", pid); return 0; } struct timeval now = { 0, 0 }; struct timespec now_nsec; if(clock_gettime(CLOCK_MONOTONIC, &now_nsec)) log_printf(ERROR, "waitpid: clock_gettime() error: %s", strerror(errno)); else timespec2timeval(&now, &now_nsec); fd_set rfds; FD_ZERO(&rfds); FD_SET(child->err_fd_, &rfds); struct timeval tv = { 0 , 0 }; if(select(child->err_fd_+1, &rfds, NULL, NULL, &tv) == 1) { int err = 0; if(read(child->err_fd_, (void*)(&err), sizeof(err)) >= sizeof(err)) { log_printf(INFO, "script '%s' exec() error: %s (at %lld.%06ld)", child->script_, strerror(err), (long long)now.tv_sec, now.tv_usec); close(child->err_fd_); child_list_rm_pid(child_lst, pid); return -1; } } if(WIFEXITED(status)) log_printf(INFO, "script '%s' (pid %d) returned %d (at %lld.%06ld)", child->script_, child->pid_, WEXITSTATUS(status), (long long)now.tv_sec, now.tv_usec); else if(WIFSIGNALED(status)) log_printf(INFO, "script '%s' (pid %d) terminated after signal %d (at %lld.%06ld)", child->script_, child->pid_, WTERMSIG(status), (long long)now.tv_sec, now.tv_usec); else log_printf(INFO, "executing script '%s' (pid %d): unkown error (at %lld.%06ld)", child->script_, child->pid_, (long long)now.tv_sec, now.tv_usec); close(child->err_fd_); child_list_rm_pid(child_lst, pid); if(child_list_num_state(child_lst, RUNNING) < opt->max_children_) dnr_exec_child(child_list_find(child_lst, -1)); return status; }