From: Shuanglei Tao Date: Thu, 5 Dec 2019 15:19:34 +0000 (+0800) Subject: terminal: set the pty fd non blocking X-Git-Url: http://git.prime8.dev/?a=commitdiff_plain;h=cfd338ea5e1a3c3023acade45980b3024c41e507;p=ttyd.git terminal: set the pty fd non blocking --- diff --git a/src/protocol.c b/src/protocol.c index 692a638..4445b39 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -175,6 +175,10 @@ spawn_process(struct pss_tty *pss) { uv_signal_start(&pss->watcher, child_cb, SIGCHLD); + // ensure the lws socket fd close-on-exec + fd_set_cloexec(lws_get_socket_fd(pss->wsi)); + + // create process with pseudo-tty pss->pid = pty_fork(&pss->pty, argv[0], argv, server->terminal_type); if (pss->pid < 0) { lwsl_err("pty_fork: %d (%s)\n", errno, strerror(errno)); diff --git a/src/terminal.c b/src/terminal.c index b0e53c2..3992ffb 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -14,6 +14,8 @@ #include #endif +#include "utils.h" + pid_t pty_fork(int *pty, const char *file, char *const argv[], const char *term) { pid_t pid = forkpty(pty, NULL, NULL, NULL); @@ -29,11 +31,13 @@ pty_fork(int *pty, const char *file, char *const argv[], const char *term) { } } - // set the file descriptor close-on-exec - int status_flags = fcntl(*pty, F_GETFL); - if (status_flags != -1) { - fcntl(*pty, F_SETFD, status_flags | FD_CLOEXEC); + // set the file descriptor non blocking + int flags = fcntl(*pty, F_GETFL); + if (flags != -1) { + fcntl(*pty, F_SETFD, flags | O_NONBLOCK); } + // set the file descriptor close-on-exec + fd_set_cloexec(*pty); return pid; } diff --git a/src/utils.c b/src/utils.c index 7203cdd..4348f6f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #ifdef __linux__ @@ -85,6 +86,14 @@ get_sig(const char *sig_name) { return atoi(sig_name); } +bool +fd_set_cloexec(const int fd) { + int flags = fcntl(fd, F_GETFD); + if (flags < 0) + return false; + return (flags & FD_CLOEXEC) == 0 || fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1; +} + int wait_proc(pid_t in, pid_t *out) { int stat = 0, pid; diff --git a/src/utils.h b/src/utils.h index 72cfce2..42477d3 100644 --- a/src/utils.h +++ b/src/utils.h @@ -25,6 +25,10 @@ get_sig_name(int sig, char *buf, size_t len); int get_sig(const char *sig_name); +// Set the given file descriptor close-on-exec +bool +fd_set_cloexec(const int fd); + // waitpid with WNOHANG and return the status int wait_proc(pid_t in, pid_t *out);