]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/powerwatch.cpp
Security-59306.101.1.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / powerwatch.cpp
1 /*
2 * Copyright (c) 2000-2004,2011-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // powerwatch - hook into system notifications of power events
27 //
28 #include "powerwatch.h"
29 #include <IOKit/IOMessage.h>
30
31 #if TARGET_OS_OSX
32
33 namespace Security {
34 namespace MachPlusPlus {
35
36
37 //
38 // The obligatory empty virtual destructor
39 //
40 PowerWatcher::~PowerWatcher()
41 { }
42
43
44 //
45 // The default NULL implementations of the callback virtuals.
46 // We define these (rather than leaving them abstract) since
47 // many users want only one of these events.
48 //
49 void PowerWatcher::systemWillSleep()
50 { }
51
52 void PowerWatcher::systemIsWaking()
53 { }
54
55 void PowerWatcher::systemWillPowerDown()
56 { }
57
58 void PowerWatcher::systemWillPowerOn()
59 { }
60
61 //
62 // IOPowerWatchers
63 //
64
65 IOPowerWatcher::IOPowerWatcher() :
66 mKernelPort(0)
67 {
68 if (!(mKernelPort = ::IORegisterForSystemPower(this, &mPortRef, ioCallback, &mHandle)))
69 UnixError::throwMe(EINVAL); // no clue
70 }
71
72 IOPowerWatcher::~IOPowerWatcher()
73 {
74 if (mKernelPort)
75 ::IODeregisterForSystemPower(&mHandle);
76 }
77
78 //
79 // The callback dispatcher
80 //
81 void IOPowerWatcher::ioCallback(void *refCon, io_service_t service,
82 natural_t messageType, void *argument)
83 {
84 IOPowerWatcher *me = (IOPowerWatcher *)refCon;
85 enum { allow, refuse, ignore } reaction;
86 switch (messageType) {
87 case kIOMessageSystemWillSleep:
88 secnotice("powerwatch", "system will sleep");
89 me->systemWillSleep();
90 reaction = allow;
91 break;
92 case kIOMessageSystemHasPoweredOn:
93 secnotice("powerwatch", "system has powered on");
94 me->systemIsWaking();
95 reaction = ignore;
96 break;
97 case kIOMessageSystemWillPowerOff:
98 secnotice("powerwatch", "system will power off");
99 me->systemWillPowerDown();
100 reaction = allow;
101 break;
102 case kIOMessageSystemWillNotPowerOff:
103 secnotice("powerwatch", "system will not power off");
104 reaction = ignore;
105 break;
106 case kIOMessageCanSystemSleep:
107 secnotice("powerwatch", "can system sleep");
108 reaction = allow;
109 break;
110 case kIOMessageSystemWillNotSleep:
111 secnotice("powerwatch", "system will not sleep");
112 reaction = ignore;
113 break;
114 case kIOMessageCanSystemPowerOff:
115 secnotice("powerwatch", "can system power off");
116 reaction = allow;
117 break;
118 case kIOMessageSystemWillPowerOn:
119 secnotice("powerwatch", "system will power on");
120 me->systemWillPowerOn();
121 reaction = ignore;
122 break;
123 default:
124 secnotice("powerwatch",
125 "type 0x%x message received (ignored)", messageType);
126 reaction = ignore;
127 break;
128 }
129
130 // handle acknowledgments
131 switch (reaction) {
132 case allow:
133 secnotice("powerwatch", "calling IOAllowPowerChange");
134 IOAllowPowerChange(me->mKernelPort, long(argument));
135 break;
136 case refuse:
137 secnotice("powerwatch", "calling IOCancelPowerChange");
138 IOCancelPowerChange(me->mKernelPort, long(argument));
139 break;
140 case ignore:
141 secnotice("powerwatch", "sending no response");
142 break;
143 }
144 }
145
146
147 //
148 // The MachServer hookup
149 //
150 PortPowerWatcher::PortPowerWatcher()
151 {
152 port(IONotificationPortGetMachPort(mPortRef));
153 }
154
155 boolean_t PortPowerWatcher::handle(mach_msg_header_t *in)
156 {
157 IODispatchCalloutFromMessage(NULL, in, mPortRef);
158 return TRUE;
159 }
160
161
162 } // end namespace MachPlusPlus
163
164 } // end namespace Security
165
166 #endif //TARGET_OS_OSX