From 67d7be198878527b25bbd8aa609a03d558ea88af Mon Sep 17 00:00:00 2001 From: baitian <919600234@qq.com> Date: Fri, 25 Nov 2022 01:47:42 +0800 Subject: [PATCH 1/2] Support exit on all clients disconnection --- README.md | 1 + man/ttyd.1 | 4 ++++ man/ttyd.man.md | 3 +++ src/protocol.c | 4 ++-- src/server.c | 6 ++++++ src/server.h | 1 + 6 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bc0c008e3..cf585bbfc 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 + -e, --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 96dfea8b5..c34c4f659 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 +-e, --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 b587aaddc..f47b7d204 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 + -e, --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 3a53f96f9..53e65d4dd 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 9c8f118e8..99189cedb 100644 --- a/src/server.c +++ b/src/server.c @@ -77,6 +77,7 @@ 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, 'e'}, {"browser", no_argument, NULL, 'B'}, {"debug", required_argument, NULL, 'd'}, {"version", no_argument, NULL, 'v'}, @@ -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" + " -e, --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 'e': + server->exit_no_conn = true; + break; case 'B': browser = true; break; diff --git a/src/server.h b/src/server.h index 4a659b0ed..589edb708 100644 --- a/src/server.h +++ b/src/server.h @@ -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 From 0ede4ecf83e1f8908345c844812c7f758a89ff40 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Tue, 5 Mar 2024 22:09:00 +0800 Subject: [PATCH 2/2] update --- README.md | 2 +- man/ttyd.1 | 2 +- man/ttyd.man.md | 2 +- src/server.c | 8 ++++---- src/server.h | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index cf585bbfc..6afcf7797 100644 --- a/README.md +++ b/README.md @@ -80,7 +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 - -e, --exit-no-conn Exit on all clients 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 c34c4f659..826ea5f18 100644 --- a/man/ttyd.1 +++ b/man/ttyd.1 @@ -102,7 +102,7 @@ Cross platform: macOS, Linux, FreeBSD/OpenBSD, OpenWrt/LEDE, Windows Accept only one client and exit on disconnection .PP --e, --exit-no-conn +-q, --exit-no-conn Exit on all clients disconnection .PP diff --git a/man/ttyd.man.md b/man/ttyd.man.md index f47b7d204..98497cb90 100644 --- a/man/ttyd.man.md +++ b/man/ttyd.man.md @@ -68,7 +68,7 @@ ttyd 1 "September 2016" ttyd "User Manual" -o, --once Accept only one client and exit on disconnection - -e, --exit-no-conn + -q, --exit-no-conn Exit on all clients disconnection -B, --browser diff --git a/src/server.c b/src/server.c index 99189cedb..39f40e993 100644 --- a/src/server.c +++ b/src/server.c @@ -77,13 +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, 'e'}, + {"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 @@ -109,7 +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" - " -e, --exit-no-conn Exit on all clients 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" @@ -370,7 +370,7 @@ int main(int argc, char **argv) { case 'o': server->once = true; break; - case 'e': + case 'q': server->exit_no_conn = true; break; case 'B': diff --git a/src/server.h b/src/server.h index 589edb708..e13d63271 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