Make sure logcat process does not become a zombie

This commit is contained in:
topjohnwu
2019-02-14 17:36:18 -05:00
parent 4872df6a46
commit 9430dbb96c
5 changed files with 48 additions and 14 deletions

View File

@@ -82,6 +82,7 @@ unsigned get_shell_uid();
unsigned get_system_uid();
unsigned get_radio_uid();
int fork_dont_care();
int fork_no_zombie();
void gen_rand_str(char *buf, int len);
int strend(const char *s1, const char *s2);
@@ -147,6 +148,9 @@ std::vector<std::string> file_to_vector(const char *filename);
// misc.cpp
int new_daemon_thread(void *(*start_routine) (void *), void *arg = nullptr,
const pthread_attr_t *attr = nullptr);
struct exec_t {
bool err = false;
int fd = -2;

View File

@@ -10,6 +10,7 @@
#include <syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <sys/sysmacros.h>
#include <logging.h>
@@ -50,6 +51,20 @@ int fork_dont_care() {
return 0;
}
int fork_no_zombie() {
int pid = xfork();
if (pid)
return pid;
// Unblock all signals
sigset_t block_set;
sigfillset(&block_set);
pthread_sigmask(SIG_UNBLOCK, &block_set, nullptr);
prctl(PR_SET_PDEATHSIG, SIGTERM);
if (getppid() == 1)
exit(1);
return 0;
}
void gen_rand_str(char *buf, int len) {
const char base[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int urandom;
@@ -201,3 +216,11 @@ int exec_command_sync(exec_t &exec) {
return WEXITSTATUS(status);
}
int new_daemon_thread(void *(*start_routine) (void *), void *arg, const pthread_attr_t *attr) {
pthread_t thread;
int ret = xpthread_create(&thread, attr, start_routine, arg);
if (ret == 0)
pthread_detach(thread);
return ret;
}