]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/RootDomainUserClient.cpp
69c0dfa1a797754d69db54401d75237fedef8116
[apple/xnu.git] / iokit / Kernel / RootDomainUserClient.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
30 *
31 */
32
33 #include <IOKit/assert.h>
34 #include <IOKit/IOLib.h>
35 #include <IOKit/IOKitKeys.h>
36 #include <IOKit/IOBufferMemoryDescriptor.h>
37 #include "RootDomainUserClient.h"
38 #include <IOKit/pwr_mgt/IOPMLibDefs.h>
39
40 #define super IOUserClient
41
42 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
43
44 OSDefineMetaClassAndStructors(RootDomainUserClient, IOUserClient)
45
46 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47
48 bool RootDomainUserClient::initWithTask(task_t owningTask, void *security_id,
49 UInt32 type, OSDictionary * properties)
50 {
51 if (properties)
52 properties->setObject(kIOUserClientCrossEndianCompatibleKey, kOSBooleanTrue);
53
54 if (!super::initWithTask(owningTask, security_id, type, properties))
55 return false;
56
57 fOwningTask = owningTask;
58 task_reference (fOwningTask);
59 return true;
60 }
61
62
63 bool RootDomainUserClient::start( IOService * provider )
64 {
65 assert(OSDynamicCast(IOPMrootDomain, provider));
66 if(!super::start(provider))
67 return false;
68 fOwner = (IOPMrootDomain *)provider;
69
70
71 return true;
72 }
73
74 IOReturn RootDomainUserClient::secureSleepSystem( uint32_t *return_code )
75 {
76 IOByteCount return_code_size = 1;
77
78 return secureSleepSystemOptions( NULL, // inOptions
79 (void *)return_code, // returnCode
80 (void *)0, // inSize
81 (void *)&return_code_size, // returnSize
82 NULL, NULL);
83 }
84
85 IOReturn RootDomainUserClient::secureSleepSystemOptions(
86 void * p1, void * p2, void * p3,
87 void * p4, void * p5, void * p6 )
88 {
89 void *inOptions = (void *)p1;
90 uint32_t *returnCode = (uint32_t *)p2;
91 // IOByteCount inOptionsSize = (uintptr_t)p3;
92 IOByteCount *returnCodeSize = (IOByteCount *)p4;
93
94 int local_priv = 0;
95 int admin_priv = 0;
96 IOReturn ret = kIOReturnNotPrivileged;
97 OSDictionary *unserializedOptions = NULL;
98 OSString *unserializeErrorString = NULL;
99
100 ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeLocalUser);
101 local_priv = (kIOReturnSuccess == ret);
102
103 ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator);
104 admin_priv = (kIOReturnSuccess == ret);
105
106 *returnCodeSize = sizeof(uint32_t);
107
108 if (inOptions)
109 {
110 unserializedOptions = OSDynamicCast( OSDictionary,
111 OSUnserializeXML((const char *)inOptions, &unserializeErrorString));
112
113 if (!unserializedOptions) {
114 IOLog("IOPMRootDomain SleepSystem unserialization failure: %s\n",
115 unserializeErrorString ? unserializeErrorString->getCStringNoCopy() : "Unknown");
116 }
117 }
118
119 if ( (local_priv || admin_priv)
120 && fOwner )
121 {
122 if (unserializedOptions)
123 {
124 // Publish Sleep Options in registry under root_domain
125 fOwner->setProperty( kRootDomainSleepOptionsKey, unserializedOptions);
126
127 *returnCode = fOwner->sleepSystemOptions( unserializedOptions );
128
129 unserializedOptions->release();
130 } else {
131 // No options
132 // Clear any pre-existing options
133 fOwner->removeProperty( kRootDomainSleepOptionsKey );
134
135 *returnCode = fOwner->sleepSystemOptions( NULL );
136 }
137
138 } else {
139 *returnCode = kIOReturnNotPrivileged;
140 }
141
142 return kIOReturnSuccess;
143 }
144
145 IOReturn RootDomainUserClient::secureSetAggressiveness(
146 unsigned long type,
147 unsigned long newLevel,
148 int *return_code )
149 {
150 int local_priv = 0;
151 int admin_priv = 0;
152 IOReturn ret = kIOReturnNotPrivileged;
153
154 ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeLocalUser);
155 local_priv = (kIOReturnSuccess == ret);
156
157 ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator);
158 admin_priv = (kIOReturnSuccess == ret);
159
160 if((local_priv || admin_priv) && fOwner) {
161 *return_code = fOwner->setAggressiveness(type, newLevel);
162 return kIOReturnSuccess;
163 } else {
164 *return_code = kIOReturnNotPrivileged;
165 return kIOReturnSuccess;
166 }
167 }
168
169 IOReturn RootDomainUserClient::secureSetMaintenanceWakeCalendar(
170 void * p1, void * p2, void * p3,
171 void * p4, void * p5, void * p6 )
172 {
173 #if ROOT_DOMAIN_RUN_STATES
174 IOPMCalendarStruct * inCalendar = (IOPMCalendarStruct *) p1;
175 uint32_t * returnCode = (uint32_t *) p2;
176 IOByteCount * returnCodeSize = (IOByteCount *) p4;
177 int admin_priv = 0;
178 IOReturn ret = kIOReturnNotPrivileged;
179
180 ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator);
181 admin_priv = (kIOReturnSuccess == ret);
182
183 *returnCodeSize = sizeof(uint32_t);
184
185 if (admin_priv && fOwner) {
186 *returnCode = fOwner->setMaintenanceWakeCalendar(inCalendar);
187 return kIOReturnSuccess;
188 } else {
189 *returnCode = kIOReturnNotPrivileged;
190 return kIOReturnSuccess;
191 }
192 #else
193 return kIOReturnUnsupported;
194 #endif
195 }
196
197 IOReturn RootDomainUserClient::secureSetUserAssertionLevels(
198 uint32_t assertBits )
199 {
200 int admin_priv = 0;
201 IOReturn ret = kIOReturnNotPrivileged;
202
203 ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator);
204 admin_priv = (kIOReturnSuccess == ret);
205
206 if (admin_priv && fOwner) {
207 ret = fOwner->setPMAssertionUserLevels(assertBits);
208 } else {
209 ret = kIOReturnNotPrivileged;
210 }
211 return kIOReturnSuccess;
212 }
213
214 IOReturn RootDomainUserClient::clientClose( void )
215 {
216 detach(fOwner);
217
218 if(fOwningTask) {
219 task_deallocate(fOwningTask);
220 fOwningTask = 0;
221 }
222
223 return kIOReturnSuccess;
224 }
225
226 IOExternalMethod *
227 RootDomainUserClient::getTargetAndMethodForIndex( IOService ** targetP, UInt32 index )
228 {
229 static const IOExternalMethod sMethods[] = {
230 { // kPMSetAggressiveness, 0
231 (IOService *)1, (IOMethod)&RootDomainUserClient::secureSetAggressiveness, kIOUCScalarIScalarO, 2, 1
232 },
233 { // kPMGetAggressiveness, 1
234 0, (IOMethod)&IOPMrootDomain::getAggressiveness, kIOUCScalarIScalarO, 1, 1
235 },
236 { // kPMSleepSystem, 2
237 (IOService *)1, (IOMethod)&RootDomainUserClient::secureSleepSystem, kIOUCScalarIScalarO, 0, 1
238 },
239 { // kPMAllowPowerChange, 3
240 0, (IOMethod)&IOPMrootDomain::allowPowerChange, kIOUCScalarIScalarO, 1, 0
241 },
242 { // kPMCancelPowerChange, 4
243 0, (IOMethod)&IOPMrootDomain::cancelPowerChange, kIOUCScalarIScalarO, 1, 0
244 },
245 { // kPMShutdownSystem, 5
246 0, (IOMethod)&IOPMrootDomain::shutdownSystem, kIOUCScalarIScalarO, 0, 0
247 },
248 { // kPMRestartSystem, 6
249 0, (IOMethod)&IOPMrootDomain::restartSystem, kIOUCScalarIScalarO, 0, 0
250 },
251 { // kPMSleepSystemOptions, 7
252 (IOService *)1, (IOMethod)&RootDomainUserClient::secureSleepSystemOptions,
253 kIOUCStructIStructO, kIOUCVariableStructureSize, sizeof(uint32_t)
254 },
255 { // kPMSetMaintenanceWakeCalendar, 8
256 (IOService *)1, (IOMethod)&RootDomainUserClient::secureSetMaintenanceWakeCalendar,
257 kIOUCStructIStructO, sizeof(IOPMCalendarStruct), sizeof(uint32_t)
258 },
259 { // kPMSetUserAssertionLevels, 9
260 (IOService *)1, (IOMethod)&RootDomainUserClient::secureSetUserAssertionLevels,
261 kIOUCScalarIScalarO, 1, 0
262 }
263 };
264
265 if(index >= kNumPMMethods)
266 return NULL;
267 else {
268 if (sMethods[index].object)
269 *targetP = this;
270 else
271 *targetP = fOwner;
272
273 return (IOExternalMethod *)&sMethods[index];
274 }
275 }
276
277 #if 0
278 IOReturn RootDomainUserClient::externalMethod( uint32_t selector, IOExternalMethodArguments * args,
279 IOExternalMethodDispatch * dispatch, OSObject * target, void * reference )
280 {
281 static const IOExternalMethodDispatch sMethods[] = {
282 { // kPMSetAggressiveness, 0
283 (IOService *)1, (IOMethod)&RootDomainUserClient::secureSetAggressiveness, kIOUCScalarIScalarO, 2, 1
284 },
285 { // kPMGetAggressiveness, 1
286 0, (IOMethod)&IOPMrootDomain::getAggressiveness, kIOUCScalarIScalarO, 1, 1
287 },
288 { // kPMSleepSystem, 2
289 (IOService *)1, (IOMethod)&RootDomainUserClient::secureSleepSystem, kIOUCScalarIScalarO, 0, 1
290 },
291 { // kPMAllowPowerChange, 3
292 0, (IOMethod)&IOPMrootDomain::allowPowerChange, kIOUCScalarIScalarO, 1, 0
293 },
294 { // kPMCancelPowerChange, 4
295 0, (IOMethod)&IOPMrootDomain::cancelPowerChange, kIOUCScalarIScalarO, 1, 0
296 },
297 { // kPMShutdownSystem, 5
298 0, (IOMethod)&IOPMrootDomain::shutdownSystem, kIOUCScalarIScalarO, 0, 0
299 },
300 { // kPMRestartSystem, 6
301 0, (IOMethod)&IOPMrootDomain::restartSystem, kIOUCScalarIScalarO, 0, 0
302 },
303 { // kPMSetPreventative, 7
304 (IOService *)1, (IOMethod)&RootDomainUserClient::setPreventative, kIOUCScalarIScalarO, 2, 0
305 },
306 };
307
308 if (selector > (sizeof(sMethods) / sizeof(sMethods[0])))
309 return (kIOReturnBadArgument);
310
311 if ((1 << selector) & ((1 << 0) | (1 << 7))
312 target = this;
313 else
314 target = fOwner;
315
316 return (super::externalMethod(selector, args, &sMethods[selector], target, 0));
317 }
318 #endif
319
320 void
321 RootDomainUserClient::setPreventative(UInt32 on_off, UInt32 types_of_sleep)
322 {
323 return;
324 }
325