2 * Copyright (c) 2013, 2015-2018 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 os_activity_t activity
;
75 activity
= os_activity_create("processing IPMonitor [rank] preference change",
77 OS_ACTIVITY_FLAG_DEFAULT
);
78 os_activity_scope(activity
);
80 /* get the current value */
81 if (S_callback
!= NULL
) {
82 (*S_callback
)(S_prefs
);
92 * kIPMonitorControlManangedPrefsID
93 * - identifies the location of the managed preferences file
95 #define kManagedPrefsDirStr "/Library/Managed Preferences/mobile/"
96 #define kIPMonitorControlManagedPrefsID CFSTR(kManagedPrefsDirStr \
97 kIPMonitorControlPrefsIDStr)
98 static SCPreferencesRef S_managed_prefs
;
100 static SCPreferencesRef
101 IPMonitorControlManagedPrefsGet(void)
103 if (S_managed_prefs
== NULL
) {
105 = SCPreferencesCreate(NULL
, CFSTR("IPMonitorControlPrefs"),
106 kIPMonitorControlManagedPrefsID
);
108 return (S_managed_prefs
);
112 enable_prefs_observer(CFRunLoopRef runloop
)
114 CFRunLoopSourceContext context
;
115 dispatch_block_t handler
;
116 dispatch_queue_t queue
;
117 CFRunLoopSourceRef source
;
119 bzero(&context
, sizeof(context
));
120 context
.perform
= prefs_changed
;
121 source
= CFRunLoopSourceCreate(kCFAllocatorDefault
, 0, &context
);
122 CFRunLoopAddSource(runloop
, source
, kCFRunLoopCommonModes
);
123 queue
= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0);
125 if (source
!= NULL
) {
126 CFRunLoopSourceSignal(source
);
127 if (runloop
!= NULL
) {
128 CFRunLoopWakeUp(runloop
);
132 _scprefs_observer_watch(scprefs_observer_type_global
,
133 kIPMonitorControlPrefsIDStr
,
138 #else /* TARGET_OS_IPHONE */
141 enable_prefs_observer(CFRunLoopRef runloop
)
143 #pragma unused(runloop)
147 #endif /* TARGET_OS_IPHONE */
150 IPMonitorControlPrefsChanged(SCPreferencesRef prefs
,
151 SCPreferencesNotification type
,
154 #pragma unused(prefs)
161 __private_extern__ SCPreferencesRef
162 IPMonitorControlPrefsInit(CFRunLoopRef runloop
,
163 IPMonitorControlPrefsCallBack callback
)
165 S_prefs
= SCPreferencesCreate(NULL
, CFSTR("IPMonitorControlPrefs"),
166 kIPMonitorControlPrefsID
);
167 if (runloop
!= NULL
&& callback
!= NULL
) {
168 S_callback
= callback
;
169 if (!SCPreferencesSetCallback(S_prefs
, IPMonitorControlPrefsChanged
, NULL
)) {
170 SC_log(LOG_NOTICE
, "SCPreferencesSetCallBack() failed: %s", SCErrorString(SCError()));
174 if (!SCPreferencesScheduleWithRunLoop(S_prefs
, runloop
, kCFRunLoopCommonModes
)) {
175 SC_log(LOG_NOTICE
, "SCPreferencesScheduleWithRunLoop() failed: %s", SCErrorString(SCError()));
176 (void) SCPreferencesSetCallback(S_prefs
, NULL
, NULL
);
179 enable_prefs_observer(runloop
);
187 IPMonitorControlPrefsSave(void)
189 Boolean saved
= FALSE
;
191 if (S_prefs
!= NULL
) {
192 saved
= SCPreferencesCommitChanges(S_prefs
);
193 SCPreferencesSynchronize(S_prefs
);
199 prefs_get_boolean(CFStringRef key
)
201 CFBooleanRef b
= NULL
;
204 b
= SCPreferencesGetValue(IPMonitorControlManagedPrefsGet(), key
);
205 b
= isA_CFBoolean(b
);
206 #endif /* TARGET_OS_IPHONE */
208 b
= SCPreferencesGetValue(IPMonitorControlPrefsGet(), key
);
209 b
= isA_CFBoolean(b
);
215 prefs_set_boolean(CFStringRef key
, CFBooleanRef b
)
217 SCPreferencesRef prefs
= IPMonitorControlPrefsGet();
220 if (isA_CFBoolean(b
) == NULL
) {
221 SCPreferencesRemoveValue(prefs
, key
);
224 SCPreferencesSetValue(prefs
, key
, b
);
231 IPMonitorControlPrefsRefresh(void)
233 if (S_prefs
!= NULL
) {
234 SCPreferencesSynchronize(S_prefs
);
237 if (S_managed_prefs
!= NULL
) {
238 SCPreferencesSynchronize(S_managed_prefs
);
240 #endif /* TARGET_OS_IPHONE */
247 __private_extern__ Boolean
248 IPMonitorControlPrefsIsVerbose(void)
251 Boolean verbose
= FALSE
;
253 b
= prefs_get_boolean(kVerbose
);
255 verbose
= CFBooleanGetValue(b
);
257 /* flush the backing store */
258 IPMonitorControlPrefsRefresh();
265 __private_extern__ Boolean
266 IPMonitorControlPrefsSetVerbose(Boolean verbose
)
269 prefs_set_boolean(kVerbose
, NULL
);
272 prefs_set_boolean(kVerbose
, kCFBooleanTrue
);
274 return (IPMonitorControlPrefsSave());
277 #ifdef TEST_IPMONITORCONTROLPREFS
279 main(int argc
, char * argv
[])
281 Boolean need_usage
= FALSE
;
282 Boolean success
= FALSE
;
287 else if (strcasecmp(argv
[1], "on") == 0) {
288 success
= IPMonitorControlPrefsSetVerbose(TRUE
);
290 else if (strcasecmp(argv
[1], "off") == 0) {
291 success
= IPMonitorControlPrefsSetVerbose(FALSE
);
297 fprintf(stderr
, "usage: %s ( on | off )\n", argv
[0]);
301 fprintf(stderr
, "failed to save prefs\n");
308 #endif /* TEST_IPMONITORCONTROLPREFS */