]> git.lttng.org Git - lttng-tools.git/commitdiff
lttng-relayd: Add `--pid-file` parameter
authorKienan Stewart <kstewart@efficios.com>
Tue, 12 Nov 2024 20:23:06 +0000 (15:23 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 18 Dec 2024 15:55:20 +0000 (15:55 +0000)
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 <kstewart@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
doc/man/lttng-relayd.8.txt
src/bin/lttng-relayd/main.cpp

index 3d1a0071f33232cf71e6b3946a74180e83d55382..3f38b2fe0658e04020b68fff68b5a540cea1b12d 100644 (file)
@@ -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 ``<<output-directory,Output directory>>'' section above to learn
index f8fcee2a1d897726cf39f4d324e59b7013c08430..c5485dfa1e218f79f8d4684a306d54da2201b1b7 100644 (file)
@@ -44,6 +44,7 @@
 #include <common/ini-config/ini-config.hpp>
 #include <common/path.hpp>
 #include <common/pthread-lock.hpp>
+#include <common/scope-exit.hpp>
 #include <common/sessiond-comm/inet.hpp>
 #include <common/sessiond-comm/relayd.hpp>
 #include <common/sessiond-comm/sessiond-comm.hpp>
@@ -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;
        }
 }
This page took 0.035085 seconds and 4 git commands to generate.