From 92b65ce268f866d0c544177c8da02edd1e8e692a Mon Sep 17 00:00:00 2001 From: Maxython Date: Wed, 10 Apr 2024 00:07:50 +0300 Subject: [PATCH] fix(gpkg/glibc): adding `__shmctl64` fix(gpkg/glibc): disabling the `epoll_pwait2` system call --- gpkg/glibc/aarch64-arch-syscall.h.patch | 27 +- gpkg/glibc/arm-arch-syscall.h.patch | 34 ++- gpkg/glibc/build.sh | 4 +- gpkg/glibc/epoll_pwait2.c | 23 ++ gpkg/glibc/i386-arch-syscall.h.patch | 32 ++- gpkg/glibc/shmat.c | 44 +++ gpkg/glibc/shmat.c.patch | 61 ----- gpkg/glibc/shmctl.c | 75 ++++++ gpkg/glibc/shmctl.c.patch | 344 ------------------------ gpkg/glibc/shmdt.c | 35 +++ gpkg/glibc/shmdt.c.patch | 45 ---- gpkg/glibc/shmget.c | 152 +++++++++++ gpkg/glibc/shmget.c.patch | 161 ----------- gpkg/glibc/x86_64-arch-syscall.h.patch | 30 ++- 14 files changed, 408 insertions(+), 659 deletions(-) create mode 100644 gpkg/glibc/epoll_pwait2.c create mode 100644 gpkg/glibc/shmat.c delete mode 100644 gpkg/glibc/shmat.c.patch create mode 100644 gpkg/glibc/shmctl.c delete mode 100644 gpkg/glibc/shmctl.c.patch create mode 100644 gpkg/glibc/shmdt.c delete mode 100644 gpkg/glibc/shmdt.c.patch create mode 100644 gpkg/glibc/shmget.c delete mode 100644 gpkg/glibc/shmget.c.patch diff --git a/gpkg/glibc/aarch64-arch-syscall.h.patch b/gpkg/glibc/aarch64-arch-syscall.h.patch index 12cb2532c..3a433ac35 100644 --- a/gpkg/glibc/aarch64-arch-syscall.h.patch +++ b/gpkg/glibc/aarch64-arch-syscall.h.patch @@ -1,5 +1,5 @@ --- glibc-2.39/sysdeps/unix/sysv/linux/aarch64/arch-syscall.h 2024-01-31 03:34:58.000000000 +0300 -+++ glibc-2.39/sysdeps/unix/sysv/linux/aarch64/arch-syscall.h.patch 2024-02-26 11:19:01.235488233 +0300 ++++ glibc-2.39/sysdeps/unix/sysv/linux/aarch64/arch-syscall.h.patch 2024-04-10 00:15:19.800138807 +0300 @@ -1,5 +1,4 @@ /* AUTOGENERATED by update-syscall-lists.py. */ -#define __NR_accept 202 @@ -16,7 +16,14 @@ #define __NR_connect 203 #define __NR_copy_file_range 285 #define __NR_delete_module 106 -@@ -36,7 +33,6 @@ +@@ -29,14 +26,12 @@ + #define __NR_epoll_create1 20 + #define __NR_epoll_ctl 21 + #define __NR_epoll_pwait 22 +-#define __NR_epoll_pwait2 441 + #define __NR_eventfd2 19 + #define __NR_execve 221 + #define __NR_execveat 281 #define __NR_exit 93 #define __NR_exit_group 94 #define __NR_faccessat 48 @@ -24,7 +31,7 @@ #define __NR_fadvise64 223 #define __NR_fallocate 47 #define __NR_fanotify_init 262 -@@ -44,7 +40,6 @@ +@@ -44,7 +39,6 @@ #define __NR_fchdir 50 #define __NR_fchmod 52 #define __NR_fchmodat 53 @@ -32,7 +39,7 @@ #define __NR_fchown 55 #define __NR_fchownat 54 #define __NR_fcntl 25 -@@ -66,7 +61,6 @@ +@@ -66,7 +60,6 @@ #define __NR_futex 98 #define __NR_futex_requeue 456 #define __NR_futex_wait 455 @@ -40,7 +47,7 @@ #define __NR_futex_wake 454 #define __NR_get_mempolicy 236 #define __NR_get_robust_list 100 -@@ -117,7 +111,6 @@ +@@ -117,7 +110,6 @@ #define __NR_keyctl 219 #define __NR_kill 129 #define __NR_landlock_add_rule 445 @@ -48,7 +55,7 @@ #define __NR_landlock_restrict_self 446 #define __NR_lgetxattr 9 #define __NR_linkat 37 -@@ -174,7 +167,6 @@ +@@ -174,7 +166,6 @@ #define __NR_personality 92 #define __NR_pidfd_getfd 438 #define __NR_pidfd_open 434 @@ -56,7 +63,7 @@ #define __NR_pipe2 59 #define __NR_pivot_root 41 #define __NR_pkey_alloc 289 -@@ -211,7 +203,6 @@ +@@ -211,7 +202,6 @@ #define __NR_renameat2 276 #define __NR_request_key 218 #define __NR_restart_syscall 128 @@ -64,7 +71,7 @@ #define __NR_rt_sigaction 134 #define __NR_rt_sigpending 136 #define __NR_rt_sigprocmask 135 -@@ -243,12 +234,8 @@ +@@ -243,12 +233,8 @@ #define __NR_sendto 206 #define __NR_set_mempolicy 237 #define __NR_set_mempolicy_home_node 450 @@ -77,7 +84,7 @@ #define __NR_setgroups 159 #define __NR_sethostname 161 #define __NR_setitimer 103 -@@ -256,14 +243,12 @@ +@@ -256,14 +242,12 @@ #define __NR_setpgid 154 #define __NR_setpriority 140 #define __NR_setregid 143 @@ -92,7 +99,7 @@ #define __NR_setxattr 5 #define __NR_shmat 196 #define __NR_shmctl 195 -@@ -276,7 +261,6 @@ +@@ -276,7 +260,6 @@ #define __NR_socketpair 199 #define __NR_splice 76 #define __NR_statfs 43 diff --git a/gpkg/glibc/arm-arch-syscall.h.patch b/gpkg/glibc/arm-arch-syscall.h.patch index 60bf4dc5a..bdd8d3088 100644 --- a/gpkg/glibc/arm-arch-syscall.h.patch +++ b/gpkg/glibc/arm-arch-syscall.h.patch @@ -1,5 +1,5 @@ --- glibc-2.39/sysdeps/unix/sysv/linux/arm/arch-syscall.h 2024-01-31 03:34:58.000000000 +0300 -+++ glibc-2.39/sysdeps/unix/sysv/linux/arm/arch-syscall.h.patch 2024-02-26 11:20:30.485488199 +0300 ++++ glibc-2.39/sysdeps/unix/sysv/linux/arm/arch-syscall.h.patch 2024-04-10 00:15:27.480138805 +0300 @@ -2,7 +2,6 @@ #define __NR__llseek 140 #define __NR__newselect 142 @@ -33,7 +33,15 @@ #define __NR_connect 283 #define __NR_copy_file_range 391 #define __NR_creat 8 -@@ -57,14 +50,12 @@ +@@ -48,7 +41,6 @@ + #define __NR_epoll_create1 357 + #define __NR_epoll_ctl 251 + #define __NR_epoll_pwait 346 +-#define __NR_epoll_pwait2 441 + #define __NR_epoll_wait 252 + #define __NR_eventfd 351 + #define __NR_eventfd2 356 +@@ -57,14 +49,12 @@ #define __NR_exit 1 #define __NR_exit_group 248 #define __NR_faccessat 334 @@ -48,7 +56,7 @@ #define __NR_fchown 95 #define __NR_fchown32 207 #define __NR_fchownat 325 -@@ -94,7 +85,6 @@ +@@ -94,7 +84,6 @@ #define __NR_futex_requeue 456 #define __NR_futex_time64 422 #define __NR_futex_wait 455 @@ -56,7 +64,7 @@ #define __NR_futex_wake 454 #define __NR_futimesat 326 #define __NR_get_mempolicy 320 -@@ -114,7 +104,6 @@ +@@ -114,7 +103,6 @@ #define __NR_getitimer 105 #define __NR_getpeername 287 #define __NR_getpgid 132 @@ -64,7 +72,7 @@ #define __NR_getpid 20 #define __NR_getppid 64 #define __NR_getpriority 96 -@@ -156,7 +145,6 @@ +@@ -156,7 +144,6 @@ #define __NR_keyctl 311 #define __NR_kill 37 #define __NR_landlock_add_rule 445 @@ -72,7 +80,7 @@ #define __NR_landlock_restrict_self 446 #define __NR_lchown 16 #define __NR_lchown32 198 -@@ -226,7 +214,6 @@ +@@ -226,7 +213,6 @@ #define __NR_personality 136 #define __NR_pidfd_getfd 438 #define __NR_pidfd_open 434 @@ -80,7 +88,7 @@ #define __NR_pipe 42 #define __NR_pipe2 359 #define __NR_pivot_root 218 -@@ -259,7 +246,6 @@ +@@ -259,7 +245,6 @@ #define __NR_readlinkat 332 #define __NR_readv 145 #define __NR_reboot 88 @@ -88,7 +96,7 @@ #define __NR_recvfrom 292 #define __NR_recvmmsg 365 #define __NR_recvmmsg_time64 417 -@@ -271,8 +257,6 @@ +@@ -271,8 +256,6 @@ #define __NR_renameat2 382 #define __NR_request_key 310 #define __NR_restart_syscall 0 @@ -97,7 +105,7 @@ #define __NR_rt_sigaction 174 #define __NR_rt_sigpending 176 #define __NR_rt_sigprocmask 175 -@@ -301,7 +285,6 @@ +@@ -301,7 +284,6 @@ #define __NR_semop 298 #define __NR_semtimedop 312 #define __NR_semtimedop_time64 420 @@ -105,7 +113,7 @@ #define __NR_sendfile 187 #define __NR_sendfile64 239 #define __NR_sendmmsg 374 -@@ -309,16 +292,9 @@ +@@ -309,16 +291,9 @@ #define __NR_sendto 290 #define __NR_set_mempolicy 321 #define __NR_set_mempolicy_home_node 450 @@ -122,7 +130,7 @@ #define __NR_setgroups 81 #define __NR_setgroups32 206 #define __NR_sethostname 74 -@@ -328,8 +304,6 @@ +@@ -328,8 +303,6 @@ #define __NR_setpriority 97 #define __NR_setregid 71 #define __NR_setregid32 204 @@ -131,7 +139,7 @@ #define __NR_setresuid 164 #define __NR_setresuid32 208 #define __NR_setreuid 70 -@@ -338,8 +312,6 @@ +@@ -338,8 +311,6 @@ #define __NR_setsid 66 #define __NR_setsockopt 294 #define __NR_settimeofday 79 @@ -140,7 +148,7 @@ #define __NR_setxattr 226 #define __NR_shmat 305 #define __NR_shmctl 308 -@@ -361,10 +333,8 @@ +@@ -361,10 +332,8 @@ #define __NR_stat64 195 #define __NR_statfs 99 #define __NR_statfs64 266 diff --git a/gpkg/glibc/build.sh b/gpkg/glibc/build.sh index a10a7f9cb..d925b28be 100644 --- a/gpkg/glibc/build.sh +++ b/gpkg/glibc/build.sh @@ -3,7 +3,7 @@ TERMUX_PKG_DESCRIPTION="GNU C Library" TERMUX_PKG_LICENSE="GPL-3.0, LGPL-3.0" TERMUX_PKG_MAINTAINER="@termux-pacman" TERMUX_PKG_VERSION=2.39 -TERMUX_PKG_REVISION=2 +TERMUX_PKG_REVISION=3 TERMUX_PKG_SRCURL=https://ftp.gnu.org/gnu/libc/glibc-$TERMUX_PKG_VERSION.tar.xz TERMUX_PKG_SHA256=f77bd47cf8170c57365ae7bf86696c118adb3b120d3259c64c502d3dc1e2d926 TERMUX_PKG_DEPENDS="linux-api-headers-glibc" @@ -16,7 +16,7 @@ termux_step_pre_configure() { termux_error_exit "Compilation is only possible based on glibc" fi - for i in shmem-android.h mprotect.c ignore-syscall.h; do + for i in shmem-android.h shmat.c shmctl.c shmdt.c shmget.c mprotect.c ignore-syscall.h epoll_pwait2.c; do install -Dm644 "${TERMUX_PKG_BUILDER_DIR}/${i}" "${TERMUX_PKG_SRCDIR}/sysdeps/unix/sysv/linux/${i}" done diff --git a/gpkg/glibc/epoll_pwait2.c b/gpkg/glibc/epoll_pwait2.c new file mode 100644 index 000000000..fde125ad7 --- /dev/null +++ b/gpkg/glibc/epoll_pwait2.c @@ -0,0 +1,23 @@ +#include +#include + +int __epoll_pwait2_time64 (int fd, struct epoll_event *ev, int maxev, const struct __timespec64 *tmo, const sigset_t *s) +{ + // tmo->tv_nsec + return INLINE_SYSCALL_CALL (epoll_pwait, fd, ev, maxev, tmo->tv_sec*1000, s, __NSIG_BYTES); +} + +#if __TIMESIZE != 64 +libc_hidden_def (__epoll_pwait2_time64) + +int epoll_pwait2 (int fd, struct epoll_event *ev, int maxev, const struct timespec *tmo, const sigset_t *s) +{ + struct __timespec64 tmo64, *ptmo64 = NULL; + if (tmo != NULL) + { + tmo64 = valid_timespec_to_timespec64 (*tmo); + ptmo64 = &tmo64; + } + return __epoll_pwait2_time64 (fd, ev, maxev, ptmo64, s); +} +#endif diff --git a/gpkg/glibc/i386-arch-syscall.h.patch b/gpkg/glibc/i386-arch-syscall.h.patch index 46147f440..42052a697 100644 --- a/gpkg/glibc/i386-arch-syscall.h.patch +++ b/gpkg/glibc/i386-arch-syscall.h.patch @@ -1,5 +1,5 @@ --- glibc-2.39/sysdeps/unix/sysv/linux/i386/arch-syscall.h 2024-01-31 03:34:58.000000000 +0300 -+++ glibc-2.39/sysdeps/unix/sysv/linux/i386/arch-syscall.h.patch 2024-02-26 11:20:38.725488196 +0300 ++++ glibc-2.39/sysdeps/unix/sysv/linux/i386/arch-syscall.h.patch 2024-04-10 00:15:37.600138801 +0300 @@ -19,24 +19,18 @@ #define __NR_capget 184 #define __NR_capset 185 @@ -25,7 +25,15 @@ #define __NR_connect 362 #define __NR_copy_file_range 377 #define __NR_creat 8 -@@ -58,7 +52,6 @@ +@@ -49,7 +43,6 @@ + #define __NR_epoll_create1 329 + #define __NR_epoll_ctl 255 + #define __NR_epoll_pwait 319 +-#define __NR_epoll_pwait2 441 + #define __NR_epoll_wait 256 + #define __NR_eventfd 323 + #define __NR_eventfd2 328 +@@ -58,7 +51,6 @@ #define __NR_exit 1 #define __NR_exit_group 252 #define __NR_faccessat 307 @@ -33,7 +41,7 @@ #define __NR_fadvise64 250 #define __NR_fadvise64_64 272 #define __NR_fallocate 324 -@@ -67,7 +60,6 @@ +@@ -67,7 +59,6 @@ #define __NR_fchdir 133 #define __NR_fchmod 94 #define __NR_fchmodat 306 @@ -41,7 +49,7 @@ #define __NR_fchown 95 #define __NR_fchown32 207 #define __NR_fchownat 298 -@@ -98,7 +90,6 @@ +@@ -98,7 +89,6 @@ #define __NR_futex_requeue 456 #define __NR_futex_time64 422 #define __NR_futex_wait 455 @@ -49,7 +57,7 @@ #define __NR_futex_wake 454 #define __NR_futimesat 299 #define __NR_get_kernel_syms 130 -@@ -120,7 +111,6 @@ +@@ -120,7 +110,6 @@ #define __NR_getitimer 105 #define __NR_getpeername 368 #define __NR_getpgid 132 @@ -57,7 +65,7 @@ #define __NR_getpid 20 #define __NR_getpmsg 188 #define __NR_getppid 64 -@@ -168,7 +158,6 @@ +@@ -168,7 +157,6 @@ #define __NR_keyctl 288 #define __NR_kill 37 #define __NR_landlock_add_rule 445 @@ -65,7 +73,7 @@ #define __NR_landlock_restrict_self 446 #define __NR_lchown 16 #define __NR_lchown32 198 -@@ -245,7 +234,6 @@ +@@ -245,7 +233,6 @@ #define __NR_personality 136 #define __NR_pidfd_getfd 438 #define __NR_pidfd_open 434 @@ -73,7 +81,7 @@ #define __NR_pipe 42 #define __NR_pipe2 331 #define __NR_pivot_root 217 -@@ -294,8 +282,6 @@ +@@ -294,8 +281,6 @@ #define __NR_renameat2 353 #define __NR_request_key 287 #define __NR_restart_syscall 0 @@ -82,7 +90,7 @@ #define __NR_rt_sigaction 174 #define __NR_rt_sigpending 176 #define __NR_rt_sigprocmask 175 -@@ -330,16 +316,9 @@ +@@ -330,16 +315,9 @@ #define __NR_sendto 369 #define __NR_set_mempolicy 276 #define __NR_set_mempolicy_home_node 450 @@ -99,7 +107,7 @@ #define __NR_setgroups 81 #define __NR_setgroups32 206 #define __NR_sethostname 74 -@@ -349,8 +328,6 @@ +@@ -349,8 +327,6 @@ #define __NR_setpriority 97 #define __NR_setregid 71 #define __NR_setregid32 204 @@ -108,7 +116,7 @@ #define __NR_setresuid 164 #define __NR_setresuid32 208 #define __NR_setreuid 70 -@@ -359,8 +336,6 @@ +@@ -359,8 +335,6 @@ #define __NR_setsid 66 #define __NR_setsockopt 366 #define __NR_settimeofday 79 @@ -117,7 +125,7 @@ #define __NR_setxattr 226 #define __NR_sgetmask 68 #define __NR_shmat 397 -@@ -386,12 +361,10 @@ +@@ -386,12 +360,10 @@ #define __NR_stat64 195 #define __NR_statfs 99 #define __NR_statfs64 268 diff --git a/gpkg/glibc/shmat.c b/gpkg/glibc/shmat.c new file mode 100644 index 000000000..4d636ddd7 --- /dev/null +++ b/gpkg/glibc/shmat.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + +/* Attach the shared memory segment associated with SHMID to the data + segment of the calling process. SHMADDR and SHMFLG determine how + and where the segment is attached. */ + +void* shmat(int shmid, void const* shmaddr, int shmflg) +{ + ashv_check_pid(); + + int socket_id = ashv_socket_id_from_shmid(shmid); + void *addr; + + pthread_mutex_lock(&mutex); + + int idx = ashv_find_local_index(shmid); + if (idx == -1 && socket_id != ashv_local_socket_id) { + idx = ashv_read_remote_segment(shmid); + } + + if (idx == -1) { + DBG ("%s: shmid %x does not exist\n", __PRETTY_FUNCTION__, shmid); + pthread_mutex_unlock(&mutex); + errno = EINVAL; + return (void*) -1; + } + + if (shmem[idx].addr == NULL) { + shmem[idx].addr = mmap((void*) shmaddr, shmem[idx].size, PROT_READ | (shmflg == 0 ? PROT_WRITE : 0), MAP_SHARED, shmem[idx].descriptor, 0); + if (shmem[idx].addr == MAP_FAILED) { + DBG ("%s: mmap() failed for ID %x FD %d: %s\n", __PRETTY_FUNCTION__, idx, shmem[idx].descriptor, strerror(errno)); + shmem[idx].addr = NULL; + } + } + addr = shmem[idx].addr; + DBG ("%s: mapped addr %p for FD %d ID %d\n", __PRETTY_FUNCTION__, addr, shmem[idx].descriptor, idx); + pthread_mutex_unlock (&mutex); + + return addr ? addr : (void *)-1; +} diff --git a/gpkg/glibc/shmat.c.patch b/gpkg/glibc/shmat.c.patch deleted file mode 100644 index 49eaec6bf..000000000 --- a/gpkg/glibc/shmat.c.patch +++ /dev/null @@ -1,61 +0,0 @@ ---- glibc-2.39/sysdeps/unix/sysv/linux/shmat.c 2023-02-01 06:27:45.000000000 +0300 -+++ glibc-2.39/sysdeps/unix/sysv/linux/shmat.c.patch 2023-06-08 12:41:26.289174354 +0300 -@@ -18,25 +18,44 @@ - #include - #include - #include -+#include -+#include - - /* Attach the shared memory segment associated with SHMID to the data - segment of the calling process. SHMADDR and SHMFLG determine how - and where the segment is attached. */ - --void * --shmat (int shmid, const void *shmaddr, int shmflg) -+void* shmat(int shmid, void const* shmaddr, int shmflg) - { --#ifdef __ASSUME_DIRECT_SYSVIPC_SYSCALLS -- return (void*) INLINE_SYSCALL_CALL (shmat, shmid, shmaddr, shmflg); --#else -- unsigned long resultvar; -- void *raddr; -- -- resultvar = INTERNAL_SYSCALL_CALL (ipc, IPCOP_shmat, shmid, shmflg, -- &raddr, shmaddr); -- if (INTERNAL_SYSCALL_ERROR_P (resultvar)) -- return (void *) INLINE_SYSCALL_ERROR_RETURN_VALUE (INTERNAL_SYSCALL_ERRNO (resultvar)); -+ ashv_check_pid(); - -- return raddr; --#endif -+ int socket_id = ashv_socket_id_from_shmid(shmid); -+ void *addr; -+ -+ pthread_mutex_lock(&mutex); -+ -+ int idx = ashv_find_local_index(shmid); -+ if (idx == -1 && socket_id != ashv_local_socket_id) { -+ idx = ashv_read_remote_segment(shmid); -+ } -+ -+ if (idx == -1) { -+ DBG ("%s: shmid %x does not exist\n", __PRETTY_FUNCTION__, shmid); -+ pthread_mutex_unlock(&mutex); -+ errno = EINVAL; -+ return (void*) -1; -+ } -+ -+ if (shmem[idx].addr == NULL) { -+ shmem[idx].addr = mmap((void*) shmaddr, shmem[idx].size, PROT_READ | (shmflg == 0 ? PROT_WRITE : 0), MAP_SHARED, shmem[idx].descriptor, 0); -+ if (shmem[idx].addr == MAP_FAILED) { -+ DBG ("%s: mmap() failed for ID %x FD %d: %s\n", __PRETTY_FUNCTION__, idx, shmem[idx].descriptor, strerror(errno)); -+ shmem[idx].addr = NULL; -+ } -+ } -+ addr = shmem[idx].addr; -+ DBG ("%s: mapped addr %p for FD %d ID %d\n", __PRETTY_FUNCTION__, addr, shmem[idx].descriptor, idx); -+ pthread_mutex_unlock (&mutex); -+ -+ return addr ? addr : (void *)-1; - } diff --git a/gpkg/glibc/shmctl.c b/gpkg/glibc/shmctl.c new file mode 100644 index 000000000..9af84d58d --- /dev/null +++ b/gpkg/glibc/shmctl.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef shmid_ds +# define shmid_ds shmid64_ds +#endif + +int shmctl(int shmid, int cmd, struct shmid_ds *buf) +{ + ashv_check_pid(); + + if (cmd == IPC_RMID) { + DBG("%s: IPC_RMID for shmid=%x\n", __PRETTY_FUNCTION__, shmid); + pthread_mutex_lock(&mutex); + int idx = ashv_find_local_index(shmid); + if (idx == -1) { + DBG("%s: shmid=%x does not exist locally\n", __PRETTY_FUNCTION__, shmid); + /* We do not rm non-local regions, but do not report an error for that. */ + pthread_mutex_unlock(&mutex); + return 0; + } + + if (shmem[idx].addr) { + // shmctl(2): The segment will actually be destroyed only + // after the last process detaches it (i.e., when the shm_nattch + // member of the associated structure shmid_ds is zero. + shmem[idx].markedForDeletion = true; + } else { + android_shmem_delete(idx); + } + pthread_mutex_unlock(&mutex); + return 0; + } else if (cmd == IPC_STAT) { + if (!buf) { + DBG ("%s: ERROR: buf == NULL for shmid %x\n", __PRETTY_FUNCTION__, shmid); + errno = EINVAL; + return -1; + } + + pthread_mutex_lock(&mutex); + int idx = ashv_find_local_index(shmid); + if (idx == -1) { + DBG ("%s: ERROR: shmid %x does not exist\n", __PRETTY_FUNCTION__, shmid); + pthread_mutex_unlock (&mutex); + errno = EINVAL; + return -1; + } + /* Report max permissive mode */ + memset(buf, 0, sizeof(struct shmid_ds)); + buf->shm_segsz = shmem[idx].size; + buf->shm_nattch = 1; + buf->shm_perm.key = shmem[idx].key; + buf->shm_perm.uid = geteuid(); + buf->shm_perm.gid = getegid(); + buf->shm_perm.cuid = geteuid(); + buf->shm_perm.cgid = getegid(); + buf->shm_perm.mode = 0666; + buf->shm_perm.seq = 1; + + pthread_mutex_unlock (&mutex); + return 0; + } + + DBG("%s: cmd %d not implemented yet!\n", __PRETTY_FUNCTION__, cmd); + errno = EINVAL; + return -1; +} + +weak_alias (shmctl, __shmctl64) diff --git a/gpkg/glibc/shmctl.c.patch b/gpkg/glibc/shmctl.c.patch deleted file mode 100644 index 9518c17c0..000000000 --- a/gpkg/glibc/shmctl.c.patch +++ /dev/null @@ -1,344 +0,0 @@ ---- glibc-2.39/sysdeps/unix/sysv/linux/shmctl.c 2023-02-01 06:27:45.000000000 +0300 -+++ glibc-2.39/sysdeps/unix/sysv/linux/shmctl.c.patch 2023-06-08 12:43:28.639174307 +0300 -@@ -15,277 +15,76 @@ - License along with the GNU C Library; if not, see - . */ - --#include - #include - #include - #include - #include - #include --#include /* For __kernel_mode_t. */ -+#include -+#include -+#include -+ -+#ifndef shmid_ds -+# define shmid_ds shmid64_ds -+#endif -+ -+int shmctl(int shmid, int cmd, struct shmid_ds *buf) -+{ -+ ashv_check_pid(); -+ -+ if (cmd == IPC_RMID) { -+ DBG("%s: IPC_RMID for shmid=%x\n", __PRETTY_FUNCTION__, shmid); -+ pthread_mutex_lock(&mutex); -+ int idx = ashv_find_local_index(shmid); -+ if (idx == -1) { -+ DBG("%s: shmid=%x does not exist locally\n", __PRETTY_FUNCTION__, shmid); -+ /* We do not rm non-local regions, but do not report an error for that. */ -+ pthread_mutex_unlock(&mutex); -+ return 0; -+ } -+ -+ if (shmem[idx].addr) { -+ // shmctl(2): The segment will actually be destroyed only -+ // after the last process detaches it (i.e., when the shm_nattch -+ // member of the associated structure shmid_ds is zero. -+ shmem[idx].markedForDeletion = true; -+ } else { -+ android_shmem_delete(idx); -+ } -+ pthread_mutex_unlock(&mutex); -+ return 0; -+ } else if (cmd == IPC_STAT) { -+ if (!buf) { -+ DBG ("%s: ERROR: buf == NULL for shmid %x\n", __PRETTY_FUNCTION__, shmid); -+ errno = EINVAL; -+ return -1; -+ } -+ -+ pthread_mutex_lock(&mutex); -+ int idx = ashv_find_local_index(shmid); -+ if (idx == -1) { -+ DBG ("%s: ERROR: shmid %x does not exist\n", __PRETTY_FUNCTION__, shmid); -+ pthread_mutex_unlock (&mutex); -+ errno = EINVAL; -+ return -1; -+ } -+ /* Report max permissive mode */ -+ memset(buf, 0, sizeof(struct shmid_ds)); -+ buf->shm_segsz = shmem[idx].size; -+ buf->shm_nattch = 1; -+ buf->shm_perm.key = shmem[idx].key; -+ buf->shm_perm.uid = geteuid(); -+ buf->shm_perm.gid = getegid(); -+ buf->shm_perm.cuid = geteuid(); -+ buf->shm_perm.cgid = getegid(); -+ buf->shm_perm.mode = 0666; -+ buf->shm_perm.seq = 1; - --/* POSIX states ipc_perm mode should have type of mode_t. */ --_Static_assert (sizeof ((struct shmid_ds){0}.shm_perm.mode) -- == sizeof (mode_t), -- "sizeof (shmid_ds.shm_perm.mode) != sizeof (mode_t)"); -- --#if __IPC_TIME64 == 0 --typedef struct shmid_ds shmctl_arg_t; --#else --# include -- --static void --shmid64_to_kshmid64 (const struct __shmid64_ds *shmid64, -- struct kernel_shmid64_ds *kshmid) --{ -- kshmid->shm_perm = shmid64->shm_perm; -- kshmid->shm_segsz = shmid64->shm_segsz; -- kshmid->shm_atime = shmid64->shm_atime; -- kshmid->shm_atime_high = shmid64->shm_atime >> 32; -- kshmid->shm_dtime = shmid64->shm_dtime; -- kshmid->shm_dtime_high = shmid64->shm_dtime >> 32; -- kshmid->shm_ctime = shmid64->shm_ctime; -- kshmid->shm_ctime_high = shmid64->shm_ctime >> 32; -- kshmid->shm_cpid = shmid64->shm_cpid; -- kshmid->shm_lpid = shmid64->shm_lpid; -- kshmid->shm_nattch = shmid64->shm_nattch; --} -- --static void --kshmid64_to_shmid64 (const struct kernel_shmid64_ds *kshmid, -- struct __shmid64_ds *shmid64) --{ -- shmid64->shm_perm = kshmid->shm_perm; -- shmid64->shm_segsz = kshmid->shm_segsz; -- shmid64->shm_atime = kshmid->shm_atime -- | ((__time64_t) kshmid->shm_atime_high << 32); -- shmid64->shm_dtime = kshmid->shm_dtime -- | ((__time64_t) kshmid->shm_dtime_high << 32); -- shmid64->shm_ctime = kshmid->shm_ctime -- | ((__time64_t) kshmid->shm_ctime_high << 32); -- shmid64->shm_cpid = kshmid->shm_cpid; -- shmid64->shm_lpid = kshmid->shm_lpid; -- shmid64->shm_nattch = kshmid->shm_nattch; --} -- --typedef struct kernel_shmid64_ds shmctl_arg_t; --#endif -- --static int --shmctl_syscall (int shmid, int cmd, shmctl_arg_t *buf) --{ --#ifdef __ASSUME_DIRECT_SYSVIPC_SYSCALLS -- return INLINE_SYSCALL_CALL (shmctl, shmid, cmd | __IPC_64, buf); --#else -- return INLINE_SYSCALL_CALL (ipc, IPCOP_shmctl, shmid, cmd | __IPC_64, 0, -- buf); --#endif --} -- --/* Provide operations to control over shared memory segments. */ --int --__shmctl64 (int shmid, int cmd, struct __shmid64_ds *buf) --{ --#if IPC_CTL_NEED_TRANSLATION --# if __IPC_TIME64 -- struct kernel_shmid64_ds kshmid, *arg = NULL; --# else -- shmctl_arg_t *arg; --# endif -- -- /* Some applications pass the __IPC_64 flag in cmd, to invoke -- previously unsupported commands back when there was no EINVAL -- error checking in glibc. Mask the flag for the switch statements -- below. shmctl_syscall adds back the __IPC_64 flag for the actual -- system call. */ -- cmd &= ~__IPC_64; -- -- switch (cmd) -- { -- case IPC_RMID: -- case SHM_LOCK: -- case SHM_UNLOCK: -- arg = NULL; -- break; -- -- case IPC_SET: -- case IPC_STAT: -- case SHM_STAT: -- case SHM_STAT_ANY: --# if __IPC_TIME64 -- if (buf != NULL) -- { -- shmid64_to_kshmid64 (buf, &kshmid); -- arg = &kshmid; -- } --# ifdef __ASSUME_SYSVIPC_BROKEN_MODE_T -- if (cmd == IPC_SET) -- arg->shm_perm.mode *= 0x10000U; --# endif --# else -- arg = buf; --# endif -- break; -- -- case IPC_INFO: -- case SHM_INFO: -- /* This is a Linux extension where kernel expects either a -- 'struct shminfo' (IPC_INFO) or 'struct shm_info' (SHM_INFO). */ -- arg = (__typeof__ (arg)) buf; -- break; -- -- default: -- __set_errno (EINVAL); -- return -1; -- } -- -- -- int ret = shmctl_syscall (shmid, cmd, arg); -- if (ret < 0) -- return ret; -- -- switch (cmd) -- { -- case IPC_STAT: -- case SHM_STAT: -- case SHM_STAT_ANY: --# ifdef __ASSUME_SYSVIPC_BROKEN_MODE_T -- arg->shm_perm.mode >>= 16; --# else -- /* Old Linux kernel versions might not clear the mode padding. */ -- if (sizeof ((struct shmid_ds){0}.shm_perm.mode) -- != sizeof (__kernel_mode_t)) -- arg->shm_perm.mode &= 0xFFFF; --# endif -- --# if __IPC_TIME64 -- kshmid64_to_shmid64 (arg, buf); --# endif -- } -- -- return ret; -- --#else /* !IPC_CTL_NEED_TRANSLATION */ -- return shmctl_syscall (shmid, cmd, buf); --#endif --} --#if __TIMESIZE != 64 --libc_hidden_def (__shmctl64) -- --static void --shmid_to_shmid64 (struct __shmid64_ds *shm64, const struct shmid_ds *shm) --{ -- shm64->shm_perm = shm->shm_perm; -- shm64->shm_segsz = shm->shm_segsz; -- shm64->shm_atime = shm->shm_atime -- | ((__time64_t) shm->__shm_atime_high << 32); -- shm64->shm_dtime = shm->shm_dtime -- | ((__time64_t) shm->__shm_dtime_high << 32); -- shm64->shm_ctime = shm->shm_ctime -- | ((__time64_t) shm->__shm_ctime_high << 32); -- shm64->shm_cpid = shm->shm_cpid; -- shm64->shm_lpid = shm->shm_lpid; -- shm64->shm_nattch = shm->shm_nattch; --} -- --static void --shmid64_to_shmid (struct shmid_ds *shm, const struct __shmid64_ds *shm64) --{ -- shm->shm_perm = shm64->shm_perm; -- shm->shm_segsz = shm64->shm_segsz; -- shm->shm_atime = shm64->shm_atime; -- shm->__shm_atime_high = 0; -- shm->shm_dtime = shm64->shm_dtime; -- shm->__shm_dtime_high = 0; -- shm->shm_ctime = shm64->shm_ctime; -- shm->__shm_ctime_high = 0; -- shm->shm_cpid = shm64->shm_cpid; -- shm->shm_lpid = shm64->shm_lpid; -- shm->shm_nattch = shm64->shm_nattch; --} -- --int --__shmctl (int shmid, int cmd, struct shmid_ds *buf) --{ -- struct __shmid64_ds shmid64, *buf64 = NULL; -- if (buf != NULL) -- { -- /* This is a Linux extension where kernel expects either a -- 'struct shminfo' (IPC_INFO) or 'struct shm_info' (SHM_INFO). */ -- if (cmd == IPC_INFO || cmd == SHM_INFO) -- buf64 = (struct __shmid64_ds *) buf; -- else -- { -- shmid_to_shmid64 (&shmid64, buf); -- buf64 = &shmid64; -+ pthread_mutex_unlock (&mutex); -+ return 0; - } -- } -- -- int ret = __shmctl64 (shmid, cmd, buf64); -- if (ret < 0) -- return ret; - -- switch (cmd) -- { -- case IPC_STAT: -- case SHM_STAT: -- case SHM_STAT_ANY: -- shmid64_to_shmid (buf, buf64); -- } -- -- return ret; -+ DBG("%s: cmd %d not implemented yet!\n", __PRETTY_FUNCTION__, cmd); -+ errno = EINVAL; -+ return -1; - } --#endif -- --#ifndef DEFAULT_VERSION --# ifndef __ASSUME_SYSVIPC_BROKEN_MODE_T --# define DEFAULT_VERSION GLIBC_2_2 --# else --# define DEFAULT_VERSION GLIBC_2_31 --# endif --#endif -- --versioned_symbol (libc, __shmctl, shmctl, DEFAULT_VERSION); -- --#if defined __ASSUME_SYSVIPC_BROKEN_MODE_T \ -- && SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_31) --int --attribute_compat_text_section --__shmctl_mode16 (int shmid, int cmd, struct shmid_ds *buf) --{ -- return shmctl_syscall (shmid, cmd, (shmctl_arg_t *) buf); --} --compat_symbol (libc, __shmctl_mode16, shmctl, GLIBC_2_2); --#endif -- --#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2) --struct __old_shmid_ds --{ -- struct __old_ipc_perm shm_perm; /* operation permission struct */ -- int shm_segsz; /* size of segment in bytes */ -- __time_t shm_atime; /* time of last shmat() */ -- __time_t shm_dtime; /* time of last shmdt() */ -- __time_t shm_ctime; /* time of last change by shmctl() */ -- __ipc_pid_t shm_cpid; /* pid of creator */ -- __ipc_pid_t shm_lpid; /* pid of last shmop */ -- unsigned short int shm_nattch; /* number of current attaches */ -- unsigned short int __shm_npages; /* size of segment (pages) */ -- unsigned long int *__shm_pages; /* array of ptrs to frames -> SHMMAX */ -- struct vm_area_struct *__attaches; /* descriptors for attaches */ --}; -- --int --attribute_compat_text_section --__old_shmctl (int shmid, int cmd, struct __old_shmid_ds *buf) --{ --#if defined __ASSUME_DIRECT_SYSVIPC_SYSCALLS \ -- && !defined __ASSUME_SYSVIPC_DEFAULT_IPC_64 -- /* For architecture that have wire-up shmctl but also have __IPC_64 to a -- value different than default (0x0), it means the compat symbol used the -- __NR_ipc syscall. */ -- return INLINE_SYSCALL_CALL (shmctl, shmid, cmd, buf); --#else -- return INLINE_SYSCALL_CALL (ipc, IPCOP_shmctl, shmid, cmd, 0, buf); --#endif --} --compat_symbol (libc, __old_shmctl, shmctl, GLIBC_2_0); --#endif diff --git a/gpkg/glibc/shmdt.c b/gpkg/glibc/shmdt.c new file mode 100644 index 000000000..06827fa75 --- /dev/null +++ b/gpkg/glibc/shmdt.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +/* Detach shared memory segment starting at address specified by SHMADDR + from the caller's data segment. */ + +int shmdt(void const* shmaddr) +{ + ashv_check_pid(); + + pthread_mutex_lock(&mutex); + for (size_t i = 0; i < shmem_amount; i++) { + if (shmem[i].addr == shmaddr) { + if (munmap(shmem[i].addr, shmem[i].size) != 0) { + DBG("%s: munmap %p failed\n", __PRETTY_FUNCTION__, shmaddr); + } + shmem[i].addr = NULL; + DBG("%s: unmapped addr %p for FD %d ID %zu shmid %x\n", __PRETTY_FUNCTION__, shmaddr, shmem[i].descriptor, i, shmem[i].id); + if (shmem[i].markedForDeletion || ashv_socket_id_from_shmid(shmem[i].id) != ashv_local_socket_id) { + DBG ("%s: deleting shmid %x\n", __PRETTY_FUNCTION__, shmem[i].id); + android_shmem_delete(i); + } + pthread_mutex_unlock(&mutex); + return 0; + } + } + pthread_mutex_unlock(&mutex); + + DBG("%s: invalid address %p\n", __PRETTY_FUNCTION__, shmaddr); + /* Could be a remove segment, do not report an error for that. */ + return 0; +} diff --git a/gpkg/glibc/shmdt.c.patch b/gpkg/glibc/shmdt.c.patch deleted file mode 100644 index 9287d7c9d..000000000 --- a/gpkg/glibc/shmdt.c.patch +++ /dev/null @@ -1,45 +0,0 @@ ---- glibc-2.39/sysdeps/unix/sysv/linux/shmdt.c 2023-02-01 06:27:45.000000000 +0300 -+++ glibc-2.39/sysdeps/unix/sysv/linux/shmdt.c.patch 2023-06-08 12:48:00.059174203 +0300 -@@ -18,16 +18,35 @@ - #include - #include - #include -+#include -+#include - - /* Detach shared memory segment starting at address specified by SHMADDR - from the caller's data segment. */ - --int --shmdt (const void *shmaddr) -+int shmdt(void const* shmaddr) - { --#ifdef __ASSUME_DIRECT_SYSVIPC_SYSCALLS -- return INLINE_SYSCALL_CALL (shmdt, shmaddr); --#else -- return INLINE_SYSCALL_CALL (ipc, IPCOP_shmdt, 0, 0, 0, shmaddr); --#endif -+ ashv_check_pid(); -+ -+ pthread_mutex_lock(&mutex); -+ for (size_t i = 0; i < shmem_amount; i++) { -+ if (shmem[i].addr == shmaddr) { -+ if (munmap(shmem[i].addr, shmem[i].size) != 0) { -+ DBG("%s: munmap %p failed\n", __PRETTY_FUNCTION__, shmaddr); -+ } -+ shmem[i].addr = NULL; -+ DBG("%s: unmapped addr %p for FD %d ID %zu shmid %x\n", __PRETTY_FUNCTION__, shmaddr, shmem[i].descriptor, i, shmem[i].id); -+ if (shmem[i].markedForDeletion || ashv_socket_id_from_shmid(shmem[i].id) != ashv_local_socket_id) { -+ DBG ("%s: deleting shmid %x\n", __PRETTY_FUNCTION__, shmem[i].id); -+ android_shmem_delete(i); -+ } -+ pthread_mutex_unlock(&mutex); -+ return 0; -+ } -+ } -+ pthread_mutex_unlock(&mutex); -+ -+ DBG("%s: invalid address %p\n", __PRETTY_FUNCTION__, shmaddr); -+ /* Could be a remove segment, do not report an error for that. */ -+ return 0; - } diff --git a/gpkg/glibc/shmget.c b/gpkg/glibc/shmget.c new file mode 100644 index 000000000..6eaa740e4 --- /dev/null +++ b/gpkg/glibc/shmget.c @@ -0,0 +1,152 @@ +#include +#include +#include +#include +#include + +/* Return an identifier for an shared memory segment of at least size SIZE + which is associated with KEY. */ + +int shmget(key_t key, size_t size, int flags) +{ + (void) flags; + + ashv_check_pid(); + + // Counter wrapping around at 15 bits. + static size_t shmem_counter = 0; + + if (!ashv_listening_thread_id) { + int sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (!sock) { + DBG ("%s: cannot create UNIX socket: %s\n", __PRETTY_FUNCTION__, strerror(errno)); + errno = EINVAL; + return -1; + } + int i; + for (i = 0; i < 4096; i++) { + struct sockaddr_un addr; + int len; + memset (&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + ashv_local_socket_id = (getpid() + i) & 0xffff; + sprintf(&addr.sun_path[1], ANDROID_SHMEM_SOCKNAME, ashv_local_socket_id); + len = sizeof(addr.sun_family) + strlen(&addr.sun_path[1]) + 1; + if (bind(sock, (struct sockaddr *)&addr, len) != 0) continue; + DBG("%s: bound UNIX socket %s in pid=%d\n", __PRETTY_FUNCTION__, addr.sun_path + 1, getpid()); + break; + } + if (i == 4096) { + DBG("%s: cannot bind UNIX socket, bailing out\n", __PRETTY_FUNCTION__); + ashv_local_socket_id = 0; + errno = ENOMEM; + return -1; + } + if (listen(sock, 4) != 0) { + DBG("%s: listen failed\n", __PRETTY_FUNCTION__); + errno = ENOMEM; + return -1; + } + int* socket_arg = malloc(sizeof(int)); + *socket_arg = sock; + pthread_create(&ashv_listening_thread_id, NULL, &ashv_thread_function, socket_arg); + } + + int shmid = -1; + + pthread_mutex_lock(&mutex); + char symlink_path[256]; + if (key != IPC_PRIVATE) { + // (1) Check if symlink exists telling us where to connect. + // (2) If so, try to connect and open. + // (3) If connected and opened, done. If connection refused + // take ownership of the key and create the symlink. + // (4) If no symlink, create it. + sprintf(symlink_path, ASHV_KEY_SYMLINK_PATH, key); + char path_buffer[256]; + char num_buffer[64]; + while (true) { + int path_length = readlink(symlink_path, path_buffer, sizeof(path_buffer) - 1); + if (path_length != -1) { + path_buffer[path_length] = '\0'; + int shmid = atoi(path_buffer); + if (shmid != 0) { + int idx = ashv_find_local_index(shmid); + + if (idx == -1) { + idx = ashv_read_remote_segment(shmid); + } + + if (idx != -1) { + pthread_mutex_unlock(&mutex); + return shmem[idx].id; + } + } + // TODO: Not sure we should try to remove previous owner if e.g. + // there was a tempporary failture to get a soket. Need to + // distinguish between why ashv_read_remote_segment failed. + unlink(symlink_path); + } + // Take ownership. + // TODO: HAndle error (out of resouces, no infinite loop) + if (shmid == -1) { + shmem_counter = (shmem_counter + 1) & 0x7fff; + shmid = ashv_shmid_from_counter(shmem_counter); + sprintf(num_buffer, "%d", shmid); + } + if (symlink(num_buffer, symlink_path) == 0) break; + } + } + + + int idx = shmem_amount; + char buf[256]; + sprintf(buf, ANDROID_SHMEM_SOCKNAME "-%d", ashv_local_socket_id, idx); + + shmem_amount++; + if (shmid == -1) { + shmem_counter = (shmem_counter + 1) & 0x7fff; + shmid = ashv_shmid_from_counter(shmem_counter); + } + + shmem = realloc(shmem, shmem_amount * sizeof(shmem_t)); + size = ROUND_UP(size, getpagesize()); + shmem[idx].size = size; + shmem[idx].descriptor = ashmem_create_region(buf, size); + shmem[idx].addr = NULL; + shmem[idx].id = shmid; + shmem[idx].markedForDeletion = false; + shmem[idx].key = key; + + if (shmem[idx].descriptor < 0) { + DBG("%s: ashmem_create_region() failed for size %zu: %s\n", __PRETTY_FUNCTION__, size, strerror(errno)); + shmem_amount --; + shmem = realloc(shmem, shmem_amount * sizeof(shmem_t)); + pthread_mutex_unlock (&mutex); + return -1; + } + //DBG("%s: ID %d shmid %x FD %d size %zu\n", __PRETTY_FUNCTION__, idx, shmid, shmem[idx].descriptor, shmem[idx].size); + /* + status = ashmem_set_prot_region (shmem[idx].descriptor, 0666); + if (status < 0) { + //DBG ("%s: ashmem_set_prot_region() failed for size %zu: %s %d\n", __PRETTY_FUNCTION__, size, strerror(status), status); + shmem_amount --; + shmem = realloc (shmem, shmem_amount * sizeof(shmem_t)); + pthread_mutex_unlock (&mutex); + return -1; + } + */ + /* + status = ashmem_pin_region (shmem[idx].descriptor, 0, shmem[idx].size); + if (status < 0) { + //DBG ("%s: ashmem_pin_region() failed for size %zu: %s %d\n", __PRETTY_FUNCTION__, size, strerror(status), status); + shmem_amount --; + shmem = realloc (shmem, shmem_amount * sizeof(shmem_t)); + pthread_mutex_unlock (&mutex); + return -1; + } + */ + pthread_mutex_unlock(&mutex); + + return shmid; +} diff --git a/gpkg/glibc/shmget.c.patch b/gpkg/glibc/shmget.c.patch deleted file mode 100644 index 8b2ee436c..000000000 --- a/gpkg/glibc/shmget.c.patch +++ /dev/null @@ -1,161 +0,0 @@ ---- glibc-2.39/sysdeps/unix/sysv/linux/shmget.c 2023-02-01 06:27:45.000000000 +0300 -+++ glibc-2.39/sysdeps/unix/sysv/linux/shmget.c.patch 2023-06-08 12:46:33.199174236 +0300 -@@ -19,16 +19,151 @@ - #include - #include - #include -+#include - - /* Return an identifier for an shared memory segment of at least size SIZE - which is associated with KEY. */ - --int --shmget (key_t key, size_t size, int shmflg) -+int shmget(key_t key, size_t size, int flags) - { --#ifdef __ASSUME_DIRECT_SYSVIPC_SYSCALLS -- return INLINE_SYSCALL_CALL (shmget, key, size, shmflg, NULL); --#else -- return INLINE_SYSCALL_CALL (ipc, IPCOP_shmget, key, size, shmflg, NULL); --#endif -+ (void) flags; -+ -+ ashv_check_pid(); -+ -+ // Counter wrapping around at 15 bits. -+ static size_t shmem_counter = 0; -+ -+ if (!ashv_listening_thread_id) { -+ int sock = socket(AF_UNIX, SOCK_STREAM, 0); -+ if (!sock) { -+ DBG ("%s: cannot create UNIX socket: %s\n", __PRETTY_FUNCTION__, strerror(errno)); -+ errno = EINVAL; -+ return -1; -+ } -+ int i; -+ for (i = 0; i < 4096; i++) { -+ struct sockaddr_un addr; -+ int len; -+ memset (&addr, 0, sizeof(addr)); -+ addr.sun_family = AF_UNIX; -+ ashv_local_socket_id = (getpid() + i) & 0xffff; -+ sprintf(&addr.sun_path[1], ANDROID_SHMEM_SOCKNAME, ashv_local_socket_id); -+ len = sizeof(addr.sun_family) + strlen(&addr.sun_path[1]) + 1; -+ if (bind(sock, (struct sockaddr *)&addr, len) != 0) continue; -+ DBG("%s: bound UNIX socket %s in pid=%d\n", __PRETTY_FUNCTION__, addr.sun_path + 1, getpid()); -+ break; -+ } -+ if (i == 4096) { -+ DBG("%s: cannot bind UNIX socket, bailing out\n", __PRETTY_FUNCTION__); -+ ashv_local_socket_id = 0; -+ errno = ENOMEM; -+ return -1; -+ } -+ if (listen(sock, 4) != 0) { -+ DBG("%s: listen failed\n", __PRETTY_FUNCTION__); -+ errno = ENOMEM; -+ return -1; -+ } -+ int* socket_arg = malloc(sizeof(int)); -+ *socket_arg = sock; -+ pthread_create(&ashv_listening_thread_id, NULL, &ashv_thread_function, socket_arg); -+ } -+ -+ int shmid = -1; -+ -+ pthread_mutex_lock(&mutex); -+ char symlink_path[256]; -+ if (key != IPC_PRIVATE) { -+ // (1) Check if symlink exists telling us where to connect. -+ // (2) If so, try to connect and open. -+ // (3) If connected and opened, done. If connection refused -+ // take ownership of the key and create the symlink. -+ // (4) If no symlink, create it. -+ sprintf(symlink_path, ASHV_KEY_SYMLINK_PATH, key); -+ char path_buffer[256]; -+ char num_buffer[64]; -+ while (true) { -+ int path_length = readlink(symlink_path, path_buffer, sizeof(path_buffer) - 1); -+ if (path_length != -1) { -+ path_buffer[path_length] = '\0'; -+ int shmid = atoi(path_buffer); -+ if (shmid != 0) { -+ int idx = ashv_find_local_index(shmid); -+ -+ if (idx == -1) { -+ idx = ashv_read_remote_segment(shmid); -+ } -+ -+ if (idx != -1) { -+ pthread_mutex_unlock(&mutex); -+ return shmem[idx].id; -+ } -+ } -+ // TODO: Not sure we should try to remove previous owner if e.g. -+ // there was a tempporary failture to get a soket. Need to -+ // distinguish between why ashv_read_remote_segment failed. -+ unlink(symlink_path); -+ } -+ // Take ownership. -+ // TODO: HAndle error (out of resouces, no infinite loop) -+ if (shmid == -1) { -+ shmem_counter = (shmem_counter + 1) & 0x7fff; -+ shmid = ashv_shmid_from_counter(shmem_counter); -+ sprintf(num_buffer, "%d", shmid); -+ } -+ if (symlink(num_buffer, symlink_path) == 0) break; -+ } -+ } -+ -+ -+ int idx = shmem_amount; -+ char buf[256]; -+ sprintf(buf, ANDROID_SHMEM_SOCKNAME "-%d", ashv_local_socket_id, idx); -+ -+ shmem_amount++; -+ if (shmid == -1) { -+ shmem_counter = (shmem_counter + 1) & 0x7fff; -+ shmid = ashv_shmid_from_counter(shmem_counter); -+ } -+ -+ shmem = realloc(shmem, shmem_amount * sizeof(shmem_t)); -+ size = ROUND_UP(size, getpagesize()); -+ shmem[idx].size = size; -+ shmem[idx].descriptor = ashmem_create_region(buf, size); -+ shmem[idx].addr = NULL; -+ shmem[idx].id = shmid; -+ shmem[idx].markedForDeletion = false; -+ shmem[idx].key = key; -+ -+ if (shmem[idx].descriptor < 0) { -+ DBG("%s: ashmem_create_region() failed for size %zu: %s\n", __PRETTY_FUNCTION__, size, strerror(errno)); -+ shmem_amount --; -+ shmem = realloc(shmem, shmem_amount * sizeof(shmem_t)); -+ pthread_mutex_unlock (&mutex); -+ return -1; -+ } -+ //DBG("%s: ID %d shmid %x FD %d size %zu\n", __PRETTY_FUNCTION__, idx, shmid, shmem[idx].descriptor, shmem[idx].size); -+ /* -+ status = ashmem_set_prot_region (shmem[idx].descriptor, 0666); -+ if (status < 0) { -+ //DBG ("%s: ashmem_set_prot_region() failed for size %zu: %s %d\n", __PRETTY_FUNCTION__, size, strerror(status), status); -+ shmem_amount --; -+ shmem = realloc (shmem, shmem_amount * sizeof(shmem_t)); -+ pthread_mutex_unlock (&mutex); -+ return -1; -+ } -+ */ -+ /* -+ status = ashmem_pin_region (shmem[idx].descriptor, 0, shmem[idx].size); -+ if (status < 0) { -+ //DBG ("%s: ashmem_pin_region() failed for size %zu: %s %d\n", __PRETTY_FUNCTION__, size, strerror(status), status); -+ shmem_amount --; -+ shmem = realloc (shmem, shmem_amount * sizeof(shmem_t)); -+ pthread_mutex_unlock (&mutex); -+ return -1; -+ } -+ */ -+ pthread_mutex_unlock(&mutex); -+ -+ return shmid; - } diff --git a/gpkg/glibc/x86_64-arch-syscall.h.patch b/gpkg/glibc/x86_64-arch-syscall.h.patch index dad0d7314..ba5a57fc4 100644 --- a/gpkg/glibc/x86_64-arch-syscall.h.patch +++ b/gpkg/glibc/x86_64-arch-syscall.h.patch @@ -1,5 +1,5 @@ --- glibc-2.39/sysdeps/unix/sysv/linux/x86_64/64/arch-syscall.h 2024-01-31 03:34:58.000000000 +0300 -+++ glibc-2.39/sysdeps/unix/sysv/linux/x86_64/64/arch-syscall.h.patch 2024-02-26 11:18:50.095488238 +0300 ++++ glibc-2.39/sysdeps/unix/sysv/linux/x86_64/64/arch-syscall.h.patch 2024-04-10 00:15:50.550138796 +0300 @@ -1,6 +1,5 @@ /* AUTOGENERATED by update-syscall-lists.py. */ #define __NR__sysctl 156 @@ -26,7 +26,15 @@ #define __NR_connect 42 #define __NR_copy_file_range 326 #define __NR_creat 85 -@@ -51,7 +46,6 @@ +@@ -41,7 +36,6 @@ + #define __NR_epoll_ctl 233 + #define __NR_epoll_ctl_old 214 + #define __NR_epoll_pwait 281 +-#define __NR_epoll_pwait2 441 + #define __NR_epoll_wait 232 + #define __NR_epoll_wait_old 215 + #define __NR_eventfd 284 +@@ -51,7 +45,6 @@ #define __NR_exit 60 #define __NR_exit_group 231 #define __NR_faccessat 269 @@ -34,7 +42,7 @@ #define __NR_fadvise64 221 #define __NR_fallocate 285 #define __NR_fanotify_init 300 -@@ -59,7 +53,6 @@ +@@ -59,7 +52,6 @@ #define __NR_fchdir 81 #define __NR_fchmod 91 #define __NR_fchmodat 268 @@ -42,7 +50,7 @@ #define __NR_fchown 93 #define __NR_fchownat 260 #define __NR_fcntl 72 -@@ -82,7 +75,6 @@ +@@ -82,7 +74,6 @@ #define __NR_futex 202 #define __NR_futex_requeue 456 #define __NR_futex_wait 455 @@ -50,7 +58,7 @@ #define __NR_futex_wake 454 #define __NR_futimesat 261 #define __NR_get_kernel_syms 177 -@@ -100,7 +92,6 @@ +@@ -100,7 +91,6 @@ #define __NR_getitimer 36 #define __NR_getpeername 52 #define __NR_getpgid 121 @@ -58,7 +66,7 @@ #define __NR_getpid 39 #define __NR_getpmsg 181 #define __NR_getppid 110 -@@ -142,7 +133,6 @@ +@@ -142,7 +132,6 @@ #define __NR_keyctl 250 #define __NR_kill 62 #define __NR_landlock_add_rule 445 @@ -66,7 +74,7 @@ #define __NR_landlock_restrict_self 446 #define __NR_lchown 94 #define __NR_lgetxattr 192 -@@ -207,7 +197,6 @@ +@@ -207,7 +196,6 @@ #define __NR_personality 135 #define __NR_pidfd_getfd 438 #define __NR_pidfd_open 434 @@ -74,7 +82,7 @@ #define __NR_pipe 22 #define __NR_pipe2 293 #define __NR_pivot_root 155 -@@ -250,8 +239,6 @@ +@@ -250,8 +238,6 @@ #define __NR_renameat2 316 #define __NR_request_key 249 #define __NR_restart_syscall 219 @@ -83,7 +91,7 @@ #define __NR_rt_sigaction 13 #define __NR_rt_sigpending 127 #define __NR_rt_sigprocmask 14 -@@ -285,13 +272,9 @@ +@@ -285,13 +271,9 @@ #define __NR_sendto 44 #define __NR_set_mempolicy 238 #define __NR_set_mempolicy_home_node 450 @@ -97,7 +105,7 @@ #define __NR_setgroups 116 #define __NR_sethostname 170 #define __NR_setitimer 38 -@@ -299,14 +282,12 @@ +@@ -299,14 +281,12 @@ #define __NR_setpgid 109 #define __NR_setpriority 141 #define __NR_setregid 114 @@ -112,7 +120,7 @@ #define __NR_setxattr 188 #define __NR_shmat 30 #define __NR_shmctl 31 -@@ -321,10 +302,8 @@ +@@ -321,10 +301,8 @@ #define __NR_splice 275 #define __NR_stat 4 #define __NR_statfs 137