From: David Goulet Date: Wed, 27 Apr 2011 15:34:10 +0000 (-0400) Subject: Add close unix socket function to libcomm API X-Git-Tag: v2.0-pre1~179 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=87378cf56f08f23cd6715a923ba10d98343902d7;p=lttng-tools.git Add close unix socket function to libcomm API Using clean_exit everywhere in lttng now and adding the session daemon disconnect to that clean exit function. Also fix the recv <= 0 for process client msg on the session daemon side in order to not send back empty data. Signed-off-by: David Goulet --- diff --git a/include/lttng/liblttngctl.h b/include/lttng/liblttngctl.h index df9731342..a3bdff807 100644 --- a/include/lttng/liblttngctl.h +++ b/include/lttng/liblttngctl.h @@ -45,6 +45,7 @@ struct lttng_session { extern int lttng_create_session(char *name, uuid_t *session_id); extern int lttng_destroy_session(uuid_t *uuid); extern int lttng_connect_sessiond(void); +extern int lttng_disconnect_sessiond(void); extern int lttng_set_tracing_group(const char *name); extern int lttng_check_session_daemon(void); extern const char *lttng_get_readable_code(int code); diff --git a/liblttngctl/liblttngctl.c b/liblttngctl/liblttngctl.c index a15a26dff..7c5af7fe7 100644 --- a/liblttngctl/liblttngctl.c +++ b/liblttngctl/liblttngctl.c @@ -280,6 +280,24 @@ int lttng_connect_sessiond(void) return 0; } +/* + * lttng_disconnect_sessiond + * + * Clean disconnect the session daemon. + */ +int lttng_disconnect_sessiond(void) +{ + int ret = 0; + + if (connected) { + ret = lttcomm_close_unix_sock(sessiond_socket); + sessiond_socket = 0; + connected = 0; + } + + return ret; +} + /* * lttng_set_current_session_uuid * diff --git a/liblttsessiondcomm/liblttsessiondcomm.c b/liblttsessiondcomm/liblttsessiondcomm.c index 0e9c290fe..8413f6b08 100644 --- a/liblttsessiondcomm/liblttsessiondcomm.c +++ b/liblttsessiondcomm/liblttsessiondcomm.c @@ -226,3 +226,21 @@ ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len) return ret; } + +/* + * lttcomm_close_unix_sock + * + * Shutdown cleanly a unix socket. + */ +int lttcomm_close_unix_sock(int sock) +{ + int ret; + + /* Shutdown receptions and transmissions */ + ret = shutdown(sock, SHUT_RDWR); + if (ret < 0) { + perror("shutdown"); + } + + return ret; +} diff --git a/liblttsessiondcomm/liblttsessiondcomm.h b/liblttsessiondcomm/liblttsessiondcomm.h index 6d6290d0a..83dcdc377 100644 --- a/liblttsessiondcomm/liblttsessiondcomm.h +++ b/liblttsessiondcomm/liblttsessiondcomm.h @@ -135,6 +135,7 @@ extern int lttcomm_create_unix_sock(const char *pathname); extern int lttcomm_connect_unix_sock(const char *pathname); extern int lttcomm_accept_unix_sock(int sock); extern int lttcomm_listen_unix_sock(int sock); +extern int lttcomm_close_unix_sock(int sock); extern ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len); extern ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len); extern const char *lttcomm_get_readable_code(enum lttcomm_return_code code); diff --git a/ltt-sessiond/ltt-sessiond.c b/ltt-sessiond/ltt-sessiond.c index 3590d7e44..eee4cc84f 100644 --- a/ltt-sessiond/ltt-sessiond.c +++ b/ltt-sessiond/ltt-sessiond.c @@ -217,7 +217,7 @@ static void *thread_manage_clients(void *data) * request of the client. */ ret = lttcomm_recv_unix_sock(sock, &lsm, sizeof(lsm)); - if (ret < 0) { + if (ret <= 0) { continue; } @@ -897,6 +897,7 @@ static void sighandler(int sig) { switch (sig) { case SIGPIPE: + return; case SIGINT: case SIGTERM: cleanup(); diff --git a/lttng/lttng.c b/lttng/lttng.c index 9ef92a7d4..40cf52bc6 100644 --- a/lttng/lttng.c +++ b/lttng/lttng.c @@ -357,6 +357,9 @@ static void sighandler(int sig) void clean_exit(int code) { DBG("Clean exit"); + if (lttng_disconnect_sessiond() < 0) { + ERR("Session daemon disconnect failed."); + } exit(code); } @@ -376,12 +379,12 @@ int main(int argc, char *argv[]) ret = parse_args(argc, (const char **) argv); if (ret < 0) { - return EXIT_FAILURE; + clean_exit(EXIT_FAILURE); } ret = set_signal_handler(); if (ret < 0) { - return ret; + clean_exit(ret); } if (opt_tracing_group != NULL) { @@ -394,7 +397,7 @@ int main(int argc, char *argv[]) DBG("Kernel tracing activated"); if (getuid() != 0) { ERR("%s must be setuid root", progname); - return -EPERM; + clean_exit(-EPERM); } } @@ -402,13 +405,15 @@ int main(int argc, char *argv[]) * If no, a daemon will be spawned. */ if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) { - return EXIT_FAILURE; + clean_exit(EXIT_FAILURE); } ret = process_client_opt(); if (ret < 0) { - return ret; + clean_exit(ret); } + clean_exit(0); + return 0; }