X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fbin%2Flttng-relayd%2Fmain.c;h=5f6f9cfd8ed78a613972c5b3ed9b999c63c90ecc;hb=6151a90fe7fa3dea52c57771df9083e56de7a60b;hp=66273107c354c5b541b372cb8725a6ad732c5ea4;hpb=de91f48a81b1212697853f57efb6532ad8cd490f;p=lttng-tools.git diff --git a/src/bin/lttng-relayd/main.c b/src/bin/lttng-relayd/main.c index 66273107c..5f6f9cfd8 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"); @@ -1041,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; @@ -1050,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; } @@ -1072,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) { @@ -1340,7 +1240,7 @@ end: */ static int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr, - struct relay_command *cmd) + struct relay_command *cmd, struct lttng_ht *streams_ht) { int ret; struct lttcomm_relayd_version reply, msg; @@ -1362,19 +1262,22 @@ int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr, goto end; } - /* - * For now, we just ignore the received version but after 2.1 stable - * release, a check must be done to see if we either adapt to the other - * side version (which MUST be lower than us) or keep the latest data - * structure considering that the other side will adapt. - */ - ret = sscanf(VERSION, "%10u.%10u", &reply.major, &reply.minor); if (ret < 2) { ERR("Error in scanning version"); ret = -1; goto end; } + + /* Major versions must be the same */ + if (reply.major != be32toh(msg.major)) { + DBG("Incompatible major versions (%u vs %u), deleting session", + reply.major, be32toh(msg.major)); + relay_delete_session(cmd, streams_ht); + ret = 0; + goto end; + } + reply.major = htobe32(reply.major); reply.minor = htobe32(reply.minor); ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, @@ -1382,8 +1285,19 @@ int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr, if (ret < 0) { ERR("Relay sending version"); } - DBG("Version check done (%u.%u)", be32toh(reply.major), - be32toh(reply.minor)); + +#if 0 + cmd->session->major = reply.major; + /* We adapt to the lowest compatible version */ + if (reply.minor <= be32toh(msg.minor)) { + cmd->session->minor = reply.minor; + } else { + cmd->session->minor = be32toh(msg.minor); + } + + DBG("Version check done using protocol %u.%u", cmd->session->major, + cmd->session->minor); +#endif end: return ret; @@ -1693,7 +1607,7 @@ int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr, ret = relay_recv_metadata(recv_hdr, cmd, streams_ht); break; case RELAYD_VERSION: - ret = relay_send_version(recv_hdr, cmd); + ret = relay_send_version(recv_hdr, cmd, streams_ht); break; case RELAYD_CLOSE_STREAM: ret = relay_close_stream(recv_hdr, cmd, streams_ht); @@ -2216,6 +2130,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);