team-ljyzipelo

공개
46줄1.9 KB
1#define _GNU_SOURCE
2#include <dlfcn.h>
3#include <errno.h>
4#include <fcntl.h>
5#include <stdio.h>
6#include <sys/stat.h>
7#include <unistd.h>
8
9typedef int (*statx_function)(int, const char *, int, unsigned int, struct statx *);
10
11int main(void) {
12 void *libc = dlopen("libc.so.6", RTLD_NOW);
13 if (!libc) return 1;
14 statx_function original = dlsym(libc, "statx");
15 statx_function intercepted = dlsym(RTLD_DEFAULT, "statx");
16 if (!original || !intercepted || original == intercepted) return 1;
17 int fd = open(".", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
18 if (fd < 0) return 1;
19 const char *paths[] = {NULL, "", "."};
20 const int flags[] = {0, AT_EMPTY_PATH};
21 int result = 0;
22 for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
23 for (size_t j = 0; j < sizeof(flags) / sizeof(flags[0]); j++) {
24 struct statx expected = {0};
25 struct statx actual = {0};
26 errno = 0;
27 int expected_result = original(fd, paths[i], flags[j], STATX_BASIC_STATS, &expected);
28 int expected_errno = errno;
29 errno = 0;
30 int actual_result = intercepted(fd, paths[i], flags[j], STATX_BASIC_STATS, &actual);
31 int actual_errno = errno;
32 if (actual_result != expected_result || actual_errno != expected_errno ||
33 (actual_result == 0 && (actual.stx_mask != expected.stx_mask ||
34 actual.stx_mode != expected.stx_mode || actual.stx_ino != expected.stx_ino ||
35 actual.stx_size != expected.stx_size))) {
36 fprintf(stderr, "path=%s flags=%d: result=%d errno=%d, expected result=%d errno=%d\n",
37 paths[i] ? paths[i] : "NULL", flags[j], actual_result, actual_errno,
38 expected_result, expected_errno);
39 result = 1;
40 }
41 }
42 }
43 close(fd);
44 dlclose(libc);
45 return result;
46}