mirror of
https://github.com/tmate-io/tmate-ssh-server.git
synced 2020-11-18 19:53:51 -08:00
Quick refactor of tmate-slave.c
This commit is contained in:
parent
0f1159aa30
commit
3b4e6bb9cc
@ -181,6 +181,7 @@ dist_tmate_slave_SOURCES = \
|
|||||||
tmate-ssh-client-pty.c \
|
tmate-ssh-client-pty.c \
|
||||||
tmate-ssh-client.c \
|
tmate-ssh-client.c \
|
||||||
tmate-ssh-server.c \
|
tmate-ssh-server.c \
|
||||||
|
tmux-bare.c \
|
||||||
tty-acs.c \
|
tty-acs.c \
|
||||||
tty-keys.c \
|
tty-keys.c \
|
||||||
tty-term.c \
|
tty-term.c \
|
||||||
|
@ -1,58 +1,8 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "tmate.h"
|
#include "tmate.h"
|
||||||
|
|
||||||
struct event_base *ev_base;
|
|
||||||
|
|
||||||
struct options global_options; /* server options */
|
|
||||||
struct options global_s_options; /* session options */
|
|
||||||
struct options global_w_options; /* window options */
|
|
||||||
struct environ global_environ;
|
|
||||||
|
|
||||||
struct event_base *ev_base;
|
|
||||||
|
|
||||||
char *cfg_file;
|
|
||||||
char *shell_cmd;
|
|
||||||
int debug_level;
|
|
||||||
time_t start_time;
|
|
||||||
char socket_path[MAXPATHLEN];
|
|
||||||
int login_shell;
|
|
||||||
char *environ_path;
|
|
||||||
pid_t environ_pid = -1;
|
|
||||||
int environ_session_id = -1;
|
|
||||||
|
|
||||||
void
|
|
||||||
setblocking(int fd, int state)
|
|
||||||
{
|
|
||||||
int mode;
|
|
||||||
|
|
||||||
if ((mode = fcntl(fd, F_GETFL)) != -1) {
|
|
||||||
if (!state)
|
|
||||||
mode |= O_NONBLOCK;
|
|
||||||
else
|
|
||||||
mode &= ~O_NONBLOCK;
|
|
||||||
fcntl(fd, F_SETFL, mode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const char*
|
|
||||||
get_full_path(const char *wd, const char *path)
|
|
||||||
{
|
|
||||||
static char newpath[MAXPATHLEN];
|
|
||||||
char oldpath[MAXPATHLEN];
|
|
||||||
|
|
||||||
if (getcwd(oldpath, sizeof oldpath) == NULL)
|
|
||||||
return (NULL);
|
|
||||||
if (chdir(wd) != 0)
|
|
||||||
return (NULL);
|
|
||||||
if (realpath(path, newpath) != 0)
|
|
||||||
return (NULL);
|
|
||||||
chdir(oldpath);
|
|
||||||
return (newpath);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void usage(void)
|
static void usage(void)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "usage: tmate-slave [-p PORT]\n");
|
fprintf(stderr, "usage: tmate-slave [-p PORT]\n");
|
||||||
@ -92,8 +42,6 @@ struct tmate_encoder *tmate_encoder;
|
|||||||
|
|
||||||
void tmate_spawn_slave_server(struct tmate_ssh_client *client)
|
void tmate_spawn_slave_server(struct tmate_ssh_client *client)
|
||||||
{
|
{
|
||||||
int quiet = 0;
|
|
||||||
int flags = IDENTIFY_UTF8 | IDENTIFY_256COLOURS;
|
|
||||||
struct tmate_encoder encoder;
|
struct tmate_encoder encoder;
|
||||||
struct tmate_decoder decoder;
|
struct tmate_decoder decoder;
|
||||||
|
|
||||||
@ -107,25 +55,7 @@ void tmate_spawn_slave_server(struct tmate_ssh_client *client)
|
|||||||
|
|
||||||
tmate_ssh_client_init(client, &encoder, &decoder);
|
tmate_ssh_client_init(client, &encoder, &decoder);
|
||||||
|
|
||||||
environ_init(&global_environ);
|
tmux_server_init(IDENTIFY_UTF8 | IDENTIFY_256COLOURS);
|
||||||
|
|
||||||
options_init(&global_options, NULL);
|
|
||||||
options_table_populate_tree(server_options_table, &global_options);
|
|
||||||
options_set_number(&global_options, "quiet", quiet);
|
|
||||||
|
|
||||||
options_init(&global_s_options, NULL);
|
|
||||||
options_table_populate_tree(session_options_table, &global_s_options);
|
|
||||||
|
|
||||||
options_init(&global_w_options, NULL);
|
|
||||||
options_table_populate_tree(window_options_table, &global_w_options);
|
|
||||||
|
|
||||||
if (flags & IDENTIFY_UTF8) {
|
|
||||||
options_set_number(&global_s_options, "status-utf8", 1);
|
|
||||||
options_set_number(&global_s_options, "mouse-utf8", 1);
|
|
||||||
options_set_number(&global_w_options, "utf8", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
server_start(0, NULL);
|
|
||||||
/* never reached */
|
/* never reached */
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,9 +78,6 @@ void tmate_spawn_slave_client(struct tmate_ssh_client *ssh_client)
|
|||||||
if (openpty(&client->pty, &slave_pty, NULL, NULL, NULL) < 0)
|
if (openpty(&client->pty, &slave_pty, NULL, NULL, NULL) < 0)
|
||||||
tmate_fatal("Cannot allocate pty");
|
tmate_fatal("Cannot allocate pty");
|
||||||
|
|
||||||
/* setsid(); */
|
|
||||||
/* ioctl(slave_pty, TIOCSCTTY, NULL); */
|
|
||||||
|
|
||||||
dup2(slave_pty, STDIN_FILENO);
|
dup2(slave_pty, STDIN_FILENO);
|
||||||
dup2(slave_pty, STDOUT_FILENO);
|
dup2(slave_pty, STDOUT_FILENO);
|
||||||
stderr = stdout;
|
stderr = stdout;
|
||||||
|
4
tmate.h
4
tmate.h
@ -110,4 +110,8 @@ extern void tmate_spawn_slave_client(struct tmate_ssh_client *client);
|
|||||||
/* tmate-debug.c */
|
/* tmate-debug.c */
|
||||||
extern void tmate_print_trace(void);
|
extern void tmate_print_trace(void);
|
||||||
|
|
||||||
|
/* tmux-bare.c */
|
||||||
|
|
||||||
|
extern void tmux_server_init(int flags);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
78
tmux-bare.c
Normal file
78
tmux-bare.c
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "tmux.h"
|
||||||
|
#include "tmate.h"
|
||||||
|
|
||||||
|
struct event_base *ev_base;
|
||||||
|
|
||||||
|
struct options global_options; /* server options */
|
||||||
|
struct options global_s_options; /* session options */
|
||||||
|
struct options global_w_options; /* window options */
|
||||||
|
struct environ global_environ;
|
||||||
|
|
||||||
|
char *cfg_file;
|
||||||
|
char *shell_cmd;
|
||||||
|
int debug_level;
|
||||||
|
time_t start_time;
|
||||||
|
char socket_path[MAXPATHLEN];
|
||||||
|
int login_shell;
|
||||||
|
char *environ_path;
|
||||||
|
pid_t environ_pid = -1;
|
||||||
|
int environ_session_id = -1;
|
||||||
|
|
||||||
|
void
|
||||||
|
setblocking(int fd, int state)
|
||||||
|
{
|
||||||
|
int mode;
|
||||||
|
|
||||||
|
if ((mode = fcntl(fd, F_GETFL)) != -1) {
|
||||||
|
if (!state)
|
||||||
|
mode |= O_NONBLOCK;
|
||||||
|
else
|
||||||
|
mode &= ~O_NONBLOCK;
|
||||||
|
fcntl(fd, F_SETFL, mode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char*
|
||||||
|
get_full_path(const char *wd, const char *path)
|
||||||
|
{
|
||||||
|
static char newpath[MAXPATHLEN];
|
||||||
|
char oldpath[MAXPATHLEN];
|
||||||
|
|
||||||
|
if (getcwd(oldpath, sizeof oldpath) == NULL)
|
||||||
|
return (NULL);
|
||||||
|
if (chdir(wd) != 0)
|
||||||
|
return (NULL);
|
||||||
|
if (realpath(path, newpath) != 0)
|
||||||
|
return (NULL);
|
||||||
|
chdir(oldpath);
|
||||||
|
return (newpath);
|
||||||
|
}
|
||||||
|
|
||||||
|
void tmux_server_init(int flags)
|
||||||
|
{
|
||||||
|
int quiet = 0;
|
||||||
|
|
||||||
|
environ_init(&global_environ);
|
||||||
|
|
||||||
|
options_init(&global_options, NULL);
|
||||||
|
options_table_populate_tree(server_options_table, &global_options);
|
||||||
|
options_set_number(&global_options, "quiet", quiet);
|
||||||
|
|
||||||
|
options_init(&global_s_options, NULL);
|
||||||
|
options_table_populate_tree(session_options_table, &global_s_options);
|
||||||
|
|
||||||
|
options_init(&global_w_options, NULL);
|
||||||
|
options_table_populate_tree(window_options_table, &global_w_options);
|
||||||
|
|
||||||
|
if (flags & IDENTIFY_UTF8) {
|
||||||
|
options_set_number(&global_s_options, "status-utf8", 1);
|
||||||
|
options_set_number(&global_s_options, "mouse-utf8", 1);
|
||||||
|
options_set_number(&global_w_options, "utf8", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
server_start(0, NULL);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user