mirror of
https://github.com/tsl0922/ttyd.git
synced 2025-12-23 12:14:20 +01:00
protocol: cleanup process without hanging
This commit is contained in:
@@ -9,7 +9,6 @@
|
||||
#include <sys/queue.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#if defined(__OpenBSD__) || defined(__APPLE__)
|
||||
@@ -148,10 +147,11 @@ tty_client_destroy(struct tty_client *client) {
|
||||
if (kill(pid, server->sig_code) != 0) {
|
||||
lwsl_err("kill: %d, errno: %d (%s)\n", pid, errno, strerror(errno));
|
||||
}
|
||||
int status;
|
||||
while (waitpid(client->pid, &status, 0) == -1 && errno == EINTR)
|
||||
;
|
||||
lwsl_notice("process exited with code %d, pid: %d\n", status, client->pid);
|
||||
pid_t pid_out;
|
||||
int status = wait_proc(client->pid, &pid_out);
|
||||
if (pid_out > 0) {
|
||||
lwsl_notice("process exited with code %d, pid: %d\n", status, pid_out);
|
||||
}
|
||||
close(client->pty);
|
||||
|
||||
cleanup:
|
||||
|
||||
10
src/server.c
10
src/server.c
@@ -181,6 +181,15 @@ sig_handler(int sig) {
|
||||
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
|
||||
calc_command_start(int argc, char **argv) {
|
||||
// make a copy of argc and argv
|
||||
@@ -456,6 +465,7 @@ main(int argc, char **argv) {
|
||||
|
||||
signal(SIGINT, sig_handler); // ^C
|
||||
signal(SIGTERM, sig_handler); // kill
|
||||
signal(SIGCHLD, sigchld_handler);
|
||||
|
||||
context = lws_create_context(&info);
|
||||
if (context == NULL) {
|
||||
|
||||
18
src/utils.c
18
src/utils.c
@@ -4,6 +4,8 @@
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <sys/errno.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#ifdef __linux__
|
||||
// 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);
|
||||
}
|
||||
|
||||
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
|
||||
open_uri(char *uri) {
|
||||
#ifdef __APPLE__
|
||||
|
||||
@@ -25,6 +25,10 @@ get_sig_name(int sig, char *buf, size_t len);
|
||||
int
|
||||
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
|
||||
int
|
||||
open_uri(char *uri);
|
||||
|
||||
Reference in New Issue
Block a user