]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/pattern.c
configd-395.11.tar.gz
[apple/configd.git] / configd.tproj / pattern.c
1 /*
2 * Copyright (c) 2003, 2004, 2006-2008 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 * Modification History
26 *
27 * April 23, 2003 Allan Nathanson <ajn@apple.com>
28 * - initial revision
29 */
30
31
32 #include "configd.h"
33 #include "pattern.h"
34
35
36 /*
37 * Notes:
38 *
39 * - pattern matching information is maintained in a dictionary (patternData).
40 * - the dictionary "key" is the regular expression being matched
41 * - the dictionary "value" is a CFArray with the following contents
42 * [0] = CFData consisting of the pre-compiled regular expression
43 * [1] = CFArray[CFNumber] consisting of the sessions watching this pattern
44 * [2-n] = dynamic store keys which match this pattern
45 */
46
47
48 typedef struct {
49 CFMutableArrayRef pInfo;
50 regex_t *preg;
51 } addContext, *addContextRef;
52
53
54 static __inline__ void
55 my_CFDictionaryApplyFunction(CFDictionaryRef theDict,
56 CFDictionaryApplierFunction applier,
57 void *context)
58 {
59 CFAllocatorRef myAllocator;
60 CFDictionaryRef myDict;
61
62 myAllocator = CFGetAllocator(theDict);
63 myDict = CFDictionaryCreateCopy(myAllocator, theDict);
64 CFDictionaryApplyFunction(myDict, applier, context);
65 CFRelease(myDict);
66 return;
67 }
68
69
70 static void
71 identifyKeyForPattern(const void *key, void *val, void *context)
72 {
73 CFStringRef storeKey = (CFStringRef)key;
74 CFDictionaryRef storeValue = (CFDictionaryRef)val;
75 CFMutableArrayRef pInfo = ((addContextRef)context)->pInfo;
76 regex_t * preg = ((addContextRef)context)->preg;
77
78 CFIndex len;
79 int reError;
80 char str_q[256];
81 char * str = str_q;
82
83 if (!CFDictionaryContainsKey(storeValue, kSCDData)) {
84 /* if no data (yet) */
85 return;
86 }
87
88 /* convert store key to C string */
89 len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(storeKey), kCFStringEncodingASCII) + 1;
90 if (len > (CFIndex)sizeof(str_q))
91 str = CFAllocatorAllocate(NULL, len, 0);
92 if (_SC_cfstring_to_cstring(storeKey, str, len, kCFStringEncodingASCII) == NULL) {
93 SCLog(TRUE, LOG_DEBUG, CFSTR("identifyKeyForPattern(): could not convert store key to C string"));
94 goto done;
95 }
96
97 /* compare store key to new notification keys regular expression pattern */
98 reError = regexec(preg, str, 0, NULL, 0);
99 switch (reError) {
100 case 0 :
101 /* we've got a match */
102 CFArrayAppendValue(pInfo, storeKey);
103 break;
104 case REG_NOMATCH :
105 /* no match */
106 break;
107 default : {
108 char reErrBuf[256];
109
110 (void)regerror(reError, preg, reErrBuf, sizeof(reErrBuf));
111 SCLog(TRUE, LOG_DEBUG, CFSTR("identifyKeyForPattern regexec(): %s"), reErrBuf);
112 break;
113 }
114 }
115
116 done :
117
118 if (str != str_q) CFAllocatorDeallocate(NULL, str);
119 return;
120 }
121
122
123 static Boolean
124 patternCompile(CFStringRef pattern, regex_t *preg, CFStringRef *error)
125 {
126 Boolean append = FALSE;
127 Boolean insert = FALSE;
128 CFIndex len = 0;
129 CFIndex len_c;
130 Boolean ok;
131 char str_q[256];
132 char * str = str_q;
133
134 if (CFStringGetLength(pattern) == 0) {
135 SCLog(TRUE, LOG_ERR, CFSTR("patternCompile(): empty string"));
136 }
137
138 if (!CFStringHasPrefix(pattern, CFSTR("^"))) {
139 insert = TRUE;
140 }
141
142 if (!CFStringHasSuffix(pattern, CFSTR("$")) ||
143 CFStringHasSuffix(pattern, CFSTR("\\$"))) {
144 append = TRUE;
145 }
146
147 /* if regex pattern is not bounded at both ends */
148 if (insert || append) {
149 pattern = CFStringCreateWithFormat(NULL,
150 NULL,
151 CFSTR("%s%@%s"),
152 insert ? "^" : "",
153 pattern,
154 append ? "$" : "");
155 }
156
157 len_c = CFStringGetBytes(pattern,
158 CFRangeMake(0, CFStringGetLength(pattern)),
159 kCFStringEncodingASCII,
160 0,
161 FALSE,
162 NULL,
163 0,
164 &len);
165 if (len_c <= 0) {
166 SCLog(TRUE, LOG_ERR, CFSTR("patternCompile(): could not get buffer length for \"%@\""), pattern);
167 len = sizeof(str_q) - 1;
168 }
169 if (++len > (CFIndex)sizeof(str_q)) {
170 str = CFAllocatorAllocate(NULL, len, 0);
171 }
172 ok = (_SC_cfstring_to_cstring(pattern, str, len, kCFStringEncodingASCII) != NULL);
173 if (insert || append) {
174 CFRelease(pattern);
175 }
176 if (ok) {
177 int reError;
178
179 reError = regcomp(preg, str, REG_EXTENDED);
180 if (reError != 0) {
181 char reErrBuf[256];
182
183 (void)regerror(reError, preg, reErrBuf, sizeof(reErrBuf));
184 *error = CFStringCreateWithCString(NULL, reErrBuf, kCFStringEncodingASCII);
185 #ifdef DEBUG
186 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("patternCompile regcomp(%s) failed: %s"), str, reErrBuf);
187 #endif /* DEBUG */
188 ok = FALSE;
189 }
190 } else {
191 *error = CFRetain(CFSTR("could not convert pattern to regex string"));
192 #ifdef DEBUG
193 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("%@"), *error);
194 #endif /* DEBUG */
195 }
196
197 if (str != str_q) CFAllocatorDeallocate(NULL, str);
198 return ok;
199 }
200
201
202 static CFMutableArrayRef
203 patternCopy(CFStringRef pattern)
204 {
205 CFArrayRef pInfo;
206
207 pInfo = CFDictionaryGetValue(patternData, pattern);
208 return pInfo ? CFArrayCreateMutableCopy(NULL, 0, pInfo) : NULL;
209 }
210
211
212 static CFMutableArrayRef
213 patternNew(CFStringRef pattern)
214 {
215 addContext context;
216 CFStringRef err = NULL;
217 CFMutableArrayRef pInfo;
218 CFMutableDataRef pRegex;
219 CFArrayRef pSessions;
220
221 /* create the pattern info */
222 pInfo = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
223
224 /* compile the regular expression from the pattern string. */
225 pRegex = CFDataCreateMutable(NULL, sizeof(regex_t));
226 CFDataSetLength(pRegex, sizeof(regex_t));
227 if (!patternCompile(pattern, (regex_t *)CFDataGetBytePtr(pRegex), &err)) {
228 CFRelease(err);
229 CFRelease(pRegex);
230 CFRelease(pInfo);
231 return NULL;
232 }
233
234 /* add the compiled regular expression to the pattern info */
235 CFArrayAppendValue(pInfo, pRegex);
236
237 /* add the initial (empty) list of sessions watching this pattern */
238 pSessions = CFArrayCreate(NULL, NULL, 0, &kCFTypeArrayCallBacks);
239 CFArrayAppendValue(pInfo, pSessions);
240 CFRelease(pSessions);
241
242 /* identify/add all existing keys that match the specified pattern */
243 context.pInfo = pInfo;
244 context.preg = (regex_t *)CFDataGetBytePtr(pRegex);
245 my_CFDictionaryApplyFunction(storeData,
246 (CFDictionaryApplierFunction)identifyKeyForPattern,
247 &context);
248
249 CFRelease(pRegex);
250 return pInfo;
251 }
252
253
254 __private_extern__
255 CFArrayRef
256 patternCopyMatches(CFStringRef pattern)
257 {
258 Boolean isNew = FALSE;
259 CFArrayRef keys;
260 CFMutableArrayRef pInfo;
261
262 /* find (or create new instance of) this pattern */
263 pInfo = patternCopy(pattern);
264 if (pInfo == NULL) {
265 /* if new pattern */
266 pInfo = patternNew(pattern);
267 if (pInfo == NULL) {
268 return NULL;
269 }
270
271 isNew = TRUE;
272 }
273
274 if (isNew) {
275 CFDataRef pRegex;
276
277 pRegex = CFArrayGetValueAtIndex(pInfo, 0);
278 regfree((regex_t *)CFDataGetBytePtr(pRegex));
279 }
280
281 CFArrayReplaceValues(pInfo, CFRangeMake(0, 2), NULL, 0);
282 keys = CFArrayCreateCopy(NULL, pInfo);
283 CFRelease(pInfo);
284
285 return keys;
286 }
287
288
289 __private_extern__
290 Boolean
291 patternAddSession(CFStringRef pattern, CFNumberRef sessionNum)
292 {
293 CFIndex i;
294 CFIndex n;
295 CFMutableArrayRef pInfo;
296 CFMutableArrayRef pSessions;
297
298 /* find (or create new instance of) this pattern */
299 pInfo = patternCopy(pattern);
300 if (pInfo == NULL) {
301 /* if new pattern */
302 pInfo = patternNew(pattern);
303 if (pInfo == NULL) {
304 return FALSE;
305 }
306 }
307
308 /* add this session as a pattern watcher */
309 pSessions = (CFMutableArrayRef)CFArrayGetValueAtIndex(pInfo, 1);
310 pSessions = CFArrayCreateMutableCopy(NULL, 0, pSessions);
311 CFArrayAppendValue(pSessions, sessionNum);
312 CFArraySetValueAtIndex(pInfo, 1, pSessions);
313 CFRelease(pSessions);
314
315 /* update pattern watcher info */
316 CFDictionarySetValue(patternData, pattern, pInfo);
317
318 /* add this session as a watcher of any existing keys */
319 n = CFArrayGetCount(pInfo);
320 for (i = 2; i < n; i++) {
321 CFStringRef matchingKey;
322
323 matchingKey = CFArrayGetValueAtIndex(pInfo, i);
324 _addWatcher(sessionNum, matchingKey);
325 }
326
327 CFRelease(pInfo);
328 return TRUE;
329 }
330
331
332 __private_extern__
333 void
334 patternRemoveSession(CFStringRef pattern, CFNumberRef sessionNum)
335 {
336 CFIndex i;
337 CFIndex n;
338 CFMutableArrayRef pInfo;
339 CFDataRef pRegex;
340 CFMutableArrayRef pSessions;
341
342 /* find instance of this pattern */
343 pInfo = patternCopy(pattern);
344
345 /* remove this session as a watcher from all matching keys */
346 n = CFArrayGetCount(pInfo);
347 for (i = 2; i < n; i++) {
348 CFStringRef matchingKey;
349
350 matchingKey = CFArrayGetValueAtIndex(pInfo, i);
351 _removeWatcher(sessionNum, matchingKey);
352 }
353
354 /* remove session from watchers */
355 pSessions = (CFMutableArrayRef)CFArrayGetValueAtIndex(pInfo, 1);
356 n = CFArrayGetCount(pSessions);
357 if (n > 1) {
358 /* if other sessions are watching this pattern */
359
360 pSessions = CFArrayCreateMutableCopy(NULL, 0, pSessions);
361 i = CFArrayGetFirstIndexOfValue(pSessions, CFRangeMake(0, n), sessionNum);
362 CFArrayRemoveValueAtIndex(pSessions, i);
363 CFArraySetValueAtIndex(pInfo, 1, pSessions);
364 CFRelease(pSessions);
365
366 CFDictionarySetValue(patternData, pattern, pInfo);
367 } else {
368 /* if no other sessions are watching this pattern */
369
370 pRegex = CFArrayGetValueAtIndex(pInfo, 0);
371 regfree((regex_t *)CFDataGetBytePtr(pRegex));
372 CFDictionaryRemoveValue(patternData, pattern);
373 }
374
375 CFRelease(pInfo);
376 return;
377 }
378
379
380 static void
381 addKeyForPattern(const void *key, void *val, void *context)
382 {
383 CFStringRef pattern = (CFStringRef)key;
384 CFArrayRef pInfo = (CFArrayRef)val;
385 CFStringRef storeKey = (CFStringRef)context;
386
387 CFIndex len;
388 regex_t *preg;
389 int reError;
390 char str_q[256];
391 char * str = str_q;
392
393 /* convert store key to C string */
394 len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(storeKey), kCFStringEncodingASCII) + 1;
395 if (len > (CFIndex)sizeof(str_q))
396 str = CFAllocatorAllocate(NULL, len, 0);
397 if (_SC_cfstring_to_cstring(storeKey, str, len, kCFStringEncodingASCII) == NULL) {
398 SCLog(TRUE, LOG_DEBUG, CFSTR("addKeyForPattern(): could not convert store key to C string"));
399 goto done;
400 }
401
402 /* compare new store key to regular expression pattern */
403 preg = (regex_t *)CFDataGetBytePtr(CFArrayGetValueAtIndex(pInfo, 0));
404 reError = regexec(preg, str, 0, NULL, 0);
405 switch (reError) {
406 case 0 : {
407 /*
408 * we've got a match
409 */
410 CFIndex i;
411 CFIndex n;
412 CFMutableArrayRef pInfo_new;
413 CFArrayRef pSessions;
414
415 /* add watchers */
416 pSessions = CFArrayGetValueAtIndex(pInfo, 1);
417 n = CFArrayGetCount(pSessions);
418 for (i = 0; i < n; i++) {
419 CFNumberRef sessionNum = CFArrayGetValueAtIndex(pSessions, i);
420
421 _addWatcher(sessionNum, storeKey);
422 }
423
424 /* add key, update pattern watcher info */
425 pInfo_new = CFArrayCreateMutableCopy(NULL, 0, pInfo);
426 CFArrayAppendValue(pInfo_new, storeKey);
427 CFDictionarySetValue(patternData, pattern, pInfo_new);
428 CFRelease(pInfo_new);
429 break;
430 }
431 case REG_NOMATCH :
432 /* no match */
433 break;
434 default : {
435 char reErrBuf[256];
436
437 (void)regerror(reError, preg, reErrBuf, sizeof(reErrBuf));
438 SCLog(TRUE, LOG_DEBUG, CFSTR("addKeyForPattern regexec(): %s"), reErrBuf);
439 break;
440 }
441 }
442
443 done :
444
445 if (str != str_q) CFAllocatorDeallocate(NULL, str);
446 return;
447 }
448
449
450 __private_extern__
451 void
452 patternAddKey(CFStringRef key)
453 {
454 void *context = (void *)key;
455
456 my_CFDictionaryApplyFunction(patternData,
457 (CFDictionaryApplierFunction)addKeyForPattern,
458 context);
459
460 return;
461 }
462
463
464 static void
465 removeKeyFromPattern(const void *key, void *val, void *context)
466 {
467 CFStringRef pattern = (CFStringRef)key;
468 CFArrayRef pInfo = (CFArrayRef)val;
469 CFStringRef storeKey = (CFStringRef)context;
470
471 CFIndex i;
472 CFIndex n;
473 CFMutableArrayRef pInfo_new;
474 CFArrayRef pSessions;
475
476 n = CFArrayGetCount(pInfo);
477 if (n <= 2) {
478 /* if no keys match this pattern */
479 return;
480 }
481
482 i = CFArrayGetFirstIndexOfValue(pInfo, CFRangeMake(2, n-2), storeKey);
483 if (i == kCFNotFound) {
484 /* if this key wasn't matched by this pattern */
485 return;
486 }
487
488 /* remove key from pattern info */
489 pInfo_new = CFArrayCreateMutableCopy(NULL, 0, pInfo);
490 CFArrayRemoveValueAtIndex(pInfo_new, i);
491
492 /* remove watchers */
493 pSessions = CFArrayGetValueAtIndex(pInfo_new, 1);
494 n = CFArrayGetCount(pSessions);
495 for (i = 0; i < n; i++) {
496 CFNumberRef sessionNum = CFArrayGetValueAtIndex(pSessions, i);
497
498 _removeWatcher(sessionNum, storeKey);
499 }
500
501 CFDictionarySetValue(patternData, pattern, pInfo_new);
502 CFRelease(pInfo_new);
503 return;
504 }
505
506
507 __private_extern__
508 void
509 patternRemoveKey(CFStringRef key)
510 {
511 void *context = (void *)key;
512
513 my_CFDictionaryApplyFunction(patternData,
514 (CFDictionaryApplierFunction)removeKeyFromPattern,
515 context);
516
517 return;
518 }