|
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 /** |
|
17 @file |
|
18 @internalComponent |
|
19 */ |
|
20 |
|
21 #include <e32capability.h> |
|
22 #include "sysmonservsess.h" |
|
23 #include "sysmoncliserv.h" |
|
24 #include "sysmonserver.h" |
|
25 #include "monitor.h" |
|
26 #include <startupproperties.h> |
|
27 #include <hal.h> |
|
28 #include <hal_data.h> |
|
29 |
|
30 #include "../src/SysStartDebug.h" |
|
31 |
|
32 |
|
33 CSysMonSession::~CSysMonSession() |
|
34 { |
|
35 } |
|
36 |
|
37 |
|
38 CSysMonSession::CSysMonSession() |
|
39 { |
|
40 } |
|
41 |
|
42 |
|
43 CSysMonSession* CSysMonSession::NewL() |
|
44 { |
|
45 CSysMonSession *session = new(ELeave) CSysMonSession(); |
|
46 return session; |
|
47 } |
|
48 |
|
49 |
|
50 void CSysMonSession::ServiceL(const RMessage2 &aMessage) |
|
51 { |
|
52 DEBUGPRINT1(_L("SysMonServSess: Received message")); |
|
53 |
|
54 iMessage = const_cast<RMessage2&> (aMessage); |
|
55 switch (iMessage.Function()) |
|
56 { |
|
57 case EMonitor: |
|
58 DoMonitorL(EFalse); |
|
59 iMessage.Complete(KErrNone); |
|
60 break; |
|
61 case EMonitorSelf: |
|
62 DoMonitorL(ETrue); |
|
63 iMessage.Complete(KErrNone); |
|
64 break; |
|
65 case ECancelSelf: |
|
66 CancelMonitorSelfL(); |
|
67 iMessage.Complete(KErrNone); |
|
68 break; |
|
69 default: |
|
70 iMessage.Complete(KErrNotSupported); |
|
71 break; |
|
72 } |
|
73 } |
|
74 |
|
75 |
|
76 void CSysMonSession::ServiceError(const RMessage2 &aMessage, TInt aError) |
|
77 { |
|
78 aMessage.Complete(aError); |
|
79 } |
|
80 |
|
81 |
|
82 /* |
|
83 Handles EMonitor and EMonitorSelf message to monitor a process |
|
84 */ |
|
85 void CSysMonSession::DoMonitorL(TBool aMonitorSelf) |
|
86 { |
|
87 // no new process monitoring will be initiate if shut down is in progress |
|
88 if (SysMonServer()->ShutDownInProgress()) |
|
89 { |
|
90 User::Leave(KErrCancel); |
|
91 } |
|
92 |
|
93 HBufC8 *msg = HBufC8::NewL(iMessage.GetDesLengthL(0)); |
|
94 CleanupStack::PushL(msg); |
|
95 |
|
96 TPtr8 msgPtr(msg->Des()); |
|
97 iMessage.ReadL(0, msgPtr); |
|
98 |
|
99 CStartupProperties *prop = CStartupProperties::NewL(); |
|
100 CleanupStack::PushL(prop); |
|
101 |
|
102 prop->InternalizeL(msgPtr); |
|
103 |
|
104 // Retrieve process id depending function type and check for platform security |
|
105 // For self monitoring, EIgnoreOnFailure does not require any capability |
|
106 // For moniting other process, EIgnoreOnFailure require capability ProtServ |
|
107 TProcessId id; |
|
108 if (aMonitorSelf) |
|
109 { |
|
110 RThread thread; |
|
111 User::LeaveIfError(iMessage.Client(thread)); |
|
112 |
|
113 RProcess process; |
|
114 User::LeaveIfError(thread.Process(process)); |
|
115 |
|
116 id = process.Id(); |
|
117 |
|
118 // Use the filename from the proces |
|
119 TFileName name = process.FileName(); |
|
120 prop->SetFileParamsL(name, prop->Args()); |
|
121 |
|
122 process.Close(); |
|
123 thread.Close(); |
|
124 } |
|
125 else |
|
126 { |
|
127 if (prop->RecoveryMethod() == EIgnoreOnFailure) |
|
128 { |
|
129 if (!iMessage.HasCapability(ECapabilityProtServ)) |
|
130 { |
|
131 User::Leave(KErrPermissionDenied); |
|
132 } |
|
133 } |
|
134 |
|
135 TPckg<TProcessId> procPckg(id); |
|
136 iMessage.ReadL(1, procPckg); |
|
137 } |
|
138 |
|
139 // For ERestartOS, capability ProtServ is required for both self-monitoring and monitoring other |
|
140 if (prop->RecoveryMethod() == ERestartOS) |
|
141 { |
|
142 if (!iMessage.HasCapability(ECapabilityProtServ)) |
|
143 { |
|
144 User::Leave(KErrPermissionDenied); |
|
145 } |
|
146 } |
|
147 // For ERestartOSWithMode, capability ProtServ and PowerMgmt are required for both self-monitoring and monitoring other |
|
148 else if (prop->RecoveryMethod() == ERestartOSWithMode) |
|
149 { |
|
150 if ((!iMessage.HasCapability(ECapabilityProtServ)) || (!iMessage.HasCapability(ECapabilityPowerMgmt))) |
|
151 { |
|
152 User::Leave(KErrPermissionDenied); |
|
153 } |
|
154 |
|
155 #ifndef __WINS__ // HAL::Get(EMaximumRestartStartupModes...) is only supported in H4 |
|
156 // check that the startup mode is valid, first retrieve max startup mode if not retrieved yet |
|
157 TInt maxStartupMode = 0; |
|
158 TInt err = HAL::Get(HALData::EMaximumRestartStartupModes, maxStartupMode); |
|
159 if (err != KErrNone) |
|
160 { |
|
161 DEBUGPRINT2(_L("Failed to get max startup mode err=%d"), err); |
|
162 User::Leave(err); |
|
163 } |
|
164 |
|
165 if (prop->RestartMode() > maxStartupMode) |
|
166 { |
|
167 DEBUGPRINT2(_L("Invalid startupMode=%d"), prop->RestartMode()); |
|
168 User::Leave(KErrArgument); |
|
169 } |
|
170 #endif |
|
171 } |
|
172 |
|
173 #ifdef _DEBUG |
|
174 TPtrC fileName = prop->FileName(); |
|
175 DEBUGPRINT2(_L("SysMonServSess: Creating monitor for %S"), &fileName); |
|
176 #endif |
|
177 |
|
178 const TBool KExecuteRecoveryMethodOnFailure = iMessage.Int2(); |
|
179 CMonitor *monitor = CMonitor::NewL(*SysMonServer(), id, prop, KExecuteRecoveryMethodOnFailure); // a new monitor is created - ownership of prop transferred |
|
180 |
|
181 CleanupStack::Pop(prop); // ownership of prop is passed into monitor |
|
182 CleanupStack::PushL(monitor); |
|
183 |
|
184 DEBUGPRINT1(_L("SysMonServSess: Monitor created.")); |
|
185 |
|
186 // new monitor is added to the list maintained by SysMonServer - ownership of monitor transferred. |
|
187 // monitor will be popped from cleanup stack in AddMonitorL |
|
188 SysMonServer()->AddAndStartMonitorL(monitor); |
|
189 |
|
190 CleanupStack::PopAndDestroy(msg); |
|
191 } |
|
192 |
|
193 |
|
194 /* |
|
195 CSysMonSession::CancelMonitorSelfL() is invoked to cancel self-monitoring |
|
196 */ |
|
197 void CSysMonSession::CancelMonitorSelfL() const |
|
198 { |
|
199 RThread thread; |
|
200 User::LeaveIfError(iMessage.Client(thread)); |
|
201 |
|
202 RProcess process; |
|
203 User::LeaveIfError(thread.Process(process)); |
|
204 |
|
205 SysMonServer()->CancelMonitor(process.Id()); |
|
206 |
|
207 DEBUGPRINT2(_L("SysMonServSess: Monitor Cancelled. ProcessId=%Lu"), process.Id().Id()); |
|
208 |
|
209 process.Close(); |
|
210 thread.Close(); |
|
211 } |
|
212 |
|
213 |
|
214 /* |
|
215 CSysMonSession::SysMonServer() returns a pointer to the server object |
|
216 */ |
|
217 CSysMonServer* CSysMonSession::SysMonServer() const |
|
218 { |
|
219 return static_cast<CSysMonServer*> (const_cast<CServer2*> (Server())); |
|
220 } |