forked from github/abduco
Compare commits
No commits in common. "d9c18f680a2f58ec3b54aec6843d53ffdce7f5c6" and "6c6b505327a95a46c11b8889d48f364892e4b0b8" have entirely different histories.
d9c18f680a
...
6c6b505327
56
client.c
56
client.c
|
|
@ -123,73 +123,25 @@ static int client_mainloop(void) {
|
||||||
debug("client-stdin: %c\n", pkt.u.msg[0]);
|
debug("client-stdin: %c\n", pkt.u.msg[0]);
|
||||||
if (client.await_command) {
|
if (client.await_command) {
|
||||||
client.await_command = false;
|
client.await_command = false;
|
||||||
bool detach = false;
|
if (pkt.u.msg[0]=='d') {
|
||||||
switch (pkt.u.msg[0]) {
|
|
||||||
case 'd':
|
|
||||||
server.next_session = "\0";
|
|
||||||
detach = true;
|
|
||||||
break;
|
|
||||||
case '0':
|
|
||||||
server.next_session = "0";
|
|
||||||
detach = true;
|
|
||||||
break;
|
|
||||||
case '1':
|
|
||||||
server.next_session = "1";
|
|
||||||
detach = true;
|
|
||||||
break;
|
|
||||||
case '2':
|
|
||||||
server.next_session = "2";
|
|
||||||
detach = true;
|
|
||||||
break;
|
|
||||||
case '3':
|
|
||||||
server.next_session = "3";
|
|
||||||
detach = true;
|
|
||||||
break;
|
|
||||||
case '4':
|
|
||||||
server.next_session = "4";
|
|
||||||
detach = true;
|
|
||||||
break;
|
|
||||||
case '5':
|
|
||||||
server.next_session = "5";
|
|
||||||
detach = true;
|
|
||||||
break;
|
|
||||||
case '6':
|
|
||||||
server.next_session = "6";
|
|
||||||
detach = true;
|
|
||||||
break;
|
|
||||||
case '7':
|
|
||||||
server.next_session = "7";
|
|
||||||
detach = true;
|
|
||||||
break;
|
|
||||||
case '8':
|
|
||||||
server.next_session = "8";
|
|
||||||
detach = true;
|
|
||||||
break;
|
|
||||||
case '9':
|
|
||||||
server.next_session = "9";
|
|
||||||
detach = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (detach) {
|
|
||||||
pkt.type = MSG_DETACH;
|
pkt.type = MSG_DETACH;
|
||||||
pkt.len = 0;
|
pkt.len = 0;
|
||||||
client_send_packet(&pkt);
|
client_send_packet(&pkt);
|
||||||
close(server.socket);
|
close(server.socket);
|
||||||
return RV_DETACH;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pkt.len = len;
|
pkt.len = len;
|
||||||
if (KEY_REDRAW && pkt.u.msg[0] == KEY_REDRAW) {
|
if (KEY_REDRAW && pkt.u.msg[0] == KEY_REDRAW) {
|
||||||
client.need_resize = true;
|
client.need_resize = true;
|
||||||
} else if (pkt.u.msg[0] == KEY_COMMAND) {
|
} else if (pkt.u.msg[0] == KEY_DETACH) {
|
||||||
client.await_command = true;
|
client.await_command = true;
|
||||||
} else if (!(client.flags & CLIENT_READONLY)) {
|
} else if (!(client.flags & CLIENT_READONLY)) {
|
||||||
client_send_packet(&pkt);
|
client_send_packet(&pkt);
|
||||||
}
|
}
|
||||||
} else if (len == 0) {
|
} else if (len == 0) {
|
||||||
debug("client-stdin: EOF\n");
|
debug("client-stdin: EOF\n");
|
||||||
server.next_session = "\0";
|
return -1;
|
||||||
return RV_DETACH;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
/* default command to execute if non is given and $ABDUCO_CMD is unset */
|
/* default command to execute if non is given and $ABDUCO_CMD is unset */
|
||||||
#define ABDUCO_CMD "bash"
|
#define ABDUCO_CMD "bash"
|
||||||
/* default detach key, can be overriden at run time using -e option */
|
/* default detach key, can be overriden at run time using -e option */
|
||||||
static char KEY_COMMAND = CTRL('\\');
|
static char KEY_DETACH = CTRL('\\');
|
||||||
/* redraw key to send a SIGWINCH signal to underlying process
|
/* redraw key to send a SIGWINCH signal to underlying process
|
||||||
* (set to 0 to disable the redraw key) */
|
* (set to 0 to disable the redraw key) */
|
||||||
static char KEY_REDRAW = 0;
|
static char KEY_REDRAW = 0;
|
||||||
|
|
|
||||||
87
splex.c
87
splex.c
|
|
@ -62,10 +62,7 @@
|
||||||
|
|
||||||
#define countof(arr) (sizeof(arr) / sizeof((arr)[0]))
|
#define countof(arr) (sizeof(arr) / sizeof((arr)[0]))
|
||||||
|
|
||||||
#define RV_DETACH -1
|
#define RV_DETACH -1000
|
||||||
|
|
||||||
char* splex_session_names = "0123456789";
|
|
||||||
char* shell_cmd[2] = { "/bin/bash", NULL };
|
|
||||||
|
|
||||||
enum PacketType {
|
enum PacketType {
|
||||||
MSG_CONTENT = 0,
|
MSG_CONTENT = 0,
|
||||||
|
|
@ -122,7 +119,6 @@ typedef struct {
|
||||||
const char *session_name;
|
const char *session_name;
|
||||||
char host[255];
|
char host[255];
|
||||||
bool read_pty;
|
bool read_pty;
|
||||||
const char *next_session;
|
|
||||||
} Server;
|
} Server;
|
||||||
|
|
||||||
static Server server = { .running = true, .exit_status = -1, .host = "@localhost" };
|
static Server server = { .running = true, .exit_status = -1, .host = "@localhost" };
|
||||||
|
|
@ -539,7 +535,7 @@ static bool attach_session(const char *name, const bool terminate) {
|
||||||
int status = client_mainloop();
|
int status = client_mainloop();
|
||||||
client_restore_terminal();
|
client_restore_terminal();
|
||||||
if (status == RV_DETACH) {
|
if (status == RV_DETACH) {
|
||||||
info("detached with return value %d", status);
|
info("detached");
|
||||||
} else if (status == -EIO) {
|
} else if (status == -EIO) {
|
||||||
info("exited due to I/O errors");
|
info("exited due to I/O errors");
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -551,70 +547,6 @@ static bool attach_session(const char *name, const bool terminate) {
|
||||||
return terminate;
|
return terminate;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool set_next_session() {
|
|
||||||
char* siter = splex_session_names;
|
|
||||||
info("siter %x\n",siter);
|
|
||||||
char sname[] = "\0";
|
|
||||||
while (*siter != '\0') {
|
|
||||||
sname[0] = *siter;
|
|
||||||
if (session_alive(sname))
|
|
||||||
break;
|
|
||||||
siter++;
|
|
||||||
}
|
|
||||||
info("*siter %c\n",*siter);
|
|
||||||
if (*siter == '\0')
|
|
||||||
return false;
|
|
||||||
server.next_session = sname;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool session_loop() {
|
|
||||||
// Set initial session, or create a new one
|
|
||||||
if (!set_next_session()) {
|
|
||||||
server.next_session = "0";
|
|
||||||
}
|
|
||||||
// printf("%s\n",server.session_name);
|
|
||||||
while (true) {
|
|
||||||
info("Iteration with session %s\n",server.next_session);
|
|
||||||
server.session_name = server.next_session;
|
|
||||||
if (!session_alive(server.session_name))
|
|
||||||
if (!create_session(server.session_name, shell_cmd))
|
|
||||||
die("create-session");
|
|
||||||
if (server.socket > 0)
|
|
||||||
close(server.socket);
|
|
||||||
if ((server.socket = session_connect(server.session_name)) == -1)
|
|
||||||
return false;
|
|
||||||
if (server_set_socket_non_blocking(server.socket) == -1)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
struct sigaction sa;
|
|
||||||
sa.sa_flags = 0;
|
|
||||||
sigemptyset(&sa.sa_mask);
|
|
||||||
sa.sa_handler = client_sigwinch_handler;
|
|
||||||
sigaction(SIGWINCH, &sa, NULL);
|
|
||||||
sa.sa_handler = SIG_IGN;
|
|
||||||
sigaction(SIGPIPE, &sa, NULL);
|
|
||||||
|
|
||||||
client_setup_terminal();
|
|
||||||
int status = client_mainloop();
|
|
||||||
client_restore_terminal();
|
|
||||||
|
|
||||||
if (status == -EIO) {
|
|
||||||
info("exited due to I/O errors");
|
|
||||||
exit(-EIO);
|
|
||||||
} else if (status == RV_DETACH) {
|
|
||||||
info("detached with return value %d and next session %s", status, server.next_session);
|
|
||||||
if (server.next_session[0] == '\0') {
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
info("session terminated with exit status %d", status);
|
|
||||||
if (!set_next_session())
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int session_filter(const struct dirent *d) {
|
static int session_filter(const struct dirent *d) {
|
||||||
return strstr(d->d_name, server.host) != NULL;
|
return strstr(d->d_name, server.host) != NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -687,7 +619,7 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
if (optarg[0] == '^' && optarg[1])
|
if (optarg[0] == '^' && optarg[1])
|
||||||
optarg[0] = CTRL(optarg[1]);
|
optarg[0] = CTRL(optarg[1]);
|
||||||
KEY_COMMAND = optarg[0];
|
KEY_DETACH = optarg[0];
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
usage();
|
usage();
|
||||||
|
|
@ -777,13 +709,12 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
session_loop();
|
if (!session_alive(server.session_name)) {
|
||||||
/* if (!session_alive(server.session_name)) { */
|
if (!create_session(server.session_name, cmd))
|
||||||
/* if (!create_session(server.session_name, cmd)) */
|
die("create-session");
|
||||||
/* die("create-session"); */
|
}
|
||||||
/* } */
|
if (!attach_session(server.session_name, true))
|
||||||
/* if (!attach_session(server.session_name, true)) */
|
die("attach-session");
|
||||||
/* die("attach-session"); */
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue