1/*
2 * Copyright (c) 2000-2004, 2010, 2011 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 * June 1, 2001			Allan Nathanson <ajn@apple.com>
28 * - public API conversion
29 *
30 * November 9, 2000		Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34#include "scutil.h"
35#include "cache.h"
36#include "session.h"
37#include "notifications.h"
38
39
40static void
41reconnected(SCDynamicStoreRef store, void *info)
42{
43	SCPrint(TRUE, stdout, CFSTR("SCDynamicStore server restarted, session reconnected\n"));
44	return;
45}
46
47
48__private_extern__
49void
50do_open(int argc, char **argv)
51{
52	if (store) {
53		CFRelease(store);
54		CFRelease(watchedKeys);
55		CFRelease(watchedPatterns);
56	}
57
58	if (argc < 1) {
59		store = SCDynamicStoreCreate(NULL, CFSTR("scutil"), storeCallback, NULL);
60	} else {
61		CFMutableDictionaryRef	options;
62
63		options = CFDictionaryCreateMutable(NULL,
64						    0,
65						    &kCFTypeDictionaryKeyCallBacks,
66						    &kCFTypeDictionaryValueCallBacks);
67		CFDictionarySetValue(options, kSCDynamicStoreUseSessionKeys, kCFBooleanTrue);
68		store = SCDynamicStoreCreateWithOptions(NULL,
69							CFSTR("scutil"),
70							options,
71							storeCallback,
72							NULL);
73		CFRelease(options);
74	}
75	if (store == NULL) {
76		SCPrint(TRUE, stdout, CFSTR("  %s\n"), SCErrorString(SCError()));
77		return;
78	}
79
80	(void) SCDynamicStoreSetDisconnectCallBack(store, reconnected);
81
82	watchedKeys     = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
83	watchedPatterns = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
84
85	cache_close();
86
87	return;
88}
89
90
91__private_extern__
92void
93do_close(int argc, char **argv)
94{
95	if (notifyRls != NULL) {
96		if (doDispatch) {
97			(void) SCDynamicStoreSetDispatchQueue(store, NULL);
98		} else {
99			CFRunLoopSourceInvalidate(notifyRls);
100			CFRelease(notifyRls);
101		}
102		notifyRls = NULL;
103	}
104
105	if (notifyRl != NULL) {
106		CFRunLoopStop(notifyRl);
107		notifyRl  = NULL;
108	}
109
110	if (store != NULL) {
111		CFRelease(store);
112		store = NULL;
113		CFRelease(watchedKeys);
114		watchedKeys = NULL;
115		CFRelease(watchedPatterns);
116		watchedPatterns = NULL;
117	}
118
119	cache_close();
120
121	return;
122}
123