Skip to content

Commit

Permalink
protocol: cleanup process without hanging
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed May 11, 2019
1 parent fc4e06b commit e73a8e8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__
Expand Down
4 changes: 4 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e73a8e8

Please sign in to comment.