this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Implement _set_user_dir_suffix

Please, please, *please* take a look at my comments in _libcoreservices_fork_child! If there are ever any issues with threading and forking, remember to take a look at this code

+104 -12
+41 -12
src/libsystem_coreservices/dirhelper.c
··· 2 2 #include <stddef.h> 3 3 #include <stdlib.h> 4 4 #include <stdio.h> 5 + #include <pthread.h> 6 + #include "dirhelper_priv.h" 7 + 8 + static const char* dir_suffix = NULL; 9 + static pthread_mutex_t dir_suffix_lock = PTHREAD_MUTEX_INITIALIZER; 5 10 6 11 void _dirhelper_fork_child(void) 7 12 { ··· 9 14 10 15 void _libcoreservices_fork_child(void) 11 16 { 17 + /** 18 + * my god, threading is *incredibly* tricky when mixed with `fork()`! 19 + * and the POSIX spec is so unclear about the behavior of mutexes when `fork()`ing 20 + * and the macOS docs are no help 21 + * 22 + * the *only* viable resources i found was this commit in jemalloc: 23 + * https://github.com/jemalloc/jemalloc/commit/0a4f5a7eea5e42292cea95fd30a88201c8d4a1ca 24 + * 25 + * basically, it *seems* that it's fine to reinitialize mutexes in the child---on macOS at least 26 + * but i really wish the POSIX spec or macOS docs mentioned this kind of stuff! 27 + * 28 + * undefined behavior is really the **worst** behavior 29 + */ 30 + dir_suffix_lock = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER; 31 + 32 + /** 33 + * i checked on a real macOS system and confirmed this behavior: 34 + * `fork()` does NOT reset the directory suffix 35 + */ 36 + //dir_suffix = NULL; 12 37 } 13 38 14 - enum { 15 - DIRHELPER_USER_LOCAL = 0, 16 - DIRHELPER_USER_LOCAL_TEMP, 17 - DIRHELPER_USER_LOCAL_CACHE 18 - }; 19 - 20 39 char* _dirhelper(int which, char* p, size_t limit) 21 40 { 41 + pthread_mutex_lock(&dir_suffix_lock); 22 42 switch (which) 23 43 { 24 44 case DIRHELPER_USER_LOCAL: ··· 27 47 28 48 home = getenv("HOME"); 29 49 if (home == NULL) 30 - home = "/"; 50 + home = ""; 31 51 32 - strlcpy(p, home, limit); 52 + snprintf(p, limit, "%s/%s%s", home, dir_suffix ? dir_suffix : "", dir_suffix ? "/" : ""); 33 53 return p; 34 54 } 35 55 case DIRHELPER_USER_LOCAL_TEMP: ··· 40 60 if (tmp == NULL) 41 61 tmp = "/tmp"; 42 62 43 - strlcpy(p, tmp, limit); 63 + snprintf(p, limit, "%s/%s%s", tmp, dir_suffix ? dir_suffix : "", dir_suffix ? "/" : ""); 44 64 return p; 45 65 } 46 66 case DIRHELPER_USER_LOCAL_CACHE: ··· 49 69 50 70 home = getenv("HOME"); 51 71 if (home != NULL) 52 - snprintf(p, limit, "%s/.cache", home); 72 + snprintf(p, limit, "%s/.cache/%s%s", home, dir_suffix ? dir_suffix : "", dir_suffix ? "/" : ""); 53 73 else 54 - strlcpy(p, "/tmp", limit); 74 + snprintf(p, limit, "/tmp/%s%s", dir_suffix ? dir_suffix : "", dir_suffix ? "/" : ""); 55 75 56 76 return p; 57 77 } 58 78 default: 59 - return NULL; 79 + p = NULL; 60 80 } 81 + pthread_mutex_unlock(&dir_suffix_lock); 82 + return p; 61 83 } 62 84 85 + int _set_user_dir_suffix(const char* suffix) { 86 + pthread_mutex_lock(&dir_suffix_lock); 87 + dir_suffix = suffix; 88 + pthread_mutex_unlock(&dir_suffix_lock); 89 + return 0; 90 + }; 91 +
+63
src/libsystem_coreservices/dirhelper_priv.h
··· 1 + /* 2 + * Copyright (c) 2006, 2007, 2010 Apple Inc. All rights reserved. 3 + * 4 + * @APPLE_LICENSE_HEADER_START@ 5 + * 6 + * This file contains Original Code and/or Modifications of Original Code 7 + * as defined in and that are subject to the Apple Public Source License 8 + * Version 2.0 (the 'License'). You may not use this file except in 9 + * compliance with the License. Please obtain a copy of the License at 10 + * http://www.opensource.apple.com/apsl/ and read it before using this 11 + * file. 12 + * 13 + * The Original Code and all software distributed under the License are 14 + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 + * Please see the License for the specific language governing rights and 19 + * limitations under the License. 20 + * 21 + * @APPLE_LICENSE_HEADER_END@ 22 + */ 23 + /** 24 + * 25 + * Modified for Darling 26 + * 27 + */ 28 + 29 + #ifndef _DIRHELPER_PRIV_H_ 30 + #define _DIRHELPER_PRIV_H_ 31 + 32 + #include <sys/cdefs.h> 33 + #include <sys/types.h> 34 + 35 + #define VAR_FOLDERS_PATH "/var/folders/" 36 + 37 + #define DIRHELPER_BOOTSTRAP_NAME "com.apple.bsd.dirhelper" 38 + #define DIRHELPER_CACHE_STR "C/" 39 + #define DIRHELPER_TEMP_STR "T/" 40 + #define DIRHELPER_TOP_STR "0/" 41 + #define DIRHELPER_ENV_USER_DIR_SUFFIX "DIRHELPER_USER_DIR_SUFFIX" 42 + 43 + typedef enum { 44 + DIRHELPER_USER_LOCAL = 0, 45 + DIRHELPER_USER_LOCAL_TEMP, 46 + DIRHELPER_USER_LOCAL_CACHE, 47 + DIRHELPER_USER_LOCAL_LAST = DIRHELPER_USER_LOCAL_CACHE 48 + } dirhelper_which_t; 49 + 50 + __BEGIN_DECLS 51 + char *__user_local_dirname(uid_t uid, dirhelper_which_t which, char *path, 52 + size_t pathlen); 53 + char *__user_local_mkdir_p(char *path); 54 + 55 + __attribute__((visibility("hidden"), __format__(__printf__,1,2))) 56 + void _setcrashlogmessage(const char *fmt, ...); 57 + #define setcrashlogmessage(fmt, ...) _setcrashlogmessage("%s: %u: " fmt, __func__, __LINE__, ##__VA_ARGS__) 58 + 59 + int _set_user_dir_suffix(const char* suffix); 60 + 61 + __END_DECLS 62 + 63 + #endif /* _DIRHELPER_PRIV_H_ */