2 * Copyright (c) 2013, 2015, 2016 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();
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(__unused
void * arg
)
72 os_activity_t activity
;
74 activity
= os_activity_create("processing IPMonitor [rank] preference change",
76 OS_ACTIVITY_FLAG_DEFAULT
);
77 os_activity_scope(activity
);
79 /* get the current value */
80 if (S_callback
!= NULL
) {
81 (*S_callback
)(S_prefs
);
91 * kIPMonitorControlManangedPrefsID
92 * - identifies the location of the managed preferences file
94 #define kManagedPrefsDirStr "/Library/Managed Preferences/mobile/"
95 #define kIPMonitorControlManagedPrefsID CFSTR(kManagedPrefsDirStr \
96 kIPMonitorControlPrefsIDStr)
97 static SCPreferencesRef S_managed_prefs
;
99 static SCPreferencesRef
100 IPMonitorControlManagedPrefsGet(void)
102 if (S_managed_prefs
== NULL
) {
104 = SCPreferencesCreate(NULL
, CFSTR("IPMonitorControlPrefs"),
105 kIPMonitorControlManagedPrefsID
);
107 return (S_managed_prefs
);
111 enable_prefs_observer(CFRunLoopRef runloop
)
113 CFRunLoopSourceContext context
;
114 dispatch_block_t handler
;
115 dispatch_queue_t queue
;
116 CFRunLoopSourceRef source
;
118 bzero(&context
, sizeof(context
));
119 context
.perform
= prefs_changed
;
120 source
= CFRunLoopSourceCreate(kCFAllocatorDefault
, 0, &context
);
121 CFRunLoopAddSource(runloop
, source
, kCFRunLoopCommonModes
);
122 queue
= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0);
124 if (source
!= NULL
) {
125 CFRunLoopSourceSignal(source
);
126 if (runloop
!= NULL
) {
127 CFRunLoopWakeUp(runloop
);
131 _scprefs_observer_watch(scprefs_observer_type_global
,
132 kIPMonitorControlPrefsIDStr
,
137 #else /* TARGET_OS_IPHONE */
140 enable_prefs_observer(CFRunLoopRef runloop
)
145 #endif /* TARGET_OS_IPHONE */
148 IPMonitorControlPrefsChanged(SCPreferencesRef prefs
,
149 SCPreferencesNotification type
,
156 __private_extern__ SCPreferencesRef
157 IPMonitorControlPrefsInit(CFRunLoopRef runloop
,
158 IPMonitorControlPrefsCallBack callback
)
160 S_prefs
= SCPreferencesCreate(NULL
, CFSTR("IPMonitorControlPrefs"),
161 kIPMonitorControlPrefsID
);
162 if (runloop
!= NULL
&& callback
!= NULL
) {
163 S_callback
= callback
;
164 if (!SCPreferencesSetCallback(S_prefs
, IPMonitorControlPrefsChanged
, NULL
)) {
165 SC_log(LOG_NOTICE
, "SCPreferencesSetCallBack() failed: %s", SCErrorString(SCError()));
169 if (!SCPreferencesScheduleWithRunLoop(S_prefs
, runloop
, kCFRunLoopCommonModes
)) {
170 SC_log(LOG_NOTICE
, "SCPreferencesScheduleWithRunLoop() failed: %s", SCErrorString(SCError()));
171 (void) SCPreferencesSetCallback(S_prefs
, NULL
, NULL
);
174 enable_prefs_observer(runloop
);
182 IPMonitorControlPrefsSave(void)
184 Boolean saved
= FALSE
;
186 if (S_prefs
!= NULL
) {
187 saved
= SCPreferencesCommitChanges(S_prefs
);
188 SCPreferencesSynchronize(S_prefs
);
194 prefs_get_boolean(CFStringRef key
)
196 CFBooleanRef b
= NULL
;
199 b
= SCPreferencesGetValue(IPMonitorControlManagedPrefsGet(), key
);
200 b
= isA_CFBoolean(b
);
201 #endif /* TARGET_OS_IPHONE */
203 b
= SCPreferencesGetValue(IPMonitorControlPrefsGet(), key
);
204 b
= isA_CFBoolean(b
);
210 prefs_set_boolean(CFStringRef key
, CFBooleanRef b
)
212 SCPreferencesRef prefs
= IPMonitorControlPrefsGet();
215 if (isA_CFBoolean(b
) == NULL
) {
216 SCPreferencesRemoveValue(prefs
, key
);
219 SCPreferencesSetValue(prefs
, key
, b
);
226 IPMonitorControlPrefsRefresh(void)
228 if (S_prefs
!= NULL
) {
229 SCPreferencesSynchronize(S_prefs
);
232 if (S_managed_prefs
!= NULL
) {
233 SCPreferencesSynchronize(S_managed_prefs
);
235 #endif /* TARGET_OS_IPHONE */
242 __private_extern__ Boolean
243 IPMonitorControlPrefsIsVerbose(void)
246 Boolean verbose
= FALSE
;
248 b
= prefs_get_boolean(kVerbose
);
250 verbose
= CFBooleanGetValue(b
);
252 /* flush the backing store */
253 IPMonitorControlPrefsRefresh();
260 __private_extern__ Boolean
261 IPMonitorControlPrefsSetVerbose(Boolean verbose
)
264 prefs_set_boolean(kVerbose
, NULL
);
267 prefs_set_boolean(kVerbose
, kCFBooleanTrue
);
269 return (IPMonitorControlPrefsSave());
272 #ifdef TEST_IPMONITORCONTROLPREFS
274 main(int argc
, char * argv
[])
276 Boolean need_usage
= FALSE
;
277 Boolean success
= FALSE
;
282 else if (strcasecmp(argv
[1], "on") == 0) {
283 success
= IPMonitorControlPrefsSetVerbose(TRUE
);
285 else if (strcasecmp(argv
[1], "off") == 0) {
286 success
= IPMonitorControlPrefsSetVerbose(FALSE
);
292 fprintf(stderr
, "usage: %s ( on | off )\n", argv
[0]);
296 fprintf(stderr
, "failed to save prefs\n");
303 #endif /* TEST_IPMONITORCONTROLPREFS */