2 * Copyright (c) 2013, 2015-2019 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 * IPMonitorControlPrefs.c
26 * - definitions for accessing IPMonitor control preferences
30 * Modification History
32 * January 14, 2013 Dieter Siegmund (dieter@apple)
36 #include <TargetConditionals.h>
37 #include <SystemConfiguration/SCPreferences.h>
38 #include <SystemConfiguration/SCPrivate.h>
39 #include <SystemConfiguration/scprefs_observer.h>
40 #include "IPMonitorControlPrefs.h"
42 os_log_t
__log_IPMonitor(void);
45 * kIPMonitorControlPrefsID
46 * - identifies the IPMonitor preferences file that contains 'Verbose'
48 #define kIPMonitorControlPrefsIDStr "com.apple.IPMonitor.control.plist"
49 #define kIPMonitorControlPrefsID CFSTR(kIPMonitorControlPrefsIDStr)
53 * - indicates whether IPMonitor is verbose or not
55 #define kVerbose CFSTR("Verbose")
57 static SCPreferencesRef S_prefs
;
58 static IPMonitorControlPrefsCallBack S_callback
;
60 static SCPreferencesRef
61 IPMonitorControlPrefsGet(void)
63 if (S_prefs
== NULL
) {
64 IPMonitorControlPrefsInit(NULL
, NULL
);
70 prefs_changed(void * arg
)
73 /* get the current value */
74 if (S_callback
!= NULL
) {
75 (*S_callback
)(S_prefs
);
83 * kIPMonitorControlManangedPrefsID
84 * - identifies the location of the managed preferences file
86 #define kManagedPrefsDirStr "/Library/Managed Preferences/mobile/"
87 #define kIPMonitorControlManagedPrefsID CFSTR(kManagedPrefsDirStr \
88 kIPMonitorControlPrefsIDStr)
89 static SCPreferencesRef S_managed_prefs
;
91 static SCPreferencesRef
92 IPMonitorControlManagedPrefsGet(void)
94 if (S_managed_prefs
== NULL
) {
96 = SCPreferencesCreate(NULL
, CFSTR("IPMonitorControlPrefs"),
97 kIPMonitorControlManagedPrefsID
);
99 return (S_managed_prefs
);
103 enable_prefs_observer(CFRunLoopRef runloop
)
105 CFRunLoopSourceContext context
;
106 dispatch_block_t handler
;
107 dispatch_queue_t queue
;
108 CFRunLoopSourceRef source
;
110 memset(&context
, 0, sizeof(context
));
111 context
.perform
= prefs_changed
;
112 source
= CFRunLoopSourceCreate(kCFAllocatorDefault
, 0, &context
);
113 CFRunLoopAddSource(runloop
, source
, kCFRunLoopCommonModes
);
114 queue
= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0);
116 if (source
!= NULL
) {
117 CFRunLoopSourceSignal(source
);
118 if (runloop
!= NULL
) {
119 CFRunLoopWakeUp(runloop
);
123 _scprefs_observer_watch(scprefs_observer_type_global
,
124 kIPMonitorControlPrefsIDStr
,
129 #else /* TARGET_OS_IPHONE */
132 enable_prefs_observer(CFRunLoopRef runloop
)
134 #pragma unused(runloop)
138 #endif /* TARGET_OS_IPHONE */
141 IPMonitorControlPrefsChanged(SCPreferencesRef prefs
,
142 SCPreferencesNotification type
,
145 #pragma unused(prefs)
152 __private_extern__ SCPreferencesRef
153 IPMonitorControlPrefsInit(CFRunLoopRef runloop
,
154 IPMonitorControlPrefsCallBack callback
)
156 S_prefs
= SCPreferencesCreate(NULL
, CFSTR("IPMonitorControlPrefs"),
157 kIPMonitorControlPrefsID
);
158 if (runloop
!= NULL
&& callback
!= NULL
) {
159 S_callback
= callback
;
160 if (!SCPreferencesSetCallback(S_prefs
, IPMonitorControlPrefsChanged
, NULL
)) {
161 SC_log(LOG_NOTICE
, "SCPreferencesSetCallBack() failed: %s", SCErrorString(SCError()));
165 if (!SCPreferencesScheduleWithRunLoop(S_prefs
, runloop
, kCFRunLoopCommonModes
)) {
166 SC_log(LOG_NOTICE
, "SCPreferencesScheduleWithRunLoop() failed: %s", SCErrorString(SCError()));
167 (void) SCPreferencesSetCallback(S_prefs
, NULL
, NULL
);
170 enable_prefs_observer(runloop
);
178 IPMonitorControlPrefsSave(void)
180 Boolean saved
= FALSE
;
182 if (S_prefs
!= NULL
) {
183 saved
= SCPreferencesCommitChanges(S_prefs
);
184 SCPreferencesSynchronize(S_prefs
);
190 prefs_get_boolean(CFStringRef key
)
192 CFBooleanRef b
= NULL
;
195 b
= SCPreferencesGetValue(IPMonitorControlManagedPrefsGet(), key
);
196 b
= isA_CFBoolean(b
);
197 #endif /* TARGET_OS_IPHONE */
199 b
= SCPreferencesGetValue(IPMonitorControlPrefsGet(), key
);
200 b
= isA_CFBoolean(b
);
206 prefs_set_boolean(CFStringRef key
, CFBooleanRef b
)
208 SCPreferencesRef prefs
= IPMonitorControlPrefsGet();
211 if (isA_CFBoolean(b
) == NULL
) {
212 SCPreferencesRemoveValue(prefs
, key
);
215 SCPreferencesSetValue(prefs
, key
, b
);
222 IPMonitorControlPrefsRefresh(void)
224 if (S_prefs
!= NULL
) {
225 SCPreferencesSynchronize(S_prefs
);
228 if (S_managed_prefs
!= NULL
) {
229 SCPreferencesSynchronize(S_managed_prefs
);
231 #endif /* TARGET_OS_IPHONE */
238 __private_extern__ Boolean
239 IPMonitorControlPrefsIsVerbose(void)
242 Boolean verbose
= FALSE
;
244 b
= prefs_get_boolean(kVerbose
);
246 verbose
= CFBooleanGetValue(b
);
248 /* flush the backing store */
249 IPMonitorControlPrefsRefresh();
256 __private_extern__ Boolean
257 IPMonitorControlPrefsSetVerbose(Boolean verbose
)
260 prefs_set_boolean(kVerbose
, NULL
);
263 prefs_set_boolean(kVerbose
, kCFBooleanTrue
);
265 return (IPMonitorControlPrefsSave());
268 #ifdef TEST_IPMONITORCONTROLPREFS
270 main(int argc
, char * argv
[])
272 Boolean need_usage
= FALSE
;
273 Boolean success
= FALSE
;
278 else if (strcasecmp(argv
[1], "on") == 0) {
279 success
= IPMonitorControlPrefsSetVerbose(TRUE
);
281 else if (strcasecmp(argv
[1], "off") == 0) {
282 success
= IPMonitorControlPrefsSetVerbose(FALSE
);
288 fprintf(stderr
, "usage: %s ( on | off )\n", argv
[0]);
292 fprintf(stderr
, "failed to save prefs\n");
299 #endif /* TEST_IPMONITORCONTROLPREFS */