From dcffe9462d11f9de5b441b801da5b2b7ae42c79a Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Mon, 15 Apr 2024 15:15:28 -0400 Subject: [PATCH] lttng: Indicate file path and error reason when open_config fails MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Observed issue ============== In a test environment where the configuration file had been chowned to no longer be writeable by the current user, `lttng` commands would work but return a non-zero exit code and print `Error: Unable to create config file`. This doesn't give the user much information to work with to address the issue. Solution ======== The `PWARN` macro is used to print the file_path and reason when the call to `fopen` fails. This is done since the `file_path` percolated up to where the existing error message is may be null for multiple reasons (eg. allocation failure). Known drawbacks =============== The output of `PWARN` is inconsistent with the output used for most of the `lttng` client. Eg. ``` $ lttng create Spawning a session daemon Session auto-20240415-151450 created. Traces will be output to /home/lttng-traces/auto-20240415-151450 PWARN - 15:14:50.420628389 [1184515/1184515]: fopen '/home/.lttngrc': Permission denied (in open_config() at conf.cpp:57) Error: Unable to create config file ``` Change-Id: I5a9253fb02f3a712e7c5c2567311920dc33d36c7 Signed-off-by: Kienan Stewart Signed-off-by: Jérémie Galarneau --- src/bin/lttng/commands/create.cpp | 3 ++- src/bin/lttng/conf.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bin/lttng/commands/create.cpp b/src/bin/lttng/commands/create.cpp index dce13866f..e1756c3d3 100644 --- a/src/bin/lttng/commands/create.cpp +++ b/src/bin/lttng/commands/create.cpp @@ -479,7 +479,8 @@ static int create_session(const char *session_name) /* Init lttng session config */ ret = config_init(created_session_name); if (ret < 0) { - ret = CMD_ERROR; + MSG("Unable to initialize configuration for created session: future commands will require the target session name explicitly"); + ret = CMD_WARNING; goto error; } diff --git a/src/bin/lttng/conf.cpp b/src/bin/lttng/conf.cpp index 65be35a15..81aa65438 100644 --- a/src/bin/lttng/conf.cpp +++ b/src/bin/lttng/conf.cpp @@ -54,6 +54,7 @@ static FILE *open_config(const char *path, const char *mode) fp = fopen(file_path, mode); if (fp == nullptr) { + PWARN("Failed to open configuration file '%s'", file_path); goto error; } @@ -74,7 +75,6 @@ static int create_config_file(const char *path) fp = open_config(path, "w+"); if (fp == nullptr) { - ERR("Unable to create config file"); ret = -1; goto error; } -- 2.34.1