forked from github/abduco
Compare commits
4 Commits
6c6b505327
...
d9c18f680a
| Author | SHA1 | Date |
|---|---|---|
|
|
d9c18f680a | |
|
|
d8f557ed20 | |
|
|
0f89851632 | |
|
|
64d897db14 |
56
client.c
56
client.c
|
|
@ -123,25 +123,73 @@ static int client_mainloop(void) {
|
|||
debug("client-stdin: %c\n", pkt.u.msg[0]);
|
||||
if (client.await_command) {
|
||||
client.await_command = false;
|
||||
if (pkt.u.msg[0]=='d') {
|
||||
bool detach = false;
|
||||
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.len = 0;
|
||||
client_send_packet(&pkt);
|
||||
close(server.socket);
|
||||
return -1;
|
||||
return RV_DETACH;
|
||||
}
|
||||
}
|
||||
pkt.len = len;
|
||||
if (KEY_REDRAW && pkt.u.msg[0] == KEY_REDRAW) {
|
||||
client.need_resize = true;
|
||||
} else if (pkt.u.msg[0] == KEY_DETACH) {
|
||||
} else if (pkt.u.msg[0] == KEY_COMMAND) {
|
||||
client.await_command = true;
|
||||
} else if (!(client.flags & CLIENT_READONLY)) {
|
||||
client_send_packet(&pkt);
|
||||
}
|
||||
} else if (len == 0) {
|
||||
debug("client-stdin: EOF\n");
|
||||
return -1;
|
||||
server.next_session = "\0";
|
||||
return RV_DETACH;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
/* default command to execute if non is given and $ABDUCO_CMD is unset */
|
||||
#define ABDUCO_CMD "bash"
|
||||
/* default detach key, can be overriden at run time using -e option */
|
||||
static char KEY_DETACH = CTRL('\\');
|
||||
static char KEY_COMMAND = CTRL('\\');
|
||||
/* redraw key to send a SIGWINCH signal to underlying process
|
||||
* (set to 0 to disable the redraw key) */
|
||||
static char KEY_REDRAW = 0;
|
||||
|
|
|
|||
87
splex.c
87
splex.c
|
|
@ -62,7 +62,10 @@
|
|||
|
||||
#define countof(arr) (sizeof(arr) / sizeof((arr)[0]))
|
||||
|
||||
#define RV_DETACH -1000
|
||||
#define RV_DETACH -1
|
||||
|
||||
char* splex_session_names = "0123456789";
|
||||
char* shell_cmd[2] = { "/bin/bash", NULL };
|
||||
|
||||
enum PacketType {
|
||||
MSG_CONTENT = 0,
|
||||
|
|
@ -119,6 +122,7 @@ typedef struct {
|
|||
const char *session_name;
|
||||
char host[255];
|
||||
bool read_pty;
|
||||
const char *next_session;
|
||||
} Server;
|
||||
|
||||
static Server server = { .running = true, .exit_status = -1, .host = "@localhost" };
|
||||
|
|
@ -535,7 +539,7 @@ static bool attach_session(const char *name, const bool terminate) {
|
|||
int status = client_mainloop();
|
||||
client_restore_terminal();
|
||||
if (status == RV_DETACH) {
|
||||
info("detached");
|
||||
info("detached with return value %d", status);
|
||||
} else if (status == -EIO) {
|
||||
info("exited due to I/O errors");
|
||||
} else {
|
||||
|
|
@ -547,6 +551,70 @@ static bool attach_session(const char *name, const bool 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) {
|
||||
return strstr(d->d_name, server.host) != NULL;
|
||||
}
|
||||
|
|
@ -619,7 +687,7 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
if (optarg[0] == '^' && optarg[1])
|
||||
optarg[0] = CTRL(optarg[1]);
|
||||
KEY_DETACH = optarg[0];
|
||||
KEY_COMMAND = optarg[0];
|
||||
break;
|
||||
case 'h':
|
||||
usage();
|
||||
|
|
@ -709,12 +777,13 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
break;
|
||||
case 't':
|
||||
if (!session_alive(server.session_name)) {
|
||||
if (!create_session(server.session_name, cmd))
|
||||
die("create-session");
|
||||
}
|
||||
if (!attach_session(server.session_name, true))
|
||||
die("attach-session");
|
||||
session_loop();
|
||||
/* if (!session_alive(server.session_name)) { */
|
||||
/* if (!create_session(server.session_name, cmd)) */
|
||||
/* die("create-session"); */
|
||||
/* } */
|
||||
/* if (!attach_session(server.session_name, true)) */
|
||||
/* die("attach-session"); */
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue