Skip to content

Commit bff3a61

Browse files
committed
server: add cwd support
1 parent 968f1e6 commit bff3a61

File tree

7 files changed

+25
-2
lines changed

7 files changed

+25
-2
lines changed

man/ttyd.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ Cross platform: macOS, Linux, FreeBSD/OpenBSD, OpenWrt/LEDE, Windows
6262
\-s, \-\-signal <signal string>
6363
Signal to send to the command when exit it (default: 1, SIGHUP)
6464

65+
.PP
66+
\-w, \-\-cwd <path>
67+
Working directory to be set for the child program
68+
6569
.PP
6670
\-a, \-\-url\-arg
6771
Allow client to send command line arguments in URL (eg:

man/ttyd.man.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ ttyd 1 "September 2016" ttyd "User Manual"
4040
-s, --signal <signal string>
4141
Signal to send to the command when exit it (default: 1, SIGHUP)
4242

43+
-w, --cwd <path>
44+
Working directory to be set for the child program
45+
4346
-a, --url-arg
4447
Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)
4548

src/protocol.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ static char **build_env(struct pss_tty *pss) {
134134

135135
static bool spawn_process(struct pss_tty *pss, uint16_t columns, uint16_t rows) {
136136
pty_process *process = process_init((void *)pss, server->loop, build_args(pss), build_env(pss));
137+
if (server->cwd != NULL) process->cwd = strdup(server->cwd);
137138
if (columns > 0) process->columns = columns;
138139
if (rows > 0) process->rows = rows;
139140
if (pty_spawn(process, process_read_cb, process_exit_cb) != 0) {

src/pty.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ void process_free(pty_process *process) {
125125
#endif
126126
if (process->io != NULL) pty_io_free(process->io);
127127
if (process->argv != NULL) free(process->argv);
128+
if (process->cwd != NULL) free(process->cwd);
128129
char **p = process->envp;
129130
for (; *p; p++) free(*p);
130131
free(process->envp);
@@ -366,7 +367,7 @@ int pty_spawn(pty_process *process, pty_read_cb read_cb, pty_exit_cb exit_cb) {
366367
env = prep_env(process->envp);
367368
if (env == NULL) goto cleanup;
368369

369-
if (!CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, flags, env, NULL, &process->si.StartupInfo, &pi)) {
370+
if (!CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, flags, env, process->cwd, &process->si.StartupInfo, &pi)) {
370371
print_error("CreateProcessW");
371372
goto cleanup;
372373
}
@@ -461,6 +462,12 @@ int pty_spawn(pty_process *process, pty_read_cb read_cb, pty_exit_cb exit_cb) {
461462
return status;
462463
} else if (pid == 0) {
463464
setsid();
465+
if (process->cwd != NULL) {
466+
if (chdir(process->cwd) == -1) {
467+
perror("chdir failed\n");
468+
_exit(1);
469+
}
470+
}
464471
int ret = pty_execvpe(process->argv[0], process->argv, process->envp);
465472
if (ret < 0) {
466473
perror("execvp failed\n");

src/pty.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct pty_process_ {
5151
#endif
5252
char **argv;
5353
char **envp;
54+
char *cwd;
5455

5556
uv_loop_t *loop;
5657
uv_async_t async;

src/server.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ static const struct option options[] = {{"port", required_argument, NULL, 'p'},
5858
{"uid", required_argument, NULL, 'u'},
5959
{"gid", required_argument, NULL, 'g'},
6060
{"signal", required_argument, NULL, 's'},
61+
{"cwd", required_argument, NULL, 'w'},
6162
{"index", required_argument, NULL, 'I'},
6263
{"base-path", required_argument, NULL, 'b'},
6364
#if LWS_LIBRARY_VERSION_NUMBER >= 4000000
@@ -80,7 +81,7 @@ static const struct option options[] = {{"port", required_argument, NULL, 'p'},
8081
{"version", no_argument, NULL, 'v'},
8182
{"help", no_argument, NULL, 'h'},
8283
{NULL, 0, 0, 0}};
83-
static const char *opt_string = "p:i:c:H:u:g:s:I:b:P:6aSC:K:A:Rt:T:Om:oBd:vh";
84+
static const char *opt_string = "p:i:c:H:u:g:s:w:I:b:P:6aSC:K:A:Rt:T:Om:oBd:vh";
8485

8586
static void print_help() {
8687
// clang-format off
@@ -97,6 +98,7 @@ static void print_help() {
9798
" -u, --uid User id to run with\n"
9899
" -g, --gid Group id to run with\n"
99100
" -s, --signal Signal to send to the command when exit it (default: 1, SIGHUP)\n"
101+
" -w, --cwd Working directory to be set for the child program\n"
100102
" -a, --url-arg Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)\n"
101103
" -R, --readonly Do not allow clients to write to the TTY\n"
102104
" -t, --client-option Send option to client (format: key=value), repeat to add more options\n"
@@ -198,6 +200,7 @@ static void server_free(struct server *ts) {
198200
if (ts->credential != NULL) free(ts->credential);
199201
if (ts->auth_header != NULL) free(ts->auth_header);
200202
if (ts->index != NULL) free(ts->index);
203+
if (ts->cwd != NULL) free(ts->cwd);
201204
free(ts->command);
202205
free(ts->prefs_json);
203206

@@ -398,6 +401,9 @@ int main(int argc, char **argv) {
398401
return -1;
399402
}
400403
} break;
404+
case 'w':
405+
server->cwd = strdup(optarg);
406+
break;
401407
case 'I':
402408
if (!strncmp(optarg, "~/", 2)) {
403409
const char *home = getenv("HOME");

src/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ struct server {
6565
char *command; // full command line
6666
char **argv; // command with arguments
6767
int argc; // command + arguments count
68+
char *cwd; // working directory
6869
int sig_code; // close signal
6970
char sig_name[20]; // human readable signal string
7071
bool url_arg; // allow client to send cli arguments in URL

0 commit comments

Comments
 (0)