X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=liblttngctl%2Flttngctl.c;h=b64ace2e6d80de0ce085791f698604e53f8eb523;hp=b266797bd1b5aa692559cecd4d555d61a2c3083e;hb=35346445460dfe7ba5bbcfdbe279c07c1ee3219b;hpb=e88129fcabf35d124a7d97f6600350745dcbbb81 diff --git a/liblttngctl/lttngctl.c b/liblttngctl/lttngctl.c index b266797bd..b64ace2e6 100644 --- a/liblttngctl/lttngctl.c +++ b/liblttngctl/lttngctl.c @@ -21,27 +21,22 @@ */ #define _GNU_SOURCE -#include #include +#include #include #include #include #include -#include - #include -#include "lttngerr.h" -#include "lttng-share.h" +#include +#include +#include /* Socket to session daemon for communication */ static int sessiond_socket; static char sessiond_sock_path[PATH_MAX]; -/* Communication structure to ltt-sessiond */ -static struct lttcomm_session_msg lsm; -static struct lttcomm_lttng_msg llm; - /* Variables */ static char *tracing_group; static int connected; @@ -55,18 +50,41 @@ static void copy_string(char *dst, const char *src, size_t len) strncpy(dst, src, len); /* Enforce the NULL terminated byte */ dst[len - 1] = '\0'; + } else if (dst) { + dst[0] = '\0'; } } /* - * send_data_sessiond + * Copy domain to lttcomm_session_msg domain. * - * Send lttcomm_session_msg to the session daemon. + * If domain is unknown, default domain will be the kernel. + */ +static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src) +{ + if (src && dst) { + switch (src->type) { + case LTTNG_DOMAIN_KERNEL: + case LTTNG_DOMAIN_UST: + case LTTNG_DOMAIN_UST_EXEC_NAME: + case LTTNG_DOMAIN_UST_PID: + case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: + memcpy(dst, src, sizeof(struct lttng_domain)); + break; + default: + dst->type = LTTNG_DOMAIN_KERNEL; + break; + } + } +} + +/* + * Send lttcomm_session_msg to the session daemon. * - * On success, return 0 - * On error, return error code + * On success, return 0 + * On error, return error code */ -static int send_data_sessiond(void) +static int send_session_msg(struct lttcomm_session_msg *lsm) { int ret; @@ -75,19 +93,18 @@ static int send_data_sessiond(void) goto end; } - ret = lttcomm_send_unix_sock(sessiond_socket, &lsm, sizeof(lsm)); + ret = lttcomm_send_unix_sock(sessiond_socket, lsm, + sizeof(struct lttcomm_session_msg)); end: return ret; } /* - * recv_data_sessiond + * Receive data from the sessiond socket. * - * Receive data from the sessiond socket. - * - * On success, return 0 - * On error, return recv() error code + * On success, return 0 + * On error, return recv() error code */ static int recv_data_sessiond(void *buf, size_t len) { @@ -107,7 +124,7 @@ end: /* * Check if the specified group name exist. * - * If yes return 0, else return -1. + * If yes return 1, else return -1. */ static int check_tracing_group(const char *grp_name) { @@ -143,7 +160,7 @@ static int check_tracing_group(const char *grp_name) for (i = 0; i < grp_list_size; i++) { if (grp_list[i] == grp_tracing->gr_gid) { - ret = 0; + ret = 1; break; } } @@ -156,24 +173,79 @@ end: } /* - * Set sessiond socket path by putting it in the global sessiond_sock_path - * variable. + * Try connect to session daemon with sock_path. + * + * Return 0 on success, else -1 */ -static int set_session_daemon_path(void) +static int try_connect_sessiond(const char *sock_path) { int ret; - /* Are we in the tracing group ? */ - ret = check_tracing_group(tracing_group); - if (ret < 0 && getuid() != 0) { + /* If socket exist, we check if the daemon listens for connect. */ + ret = access(sock_path, F_OK); + if (ret < 0) { + /* Not alive */ + return -1; + } + + ret = lttcomm_connect_unix_sock(sock_path); + if (ret < 0) { + /* Not alive */ + return -1; + } + + ret = lttcomm_close_unix_sock(ret); + if (ret < 0) { + perror("lttcomm_close_unix_sock"); + } + + return 0; +} + +/* + * Set sessiond socket path by putting it in the global sessiond_sock_path + * variable. + */ +static int set_session_daemon_path(void) +{ + int ret; + int in_tgroup = 0; /* In tracing group */ + uid_t uid; + + uid = getuid(); + + if (uid != 0) { + /* Are we in the tracing group ? */ + in_tgroup = check_tracing_group(tracing_group); + } + + if (uid == 0) { + /* Root */ + copy_string(sessiond_sock_path, + DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, + sizeof(sessiond_sock_path)); + } else if (in_tgroup) { + /* Tracing group */ + copy_string(sessiond_sock_path, + DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, + sizeof(sessiond_sock_path)); + + ret = try_connect_sessiond(sessiond_sock_path); + if (ret < 0) { + /* Global session daemon not available */ + if (snprintf(sessiond_sock_path, sizeof(sessiond_sock_path), + DEFAULT_HOME_CLIENT_UNIX_SOCK, + getenv("HOME")) < 0) { + return -ENOMEM; + } + } + } else { + /* Not in tracing group and not root, default */ if (snprintf(sessiond_sock_path, PATH_MAX, - DEFAULT_HOME_CLIENT_UNIX_SOCK, - getenv("HOME")) < 0) { + DEFAULT_HOME_CLIENT_UNIX_SOCK, + getenv("HOME")) < 0) { return -ENOMEM; } - } else { - copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, - PATH_MAX); } return 0; @@ -222,35 +294,24 @@ static int disconnect_sessiond(void) } /* - * Reset the session message structure. - */ -static void reset_session_msg(void) -{ - memset(&lsm, 0, sizeof(struct lttcomm_session_msg)); -} - -/* - * ask_sessiond - * - * Ask the session daemon a specific command and put the data into buf. + * Ask the session daemon a specific command and put the data into buf. * - * Return size of data (only payload, not header). + * Return size of data (only payload, not header). */ -static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf) +static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf) { int ret; size_t size; void *data = NULL; + struct lttcomm_lttng_msg llm; ret = connect_sessiond(); if (ret < 0) { goto end; } - lsm.cmd_type = lct; - /* Send command to session daemon */ - ret = send_data_sessiond(); + ret = send_session_msg(lsm); if (ret < 0) { goto end; } @@ -282,149 +343,263 @@ static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf) goto end; } + /* + * Extra protection not to dereference a NULL pointer. If buf is NULL at + * this point, an error is returned and data is freed. + */ + if (buf == NULL) { + ret = -1; + free(data); + goto end; + } + *buf = data; ret = size; end: disconnect_sessiond(); - reset_session_msg(); return ret; } /* - * Copy domain to lttcomm_session_msg domain. If unknown domain, default domain - * will be the kernel. + * Create lttng handle and return pointer. */ -static void copy_lttng_domain(struct lttng_domain *dom) +struct lttng_handle *lttng_create_handle(const char *session_name, + struct lttng_domain *domain) { - if (dom) { - switch (dom->type) { - case LTTNG_DOMAIN_KERNEL: - case LTTNG_DOMAIN_UST: - case LTTNG_DOMAIN_UST_EXEC_NAME: - case LTTNG_DOMAIN_UST_PID: - case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: - memcpy(&lsm.domain, dom, sizeof(struct lttng_domain)); - break; - default: - lsm.domain.type = LTTNG_DOMAIN_KERNEL; - break; - } + struct lttng_handle *handle; + + handle = malloc(sizeof(struct lttng_handle)); + if (handle == NULL) { + perror("malloc handle"); + goto end; + } + + /* Copy session name */ + copy_string(handle->session_name, session_name, + sizeof(handle->session_name)); + + /* Copy lttng domain */ + copy_lttng_domain(&handle->domain, domain); + +end: + return handle; +} + +/* + * Destroy handle by free(3) the pointer. + */ +void lttng_destroy_handle(struct lttng_handle *handle) +{ + if (handle) { + free(handle); } } +/* + * Register an outside consumer. + */ +int lttng_register_consumer(struct lttng_handle *handle, + const char *socket_path) +{ + struct lttcomm_session_msg lsm; + + lsm.cmd_type = LTTNG_REGISTER_CONSUMER; + copy_string(lsm.session.name, handle->session_name, + sizeof(lsm.session.name)); + copy_lttng_domain(&lsm.domain, &handle->domain); + + copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path)); + + return ask_sessiond(&lsm, NULL); +} + /* * Start tracing for all trace of the session. */ -int lttng_start_tracing(const char *session_name) +int lttng_start_tracing(struct lttng_handle *handle) { - copy_string(lsm.session.name, session_name, NAME_MAX); - return ask_sessiond(LTTNG_START_TRACE, NULL); + struct lttcomm_session_msg lsm; + + if (!handle) { + return -1; + } + + lsm.cmd_type = LTTNG_START_TRACE; + copy_string(lsm.session.name, handle->session_name, + sizeof(lsm.session.name)); + + return ask_sessiond(&lsm, NULL); } /* * Stop tracing for all trace of the session. */ -int lttng_stop_tracing(const char *session_name) +int lttng_stop_tracing(struct lttng_handle *handle) { - copy_string(lsm.session.name, session_name, NAME_MAX); - return ask_sessiond(LTTNG_STOP_TRACE, NULL); + struct lttcomm_session_msg lsm; + + lsm.cmd_type = LTTNG_STOP_TRACE; + copy_string(lsm.session.name, handle->session_name, + sizeof(lsm.session.name)); + + return ask_sessiond(&lsm, NULL); } /* - * lttng_add_context + * Add context to event or/and channel. */ -int lttng_add_context(struct lttng_domain *domain, +int lttng_add_context(struct lttng_handle *handle, struct lttng_event_context *ctx, const char *event_name, const char *channel_name) { - copy_string(lsm.u.context.channel_name, channel_name, NAME_MAX); - copy_string(lsm.u.context.event_name, event_name, NAME_MAX); - copy_lttng_domain(domain); + struct lttcomm_session_msg lsm; + + if (!handle) { + return -1; + } + + lsm.cmd_type = LTTNG_ADD_CONTEXT; + + /* Copy channel name */ + copy_string(lsm.u.context.channel_name, channel_name, + sizeof(lsm.u.context.channel_name)); + /* Copy event name */ + copy_string(lsm.u.context.event_name, event_name, + sizeof(lsm.u.context.event_name)); + + copy_lttng_domain(&lsm.domain, &handle->domain); if (ctx) { memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); } - return ask_sessiond(LTTNG_ADD_CONTEXT, NULL); + copy_string(lsm.session.name, handle->session_name, + sizeof(lsm.session.name)); + + return ask_sessiond(&lsm, NULL); } /* - * lttng_enable_event + * Enable event */ -int lttng_enable_event(struct lttng_domain *domain, +int lttng_enable_event(struct lttng_handle *handle, struct lttng_event *ev, const char *channel_name) { - int ret; + struct lttcomm_session_msg lsm; + + if (!handle) { + return -1; + } if (channel_name == NULL) { - copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX); + copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, + sizeof(lsm.u.enable.channel_name)); } else { - copy_string(lsm.u.enable.channel_name, channel_name, NAME_MAX); + copy_string(lsm.u.enable.channel_name, channel_name, + sizeof(lsm.u.enable.channel_name)); } - copy_lttng_domain(domain); + copy_lttng_domain(&lsm.domain, &handle->domain); - if (ev == NULL) { - ret = ask_sessiond(LTTNG_ENABLE_ALL_EVENT, NULL); + if (ev) { + lsm.cmd_type = LTTNG_ENABLE_EVENT; + memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event)); } else { - memcpy(&lsm.u.enable.event, ev, sizeof(struct lttng_event)); - ret = ask_sessiond(LTTNG_ENABLE_EVENT, NULL); + lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT; } - return ret; + copy_string(lsm.session.name, handle->session_name, + sizeof(lsm.session.name)); + + return ask_sessiond(&lsm, NULL); } /* * Disable event of a channel and domain. */ -int lttng_disable_event(struct lttng_domain *domain, const char *name, +int lttng_disable_event(struct lttng_handle *handle, const char *name, const char *channel_name) { - int ret = -1; + struct lttcomm_session_msg lsm; - if (channel_name == NULL) { - copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX); + if (!handle) { + return -1; + } + + if (channel_name) { + copy_string(lsm.u.disable.channel_name, channel_name, + sizeof(lsm.u.disable.channel_name)); } else { - copy_string(lsm.u.disable.channel_name, channel_name, NAME_MAX); + copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, + sizeof(lsm.u.disable.channel_name)); } - copy_lttng_domain(domain); + copy_lttng_domain(&lsm.domain, &handle->domain); if (name == NULL) { - ret = ask_sessiond(LTTNG_DISABLE_ALL_EVENT, NULL); + copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name)); + lsm.cmd_type = LTTNG_DISABLE_EVENT; } else { - copy_string(lsm.u.disable.name, name, NAME_MAX); - ret = ask_sessiond(LTTNG_DISABLE_EVENT, NULL); + lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT; } - return ret; + copy_string(lsm.session.name, handle->session_name, + sizeof(lsm.session.name)); + + return ask_sessiond(&lsm, NULL); } /* * Enable channel per domain */ -int lttng_enable_channel(struct lttng_domain *domain, +int lttng_enable_channel(struct lttng_handle *handle, struct lttng_channel *chan) { + struct lttcomm_session_msg lsm; + + if (!handle) { + return -1; + } + if (chan) { - memcpy(&lsm.u.channel.chan, chan, sizeof(struct lttng_channel)); + memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan)); } - copy_lttng_domain(domain); + lsm.cmd_type = LTTNG_ENABLE_CHANNEL; + + copy_lttng_domain(&lsm.domain, &handle->domain); - return ask_sessiond(LTTNG_ENABLE_CHANNEL, NULL); + copy_string(lsm.session.name, handle->session_name, + sizeof(lsm.session.name)); + + return ask_sessiond(&lsm, NULL); } /* * All tracing will be stopped for registered events of the channel. */ -int lttng_disable_channel(struct lttng_domain *domain, const char *name) +int lttng_disable_channel(struct lttng_handle *handle, const char *name) { - copy_string(lsm.u.disable.channel_name, name, NAME_MAX); - copy_lttng_domain(domain); + struct lttcomm_session_msg lsm; + + if (!handle) { + return -1; + } + + if (name) { + copy_string(lsm.u.disable.channel_name, name, + sizeof(lsm.u.disable.channel_name)); + } + + lsm.cmd_type = LTTNG_DISABLE_CHANNEL; - return ask_sessiond(LTTNG_DISABLE_CHANNEL, NULL); + copy_lttng_domain(&lsm.domain, &handle->domain); + + copy_string(lsm.session.name, handle->session_name, + sizeof(lsm.session.name)); + + return ask_sessiond(&lsm, NULL); } /* @@ -433,14 +608,20 @@ int lttng_disable_channel(struct lttng_domain *domain, const char *name) * Return the size (bytes) of the list and set the events array. * On error, return negative value. */ -int lttng_list_tracepoints(struct lttng_domain *domain, +int lttng_list_tracepoints(struct lttng_handle *handle, struct lttng_event **events) { int ret; + struct lttcomm_session_msg lsm; - copy_lttng_domain(domain); + if (!handle) { + return -1; + } - ret = ask_sessiond(LTTNG_LIST_TRACEPOINTS, (void **) events); + lsm.cmd_type = LTTNG_LIST_TRACEPOINTS; + copy_lttng_domain(&lsm.domain, &handle->domain); + + ret = ask_sessiond(&lsm, (void **) events); if (ret < 0) { return ret; } @@ -465,18 +646,31 @@ const char *lttng_get_readable_code(int code) */ int lttng_create_session(const char *name, const char *path) { - copy_string(lsm.session.name, name, NAME_MAX); - copy_string(lsm.session.path, path, PATH_MAX); - return ask_sessiond(LTTNG_CREATE_SESSION, NULL); + struct lttcomm_session_msg lsm; + + lsm.cmd_type = LTTNG_CREATE_SESSION; + copy_string(lsm.session.name, name, sizeof(lsm.session.name)); + copy_string(lsm.session.path, path, sizeof(lsm.session.path)); + + return ask_sessiond(&lsm, NULL); } /* * Destroy session using name. */ -int lttng_destroy_session(const char *name) +int lttng_destroy_session(struct lttng_handle *handle) { - copy_string(lsm.session.name, name, NAME_MAX); - return ask_sessiond(LTTNG_DESTROY_SESSION, NULL); + struct lttcomm_session_msg lsm; + + if (!handle) { + return -1; + } + + lsm.cmd_type = LTTNG_DESTROY_SESSION; + copy_string(lsm.session.name, handle->session_name, + sizeof(lsm.session.name)); + + return ask_sessiond(&lsm, NULL); } /* @@ -488,8 +682,10 @@ int lttng_destroy_session(const char *name) int lttng_list_sessions(struct lttng_session **sessions) { int ret; + struct lttcomm_session_msg lsm; - ret = ask_sessiond(LTTNG_LIST_SESSIONS, (void**) sessions); + lsm.cmd_type = LTTNG_LIST_SESSIONS; + ret = ask_sessiond(&lsm, (void**) sessions); if (ret < 0) { return ret; } @@ -500,12 +696,22 @@ int lttng_list_sessions(struct lttng_session **sessions) /* * List domain of a session. */ -int lttng_list_domains(const char *session_name, struct lttng_domain **domains) +int lttng_list_domains(struct lttng_handle *handle, + struct lttng_domain **domains) { int ret; + struct lttcomm_session_msg lsm; + + if (!handle) { + return -1; + } + + lsm.cmd_type = LTTNG_LIST_DOMAINS; + + copy_string(lsm.session.name, handle->session_name, + sizeof(lsm.session.name)); - copy_string(lsm.session.name, session_name, NAME_MAX); - ret = ask_sessiond(LTTNG_LIST_DOMAINS, (void**) domains); + ret = ask_sessiond(&lsm, (void**) domains); if (ret < 0) { return ret; } @@ -516,15 +722,23 @@ int lttng_list_domains(const char *session_name, struct lttng_domain **domains) /* * List channels of a session */ -int lttng_list_channels(struct lttng_domain *domain, - const char *session_name, struct lttng_channel **channels) +int lttng_list_channels(struct lttng_handle *handle, + struct lttng_channel **channels) { int ret; + struct lttcomm_session_msg lsm; - copy_string(lsm.session.name, session_name, NAME_MAX); - copy_lttng_domain(domain); + if (!handle) { + return -1; + } + + lsm.cmd_type = LTTNG_LIST_CHANNELS; + copy_string(lsm.session.name, handle->session_name, + sizeof(lsm.session.name)); - ret = ask_sessiond(LTTNG_LIST_CHANNELS, (void**) channels); + copy_lttng_domain(&lsm.domain, &handle->domain); + + ret = ask_sessiond(&lsm, (void**) channels); if (ret < 0) { return ret; } @@ -535,17 +749,25 @@ int lttng_list_channels(struct lttng_domain *domain, /* * List events of a session channel. */ -int lttng_list_events(struct lttng_domain *domain, - const char *session_name, const char *channel_name, - struct lttng_event **events) +int lttng_list_events(struct lttng_handle *handle, + const char *channel_name, struct lttng_event **events) { int ret; + struct lttcomm_session_msg lsm; - copy_string(lsm.session.name, session_name, NAME_MAX); - copy_string(lsm.u.list.channel_name, channel_name, NAME_MAX); - copy_lttng_domain(domain); + if (!handle) { + return -1; + } + + lsm.cmd_type = LTTNG_LIST_EVENTS; + copy_string(lsm.session.name, handle->session_name, + sizeof(lsm.session.name)); + copy_string(lsm.u.list.channel_name, channel_name, + sizeof(lsm.u.list.channel_name)); + + copy_lttng_domain(&lsm.domain, &handle->domain); - ret = ask_sessiond(LTTNG_LIST_EVENTS, (void**) events); + ret = ask_sessiond(&lsm, (void**) events); if (ret < 0) { return ret; } @@ -553,14 +775,6 @@ int lttng_list_events(struct lttng_domain *domain, return ret / sizeof(struct lttng_event); } -/* - * Set session name for the current lsm. - */ -void lttng_set_session_name(const char *name) -{ - copy_string(lsm.session.name, name, NAME_MAX); -} - /* * lttng_set_tracing_group * @@ -579,25 +793,28 @@ int lttng_set_tracing_group(const char *name) /* * lttng_calibrate */ -int lttng_calibrate(struct lttng_domain *domain, +int lttng_calibrate(struct lttng_handle *handle, struct lttng_calibrate *calibrate) { - int ret; + struct lttcomm_session_msg lsm; - copy_lttng_domain(domain); + if (!handle) { + return -1; + } - memcpy(&lsm.u.calibrate, calibrate, sizeof(struct lttng_calibrate)); - ret = ask_sessiond(LTTNG_CALIBRATE, NULL); + lsm.cmd_type = LTTNG_CALIBRATE; + copy_lttng_domain(&lsm.domain, &handle->domain); - return ret; + memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate)); + + return ask_sessiond(&lsm, NULL); } /* - * lttng_check_session_daemon + * Check if session daemon is alive. * - * Yes, return 1 - * No, return 0 - * Error, return negative value + * Return 1 if alive or 0 if not. + * On error return -1 */ int lttng_session_daemon_alive(void) { @@ -609,21 +826,16 @@ int lttng_session_daemon_alive(void) return ret; } - /* If socket exist, we check if the daemon listens to connect. */ - ret = access(sessiond_sock_path, F_OK); - if (ret < 0) { - /* Not alive */ - return 0; + if (strlen(sessiond_sock_path) == 0) { + /* No socket path set. Weird error */ + return -1; } - ret = lttcomm_connect_unix_sock(sessiond_sock_path); + ret = try_connect_sessiond(sessiond_sock_path); if (ret < 0) { /* Not alive */ return 0; } - ret = lttcomm_close_unix_sock(ret); - if (ret < 0) - perror("lttcomm_close_unix_sock"); /* Is alive */ return 1;