X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Flib%2Flttng-ctl%2Flttng-ctl.c;h=c5887fe65ead21a41f637aafa3adc10d60e0eb45;hp=c62dcfa1d4b335748f5c192fe8b712120a71decb;hb=b4d8603b75b780fd3b598ba9347bdfade6132ea5;hpb=1c8d13c8a4111fcfd4cc00c335d47506ab98bb33 diff --git a/src/lib/lttng-ctl/lttng-ctl.c b/src/lib/lttng-ctl/lttng-ctl.c index c62dcfa1d..c5887fe65 100644 --- a/src/lib/lttng-ctl/lttng-ctl.c +++ b/src/lib/lttng-ctl/lttng-ctl.c @@ -74,6 +74,7 @@ static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src memcpy(dst, src, sizeof(struct lttng_domain)); break; default: + memset(dst, 0, sizeof(struct lttng_domain)); dst->type = LTTNG_DOMAIN_KERNEL; break; } @@ -137,11 +138,8 @@ static int check_tracing_group(const char *grp_name) /* Get GID of group 'tracing' */ grp_tracing = getgrnam(grp_name); - if (grp_tracing == NULL) { - /* NULL means not found also. getgrnam(3) */ - if (errno != 0) { - perror("getgrnam"); - } + if (!grp_tracing) { + /* If grp_tracing is NULL, the group does not exist. */ goto end; } @@ -227,35 +225,33 @@ static int set_session_daemon_path(void) 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, + if ((uid == 0) || in_tgroup) { + 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; + if (uid != 0) { + if (in_tgroup) { + /* Tracing group */ + ret = try_connect_sessiond(sessiond_sock_path); + if (ret >= 0) { + goto end; } + /* Global session daemon not available... */ } - } else { - /* Not in tracing group and not root, default */ - if (snprintf(sessiond_sock_path, PATH_MAX, - DEFAULT_HOME_CLIENT_UNIX_SOCK, - getenv("HOME")) < 0) { + /* ...or not in tracing group (and not root), default */ + + /* + * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small; + * With GNU C >= 2.1, snprintf returns the required size (excluding closing null) + */ + ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path), + DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME")); + if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) { return -ENOMEM; } } - +end: return 0; } @@ -468,7 +464,10 @@ int lttng_stop_tracing(const char *session_name) } /* - * Add context to event or/and channel. + * Add context to event and/or channel. + * If event_name is NULL, the context is applied to all events of the channel. + * If channel_name is NULL, a lookup of the event's channel is done. + * If both are NULL, the context is applied to all events of all channels. * * Returns the size of the returned payload data or a negative error code. */ @@ -483,6 +482,8 @@ int lttng_add_context(struct lttng_handle *handle, return -1; } + memset(&lsm, 0, sizeof(lsm)); + lsm.cmd_type = LTTNG_ADD_CONTEXT; /* Copy channel name */ @@ -517,6 +518,8 @@ int lttng_enable_event(struct lttng_handle *handle, return -1; } + memset(&lsm, 0, sizeof(lsm)); + /* If no channel name, we put the default name */ if (channel_name == NULL) { copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, @@ -556,6 +559,8 @@ int lttng_disable_event(struct lttng_handle *handle, const char *name, return -1; } + memset(&lsm, 0, sizeof(lsm)); + if (channel_name) { copy_string(lsm.u.disable.channel_name, channel_name, sizeof(lsm.u.disable.channel_name)); @@ -595,6 +600,8 @@ int lttng_enable_channel(struct lttng_handle *handle, return -1; } + memset(&lsm, 0, sizeof(lsm)); + memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan)); lsm.cmd_type = LTTNG_ENABLE_CHANNEL; @@ -620,6 +627,8 @@ int lttng_disable_channel(struct lttng_handle *handle, const char *name) return -1; } + memset(&lsm, 0, sizeof(lsm)); + lsm.cmd_type = LTTNG_DISABLE_CHANNEL; copy_string(lsm.u.disable.channel_name, name, @@ -860,11 +869,13 @@ int lttng_calibrate(struct lttng_handle *handle, /* * Set default channel attributes. - * If either or both of the arguments are null, nothing happens. + * If either or both of the arguments are null, attr content is zeroe'd. */ void lttng_channel_set_default_attr(struct lttng_domain *domain, struct lttng_channel_attr *attr) { + memset(attr, 0, sizeof(struct lttng_channel_attr)); + /* Safety check */ if (attr == NULL || domain == NULL) { return; @@ -895,8 +906,7 @@ void lttng_channel_set_default_attr(struct lttng_domain *domain, attr->output = DEFAULT_UST_CHANNEL_OUTPUT; break; default: - /* Default behavior */ - memset(attr, 0, sizeof(struct lttng_channel_attr)); + /* Default behavior: leave set to 0. */ break; } }