| 1 | /* |
| 2 | * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
| 4 | * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | |
| 12 | #include "cmd-2-1.hpp" |
| 13 | #include "utils.hpp" |
| 14 | |
| 15 | #include <common/common.hpp> |
| 16 | #include <common/compat/string.hpp> |
| 17 | #include <common/sessiond-comm/relayd.hpp> |
| 18 | |
| 19 | #include <lttng/constant.h> |
| 20 | |
| 21 | /* |
| 22 | * cmd_recv_stream_2_1 allocates path_name and channel_name. |
| 23 | */ |
| 24 | int cmd_recv_stream_2_1(const struct lttng_buffer_view *payload, |
| 25 | char **ret_path_name, |
| 26 | char **ret_channel_name) |
| 27 | { |
| 28 | int ret; |
| 29 | struct lttcomm_relayd_add_stream stream_info; |
| 30 | char *path_name = nullptr; |
| 31 | char *channel_name = nullptr; |
| 32 | size_t len; |
| 33 | |
| 34 | if (payload->size < sizeof(stream_info)) { |
| 35 | ERR("Unexpected payload size in \"cmd_recv_stream_2_1\": expected >= %zu bytes, got %zu bytes", |
| 36 | sizeof(stream_info), |
| 37 | payload->size); |
| 38 | ret = -1; |
| 39 | goto error; |
| 40 | } |
| 41 | memcpy(&stream_info, payload->data, sizeof(stream_info)); |
| 42 | |
| 43 | len = lttng_strnlen(stream_info.pathname, sizeof(stream_info.pathname)); |
| 44 | /* Ensure that NULL-terminated and fits in local filename length. */ |
| 45 | if (len == sizeof(stream_info.pathname) || len >= LTTNG_NAME_MAX) { |
| 46 | ret = -ENAMETOOLONG; |
| 47 | ERR("Path name too long"); |
| 48 | goto error; |
| 49 | } |
| 50 | path_name = strdup(stream_info.pathname); |
| 51 | if (!path_name) { |
| 52 | PERROR("Path name allocation"); |
| 53 | ret = -ENOMEM; |
| 54 | goto error; |
| 55 | } |
| 56 | len = lttng_strnlen(stream_info.channel_name, sizeof(stream_info.channel_name)); |
| 57 | if (len == sizeof(stream_info.channel_name) || len >= DEFAULT_STREAM_NAME_LEN) { |
| 58 | ret = -ENAMETOOLONG; |
| 59 | ERR("Channel name too long"); |
| 60 | goto error; |
| 61 | } |
| 62 | channel_name = strdup(stream_info.channel_name); |
| 63 | if (!channel_name) { |
| 64 | ret = -errno; |
| 65 | PERROR("Channel name allocation"); |
| 66 | goto error; |
| 67 | } |
| 68 | |
| 69 | *ret_path_name = path_name; |
| 70 | *ret_channel_name = channel_name; |
| 71 | return 0; |
| 72 | error: |
| 73 | free(path_name); |
| 74 | free(channel_name); |
| 75 | return ret; |
| 76 | } |