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>
40 #define super IOUserClient
42 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
44 OSDefineMetaClassAndStructors(RootDomainUserClient
, IOUserClient
)
46 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
48 bool RootDomainUserClient::initWithTask(task_t owningTask
, void *security_id
,
49 UInt32 type
, OSDictionary
* properties
)
52 properties
->setObject(kIOUserClientCrossEndianCompatibleKey
, kOSBooleanTrue
);
54 if (!super::initWithTask(owningTask
, security_id
, type
, properties
))
57 fOwningTask
= owningTask
;
58 task_reference (fOwningTask
);
63 bool RootDomainUserClient::start( IOService
* provider
)
65 assert(OSDynamicCast(IOPMrootDomain
, provider
));
66 if(!super::start(provider
))
68 fOwner
= (IOPMrootDomain
*)provider
;
74 IOReturn
RootDomainUserClient::secureSleepSystem( uint32_t *return_code
)
76 return secureSleepSystemOptions(NULL
, 0, return_code
);
79 IOReturn
RootDomainUserClient::secureSleepSystemOptions(
80 const void *inOptions
,
81 IOByteCount inOptionsSize __unused
,
87 IOReturn ret
= kIOReturnNotPrivileged
;
88 OSDictionary
*unserializedOptions
= NULL
;
89 OSString
*unserializeErrorString
= NULL
;
91 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeLocalUser
);
92 local_priv
= (kIOReturnSuccess
== ret
);
94 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
95 admin_priv
= (kIOReturnSuccess
== ret
);
100 unserializedOptions
= OSDynamicCast( OSDictionary
,
101 OSUnserializeXML((const char *)inOptions
, &unserializeErrorString
));
103 if (!unserializedOptions
) {
104 IOLog("IOPMRootDomain SleepSystem unserialization failure: %s\n",
105 unserializeErrorString
? unserializeErrorString
->getCStringNoCopy() : "Unknown");
109 if ( (local_priv
|| admin_priv
)
112 if (unserializedOptions
)
114 // Publish Sleep Options in registry under root_domain
115 fOwner
->setProperty( kRootDomainSleepOptionsKey
, unserializedOptions
);
117 *returnCode
= fOwner
->sleepSystemOptions( unserializedOptions
);
119 unserializedOptions
->release();
122 // Clear any pre-existing options
123 fOwner
->removeProperty( kRootDomainSleepOptionsKey
);
125 *returnCode
= fOwner
->sleepSystemOptions( NULL
);
129 *returnCode
= kIOReturnNotPrivileged
;
132 return kIOReturnSuccess
;
135 IOReturn
RootDomainUserClient::secureSetAggressiveness(
137 unsigned long newLevel
,
142 IOReturn ret
= kIOReturnNotPrivileged
;
144 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeLocalUser
);
145 local_priv
= (kIOReturnSuccess
== ret
);
147 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
148 admin_priv
= (kIOReturnSuccess
== ret
);
150 if((local_priv
|| admin_priv
) && fOwner
) {
151 *return_code
= fOwner
->setAggressiveness(type
, newLevel
);
153 *return_code
= kIOReturnNotPrivileged
;
155 return kIOReturnSuccess
;
158 IOReturn
RootDomainUserClient::secureSetMaintenanceWakeCalendar(
159 IOPMCalendarStruct
*inCalendar
,
160 uint32_t *returnCode
)
163 IOReturn ret
= kIOReturnNotPrivileged
;
165 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
166 admin_priv
= (kIOReturnSuccess
== ret
);
168 if (admin_priv
&& fOwner
) {
169 *returnCode
= fOwner
->setMaintenanceWakeCalendar(inCalendar
);
171 *returnCode
= kIOReturnNotPrivileged
;
173 return kIOReturnSuccess
;
176 IOReturn
RootDomainUserClient::secureSetUserAssertionLevels(
177 uint32_t assertionBitfield
)
180 IOReturn ret
= kIOReturnNotPrivileged
;
182 ret
= clientHasPrivilege(fOwningTask
, kIOClientPrivilegeAdministrator
);
183 admin_priv
= (kIOReturnSuccess
== ret
);
185 if (admin_priv
&& fOwner
) {
186 ret
= fOwner
->setPMAssertionUserLevels(assertionBitfield
);
188 ret
= kIOReturnNotPrivileged
;
190 return kIOReturnSuccess
;
193 IOReturn
RootDomainUserClient::clientClose( void )
198 task_deallocate(fOwningTask
);
202 return kIOReturnSuccess
;
205 IOReturn
RootDomainUserClient::clientMemoryForType(
207 IOOptionBits
*options
,
208 IOMemoryDescriptor
** memory
)
211 return kIOReturnNotReady
;
213 if (kPMRootDomainMapTraceBuffer
== type
)
215 *memory
= fOwner
->getPMTraceMemoryDescriptor();
219 return kIOReturnSuccess
;
221 return kIOReturnNotFound
;
225 return kIOReturnUnsupported
;
228 IOReturn
RootDomainUserClient::externalMethod(
230 IOExternalMethodArguments
* arguments
,
231 IOExternalMethodDispatch
* dispatch __unused
,
232 OSObject
* target __unused
,
233 void * reference __unused
)
235 IOReturn ret
= kIOReturnBadArgument
;
239 case kPMSetAggressiveness
:
240 if ((2 == arguments
->scalarInputCount
)
241 && (1 == arguments
->scalarOutputCount
))
243 ret
= this->secureSetAggressiveness(
244 (unsigned long)arguments
->scalarInput
[0],
245 (unsigned long)arguments
->scalarInput
[1],
246 (int *)&arguments
->scalarOutput
[0]);
250 case kPMGetAggressiveness
:
251 if ((1 == arguments
->scalarInputCount
)
252 && (1 == arguments
->scalarOutputCount
))
254 ret
= fOwner
->getAggressiveness(
255 (unsigned long)arguments
->scalarInput
[0],
256 (unsigned long *)&arguments
->scalarOutput
[0]);
261 if (1 == arguments
->scalarOutputCount
)
263 ret
= this->secureSleepSystem(
264 (uint32_t *)&arguments
->scalarOutput
[0]);
268 case kPMAllowPowerChange
:
269 if (1 == arguments
->scalarInputCount
)
271 ret
= fOwner
->allowPowerChange(
272 arguments
->scalarInput
[0]);
276 case kPMCancelPowerChange
:
277 if (1 == arguments
->scalarInputCount
)
279 ret
= fOwner
->cancelPowerChange(
280 arguments
->scalarInput
[0]);
284 case kPMShutdownSystem
:
285 // deperecated interface
286 ret
= kIOReturnUnsupported
;
289 case kPMRestartSystem
:
290 // deperecated interface
291 ret
= kIOReturnUnsupported
;
294 case kPMSleepSystemOptions
:
295 ret
= this->secureSleepSystemOptions(
296 arguments
->structureInput
,
297 arguments
->structureInputSize
,
298 (uint32_t *)&arguments
->scalarOutput
[0]);
300 case kPMSetMaintenanceWakeCalendar
:
301 ret
= this->secureSetMaintenanceWakeCalendar(
302 (IOPMCalendarStruct
*)arguments
->structureInput
,
303 (uint32_t *)&arguments
->structureOutput
);
304 arguments
->structureOutputSize
= sizeof(uint32_t);
307 case kPMSetUserAssertionLevels
:
308 ret
= this->secureSetUserAssertionLevels(
309 (uint32_t)arguments
->scalarInput
[0]);
313 case kPMMethodCopySystemTimeline:
314 // intentional fallthrough
315 case kPMMethodCopyDetailedTimeline:
317 if (!arguments->structureOutputDescriptor)
319 // TODO: Force IOKit.framework to always send this data out
320 // of line; so I don't have to create a MemoryDescriptor here.
321 mem_size = arguments->structureOutputSize;
322 mem = IOMemoryDescriptor::withAddressRange(
323 (mach_vm_address_t)arguments->structureOutput,
324 (mach_vm_size_t)mem_size,
325 kIODirectionIn, current_task());
327 mem_size = arguments->structureOutputDescriptorSize;
328 if (( mem = arguments->structureOutputDescriptor ))
334 mem->prepare(kIODirectionNone);
336 if (kPMMethodCopySystemTimeline == selector) {
337 arguments->scalarOutput[0] = fOwner->copySystemTimeline(
341 if (kPMMethodCopyDetailedTimeline == selector) {
342 arguments->scalarOutput[0] = fOwner->copyDetailedTimeline(
346 if (arguments->structureOutputDescriptor) {
347 arguments->structureOutputDescriptorSize = mem_size;
349 arguments->structureOutputSize = mem_size;
354 ret = kIOReturnSuccess;
356 ret = kIOReturnCannotWire;
363 return kIOReturnBadArgument
;
369 /* getTargetAndMethodForIndex
370 * Not used. We prefer to use externalMethod() for user client invocations.
371 * We maintain getTargetAndExternalMethod since it's an exported symbol,
372 * and only for that reason.
374 IOExternalMethod
* RootDomainUserClient::getTargetAndMethodForIndex(
375 IOService
** targetP
, UInt32 index
)
378 return super::getTargetAndMethodForIndex(targetP
, index
);
382 * Does nothing. Exists only for exported symbol compatibility.
385 RootDomainUserClient::setPreventative(UInt32 on_off
, UInt32 types_of_sleep
)
386 { return; } // DO NOT EDIT