Skip to content

Commit

Permalink
server: add cwd support
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Aug 16, 2021
1 parent 968f1e6 commit bff3a61
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 2 deletions.
4 changes: 4 additions & 0 deletions man/ttyd.1
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ Cross platform: macOS, Linux, FreeBSD/OpenBSD, OpenWrt/LEDE, Windows
\-s, \-\-signal <signal string>
Signal to send to the command when exit it (default: 1, SIGHUP)

.PP
\-w, \-\-cwd <path>
Working directory to be set for the child program

.PP
\-a, \-\-url\-arg
Allow client to send command line arguments in URL (eg:
Expand Down
3 changes: 3 additions & 0 deletions man/ttyd.man.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ ttyd 1 "September 2016" ttyd "User Manual"
-s, --signal <signal string>
Signal to send to the command when exit it (default: 1, SIGHUP)

-w, --cwd <path>
Working directory to be set for the child program

-a, --url-arg
Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)

Expand Down
1 change: 1 addition & 0 deletions src/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ static char **build_env(struct pss_tty *pss) {

static bool spawn_process(struct pss_tty *pss, uint16_t columns, uint16_t rows) {
pty_process *process = process_init((void *)pss, server->loop, build_args(pss), build_env(pss));
if (server->cwd != NULL) process->cwd = strdup(server->cwd);
if (columns > 0) process->columns = columns;
if (rows > 0) process->rows = rows;
if (pty_spawn(process, process_read_cb, process_exit_cb) != 0) {
Expand Down
9 changes: 8 additions & 1 deletion src/pty.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ void process_free(pty_process *process) {
#endif
if (process->io != NULL) pty_io_free(process->io);
if (process->argv != NULL) free(process->argv);
if (process->cwd != NULL) free(process->cwd);
char **p = process->envp;
for (; *p; p++) free(*p);
free(process->envp);
Expand Down Expand Up @@ -366,7 +367,7 @@ int pty_spawn(pty_process *process, pty_read_cb read_cb, pty_exit_cb exit_cb) {
env = prep_env(process->envp);
if (env == NULL) goto cleanup;

if (!CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, flags, env, NULL, &process->si.StartupInfo, &pi)) {
if (!CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, flags, env, process->cwd, &process->si.StartupInfo, &pi)) {
print_error("CreateProcessW");
goto cleanup;
}
Expand Down Expand Up @@ -461,6 +462,12 @@ int pty_spawn(pty_process *process, pty_read_cb read_cb, pty_exit_cb exit_cb) {
return status;
} else if (pid == 0) {
setsid();
if (process->cwd != NULL) {
if (chdir(process->cwd) == -1) {
perror("chdir failed\n");
_exit(1);
}
}
int ret = pty_execvpe(process->argv[0], process->argv, process->envp);
if (ret < 0) {
perror("execvp failed\n");
Expand Down
1 change: 1 addition & 0 deletions src/pty.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ struct pty_process_ {
#endif
char **argv;
char **envp;
char *cwd;

uv_loop_t *loop;
uv_async_t async;
Expand Down
8 changes: 7 additions & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ static const struct option options[] = {{"port", required_argument, NULL, 'p'},
{"uid", required_argument, NULL, 'u'},
{"gid", required_argument, NULL, 'g'},
{"signal", required_argument, NULL, 's'},
{"cwd", required_argument, NULL, 'w'},
{"index", required_argument, NULL, 'I'},
{"base-path", required_argument, NULL, 'b'},
#if LWS_LIBRARY_VERSION_NUMBER >= 4000000
Expand All @@ -80,7 +81,7 @@ static const struct option options[] = {{"port", required_argument, NULL, 'p'},
{"version", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, 0, 0}};
static const char *opt_string = "p:i:c:H:u:g:s:I:b:P:6aSC:K:A:Rt:T:Om:oBd:vh";
static const char *opt_string = "p:i:c:H:u:g:s:w:I:b:P:6aSC:K:A:Rt:T:Om:oBd:vh";

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

Expand Down Expand Up @@ -398,6 +401,9 @@ int main(int argc, char **argv) {
return -1;
}
} break;
case 'w':
server->cwd = strdup(optarg);
break;
case 'I':
if (!strncmp(optarg, "~/", 2)) {
const char *home = getenv("HOME");
Expand Down
1 change: 1 addition & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct server {
char *command; // full command line
char **argv; // command with arguments
int argc; // command + arguments count
char *cwd; // working directory
int sig_code; // close signal
char sig_name[20]; // human readable signal string
bool url_arg; // allow client to send cli arguments in URL
Expand Down

0 comments on commit bff3a61

Please sign in to comment.