William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 1 | From bca73ff2fbff2dc311040a87a4f536f89af07ad6 Mon Sep 17 00:00:00 2001 |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 2 | From: Chen Qi <Qi.Chen@windriver.com> |
| 3 | Date: Mon, 25 Feb 2019 14:56:21 +0800 |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 4 | Subject: [PATCH] don't fail if GLOB_BRACE and GLOB_ALTDIRFUNC is not defined |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 5 | |
| 6 | If the standard library doesn't provide brace |
| 7 | expansion users just won't get it. |
| 8 | |
| 9 | Dont use GNU GLOB extentions on non-glibc systems |
| 10 | |
| 11 | Conditionalize use of GLOB_ALTDIRFUNC |
| 12 | |
| 13 | Upstream-Status: Inappropriate [musl specific] |
| 14 | |
| 15 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 16 | Signed-off-by: Chen Qi <Qi.Chen@windriver.com> |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 17 | [rebased for systemd 243] |
| 18 | Signed-off-by: Scott Murray <scott.murray@konsulko.com> |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 19 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 20 | --- |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 21 | src/basic/glob-util.c | 12 ++++++++++++ |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 22 | src/test/test-glob-util.c | 16 ++++++++++++++++ |
| 23 | src/tmpfiles/tmpfiles.c | 10 ++++++++++ |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 24 | 3 files changed, 38 insertions(+) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 25 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 26 | diff --git a/src/basic/glob-util.c b/src/basic/glob-util.c |
| 27 | index bc0278e57f..c973f82e54 100644 |
| 28 | --- a/src/basic/glob-util.c |
| 29 | +++ b/src/basic/glob-util.c |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 30 | @@ -12,6 +12,12 @@ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 31 | #include "path-util.h" |
| 32 | #include "strv.h" |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 33 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 34 | +/* Don't fail if the standard library |
| 35 | + * doesn't provide brace expansion */ |
| 36 | +#ifndef GLOB_BRACE |
| 37 | +#define GLOB_BRACE 0 |
| 38 | +#endif |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 39 | + |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 40 | static void closedir_wrapper(void* v) { |
| 41 | (void) closedir(v); |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 42 | } |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 43 | @@ -19,6 +25,7 @@ static void closedir_wrapper(void* v) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 44 | int safe_glob(const char *path, int flags, glob_t *pglob) { |
| 45 | int k; |
| 46 | |
| 47 | +#ifdef GLOB_ALTDIRFUNC |
| 48 | /* We want to set GLOB_ALTDIRFUNC ourselves, don't allow it to be set. */ |
| 49 | assert(!(flags & GLOB_ALTDIRFUNC)); |
| 50 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 51 | @@ -32,9 +39,14 @@ int safe_glob(const char *path, int flags, glob_t *pglob) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 52 | pglob->gl_lstat = lstat; |
| 53 | if (!pglob->gl_stat) |
| 54 | pglob->gl_stat = stat; |
| 55 | +#endif |
| 56 | |
| 57 | errno = 0; |
| 58 | +#ifdef GLOB_ALTDIRFUNC |
| 59 | k = glob(path, flags | GLOB_ALTDIRFUNC, NULL, pglob); |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 60 | +#else |
| 61 | + k = glob(path, flags, NULL, pglob); |
| 62 | +#endif |
| 63 | if (k == GLOB_NOMATCH) |
| 64 | return -ENOENT; |
| 65 | if (k == GLOB_NOSPACE) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 66 | diff --git a/src/test/test-glob-util.c b/src/test/test-glob-util.c |
| 67 | index df6444c433..79a692046e 100644 |
| 68 | --- a/src/test/test-glob-util.c |
| 69 | +++ b/src/test/test-glob-util.c |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 70 | @@ -12,6 +12,12 @@ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 71 | #include "rm-rf.h" |
| 72 | #include "tmpfile-util.h" |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 73 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 74 | +/* Don't fail if the standard library |
| 75 | + * doesn't provide brace expansion */ |
| 76 | +#ifndef GLOB_BRACE |
| 77 | +#define GLOB_BRACE 0 |
| 78 | +#endif |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 79 | + |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 80 | static void test_glob_exists(void) { |
| 81 | char name[] = "/tmp/test-glob_exists.XXXXXX"; |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 82 | int fd = -1; |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 83 | @@ -39,11 +45,13 @@ static void test_glob_no_dot(void) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 84 | const char *fn; |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 85 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 86 | _cleanup_globfree_ glob_t g = { |
| 87 | +#ifdef GLOB_ALTDIRFUNC |
| 88 | .gl_closedir = closedir_wrapper, |
| 89 | .gl_readdir = (struct dirent *(*)(void *)) readdir_no_dot, |
| 90 | .gl_opendir = (void *(*)(const char *)) opendir, |
| 91 | .gl_lstat = lstat, |
| 92 | .gl_stat = stat, |
| 93 | +#endif |
| 94 | }; |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 95 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 96 | int r; |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 97 | @@ -51,11 +59,19 @@ static void test_glob_no_dot(void) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 98 | assert_se(mkdtemp(template)); |
| 99 | |
| 100 | fn = strjoina(template, "/*"); |
| 101 | +#ifdef GLOB_ALTDIRFUNC |
| 102 | r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g); |
| 103 | +#else |
| 104 | + r = glob(fn, GLOB_NOSORT|GLOB_BRACE, NULL, &g); |
| 105 | +#endif |
| 106 | assert_se(r == GLOB_NOMATCH); |
| 107 | |
| 108 | fn = strjoina(template, "/.*"); |
| 109 | +#ifdef GLOB_ALTDIRFUNC |
| 110 | r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g); |
| 111 | +#else |
| 112 | + r = glob(fn, GLOB_NOSORT|GLOB_BRACE, NULL, &g); |
| 113 | +#endif |
| 114 | assert_se(r == GLOB_NOMATCH); |
| 115 | |
| 116 | (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL); |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 117 | diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 118 | index 6b73d1e6d5..26b3b74275 100644 |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 119 | --- a/src/tmpfiles/tmpfiles.c |
| 120 | +++ b/src/tmpfiles/tmpfiles.c |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 121 | @@ -66,6 +66,12 @@ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 122 | #include "umask-util.h" |
| 123 | #include "user-util.h" |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 124 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 125 | +/* Don't fail if the standard library |
| 126 | + * doesn't provide brace expansion */ |
| 127 | +#ifndef GLOB_BRACE |
| 128 | +#define GLOB_BRACE 0 |
| 129 | +#endif |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 130 | + |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 131 | /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates |
| 132 | * them in the file system. This is intended to be used to create |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 133 | * properly owned directories beneath /tmp, /var/tmp, /run, which are |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 134 | @@ -1947,7 +1953,9 @@ finish: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 135 | |
| 136 | static int glob_item(Item *i, action_t action) { |
| 137 | _cleanup_globfree_ glob_t g = { |
| 138 | +#ifdef GLOB_ALTDIRFUNC |
| 139 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, |
| 140 | +#endif |
| 141 | }; |
| 142 | int r = 0, k; |
| 143 | char **fn; |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 144 | @@ -1967,7 +1975,9 @@ static int glob_item(Item *i, action_t action) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 145 | |
| 146 | static int glob_item_recursively(Item *i, fdaction_t action) { |
| 147 | _cleanup_globfree_ glob_t g = { |
| 148 | +#ifdef GLOB_ALTDIRFUNC |
| 149 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, |
| 150 | +#endif |
| 151 | }; |
| 152 | int r = 0, k; |
| 153 | char **fn; |