From bfb2d0cc8644e1768884505cdccea7dea6eedadb Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Wed, 3 Feb 2021 21:32:51 +0800 Subject: [PATCH] src: move pty code to protocol --- CMakeLists.txt | 2 +- src/protocol.c | 48 ++++++++++++++++++++++++++++++++++++++++++++- src/terminal.c | 53 -------------------------------------------------- src/terminal.h | 8 -------- 4 files changed, 48 insertions(+), 63 deletions(-) delete mode 100644 src/terminal.c delete mode 100644 src/terminal.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fe9f468..85fc63c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ else() set(CMAKE_C_STANDARD 99) endif() -set(SOURCE_FILES src/server.c src/http.c src/protocol.c src/terminal.c src/utils.c) +set(SOURCE_FILES src/server.c src/http.c src/protocol.c src/utils.c) include(FindPackageHandleStandardArgs) diff --git a/src/protocol.c b/src/protocol.c index a02d3ba..2675d49 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -6,10 +7,19 @@ #include #include #include +#include #include +#include + +#if defined(__OpenBSD__) || defined(__APPLE__) +#include +#elif defined(__FreeBSD__) +#include +#else +#include +#endif #include "server.h" -#include "terminal.h" #include "utils.h" // initial message list @@ -87,6 +97,42 @@ static bool check_host_origin(struct lws *wsi) { return len > 0 && strcasecmp(buf, host_buf) == 0; } +pid_t pty_fork(int *pty, const char *file, char *const argv[], const char *term) { + pid_t pid = forkpty(pty, NULL, NULL, NULL); + + if (pid < 0) { + return pid; + } else if (pid == 0) { + setenv("TERM", term, true); + int ret = execvp(file, argv); + if (ret < 0) { + perror("execvp failed\n"); + _exit(-errno); + } + } + + // 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; +} + +int pty_resize(int pty, int cols, int rows) { + struct winsize size; + + size.ws_col = (unsigned short)cols; + size.ws_row = (unsigned short)rows; + size.ws_xpixel = 0; + size.ws_ypixel = 0; + + return ioctl(pty, TIOCSWINSZ, &size); +} + static void close_cb(uv_handle_t *handle) { struct pty_proc *proc = container_of((uv_pipe_t *)handle, struct pty_proc, pipe); free(proc); diff --git a/src/terminal.c b/src/terminal.c deleted file mode 100644 index 2954669..0000000 --- a/src/terminal.c +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#if defined(__OpenBSD__) || defined(__APPLE__) -#include -#elif defined(__FreeBSD__) -#include -#else -#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); - - if (pid < 0) { - return pid; - } else if (pid == 0) { - setenv("TERM", term, true); - int ret = execvp(file, argv); - if (ret < 0) { - perror("execvp failed\n"); - _exit(-errno); - } - } - - // 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; -} - -int pty_resize(int pty, int cols, int rows) { - struct winsize size; - - size.ws_col = (unsigned short)cols; - size.ws_row = (unsigned short)rows; - size.ws_xpixel = 0; - size.ws_ypixel = 0; - - return ioctl(pty, TIOCSWINSZ, &size); -} diff --git a/src/terminal.h b/src/terminal.h deleted file mode 100644 index 63253e9..0000000 --- a/src/terminal.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef TTYD_TERMINAL_H -#define TTYD_TERMINAL_H - -int pty_fork(int *pty, const char *file, char *const argv[], const char *term); - -int pty_resize(int pty, int cols, int rows); - -#endif // TTYD_TERMINAL_H -- 2.43.4