From 116ce8ff07a6b646ca5884bbceb39e0cf91df906 Mon Sep 17 00:00:00 2001 From: baitian <919600234@qq.com> Date: Tue, 5 Mar 2024 22:11:37 +0800 Subject: [PATCH] server: add --exit-no-conn option (#1029) Co-authored-by: Shuanglei Tao --- README.md | 1 + man/ttyd.1 | 4 ++++ man/ttyd.man.md | 3 +++ src/protocol.c | 4 ++-- src/server.c | 8 +++++++- src/server.h | 3 ++- 6 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bc0c008..6afcf77 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ OPTIONS: -O, --check-origin Do not allow websocket connection from different origin -m, --max-clients Maximum clients to support (default: 0, no limit) -o, --once Accept only one client and exit on disconnection + -q, --exit-no-conn Exit on all clients disconnection -B, --browser Open terminal with the default system browser -I, --index Custom index.html path -b, --base-path Expected base path for requests coming from a reverse proxy (eg: /mounted/here, max length: 128) diff --git a/man/ttyd.1 b/man/ttyd.1 index 96dfea8..826ea5f 100644 --- a/man/ttyd.1 +++ b/man/ttyd.1 @@ -101,6 +101,10 @@ Cross platform: macOS, Linux, FreeBSD/OpenBSD, OpenWrt/LEDE, Windows -o, --once Accept only one client and exit on disconnection +.PP +-q, --exit-no-conn + Exit on all clients disconnection + .PP -B, --browser Open terminal with the default system browser diff --git a/man/ttyd.man.md b/man/ttyd.man.md index b587aad..98497cb 100644 --- a/man/ttyd.man.md +++ b/man/ttyd.man.md @@ -68,6 +68,9 @@ ttyd 1 "September 2016" ttyd "User Manual" -o, --once Accept only one client and exit on disconnection + -q, --exit-no-conn + Exit on all clients disconnection + -B, --browser Open terminal with the default system browser diff --git a/src/protocol.c b/src/protocol.c index 3a53f96..53e65d4 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -379,8 +379,8 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, } } - if (server->once && server->client_count == 0) { - lwsl_notice("exiting due to the --once option.\n"); + if ((server->once || server->exit_no_conn) && server->client_count == 0) { + lwsl_notice("exiting due to the --once/--exit-no-conn option.\n"); force_exit = true; lws_cancel_service(context); exit(0); diff --git a/src/server.c b/src/server.c index 9c8f118..39f40e9 100644 --- a/src/server.c +++ b/src/server.c @@ -77,12 +77,13 @@ static const struct option options[] = {{"port", required_argument, NULL, 'p'}, {"check-origin", no_argument, NULL, 'O'}, {"max-clients", required_argument, NULL, 'm'}, {"once", no_argument, NULL, 'o'}, + {"exit-no-conn", no_argument, NULL, 'q'}, {"browser", no_argument, NULL, 'B'}, {"debug", required_argument, NULL, 'd'}, {"version", no_argument, NULL, 'v'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, 0, 0}}; -static const char *opt_string = "p:i:U:c:H:u:g:s:w:I:b:P:6aSC:K:A:Wt:T:Om:oBd:vh"; +static const char *opt_string = "p:i:U:c:H:u:g:s:w:I:b:P:6aSC:K:A:Wt:T:Om:oqBd:vh"; static void print_help() { // clang-format off @@ -108,6 +109,7 @@ static void print_help() { " -O, --check-origin Do not allow websocket connection from different origin\n" " -m, --max-clients Maximum clients to support (default: 0, no limit)\n" " -o, --once Accept only one client and exit on disconnection\n" + " -q, --exit-no-conn Exit on all clients disconnection\n" " -B, --browser Open terminal with the default system browser\n" " -I, --index Custom index.html path\n" " -b, --base-path Expected base path for requests coming from a reverse proxy (eg: /mounted/here, max length: 128)\n" @@ -150,6 +152,7 @@ static void print_config() { if (server->url_arg) lwsl_notice(" allow url arg: true\n"); if (server->max_clients > 0) lwsl_notice(" max clients: %d\n", server->max_clients); if (server->once) lwsl_notice(" once: true\n"); + if (server->exit_no_conn) lwsl_notice(" exit_no_conn: true\n"); if (server->index != NULL) lwsl_notice(" custom index.html: %s\n", server->index); if (server->cwd != NULL) lwsl_notice(" working directory: %s\n", server->cwd); if (!server->writable) lwsl_notice("The --writable option is not set, will start in readonly mode"); @@ -367,6 +370,9 @@ int main(int argc, char **argv) { case 'o': server->once = true; break; + case 'q': + server->exit_no_conn = true; + break; case 'B': browser = true; break; diff --git a/src/server.h b/src/server.h index 4a659b0..e13d632 100644 --- a/src/server.h +++ b/src/server.h @@ -59,7 +59,7 @@ struct pss_tty { typedef struct { struct pss_tty *pss; bool ws_closed; -} pty_ctx_t ; +} pty_ctx_t; struct server { int client_count; // client count @@ -78,6 +78,7 @@ struct server { bool check_origin; // whether allow websocket connection from different origin int max_clients; // maximum clients to support bool once; // whether accept only one client and exit on disconnection + bool exit_no_conn; // whether exit on all clients disconnection char socket_path[255]; // UNIX domain socket path char terminal_type[30]; // terminal type to report -- 2.43.4