mirror of
https://github.com/tsl0922/ttyd.git
synced 2025-12-23 12:14:20 +01:00
protocol: split forkpty to separate file
This commit is contained in:
@@ -30,7 +30,7 @@ else()
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
set(LIBWEBSOCKETS_MIN_VERSION 1.7.0)
|
set(LIBWEBSOCKETS_MIN_VERSION 1.7.0)
|
||||||
set(SOURCE_FILES src/server.c src/http.c src/protocol.c src/utils.c)
|
set(SOURCE_FILES src/server.c src/http.c src/protocol.c src/terminal.c src/utils.c)
|
||||||
|
|
||||||
find_package(OpenSSL REQUIRED)
|
find_package(OpenSSL REQUIRED)
|
||||||
find_package(Libwebsockets ${LIBWEBSOCKETS_MIN_VERSION} QUIET)
|
find_package(Libwebsockets ${LIBWEBSOCKETS_MIN_VERSION} QUIET)
|
||||||
|
|||||||
@@ -1,24 +1,15 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/ioctl.h>
|
|
||||||
|
|
||||||
#if defined(__OpenBSD__) || defined(__APPLE__)
|
|
||||||
#include <util.h>
|
|
||||||
#elif defined(__FreeBSD__)
|
|
||||||
#include <libutil.h>
|
|
||||||
#else
|
|
||||||
#include <pty.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <libwebsockets.h>
|
#include <libwebsockets.h>
|
||||||
#include <json.h>
|
#include <json.h>
|
||||||
|
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
#include "terminal.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
// initial message list
|
// initial message list
|
||||||
@@ -51,8 +42,7 @@ send_initial_message(struct lws *wsi, int index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
parse_window_size(const char *json, struct winsize *size) {
|
parse_window_size(const char *json, int *cols, int *rows) {
|
||||||
int columns, rows;
|
|
||||||
json_object *obj = json_tokener_parse(json);
|
json_object *obj = json_tokener_parse(json);
|
||||||
struct json_object *o = NULL;
|
struct json_object *o = NULL;
|
||||||
|
|
||||||
@@ -60,18 +50,14 @@ parse_window_size(const char *json, struct winsize *size) {
|
|||||||
lwsl_err("columns field not exists, json: %s\n", json);
|
lwsl_err("columns field not exists, json: %s\n", json);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
columns = json_object_get_int(o);
|
*cols = json_object_get_int(o);
|
||||||
if (!json_object_object_get_ex(obj, "rows", &o)) {
|
if (!json_object_object_get_ex(obj, "rows", &o)) {
|
||||||
lwsl_err("rows field not exists, json: %s\n", json);
|
lwsl_err("rows field not exists, json: %s\n", json);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
rows = json_object_get_int(o);
|
*rows = json_object_get_int(o);
|
||||||
json_object_put(obj);
|
json_object_put(obj);
|
||||||
|
|
||||||
memset(size, 0, sizeof(struct winsize));
|
|
||||||
size->ws_col = (unsigned short) columns;
|
|
||||||
size->ws_row = (unsigned short) rows;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,37 +175,16 @@ spawn_process(struct pss_tty *pss) {
|
|||||||
|
|
||||||
uv_signal_start(&pss->watcher, child_cb, SIGCHLD);
|
uv_signal_start(&pss->watcher, child_cb, SIGCHLD);
|
||||||
|
|
||||||
int pty;
|
pss->pid = pty_fork(&pss->pty, argv[0], argv, server->terminal_type);
|
||||||
pid_t pid = forkpty(&pty, NULL, NULL, NULL);
|
if (pss->pid < 0) {
|
||||||
if (pid < 0) { /* error */
|
lwsl_err("pty_fork: %d (%s)\n", errno, strerror(errno));
|
||||||
lwsl_err("forkpty failed: %d (%s)\n", errno, strerror(errno));
|
|
||||||
return 1;
|
return 1;
|
||||||
} else if (pid == 0) { /* child */
|
|
||||||
setenv("TERM", server->terminal_type, true);
|
|
||||||
#if LWS_LIBRARY_VERSION_NUMBER < 3001000
|
|
||||||
// libwebsockets set FD_CLOEXEC since v3.1.0
|
|
||||||
close(lws_get_socket_fd(pss->wsi));
|
|
||||||
#endif
|
|
||||||
if (execvp(argv[0], argv) < 0) {
|
|
||||||
perror("execvp failed\n");
|
|
||||||
_exit(-errno);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the file descriptor close-on-exec
|
lwsl_notice("started process, pid: %d\n", pss->pid);
|
||||||
int status_flags = fcntl(pty, F_GETFL);
|
|
||||||
if (status_flags != -1) {
|
|
||||||
fcntl(pty, F_SETFD, status_flags | FD_CLOEXEC);
|
|
||||||
}
|
|
||||||
|
|
||||||
lwsl_notice("started process, pid: %d\n", pid);
|
|
||||||
pss->pid = pid;
|
|
||||||
pss->pty = pty;
|
|
||||||
if (pss->size.ws_row > 0 && pss->size.ws_col > 0)
|
|
||||||
ioctl(pss->pty, TIOCSWINSZ, &pss->size);
|
|
||||||
|
|
||||||
pss->pipe.data = pss;
|
pss->pipe.data = pss;
|
||||||
uv_pipe_open(&pss->pipe, pty);
|
uv_pipe_open(&pss->pipe, pss->pty);
|
||||||
|
|
||||||
lws_callback_on_writable(pss->wsi);
|
lws_callback_on_writable(pss->wsi);
|
||||||
|
|
||||||
@@ -380,9 +345,12 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case RESIZE_TERMINAL:
|
case RESIZE_TERMINAL:
|
||||||
if (parse_window_size(pss->buffer + 1, &pss->size) && pss->pty > 0) {
|
{
|
||||||
if (ioctl(pss->pty, TIOCSWINSZ, &pss->size) == -1) {
|
int cols, rows;
|
||||||
lwsl_err("ioctl TIOCSWINSZ: %d (%s)\n", errno, strerror(errno));
|
if (parse_window_size(pss->buffer + 1, &cols, &rows)) {
|
||||||
|
if (pty_resize(pss->pty, cols, rows) < 0) {
|
||||||
|
lwsl_err("pty_resize: %d (%s)\n", errno, strerror(errno));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <sys/ioctl.h>
|
|
||||||
#include <uv.h>
|
#include <uv.h>
|
||||||
|
|
||||||
// client message
|
// client message
|
||||||
@@ -35,11 +34,10 @@ struct pss_tty {
|
|||||||
int argc;
|
int argc;
|
||||||
|
|
||||||
struct lws *wsi;
|
struct lws *wsi;
|
||||||
struct winsize size;
|
|
||||||
char *buffer;
|
char *buffer;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
int pid;
|
pid_t pid;
|
||||||
int pty;
|
int pty;
|
||||||
char *pty_buffer;
|
char *pty_buffer;
|
||||||
ssize_t pty_len;
|
ssize_t pty_len;
|
||||||
|
|||||||
51
src/terminal.c
Normal file
51
src/terminal.c
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
|
#if defined(__OpenBSD__) || defined(__APPLE__)
|
||||||
|
#include <util.h>
|
||||||
|
#elif defined(__FreeBSD__)
|
||||||
|
#include <libutil.h>
|
||||||
|
#else
|
||||||
|
#include <pty.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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 close-on-exec
|
||||||
|
int status_flags = fcntl(*pty, F_GETFL);
|
||||||
|
if (status_flags != -1) {
|
||||||
|
fcntl(*pty, F_SETFD, status_flags | FD_CLOEXEC);
|
||||||
|
}
|
||||||
|
|
||||||
|
return pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
pty_resize(pid_t 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);
|
||||||
|
}
|
||||||
10
src/terminal.h
Normal file
10
src/terminal.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#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
|
||||||
Reference in New Issue
Block a user