author | William Roberts <williamr@symbian.org> |
Thu, 22 Jul 2010 16:48:56 +0100 | |
branch | GCC_SURGE |
changeset 45 | 4b03adbd26ca |
parent 18 | 47c74d1534e1 |
parent 34 | 5fae379060a7 |
permissions | -rw-r--r-- |
31 | 1 |
// Copyright (c) 2005-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 |
// Name : signalserver.cpp |
|
15 |
// Part of : Signal Server |
|
16 |
// |
|
17 |
||
18 |
||
19 |
||
20 |
#include <e32base.h> |
|
21 |
#include "signalserver.h" |
|
22 |
#include "signalsession.h" |
|
23 |
#include "signalcommon.h" |
|
24 |
#include "tsignalmessage.h" |
|
25 |
#include <signal.h> |
|
26 |
||
27 |
||
28 |
||
29 |
static void RunTheServerL() |
|
30 |
{ |
|
31 |
CSignalServer* lServer = 0; |
|
32 |
||
33 |
User::LeaveIfError(RThread::RenameMe(KServerName)); |
|
34 |
||
35 |
CActiveScheduler* scheduler = new (ELeave) CActiveScheduler; |
|
36 |
CleanupStack::PushL(scheduler); |
|
37 |
CActiveScheduler::Install(scheduler); |
|
38 |
lServer = static_cast<CSignalServer*>(CSignalServer::NewL()); |
|
39 |
RProcess::Rendezvous(KErrNone); |
|
40 |
CActiveScheduler::Start(); |
|
41 |
if(lServer) |
|
42 |
{ |
|
43 |
delete lServer; |
|
44 |
lServer = 0; |
|
45 |
} |
|
46 |
CleanupStack::PopAndDestroy(); |
|
47 |
} |
|
48 |
||
49 |
static TInt RunServer() |
|
50 |
{ |
|
51 |
TRAPD(ret, RunTheServerL()); |
|
52 |
return ret; |
|
53 |
} |
|
54 |
||
55 |
//------------------------------------------------------------------------------- |
|
56 |
// Function Name : E32Main() |
|
57 |
// Description : Entry point for the server process. |
|
58 |
// |
|
59 |
//------------------------------------------------------------------------------- |
|
60 |
TInt E32Main() |
|
61 |
{ |
|
62 |
TInt ret = KErrNoMemory; |
|
63 |
CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
64 |
if (cleanup) |
|
65 |
{ |
|
66 |
ret = RunServer(); |
|
67 |
delete cleanup; |
|
68 |
} |
|
69 |
return ret; |
|
70 |
} |
|
71 |
||
72 |
CSignalServer::CSignalServer(CActive::TPriority aPriority) : CServer2(aPriority, ESharableSessions) |
|
73 |
{ |
|
74 |
iSessionCount = 0; |
|
75 |
} |
|
76 |
||
77 |
CSignalServer::~CSignalServer() |
|
78 |
{ |
|
79 |
if(iShutDown) |
|
80 |
{ |
|
81 |
delete iShutDown; |
|
82 |
iShutDown = 0; |
|
83 |
} |
|
84 |
} |
|
85 |
||
86 |
CServer2* CSignalServer::NewL() |
|
87 |
{ |
|
88 |
CSignalServer* self = new (ELeave)CSignalServer(EPriorityStandard); |
|
89 |
CleanupStack::PushL(self); |
|
90 |
self->ConstructL(); |
|
91 |
CleanupStack::Pop(); |
|
92 |
return self; |
|
93 |
} |
|
94 |
||
95 |
void CSignalServer::ConstructL() |
|
96 |
{ |
|
97 |
StartL(KServerName); |
|
98 |
iShutDown = CSigShutDown::NewL(); |
|
99 |
RPipe::Init(); |
|
100 |
iShutDown->Start(); |
|
101 |
} |
|
102 |
||
103 |
TInt CSignalServer::CheckForCapability(const RMessage2& aMessage) |
|
104 |
{ |
|
105 |
RThread lThread; |
|
106 |
RProcess lProcess; |
|
107 |
TInt lRetVal = KErrGeneral; |
|
108 |
||
109 |
TInt lPid = aMessage.Int0(); |
|
110 |
TInt lSigNo = aMessage.Int1(); |
|
111 |
||
112 |
if(lSigNo != SIGKILL && lSigNo != SIGSTOP) |
|
113 |
return KErrNone; |
|
114 |
||
115 |
lRetVal = aMessage.Client(lThread); |
|
116 |
if(lRetVal!= KErrNone) |
|
117 |
return lRetVal; |
|
118 |
lRetVal = lThread.Process(lProcess); |
|
119 |
if(lRetVal!= KErrNone) |
|
120 |
return lRetVal; |
|
121 |
||
122 |
if(!lProcess.HasCapability(ECapabilityPowerMgmt) && lPid!=lProcess.Id()) |
|
123 |
lRetVal = KErrGeneral; |
|
124 |
else |
|
125 |
lRetVal = KErrNone; |
|
126 |
||
127 |
lThread.Close(); |
|
128 |
lProcess.Close(); |
|
129 |
return lRetVal; |
|
130 |
} |
|
131 |
||
132 |
CSession2* CSignalServer::NewSessionL(const TVersion& aVersion,const RMessage2& /*aMessage2*/) const |
|
133 |
{ |
|
134 |
TVersion v(KSignalServMajorVersionNumber, KSignalServMinorVersionNumber, KSignalServBuildVersionNumber); |
|
135 |
||
136 |
if (!User::QueryVersionSupported(v, aVersion)) |
|
137 |
{ |
|
138 |
User::Leave(KErrNotSupported); |
|
139 |
} |
|
140 |
||
141 |
return CSignalSession::NewL(); // TBD |
|
142 |
} |
|
143 |
||
144 |
void CSignalServer::AddSession() |
|
145 |
{ |
|
146 |
iSessionCount++; |
|
147 |
iShutDown->Cancel(); |
|
148 |
} |
|
149 |
||
150 |
void CSignalServer::RemoveSession() |
|
151 |
{ |
|
152 |
iSessionCount--; |
|
153 |
||
154 |
TInt lIdx = 0; |
|
155 |
for(lIdx = 0; lIdx < iProcessHdlList.Count();) |
|
156 |
{ |
|
157 |
TProcessId lPid(iProcessHdlList[lIdx].mPid); |
|
158 |
RProcess lProcHdl; |
|
159 |
if(lProcHdl.Open(lPid) != KErrNone) |
|
160 |
{ |
|
161 |
iProcessHdlList[lIdx].mPipe.Close(); |
|
162 |
iProcessHdlList.Remove(lIdx); |
|
163 |
} |
|
164 |
else |
|
165 |
{ |
|
166 |
lProcHdl.Close(); |
|
167 |
lIdx++; |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
if( iSessionCount == 0 ) |
|
172 |
{ |
|
173 |
iShutDown->Start(); |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
void CSignalServer::ServiceL(const RMessage2& aMessage) |
|
178 |
{ |
|
179 |
TInt lPid = 0; |
|
180 |
TOeProcessHdl lTmpHdl; |
|
181 |
TInt lIndex = 0; |
|
182 |
TUint8 lSignal = 0; |
|
183 |
RThread lClientThread; |
|
184 |
RProcess lProcess; |
|
185 |
||
186 |
switch (aMessage.Function ()) |
|
187 |
{ |
|
188 |
//if this query is related to message Q, call |
|
189 |
//ServiceL of message Q handler |
|
190 |
case ESendSignal: |
|
191 |
if(CheckForCapability(aMessage) != KErrNone) |
|
192 |
aMessage.Complete(KErrPermissionDenied); |
|
193 |
else |
|
194 |
{ |
|
195 |
// serialize data |
|
196 |
lPid = aMessage.Int0(); |
|
197 |
lSignal = aMessage.Int1(); |
|
198 |
||
199 |
lTmpHdl.mPid = lPid; |
|
200 |
lIndex = iProcessHdlList.Find(lTmpHdl,TIdentityRelation<TOeProcessHdl>(TOeProcessHdl::MatchByPid)); |
|
201 |
||
202 |
if(lIndex == KErrNotFound) |
|
203 |
aMessage.Complete(KErrGeneral); |
|
204 |
else |
|
205 |
{ |
|
206 |
TSignalMessage lMsg; |
|
207 |
lMsg.mType = TSignalMessage::ESignal; |
|
208 |
lMsg.mSignal = lSignal; |
|
209 |
TBuf8<KSigMsgLength> lMsgBuf; |
|
210 |
lMsg.Marshall(lMsgBuf); |
|
211 |
||
212 |
iProcessHdlList[lIndex].mPipe.WriteBlocking(lMsgBuf,KSigMsgLength); |
|
213 |
aMessage.Complete (KErrNone); |
|
214 |
} |
|
215 |
} |
|
216 |
break; |
|
217 |
case ESendSignalValue: |
|
218 |
if(CheckForCapability(aMessage) != KErrNone) |
|
219 |
aMessage.Complete(KErrPermissionDenied); |
|
220 |
else |
|
221 |
{ |
|
222 |
// serialize data |
|
223 |
lPid = aMessage.Int0(); |
|
224 |
lSignal = aMessage.Int1(); |
|
225 |
TUint32 lValue = (TUint32)aMessage.Int2(); |
|
226 |
lTmpHdl.mPid = lPid; |
|
227 |
||
228 |
lIndex = iProcessHdlList.Find(lTmpHdl,TIdentityRelation<TOeProcessHdl>(TOeProcessHdl::MatchByPid)); |
|
229 |
if(lIndex == KErrNotFound) |
|
230 |
aMessage.Complete(KErrGeneral); |
|
231 |
else |
|
232 |
{ |
|
233 |
TSignalMessage lMsg; |
|
234 |
lMsg.mType = TSignalMessage::ESignalValuePair; |
|
235 |
lMsg.mSigVal.mSignal = lSignal; |
|
236 |
lMsg.mSigVal.mValue = lValue; |
|
237 |
TBuf8<KSigMsgLength> lMsgBuf; |
|
238 |
lMsg.Marshall(lMsgBuf); |
|
239 |
||
240 |
iProcessHdlList[lIndex].mPipe.WriteBlocking(lMsgBuf,KSigMsgLength); |
|
241 |
aMessage.Complete (KErrNone); |
|
242 |
} |
|
243 |
} |
|
244 |
break; |
|
245 |
case ESendPipe: |
|
246 |
aMessage.Client(lClientThread); |
|
247 |
lClientThread.Process(lProcess); |
|
248 |
lPid = lProcess.Id(); |
|
249 |
||
250 |
lTmpHdl.mPid = lPid; |
|
251 |
lIndex = iProcessHdlList.Find(lTmpHdl,TIdentityRelation<TOeProcessHdl>(TOeProcessHdl::MatchByPid)); |
|
252 |
if(lIndex == KErrNotFound) |
|
253 |
{ |
|
254 |
TOeProcessHdl lHdl; |
|
255 |
lHdl.mPid = lPid; |
|
256 |
lHdl.mPipe.Open(aMessage,0); |
|
257 |
iProcessHdlList.Append(lHdl); |
|
34
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
258 |
aMessage.Complete(KErrNone); |
31 | 259 |
} |
260 |
else |
|
261 |
{ |
|
262 |
if(iProcessHdlList[lIndex].mPipe.Open(aMessage,0)!=KErrNone) |
|
263 |
aMessage.Complete(KErrGeneral); |
|
34
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
264 |
else |
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
265 |
aMessage.Complete(KErrNone); |
31 | 266 |
} |
34
5fae379060a7
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
267 |
|
31 | 268 |
lProcess.Close(); |
269 |
lClientThread.Close(); |
|
270 |
break; |
|
271 |
default: |
|
272 |
aMessage.Complete(KErrNotSupported); |
|
273 |
break; |
|
274 |
} |
|
275 |
return; |
|
276 |
} |