Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1 | From fb068403b25002156435350165ea418a6338a313 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 | --- a/src/basic/glob-util.c |
| 27 | +++ b/src/basic/glob-util.c |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 28 | @@ -12,6 +12,12 @@ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 29 | #include "path-util.h" |
| 30 | #include "strv.h" |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 31 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 32 | +/* Don't fail if the standard library |
| 33 | + * doesn't provide brace expansion */ |
| 34 | +#ifndef GLOB_BRACE |
| 35 | +#define GLOB_BRACE 0 |
| 36 | +#endif |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 37 | + |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 38 | static void closedir_wrapper(void* v) { |
| 39 | (void) closedir(v); |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 40 | } |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 41 | @@ -19,6 +25,7 @@ static void closedir_wrapper(void* v) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 42 | int safe_glob(const char *path, int flags, glob_t *pglob) { |
| 43 | int k; |
| 44 | |
| 45 | +#ifdef GLOB_ALTDIRFUNC |
| 46 | /* We want to set GLOB_ALTDIRFUNC ourselves, don't allow it to be set. */ |
| 47 | assert(!(flags & GLOB_ALTDIRFUNC)); |
| 48 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 49 | @@ -32,9 +39,14 @@ int safe_glob(const char *path, int flag |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 50 | pglob->gl_lstat = lstat; |
| 51 | if (!pglob->gl_stat) |
| 52 | pglob->gl_stat = stat; |
| 53 | +#endif |
| 54 | |
| 55 | errno = 0; |
| 56 | +#ifdef GLOB_ALTDIRFUNC |
| 57 | k = glob(path, flags | GLOB_ALTDIRFUNC, NULL, pglob); |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 58 | +#else |
| 59 | + k = glob(path, flags, NULL, pglob); |
| 60 | +#endif |
| 61 | if (k == GLOB_NOMATCH) |
| 62 | return -ENOENT; |
| 63 | if (k == GLOB_NOSPACE) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 64 | --- a/src/test/test-glob-util.c |
| 65 | +++ b/src/test/test-glob-util.c |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 66 | @@ -13,6 +13,12 @@ |
| 67 | #include "tests.h" |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 68 | #include "tmpfile-util.h" |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 69 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 70 | +/* Don't fail if the standard library |
| 71 | + * doesn't provide brace expansion */ |
| 72 | +#ifndef GLOB_BRACE |
| 73 | +#define GLOB_BRACE 0 |
| 74 | +#endif |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 75 | + |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 76 | TEST(glob_exists) { |
| 77 | char name[] = "/tmp/test-glob_exists.XXXXXX"; |
| 78 | int fd = -1; |
| 79 | @@ -40,11 +46,13 @@ TEST(glob_no_dot) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 80 | const char *fn; |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 81 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 82 | _cleanup_globfree_ glob_t g = { |
| 83 | +#ifdef GLOB_ALTDIRFUNC |
| 84 | .gl_closedir = closedir_wrapper, |
| 85 | .gl_readdir = (struct dirent *(*)(void *)) readdir_no_dot, |
| 86 | .gl_opendir = (void *(*)(const char *)) opendir, |
| 87 | .gl_lstat = lstat, |
| 88 | .gl_stat = stat, |
| 89 | +#endif |
| 90 | }; |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 91 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 92 | int r; |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 93 | @@ -52,11 +60,19 @@ TEST(glob_no_dot) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 94 | assert_se(mkdtemp(template)); |
| 95 | |
| 96 | fn = strjoina(template, "/*"); |
| 97 | +#ifdef GLOB_ALTDIRFUNC |
| 98 | r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g); |
| 99 | +#else |
| 100 | + r = glob(fn, GLOB_NOSORT|GLOB_BRACE, NULL, &g); |
| 101 | +#endif |
| 102 | assert_se(r == GLOB_NOMATCH); |
| 103 | |
| 104 | fn = strjoina(template, "/.*"); |
| 105 | +#ifdef GLOB_ALTDIRFUNC |
| 106 | r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g); |
| 107 | +#else |
| 108 | + r = glob(fn, GLOB_NOSORT|GLOB_BRACE, NULL, &g); |
| 109 | +#endif |
| 110 | assert_se(r == GLOB_NOMATCH); |
| 111 | |
| 112 | (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL); |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 113 | --- a/src/tmpfiles/tmpfiles.c |
| 114 | +++ b/src/tmpfiles/tmpfiles.c |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 115 | @@ -67,6 +67,12 @@ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 116 | #include "umask-util.h" |
| 117 | #include "user-util.h" |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 118 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 119 | +/* Don't fail if the standard library |
| 120 | + * doesn't provide brace expansion */ |
| 121 | +#ifndef GLOB_BRACE |
| 122 | +#define GLOB_BRACE 0 |
| 123 | +#endif |
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 | /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates |
| 126 | * 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] | 127 | * properly owned directories beneath /tmp, /var/tmp, /run, which are |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 128 | @@ -1961,7 +1967,9 @@ finish: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 129 | |
| 130 | static int glob_item(Item *i, action_t action) { |
| 131 | _cleanup_globfree_ glob_t g = { |
| 132 | +#ifdef GLOB_ALTDIRFUNC |
| 133 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, |
| 134 | +#endif |
| 135 | }; |
| 136 | int r = 0, k; |
| 137 | char **fn; |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 138 | @@ -1981,7 +1989,9 @@ static int glob_item(Item *i, action_t a |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 139 | |
| 140 | static int glob_item_recursively(Item *i, fdaction_t action) { |
| 141 | _cleanup_globfree_ glob_t g = { |
| 142 | +#ifdef GLOB_ALTDIRFUNC |
| 143 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, |
| 144 | +#endif |
| 145 | }; |
| 146 | int r = 0, k; |
| 147 | char **fn; |