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
;
97 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeLocalUser
);
98 local_priv
= (kIOReturnSuccess
== ret
);
100 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
101 admin_priv
= (kIOReturnSuccess
== ret
);
103 if ((local_priv
|| admin_priv
) && fOwner
) {
104 OSString
*unserializeErrorString
= NULL
;
105 OSObject
*unserializedObject
= NULL
;
106 OSDictionary
*sleepOptionsDict
= NULL
; // do not release
109 p
= (proc_t
)get_bsdtask_info(fOwningTask
);
111 fOwner
->setProperty("SleepRequestedByPID", proc_pid(p
), 32);
115 unserializedObject
= OSUnserializeXML((const char *)inOptions
, inOptionsSize
, &unserializeErrorString
);
116 sleepOptionsDict
= OSDynamicCast( OSDictionary
, unserializedObject
);
117 if (!sleepOptionsDict
) {
118 IOLog("IOPMRootDomain SleepSystem unserialization failure: %s\n",
119 unserializeErrorString
? unserializeErrorString
->getCStringNoCopy() : "Unknown");
123 if (sleepOptionsDict
) {
124 // Publish Sleep Options in registry under root_domain
125 fOwner
->setProperty( kRootDomainSleepOptionsKey
, sleepOptionsDict
);
128 // Clear any pre-existing options
129 fOwner
->removeProperty( kRootDomainSleepOptionsKey
);
132 *returnCode
= fOwner
->sleepSystemOptions( sleepOptionsDict
);
133 OSSafeReleaseNULL(unserializedObject
);
134 OSSafeReleaseNULL(unserializeErrorString
);
136 *returnCode
= kIOReturnNotPrivileged
;
139 return kIOReturnSuccess
;
143 RootDomainUserClient::secureSetAggressiveness(
145 unsigned long newLevel
,
150 IOReturn ret
= kIOReturnNotPrivileged
;
152 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeLocalUser
);
153 local_priv
= (kIOReturnSuccess
== ret
);
155 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
156 admin_priv
= (kIOReturnSuccess
== ret
);
158 if ((local_priv
|| admin_priv
) && fOwner
) {
159 *return_code
= fOwner
->setAggressiveness(type
, newLevel
);
161 *return_code
= kIOReturnNotPrivileged
;
163 return kIOReturnSuccess
;
167 RootDomainUserClient::secureSetMaintenanceWakeCalendar(
168 IOPMCalendarStruct
*inCalendar
,
169 uint32_t *returnCode
)
172 IOReturn ret
= kIOReturnNotPrivileged
;
174 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
175 admin_priv
= (kIOReturnSuccess
== ret
);
177 if (admin_priv
&& fOwner
) {
178 *returnCode
= fOwner
->setMaintenanceWakeCalendar(inCalendar
);
180 *returnCode
= kIOReturnNotPrivileged
;
182 return kIOReturnSuccess
;
186 RootDomainUserClient::secureSetUserAssertionLevels(
187 uint32_t assertionBitfield
)
190 IOReturn ret
= kIOReturnNotPrivileged
;
192 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
193 admin_priv
= (kIOReturnSuccess
== ret
);
195 if (admin_priv
&& fOwner
) {
196 ret
= fOwner
->setPMAssertionUserLevels(assertionBitfield
);
198 ret
= kIOReturnNotPrivileged
;
200 return kIOReturnSuccess
;
204 RootDomainUserClient::secureGetSystemSleepType(
205 uint32_t *outSleepType
, uint32_t *sleepTimer
)
210 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
211 admin_priv
= (kIOReturnSuccess
== ret
);
213 if (admin_priv
&& fOwner
) {
214 ret
= fOwner
->getSystemSleepType(outSleepType
, sleepTimer
);
216 ret
= kIOReturnNotPrivileged
;
222 RootDomainUserClient::clientClose( void )
226 return kIOReturnSuccess
;
230 RootDomainUserClient::stop( IOService
*provider
)
233 task_deallocate(fOwningTask
);
237 super::stop(provider
);
241 RootDomainUserClient::externalMethod(
243 IOExternalMethodArguments
* arguments
,
244 IOExternalMethodDispatch
* dispatch __unused
,
245 OSObject
* target __unused
,
246 void * reference __unused
)
248 IOReturn ret
= kIOReturnBadArgument
;
251 case kPMSetAggressiveness
:
252 if ((2 == arguments
->scalarInputCount
)
253 && (1 == arguments
->scalarOutputCount
)) {
254 ret
= this->secureSetAggressiveness(
255 (unsigned long)arguments
->scalarInput
[0],
256 (unsigned long)arguments
->scalarInput
[1],
257 (int *)&arguments
->scalarOutput
[0]);
261 case kPMGetAggressiveness
:
262 if ((1 == arguments
->scalarInputCount
)
263 && (1 == arguments
->scalarOutputCount
)) {
264 ret
= fOwner
->getAggressiveness(
265 (unsigned long)arguments
->scalarInput
[0],
266 (unsigned long *)&arguments
->scalarOutput
[0]);
271 if (1 == arguments
->scalarOutputCount
) {
272 ret
= this->secureSleepSystem(
273 (uint32_t *)&arguments
->scalarOutput
[0]);
277 case kPMAllowPowerChange
:
278 if (1 == arguments
->scalarInputCount
) {
279 ret
= fOwner
->allowPowerChange(
280 arguments
->scalarInput
[0]);
284 case kPMCancelPowerChange
:
285 if (1 == arguments
->scalarInputCount
) {
286 ret
= fOwner
->cancelPowerChange(
287 arguments
->scalarInput
[0]);
291 case kPMShutdownSystem
:
292 // deperecated interface
293 ret
= kIOReturnUnsupported
;
296 case kPMRestartSystem
:
297 // deperecated interface
298 ret
= kIOReturnUnsupported
;
301 case kPMSleepSystemOptions
:
302 ret
= this->secureSleepSystemOptions(
303 arguments
->structureInput
,
304 arguments
->structureInputSize
,
305 (uint32_t *)&arguments
->scalarOutput
[0]);
307 case kPMSetMaintenanceWakeCalendar
:
308 if ((arguments
->structureInputSize
>= sizeof(IOPMCalendarStruct
)) &&
309 (arguments
->structureOutputSize
>= sizeof(uint32_t))) {
310 ret
= this->secureSetMaintenanceWakeCalendar(
311 (IOPMCalendarStruct
*)arguments
->structureInput
,
312 (uint32_t *)&arguments
->structureOutput
);
313 arguments
->structureOutputSize
= sizeof(uint32_t);
317 case kPMSetUserAssertionLevels
:
318 ret
= this->secureSetUserAssertionLevels(
319 (uint32_t)arguments
->scalarInput
[0]);
322 case kPMActivityTickle
:
323 if (fOwner
->checkSystemCanSustainFullWake()) {
324 fOwner
->reportUserInput();
325 fOwner
->setProperty(kIOPMRootDomainWakeTypeKey
, "UserActivity Assertion");
327 ret
= kIOReturnSuccess
;
330 case kPMSetClamshellSleepState
:
331 fOwner
->setDisableClamShellSleep(arguments
->scalarInput
[0] ? true : false);
332 ret
= kIOReturnSuccess
;
335 case kPMGetSystemSleepType
:
336 if (2 == arguments
->scalarOutputCount
) {
337 ret
= this->secureGetSystemSleepType(
338 (uint32_t *) &arguments
->scalarOutput
[0],
339 (uint32_t *) &arguments
->scalarOutput
[1]);
343 #if defined(__i386__) || defined(__x86_64__)
344 case kPMSleepWakeWatchdogEnable
:
345 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
346 if (ret
== kIOReturnSuccess
) {
347 fOwner
->sleepWakeDebugEnableWdog();
352 case kPMSleepWakeDebugTrig
:
353 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
354 if (ret
== kIOReturnSuccess
) {
355 fOwner
->sleepWakeDebugTrig(false);
360 case kPMSetDisplayPowerOn
:
361 if (1 == arguments
->scalarInputCount
) {
362 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
363 if (ret
== kIOReturnSuccess
) {
364 fOwner
->setDisplayPowerOn((uint32_t)arguments
->scalarInput
[0]);
371 return kIOReturnBadArgument
;
377 /* getTargetAndMethodForIndex
378 * Not used. We prefer to use externalMethod() for user client invocations.
379 * We maintain getTargetAndExternalMethod since it's an exported symbol,
380 * and only for that reason.
383 RootDomainUserClient::getTargetAndMethodForIndex(
384 IOService
** targetP
, UInt32 index
)
387 return super::getTargetAndMethodForIndex(targetP
, index
);
391 * Does nothing. Exists only for exported symbol compatibility.
394 RootDomainUserClient::setPreventative(UInt32 on_off
, UInt32 types_of_sleep
)