Skip to content

Commit

Permalink
use new threading to run waipit
Browse files Browse the repository at this point in the history
  • Loading branch information
Zheaoli committed Feb 27, 2021
1 parent b9f42a0 commit f5286c2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 2.8.0)
# cmake_minimum_required (VERSION 2.8.0)
project (tini C)

# Config
Expand Down Expand Up @@ -53,7 +53,7 @@ if(NOT HAS_BUILTIN_FORTIFY)
add_definitions(-D_FORTIFY_SOURCE=2)
endif()

set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Werror -Wextra -Wall -pedantic-errors -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wextra -Wall -pedantic-errors -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -pthread")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-s")

# Build
Expand Down
29 changes: 19 additions & 10 deletions src/tini.c
Expand Up @@ -14,6 +14,8 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <pthread.h>

#include "tiniConfig.h"
#include "tiniLicense.h"
Expand Down Expand Up @@ -128,6 +130,8 @@ To fix the problem, "
#endif
"run Tini as PID 1.";

static pid_t child_pid=0;

int restore_signals(const signal_configuration_t* const sigconf_ptr) {
if (sigprocmask(SIG_SETMASK, sigconf_ptr->sigmask_ptr, NULL)) {
PRINT_FATAL("Restoring child signal mask failed: '%s'", strerror(errno));
Expand Down Expand Up @@ -538,12 +542,13 @@ int wait_and_forward_signal(sigset_t const* const parent_sigset_ptr, pid_t const
return 0;
}

int reap_zombies(const pid_t child_pid, int* const child_exitcode_ptr) {
void* reap_zombies(void *arg) {
pid_t current_pid;
int current_status;
int* child_exitcode_ptr = malloc(sizeof(int));

while (1) {
current_pid = waitpid(-1, &current_status, WNOHANG);
current_pid = waitpid(-1, &current_status, 0);

switch (current_pid) {

Expand All @@ -553,7 +558,7 @@ int reap_zombies(const pid_t child_pid, int* const child_exitcode_ptr) {
break;
}
PRINT_FATAL("Error while waiting for pids: '%s'", strerror(errno));
return 1;
break;

case 0:
PRINT_TRACE("No child to reap");
Expand All @@ -577,7 +582,7 @@ int reap_zombies(const pid_t child_pid, int* const child_exitcode_ptr) {
*child_exitcode_ptr = 128 + WTERMSIG(current_status);
} else {
PRINT_FATAL("Main child exited for unknown reason");
return 1;
exit(1);
}

// Be safe, ensure the status code is indeed between 0 and 255.
Expand All @@ -599,13 +604,12 @@ int reap_zombies(const pid_t child_pid, int* const child_exitcode_ptr) {
/* If we make it here, that's because we did not continue in the switch case. */
break;
}

return 0;
exit(*child_exitcode_ptr);
}


int main(int argc, char *argv[]) {
pid_t child_pid;
// pid_t child_pid;

// Those are passed to functions to get an exitcode back.
int child_exitcode = -1; // This isn't a valid exitcode, and lets us tell whether the child has exited.
Expand Down Expand Up @@ -660,6 +664,11 @@ int main(int argc, char *argv[]) {
if (spawn_ret) {
return spawn_ret;
}
pthread_t ptid;
int err = pthread_create(&ptid, NULL, &reap_zombies, NULL);
if (err !=0 ){
return 1;
}
free(child_args_ptr);

while (1) {
Expand All @@ -669,9 +678,9 @@ int main(int argc, char *argv[]) {
}

/* Now, reap zombies */
if (reap_zombies(child_pid, &child_exitcode)) {
return 1;
}
// if (reap_zombies(child_pid, &child_exitcode)) {
// return 1;
// }

if (child_exitcode != -1) {
PRINT_TRACE("Exiting: child has exited");
Expand Down

0 comments on commit f5286c2

Please sign in to comment.