]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/RootDomainUserClient.cpp
646ccec4e38d9224d9175dc3167db21ce10c955c
[apple/xnu.git] / iokit / Kernel / RootDomainUserClient.cpp
1 /*
2 * Copyright (c) 1998-2012 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 #include <IOKit/pwr_mgt/IOPMPrivate.h>
40 #include <sys/proc.h>
41
42 #define super IOUserClient
43
44 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
45
46 OSDefineMetaClassAndStructors(RootDomainUserClient, IOUserClient)
47
48 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
49
50 bool
51 RootDomainUserClient::initWithTask(task_t owningTask, void *security_id,
52 UInt32 type, OSDictionary * properties)
53 {
54 if (properties) {
55 properties->setObject(kIOUserClientCrossEndianCompatibleKey, kOSBooleanTrue);
56 }
57
58 if (!super::initWithTask(owningTask, security_id, type, properties)) {
59 return false;
60 }
61
62 fOwningTask = owningTask;
63 task_reference(fOwningTask);
64 return true;
65 }
66
67
68 bool
69 RootDomainUserClient::start( IOService * provider )
70 {
71 assert(OSDynamicCast(IOPMrootDomain, provider));
72 if (!super::start(provider)) {
73 return false;
74 }
75 fOwner = (IOPMrootDomain *)provider;
76
77
78 return true;
79 }
80
81 IOReturn
82 RootDomainUserClient::secureSleepSystem( uint32_t *return_code )
83 {
84 return secureSleepSystemOptions(NULL, 0, return_code);
85 }
86
87 IOReturn
88 RootDomainUserClient::secureSleepSystemOptions(
89 const void *inOptions,
90 IOByteCount inOptionsSize,
91 uint32_t *returnCode)
92 {
93 int local_priv = 0;
94 int admin_priv = 0;
95 IOReturn ret = kIOReturnNotPrivileged;
96 OSDictionary *unserializedOptions = NULL;
97 OSString *unserializeErrorString = NULL;
98
99 ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeLocalUser);
100 local_priv = (kIOReturnSuccess == ret);
101
102 ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator);
103 admin_priv = (kIOReturnSuccess == ret);
104
105
106 if (inOptions) {
107 unserializedOptions = OSDynamicCast( OSDictionary,
108 OSUnserializeXML((const char *)inOptions, inOptionsSize, &unserializeErrorString));
109
110 if (!unserializedOptions) {
111 IOLog("IOPMRootDomain SleepSystem unserialization failure: %s\n",
112 unserializeErrorString ? unserializeErrorString->getCStringNoCopy() : "Unknown");
113 }
114 }
115
116 if ((local_priv || admin_priv) && fOwner) {
117 proc_t p;
118 p = (proc_t)get_bsdtask_info(fOwningTask);
119 if (p) {
120 fOwner->setProperty("SleepRequestedByPID", proc_pid(p), 32);
121 }
122
123 if (unserializedOptions) {
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 } else {
138 *returnCode = kIOReturnNotPrivileged;
139 }
140
141 return kIOReturnSuccess;
142 }
143
144 IOReturn
145 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 } else {
163 *return_code = kIOReturnNotPrivileged;
164 }
165 return kIOReturnSuccess;
166 }
167
168 IOReturn
169 RootDomainUserClient::secureSetMaintenanceWakeCalendar(
170 IOPMCalendarStruct *inCalendar,
171 uint32_t *returnCode)
172 {
173 int admin_priv = 0;
174 IOReturn ret = kIOReturnNotPrivileged;
175
176 ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator);
177 admin_priv = (kIOReturnSuccess == ret);
178
179 if (admin_priv && fOwner) {
180 *returnCode = fOwner->setMaintenanceWakeCalendar(inCalendar);
181 } else {
182 *returnCode = kIOReturnNotPrivileged;
183 }
184 return kIOReturnSuccess;
185 }
186
187 IOReturn
188 RootDomainUserClient::secureSetUserAssertionLevels(
189 uint32_t assertionBitfield)
190 {
191 int admin_priv = 0;
192 IOReturn ret = kIOReturnNotPrivileged;
193
194 ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator);
195 admin_priv = (kIOReturnSuccess == ret);
196
197 if (admin_priv && fOwner) {
198 ret = fOwner->setPMAssertionUserLevels(assertionBitfield);
199 } else {
200 ret = kIOReturnNotPrivileged;
201 }
202 return kIOReturnSuccess;
203 }
204
205 IOReturn
206 RootDomainUserClient::secureGetSystemSleepType(
207 uint32_t *outSleepType, uint32_t *sleepTimer)
208 {
209 int admin_priv = 0;
210 IOReturn ret;
211
212 ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator);
213 admin_priv = (kIOReturnSuccess == ret);
214
215 if (admin_priv && fOwner) {
216 ret = fOwner->getSystemSleepType(outSleepType, sleepTimer);
217 } else {
218 ret = kIOReturnNotPrivileged;
219 }
220 return ret;
221 }
222
223 IOReturn
224 RootDomainUserClient::clientClose( void )
225 {
226 terminate();
227
228 return kIOReturnSuccess;
229 }
230
231 void
232 RootDomainUserClient::stop( IOService *provider)
233 {
234 if (fOwningTask) {
235 task_deallocate(fOwningTask);
236 fOwningTask = 0;
237 }
238
239 super::stop(provider);
240 }
241
242 IOReturn
243 RootDomainUserClient::externalMethod(
244 uint32_t selector,
245 IOExternalMethodArguments * arguments,
246 IOExternalMethodDispatch * dispatch __unused,
247 OSObject * target __unused,
248 void * reference __unused )
249 {
250 IOReturn ret = kIOReturnBadArgument;
251
252 switch (selector) {
253 case kPMSetAggressiveness:
254 if ((2 == arguments->scalarInputCount)
255 && (1 == arguments->scalarOutputCount)) {
256 ret = this->secureSetAggressiveness(
257 (unsigned long)arguments->scalarInput[0],
258 (unsigned long)arguments->scalarInput[1],
259 (int *)&arguments->scalarOutput[0]);
260 }
261 break;
262
263 case kPMGetAggressiveness:
264 if ((1 == arguments->scalarInputCount)
265 && (1 == arguments->scalarOutputCount)) {
266 ret = fOwner->getAggressiveness(
267 (unsigned long)arguments->scalarInput[0],
268 (unsigned long *)&arguments->scalarOutput[0]);
269 }
270 break;
271
272 case kPMSleepSystem:
273 if (1 == arguments->scalarOutputCount) {
274 ret = this->secureSleepSystem(
275 (uint32_t *)&arguments->scalarOutput[0]);
276 }
277 break;
278
279 case kPMAllowPowerChange:
280 if (1 == arguments->scalarInputCount) {
281 ret = fOwner->allowPowerChange(
282 arguments->scalarInput[0]);
283 }
284 break;
285
286 case kPMCancelPowerChange:
287 if (1 == arguments->scalarInputCount) {
288 ret = fOwner->cancelPowerChange(
289 arguments->scalarInput[0]);
290 }
291 break;
292
293 case kPMShutdownSystem:
294 // deperecated interface
295 ret = kIOReturnUnsupported;
296 break;
297
298 case kPMRestartSystem:
299 // deperecated interface
300 ret = kIOReturnUnsupported;
301 break;
302
303 case kPMSleepSystemOptions:
304 ret = this->secureSleepSystemOptions(
305 arguments->structureInput,
306 arguments->structureInputSize,
307 (uint32_t *)&arguments->scalarOutput[0]);
308 break;
309 case kPMSetMaintenanceWakeCalendar:
310 if ((arguments->structureInputSize >= sizeof(IOPMCalendarStruct)) &&
311 (arguments->structureOutputSize >= sizeof(uint32_t))) {
312 ret = this->secureSetMaintenanceWakeCalendar(
313 (IOPMCalendarStruct *)arguments->structureInput,
314 (uint32_t *)&arguments->structureOutput);
315 arguments->structureOutputSize = sizeof(uint32_t);
316 }
317 break;
318
319 case kPMSetUserAssertionLevels:
320 ret = this->secureSetUserAssertionLevels(
321 (uint32_t)arguments->scalarInput[0]);
322 break;
323
324 case kPMActivityTickle:
325 if (fOwner->checkSystemCanSustainFullWake()) {
326 fOwner->reportUserInput();
327 fOwner->setProperty(kIOPMRootDomainWakeTypeKey, "UserActivity Assertion");
328 }
329 ret = kIOReturnSuccess;
330 break;
331
332 case kPMSetClamshellSleepState:
333 fOwner->setDisableClamShellSleep(arguments->scalarInput[0] ? true : false);
334 ret = kIOReturnSuccess;
335 break;
336
337 case kPMGetSystemSleepType:
338 if (2 == arguments->scalarOutputCount) {
339 ret = this->secureGetSystemSleepType(
340 (uint32_t *) &arguments->scalarOutput[0],
341 (uint32_t *) &arguments->scalarOutput[1]);
342 }
343 break;
344
345 #if defined(__i386__) || defined(__x86_64__)
346 case kPMSleepWakeWatchdogEnable:
347 ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator);
348 if (ret == kIOReturnSuccess) {
349 fOwner->sleepWakeDebugEnableWdog();
350 }
351 break;
352
353
354 case kPMSleepWakeDebugTrig:
355 ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator);
356 if (ret == kIOReturnSuccess) {
357 fOwner->sleepWakeDebugTrig(false);
358 }
359 break;
360 #endif
361
362 case kPMSetDisplayPowerOn:
363 if (1 == arguments->scalarInputCount) {
364 ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator);
365 if (ret == kIOReturnSuccess) {
366 fOwner->setDisplayPowerOn((uint32_t)arguments->scalarInput[0]);
367 }
368 }
369 break;
370
371 default:
372 // bad selector
373 return kIOReturnBadArgument;
374 }
375
376 return ret;
377 }
378
379 /* getTargetAndMethodForIndex
380 * Not used. We prefer to use externalMethod() for user client invocations.
381 * We maintain getTargetAndExternalMethod since it's an exported symbol,
382 * and only for that reason.
383 */
384 IOExternalMethod *
385 RootDomainUserClient::getTargetAndMethodForIndex(
386 IOService ** targetP, UInt32 index )
387 {
388 // DO NOT EDIT
389 return super::getTargetAndMethodForIndex(targetP, index);
390 }
391
392 /* setPreventative
393 * Does nothing. Exists only for exported symbol compatibility.
394 */
395 void
396 RootDomainUserClient::setPreventative(UInt32 on_off, UInt32 types_of_sleep)
397 {
398 return;
399 } // DO NOT EDIT