journalctl,elsewhere: make sure --file=foo fails with sane error msg if foo is not readable

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.
This commit is contained in:
Lennart Poettering 2020-05-12 23:36:27 +02:00 committed by Zbigniew Jędrzejewski-Szmek
parent 7f6b827f36
commit 544e146b0e
7 changed files with 8 additions and 8 deletions

View File

@ -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;

View File

@ -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)

View File

@ -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)

View File

@ -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;

View File

@ -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;

View File

@ -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");
}

View File

@ -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",