X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng-relayd%2Fmain.c;h=082eb7975efd0adb1c41bfbfee65c621dd34abeb;hb=d620da680b1e5fc67bdacaf6150ca2201cad37b7;hp=6d800f50b3c0db40de5997ead9625fee8cf8938c;hpb=4169f5ad171ab88ac181ec4080f46af379bc7676;p=lttng-tools.git diff --git a/src/bin/lttng-relayd/main.c b/src/bin/lttng-relayd/main.c index 6d800f50b..082eb7975 100644 --- a/src/bin/lttng-relayd/main.c +++ b/src/bin/lttng-relayd/main.c @@ -685,53 +685,6 @@ error: return NULL; } -/* - * Return the realpath(3) of the path even if the last directory token does not - * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the - * /tmp/test1 does, the real path is returned. In normal time, realpath(3) - * fails if the end point directory does not exist. - */ -static -char *expand_full_path(const char *path) -{ - const char *end_path = path; - char *next, *cut_path, *expanded_path, *respath; - - /* Find last token delimited by '/' */ - while ((next = strpbrk(end_path + 1, "/"))) { - end_path = next; - } - - /* Cut last token from original path */ - cut_path = strndup(path, end_path - path); - - expanded_path = malloc(PATH_MAX); - if (expanded_path == NULL) { - respath = NULL; - goto end; - } - - respath = realpath(cut_path, expanded_path); - if (respath == NULL) { - switch (errno) { - case ENOENT: - ERR("%s: No such file or directory", cut_path); - break; - default: - PERROR("realpath"); - break; - } - free(expanded_path); - } else { - /* Add end part to expanded path */ - strcat(respath, end_path); - } -end: - free(cut_path); - return respath; -} - - /* * config_get_default_path * @@ -743,68 +696,6 @@ char *config_get_default_path(void) return getenv("HOME"); } -/* - * Create recursively directory using the FULL path. - */ -static -int mkdir_recursive(char *path, mode_t mode) -{ - char *p, tmp[PATH_MAX]; - struct stat statbuf; - size_t len; - int ret; - - ret = snprintf(tmp, sizeof(tmp), "%s", path); - if (ret < 0) { - PERROR("snprintf mkdir"); - goto error; - } - - len = ret; - if (tmp[len - 1] == '/') { - tmp[len - 1] = 0; - } - - for (p = tmp + 1; *p; p++) { - if (*p == '/') { - *p = 0; - if (tmp[strlen(tmp) - 1] == '.' && - tmp[strlen(tmp) - 2] == '.' && - tmp[strlen(tmp) - 3] == '/') { - ERR("Using '/../' is not permitted in the trace path (%s)", - tmp); - ret = -1; - goto error; - } - ret = stat(tmp, &statbuf); - if (ret < 0) { - ret = mkdir(tmp, mode); - if (ret < 0) { - if (errno != EEXIST) { - PERROR("mkdir recursive"); - ret = -errno; - goto error; - } - } - } - *p = '/'; - } - } - - ret = mkdir(tmp, mode); - if (ret < 0) { - if (errno != EEXIST) { - PERROR("mkdir recursive last piece"); - ret = -errno; - } else { - ret = 0; - } - } - -error: - return ret; -} - static char *create_output_path_auto(char *path_name) { @@ -842,7 +733,11 @@ char *create_output_path_noauto(char *path_name) char *traces_path = NULL; char *full_path; - full_path = expand_full_path(opt_output_path); + full_path = utils_expand_path(opt_output_path); + if (!full_path) { + goto exit; + } + ret = asprintf(&traces_path, "%s/%s", full_path, path_name); if (ret < 0) { PERROR("asprintf trace dir name"); @@ -866,6 +761,34 @@ char *create_output_path(char *path_name) } } +/* + * Get stream from stream id. + * Need to be called with RCU read-side lock held. + */ +static +struct relay_stream *relay_stream_from_stream_id(uint64_t stream_id, + struct lttng_ht *streams_ht) +{ + struct lttng_ht_node_ulong *node; + struct lttng_ht_iter iter; + struct relay_stream *ret; + + lttng_ht_lookup(streams_ht, + (void *)((unsigned long) stream_id), + &iter); + node = lttng_ht_iter_get_node_ulong(&iter); + if (node == NULL) { + DBG("Relay stream %" PRIu64 " not found", stream_id); + ret = NULL; + goto end; + } + + ret = caa_container_of(node, struct relay_stream, stream_n); + +end: + return ret; +} + static void deferred_free_stream(struct rcu_head *head) { @@ -1013,7 +936,7 @@ int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr, ret = -1; goto end; } - ret = mkdir_recursive(root_path, S_IRWXU | S_IRWXG); + ret = utils_mkdir_recursive(root_path, S_IRWXU | S_IRWXG); if (ret < 0) { ERR("relay creating output directory"); goto end; @@ -1022,6 +945,7 @@ int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr, ret = asprintf(&path, "%s/%s", root_path, stream_info.channel_name); if (ret < 0) { PERROR("asprintf stream path"); + path = NULL; goto end; } @@ -1044,13 +968,17 @@ int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr, end: free(path); free(root_path); + + reply.handle = htobe64(stream->stream_handle); /* send the session id to the client or a negative return code on error */ if (ret < 0) { reply.ret_code = htobe32(LTTNG_ERR_UNK); + /* stream was not properly added to the ht, so free it */ + free(stream); } else { reply.ret_code = htobe32(LTTNG_OK); } - reply.handle = htobe64(stream->stream_handle); + send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(struct lttcomm_relayd_status_stream), 0); if (send_ret < 0) { @@ -1075,7 +1003,6 @@ int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr, struct lttcomm_relayd_generic_reply reply; struct relay_stream *stream; int ret, send_ret; - struct lttng_ht_node_ulong *node; struct lttng_ht_iter iter; DBG("Close stream received"); @@ -1100,17 +1027,8 @@ int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr, } rcu_read_lock(); - lttng_ht_lookup(streams_ht, - (void *)((unsigned long) be64toh(stream_info.stream_id)), - &iter); - node = lttng_ht_iter_get_node_ulong(&iter); - if (node == NULL) { - DBG("Relay stream %" PRIu64 " not found", be64toh(stream_info.stream_id)); - ret = -1; - goto end_unlock; - } - - stream = caa_container_of(node, struct relay_stream, stream_n); + stream = relay_stream_from_stream_id(be64toh(stream_info.stream_id), + streams_ht); if (!stream) { ret = -1; goto end_unlock; @@ -1126,6 +1044,7 @@ int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr, if (delret < 0) { PERROR("close stream"); } + iter.iter.node = &stream->stream_n.node; delret = lttng_ht_del(streams_ht, &iter); assert(!delret); call_rcu(&stream->rcu_node, @@ -1196,34 +1115,6 @@ int relay_start(struct lttcomm_relayd_hdr *recv_hdr, return ret; } -/* - * Get stream from stream id. - * Need to be called with RCU read-side lock held. - */ -static -struct relay_stream *relay_stream_from_stream_id(uint64_t stream_id, - struct lttng_ht *streams_ht) -{ - struct lttng_ht_node_ulong *node; - struct lttng_ht_iter iter; - struct relay_stream *ret; - - lttng_ht_lookup(streams_ht, - (void *)((unsigned long) stream_id), - &iter); - node = lttng_ht_iter_get_node_ulong(&iter); - if (node == NULL) { - DBG("Relay stream %" PRIu64 " not found", stream_id); - ret = NULL; - goto end; - } - - ret = caa_container_of(node, struct relay_stream, stream_n); - -end: - return ret; -} - /* * Append padding to the file pointed by the file descriptor fd. */ @@ -1410,8 +1301,6 @@ int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr, struct lttcomm_relayd_generic_reply reply; struct relay_stream *stream; int ret; - struct lttng_ht_node_ulong *node; - struct lttng_ht_iter iter; uint64_t last_net_seq_num, stream_id; DBG("Data pending command received"); @@ -1439,17 +1328,12 @@ int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr, last_net_seq_num = be64toh(msg.last_net_seq_num); rcu_read_lock(); - lttng_ht_lookup(streams_ht, (void *)((unsigned long) stream_id), &iter); - node = lttng_ht_iter_get_node_ulong(&iter); - if (node == NULL) { - DBG("Relay stream %" PRIu64 " not found", stream_id); + stream = relay_stream_from_stream_id(stream_id, streams_ht); + if (stream == NULL) { ret = -1; goto end_unlock; } - stream = caa_container_of(node, struct relay_stream, stream_n); - assert(stream); - DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64 " and last_seq %" PRIu64, stream_id, stream->prev_seq, last_net_seq_num); @@ -2232,6 +2116,15 @@ int main(int argc, char **argv) goto exit; } + /* Try to create directory if -o, --output is specified. */ + if (opt_output_path) { + ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG); + if (ret < 0) { + ERR("Unable to create %s", opt_output_path); + goto exit; + } + } + /* Daemonize */ if (opt_daemon) { ret = daemon(0, 0);