From 544e146b0e2f6227e28476e36becd1019b14ef70 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 12 May 2020 23:36:27 +0200 Subject: [PATCH] journalctl,elsewhere: make sure --file=foo fails with sane error msg if foo is not readable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It annoyed me for quite a while that running "journalctl --file=…" on a file that is not readable failed with a "File not found" error instead of a permission error. Let's fix that. We make this work by using the GLOB_NOCHECK flag for glob() which means that files are not accessible will be returned in the array as they are instead of being filtered away. This then means that our later attemps to open the files will fail cleanly with a good error message. --- src/basic/glob-util.c | 4 ++-- src/basic/glob-util.h | 2 +- src/basic/path-util.c | 2 +- src/coredump/coredumpctl.c | 2 +- src/journal-remote/journal-upload.c | 2 +- src/journal/journalctl.c | 2 +- src/sysctl/sysctl.c | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/basic/glob-util.c b/src/basic/glob-util.c index e3aa6c2e15..1e7e301e09 100644 --- a/src/basic/glob-util.c +++ b/src/basic/glob-util.c @@ -61,11 +61,11 @@ int glob_exists(const char *path) { return true; } -int glob_extend(char ***strv, const char *path) { +int glob_extend(char ***strv, const char *path, int flags) { _cleanup_globfree_ glob_t g = {}; int k; - k = safe_glob(path, GLOB_NOSORT|GLOB_BRACE, &g); + k = safe_glob(path, GLOB_NOSORT|GLOB_BRACE|flags, &g); if (k < 0) return k; diff --git a/src/basic/glob-util.h b/src/basic/glob-util.h index 8b1bb02be1..3d5f543508 100644 --- a/src/basic/glob-util.h +++ b/src/basic/glob-util.h @@ -11,7 +11,7 @@ int safe_glob(const char *path, int flags, glob_t *pglob); int glob_exists(const char *path); -int glob_extend(char ***strv, const char *path); +int glob_extend(char ***strv, const char *path, int flags); #define _cleanup_globfree_ _cleanup_(globfree) diff --git a/src/basic/path-util.c b/src/basic/path-util.c index ba13de01ff..986dfe94a4 100644 --- a/src/basic/path-util.c +++ b/src/basic/path-util.c @@ -1051,7 +1051,7 @@ int systemd_installation_has_version(const char *root, unsigned minimal_version) if (!path) return -ENOMEM; - r = glob_extend(&names, path); + r = glob_extend(&names, path, 0); if (r == -ENOENT) continue; if (r < 0) diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index 60a02b770b..ed4d06e986 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -237,7 +237,7 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_FILE: - r = glob_extend(&arg_file, optarg); + r = glob_extend(&arg_file, optarg, GLOB_NOCHECK); if (r < 0) return log_error_errno(r, "Failed to add paths: %m"); break; diff --git a/src/journal-remote/journal-upload.c b/src/journal-remote/journal-upload.c index 2f9585df56..bf656ac670 100644 --- a/src/journal-remote/journal-upload.c +++ b/src/journal-remote/journal-upload.c @@ -720,7 +720,7 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_FILE: - r = glob_extend(&arg_file, optarg); + r = glob_extend(&arg_file, optarg, GLOB_NOCHECK); if (r < 0) return log_error_errno(r, "Failed to add paths: %m"); break; diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 01d75b0e24..a082995498 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -705,7 +705,7 @@ static int parse_argv(int argc, char *argv[]) { * STDIN. To avoid confusion we hence don't document this feature. */ arg_file_stdin = true; else { - r = glob_extend(&arg_file, optarg); + r = glob_extend(&arg_file, optarg, GLOB_NOCHECK); if (r < 0) return log_error_errno(r, "Failed to add paths: %m"); } diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index 3b3305d7f6..5274cd24b3 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -138,7 +138,7 @@ static int apply_all(OrderedHashmap *sysctl_options) { if (!pattern) return log_oom(); - k = glob_extend(&paths, pattern); + k = glob_extend(&paths, pattern, GLOB_NOCHECK); if (k < 0) { if (option->ignore_failure || ERRNO_IS_PRIVILEGE(k)) log_debug_errno(k, "Failed to resolve glob '%s', ignoring: %m",