From 69207c3050bbc73d27b67190b70efc65d5a343e6 Mon Sep 17 00:00:00 2001 From: Nicolas Viennot Date: Thu, 13 Jun 2013 04:13:40 -0400 Subject: [PATCH] Debug message clean up --- tmate-debug.c | 6 +++--- tmate-slave.c | 16 ++++++++------- tmate-ssh-client-pty.c | 21 +++++++------------ tmate-ssh-client.c | 23 +++++++-------------- tmate-ssh-server.c | 46 +++++++++++++++++++++++++++++++++++++----- tmate.h | 9 +++++++-- 6 files changed, 74 insertions(+), 47 deletions(-) diff --git a/tmate-debug.c b/tmate-debug.c index c480e658..f69bcdc5 100644 --- a/tmate-debug.c +++ b/tmate-debug.c @@ -47,7 +47,7 @@ static int print_resolved_stack_frame(const char *frame) return -1; line[strlen(line)-1] = 0; /* remove \n */ - tmate_debug("%s(%s) [%s]", file, line, address); + tmate_info("%s(%s) [%s]", file, line, address); return 0; } #endif @@ -62,13 +62,13 @@ void tmate_print_trace(void) size = backtrace (array, 20); strings = backtrace_symbols (array, size); - tmate_debug ("============ %zd stack frames ============", size); + tmate_info("============ %zd stack frames ============", size); for (i = 1; i < size; i++) { #if DEBUG if (print_resolved_stack_frame(strings[i]) < 0) #endif - tmate_debug("%s", strings[i]); + tmate_info("%s", strings[i]); } free (strings); diff --git a/tmate-slave.c b/tmate-slave.c index f63a6ea6..2942e779 100644 --- a/tmate-slave.c +++ b/tmate-slave.c @@ -26,7 +26,7 @@ extern int client_connect(char *path, int start_server); static void usage(void) { - fprintf(stderr, "usage: tmate-slave [-p PORT]\n"); + fprintf(stderr, "usage: tmate-slave [-l logfile] [-p PORT] [-v]\n"); } int main(int argc, char **argv) @@ -64,15 +64,17 @@ int main(int argc, char **argv) tmate_ssh_server_main(port); return 0; } - -static void set_session_token(const char *token, int master) +static void set_session_token(struct tmate_ssh_client *client, + const char *token) { tmate_session_token = xstrdup(token); strcpy(socket_path, TMATE_WORKDIR "/sessions/"); strcat(socket_path, token); - sprintf(cmdline, "tmate-slave (%s) %s", - master ? "master" : "client", tmate_session_token); + sprintf(cmdline, "tmate-slave [%s] %s %s", + tmate_session_token, + client->ip_address, + client->role == TMATE_ROLE_SERVER ? "(server)" : ""); } static char tmate_token_digits[] = "abcdefghijklmnopqrstuvwxyz" @@ -215,7 +217,7 @@ static void tmate_spawn_slave_server(struct tmate_ssh_client *client) struct tmate_decoder decoder; token = get_random_token(); - set_session_token(token, 1); + set_session_token(client, token); free(token); tmate_debug("Spawning tmux slave server"); @@ -257,7 +259,7 @@ static void tmate_spawn_slave_client(struct tmate_ssh_client *client) tmate_fatal("Bad token"); } - set_session_token(token, 0); + set_session_token(client, token); tmate_debug("Spawn tmux slave client"); diff --git a/tmate-ssh-client-pty.c b/tmate-ssh-client-pty.c index b36910af..4c1af5b0 100644 --- a/tmate-ssh-client-pty.c +++ b/tmate-ssh-client-pty.c @@ -16,9 +16,11 @@ static void consume_channel(struct tmate_ssh_client *client) len = ssh_channel_read_nonblocking(client->channel, buf, sizeof(buf), 0); if (len < 0) { - tmate_debug("Error reading from channel: %s", + if (!ssh_is_connected(client->session)) + tmate_fatal("Disconnected"); + + tmate_fatal("Error reading from channel: %s", ssh_get_error(client->session)); - exit(1); } if (len == 0) @@ -41,13 +43,7 @@ static void consume_channel(struct tmate_ssh_client *client) static void on_session_event(struct tmate_ssh_client *client) { ssh_execute_message_callbacks(client->session); - consume_channel(client); - - if (!ssh_is_connected(client->session)) { - tmate_debug("Disconnected"); - exit(1); - } } static void __on_session_event(evutil_socket_t fd, short what, void *arg) @@ -104,15 +100,12 @@ static void on_pty_event(struct tmate_ssh_client *client) tmate_fatal("pty reached EOF"); written = ssh_channel_write(client->channel, buf, len); - if (written < 0) { - tmate_debug("Error writing to channel: %s", + if (written < 0) + tmate_fatal("Error writing to channel: %s", ssh_get_error(client->session)); - exit(1); - } - if (len != written) { + if (len != written) tmate_fatal("Cannot write %d bytes, wrote %d", (int)len, (int)written); - } } } diff --git a/tmate-ssh-client.c b/tmate-ssh-client.c index 9a425d75..5885a8c2 100644 --- a/tmate-ssh-client.c +++ b/tmate-ssh-client.c @@ -18,10 +18,11 @@ static void consume_channel(struct tmate_ssh_client *client) len = ssh_channel_read_nonblocking(client->channel, buf, len, 0); if (len < 0) { - tmate_debug("Error reading from channel: %s", + if (!ssh_is_connected(client->session)) + tmate_fatal("Disconnected"); + + tmate_fatal("Error reading from channel: %s", ssh_get_error(client->session)); - server_shutdown = 1; - break; } if (len == 0) break; @@ -32,15 +33,8 @@ static void consume_channel(struct tmate_ssh_client *client) static void on_session_event(struct tmate_ssh_client *client) { - ssh_session session = client->session; - ssh_channel channel = client->channel; - + ssh_execute_message_callbacks(client->session); consume_channel(client); - if (!ssh_is_connected(session)) { - tmate_debug("Disconnected"); - server_shutdown = 1; - return; - } } static void __on_session_event(evutil_socket_t fd, short what, void *arg) @@ -69,12 +63,9 @@ static void flush_input_stream(struct tmate_ssh_client *client) buf = evbuffer_pullup(evb, -1); written = ssh_channel_write(client->channel, buf, len); - if (written < 0) { - tmate_debug("Error writing to channel: %s", + if (written < 0) + tmate_fatal("Error writing to channel: %s", ssh_get_error(client->session)); - server_shutdown = 1; - break; - } evbuffer_drain(evb, written); } diff --git a/tmate-ssh-server.c b/tmate-ssh-server.c index bb8b0d43..d894ce60 100644 --- a/tmate-ssh-server.c +++ b/tmate-ssh-server.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "tmate.h" @@ -179,13 +180,13 @@ static void handle_sigchld(void) if (!child_dead) continue; - tmate_debug("Child reaped pid=%d exit=%d", pid, child_exit_status); + tmate_info("Child reaped pid=%d exit=%d", pid, child_exit_status); } while (pid > 0); } static void handle_sigalrm(void) { - log_fatal("Connection grace period (%d) passed", SSH_GRACE_PERIOD); + tmate_fatal("Connection grace period (%d) passed", SSH_GRACE_PERIOD); } static void signal_handler(int sig) @@ -220,6 +221,33 @@ static pid_t namespace_fork(void) return syscall(SYS_clone, flags | SIGCHLD, NULL, NULL, NULL); } +static int get_ip(int fd, char *dst, size_t len) +{ + struct sockaddr sa; + socklen_t sa_len = sizeof(sa); + + if (getpeername(fd, &sa, &sa_len) < 0) + return -1; + + + switch (sa.sa_family) { + case AF_INET: + if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&sa)->sin_addr, + dst, len)) + return -1; + break; + case AF_INET6: + if (!inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&sa)->sin6_addr, + dst, len)) + return -1; + break; + default: + return -1; + } + + return 0; +} + void tmate_ssh_server_main(int port) { struct tmate_ssh_client _client; @@ -235,7 +263,7 @@ void tmate_ssh_server_main(int port) bind = ssh_bind_new(); if (!bind) - log_fatal("Cannot initialize ssh"); + tmate_fatal("Cannot initialize ssh"); ssh_bind_options_set(bind, SSH_BIND_OPTIONS_BINDPORT, &port); ssh_bind_options_set(bind, SSH_BIND_OPTIONS_BANNER, SSH_BANNER); @@ -244,7 +272,9 @@ void tmate_ssh_server_main(int port) ssh_bind_options_set(bind, SSH_BIND_OPTIONS_RSAKEY, "keys/ssh_host_rsa_key"); if (ssh_bind_listen(bind) < 0) - log_fatal("Error listening to socket: %s\n", ssh_get_error(bind)); + tmate_fatal("Error listening to socket: %s\n", ssh_get_error(bind)); + + tmate_info("Accepting connections on %d", port); for (;;) { client->session = ssh_new(); @@ -257,14 +287,20 @@ void tmate_ssh_server_main(int port) if (ssh_bind_accept(bind, client->session) < 0) tmate_fatal("Error accepting connection: %s", ssh_get_error(bind)); + if (get_ip(ssh_get_fd(client->session), + client->ip_address, sizeof(client->ip_address)) < 0) + tmate_fatal("Error getting IP address from connection"); + if ((pid = namespace_fork()) < 0) tmate_fatal("Can't fork in new namespace"); if (pid) { + tmate_info("Child spawned pid=%d, ip=%s", + pid, client->ip_address); ssh_free(client->session); } else { ssh_bind_free(bind); - tmate_debug("Child spawned pid=%d", getpid()); + tmate_session_token = "........................."; client_bootstrap(client); } } diff --git a/tmate.h b/tmate.h index 8a2e1faf..fb0cd4fd 100644 --- a/tmate.h +++ b/tmate.h @@ -8,9 +8,12 @@ #include "tmux.h" #define tmate_debug(str, ...) log_debug("[tmate] " str, ##__VA_ARGS__) -#define tmate_warn(str, ...) log_warn("[tmate] " str, ##__VA_ARGS__) #define tmate_info(str, ...) log_info("[tmate] " str, ##__VA_ARGS__) -#define tmate_fatal(str, ...) log_fatal("[tmate] " str, ##__VA_ARGS__) +#define tmate_fatal(str, ...) \ +do { \ + log_info("[tmate] FATAL " str, ##__VA_ARGS__); \ + exit(-1); \ +} while (0) /* tmate-encoder.c */ @@ -73,6 +76,8 @@ typedef struct ssh_channel_struct* ssh_channel; #define TMATE_ROLE_CLIENT 2 struct tmate_ssh_client { + char ip_address[64]; + ssh_session session; ssh_channel channel;