/** * @brief SIGCHILD signal handler * * @param sig signal number */ void sigchild_handler(int sig) { UNUSED(sig); struct pidlist_item_t * item; pid_t child_pid = waitpid(-1, NULL, WNOHANG); if ((item = pidlist_find(&pidlist, child_pid))) { // was it running on background? pidlist_remove(&pidlist, item); fprintf(stderr, MSG_SIGCHILD, child_pid); } }
static void childsig_handler(int signum) { if (signum != SIGCHLD) { return; } int chld_stat = 0; /* Wait for the child to exit */ pid_t ret = wait(&chld_stat); /* Log the child termination and return value */ if (ret == -1) { bb_log(LOG_DEBUG, "SIGCHILD received, but wait failed with %s\n", strerror(errno)); } else if (WIFEXITED(chld_stat)) { bb_log(LOG_DEBUG, "Process with PID %i returned code %i\n", ret, WEXITSTATUS (chld_stat)); } else if (WIFSIGNALED(chld_stat)) { bb_log(LOG_DEBUG, "Process with PID %i terminated with %i\n", ret, WTERMSIG(chld_stat)); } pidlist_remove(ret); }//childsig_handler
/** * Forks and runs the given application and waits for the process to finish * * @param argv The arguments values, the first one is the program * @param detached non-zero if the std in/output must be redirected to /dev/null, zero otherwise * @return Exit code of the program (between 0 and 255) or -1 on failure */ int bb_run_fork(char **argv, int detached) { int exitcode = -1; check_handler(); /* Fork and attempt to run given application */ pid_t pid = fork(); if (pid == 0) { /* child process after fork */ if (detached) { bb_run_exec_detached(argv); } else { bb_run_exec(argv); } } else if (pid > 0) { /* parent process after fork */ int status = 0; bb_log(LOG_DEBUG, "Process %s started, PID %i.\n", argv[0], pid); pidlist_add(pid); if (waitpid(pid, &status, 0) != -1) { if (WIFEXITED(status)) { /* program exited normally, return status */ exitcode = WEXITSTATUS(status); } else if (WIFSIGNALED(status)) { /* program was terminated by a signal */ exitcode = 128 + WTERMSIG(status); } } else { bb_log(LOG_ERR, "waitpid(%i) faild with %s\n", pid, strerror(errno)); } pidlist_remove(pid); } else { /* Fork failed */ bb_log(LOG_ERR, "Process %s could not be started. fork() failed.\n", argv[0]); } /* could not determine return value */ return exitcode; }