2 * Copyright (c) 2013, 2015 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 <SystemConfiguration/SCPreferences.h>
37 #include <SystemConfiguration/SCPrivate.h>
38 #include <SystemConfiguration/scprefs_observer.h>
39 #include <TargetConditionals.h>
40 #include "IPMonitorControlPrefs.h"
43 * kIPMonitorControlPrefsID
44 * - identifies the IPMonitor preferences file that contains 'Verbose'
46 #define kIPMonitorControlPrefsIDStr "com.apple.IPMonitor.control.plist"
47 #define kIPMonitorControlPrefsID CFSTR(kIPMonitorControlPrefsIDStr)
51 * - indicates whether IPMonitor is verbose or not
53 #define kVerbose CFSTR("Verbose")
55 static SCPreferencesRef S_prefs
;
56 static IPMonitorControlPrefsCallBack S_callback
;
58 static SCPreferencesRef
59 IPMonitorControlPrefsGet(void)
61 if (S_prefs
== NULL
) {
62 IPMonitorControlPrefsInit(NULL
, NULL
);
68 prefs_changed(__unused
void * arg
)
70 os_activity_t activity_id
;
72 activity_id
= os_activity_start("processing logging preference change",
73 OS_ACTIVITY_FLAG_DEFAULT
);
75 /* get the current value */
76 if (S_callback
!= NULL
) {
77 (*S_callback
)(S_prefs
);
80 os_activity_end(activity_id
);
87 * kIPMonitorControlManangedPrefsID
88 * - identifies the location of the managed preferences file
90 #define kManagedPrefsDirStr "/Library/Managed Preferences/mobile/"
91 #define kIPMonitorControlManagedPrefsID CFSTR(kManagedPrefsDirStr \
92 kIPMonitorControlPrefsIDStr)
93 static SCPreferencesRef S_managed_prefs
;
95 static SCPreferencesRef
96 IPMonitorControlManagedPrefsGet(void)
98 if (S_managed_prefs
== NULL
) {
100 = SCPreferencesCreate(NULL
, CFSTR("IPMonitorControlPrefs"),
101 kIPMonitorControlManagedPrefsID
);
103 return (S_managed_prefs
);
107 enable_prefs_observer(CFRunLoopRef runloop
)
109 CFRunLoopSourceContext context
;
110 dispatch_block_t handler
;
111 dispatch_queue_t queue
;
112 CFRunLoopSourceRef source
;
114 bzero(&context
, sizeof(context
));
115 context
.perform
= prefs_changed
;
116 source
= CFRunLoopSourceCreate(kCFAllocatorDefault
, 0, &context
);
117 CFRunLoopAddSource(runloop
, source
, kCFRunLoopCommonModes
);
118 queue
= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0);
120 if (source
!= NULL
) {
121 CFRunLoopSourceSignal(source
);
122 if (runloop
!= NULL
) {
123 CFRunLoopWakeUp(runloop
);
127 _scprefs_observer_watch(scprefs_observer_type_global
,
128 kIPMonitorControlPrefsIDStr
,
133 #else /* TARGET_OS_IPHONE */
136 enable_prefs_observer(CFRunLoopRef runloop
)
141 #endif /* TARGET_OS_IPHONE */
144 IPMonitorControlPrefsChanged(SCPreferencesRef prefs
,
145 SCPreferencesNotification type
,
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 SCPreferencesSetCallback(S_prefs
, IPMonitorControlPrefsChanged
, NULL
);
161 SCPreferencesScheduleWithRunLoop(S_prefs
, runloop
,
162 kCFRunLoopCommonModes
);
163 enable_prefs_observer(runloop
);
169 IPMonitorControlPrefsSave(void)
171 Boolean saved
= FALSE
;
173 if (S_prefs
!= NULL
) {
174 saved
= SCPreferencesCommitChanges(S_prefs
);
175 SCPreferencesSynchronize(S_prefs
);
181 prefs_get_boolean(CFStringRef key
)
183 CFBooleanRef b
= NULL
;
186 b
= SCPreferencesGetValue(IPMonitorControlManagedPrefsGet(), key
);
187 b
= isA_CFBoolean(b
);
188 #endif /* TARGET_OS_IPHONE */
190 b
= SCPreferencesGetValue(IPMonitorControlPrefsGet(), key
);
191 b
= isA_CFBoolean(b
);
197 prefs_set_boolean(CFStringRef key
, CFBooleanRef b
)
199 SCPreferencesRef prefs
= IPMonitorControlPrefsGet();
202 if (isA_CFBoolean(b
) == NULL
) {
203 SCPreferencesRemoveValue(prefs
, key
);
206 SCPreferencesSetValue(prefs
, key
, b
);
213 IPMonitorControlPrefsRefresh(void)
215 if (S_prefs
!= NULL
) {
216 SCPreferencesSynchronize(S_prefs
);
219 if (S_managed_prefs
!= NULL
) {
220 SCPreferencesSynchronize(S_managed_prefs
);
222 #endif /* TARGET_OS_IPHONE */
229 __private_extern__ Boolean
230 IPMonitorControlPrefsIsVerbose(void)
233 Boolean verbose
= FALSE
;
235 b
= prefs_get_boolean(kVerbose
);
237 verbose
= CFBooleanGetValue(b
);
239 /* flush the backing store */
240 IPMonitorControlPrefsRefresh();
247 __private_extern__ Boolean
248 IPMonitorControlPrefsSetVerbose(Boolean verbose
)
250 if (verbose
== FALSE
) {
251 prefs_set_boolean(kVerbose
, NULL
);
254 prefs_set_boolean(kVerbose
, kCFBooleanTrue
);
256 return (IPMonitorControlPrefsSave());
259 #ifdef TEST_IPMONITORCONTROLPREFS
261 main(int argc
, char * argv
[])
263 Boolean need_usage
= FALSE
;
264 Boolean success
= FALSE
;
269 else if (strcasecmp(argv
[1], "on") == 0) {
270 success
= IPMonitorControlPrefsSetVerbose(TRUE
);
272 else if (strcasecmp(argv
[1], "off") == 0) {
273 success
= IPMonitorControlPrefsSetVerbose(FALSE
);
279 fprintf(stderr
, "usage: %s ( on | off )\n", argv
[0]);
282 if (success
== FALSE
) {
283 fprintf(stderr
, "failed to save prefs\n");
290 #endif /* TEST_IPMONITORCONTROLPREFS */