2 * Copyright (c) 2011 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 Copyright (c) 1999-2011, Apple Inc. All rights reserved.
26 Responsibility: Tony Parker
29 #include "CFInternal.h"
30 #include <CoreFoundation/CFPriv.h>
31 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
38 #include <crt_externs.h>
39 #include <mach-o/dyld.h>
42 #if DEPLOYMENT_TARGET_WINDOWS
47 #define getcwd _NS_getcwd
51 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
52 #define kCFPlatformInterfaceStringEncoding kCFStringEncodingUTF8
54 #define kCFPlatformInterfaceStringEncoding CFStringGetSystemEncoding()
57 static CFStringRef
_CFUserName(void);
59 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
60 // CoreGraphics and LaunchServices are only projects (1 Dec 2006) that use these
61 char **_CFArgv(void) { return *_NSGetArgv(); }
62 int _CFArgc(void) { return *_NSGetArgc(); }
66 __private_extern__ Boolean
_CFGetCurrentDirectory(char *path
, int maxlen
) {
67 return getcwd(path
, maxlen
) != NULL
;
71 static Boolean __CFIsCFM
= false;
73 // If called super early, we just return false
74 __private_extern__ Boolean
_CFIsCFM(void) {
79 #if DEPLOYMENT_TARGET_WINDOWS
86 #if DEPLOYMENT_TARGET_WINDOWS
87 // Returns the path to the CF DLL, which we can then use to find resources like char sets
88 bool bDllPathCached
= false;
89 __private_extern__
const wchar_t *_CFDLLPath(void) {
90 static wchar_t cachedPath
[MAX_PATH
+1];
92 if (!bDllPathCached
) {
94 // might be nice to get this from the project file at some point
95 wchar_t *DLLFileName
= L
"CoreFoundation_debug.dll";
97 wchar_t *DLLFileName
= L
"CoreFoundation.dll";
99 HMODULE ourModule
= GetModuleHandleW(DLLFileName
);
101 CFAssert(ourModule
, __kCFLogAssertion
, "GetModuleHandle failed");
103 DWORD wResult
= GetModuleFileNameW(ourModule
, cachedPath
, MAX_PATH
+1);
104 CFAssert1(wResult
> 0, __kCFLogAssertion
, "GetModuleFileName failed: %d", GetLastError());
105 CFAssert1(wResult
< MAX_PATH
+1, __kCFLogAssertion
, "GetModuleFileName result truncated: %s", cachedPath
);
107 // strip off last component, the DLL name
109 for (idx
= wResult
- 1; idx
; idx
--) {
110 if ('\\' == cachedPath
[idx
]) {
111 cachedPath
[idx
] = '\0';
115 bDllPathCached
= true;
121 static const char *__CFProcessPath
= NULL
;
122 static const char *__CFprogname
= NULL
;
124 const char **_CFGetProgname(void) {
126 _CFProcessPath(); // sets up __CFprogname as a side-effect
127 return &__CFprogname
;
130 const char **_CFGetProcessPath(void) {
131 if (!__CFProcessPath
)
132 _CFProcessPath(); // sets up __CFProcessPath as a side-effect
133 return &__CFProcessPath
;
136 #if DEPLOYMENT_TARGET_WINDOWS
137 const char *_CFProcessPath(void) {
138 if (__CFProcessPath
) return __CFProcessPath
;
139 wchar_t buf
[CFMaxPathSize
] = {0};
140 DWORD rlen
= GetModuleFileNameW(NULL
, buf
, sizeof(buf
) / sizeof(buf
[0]));
142 char asciiBuf
[CFMaxPathSize
] = {0};
143 int res
= WideCharToMultiByte(CP_UTF8
, 0, buf
, rlen
, asciiBuf
, sizeof(asciiBuf
) / sizeof(asciiBuf
[0]), NULL
, NULL
);
145 __CFProcessPath
= strdup(asciiBuf
);
146 __CFprogname
= strrchr(__CFProcessPath
, PATH_SEP
);
147 __CFprogname
= (__CFprogname
? __CFprogname
+ 1 : __CFProcessPath
);
150 if (!__CFProcessPath
) {
151 __CFProcessPath
= "";
152 __CFprogname
= __CFProcessPath
;
154 return __CFProcessPath
;
158 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
159 const char *_CFProcessPath(void) {
160 if (__CFProcessPath
) return __CFProcessPath
;
161 #if DEPLOYMENT_TARGET_MACOSX
163 const char *path
= (char *)__CFgetenv("CFProcessPath");
165 __CFProcessPath
= strdup(path
);
166 __CFprogname
= strrchr(__CFProcessPath
, PATH_SEP
);
167 __CFprogname
= (__CFprogname
? __CFprogname
+ 1 : __CFProcessPath
);
168 return __CFProcessPath
;
172 uint32_t size
= CFMaxPathSize
;
174 if (0 == _NSGetExecutablePath(buffer
, &size
)) {
176 size_t len
= strlen(buffer
);
177 if (12 <= len
&& 0 == strcmp("LaunchCFMApp", buffer
+ len
- 12)) {
178 struct stat exec
, lcfm
;
179 const char *launchcfm
= "/System/Library/Frameworks/Carbon.framework/Versions/Current/Support/LaunchCFMApp";
180 if (0 == stat(launchcfm
, &lcfm
) && 0 == stat(buffer
, &exec
) && (lcfm
.st_dev
== exec
.st_dev
) && (lcfm
.st_ino
== exec
.st_ino
)) {
181 // Executable is LaunchCFMApp, take special action
183 if ((*_NSGetArgv())[1] && '/' == *((*_NSGetArgv())[1])) {
184 strlcpy(buffer
, (*_NSGetArgv())[1], sizeof(buffer
));
189 __CFProcessPath
= strdup(buffer
);
190 __CFprogname
= strrchr(__CFProcessPath
, PATH_SEP
);
191 __CFprogname
= (__CFprogname
? __CFprogname
+ 1 : __CFProcessPath
);
193 if (!__CFProcessPath
) {
194 __CFProcessPath
= "";
195 __CFprogname
= __CFProcessPath
;
197 return __CFProcessPath
;
201 #if DEPLOYMENT_TARGET_LINUX
204 const char *_CFProcessPath(void) {
205 if (__CFProcessPath
) return __CFProcessPath
;
206 char buf
[CFMaxPathSize
+ 1];
208 ssize_t res
= readlink("/proc/self/exe", buf
, CFMaxPathSize
);
210 // null terminate, readlink does not
212 __CFProcessPath
= strdup(buf
);
213 __CFprogname
= strrchr(__CFProcessPath
, PATH_SEP
);
214 __CFprogname
= (__CFprogname
? __CFprogname
+ 1 : __CFProcessPath
);
216 __CFProcessPath
= "";
217 __CFprogname
= __CFProcessPath
;
219 return __CFProcessPath
;
223 __private_extern__ CFStringRef
_CFProcessNameString(void) {
224 static CFStringRef __CFProcessNameString
= NULL
;
225 if (!__CFProcessNameString
) {
226 const char *processName
= *_CFGetProgname();
227 if (!processName
) processName
= "";
228 CFStringRef newStr
= CFStringCreateWithCString(kCFAllocatorSystemDefault
, processName
, kCFPlatformInterfaceStringEncoding
);
229 if (!OSAtomicCompareAndSwapPtrBarrier(NULL
, (void *) newStr
, (void * volatile *)& __CFProcessNameString
)) {
230 CFRelease(newStr
); // someone else made the assignment, so just release the extra string.
233 return __CFProcessNameString
;
236 static CFStringRef __CFUserName
= NULL
;
237 static CFSpinLock_t __CFPlatformCacheLock
= CFSpinLockInit
;
239 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
243 static CFURLRef __CFHomeDirectory
= NULL
;
244 static uint32_t __CFEUID
= -1;
245 static uint32_t __CFUID
= -1;
247 static CFURLRef
_CFCopyHomeDirURLForUser(struct passwd
*upwd
) { // __CFPlatformCacheLock must be locked on entry and will be on exit
248 CFURLRef home
= NULL
;
250 const char *path
= __CFgetenv("CFFIXED_USER_HOME");
252 home
= CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault
, (uint8_t *)path
, strlen(path
), true);
256 if (upwd
&& upwd
->pw_dir
) {
257 home
= CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault
, (uint8_t *)upwd
->pw_dir
, strlen(upwd
->pw_dir
), true);
263 static void _CFUpdateUserInfo(void) { // __CFPlatformCacheLock must be locked on entry and will be on exit
266 __CFEUID
= geteuid();
268 if (__CFHomeDirectory
) CFRelease(__CFHomeDirectory
);
269 __CFHomeDirectory
= NULL
;
270 if (__CFUserName
) CFRelease(__CFUserName
);
273 upwd
= getpwuid(__CFEUID
? __CFEUID
: __CFUID
);
274 __CFHomeDirectory
= _CFCopyHomeDirURLForUser(upwd
);
275 if (!__CFHomeDirectory
) {
276 const char *cpath
= __CFgetenv("HOME");
278 __CFHomeDirectory
= CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault
, (uint8_t *)cpath
, strlen(cpath
), true);
282 // This implies that UserManager stores directory info in CString
283 // rather than FileSystemRep. Perhaps this is wrong & we should
284 // expect NeXTSTEP encodings. A great test of our localized system would
285 // be to have a user "O-umlat z e r". XXX
286 if (upwd
&& upwd
->pw_name
) {
287 __CFUserName
= CFStringCreateWithCString(kCFAllocatorSystemDefault
, upwd
->pw_name
, kCFPlatformInterfaceStringEncoding
);
289 const char *cuser
= __CFgetenv("USER");
291 __CFUserName
= CFStringCreateWithCString(kCFAllocatorSystemDefault
, cuser
, kCFPlatformInterfaceStringEncoding
);
296 static CFURLRef
_CFCreateHomeDirectoryURLForUser(CFStringRef uName
) { // __CFPlatformCacheLock must be locked on entry and will be on exit
297 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
299 if (geteuid() != __CFEUID
|| getuid() != __CFUID
|| !__CFHomeDirectory
)
301 if (__CFHomeDirectory
) CFRetain(__CFHomeDirectory
);
302 return __CFHomeDirectory
;
304 struct passwd
*upwd
= NULL
;
305 char buf
[128], *user
;
306 SInt32 len
= CFStringGetLength(uName
), size
= CFStringGetMaximumSizeForEncoding(len
, kCFPlatformInterfaceStringEncoding
);
311 user
= CFAllocatorAllocate(kCFAllocatorSystemDefault
, size
+1, 0);
312 if (__CFOASafe
) __CFSetLastAllocationEventName(user
, "CFUtilities (temp)");
314 if (CFStringGetBytes(uName
, CFRangeMake(0, len
), kCFPlatformInterfaceStringEncoding
, 0, true, (uint8_t *)user
, size
, &usedSize
) == len
) {
315 user
[usedSize
] = '\0';
316 upwd
= getpwnam(user
);
319 CFAllocatorDeallocate(kCFAllocatorSystemDefault
, user
);
321 return _CFCopyHomeDirURLForUser(upwd
);
323 #elif DEPLOYMENT_TARGET_WINDOWS
324 // This code can only get the directory for the current user
325 if (uName
&& !CFEqual(uName
, _CFUserName())) {
326 CFLog(kCFLogLevelError
, CFSTR("CFCopyHomeDirectoryURLForUser(): Unable to get home directory for other user"));
330 CFURLRef retVal
= NULL
;
332 CFStringRef str
= NULL
;
334 UniChar pathChars
[MAX_PATH
];
335 if (S_OK
== SHGetFolderPathW(NULL
, CSIDL_PROFILE
, NULL
, SHGFP_TYPE_CURRENT
, (wchar_t *)pathChars
)) {
336 len
= (CFIndex
)wcslen((wchar_t *)pathChars
);
337 str
= CFStringCreateWithCharacters(kCFAllocatorSystemDefault
, pathChars
, len
);
338 retVal
= CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault
, str
, kCFURLWindowsPathStyle
, true);
343 // Fall back to environment variable, but this will not be unicode compatible
344 const char *cpath
= __CFgetenv("HOMEPATH");
345 const char *cdrive
= __CFgetenv("HOMEDRIVE");
346 if (cdrive
&& cpath
) {
347 char fullPath
[CFMaxPathSize
];
348 strlcpy(fullPath
, cdrive
, sizeof(fullPath
));
349 strlcat(fullPath
, cpath
, sizeof(fullPath
));
350 str
= CFStringCreateWithCString(kCFAllocatorSystemDefault
, fullPath
, kCFPlatformInterfaceStringEncoding
);
351 retVal
= CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault
, str
, kCFURLWindowsPathStyle
, true);
357 // Last resort: We have to get "some" directory location, so fall-back to the processes current directory.
358 UniChar currDir
[MAX_PATH
];
359 DWORD dwChars
= GetCurrentDirectoryW(MAX_PATH
+ 1, (wchar_t *)currDir
);
361 len
= (CFIndex
)wcslen((wchar_t *)currDir
);
362 str
= CFStringCreateWithCharacters(kCFAllocatorDefault
, currDir
, len
);
363 retVal
= CFURLCreateWithFileSystemPath(NULL
, str
, kCFURLWindowsPathStyle
, true);
368 // We could do more here (as in KB Article Q101507). If that article is to be believed, we should only run into this case on Win95, or through user error.
369 CFStringRef testPath
= CFURLCopyFileSystemPath(retVal
, kCFURLWindowsPathStyle
);
370 if (CFStringGetLength(testPath
) == 0) {
374 if (testPath
) CFRelease(testPath
);
378 #error Dont know how to compute users home directories on this platform
382 static CFStringRef
_CFUserName(void) { // __CFPlatformCacheLock must be locked on entry and will be on exit
383 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
384 if (geteuid() != __CFEUID
|| getuid() != __CFUID
)
386 #elif DEPLOYMENT_TARGET_WINDOWS
388 wchar_t username
[1040];
391 if (GetUserNameW(username
, &size
)) {
392 // discount the extra NULL by decrementing the size
393 __CFUserName
= CFStringCreateWithCharacters(kCFAllocatorSystemDefault
, (const UniChar
*)username
, size
- 1);
395 const char *cname
= __CFgetenv("USERNAME");
397 __CFUserName
= CFStringCreateWithCString(kCFAllocatorSystemDefault
, cname
, kCFPlatformInterfaceStringEncoding
);
401 #error Dont know how to compute user name on this platform
404 __CFUserName
= (CFStringRef
)CFRetain(CFSTR(""));
408 #define CFMaxHostNameLength 256
409 #define CFMaxHostNameSize (CFMaxHostNameLength+1)
411 __private_extern__ CFStringRef
_CFStringCreateHostName(void) {
412 char myName
[CFMaxHostNameSize
];
414 // return @"" instead of nil a la CFUserName() and Ali Ozer
415 if (0 != gethostname(myName
, CFMaxHostNameSize
)) myName
[0] = '\0';
416 return CFStringCreateWithCString(kCFAllocatorSystemDefault
, myName
, kCFPlatformInterfaceStringEncoding
);
419 /* These are sanitized versions of the above functions. We might want to eliminate the above ones someday.
420 These can return NULL.
422 CF_EXPORT CFStringRef
CFGetUserName(void) {
423 CFStringRef result
= NULL
;
424 __CFSpinLock(&__CFPlatformCacheLock
);
425 result
= CFStringCreateCopy(kCFAllocatorSystemDefault
, _CFUserName());
426 __CFSpinUnlock(&__CFPlatformCacheLock
);
430 CF_EXPORT CFStringRef
CFCopyUserName(void) {
431 CFStringRef result
= NULL
;
432 __CFSpinLock(&__CFPlatformCacheLock
);
433 result
= CFStringCreateCopy(kCFAllocatorSystemDefault
, _CFUserName());
434 __CFSpinUnlock(&__CFPlatformCacheLock
);
438 CF_EXPORT CFURLRef
CFCopyHomeDirectoryURLForUser(CFStringRef uName
) {
439 CFURLRef result
= NULL
;
440 __CFSpinLock(&__CFPlatformCacheLock
);
441 result
= _CFCreateHomeDirectoryURLForUser(uName
);
442 __CFSpinUnlock(&__CFPlatformCacheLock
);
446 #undef CFMaxHostNameLength
447 #undef CFMaxHostNameSize
449 #if DEPLOYMENT_TARGET_WINDOWS
450 CF_INLINE CFIndex
strlen_UniChar(const UniChar
* p
) {
457 //#include <shfolder.h>
459 * _CFCreateApplicationRepositoryPath returns the path to the application's
460 * repository in a CFMutableStringRef. The path returned will be:
461 * <nFolder_path>\Apple Computer\<bundle_name>\
462 * or if the bundle name cannot be obtained:
463 * <nFolder_path>\Apple Computer\
464 * where nFolder_path is obtained by calling SHGetFolderPath with nFolder
465 * (for example, with CSIDL_APPDATA or CSIDL_LOCAL_APPDATA).
467 * The CFMutableStringRef result must be released by the caller.
469 * If anything fails along the way, the result will be NULL.
471 CF_EXPORT CFMutableStringRef
_CFCreateApplicationRepositoryPath(CFAllocatorRef alloc
, int nFolder
) {
472 CFMutableStringRef result
= NULL
;
473 UniChar szPath
[MAX_PATH
];
475 // get the current path to the data repository: CSIDL_APPDATA (roaming) or CSIDL_LOCAL_APPDATA (nonroaming)
476 if (S_OK
== SHGetFolderPathW(NULL
, nFolder
, NULL
, 0, (wchar_t *) szPath
)) {
477 CFStringRef directoryPath
;
479 // make it a CFString
480 directoryPath
= CFStringCreateWithCharacters(alloc
, szPath
, strlen_UniChar(szPath
));
483 CFStringRef bundleName
;
484 CFStringRef completePath
;
486 // attempt to get the bundle name
487 bundle
= CFBundleGetMainBundle();
489 bundleName
= (CFStringRef
)CFBundleGetValueForInfoDictionaryKey(bundle
, kCFBundleNameKey
);
496 // the path will be "<directoryPath>\Apple Computer\<bundleName>\" if there is a bundle name
497 completePath
= CFStringCreateWithFormat(alloc
, NULL
, CFSTR("%@\\Apple Computer\\%@\\"), directoryPath
, bundleName
);
500 // or "<directoryPath>\Apple Computer\" if there is no bundle name.
501 completePath
= CFStringCreateWithFormat(alloc
, NULL
, CFSTR("%@\\Apple Computer\\"), directoryPath
);
504 CFRelease(directoryPath
);
506 // make a mutable copy to return
508 result
= CFStringCreateMutableCopy(alloc
, 0, completePath
);
509 CFRelease(completePath
);
519 #pragma mark Thread Functions
521 #if DEPLOYMENT_TARGET_WINDOWS
523 // This code from here:
524 // http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
526 const DWORD MS_VC_EXCEPTION
=0x406D1388;
528 typedef struct tagTHREADNAME_INFO
530 DWORD dwType
; // Must be 0x1000.
531 LPCSTR szName
; // Pointer to name (in user addr space).
532 DWORD dwThreadID
; // Thread ID (-1=caller thread).
533 DWORD dwFlags
; // Reserved for future use, must be zero.
537 CF_EXPORT
void _NS_pthread_setname_np(const char *name
) {
538 THREADNAME_INFO info
;
539 info
.dwType
= 0x1000;
541 info
.dwThreadID
= GetCurrentThreadId();
546 RaiseException( MS_VC_EXCEPTION
, 0, sizeof(info
)/sizeof(ULONG_PTR
), (ULONG_PTR
*)&info
);
548 __except(EXCEPTION_EXECUTE_HANDLER
)
553 static pthread_t __initialPthread
= { NULL
, 0 };
555 CF_EXPORT
int _NS_pthread_main_np() {
556 pthread_t me
= pthread_self();
557 if (NULL
== __initialPthread
.p
) {
558 __initialPthread
.p
= me
.p
;
559 __initialPthread
.x
= me
.x
;
561 return (pthread_equal(__initialPthread
, me
));
567 #pragma mark Thread Local Data
569 // If slot >= CF_TSD_MAX_SLOTS, the SPI functions will crash at NULL + slot address.
570 // If thread data has been torn down, these functions should crash on CF_TSD_BAD_PTR + slot address.
571 #define CF_TSD_MAX_SLOTS 70
573 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
574 #define CF_TSD_KEY 55
577 // Windows and Linux, not sure how many times the destructor could get called; CF_TSD_MAX_DESTRUCTOR_CALLS could be 1
579 #define CF_TSD_BAD_PTR ((void *)0x1000)
581 typedef void (*tsdDestructor
)(void *);
583 // Data structure to hold TSD data, cleanup functions for each
584 typedef struct __CFTSDTable
{
585 uint32_t destructorCount
;
586 uintptr_t data
[CF_TSD_MAX_SLOTS
];
587 tsdDestructor destructors
[CF_TSD_MAX_SLOTS
];
590 static void __CFTSDFinalize(void *arg
);
592 #if DEPLOYMENT_TARGET_WINDOWS
594 #include "CFVersionCheck.h"
596 static DWORD __CFTSDIndexKey
= 0xFFFFFFFF;
598 // Called from CFRuntime's startup code, on Windows only
599 __private_extern__
void __CFTSDWindowsInitialize() {
600 __CFTSDIndexKey
= TlsAlloc();
603 // Called from CFRuntime's cleanup code, on Windows only
604 __private_extern__
void __CFTSDWindowsCleanup() {
605 TlsFree(__CFTSDIndexKey
);
608 // Called for each thread as it exits, on Windows only
609 __private_extern__
void __CFFinalizeWindowsThreadData() {
610 // Normally, this should call the finalizer several times to emulate the behavior of pthreads on Windows. However, a few bugs keep us from doing this:
611 // <rdar://problem/8989063> REGRESSION(CF-610-CF-611): Crash closing Safari in BonjourDB destructor (Windows)
612 // <rdar://problem/9326814> SyncUIHandler crashes after conflict is resolved and we do SyncNow
613 // and a bug in dispatch keeps us from using pthreadsWin32 directly, because it does not deal with the case of a dispatch_async happening during process exit (it attempts to create a thread, but that is illegal on Win32 and causes a hang).
614 // So instead we just finalize once, which is the behavior pre-Airwolf anyway
615 __CFTSDFinalize(TlsGetValue(__CFTSDIndexKey
));
620 #if DEPLOYMENT_TARGET_LINUX
622 static pthread_key_t __CFTSDIndexKey
;
624 // Called from CFRuntime's startup code, on Linux only
625 __private_extern__
void __CFTSDLinuxInitialize() {
626 (void)pthread_key_create(&__CFTSDIndexKey
, __CFTSDFinalize
);
631 static void __CFTSDSetSpecific(void *arg
) {
632 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
633 pthread_setspecific(CF_TSD_KEY
, arg
);
634 #elif DEPLOYMENT_TARGET_LINUX
635 pthread_setspecific(__CFTSDIndexKey
, arg
);
636 #elif DEPLOYMENT_TARGET_WINDOWS
637 TlsSetValue(__CFTSDIndexKey
, arg
);
641 static void *__CFTSDGetSpecific() {
642 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
643 return pthread_getspecific(CF_TSD_KEY
);
644 #elif DEPLOYMENT_TARGET_LINUX
645 return pthread_getspecific(__CFTSDIndexKey
);
646 #elif DEPLOYMENT_TARGET_WINDOWS
647 return TlsGetValue(__CFTSDIndexKey
);
651 static void __CFTSDFinalize(void *arg
) {
652 // Set our TSD so we're called again by pthreads. It will call the destructor 5 times as long as a value is set in the thread specific data. We handle each case below.
653 __CFTSDSetSpecific(arg
);
655 if (!arg
|| arg
== CF_TSD_BAD_PTR
) {
656 // We've already been destroyed. The call above set the bad pointer again. Now we just return.
660 __CFTSDTable
*table
= (__CFTSDTable
*)arg
;
661 table
->destructorCount
++;
663 // On 1st, 2nd, 3rd, 4th calls, invoke destructor
664 // Note that invocation of the destructor may cause a value to be set again in the per-thread data slots. The destructor count and destructors are preserved.
665 // This logic is basically the same as what pthreads does. We just skip the 'created' flag.
667 uintptr_t pool
= _CFAutoreleasePoolPush();
669 for (int32_t i
= 0; i
< CF_TSD_MAX_SLOTS
; i
++) {
670 if (table
->data
[i
] && table
->destructors
[i
]) {
671 uintptr_t old
= table
->data
[i
];
672 table
->data
[i
] = (uintptr_t)NULL
;
673 table
->destructors
[i
]((void *)(old
));
677 _CFAutoreleasePoolPop(pool
);
680 if (table
->destructorCount
== PTHREAD_DESTRUCTOR_ITERATIONS
- 1) { // On 4th call, destroy our data
683 // Now if the destructor is called again we will take the shortcut at the beginning of this function.
684 __CFTSDSetSpecific(CF_TSD_BAD_PTR
);
689 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
690 extern int pthread_key_init_np(int, void (*)(void *));
693 // Get or initialize a thread local storage. It is created on demand.
694 static __CFTSDTable
*__CFTSDGetTable() {
695 __CFTSDTable
*table
= (__CFTSDTable
*)__CFTSDGetSpecific();
696 // Make sure we're not setting data again after destruction.
697 if (table
== CF_TSD_BAD_PTR
) {
700 // Create table on demand
702 // This memory is freed in the finalize function
703 table
= (__CFTSDTable
*)calloc(1, sizeof(__CFTSDTable
));
704 // Windows and Linux have created the table already, we need to initialize it here for other platforms. On Windows, the cleanup function is called by DllMain when a thread exits. On Linux the destructor is set at init time.
705 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
706 pthread_key_init_np(CF_TSD_KEY
, __CFTSDFinalize
);
708 __CFTSDSetSpecific(table
);
715 // For the use of CF and Foundation only
716 CF_EXPORT
void *_CFGetTSD(uint32_t slot
) {
717 if (slot
> CF_TSD_MAX_SLOTS
) {
718 _CFLogSimple(kCFLogLevelError
, "Error: TSD slot %d out of range (get)", slot
);
721 __CFTSDTable
*table
= __CFTSDGetTable();
723 // Someone is getting TSD during thread destruction. The table is gone, so we can't get any data anymore.
724 _CFLogSimple(kCFLogLevelWarning
, "Warning: TSD slot %d retrieved but the thread data has already been torn down.", slot
);
727 uintptr_t *slots
= (uintptr_t *)(table
->data
);
728 return (void *)slots
[slot
];
731 // For the use of CF and Foundation only
732 CF_EXPORT
void *_CFSetTSD(uint32_t slot
, void *newVal
, tsdDestructor destructor
) {
733 if (slot
> CF_TSD_MAX_SLOTS
) {
734 _CFLogSimple(kCFLogLevelError
, "Error: TSD slot %d out of range (set)", slot
);
737 __CFTSDTable
*table
= __CFTSDGetTable();
739 // Someone is setting TSD during thread destruction. The table is gone, so we can't get any data anymore.
740 _CFLogSimple(kCFLogLevelWarning
, "Warning: TSD slot %d set but the thread data has already been torn down.", slot
);
744 void *oldVal
= (void *)table
->data
[slot
];
746 table
->data
[slot
] = (uintptr_t)newVal
;
747 table
->destructors
[slot
] = destructor
;
753 #pragma mark Windows Wide to UTF8 and UTF8 to Wide
755 #if DEPLOYMENT_TARGET_WINDOWS
756 /* On Windows, we want to use UTF-16LE for path names to get full unicode support. Internally, however, everything remains in UTF-8 representation. These helper functions stand between CF and the Microsoft CRT to ensure that we are using the right representation on both sides. */
758 #include <sys/stat.h>
761 // Creates a buffer of wchar_t to hold a UTF16LE version of the UTF8 str passed in. Caller must free the buffer when done. If resultLen is non-NULL, it is filled out with the number of characters in the string.
762 static wchar_t *createWideFileSystemRepresentation(const char *str
, CFIndex
*resultLen
) {
763 // Get the real length of the string in UTF16 characters
764 CFStringRef cfStr
= CFStringCreateWithCString(kCFAllocatorSystemDefault
, str
, kCFStringEncodingUTF8
);
765 CFIndex strLen
= CFStringGetLength(cfStr
);
767 // Allocate a wide buffer to hold the converted string, including space for a NULL terminator
768 wchar_t *wideBuf
= (wchar_t *)malloc((strLen
+ 1) * sizeof(wchar_t));
770 // Copy the string into the buffer and terminate
771 CFStringGetCharacters(cfStr
, CFRangeMake(0, strLen
), (UniChar
*)wideBuf
);
775 if (resultLen
) *resultLen
= strLen
;
779 // Copies a UTF16 buffer into a supplied UTF8 buffer.
780 static void copyToNarrowFileSystemRepresentation(const wchar_t *wide
, CFIndex dstBufSize
, char *dstbuf
) {
781 // Get the real length of the wide string in UTF8 characters
782 CFStringRef cfStr
= CFStringCreateWithCharacters(kCFAllocatorSystemDefault
, (const UniChar
*)wide
, wcslen(wide
));
783 CFIndex strLen
= CFStringGetLength(cfStr
);
786 // Copy the wide string into the buffer and terminate
787 CFStringGetBytes(cfStr
, CFRangeMake(0, strLen
), kCFStringEncodingUTF8
, 0, false, (uint8_t *)dstbuf
, dstBufSize
, &bytesUsed
);
788 dstbuf
[bytesUsed
] = 0;
793 CF_EXPORT
int _NS_stat(const char *name
, struct _stat
*st
) {
794 wchar_t *wide
= createWideFileSystemRepresentation(name
, NULL
);
795 int res
= _wstat(wide
, st
);
800 CF_EXPORT
int _NS_mkdir(const char *name
) {
801 wchar_t *wide
= createWideFileSystemRepresentation(name
, NULL
);
802 int res
= _wmkdir(wide
);
807 CF_EXPORT
int _NS_rmdir(const char *name
) {
808 wchar_t *wide
= createWideFileSystemRepresentation(name
, NULL
);
809 int res
= _wrmdir(wide
);
814 CF_EXPORT
int _NS_chmod(const char *name
, int mode
) {
815 wchar_t *wide
= createWideFileSystemRepresentation(name
, NULL
);
819 if (mode
| 0400) newMode
|= _S_IREAD
;
820 if (mode
| 0200) newMode
|= _S_IWRITE
;
821 if (mode
| 0100) newMode
|= _S_IEXEC
;
823 int res
= _wchmod(wide
, newMode
);
828 CF_EXPORT
int _NS_unlink(const char *name
) {
829 wchar_t *wide
= createWideFileSystemRepresentation(name
, NULL
);
830 int res
= _wunlink(wide
);
835 // Warning: this doesn't support dstbuf as null even though 'getcwd' does
836 CF_EXPORT
char *_NS_getcwd(char *dstbuf
, size_t size
) {
838 CFLog(kCFLogLevelWarning
, CFSTR("CFPlatform: getcwd called with null buffer"));
842 wchar_t *buf
= _wgetcwd(NULL
, 0);
847 // Convert result to UTF8
848 copyToNarrowFileSystemRepresentation(buf
, (CFIndex
)size
, dstbuf
);
853 CF_EXPORT
char *_NS_getenv(const char *name
) {
855 // We have to be careful what happens here, because getenv is called during cf initialization, and things like cfstring may not be working yet
859 CF_EXPORT
int _NS_rename(const char *oldName
, const char *newName
) {
860 wchar_t *oldWide
= createWideFileSystemRepresentation(oldName
, NULL
);
861 wchar_t *newWide
= createWideFileSystemRepresentation(newName
, NULL
);
862 // _wrename on Windows does not behave exactly as rename() on Mac OS -- if the file exists, the Windows one will fail whereas the Mac OS version will replace
863 // To simulate the Mac OS behavior, we use the Win32 API then fill out errno if something goes wrong
864 BOOL winRes
= MoveFileExW(oldWide
, newWide
, MOVEFILE_REPLACE_EXISTING
);
865 DWORD error
= GetLastError();
871 case ERROR_FILE_NOT_FOUND
:
872 case ERROR_PATH_NOT_FOUND
:
873 case ERROR_OPEN_FAILED
:
876 case ERROR_ACCESS_DENIED
:
885 return (winRes
? 0 : -1);
888 CF_EXPORT
int _NS_open(const char *name
, int oflag
, int pmode
) {
889 wchar_t *wide
= createWideFileSystemRepresentation(name
, NULL
);
891 _wsopen_s(&fd
, wide
, oflag
, _SH_DENYNO
, _S_IREAD
| _S_IWRITE
);
896 CF_EXPORT
int _NS_chdir(const char *name
) {
897 wchar_t *wide
= createWideFileSystemRepresentation(name
, NULL
);
898 int res
= _wchdir(wide
);
903 CF_EXPORT
int _NS_access(const char *name
, int amode
) {
904 // execute is always true
905 if (amode
== 1) return 0;
907 wchar_t *wide
= createWideFileSystemRepresentation(name
, NULL
);
908 // we only care about the read-only (04) and write-only (02) bits, so mask octal 06
909 int res
= _waccess(wide
, amode
& 06);
914 // This is a bit different than the standard 'mkstemp', because the size parameter is needed so we know the size of the UTF8 buffer
915 // Also, we don't avoid the race between creating a temporary file name and opening it on Windows like we do on Mac
916 CF_EXPORT
int _NS_mkstemp(char *name
, int bufSize
) {
918 wchar_t *wide
= createWideFileSystemRepresentation(name
, &nameLen
);
920 // First check to see if the directory that this new temporary file will be created in exists. If not, set errno to ENOTDIR. This mimics the behavior of mkstemp on MacOS more closely.
921 // Look for the last '\' in the path
922 wchar_t *lastSlash
= wcsrchr(wide
, '\\');
928 // Set the last slash to NULL temporarily and use it for _wstat
930 struct _stat dirInfo
;
931 int res
= _wstat(wide
, &dirInfo
);
933 if (errno
== ENOENT
) {
939 // Restore the last slash
942 errno_t err
= _wmktemp_s(wide
, nameLen
+ 1);
949 _wsopen_s(&fd
, wide
, _O_RDWR
| _O_CREAT
| CF_OPENFLGS
, _SH_DENYNO
, _S_IREAD
| _S_IWRITE
);
951 // Convert the wide name back into the UTF8 buffer the caller supplied
952 copyToNarrowFileSystemRepresentation(wide
, bufSize
, name
);
959 #if DEPLOYMENT_TARGET_WINDOWS
960 // Utilities to convert from a volume name to a drive letter
962 Boolean
_isAFloppy(char driveLetter
)
966 Boolean retval
= false;
969 if (driveLetter
>= 'a' && driveLetter
<= 'z') {
970 driveLetter
= driveLetter
- 'a' + 'A';
973 if ((driveLetter
< 'A') || (driveLetter
> 'Z')) {
974 // invalid driveLetter; I guess it's not a floppy...
978 iDrive
= driveLetter
- 'A' + 1;
980 // On Windows NT, use the technique described in the Knowledge Base article Q115828 and in the "FLOPPY" SDK sample.
981 wsprintf(tsz
, TEXT("\\\\.\\%c:"), TEXT('@') + iDrive
);
982 h
= CreateFile(tsz
, 0, FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
983 if (h
!= INVALID_HANDLE_VALUE
)
985 DISK_GEOMETRY Geom
[20];
988 if (DeviceIoControl (h
, IOCTL_DISK_GET_MEDIA_TYPES
, 0, 0,
989 Geom
, sizeof(Geom
), &cb
, 0)
992 switch (Geom
[0].MediaType
)
994 case F5_1Pt2_512
: // 5.25 1.2MB floppy
995 case F5_360_512
: // 5.25 360K floppy
996 case F5_320_512
: // 5.25 320K floppy
997 case F5_320_1024
: // 5.25 320K floppy
998 case F5_180_512
: // 5.25 180K floppy
999 case F5_160_512
: // 5.25 160K floppy
1000 case F3_1Pt44_512
: // 3.5 1.44MB floppy
1001 case F3_2Pt88_512
: // 3.5 2.88MB floppy
1002 case F3_20Pt8_512
: // 3.5 20.8MB floppy
1003 case F3_720_512
: // 3.5 720K floppy
1016 extern CFStringRef
CFCreateWindowsDrivePathFromVolumeName(CFStringRef volNameStr
) {
1017 if (!volNameStr
) return NULL
;
1019 // This code is designed to match as closely as possible code from QuickTime's library
1020 CFIndex strLen
= CFStringGetLength(volNameStr
);
1026 long length
, result
;
1027 wchar_t *driveNames
= NULL
;
1029 // Get the size of the buffer to store the list of drives
1030 length
= GetLogicalDriveStringsW(0, 0);
1035 driveNames
= (wchar_t *)malloc((length
+ 1) * sizeof(wchar_t));
1036 result
= GetLogicalDriveStringsW(length
, driveNames
);
1038 if (!result
|| result
> length
) {
1043 // Get the volume name string into a wide buffer
1044 wchar_t *theVolumeName
= (wchar_t *)malloc((strLen
+ 1) * sizeof(wchar_t));
1045 CFStringGetCharacters(volNameStr
, CFRangeMake(0, strLen
), (UniChar
*)theVolumeName
);
1046 theVolumeName
[strLen
] = 0;
1048 // lowercase volume name
1049 _wcslwr(theVolumeName
);
1051 // Iterate through the drive names, looking for something that matches
1052 wchar_t *drivePtr
= driveNames
;
1053 CFStringRef drivePathResult
= NULL
;
1058 if (!_isAFloppy((char)*drivePtr
)) {
1060 DWORD whoCares1
, whoCares2
;
1061 BOOL getVolInfoSucceeded
;
1062 UniChar thisVolumeName
[MAX_PATH
];
1064 // Convert this drive string into a volume name
1065 oldErrorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
);
1066 getVolInfoSucceeded
= GetVolumeInformationW(drivePtr
, (LPWSTR
)thisVolumeName
, sizeof(thisVolumeName
), NULL
, &whoCares1
, &whoCares2
, NULL
, 0);
1067 SetErrorMode(oldErrorMode
);
1069 if (getVolInfoSucceeded
) {
1070 _wcslwr((wchar_t *)thisVolumeName
);
1072 // If the volume corresponding to this drive matches the input volume
1073 // then this drive is the winner.
1074 if (!wcscmp((const wchar_t *)thisVolumeName
, theVolumeName
) ||
1075 (*thisVolumeName
== 0x00 && (CFStringCompare(volNameStr
, CFSTR("NONAME"), 0) == kCFCompareEqualTo
))) {
1076 drivePathResult
= CFStringCreateWithCharacters(kCFAllocatorSystemDefault
, (const UniChar
*)drivePtr
, wcslen(drivePtr
));
1082 drivePtr
+= wcslen(drivePtr
) + 1;
1087 free(theVolumeName
);
1088 return drivePathResult
;
1091 #endif // DEPLOYMENT_TARGET_WINDOWS
1094 #pragma mark Linux OSAtomic
1096 #if DEPLOYMENT_TARGET_LINUX
1098 bool OSAtomicCompareAndSwapPtr(void *oldp
, void *newp
, void *volatile *dst
)
1100 return __sync_bool_compare_and_swap(dst
, oldp
, newp
);
1103 bool OSAtomicCompareAndSwapLong(long oldl
, long newl
, long volatile *dst
)
1105 return __sync_val_compare_and_swap(dst
, oldl
, newl
);
1108 bool OSAtomicCompareAndSwapPtrBarrier(void *oldp
, void *newp
, void *volatile *dst
)
1110 return __sync_bool_compare_and_swap(dst
, oldp
, newp
);
1113 int32_t OSAtomicAdd32Barrier( int32_t theAmount
, volatile int32_t *theValue
) {
1114 return __sync_fetch_and_add(theValue
, theAmount
) + theAmount
;
1117 bool OSAtomicCompareAndSwap32Barrier(int32_t oldValue
, int32_t newValue
, volatile int32_t *theValue
) {
1118 return __sync_bool_compare_and_swap(theValue
, oldValue
, newValue
);
1121 int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst
)
1123 return OSAtomicAdd32Barrier(-1, dst
);
1126 int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst
)
1128 return OSAtomicAdd32Barrier(1, dst
);
1131 int32_t OSAtomicAdd32( int32_t theAmount
, volatile int32_t *theValue
) {
1132 return OSAtomicAdd32Barrier(theAmount
, theValue
);
1135 int32_t OSAtomicIncrement32(volatile int32_t *theValue
) {
1136 return OSAtomicIncrement32Barrier(theValue
);
1139 int32_t OSAtomicDecrement32(volatile int32_t *theValue
) {
1140 return OSAtomicDecrement32Barrier(theValue
);
1143 void OSMemoryBarrier() {
1144 __sync_synchronize();
1147 #endif // DEPLOYMENT_TARGET_LINUX
1150 #pragma mark Windows and Linux Helpers
1152 #if DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
1156 __private_extern__
int asprintf(char **ret
, const char *format
, ...) {
1159 *ret
= (char *) malloc(sz
* sizeof(char));
1160 if (!*ret
) return -1;
1161 va_start(args
, format
);
1162 int cnt
= vsnprintf(*ret
, sz
, format
, args
);
1164 if (cnt
< sz
- 1) return cnt
;
1166 char *oldret
= *ret
;
1167 *ret
= (char *) realloc(*ret
, sz
* sizeof(char));
1168 if (!*ret
&& oldret
) free(oldret
);
1169 if (!*ret
) return -1;
1170 va_start(args
, format
);
1171 cnt
= vsnprintf(*ret
, sz
, format
, args
);
1173 if (cnt
< sz
- 1) return cnt
;