]>
Commit | Line | Data |
---|---|---|
55e303ae | 1 | /* |
39236c6e | 2 | * Copyright (c) 1998-2012 Apple Computer, Inc. All rights reserved. |
55e303ae | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
55e303ae A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
30 | * | |
31 | */ | |
32 | ||
33 | #include <IOKit/assert.h> | |
34 | #include <IOKit/IOLib.h> | |
0c530ab8 | 35 | #include <IOKit/IOKitKeys.h> |
55e303ae A |
36 | #include <IOKit/IOBufferMemoryDescriptor.h> |
37 | #include "RootDomainUserClient.h" | |
38 | #include <IOKit/pwr_mgt/IOPMLibDefs.h> | |
316670eb | 39 | #include <IOKit/pwr_mgt/IOPMPrivate.h> |
39236c6e | 40 | #include <sys/proc.h> |
55e303ae A |
41 | |
42 | #define super IOUserClient | |
43 | ||
44 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
45 | ||
46 | OSDefineMetaClassAndStructors(RootDomainUserClient, IOUserClient) | |
47 | ||
48 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
49 | ||
0a7de745 A |
50 | bool |
51 | RootDomainUserClient::initWithTask(task_t owningTask, void *security_id, | |
52 | UInt32 type, OSDictionary * properties) | |
91447636 | 53 | { |
0a7de745 A |
54 | if (properties) { |
55 | properties->setObject(kIOUserClientCrossEndianCompatibleKey, kOSBooleanTrue); | |
56 | } | |
0c530ab8 | 57 | |
0a7de745 A |
58 | if (!super::initWithTask(owningTask, security_id, type, properties)) { |
59 | return false; | |
60 | } | |
91447636 | 61 | |
0a7de745 A |
62 | fOwningTask = owningTask; |
63 | task_reference(fOwningTask); | |
64 | return true; | |
91447636 A |
65 | } |
66 | ||
67 | ||
0a7de745 A |
68 | bool |
69 | RootDomainUserClient::start( IOService * provider ) | |
55e303ae | 70 | { |
0a7de745 A |
71 | assert(OSDynamicCast(IOPMrootDomain, provider)); |
72 | if (!super::start(provider)) { | |
73 | return false; | |
74 | } | |
75 | fOwner = (IOPMrootDomain *)provider; | |
55e303ae A |
76 | |
77 | ||
0a7de745 | 78 | return true; |
55e303ae A |
79 | } |
80 | ||
0a7de745 A |
81 | IOReturn |
82 | RootDomainUserClient::secureSleepSystem( uint32_t *return_code ) | |
91447636 | 83 | { |
0a7de745 | 84 | return secureSleepSystemOptions(NULL, 0, return_code); |
2d21ac55 A |
85 | } |
86 | ||
0a7de745 A |
87 | IOReturn |
88 | RootDomainUserClient::secureSleepSystemOptions( | |
89 | const void *inOptions, | |
90 | IOByteCount inOptionsSize, | |
91 | uint32_t *returnCode) | |
2d21ac55 | 92 | { |
0a7de745 A |
93 | int local_priv = 0; |
94 | int admin_priv = 0; | |
95 | IOReturn ret = kIOReturnNotPrivileged; | |
0a7de745 A |
96 | |
97 | ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeLocalUser); | |
98 | local_priv = (kIOReturnSuccess == ret); | |
99 | ||
100 | ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator); | |
101 | admin_priv = (kIOReturnSuccess == ret); | |
102 | ||
0a7de745 | 103 | if ((local_priv || admin_priv) && fOwner) { |
cb323159 A |
104 | OSString *unserializeErrorString = NULL; |
105 | OSObject *unserializedObject = NULL; | |
106 | OSDictionary *sleepOptionsDict = NULL; // do not release | |
107 | ||
0a7de745 A |
108 | proc_t p; |
109 | p = (proc_t)get_bsdtask_info(fOwningTask); | |
110 | if (p) { | |
111 | fOwner->setProperty("SleepRequestedByPID", proc_pid(p), 32); | |
112 | } | |
113 | ||
cb323159 A |
114 | if (inOptions) { |
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"); | |
120 | } | |
121 | } | |
0a7de745 | 122 | |
cb323159 A |
123 | if (sleepOptionsDict) { |
124 | // Publish Sleep Options in registry under root_domain | |
125 | fOwner->setProperty( kRootDomainSleepOptionsKey, sleepOptionsDict); | |
0a7de745 A |
126 | } else { |
127 | // No options | |
128 | // Clear any pre-existing options | |
129 | fOwner->removeProperty( kRootDomainSleepOptionsKey ); | |
0a7de745 | 130 | } |
cb323159 A |
131 | |
132 | *returnCode = fOwner->sleepSystemOptions( sleepOptionsDict ); | |
133 | OSSafeReleaseNULL(unserializedObject); | |
134 | OSSafeReleaseNULL(unserializeErrorString); | |
0a7de745 A |
135 | } else { |
136 | *returnCode = kIOReturnNotPrivileged; | |
137 | } | |
138 | ||
139 | return kIOReturnSuccess; | |
91447636 A |
140 | } |
141 | ||
0a7de745 A |
142 | IOReturn |
143 | RootDomainUserClient::secureSetAggressiveness( | |
144 | unsigned long type, | |
145 | unsigned long newLevel, | |
146 | int *return_code ) | |
91447636 | 147 | { |
0a7de745 A |
148 | int local_priv = 0; |
149 | int admin_priv = 0; | |
150 | IOReturn ret = kIOReturnNotPrivileged; | |
151 | ||
152 | ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeLocalUser); | |
153 | local_priv = (kIOReturnSuccess == ret); | |
154 | ||
155 | ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator); | |
156 | admin_priv = (kIOReturnSuccess == ret); | |
157 | ||
158 | if ((local_priv || admin_priv) && fOwner) { | |
159 | *return_code = fOwner->setAggressiveness(type, newLevel); | |
160 | } else { | |
161 | *return_code = kIOReturnNotPrivileged; | |
162 | } | |
163 | return kIOReturnSuccess; | |
91447636 A |
164 | } |
165 | ||
0a7de745 A |
166 | IOReturn |
167 | RootDomainUserClient::secureSetMaintenanceWakeCalendar( | |
168 | IOPMCalendarStruct *inCalendar, | |
169 | uint32_t *returnCode) | |
b0d623f7 | 170 | { |
0a7de745 A |
171 | int admin_priv = 0; |
172 | IOReturn ret = kIOReturnNotPrivileged; | |
173 | ||
174 | ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator); | |
175 | admin_priv = (kIOReturnSuccess == ret); | |
176 | ||
177 | if (admin_priv && fOwner) { | |
178 | *returnCode = fOwner->setMaintenanceWakeCalendar(inCalendar); | |
179 | } else { | |
180 | *returnCode = kIOReturnNotPrivileged; | |
181 | } | |
182 | return kIOReturnSuccess; | |
b0d623f7 | 183 | } |
55e303ae | 184 | |
0a7de745 A |
185 | IOReturn |
186 | RootDomainUserClient::secureSetUserAssertionLevels( | |
187 | uint32_t assertionBitfield) | |
0b4c1975 | 188 | { |
0a7de745 A |
189 | int admin_priv = 0; |
190 | IOReturn ret = kIOReturnNotPrivileged; | |
191 | ||
192 | ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator); | |
193 | admin_priv = (kIOReturnSuccess == ret); | |
194 | ||
195 | if (admin_priv && fOwner) { | |
196 | ret = fOwner->setPMAssertionUserLevels(assertionBitfield); | |
197 | } else { | |
198 | ret = kIOReturnNotPrivileged; | |
199 | } | |
200 | return kIOReturnSuccess; | |
0b4c1975 A |
201 | } |
202 | ||
0a7de745 A |
203 | IOReturn |
204 | RootDomainUserClient::secureGetSystemSleepType( | |
205 | uint32_t *outSleepType, uint32_t *sleepTimer) | |
db609669 | 206 | { |
0a7de745 A |
207 | int admin_priv = 0; |
208 | IOReturn ret; | |
209 | ||
210 | ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator); | |
211 | admin_priv = (kIOReturnSuccess == ret); | |
212 | ||
213 | if (admin_priv && fOwner) { | |
214 | ret = fOwner->getSystemSleepType(outSleepType, sleepTimer); | |
215 | } else { | |
216 | ret = kIOReturnNotPrivileged; | |
217 | } | |
218 | return ret; | |
db609669 A |
219 | } |
220 | ||
0a7de745 A |
221 | IOReturn |
222 | RootDomainUserClient::clientClose( void ) | |
55e303ae | 223 | { |
0a7de745 | 224 | terminate(); |
fe8ab488 | 225 | |
0a7de745 | 226 | return kIOReturnSuccess; |
d190cdc3 A |
227 | } |
228 | ||
0a7de745 A |
229 | void |
230 | RootDomainUserClient::stop( IOService *provider) | |
d190cdc3 | 231 | { |
0a7de745 A |
232 | if (fOwningTask) { |
233 | task_deallocate(fOwningTask); | |
cb323159 | 234 | fOwningTask = NULL; |
0a7de745 | 235 | } |
fe8ab488 | 236 | |
0a7de745 | 237 | super::stop(provider); |
55e303ae A |
238 | } |
239 | ||
0a7de745 A |
240 | IOReturn |
241 | RootDomainUserClient::externalMethod( | |
242 | uint32_t selector, | |
243 | IOExternalMethodArguments * arguments, | |
244 | IOExternalMethodDispatch * dispatch __unused, | |
245 | OSObject * target __unused, | |
246 | void * reference __unused ) | |
2d21ac55 | 247 | { |
0a7de745 A |
248 | IOReturn ret = kIOReturnBadArgument; |
249 | ||
250 | switch (selector) { | |
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]); | |
258 | } | |
259 | break; | |
260 | ||
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]); | |
267 | } | |
268 | break; | |
269 | ||
270 | case kPMSleepSystem: | |
271 | if (1 == arguments->scalarOutputCount) { | |
272 | ret = this->secureSleepSystem( | |
273 | (uint32_t *)&arguments->scalarOutput[0]); | |
274 | } | |
275 | break; | |
276 | ||
277 | case kPMAllowPowerChange: | |
278 | if (1 == arguments->scalarInputCount) { | |
279 | ret = fOwner->allowPowerChange( | |
280 | arguments->scalarInput[0]); | |
281 | } | |
282 | break; | |
283 | ||
284 | case kPMCancelPowerChange: | |
285 | if (1 == arguments->scalarInputCount) { | |
286 | ret = fOwner->cancelPowerChange( | |
287 | arguments->scalarInput[0]); | |
288 | } | |
289 | break; | |
290 | ||
291 | case kPMShutdownSystem: | |
292 | // deperecated interface | |
293 | ret = kIOReturnUnsupported; | |
294 | break; | |
295 | ||
296 | case kPMRestartSystem: | |
297 | // deperecated interface | |
298 | ret = kIOReturnUnsupported; | |
299 | break; | |
300 | ||
301 | case kPMSleepSystemOptions: | |
302 | ret = this->secureSleepSystemOptions( | |
303 | arguments->structureInput, | |
304 | arguments->structureInputSize, | |
305 | (uint32_t *)&arguments->scalarOutput[0]); | |
306 | break; | |
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); | |
314 | } | |
315 | break; | |
316 | ||
317 | case kPMSetUserAssertionLevels: | |
318 | ret = this->secureSetUserAssertionLevels( | |
319 | (uint32_t)arguments->scalarInput[0]); | |
320 | break; | |
321 | ||
322 | case kPMActivityTickle: | |
323 | if (fOwner->checkSystemCanSustainFullWake()) { | |
324 | fOwner->reportUserInput(); | |
325 | fOwner->setProperty(kIOPMRootDomainWakeTypeKey, "UserActivity Assertion"); | |
326 | } | |
327 | ret = kIOReturnSuccess; | |
328 | break; | |
329 | ||
330 | case kPMSetClamshellSleepState: | |
331 | fOwner->setDisableClamShellSleep(arguments->scalarInput[0] ? true : false); | |
332 | ret = kIOReturnSuccess; | |
333 | break; | |
334 | ||
335 | case kPMGetSystemSleepType: | |
336 | if (2 == arguments->scalarOutputCount) { | |
337 | ret = this->secureGetSystemSleepType( | |
338 | (uint32_t *) &arguments->scalarOutput[0], | |
339 | (uint32_t *) &arguments->scalarOutput[1]); | |
340 | } | |
341 | break; | |
39236c6e | 342 | |
fe8ab488 | 343 | #if defined(__i386__) || defined(__x86_64__) |
0a7de745 A |
344 | case kPMSleepWakeWatchdogEnable: |
345 | ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator); | |
346 | if (ret == kIOReturnSuccess) { | |
347 | fOwner->sleepWakeDebugEnableWdog(); | |
348 | } | |
349 | break; | |
350 | ||
351 | ||
352 | case kPMSleepWakeDebugTrig: | |
353 | ret = clientHasPrivilege(fOwningTask, kIOClientPrivilegeAdministrator); | |
354 | if (ret == kIOReturnSuccess) { | |
355 | fOwner->sleepWakeDebugTrig(false); | |
356 | } | |
357 | break; | |
fe8ab488 | 358 | #endif |
39236c6e | 359 | |
0a7de745 A |
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]); | |
365 | } | |
366 | } | |
367 | break; | |
368 | ||
369 | default: | |
370 | // bad selector | |
371 | return kIOReturnBadArgument; | |
372 | } | |
373 | ||
374 | return ret; | |
2d21ac55 | 375 | } |
2d21ac55 | 376 | |
6d2010ae A |
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. | |
381 | */ | |
0a7de745 A |
382 | IOExternalMethod * |
383 | RootDomainUserClient::getTargetAndMethodForIndex( | |
384 | IOService ** targetP, UInt32 index ) | |
55e303ae | 385 | { |
0a7de745 A |
386 | // DO NOT EDIT |
387 | return super::getTargetAndMethodForIndex(targetP, index); | |
55e303ae A |
388 | } |
389 | ||
6d2010ae A |
390 | /* setPreventative |
391 | * Does nothing. Exists only for exported symbol compatibility. | |
392 | */ | |
fe8ab488 | 393 | void |
6d2010ae | 394 | RootDomainUserClient::setPreventative(UInt32 on_off, UInt32 types_of_sleep) |
0a7de745 A |
395 | { |
396 | return; | |
397 | } // DO NOT EDIT |