From: Kienan Stewart Date: Fri, 25 Oct 2024 20:18:53 +0000 (+0000) Subject: lttng-relayd: Add option to delegate port choice to OS X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=101fd95a67a9ab748f188cfbd6fa8e26de499168;p=lttng-tools.git lttng-relayd: Add option to delegate port choice to OS Change-Id: I8fdbfac5511f404eb6868c0f5b9de7bbb1ad7623 Signed-off-by: Kienan Stewart Signed-off-by: Jérémie Galarneau --- diff --git a/doc/man/lttng-relayd.8.txt b/doc/man/lttng-relayd.8.txt index 4a51e15ba..3d1a0071f 100644 --- a/doc/man/lttng-relayd.8.txt +++ b/doc/man/lttng-relayd.8.txt @@ -18,6 +18,7 @@ SYNOPSIS [option:--live-port='URL'] [option:--output='DIR'] [option:--group='GROUP'] [option:--verbose]... [option:--working-directory='DIR'] [option:--group-output-by-host | option:--group-output-by-session] [option:--disallow-clear] + [option:--dynamic-port-allocation] DESCRIPTION @@ -227,6 +228,13 @@ option:-v, option:--verbose:: + Specify this option up to three times to get more levels of verbosity. +option:--dynamic-port-allocation:: + When specific any of the control, data, and live ports not specifically + given, or which are given a value of '0' will have the port choice delegated + to the operating system. ++ +lttng-relayd will write `control.port`, `data.port`, and `live.port` files to +its run-time directory. Output ~~~~~~ diff --git a/src/bin/lttng-relayd/live.cpp b/src/bin/lttng-relayd/live.cpp index b262580f0..48d014956 100644 --- a/src/bin/lttng-relayd/live.cpp +++ b/src/bin/lttng-relayd/live.cpp @@ -796,6 +796,7 @@ static struct lttcomm_sock *init_socket(struct lttng_uri *uri, const char *name) goto error; } + DBG("Bound %s socket fd %d to port %d", name, sock->fd, ntohs(sock->sockaddr.addr.sin.sin_port)); ret = sock->ops->listen(sock, -1); if (ret < 0) { goto error; @@ -2955,7 +2956,6 @@ int relayd_live_create(struct lttng_uri *uri) { int ret = 0, retval = 0; void *status; - int is_root; if (!uri) { retval = -1; @@ -2963,17 +2963,6 @@ int relayd_live_create(struct lttng_uri *uri) } live_uri = uri; - /* Check if daemon is UID = 0 */ - is_root = !getuid(); - - if (!is_root) { - if (live_uri->port < 1024) { - ERR("Need to be root to use ports < 1024"); - retval = -1; - goto exit_init_data; - } - } - /* Setup the thread apps communication pipe. */ if (create_conn_pipe()) { retval = -1; diff --git a/src/bin/lttng-relayd/main.cpp b/src/bin/lttng-relayd/main.cpp index ed5ec9d8f..0045c5b5f 100644 --- a/src/bin/lttng-relayd/main.cpp +++ b/src/bin/lttng-relayd/main.cpp @@ -97,7 +97,7 @@ enum relay_connection_status { /* command line options */ char *opt_output_path, *opt_working_directory; -static int opt_daemon, opt_background, opt_print_version, opt_allow_clear = 1; +static int opt_daemon, opt_background, opt_print_version, opt_allow_clear = 1, opt_dynamic_port_allocation = 0; enum relay_group_output_by opt_group_output_by = RELAYD_GROUP_OUTPUT_BY_UNKNOWN; /* Argument variables */ @@ -263,6 +263,7 @@ static struct option long_options[] = { 'p', }, { "disallow-clear", 0, nullptr, 'x' }, + { "dynamic-port-allocation", 0, nullptr, '\0' }, { nullptr, 0, @@ -320,6 +321,8 @@ static int set_option(int opt, const char *arg, const char *optname) goto end; } lttng_opt_fd_pool_size = (unsigned int) v; + } else if (!strcmp(optname, "dynamic-port-allocation")) { + opt_dynamic_port_allocation = 1; } else { fprintf(stderr, "unknown option %s", optname); if (arg) { @@ -337,7 +340,7 @@ static int set_option(int opt, const char *arg, const char *optname) ERR("Invalid control URI specified"); goto end; } - if (control_uri->port == 0) { + if (control_uri->port == 0 && !opt_dynamic_port_allocation) { control_uri->port = DEFAULT_NETWORK_CONTROL_PORT; } } @@ -352,7 +355,7 @@ static int set_option(int opt, const char *arg, const char *optname) ERR("Invalid data URI specified"); goto end; } - if (data_uri->port == 0) { + if (data_uri->port == 0 && !opt_dynamic_port_allocation) { data_uri->port = DEFAULT_NETWORK_DATA_PORT; } } @@ -367,7 +370,7 @@ static int set_option(int opt, const char *arg, const char *optname) ERR("Invalid live URI specified"); goto end; } - if (live_uri->port == 0) { + if (live_uri->port == 0 && !opt_dynamic_port_allocation) { live_uri->port = DEFAULT_NETWORK_VIEWER_PORT; } } @@ -663,7 +666,7 @@ static int set_options(int argc, char **argv) if (control_uri == nullptr) { ret = asprintf(&default_address, "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS ":%d", - DEFAULT_NETWORK_CONTROL_PORT); + opt_dynamic_port_allocation ? 0 : DEFAULT_NETWORK_CONTROL_PORT); if (ret < 0) { PERROR("asprintf default data address"); retval = -1; @@ -681,7 +684,7 @@ static int set_options(int argc, char **argv) if (data_uri == nullptr) { ret = asprintf(&default_address, "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS ":%d", - DEFAULT_NETWORK_DATA_PORT); + (opt_dynamic_port_allocation) ? 0 : DEFAULT_NETWORK_DATA_PORT); if (ret < 0) { PERROR("asprintf default data address"); retval = -1; @@ -699,7 +702,7 @@ static int set_options(int argc, char **argv) if (live_uri == nullptr) { ret = asprintf(&default_address, "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS ":%d", - DEFAULT_NETWORK_VIEWER_PORT); + (opt_dynamic_port_allocation) ? 0 : DEFAULT_NETWORK_VIEWER_PORT); if (ret < 0) { PERROR("asprintf default viewer control address"); retval = -1; @@ -1034,14 +1037,15 @@ static struct lttcomm_sock *relay_socket_create(struct lttng_uri *uri, const cha PERROR("Failed to open \"%s\" relay socket", formated_name ?: "Unknown"); goto error; } - DBG("Listening on %s socket %d", name, sock->fd); + DBG("Listening on %s socket %d", name, sock->fd); ret = sock->ops->bind(sock); if (ret < 0) { PERROR("Failed to bind socket"); goto error; } + DBG("Bound %s socket fd %d to port %d", name, sock->fd, ntohs(sock->sockaddr.addr.sin.sin_port)); ret = sock->ops->listen(sock, -1); if (ret < 0) { goto error; diff --git a/src/common/sessiond-comm/inet.cpp b/src/common/sessiond-comm/inet.cpp index 3471f6c0b..1fbc01fb9 100644 --- a/src/common/sessiond-comm/inet.cpp +++ b/src/common/sessiond-comm/inet.cpp @@ -89,8 +89,19 @@ error: int lttcomm_bind_inet_sock(struct lttcomm_sock *sock) { struct sockaddr_in sockaddr = sock->sockaddr.addr.sin; + int ret; + + ret = bind(sock->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)); + if (ret) { + return ret; + } - return bind(sock->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)); + if (sockaddr.sin_port == 0) { + socklen_t socklen = sizeof(sock->sockaddr.addr.sin); + ret = getsockname(sock->fd, (struct sockaddr *) &sock->sockaddr.addr.sin, &socklen); + } + + return ret; } static int connect_no_timeout(struct lttcomm_sock *sock) diff --git a/src/common/sessiond-comm/inet6.cpp b/src/common/sessiond-comm/inet6.cpp index 0d1b6f15b..4d80e92f0 100644 --- a/src/common/sessiond-comm/inet6.cpp +++ b/src/common/sessiond-comm/inet6.cpp @@ -86,7 +86,19 @@ error: int lttcomm_bind_inet6_sock(struct lttcomm_sock *sock) { struct sockaddr_in6 sockaddr = sock->sockaddr.addr.sin6; - return bind(sock->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)); + int ret; + + ret = bind(sock->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)); + if (ret) { + return ret; + } + + if (sockaddr.sin6_port == 0) { + socklen_t socklen = sizeof(sock->sockaddr.addr.sin); + ret = getsockname(sock->fd, (struct sockaddr *) &sock->sockaddr.addr.sin, &socklen); + } + + return ret; } static int connect_no_timeout(struct lttcomm_sock *sock) diff --git a/src/common/sessiond-comm/sessiond-comm.cpp b/src/common/sessiond-comm/sessiond-comm.cpp index c5236a432..c7d03217f 100644 --- a/src/common/sessiond-comm/sessiond-comm.cpp +++ b/src/common/sessiond-comm/sessiond-comm.cpp @@ -245,7 +245,7 @@ int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr *sockaddr, const char *ip LTTNG_ASSERT(sockaddr); LTTNG_ASSERT(ip); - LTTNG_ASSERT(port > 0 && port <= 65535); + LTTNG_ASSERT(port <= 65535); memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr)); @@ -275,7 +275,7 @@ int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr *sockaddr, LTTNG_ASSERT(sockaddr); LTTNG_ASSERT(ip); - LTTNG_ASSERT(port > 0 && port <= 65535); + LTTNG_ASSERT(port <= 65535); memset(sockaddr, 0, sizeof(struct lttcomm_sockaddr)); diff --git a/src/common/uri.cpp b/src/common/uri.cpp index af78bd355..e1b24432a 100644 --- a/src/common/uri.cpp +++ b/src/common/uri.cpp @@ -451,7 +451,7 @@ ssize_t uri_parse(const char *str_uri, struct lttng_uri **uris) } port = atoi(port_f); - if (port > 0xffff || port <= 0x0) { + if (port > 0xffff || port < 0x0) { ERR("Invalid port number %d", port); free(port_f); goto free_error;