mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 12:56:06 +00:00
cleanup (#764)
This commit is contained in:
1 parent
4163b31b02
commit
83ec5708ed
852 files changed
+4844
-46144
No files matched your search
@@ -1,14 +1,3 @@
|
||||
/* System V semaphores and shared memory for programs in the Linux runtime.
|
||||
*
|
||||
* Android kernels are built without System V IPC, so semget/shmget fail with ENOSYS. Steam's tier0
|
||||
* creates its client-instance semaphore with semget and gives up when that fails. This library is
|
||||
* LD_PRELOADed into the session and keeps each object in a file under /dev/shm (a bound directory
|
||||
* the whole session shares): a semaphore set is an array of counters waited on with futexes, a
|
||||
* segment is a plain file that shmat maps. Objects are found by the inode of their file, which is
|
||||
* what the id is. semop applies its operations one by one (no all-or-nothing across a set) and
|
||||
* SEM_UNDO is accepted but not performed - enough for the client, which uses single-counter sets.
|
||||
* A message queue is a table of slots in its file: gamescope hands mangoapp (Deck mode's performance
|
||||
* overlay) each frame's timings through one. */
|
||||
#define _GNU_SOURCE
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
@@ -34,7 +23,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#define DIR_DEFAULT "/dev/shm/wnsysv"
|
||||
#define SEM_MAGIC 0x574e5345u /* "WNSE" */
|
||||
#define SEM_MAGIC 0x574e5345u
|
||||
#define MAX_OBJECTS 64
|
||||
|
||||
struct sem_header {
|
||||
@@ -43,15 +32,11 @@ struct sem_header {
|
||||
uint32_t key;
|
||||
uint32_t mode;
|
||||
int32_t creator_pid;
|
||||
/* What IPC_STAT reports as sem_otime / sem_ctime (seconds). The usual race-free init has the
|
||||
* creator semop the new set once, and every other process wait until sem_otime is non-zero;
|
||||
* SteamVR's web helper spins on exactly that. */
|
||||
_Atomic uint32_t otime;
|
||||
_Atomic uint32_t ctime;
|
||||
int32_t pad[9];
|
||||
};
|
||||
_Static_assert(sizeof(struct sem_header) == 64, "semaphore files written by older sessions keep their layout");
|
||||
/* The counters follow the header, one 32-bit word each (a futex word). */
|
||||
|
||||
struct sem_map { int id; struct sem_header *hdr; size_t len; };
|
||||
struct shm_map { void *addr; size_t len; int id; };
|
||||
@@ -76,7 +61,6 @@ static int object_id(int fd) {
|
||||
return (int)(st.st_ino & 0x7fffffff);
|
||||
}
|
||||
|
||||
/* Open the object file of kind ("sem"/"shm") for key, creating it as flags ask. */
|
||||
static int open_object(const char *kind, key_t key, int flags, int *created) {
|
||||
char path[PATH_MAX];
|
||||
int oflags = O_RDWR | O_CLOEXEC;
|
||||
@@ -98,7 +82,6 @@ static int open_object(const char *kind, key_t key, int flags, int *created) {
|
||||
return fd;
|
||||
}
|
||||
|
||||
/* The object file for an id, found by inode. */
|
||||
static int open_by_id(const char *kind, int id) {
|
||||
DIR *d = opendir(object_dir());
|
||||
if (!d) return -1;
|
||||
@@ -141,7 +124,6 @@ static long futex(void *addr, int op, int val, const struct timespec *timeout) {
|
||||
return syscall(SYS_futex, addr, op, val, timeout, NULL, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ semaphores */
|
||||
|
||||
static struct sem_map *sem_lookup(int id) {
|
||||
for (int i = 0; i < MAX_OBJECTS; i++)
|
||||
@@ -227,7 +209,7 @@ int semtimedop(int semid, struct sembuf *sops, size_t nsops, const struct timesp
|
||||
} else if (sem_wait_op(w, sops[i].sem_op, sops[i].sem_flg, timeout) != 0) {
|
||||
return -1;
|
||||
}
|
||||
if (sops[i].sem_op < 0) futex(w, FUTEX_WAKE, INT_MAX, NULL); /* zero-waiters see the change */
|
||||
if (sops[i].sem_op < 0) futex(w, FUTEX_WAKE, INT_MAX, NULL);
|
||||
}
|
||||
atomic_store(&h->otime, (uint32_t)time(NULL));
|
||||
return 0;
|
||||
@@ -293,11 +275,7 @@ int semctl(int semid, int semnum, int cmd, ...) {
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ shared memory */
|
||||
|
||||
/* Linux keeps a removed segment attachable until the last detach, which X clients rely on: they
|
||||
* remove the segment right after asking the server to attach it. A removed segment is renamed out
|
||||
* of key lookup instead, and removed for good once it has been out of use for a while. */
|
||||
static int remove_shm(int id) {
|
||||
DIR *d = opendir(object_dir());
|
||||
if (!d) return -1;
|
||||
@@ -411,18 +389,18 @@ int shmctl(int shmid, int cmd, struct shmid_ds *buf) {
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ message queues */
|
||||
|
||||
/* Only what gamescope and mangoapp use: msgtyp 0 or > 0, IPC_NOWAIT and IPC_RMID. */
|
||||
#define MSG_MAGIC 0x574e4d51u /* "WNMQ" */
|
||||
|
||||
|
||||
#define MSG_MAGIC 0x574e4d51u
|
||||
#define MSG_SLOTS 32
|
||||
|
||||
struct msg_queue {
|
||||
uint32_t magic;
|
||||
_Atomic int32_t lock; /* 0 free, else the holder's pid */
|
||||
_Atomic int32_t changed; /* bumped on every send and receive; waiters sleep on it */
|
||||
_Atomic int32_t lock;
|
||||
_Atomic int32_t changed;
|
||||
uint64_t next_seq;
|
||||
struct { uint64_t seq; long mtype; size_t len; char data[2048]; } slot[MSG_SLOTS]; /* seq 0: free */
|
||||
struct { uint64_t seq; long mtype; size_t len; char data[2048]; } slot[MSG_SLOTS];
|
||||
};
|
||||
|
||||
static struct msg_queue *msg_map(int id) {
|
||||
@@ -437,7 +415,6 @@ static struct msg_queue *msg_map(int id) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* A process killed while holding the lock (Android ends sessions without warning) loses it. */
|
||||
static void msg_lock(struct msg_queue *q) {
|
||||
for (int32_t owner = 0; !atomic_compare_exchange_strong(&q->lock, &owner, getpid()); owner = 0) {
|
||||
if (kill(owner, 0) != 0 && errno == ESRCH) atomic_compare_exchange_strong(&q->lock, &owner, 0);
|
||||
@@ -531,12 +508,6 @@ int msgctl(int msqid, int cmd, struct msqid_ds *buf) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
* fork() carries over only the calling thread, so a lock another thread was holding at that
|
||||
* instant stays held in the child by a thread that is not there to release it. Taking it before
|
||||
* the fork makes the copy consistent; the parent then unlocks it and the child, whose one thread
|
||||
* never locked it, gets a fresh one. (WinNative 79aa7f68.)
|
||||
*/
|
||||
static void sysv_lock_before_fork(void) { pthread_mutex_lock(&g_lock); }
|
||||
static void sysv_unlock_after_fork(void) { pthread_mutex_unlock(&g_lock); }
|
||||
static void sysv_reset_after_fork(void) { pthread_mutex_init(&g_lock, NULL); }
|
||||
|
||||
Reference in new issue
Block a user