From fd53859ce5002947719e7e56d9731d5608c3aa96 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 9 May 2023 09:09:01 +0200 Subject: [PATCH 1/3] cmake: Define large file support for tests Signed-off-by: Andreas Schneider --- src/CMakeLists.txt | 6 ++++++ tests/CMakeLists.txt | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a1dbfaf..19e0d26 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,12 @@ target_compile_options(socket_wrapper PRIVATE ${DEFAULT_C_COMPILE_FLAGS} -D_GNU_SOURCE) +if (CMAKE_SIZEOF_VOID_P EQUAL 4) + target_compile_options(socket_wrapper + PRIVATE + -D_LARGEFILE64_SOURCE) +endif() + target_link_libraries(socket_wrapper PRIVATE ${SWRAP_REQUIRED_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e35c258..17d8d3c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -127,10 +127,15 @@ function(ADD_CMOCKA_TEST_ENVIRONMENT _TEST_NAME) ENVIRONMENT "${TORTURE_ENVIRONMENT}") endfunction() +if (CMAKE_SIZEOF_VOID_P EQUAL 4) + message(STATUS "Enabling large file support for tests") + set(LFS_CFLAGS "-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64") +endif() + foreach(_SWRAP_TEST ${SWRAP_TESTS}) add_cmocka_test(${_SWRAP_TEST} SOURCES ${_SWRAP_TEST}.c - COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS} -D_GNU_SOURCE + COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS} -D_GNU_SOURCE ${LFS_CFLAGS} LINK_LIBRARIES ${TORTURE_LIBRARY} socket_wrapper_noop LINK_OPTIONS ${DEFAULT_LINK_FLAGS}) add_cmocka_test_environment(${_SWRAP_TEST}) -- GitLab From 7a9554cea80415b75260daa1880aad0f0dcf0a26 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 9 May 2023 09:19:07 +0200 Subject: [PATCH 2/3] tests: Use F_(OFD)SETLK(64) in test_fcntl_lock Signed-off-by: Andreas Schneider --- tests/test_fcntl_lock.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/test_fcntl_lock.c b/tests/test_fcntl_lock.c index 98cf7c0..6bf2cd1 100644 --- a/tests/test_fcntl_lock.c +++ b/tests/test_fcntl_lock.c @@ -52,6 +52,13 @@ static void test_fcntl_lock(void **state) int fd, rc; struct flock lock; char *s = (char *)*state; + int cmd = F_SETLK; +#ifdef F_SETLK64 + cmd = F_SETLK64; +#endif +#ifdef F_OFD_SETLK + cmd = F_OFD_SETLK; +#endif rc = snprintf(file, sizeof(file), "%s/file", s); assert_in_range(rc, 0, PATH_MAX); @@ -65,7 +72,7 @@ static void test_fcntl_lock(void **state) lock.l_len = 4; lock.l_pid = 0; - rc = fcntl(fd, F_SETLK, &lock); + rc = fcntl(fd, cmd, &lock); assert_return_code(rc, errno); rc = unlink(file); -- GitLab From 082a4019926a1526cfd1e2341249202bf47822b9 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Mon, 19 Jun 2023 16:23:50 +0200 Subject: [PATCH 3/3] swrap: Add support for openat64() --- ConfigureChecks.cmake | 1 + config.h.cmake | 1 + src/socket_wrapper.c | 68 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake index c99e2ae..daaee2b 100644 --- a/ConfigureChecks.cmake +++ b/ConfigureChecks.cmake @@ -70,6 +70,7 @@ check_function_exists(timerfd_create HAVE_TIMERFD_CREATE) check_function_exists(bindresvport HAVE_BINDRESVPORT) check_function_exists(accept4 HAVE_ACCEPT4) check_function_exists(open64 HAVE_OPEN64) +check_function_exists(openat64 HAVE_OPENAT64) check_function_exists(fopen64 HAVE_FOPEN64) check_function_exists(getprogname HAVE_GETPROGNAME) check_function_exists(getexecname HAVE_GETEXECNAME) diff --git a/config.h.cmake b/config.h.cmake index a637a34..399013e 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -43,6 +43,7 @@ #cmakedefine HAVE_BINDRESVPORT 1 #cmakedefine HAVE_ACCEPT4 1 #cmakedefine HAVE_OPEN64 1 +#cmakedefine HAVE_OPENAT64 1 #cmakedefine HAVE_FOPEN64 1 #cmakedefine HAVE_GETPROGNAME 1 #cmakedefine HAVE_GETEXECNAME 1 diff --git a/src/socket_wrapper.c b/src/socket_wrapper.c index de2f732..dc07b53 100644 --- a/src/socket_wrapper.c +++ b/src/socket_wrapper.c @@ -531,6 +531,9 @@ typedef int (*__libc_open)(const char *pathname, int flags, ...); #ifdef HAVE_OPEN64 typedef int (*__libc_open64)(const char *pathname, int flags, ...); #endif /* HAVE_OPEN64 */ +#ifdef HAVE_OPENAT64 +typedef int (*__libc_openat64)(int dirfd, const char *pathname, int flags, ...); +#endif /* HAVE_OPENAT64 */ typedef int (*__libc_openat)(int dirfd, const char *path, int flags, ...); typedef int (*__libc_pipe)(int pipefd[2]); typedef int (*__libc_read)(int fd, void *buf, size_t count); @@ -631,6 +634,9 @@ struct swrap_libc_symbols { SWRAP_SYMBOL_ENTRY(open); #ifdef HAVE_OPEN64 SWRAP_SYMBOL_ENTRY(open64); +#endif +#ifdef HAVE_OPENAT64 + SWRAP_SYMBOL_ENTRY(openat64); #endif SWRAP_SYMBOL_ENTRY(openat); SWRAP_SYMBOL_ENTRY(pipe); @@ -1136,6 +1142,29 @@ static int libc_vopen64(const char *pathname, int flags, va_list ap) } #endif /* HAVE_OPEN64 */ +#ifdef HAVE_OPENAT64 +static int +libc_vopenat64(int dirfd, const char *pathname, int flags, va_list ap) +{ + int mode = 0; + int fd; + + swrap_bind_symbol_all(); + + swrap_inject_o_largefile(&flags); + + if (flags & O_CREAT) { + mode = va_arg(ap, int); + } + fd = swrap.libc.symbols._libc_openat64.f(dirfd, + pathname, + flags, + (mode_t)mode); + + return fd; +} +#endif /* HAVE_OPENAT64 */ + static int libc_vopenat(int dirfd, const char *path, int flags, va_list ap) { int mode = 0; @@ -1441,6 +1470,9 @@ static void __swrap_bind_symbol_all_once(void) swrap_bind_symbol_libc(open); #ifdef HAVE_OPEN64 swrap_bind_symbol_libc(open64); +#endif +#ifdef HAVE_OPENAT64 + swrap_bind_symbol_libc(openat64); #endif swrap_bind_symbol_libc(openat); swrap_bind_symbol_libsocket(pipe); @@ -4691,6 +4723,42 @@ int open64(const char *pathname, int flags, ...) } #endif /* HAVE_OPEN64 */ +/**************************************************************************** + * OPENAT64 + ***************************************************************************/ + +#ifdef HAVE_OPENAT64 +static int +swrap_vopenat64(int dirfd, const char *pathname, int flags, va_list ap) +{ + int ret; + + ret = libc_vopenat64(dirfd, pathname, flags, ap); + if (ret != -1) { + /* + * There are methods for closing descriptors (libc-internal code + * paths, direct syscalls) which close descriptors in ways that + * we can't intercept, so try to recover when we notice that + * that's happened + */ + swrap_remove_stale(ret); + } + return ret; +} + +int openat64(int dirfd, const char *pathname, int flags, ...) +{ + va_list ap; + int fd; + + va_start(ap, flags); + fd = swrap_vopenat64(dirfd, pathname, flags, ap); + va_end(ap); + + return fd; +} +#endif /* HAVE_OPENAT64 */ + /**************************************************************************** * OPENAT ***************************************************************************/ -- GitLab