protocol: cleanup process without hanging

This commit is contained in:
Shuanglei Tao
2019-05-11 09:48:16 +08:00
parent fc4e06b907
commit e73a8e8587
4 changed files with 37 additions and 5 deletions

View File

@@ -9,7 +9,6 @@
#include <sys/queue.h> #include <sys/queue.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h> #include <pthread.h>
#if defined(__OpenBSD__) || defined(__APPLE__) #if defined(__OpenBSD__) || defined(__APPLE__)
@@ -148,10 +147,11 @@ tty_client_destroy(struct tty_client *client) {
if (kill(pid, server->sig_code) != 0) { if (kill(pid, server->sig_code) != 0) {
lwsl_err("kill: %d, errno: %d (%s)\n", pid, errno, strerror(errno)); lwsl_err("kill: %d, errno: %d (%s)\n", pid, errno, strerror(errno));
} }
int status; pid_t pid_out;
while (waitpid(client->pid, &status, 0) == -1 && errno == EINTR) int status = wait_proc(client->pid, &pid_out);
; if (pid_out > 0) {
lwsl_notice("process exited with code %d, pid: %d\n", status, client->pid); lwsl_notice("process exited with code %d, pid: %d\n", status, pid_out);
}
close(client->pty); close(client->pty);
cleanup: cleanup:

View File

@@ -181,6 +181,15 @@ sig_handler(int sig) {
lwsl_notice("send ^C to force exit.\n"); lwsl_notice("send ^C to force exit.\n");
} }
void
sigchld_handler() {
pid_t pid;
int status = wait_proc(-1, &pid);
if (pid > 0) {
lwsl_notice("process exited with code %d, pid: %d\n", status, pid);
}
}
int int
calc_command_start(int argc, char **argv) { calc_command_start(int argc, char **argv) {
// make a copy of argc and argv // make a copy of argc and argv
@@ -456,6 +465,7 @@ main(int argc, char **argv) {
signal(SIGINT, sig_handler); // ^C signal(SIGINT, sig_handler); // ^C
signal(SIGTERM, sig_handler); // kill signal(SIGTERM, sig_handler); // kill
signal(SIGCHLD, sigchld_handler);
context = lws_create_context(&info); context = lws_create_context(&info);
if (context == NULL) { if (context == NULL) {

View File

@@ -4,6 +4,8 @@
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>
#include <sys/errno.h>
#include <sys/wait.h>
#ifdef __linux__ #ifdef __linux__
// https://github.com/karelzak/util-linux/blob/master/misc-utils/kill.c // https://github.com/karelzak/util-linux/blob/master/misc-utils/kill.c
@@ -83,6 +85,22 @@ get_sig(const char *sig_name) {
return atoi(sig_name); return atoi(sig_name);
} }
int
wait_proc(pid_t in, pid_t *out) {
int stat = 0, pid;
do {
pid = waitpid(in, &stat, WNOHANG);
} while (pid < 0 && errno == EINTR);
if (out != NULL) *out = pid;
int status = -1;
if (WIFEXITED(stat)) {
status = WEXITSTATUS(stat);
} else if (WIFSIGNALED(status)) {
status = WTERMSIG(stat);
}
return status;
}
int int
open_uri(char *uri) { open_uri(char *uri) {
#ifdef __APPLE__ #ifdef __APPLE__

View File

@@ -25,6 +25,10 @@ get_sig_name(int sig, char *buf, size_t len);
int int
get_sig(const char *sig_name); get_sig(const char *sig_name);
// waitpid with WNOHANG and return the status
int
wait_proc(pid_t in, pid_t *out);
// Open uri with the default application of system // Open uri with the default application of system
int int
open_uri(char *uri); open_uri(char *uri);