From: Kienan Stewart Date: Tue, 12 Nov 2024 20:23:06 +0000 (-0500) Subject: lttng-relayd: Add `--pid-file` parameter X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=58b8d49ba10e6f2438ec6ef6662c510e9a94451b;p=lttng-tools.git lttng-relayd: Add `--pid-file` parameter Intended to be used a mechanism to get the PID of the process when starting with `-b` or `-d`. `lttng-sessiond` writes to a well-known path inside `$LTTNG_HOME` instead. The exit for the lttng-relayd `main()` function is changed from `exit()` to `return`. When using `exit()` or `std::exit()`, the end-of-scope objects (e.g. from `lttng::make_scope_exit()` do not fire. Change-Id: I1ce6fd316356ac251cb3a0c0039c565f1ca1210f 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 3d1a0071f..3f38b2fe0 100644 --- a/doc/man/lttng-relayd.8.txt +++ b/doc/man/lttng-relayd.8.txt @@ -18,7 +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] + [option:--dynamic-port-allocation] [option:--pid-file='FILE'] DESCRIPTION @@ -236,6 +236,11 @@ option:--dynamic-port-allocation:: lttng-relayd will write `control.port`, `data.port`, and `live.port` files to its run-time directory. +option:-P, option:--pid-file:: + Write the PID of the lttng-relayd process to the given file. ++ +This file is not otherwise used by lttng-relayd and is removed at shutdown. + Output ~~~~~~ See the ``<>'' section above to learn diff --git a/src/bin/lttng-relayd/main.cpp b/src/bin/lttng-relayd/main.cpp index f8fcee2a1..c5485dfa1 100644 --- a/src/bin/lttng-relayd/main.cpp +++ b/src/bin/lttng-relayd/main.cpp @@ -44,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -96,8 +97,9 @@ 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, opt_dynamic_port_allocation = 0; +char *opt_output_path, *opt_working_directory, *opt_pid_file = nullptr; +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 */ @@ -212,6 +214,12 @@ static struct option long_options[] = { nullptr, 'b', }, + { + "pid-file", + 1, + nullptr, + 'P' + }, { "group", 1, @@ -381,6 +389,15 @@ static int set_option(int opt, const char *arg, const char *optname) case 'b': opt_background = 1; break; + case 'P': + opt_pid_file = utils_partial_realpath(arg); + if (opt_pid_file == nullptr) { + ret = -1; + ERR_FMT("Failed to determine partial realpath for PID file at path '{}'", + arg); + goto end; + } + break; case 'g': if (lttng_is_setuid_setgid()) { WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", @@ -4353,6 +4370,14 @@ int main(int argc, char **argv) int ret = 0, retval = 0; void *status; char *unlinked_file_directory_path = nullptr, *output_path = nullptr; + auto delete_pid_file = lttng::make_scope_exit([]() noexcept { + if (opt_pid_file != nullptr) { + if (unlink(opt_pid_file)) { + PERROR_FMT("Failed to delete PID file at path '{}'", opt_pid_file); + } + } + }); + delete_pid_file.disarm(); /* Parse environment variables */ ret = parse_env_options(); @@ -4425,6 +4450,15 @@ int main(int argc, char **argv) } } + if (opt_pid_file != nullptr) { + ret = utils_create_pid_file(getpid(), opt_pid_file); + if (ret) { + ERR_FMT("Failed to create PID file at path '{}'", opt_pid_file); + goto exit_options; + } + delete_pid_file.arm(); + } + sessiond_trace_chunk_registry = sessiond_trace_chunk_registry_create(); if (!sessiond_trace_chunk_registry) { ERR("Failed to initialize session daemon trace chunk registry"); @@ -4639,8 +4673,8 @@ exit_options: } if (!retval) { - exit(EXIT_SUCCESS); + return EXIT_SUCCESS; } else { - exit(EXIT_FAILURE); + return EXIT_FAILURE; } }