2 // iCloudKeychainTrace.c
6 // Copyright (c) 2013 Apple Inc. All rights reserved.
9 #include "iCloudKeychainTrace.h"
10 #include <TargetConditionals.h>
12 #include "SecCFWrappers.h"
14 const CFStringRef kCloudKeychainNumbrerOfSyncingConflicts
= CFSTR("com.apple.cloudkeychain.conflictsCount");
15 const CFStringRef kCloudKeychainNumberOfTimesSyncFailed
= CFSTR("com.apple.cloudkeychain.syncFailureCount");
16 const CFStringRef kCloudKeychainNumberOfConflictsResolved
= CFSTR("com.apple.cloudkeychain.conflictsResolved");
17 const CFStringRef kCloudKeychainNumberOfTimesSyncedWithPeers
= CFSTR("com.apple.cloudkeychain.syncedWithPeers");
19 static const CFStringRef gMessageTracerPrefix
= CFSTR("com.apple.message.");
21 #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR))
24 static const char* gMessageTracerDomainField
= "com.apple.message.domain";
25 //static const char* gTopLevelKeyForiCloudKeychainTracing = "com.apple.cloudkeychain";
27 static bool OSX_SetCloudKeychainTraceValueForKey(CFStringRef key
, int64_t value
)
37 mAsl
= asl_new(ASL_TYPE_MSG
);
43 CFIndex key_length
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(key
), kCFStringEncodingUTF8
);
44 key_length
+= 1; // For null
45 char base_key_buffer
[key_length
];
46 memset(base_key_buffer
, 0,key_length
);
47 if (!CFStringGetCString(key
, base_key_buffer
, key_length
, kCFStringEncodingUTF8
))
54 CFStringRef key_str
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("%@%@"), gMessageTracerPrefix
, key
);
61 CFStringRef value_str
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("%lld"), value
);
62 if (NULL
== value_str
)
69 CFIndex key_str_numBytes
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(key_str
), kCFStringEncodingUTF8
);
70 key_str_numBytes
+= 1; // For null
71 char key_buffer
[key_str_numBytes
];
72 memset(key_buffer
, 0, key_str_numBytes
);
73 if (!CFStringGetCString(key_str
, key_buffer
, key_str_numBytes
, kCFStringEncodingUTF8
))
82 CFIndex value_str_numBytes
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(value_str
), kCFStringEncodingUTF8
);
83 value_str_numBytes
+= 1; // For null
84 char value_buffer
[value_str_numBytes
];
85 memset(value_buffer
, 0, value_str_numBytes
);
86 if (!CFStringGetCString(value_str
, value_buffer
, value_str_numBytes
, kCFStringEncodingUTF8
))
94 asl_set(mAsl
, gMessageTracerDomainField
, base_key_buffer
);
96 asl_set(mAsl
, key_buffer
, value_buffer
);
97 asl_log(NULL
, mAsl
, ASL_LEVEL_NOTICE
, "%s is %lld", key_buffer
, value
);
103 #if (TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR)
105 typedef void (*type_ADClientClearScalarKey
)(CFStringRef key
);
106 typedef void (*type_ADClientAddValueForScalarKey
)(CFStringRef key
, int64_t value
);
108 static type_ADClientClearScalarKey gADClientClearScalarKey
= NULL
;
109 static type_ADClientAddValueForScalarKey gADClientAddValueForScalarKey
= NULL
;
111 static dispatch_once_t gADFunctionPointersSet
= 0;
112 static CFBundleRef gAggdBundleRef
= NULL
;
113 static bool gFunctionPointersAreLoaded
= false;
115 static bool InitializeADFunctionPointers()
117 if (gFunctionPointersAreLoaded
)
119 return gFunctionPointersAreLoaded
;
122 dispatch_once(&gADFunctionPointersSet
,
124 CFStringRef path_to_aggd_framework
= CFSTR("/System/Library/PrivateFrameworks/AggregateDictionary.framework");
126 CFURLRef aggd_url
= CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, path_to_aggd_framework
, kCFURLPOSIXPathStyle
, true);
128 if (NULL
!= aggd_url
)
130 gAggdBundleRef
= CFBundleCreate(kCFAllocatorDefault
, aggd_url
);
131 if (NULL
!= gAggdBundleRef
)
133 gADClientClearScalarKey
= (type_ADClientClearScalarKey
)
134 CFBundleGetFunctionPointerForName(gAggdBundleRef
, CFSTR("ADClientClearScalarKey"));
136 gADClientAddValueForScalarKey
= (type_ADClientAddValueForScalarKey
)
137 CFBundleGetFunctionPointerForName(gAggdBundleRef
, CFSTR("ADClientAddValueForScalarKey"));
143 gFunctionPointersAreLoaded
= ((NULL
!= gADClientClearScalarKey
) && (NULL
!= gADClientAddValueForScalarKey
));
144 return gFunctionPointersAreLoaded
;
147 static void Internal_ADClientClearScalarKey(CFStringRef key
)
149 if (InitializeADFunctionPointers())
151 gADClientClearScalarKey(key
);
155 static void Internal_ADClientAddValueForScalarKey(CFStringRef key
, int64_t value
)
157 if (InitializeADFunctionPointers())
159 gADClientAddValueForScalarKey(key
, value
);
163 static bool iOS_SetCloudKeychainTraceValueForKey(CFStringRef key
, int64_t value
)
172 Internal_ADClientClearScalarKey(key
);
176 Internal_ADClientAddValueForScalarKey(key
, value
);
182 bool SetCloudKeychainTraceValueForKey(CFStringRef key
, int64_t value
)
184 #if (TARGET_IPHONE_SIMULATOR)
188 #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
189 return OSX_SetCloudKeychainTraceValueForKey(key
, value
);
192 #if (TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR)
193 return iOS_SetCloudKeychainTraceValueForKey(key
, value
);