mirror of
https://github.com/tsl0922/ttyd.git
synced 2025-12-23 04:14:18 +01:00
terminal: set the pty fd non blocking
This commit is contained in:
@@ -175,6 +175,10 @@ spawn_process(struct pss_tty *pss) {
|
|||||||
|
|
||||||
uv_signal_start(&pss->watcher, child_cb, SIGCHLD);
|
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);
|
pss->pid = pty_fork(&pss->pty, argv[0], argv, server->terminal_type);
|
||||||
if (pss->pid < 0) {
|
if (pss->pid < 0) {
|
||||||
lwsl_err("pty_fork: %d (%s)\n", errno, strerror(errno));
|
lwsl_err("pty_fork: %d (%s)\n", errno, strerror(errno));
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
#include <pty.h>
|
#include <pty.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
pid_t
|
pid_t
|
||||||
pty_fork(int *pty, const char *file, char *const argv[], const char *term) {
|
pty_fork(int *pty, const char *file, char *const argv[], const char *term) {
|
||||||
pid_t pid = forkpty(pty, NULL, NULL, NULL);
|
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
|
// set the file descriptor non blocking
|
||||||
int status_flags = fcntl(*pty, F_GETFL);
|
int flags = fcntl(*pty, F_GETFL);
|
||||||
if (status_flags != -1) {
|
if (flags != -1) {
|
||||||
fcntl(*pty, F_SETFD, status_flags | FD_CLOEXEC);
|
fcntl(*pty, F_SETFD, flags | O_NONBLOCK);
|
||||||
}
|
}
|
||||||
|
// set the file descriptor close-on-exec
|
||||||
|
fd_set_cloexec(*pty);
|
||||||
|
|
||||||
return pid;
|
return pid;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
@@ -85,6 +86,14 @@ get_sig(const char *sig_name) {
|
|||||||
return atoi(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
|
int
|
||||||
wait_proc(pid_t in, pid_t *out) {
|
wait_proc(pid_t in, pid_t *out) {
|
||||||
int stat = 0, pid;
|
int stat = 0, pid;
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|
||||||
|
// Set the given file descriptor close-on-exec
|
||||||
|
bool
|
||||||
|
fd_set_cloexec(const int fd);
|
||||||
|
|
||||||
// waitpid with WNOHANG and return the status
|
// waitpid with WNOHANG and return the status
|
||||||
int
|
int
|
||||||
wait_proc(pid_t in, pid_t *out);
|
wait_proc(pid_t in, pid_t *out);
|
||||||
|
|||||||
Reference in New Issue
Block a user