]> git.saurik.com Git - apple/configd.git/blob - Plugins/common/IPMonitorControlPrefs.c
configd-801.1.1.tar.gz
[apple/configd.git] / Plugins / common / IPMonitorControlPrefs.c
1 /*
2 * Copyright (c) 2013, 2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * IPMonitorControlPrefs.c
26 * - definitions for accessing IPMonitor control preferences
27 */
28
29 /*
30 * Modification History
31 *
32 * January 14, 2013 Dieter Siegmund (dieter@apple)
33 * - created
34 */
35
36 #include <SystemConfiguration/SCPreferences.h>
37 #include <SystemConfiguration/SCPrivate.h>
38 #include <SystemConfiguration/scprefs_observer.h>
39 #include <TargetConditionals.h>
40 #include "IPMonitorControlPrefs.h"
41
42 /*
43 * kIPMonitorControlPrefsID
44 * - identifies the IPMonitor preferences file that contains 'Verbose'
45 */
46 #define kIPMonitorControlPrefsIDStr "com.apple.IPMonitor.control.plist"
47 #define kIPMonitorControlPrefsID CFSTR(kIPMonitorControlPrefsIDStr)
48
49 /*
50 * kVerbose
51 * - indicates whether IPMonitor is verbose or not
52 */
53 #define kVerbose CFSTR("Verbose")
54
55 static SCPreferencesRef S_prefs;
56 static IPMonitorControlPrefsCallBack S_callback;
57
58 static SCPreferencesRef
59 IPMonitorControlPrefsGet(void)
60 {
61 if (S_prefs == NULL) {
62 IPMonitorControlPrefsInit(NULL, NULL);
63 }
64 return (S_prefs);
65 }
66
67 static void
68 prefs_changed(__unused void * arg)
69 {
70 os_activity_t activity_id;
71
72 activity_id = os_activity_start("processing logging preference change",
73 OS_ACTIVITY_FLAG_DEFAULT);
74
75 /* get the current value */
76 if (S_callback != NULL) {
77 (*S_callback)(S_prefs);
78 }
79
80 os_activity_end(activity_id);
81
82 return;
83 }
84
85 #if TARGET_OS_IPHONE
86 /*
87 * kIPMonitorControlManangedPrefsID
88 * - identifies the location of the managed preferences file
89 */
90 #define kManagedPrefsDirStr "/Library/Managed Preferences/mobile/"
91 #define kIPMonitorControlManagedPrefsID CFSTR(kManagedPrefsDirStr \
92 kIPMonitorControlPrefsIDStr)
93 static SCPreferencesRef S_managed_prefs;
94
95 static SCPreferencesRef
96 IPMonitorControlManagedPrefsGet(void)
97 {
98 if (S_managed_prefs == NULL) {
99 S_managed_prefs
100 = SCPreferencesCreate(NULL, CFSTR("IPMonitorControlPrefs"),
101 kIPMonitorControlManagedPrefsID);
102 }
103 return (S_managed_prefs);
104 }
105
106 static void
107 enable_prefs_observer(CFRunLoopRef runloop)
108 {
109 CFRunLoopSourceContext context;
110 dispatch_block_t handler;
111 dispatch_queue_t queue;
112 CFRunLoopSourceRef source;
113
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);
119 handler = ^{
120 if (source != NULL) {
121 CFRunLoopSourceSignal(source);
122 if (runloop != NULL) {
123 CFRunLoopWakeUp(runloop);
124 }
125 };
126 };
127 _scprefs_observer_watch(scprefs_observer_type_global,
128 kIPMonitorControlPrefsIDStr,
129 queue, handler);
130 return;
131 }
132
133 #else /* TARGET_OS_IPHONE */
134
135 static void
136 enable_prefs_observer(CFRunLoopRef runloop)
137 {
138 return;
139 }
140
141 #endif /* TARGET_OS_IPHONE */
142
143 static void
144 IPMonitorControlPrefsChanged(SCPreferencesRef prefs,
145 SCPreferencesNotification type,
146 void * info)
147 {
148 prefs_changed(NULL);
149 return;
150 }
151
152 __private_extern__ SCPreferencesRef
153 IPMonitorControlPrefsInit(CFRunLoopRef runloop,
154 IPMonitorControlPrefsCallBack callback)
155 {
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);
164 }
165 return (S_prefs);
166 }
167
168 static Boolean
169 IPMonitorControlPrefsSave(void)
170 {
171 Boolean saved = FALSE;
172
173 if (S_prefs != NULL) {
174 saved = SCPreferencesCommitChanges(S_prefs);
175 SCPreferencesSynchronize(S_prefs);
176 }
177 return (saved);
178 }
179
180 static CFBooleanRef
181 prefs_get_boolean(CFStringRef key)
182 {
183 CFBooleanRef b = NULL;
184
185 #if TARGET_OS_IPHONE
186 b = SCPreferencesGetValue(IPMonitorControlManagedPrefsGet(), key);
187 b = isA_CFBoolean(b);
188 #endif /* TARGET_OS_IPHONE */
189 if (b == NULL) {
190 b = SCPreferencesGetValue(IPMonitorControlPrefsGet(), key);
191 b = isA_CFBoolean(b);
192 }
193 return (b);
194 }
195
196 static void
197 prefs_set_boolean(CFStringRef key, CFBooleanRef b)
198 {
199 SCPreferencesRef prefs = IPMonitorControlPrefsGet();
200
201 if (prefs != NULL) {
202 if (isA_CFBoolean(b) == NULL) {
203 SCPreferencesRemoveValue(prefs, key);
204 }
205 else {
206 SCPreferencesSetValue(prefs, key, b);
207 }
208 }
209 return;
210 }
211
212 static void
213 IPMonitorControlPrefsRefresh(void)
214 {
215 if (S_prefs != NULL) {
216 SCPreferencesSynchronize(S_prefs);
217 }
218 #if TARGET_OS_IPHONE
219 if (S_managed_prefs != NULL) {
220 SCPreferencesSynchronize(S_managed_prefs);
221 }
222 #endif /* TARGET_OS_IPHONE */
223 return;
224 }
225
226 /**
227 ** Get
228 **/
229 __private_extern__ Boolean
230 IPMonitorControlPrefsIsVerbose(void)
231 {
232 CFBooleanRef b;
233 Boolean verbose = FALSE;
234
235 b = prefs_get_boolean(kVerbose);
236 if (b != NULL) {
237 verbose = CFBooleanGetValue(b);
238 }
239 /* flush the backing store */
240 IPMonitorControlPrefsRefresh();
241 return (verbose);
242 }
243
244 /**
245 ** Set
246 **/
247 __private_extern__ Boolean
248 IPMonitorControlPrefsSetVerbose(Boolean verbose)
249 {
250 if (verbose == FALSE) {
251 prefs_set_boolean(kVerbose, NULL);
252 }
253 else {
254 prefs_set_boolean(kVerbose, kCFBooleanTrue);
255 }
256 return (IPMonitorControlPrefsSave());
257 }
258
259 #ifdef TEST_IPMONITORCONTROLPREFS
260 int
261 main(int argc, char * argv[])
262 {
263 Boolean need_usage = FALSE;
264 Boolean success = FALSE;
265
266 if (argc < 2) {
267 need_usage = TRUE;
268 }
269 else if (strcasecmp(argv[1], "on") == 0) {
270 success = IPMonitorControlPrefsSetVerbose(TRUE);
271 }
272 else if (strcasecmp(argv[1], "off") == 0) {
273 success = IPMonitorControlPrefsSetVerbose(FALSE);
274 }
275 else {
276 need_usage = TRUE;
277 }
278 if (need_usage) {
279 fprintf(stderr, "usage: %s ( on | off )\n", argv[0]);
280 exit(1);
281 }
282 if (success == FALSE) {
283 fprintf(stderr, "failed to save prefs\n");
284 exit(2);
285 }
286 exit(0);
287 return (0);
288 }
289
290 #endif /* TEST_IPMONITORCONTROLPREFS */