2 * Copyright (c) 1998-2012 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
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>
42 #define super IOUserClient
44 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
46 OSDefineMetaClassAndStructors(RootDomainUserClient
, IOUserClient
)
48 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
51 RootDomainUserClient::initWithTask(task_t owningTask
, void *security_id
,
52 UInt32 type
, OSDictionary
* properties
)
55 properties
->setObject(kIOUserClientCrossEndianCompatibleKey
, kOSBooleanTrue
);
58 if (!super::initWithTask(owningTask
, security_id
, type
, properties
)) {
62 fOwningTask
= owningTask
;
63 task_reference(fOwningTask
);
69 RootDomainUserClient::start( IOService
* provider
)
71 assert(OSDynamicCast(IOPMrootDomain
, provider
));
72 if (!super::start(provider
)) {
75 fOwner
= (IOPMrootDomain
*)provider
;
82 RootDomainUserClient::secureSleepSystem( uint32_t *return_code
)
84 return secureSleepSystemOptions(NULL
, 0, return_code
);
88 RootDomainUserClient::secureSleepSystemOptions(
89 const void *inOptions
,
90 IOByteCount inOptionsSize
,
95 IOReturn ret
= kIOReturnNotPrivileged
;
96 OSDictionary
*unserializedOptions
= NULL
;
97 OSString
*unserializeErrorString
= NULL
;
99 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeLocalUser
);
100 local_priv
= (kIOReturnSuccess
== ret
);
102 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
103 admin_priv
= (kIOReturnSuccess
== ret
);
107 unserializedOptions
= OSDynamicCast( OSDictionary
,
108 OSUnserializeXML((const char *)inOptions
, inOptionsSize
, &unserializeErrorString
));
110 if (!unserializedOptions
) {
111 IOLog("IOPMRootDomain SleepSystem unserialization failure: %s\n",
112 unserializeErrorString
? unserializeErrorString
->getCStringNoCopy() : "Unknown");
116 if ((local_priv
|| admin_priv
) && fOwner
) {
118 p
= (proc_t
)get_bsdtask_info(fOwningTask
);
120 fOwner
->setProperty("SleepRequestedByPID", proc_pid(p
), 32);
123 if (unserializedOptions
) {
124 // Publish Sleep Options in registry under root_domain
125 fOwner
->setProperty( kRootDomainSleepOptionsKey
, unserializedOptions
);
127 *returnCode
= fOwner
->sleepSystemOptions( unserializedOptions
);
129 unserializedOptions
->release();
132 // Clear any pre-existing options
133 fOwner
->removeProperty( kRootDomainSleepOptionsKey
);
135 *returnCode
= fOwner
->sleepSystemOptions( NULL
);
138 *returnCode
= kIOReturnNotPrivileged
;
141 return kIOReturnSuccess
;
145 RootDomainUserClient::secureSetAggressiveness(
147 unsigned long newLevel
,
152 IOReturn ret
= kIOReturnNotPrivileged
;
154 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeLocalUser
);
155 local_priv
= (kIOReturnSuccess
== ret
);
157 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
158 admin_priv
= (kIOReturnSuccess
== ret
);
160 if ((local_priv
|| admin_priv
) && fOwner
) {
161 *return_code
= fOwner
->setAggressiveness(type
, newLevel
);
163 *return_code
= kIOReturnNotPrivileged
;
165 return kIOReturnSuccess
;
169 RootDomainUserClient::secureSetMaintenanceWakeCalendar(
170 IOPMCalendarStruct
*inCalendar
,
171 uint32_t *returnCode
)
174 IOReturn ret
= kIOReturnNotPrivileged
;
176 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
177 admin_priv
= (kIOReturnSuccess
== ret
);
179 if (admin_priv
&& fOwner
) {
180 *returnCode
= fOwner
->setMaintenanceWakeCalendar(inCalendar
);
182 *returnCode
= kIOReturnNotPrivileged
;
184 return kIOReturnSuccess
;
188 RootDomainUserClient::secureSetUserAssertionLevels(
189 uint32_t assertionBitfield
)
192 IOReturn ret
= kIOReturnNotPrivileged
;
194 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
195 admin_priv
= (kIOReturnSuccess
== ret
);
197 if (admin_priv
&& fOwner
) {
198 ret
= fOwner
->setPMAssertionUserLevels(assertionBitfield
);
200 ret
= kIOReturnNotPrivileged
;
202 return kIOReturnSuccess
;
206 RootDomainUserClient::secureGetSystemSleepType(
207 uint32_t *outSleepType
, uint32_t *sleepTimer
)
212 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
213 admin_priv
= (kIOReturnSuccess
== ret
);
215 if (admin_priv
&& fOwner
) {
216 ret
= fOwner
->getSystemSleepType(outSleepType
, sleepTimer
);
218 ret
= kIOReturnNotPrivileged
;
224 RootDomainUserClient::clientClose( void )
228 return kIOReturnSuccess
;
232 RootDomainUserClient::stop( IOService
*provider
)
235 task_deallocate(fOwningTask
);
239 super::stop(provider
);
243 RootDomainUserClient::externalMethod(
245 IOExternalMethodArguments
* arguments
,
246 IOExternalMethodDispatch
* dispatch __unused
,
247 OSObject
* target __unused
,
248 void * reference __unused
)
250 IOReturn ret
= kIOReturnBadArgument
;
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]);
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]);
273 if (1 == arguments
->scalarOutputCount
) {
274 ret
= this->secureSleepSystem(
275 (uint32_t *)&arguments
->scalarOutput
[0]);
279 case kPMAllowPowerChange
:
280 if (1 == arguments
->scalarInputCount
) {
281 ret
= fOwner
->allowPowerChange(
282 arguments
->scalarInput
[0]);
286 case kPMCancelPowerChange
:
287 if (1 == arguments
->scalarInputCount
) {
288 ret
= fOwner
->cancelPowerChange(
289 arguments
->scalarInput
[0]);
293 case kPMShutdownSystem
:
294 // deperecated interface
295 ret
= kIOReturnUnsupported
;
298 case kPMRestartSystem
:
299 // deperecated interface
300 ret
= kIOReturnUnsupported
;
303 case kPMSleepSystemOptions
:
304 ret
= this->secureSleepSystemOptions(
305 arguments
->structureInput
,
306 arguments
->structureInputSize
,
307 (uint32_t *)&arguments
->scalarOutput
[0]);
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);
319 case kPMSetUserAssertionLevels
:
320 ret
= this->secureSetUserAssertionLevels(
321 (uint32_t)arguments
->scalarInput
[0]);
324 case kPMActivityTickle
:
325 if (fOwner
->checkSystemCanSustainFullWake()) {
326 fOwner
->reportUserInput();
327 fOwner
->setProperty(kIOPMRootDomainWakeTypeKey
, "UserActivity Assertion");
329 ret
= kIOReturnSuccess
;
332 case kPMSetClamshellSleepState
:
333 fOwner
->setDisableClamShellSleep(arguments
->scalarInput
[0] ? true : false);
334 ret
= kIOReturnSuccess
;
337 case kPMGetSystemSleepType
:
338 if (2 == arguments
->scalarOutputCount
) {
339 ret
= this->secureGetSystemSleepType(
340 (uint32_t *) &arguments
->scalarOutput
[0],
341 (uint32_t *) &arguments
->scalarOutput
[1]);
345 #if defined(__i386__) || defined(__x86_64__)
346 case kPMSleepWakeWatchdogEnable
:
347 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
348 if (ret
== kIOReturnSuccess
) {
349 fOwner
->sleepWakeDebugEnableWdog();
354 case kPMSleepWakeDebugTrig
:
355 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
356 if (ret
== kIOReturnSuccess
) {
357 fOwner
->sleepWakeDebugTrig(false);
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]);
373 return kIOReturnBadArgument
;
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.
385 RootDomainUserClient::getTargetAndMethodForIndex(
386 IOService
** targetP
, UInt32 index
)
389 return super::getTargetAndMethodForIndex(targetP
, index
);
393 * Does nothing. Exists only for exported symbol compatibility.
396 RootDomainUserClient::setPreventative(UInt32 on_off
, UInt32 types_of_sleep
)