sysvipc: implement missing semctl commands for semaphores#343
Open
hernandevelop wants to merge 2 commits intotermux:masterfrom
Open
sysvipc: implement missing semctl commands for semaphores#343hernandevelop wants to merge 2 commits intotermux:masterfrom
hernandevelop wants to merge 2 commits intotermux:masterfrom
Conversation
…T, GETZCNT, GETPID) semctl for semaphores was missing several commands that are commonly used by applications to query semaphore state. Notably IPC_STAT was stubbed out with #if 0 and fell through to the default case returning -EINVAL, causing applications that verify semaphore existence via IPC_STAT (checking sem_otime) to incorrectly conclude the semaphore was destroyed. Changes: - Add SysVIpcSemidDs struct to sysvipc_sys.h (matching kernel layout) - Add stats field to SysVIpcSemaphore for tracking metadata - Implement IPC_STAT, SEM_STAT, SEM_STAT_ANY: return semaphore metadata - Implement IPC_SET: allow updating permissions - Implement GETPID, GETNCNT, GETZCNT: return waiter counts - Initialize sem_ctime and sem_perm.mode on semget - Update sem_otime on successful semop - Update sem_ctime on SETVAL Tested with JetBrains Toolbox which relies on IPC_STAT to verify semaphore initialization state.
The SHM helper process now creates an abstract Unix socket listener using Termux:X11's protocol (\0/dev/shm/<socket_id>), and generates shmids in Termux:X11-compatible format (socket_id << 16 | counter). When a process inside proot creates a SHM segment and sends the shmid to the X11 server via XShmAttach, the server can now find and attach the segment through the bridge socket, receiving the ashmem fd via SCM_RIGHTS. This eliminates the need for workarounds like: - GSK_RENDERER=cairo (disabling GL rendering in GTK4) - termux-x11 -extension MIT-SHM (disabling SHM entirely) - LD_PRELOAD with external shm_compat.so Changes: - sysvipc_shm.c: Add X11 bridge thread in helper process that serves the Termux:X11 fd-passing protocol. Register segments on allocation, unregister on free. Send bridge socket_id to proot main process via existing pipe. Refactor helper launch into sysvipc_shm_ensure_helper() so socket_id is available before generating shmids. - sysvipc_internal.h: Add x11_shmid field to SysVIpcSharedMem struct. Add LOOKUP_SHM_OBJECT macro for x11_shmid-based lookup. - GNUmakefile: Link with -lpthread for bridge thread. Tested with GTK4 file-roller (MIT-SHM) and Cinnamon desktop session on Termux:X11 with proot-distro.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The semaphore implementation in
sysvipc_sem.cwas missing severalsemctlcommands, most notablyIPC_STATwhich was stubbed out with#if 0(containing a copy-paste from message queues that referencedqueue->stats). Any unimplemented command fell through todefault: return -EINVAL.This caused applications that verify semaphore state via
semctl(semid, 0, IPC_STAT, &buf)to receiveEINVALand incorrectly conclude the semaphore was destroyed.Changes
sysvipc_sys.h: AddSysVIpcSemidDsstruct (matching kernelsemid_dslayout, consistent with existingSysVIpcShmidDs)sysvipc_internal.h: Addstatsfield toSysVIpcSemaphoresysvipc_sem.c:IPC_STAT/SEM_STAT/SEM_STAT_ANY: return semaphore metadata to userspaceIPC_SET: allow updating permissionsGETPID,GETNCNT,GETZCNT: return waiter countssem_ctimeandsem_perm.modeonsemgetsem_otimeon successfulsemopsem_ctimeonSETVALContext
I ran into this while trying to run JetBrains Toolbox inside proot-distro on Android. Toolbox uses SysV semaphores for single-instance locking and calls
IPC_STATto checksem_otimeafter initialization. The missing implementation caused it to always fail with "Semaphore doesn't exist anymore".The fix follows the same patterns already used in
sysvipc_shm.c(which has a workingIPC_STATimplementation) andsysvipc_msg.c.Testing
semget→semctl SETVAL→semop→semctl IPC_STATsequence works end-to-end