From 5d2fc0ed544af1fbb6ae7899a968bd280cab518a Mon Sep 17 00:00:00 2001 From: William Casarin Date: Sun, 1 Sep 2024 06:20:11 -0700 Subject: [PATCH] lmdb: patch semaphore names to use group container prefix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is an attempt to fix various issues when acquiring a IPC semaphore on iOS See: https://github.com/damus-io/damus/issues/2323#issuecomment-2323181204 Running this patch gives us these names: mdb_env_setup_locks: using semnames 'group.com.damus/MDBrwDDi_FHxD' (29), 'group.com.damus/MDBwwDDi_FHxD' (29) From old Apple docs: > IPC and POSIX Semaphores and Shared Memory > > Normally, sandboxed apps cannot use Mach IPC, POSIX semaphores and > shared memory, or UNIX domain sockets (usefully). However, by specifying > an entitlement that requests membership in an application group, an app > can use these technologies to communicate with other members of that > application group. > > Note: System V semaphores are not supported in sandboxed apps. > > UNIX domain sockets are straightforward; they work just like any other > file. > > Any semaphore or Mach port that you wish to access within a sandboxed > app must be named according to a special convention: > > POSIX semaphores and shared memory names must begin with the application > group identifier, followed by a slash (/), followed by a name of your > choosing. > > Mach port names must begin with the application group identifier, > followed by a period (.), followed by a name of your choosing. > > For example, if your application group’s name is > Z123456789.com.example.app-group, you might create two semaphores named > Z123456789.myappgroup/rdyllwflg and Z123456789.myappgroup/bluwhtflg. You > might create a Mach port named > Z123456789.com.example.app-group.Port_of_Kobe. > > Note: The maximum length of a POSIX semaphore name is only 31 bytes, so > if you need to use POSIX semaphores, you should keep your app group > names short. Link: https://github.com/damus-io/damus/issues/2323#issuecomment-2323305949 Signed-off-by: William Casarin --- damus.xcodeproj/project.pbxproj | 7 ++++++- nostrdb/mdb.c | 13 +++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj index adfb6c9f..9b096ad4 100644 --- a/damus.xcodeproj/project.pbxproj +++ b/damus.xcodeproj/project.pbxproj @@ -5034,6 +5034,8 @@ GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", + "MDB_SHORT_SEMNAMES=1", + "MDB_SEM_NAME_PREFIX=\"group.com.damus\"", "$(inherited)", ); GCC_WARN_64_TO_32_BIT_CONVERSION = YES; @@ -5097,7 +5099,10 @@ ENABLE_USER_SCRIPT_SANDBOXING = NO; GCC_C_LANGUAGE_STANDARD = gnu11; GCC_NO_COMMON_BLOCKS = YES; - "GCC_PREPROCESSOR_DEFINITIONS[arch=*]" = ""; + GCC_PREPROCESSOR_DEFINITIONS = ( + "MDB_SHORT_SEMNAMES=1", + "MDB_SEM_NAME_PREFIX=\"group.com.damus\"", + ); GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; diff --git a/nostrdb/mdb.c b/nostrdb/mdb.c index deb67796..c9614a30 100644 --- a/nostrdb/mdb.c +++ b/nostrdb/mdb.c @@ -4893,8 +4893,17 @@ mdb_env_setup_locks(MDB_env *env, MDB_name *fname, int mode, int *excl) #ifdef MDB_SHORT_SEMNAMES encbuf[9] = '\0'; /* drop name from 15 chars to 14 chars */ #endif - sprintf(env->me_txns->mti_rmname, "/MDBr%s", encbuf); - sprintf(env->me_txns->mti_wmname, "/MDBw%s", encbuf); + +#define DEF_STR(x) #x +#define DEF_TO_STRING(x) DEF_STR(x) + sprintf(env->me_txns->mti_rmname, DEF_TO_STRING(MDB_SEM_NAME_PREFIX) "/MDBr%s", encbuf); + sprintf(env->me_txns->mti_wmname, DEF_TO_STRING(MDB_SEM_NAME_PREFIX) "/MDBw%s", encbuf); +#undef DEF_STR +#undef DEF_TO_STRING + + printf("mdb_env_setup_locks: using semnames '%s' (%d), '%s' (%d)\n", + env->me_txns->mti_rmname, strlen(env->me_txns->mti_rmname), + env->me_txns->mti_wmname, strlen(env->me_txns->mti_wmname)); /* Clean up after a previous run, if needed: Try to * remove both semaphores before doing anything else. */