2 * Copyright (c) 1998-2000 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>
41 #define super IOUserClient
43 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
45 OSDefineMetaClassAndStructors(RootDomainUserClient
, IOUserClient
)
47 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
49 bool RootDomainUserClient::initWithTask(task_t owningTask
, void *security_id
,
50 UInt32 type
, OSDictionary
* properties
)
53 properties
->setObject(kIOUserClientCrossEndianCompatibleKey
, kOSBooleanTrue
);
55 if (!super::initWithTask(owningTask
, security_id
, type
, properties
))
58 fOwningTask
= owningTask
;
59 task_reference (fOwningTask
);
64 bool RootDomainUserClient::start( IOService
* provider
)
66 assert(OSDynamicCast(IOPMrootDomain
, provider
));
67 if(!super::start(provider
))
69 fOwner
= (IOPMrootDomain
*)provider
;
75 IOReturn
RootDomainUserClient::secureSleepSystem( uint32_t *return_code
)
77 return secureSleepSystemOptions(NULL
, 0, return_code
);
80 IOReturn
RootDomainUserClient::secureSleepSystemOptions(
81 const void *inOptions
,
82 IOByteCount inOptionsSize __unused
,
88 IOReturn ret
= kIOReturnNotPrivileged
;
89 OSDictionary
*unserializedOptions
= NULL
;
90 OSString
*unserializeErrorString
= NULL
;
92 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeLocalUser
);
93 local_priv
= (kIOReturnSuccess
== ret
);
95 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
96 admin_priv
= (kIOReturnSuccess
== ret
);
101 unserializedOptions
= OSDynamicCast( OSDictionary
,
102 OSUnserializeXML((const char *)inOptions
, &unserializeErrorString
));
104 if (!unserializedOptions
) {
105 IOLog("IOPMRootDomain SleepSystem unserialization failure: %s\n",
106 unserializeErrorString
? unserializeErrorString
->getCStringNoCopy() : "Unknown");
110 if ( (local_priv
|| admin_priv
)
113 if (unserializedOptions
)
115 // Publish Sleep Options in registry under root_domain
116 fOwner
->setProperty( kRootDomainSleepOptionsKey
, unserializedOptions
);
118 *returnCode
= fOwner
->sleepSystemOptions( unserializedOptions
);
120 unserializedOptions
->release();
123 // Clear any pre-existing options
124 fOwner
->removeProperty( kRootDomainSleepOptionsKey
);
126 *returnCode
= fOwner
->sleepSystemOptions( NULL
);
130 *returnCode
= kIOReturnNotPrivileged
;
133 return kIOReturnSuccess
;
136 IOReturn
RootDomainUserClient::secureSetAggressiveness(
138 unsigned long newLevel
,
143 IOReturn ret
= kIOReturnNotPrivileged
;
145 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeLocalUser
);
146 local_priv
= (kIOReturnSuccess
== ret
);
148 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
149 admin_priv
= (kIOReturnSuccess
== ret
);
151 if((local_priv
|| admin_priv
) && fOwner
) {
152 *return_code
= fOwner
->setAggressiveness(type
, newLevel
);
154 *return_code
= kIOReturnNotPrivileged
;
156 return kIOReturnSuccess
;
159 IOReturn
RootDomainUserClient::secureSetMaintenanceWakeCalendar(
160 IOPMCalendarStruct
*inCalendar
,
161 uint32_t *returnCode
)
164 IOReturn ret
= kIOReturnNotPrivileged
;
166 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
167 admin_priv
= (kIOReturnSuccess
== ret
);
169 if (admin_priv
&& fOwner
) {
170 *returnCode
= fOwner
->setMaintenanceWakeCalendar(inCalendar
);
172 *returnCode
= kIOReturnNotPrivileged
;
174 return kIOReturnSuccess
;
177 IOReturn
RootDomainUserClient::secureSetUserAssertionLevels(
178 uint32_t assertionBitfield
)
181 IOReturn ret
= kIOReturnNotPrivileged
;
183 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
184 admin_priv
= (kIOReturnSuccess
== ret
);
186 if (admin_priv
&& fOwner
) {
187 ret
= fOwner
->setPMAssertionUserLevels(assertionBitfield
);
189 ret
= kIOReturnNotPrivileged
;
191 return kIOReturnSuccess
;
194 IOReturn
RootDomainUserClient::secureGetSystemSleepType(
195 uint32_t *outSleepType
)
200 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
201 admin_priv
= (kIOReturnSuccess
== ret
);
203 if (admin_priv
&& fOwner
) {
204 ret
= fOwner
->getSystemSleepType(outSleepType
);
206 ret
= kIOReturnNotPrivileged
;
211 IOReturn
RootDomainUserClient::clientClose( void )
216 task_deallocate(fOwningTask
);
220 return kIOReturnSuccess
;
223 IOReturn
RootDomainUserClient::clientMemoryForType(
225 IOOptionBits
*options
,
226 IOMemoryDescriptor
** memory
)
229 return kIOReturnNotReady
;
231 if (kPMRootDomainMapTraceBuffer
== type
)
233 *memory
= fOwner
->getPMTraceMemoryDescriptor();
237 return kIOReturnSuccess
;
239 return kIOReturnNotFound
;
243 return kIOReturnUnsupported
;
246 IOReturn
RootDomainUserClient::externalMethod(
248 IOExternalMethodArguments
* arguments
,
249 IOExternalMethodDispatch
* dispatch __unused
,
250 OSObject
* target __unused
,
251 void * reference __unused
)
253 IOReturn ret
= kIOReturnBadArgument
;
257 case kPMSetAggressiveness
:
258 if ((2 == arguments
->scalarInputCount
)
259 && (1 == arguments
->scalarOutputCount
))
261 ret
= this->secureSetAggressiveness(
262 (unsigned long)arguments
->scalarInput
[0],
263 (unsigned long)arguments
->scalarInput
[1],
264 (int *)&arguments
->scalarOutput
[0]);
268 case kPMGetAggressiveness
:
269 if ((1 == arguments
->scalarInputCount
)
270 && (1 == arguments
->scalarOutputCount
))
272 ret
= fOwner
->getAggressiveness(
273 (unsigned long)arguments
->scalarInput
[0],
274 (unsigned long *)&arguments
->scalarOutput
[0]);
279 if (1 == arguments
->scalarOutputCount
)
281 ret
= this->secureSleepSystem(
282 (uint32_t *)&arguments
->scalarOutput
[0]);
286 case kPMAllowPowerChange
:
287 if (1 == arguments
->scalarInputCount
)
289 ret
= fOwner
->allowPowerChange(
290 arguments
->scalarInput
[0]);
294 case kPMCancelPowerChange
:
295 if (1 == arguments
->scalarInputCount
)
297 ret
= fOwner
->cancelPowerChange(
298 arguments
->scalarInput
[0]);
302 case kPMShutdownSystem
:
303 // deperecated interface
304 ret
= kIOReturnUnsupported
;
307 case kPMRestartSystem
:
308 // deperecated interface
309 ret
= kIOReturnUnsupported
;
312 case kPMSleepSystemOptions
:
313 ret
= this->secureSleepSystemOptions(
314 arguments
->structureInput
,
315 arguments
->structureInputSize
,
316 (uint32_t *)&arguments
->scalarOutput
[0]);
318 case kPMSetMaintenanceWakeCalendar
:
319 ret
= this->secureSetMaintenanceWakeCalendar(
320 (IOPMCalendarStruct
*)arguments
->structureInput
,
321 (uint32_t *)&arguments
->structureOutput
);
322 arguments
->structureOutputSize
= sizeof(uint32_t);
325 case kPMSetUserAssertionLevels
:
326 ret
= this->secureSetUserAssertionLevels(
327 (uint32_t)arguments
->scalarInput
[0]);
330 case kPMActivityTickle
:
331 if ( fOwner
->checkSystemCanSustainFullWake() )
333 fOwner
->reportUserInput( );
334 fOwner
->setProperty(kIOPMRootDomainWakeTypeKey
, "UserActivity Assertion");
336 ret
= kIOReturnSuccess
;
339 case kPMSetClamshellSleepState
:
340 fOwner
->setDisableClamShellSleep(arguments
->scalarInput
[0] ? true : false);
341 ret
= kIOReturnSuccess
;
344 case kPMGetSystemSleepType
:
345 if (1 == arguments
->scalarOutputCount
)
347 ret
= this->secureGetSystemSleepType(
348 (uint32_t *) &arguments
->scalarOutput
[0]);
352 case kPMMethodCopySystemTimeline:
353 // intentional fallthrough
354 case kPMMethodCopyDetailedTimeline:
356 if (!arguments->structureOutputDescriptor)
358 // TODO: Force IOKit.framework to always send this data out
359 // of line; so I don't have to create a MemoryDescriptor here.
360 mem_size = arguments->structureOutputSize;
361 mem = IOMemoryDescriptor::withAddressRange(
362 (mach_vm_address_t)arguments->structureOutput,
363 (mach_vm_size_t)mem_size,
364 kIODirectionIn, current_task());
366 mem_size = arguments->structureOutputDescriptorSize;
367 if (( mem = arguments->structureOutputDescriptor ))
373 mem->prepare(kIODirectionNone);
375 if (kPMMethodCopySystemTimeline == selector) {
376 arguments->scalarOutput[0] = fOwner->copySystemTimeline(
380 if (kPMMethodCopyDetailedTimeline == selector) {
381 arguments->scalarOutput[0] = fOwner->copyDetailedTimeline(
385 if (arguments->structureOutputDescriptor) {
386 arguments->structureOutputDescriptorSize = mem_size;
388 arguments->structureOutputSize = mem_size;
393 ret = kIOReturnSuccess;
395 ret = kIOReturnCannotWire;
402 return kIOReturnBadArgument
;
408 /* getTargetAndMethodForIndex
409 * Not used. We prefer to use externalMethod() for user client invocations.
410 * We maintain getTargetAndExternalMethod since it's an exported symbol,
411 * and only for that reason.
413 IOExternalMethod
* RootDomainUserClient::getTargetAndMethodForIndex(
414 IOService
** targetP
, UInt32 index
)
417 return super::getTargetAndMethodForIndex(targetP
, index
);
421 * Does nothing. Exists only for exported symbol compatibility.
424 RootDomainUserClient::setPreventative(UInt32 on_off
, UInt32 types_of_sleep
)
425 { return; } // DO NOT EDIT