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 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
50 bool RootDomainUserClient::initWithTask(task_t owningTask
, void *security_id
,
51 UInt32 type
, OSDictionary
* properties
)
54 properties
->setObject(kIOUserClientCrossEndianCompatibleKey
, kOSBooleanTrue
);
56 if (!super::initWithTask(owningTask
, security_id
, type
, properties
))
59 fOwningTask
= owningTask
;
60 task_reference (fOwningTask
);
65 bool RootDomainUserClient::start( IOService
* provider
)
67 assert(OSDynamicCast(IOPMrootDomain
, provider
));
68 if(!super::start(provider
))
70 fOwner
= (IOPMrootDomain
*)provider
;
76 IOReturn
RootDomainUserClient::secureSleepSystem( uint32_t *return_code
)
78 return secureSleepSystemOptions(NULL
, 0, return_code
);
81 IOReturn
RootDomainUserClient::secureSleepSystemOptions(
82 const void *inOptions
,
83 IOByteCount inOptionsSize
,
89 IOReturn ret
= kIOReturnNotPrivileged
;
90 OSDictionary
*unserializedOptions
= NULL
;
91 OSString
*unserializeErrorString
= NULL
;
93 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeLocalUser
);
94 local_priv
= (kIOReturnSuccess
== ret
);
96 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
97 admin_priv
= (kIOReturnSuccess
== ret
);
102 unserializedOptions
= OSDynamicCast( OSDictionary
,
103 OSUnserializeXML((const char *)inOptions
, inOptionsSize
, &unserializeErrorString
));
105 if (!unserializedOptions
) {
106 IOLog("IOPMRootDomain SleepSystem unserialization failure: %s\n",
107 unserializeErrorString
? unserializeErrorString
->getCStringNoCopy() : "Unknown");
111 if ( (local_priv
|| admin_priv
) && fOwner
)
114 p
= (proc_t
)get_bsdtask_info(fOwningTask
);
116 fOwner
->setProperty("SleepRequestedByPID", proc_pid(p
), 32);
119 if (unserializedOptions
)
121 // Publish Sleep Options in registry under root_domain
122 fOwner
->setProperty( kRootDomainSleepOptionsKey
, unserializedOptions
);
124 *returnCode
= fOwner
->sleepSystemOptions( unserializedOptions
);
126 unserializedOptions
->release();
129 // Clear any pre-existing options
130 fOwner
->removeProperty( kRootDomainSleepOptionsKey
);
132 *returnCode
= fOwner
->sleepSystemOptions( NULL
);
136 *returnCode
= kIOReturnNotPrivileged
;
139 return kIOReturnSuccess
;
142 IOReturn
RootDomainUserClient::secureSetAggressiveness(
144 unsigned long newLevel
,
149 IOReturn ret
= kIOReturnNotPrivileged
;
151 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeLocalUser
);
152 local_priv
= (kIOReturnSuccess
== ret
);
154 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
155 admin_priv
= (kIOReturnSuccess
== ret
);
157 if((local_priv
|| admin_priv
) && fOwner
) {
158 *return_code
= fOwner
->setAggressiveness(type
, newLevel
);
160 *return_code
= kIOReturnNotPrivileged
;
162 return kIOReturnSuccess
;
165 IOReturn
RootDomainUserClient::secureSetMaintenanceWakeCalendar(
166 IOPMCalendarStruct
*inCalendar
,
167 uint32_t *returnCode
)
170 IOReturn ret
= kIOReturnNotPrivileged
;
172 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
173 admin_priv
= (kIOReturnSuccess
== ret
);
175 if (admin_priv
&& fOwner
) {
176 *returnCode
= fOwner
->setMaintenanceWakeCalendar(inCalendar
);
178 *returnCode
= kIOReturnNotPrivileged
;
180 return kIOReturnSuccess
;
183 IOReturn
RootDomainUserClient::secureSetUserAssertionLevels(
184 uint32_t assertionBitfield
)
187 IOReturn ret
= kIOReturnNotPrivileged
;
189 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
190 admin_priv
= (kIOReturnSuccess
== ret
);
192 if (admin_priv
&& fOwner
) {
193 ret
= fOwner
->setPMAssertionUserLevels(assertionBitfield
);
195 ret
= kIOReturnNotPrivileged
;
197 return kIOReturnSuccess
;
200 IOReturn
RootDomainUserClient::secureGetSystemSleepType(
201 uint32_t *outSleepType
, uint32_t *sleepTimer
)
206 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
207 admin_priv
= (kIOReturnSuccess
== ret
);
209 if (admin_priv
&& fOwner
) {
210 ret
= fOwner
->getSystemSleepType(outSleepType
, sleepTimer
);
212 ret
= kIOReturnNotPrivileged
;
217 IOReturn
RootDomainUserClient::clientClose( void )
221 return kIOReturnSuccess
;
224 void RootDomainUserClient::stop( IOService
*provider
)
227 task_deallocate(fOwningTask
);
231 super::stop(provider
);
234 IOReturn
RootDomainUserClient::externalMethod(
236 IOExternalMethodArguments
* arguments
,
237 IOExternalMethodDispatch
* dispatch __unused
,
238 OSObject
* target __unused
,
239 void * reference __unused
)
241 IOReturn ret
= kIOReturnBadArgument
;
245 case kPMSetAggressiveness
:
246 if ((2 == arguments
->scalarInputCount
)
247 && (1 == arguments
->scalarOutputCount
))
249 ret
= this->secureSetAggressiveness(
250 (unsigned long)arguments
->scalarInput
[0],
251 (unsigned long)arguments
->scalarInput
[1],
252 (int *)&arguments
->scalarOutput
[0]);
256 case kPMGetAggressiveness
:
257 if ((1 == arguments
->scalarInputCount
)
258 && (1 == arguments
->scalarOutputCount
))
260 ret
= fOwner
->getAggressiveness(
261 (unsigned long)arguments
->scalarInput
[0],
262 (unsigned long *)&arguments
->scalarOutput
[0]);
267 if (1 == arguments
->scalarOutputCount
)
269 ret
= this->secureSleepSystem(
270 (uint32_t *)&arguments
->scalarOutput
[0]);
274 case kPMAllowPowerChange
:
275 if (1 == arguments
->scalarInputCount
)
277 ret
= fOwner
->allowPowerChange(
278 arguments
->scalarInput
[0]);
282 case kPMCancelPowerChange
:
283 if (1 == arguments
->scalarInputCount
)
285 ret
= fOwner
->cancelPowerChange(
286 arguments
->scalarInput
[0]);
290 case kPMShutdownSystem
:
291 // deperecated interface
292 ret
= kIOReturnUnsupported
;
295 case kPMRestartSystem
:
296 // deperecated interface
297 ret
= kIOReturnUnsupported
;
300 case kPMSleepSystemOptions
:
301 ret
= this->secureSleepSystemOptions(
302 arguments
->structureInput
,
303 arguments
->structureInputSize
,
304 (uint32_t *)&arguments
->scalarOutput
[0]);
306 case kPMSetMaintenanceWakeCalendar
:
307 if ((arguments
->structureInputSize
>= sizeof(IOPMCalendarStruct
)) &&
308 (arguments
->structureOutputSize
>= sizeof(uint32_t) )) {
309 ret
= this->secureSetMaintenanceWakeCalendar(
310 (IOPMCalendarStruct
*)arguments
->structureInput
,
311 (uint32_t *)&arguments
->structureOutput
);
312 arguments
->structureOutputSize
= sizeof(uint32_t);
316 case kPMSetUserAssertionLevels
:
317 ret
= this->secureSetUserAssertionLevels(
318 (uint32_t)arguments
->scalarInput
[0]);
321 case kPMActivityTickle
:
322 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
)
338 ret
= this->secureGetSystemSleepType(
339 (uint32_t *) &arguments
->scalarOutput
[0],
340 (uint32_t *) &arguments
->scalarOutput
[1]);
344 #if defined(__i386__) || defined(__x86_64__)
345 case kPMSleepWakeWatchdogEnable
:
346 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
347 if (ret
== kIOReturnSuccess
)
348 fOwner
->sleepWakeDebugEnableWdog();
352 case kPMSleepWakeDebugTrig
:
353 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
354 if (ret
== kIOReturnSuccess
)
355 fOwner
->sleepWakeDebugTrig(false);
359 case kPMSetDisplayPowerOn
:
360 if (1 == arguments
->scalarInputCount
)
362 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
363 if (ret
== kIOReturnSuccess
)
364 fOwner
->setDisplayPowerOn((uint32_t)arguments
->scalarInput
[0]);
370 return kIOReturnBadArgument
;
376 /* getTargetAndMethodForIndex
377 * Not used. We prefer to use externalMethod() for user client invocations.
378 * We maintain getTargetAndExternalMethod since it's an exported symbol,
379 * and only for that reason.
381 IOExternalMethod
* RootDomainUserClient::getTargetAndMethodForIndex(
382 IOService
** targetP
, UInt32 index
)
385 return super::getTargetAndMethodForIndex(targetP
, index
);
389 * Does nothing. Exists only for exported symbol compatibility.
392 RootDomainUserClient::setPreventative(UInt32 on_off
, UInt32 types_of_sleep
)
393 { return; } // DO NOT EDIT