|
1 | 1 | #define _GNU_SOURCE
|
2 | 2 |
|
| 3 | +#include <assert.h> |
| 4 | +#include <dlfcn.h> |
3 | 5 | #include <errno.h>
|
4 | 6 | #include <glob.h>
|
5 | 7 | #include <stdarg.h>
|
6 | 8 | #include <stdlib.h>
|
7 | 9 | #include <string.h>
|
8 | 10 |
|
| 11 | +const char *renderd = "/dev/dri/renderD*"; |
| 12 | +const char *mock_render_dirs[2] = {"/dev/dri/renderD128", |
| 13 | + "/dev/dri/renderD129"}; |
| 14 | + |
9 | 15 | int glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
|
10 | 16 | glob_t *pglob) {
|
11 | 17 | const char *mock_mode = getenv("MOCK_GLOB_MODE");
|
12 |
| - if (mock_mode && strcmp(mock_mode, "exists") == 0) { |
13 |
| - // Simulate that /dev/dri/renderD* exists |
14 |
| - pglob->gl_pathc = 2; |
15 |
| - pglob->gl_pathv = malloc(2 * sizeof(char *)); |
16 |
| - pglob->gl_pathv[0] = strdup("/dev/dri/renderD128"); |
17 |
| - pglob->gl_pathv[1] = strdup("/dev/dri/renderD129"); |
| 18 | + if (!mock_mode || (strcmp(pattern, renderd) != 0)) { |
| 19 | + // Delegate to real glob |
| 20 | + int (*real_glob)(const char *, int, int (*)(const char *, int), glob_t *); |
| 21 | + real_glob = dlsym(RTLD_NEXT, "glob"); |
| 22 | + if (!real_glob) { |
| 23 | + errno = ENOSYS; |
| 24 | + return -1; |
| 25 | + } |
| 26 | + return real_glob(pattern, flags, errfunc, pglob); |
| 27 | + } |
| 28 | + if (strcmp(mock_mode, "notfound") == 0) { |
| 29 | + pglob->gl_pathc = 0; |
| 30 | + pglob->gl_pathv = NULL; |
18 | 31 | return 0;
|
19 | 32 | }
|
20 |
| - // Default behavior: no matches |
21 |
| - pglob->gl_pathc = 0; |
22 |
| - pglob->gl_pathv = NULL; |
| 33 | + assert(strcmp(mock_mode, "exists") == 0); |
| 34 | + pglob->gl_pathc = 2; |
| 35 | + pglob->gl_pathv = mock_render_dirs; |
23 | 36 | return 0;
|
24 | 37 | }
|
25 | 38 |
|
26 |
| -void globfree(glob_t *pglob) { |
27 |
| - if (pglob->gl_pathv) { |
28 |
| - for (size_t i = 0; i < pglob->gl_pathc; ++i) { |
29 |
| - free(pglob->gl_pathv[i]); |
| 39 | +int glob64(const char *pattern, int flags, int (*errfunc)(const char *, int), |
| 40 | + glob64_t *pglob) { |
| 41 | + const char *mock_mode = getenv("MOCK_GLOB_MODE"); |
| 42 | + if (!mock_mode || (strcmp(pattern, renderd) != 0)) { |
| 43 | + // Delegate to real glob64 |
| 44 | + int (*real_glob64)(const char *, int, int (*)(const char *, int), |
| 45 | + glob64_t *); |
| 46 | + real_glob64 = dlsym(RTLD_NEXT, "glob64"); |
| 47 | + if (!real_glob64) { |
| 48 | + errno = ENOSYS; |
| 49 | + return -1; |
30 | 50 | }
|
31 |
| - free(pglob->gl_pathv); |
| 51 | + return real_glob64(pattern, flags, errfunc, pglob); |
| 52 | + } |
| 53 | + if (strcmp(mock_mode, "notfound") == 0) { |
| 54 | + pglob->gl_pathc = 0; |
| 55 | + pglob->gl_pathv = NULL; |
| 56 | + return 0; |
| 57 | + } |
| 58 | + assert(strcmp(mock_mode, "exists") == 0); |
| 59 | + pglob->gl_pathc = 2; |
| 60 | + pglob->gl_pathv = mock_render_dirs; |
| 61 | + return 0; |
| 62 | +} |
| 63 | + |
| 64 | +void globfree(glob_t *pglob) { |
| 65 | + if (pglob->gl_pathv == mock_render_dirs) { |
32 | 66 | pglob->gl_pathv = NULL;
|
33 | 67 | pglob->gl_pathc = 0;
|
| 68 | + return; |
34 | 69 | }
|
| 70 | + // Default behavior: call real globfree |
| 71 | + void (*real_globfree)(glob_t *); |
| 72 | + real_globfree = dlsym(RTLD_NEXT, "globfree"); |
| 73 | + if (!real_globfree) { |
| 74 | + errno = ENOSYS; |
| 75 | + return; |
| 76 | + } |
| 77 | + real_globfree(pglob); |
35 | 78 | }
|
36 | 79 |
|
37 |
| -int open(const char *pathname, int flags, ...) { |
| 80 | +void globfree64(glob64_t *pglob) { |
| 81 | + if (pglob->gl_pathv == mock_render_dirs) { |
| 82 | + pglob->gl_pathv = NULL; |
| 83 | + pglob->gl_pathc = 0; |
| 84 | + return; |
| 85 | + } |
| 86 | + void (*real_globfree64)(glob64_t *); |
| 87 | + real_globfree64 = dlsym(RTLD_NEXT, "globfree64"); |
| 88 | + if (!real_globfree64) { |
| 89 | + errno = ENOSYS; |
| 90 | + return; |
| 91 | + } |
| 92 | + real_globfree64(pglob); |
| 93 | +} |
| 94 | + |
| 95 | +#define DUMMY_FD_128 128 |
| 96 | +#define DUMMY_FD_129 129 |
| 97 | + |
| 98 | +int mock_open_helper(const char *pathname) { |
38 | 99 | const char *mock_mode = getenv("MOCK_OPEN_MODE");
|
39 |
| - if (strstr(pathname, "renderD12")) { |
40 |
| - if (mock_mode && strcmp(mock_mode, "deny") == 0) { |
| 100 | + assert(mock_mode != NULL && "MOCK_OPEN_MODE environment variable is not set"); |
| 101 | + if (strcmp(mock_mode, "deny") == 0) { |
| 102 | + errno = EACCES; |
| 103 | + return -1; |
| 104 | + } |
| 105 | + |
| 106 | + if (pathname == mock_render_dirs[0]) |
| 107 | + return DUMMY_FD_128; |
| 108 | + |
| 109 | + if (pathname == mock_render_dirs[1]) { |
| 110 | + if (mock_mode && strcmp(mock_mode, "deny_second") == 0) { |
41 | 111 | errno = EACCES;
|
42 | 112 | return -1;
|
43 | 113 | }
|
| 114 | + return DUMMY_FD_129; |
| 115 | + } |
| 116 | + assert(0 && "Unexpected pathname in mock_open_helper"); |
| 117 | +} |
44 | 118 |
|
45 |
| - if (mock_mode && strcmp(mock_mode, "deny_second") == 0) { |
46 |
| - // Simulate that the second file is not accessible |
47 |
| - if (strstr(pathname, "renderD129")) { |
48 |
| - errno = EACCES; |
49 |
| - return -1; |
50 |
| - } else { |
51 |
| - return 3; |
52 |
| - } |
53 |
| - } |
| 119 | +int open(const char *pathname, int flags, ...) { |
| 120 | + if (pathname == mock_render_dirs[0] || pathname == mock_render_dirs[1]) |
| 121 | + return mock_open_helper(pathname); |
54 | 122 |
|
55 |
| - if (mock_mode && strcmp(mock_mode, "allow") == 0) { |
56 |
| - return 3; // Dummy fd |
57 |
| - } |
| 123 | + // Call the real open function if not renderD12*. |
| 124 | + va_list ap; |
| 125 | + va_start(ap, flags); |
| 126 | + mode_t mode = va_arg(ap, mode_t); |
| 127 | + va_end(ap); |
| 128 | + int (*real_open)(const char *, int, ...); |
| 129 | + real_open = dlsym(RTLD_NEXT, "open"); |
| 130 | + if (!real_open) { |
| 131 | + errno = ENOSYS; |
| 132 | + return -1; |
| 133 | + } |
| 134 | + return real_open(pathname, flags, mode); |
| 135 | +} |
| 136 | + |
| 137 | +int open64(const char *pathname, int flags, ...) { |
| 138 | + if (pathname == mock_render_dirs[0] || pathname == mock_render_dirs[1]) |
| 139 | + return mock_open_helper(pathname); |
| 140 | + |
| 141 | + // Call the real open function if not renderD12*. |
| 142 | + va_list ap; |
| 143 | + va_start(ap, flags); |
| 144 | + mode_t mode = va_arg(ap, mode_t); |
| 145 | + va_end(ap); |
| 146 | + int (*real_open64)(const char *, int, ...); |
| 147 | + real_open64 = dlsym(RTLD_NEXT, "open64"); |
| 148 | + if (!real_open64) { |
| 149 | + errno = ENOSYS; |
| 150 | + return -1; |
58 | 151 | }
|
59 |
| - // Default: permission denied |
60 |
| - errno = EACCES; |
61 |
| - return -1; |
| 152 | + return real_open64(pathname, flags, mode); |
62 | 153 | }
|
63 | 154 |
|
64 |
| -int close(int fd) { return 0; } |
| 155 | +int close(int fd) { |
| 156 | + if (fd == DUMMY_FD_128 || fd == DUMMY_FD_129) { |
| 157 | + // Mock close for our dummy file descriptors. |
| 158 | + return 0; |
| 159 | + } |
| 160 | + |
| 161 | + int (*real_close)(int) = dlsym(RTLD_NEXT, "close"); |
| 162 | + if (!real_close) { |
| 163 | + errno = ENOSYS; |
| 164 | + return -1; |
| 165 | + } |
| 166 | + return real_close(fd); |
| 167 | +} |
0 commit comments