Skip to content

Commit 8a7db24

Browse files
Add argument for temp file path/prefix
1 parent cde0982 commit 8a7db24

File tree

6 files changed

+15
-13
lines changed

6 files changed

+15
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ OPTIONS:
7676
-g, --gid Group id to run with
7777
-s, --signal Signal to send to the command when exit it (default: 1, SIGHUP)
7878
-a, --url-arg Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)
79+
-f, --arg-file Temporary file template to write URL arguments to (ex. /tmp/prefix); the temporary file's full path is then passed in as a command line argument
7980
-R, --readonly Do not allow clients to write to the TTY
8081
-t, --client-option Send option to client (format: key=value), repeat to add more options
8182
-T, --terminal-type Terminal type to report, default: xterm-256color

man/ttyd.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Cross platform: macOS, Linux, FreeBSD/OpenBSD, OpenWrt/LEDE, Windows
6565

6666
.PP
6767
\-f, \-\-arg\-file
68-
Allow client to write URL arguments to a temporary file; the file name is then passed in as a command line argument
68+
Temporary file template to write URL arguments to (ex. /tmp/prefix); the temporary file's full path is then passed in as a command line argument
6969

7070
.PP
7171
\-R, \-\-readonly

man/ttyd.man.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ ttyd 1 "September 2016" ttyd "User Manual"
4141
Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)
4242

4343
-f, --arg-file
44-
Allow client to write URL arguments to a temporary file; the file name is then passed in as a command line argument
44+
Temporary file template to write URL arguments to (ex. /tmp/prefix); the temporary file's full path is then passed in as a command line argument
4545

4646
-R, --readonly
4747
Do not allow clients to write to the TTY

src/protocol.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ static bool spawn_process(struct pss_tty *pss, uint16_t columns, uint16_t rows)
107107
argv[n++] = pss->args[i];
108108
}
109109
}
110-
else if (server->arg_file) {
110+
else if (server->arg_file != NULL) {
111111
int fd = -1;
112-
char filePath[] = "/tmp/XXXXXX";
112+
char *filePath = strcat(server->arg_file, "XXXXXX");
113113

114114
if ((fd = mkstemp(filePath)) == -1) {
115115
lwsl_err("Creation of temp file failed with error: %d (%s)\n", errno, strerror(errno));
@@ -200,7 +200,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
200200
pss->wsi = wsi;
201201
pss->lws_close_status = LWS_CLOSE_STATUS_NOSTATUS;
202202

203-
if (server->url_arg || server->arg_file) {
203+
if (server->url_arg || server->arg_file != NULL) {
204204
while (lws_hdr_copy_fragment(wsi, buf, sizeof(buf), WSI_TOKEN_HTTP_URI_ARGS, n++) > 0) {
205205
if (strncmp(buf, "arg=", 4) == 0) {
206206
pss->args = xrealloc(pss->args, (pss->argc + 1) * sizeof(char *));

src/server.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static const struct option options[] = {
7373
{"ssl-key", required_argument, NULL, 'K'},
7474
{"ssl-ca", required_argument, NULL, 'A'},
7575
{"url-arg", no_argument, NULL, 'a'},
76-
{"arg-file", no_argument, NULL, 'f'},
76+
{"arg-file", required_argument, NULL, 'f'},
7777
{"readonly", no_argument, NULL, 'R'},
7878
{"terminal-type", required_argument, NULL, 'T'},
7979
{"client-option", required_argument, NULL, 't'},
@@ -87,10 +87,10 @@ static const struct option options[] = {
8787
{NULL, 0, 0, 0}};
8888

8989
#if LWS_LIBRARY_VERSION_NUMBER < 4000000
90-
static const char *opt_string = "p:i:c:u:g:s:I:b:6afSC:K:A:Rt:T:Om:oBd:vh";
90+
static const char *opt_string = "p:i:c:u:g:s:I:b:6af:SC:K:A:Rt:T:Om:oBd:vh";
9191
#endif
9292
#if LWS_LIBRARY_VERSION_NUMBER >= 4000000
93-
static const char *opt_string = "p:i:c:u:g:s:I:b:P:6afSC:K:A:Rt:T:Om:oBd:vh";
93+
static const char *opt_string = "p:i:c:u:g:s:I:b:P:6af:SC:K:A:Rt:T:Om:oBd:vh";
9494
#endif
9595

9696
static void print_help() {
@@ -108,7 +108,7 @@ static void print_help() {
108108
" -g, --gid Group id to run with\n"
109109
" -s, --signal Signal to send to the command when exit it (default: 1, SIGHUP)\n"
110110
" -a, --url-arg Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)\n"
111-
" -f, --arg-file Allow client to write URL arguments to a temporary file; the file name is then passed in as a command line argument\n"
111+
" -f, --arg-file Temporary file template to write URL arguments to (ex. /tmp/prefix); the temporary file's full path is then passed in as a command line argument\n"
112112
" -R, --readonly Do not allow clients to write to the TTY\n"
113113
" -t, --client-option Send option to client (format: key=value), repeat to add more options\n"
114114
" -T, --terminal-type Terminal type to report, default: xterm-256color\n"
@@ -184,6 +184,7 @@ static struct server *server_new(int argc, char **argv, int start) {
184184

185185
static void server_free(struct server *ts) {
186186
if (ts == NULL) return;
187+
if (ts->arg_file != NULL) free(ts->arg_file);
187188
if (ts->credential != NULL) free(ts->credential);
188189
if (ts->index != NULL) free(ts->index);
189190
free(ts->command);
@@ -323,10 +324,10 @@ int main(int argc, char **argv) {
323324
break;
324325
case 'a':
325326
server->url_arg = true;
326-
server->arg_file = false;
327+
server->arg_file = NULL;
327328
break;
328329
case 'f':
329-
server->arg_file = true;
330+
server->arg_file = strdup(optarg);
330331
server->url_arg = false;
331332
break;
332333
case 'R':
@@ -545,7 +546,7 @@ int main(int argc, char **argv) {
545546
}
546547
if (server->check_origin) lwsl_notice(" check origin: true\n");
547548
if (server->url_arg) lwsl_notice(" allow url arg to cli arg: true\n");
548-
if (server->arg_file) lwsl_notice(" allow url arg to tmp file: true\n");
549+
if (server->arg_file != NULL) lwsl_notice(" temp file template: %s\n", server->arg_file);
549550
if (server->readonly) lwsl_notice(" readonly: true\n");
550551
if (server->max_clients > 0)
551552
lwsl_notice(" max clients: %d\n", server->max_clients);

src/server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct server {
6565
int sig_code; // close signal
6666
char sig_name[20]; // human readable signal string
6767
bool url_arg; // allow client to send cli arguments in URL
68-
bool arg_file; // allow client to write to a temp file through URL arguments
68+
char *arg_file; // temp file template to write URL arguments
6969
bool readonly; // whether not allow clients to write to the TTY
7070
bool check_origin; // whether allow websocket connection from different origin
7171
int max_clients; // maximum clients to support

0 commit comments

Comments
 (0)