remotestoragefw/remotefileengine/src/rsfwrfeserver.cpp
branchRCL_3
changeset 19 88ee4cf65e19
parent 16 87c71b25c937
child 20 1aa8c82cb4cb
equal deleted inserted replaced
16:87c71b25c937 19:88ee4cf65e19
     1 /*
       
     2 * Copyright (c) 2003-2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Remote File Engine server
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <e32svr.h>
       
    22 #include <e32math.h>
       
    23 #include <e32cons.h>
       
    24 
       
    25 #include <bacline.h>
       
    26 
       
    27 #include "rsfwvolumetable.h"
       
    28 #include "rsfwcommon.h"
       
    29 #include "rsfwinterface.h"
       
    30 #include "rsfwrfesession.h"
       
    31 #include "rsfwrfeserver.h"
       
    32 #include "mdebug.h"
       
    33 #include "ecom.h"
       
    34 #include "rsfwmountstore.h"
       
    35 
       
    36 #include "rsfwconfig.h"
       
    37 
       
    38 
       
    39 // ----------------------------------------------------------------------------------------
       
    40 // Server's policy here
       
    41 // ----------------------------------------------------------------------------------------
       
    42 
       
    43 //Total number of ranges
       
    44 const TUint remoteFileEngineRangeCount = 3;
       
    45 
       
    46 //Definition of the ranges of IPC numbers
       
    47 const TInt remoteFileEngineRanges[remoteFileEngineRangeCount] = 
       
    48         {
       
    49         0, // 0 & 1 ; ERfeRequest, EAsynchRequest
       
    50         2, // 2 ; The Control API starts from EMount
       
    51         12 // 12 ; The Access API starts from ERenameReplace
       
    52         }; 
       
    53 
       
    54 //Policy to implement for each of the above ranges        
       
    55 const TUint8 remoteFileEngineElementsIndex[remoteFileEngineRangeCount] = 
       
    56         {
       
    57         CPolicyServer::EAlwaysPass,	//applies to 0th range
       
    58         0,  //applies to 1st range
       
    59         1  //applies to 2nd range
       
    60         };
       
    61 
       
    62 //Specific capability checks
       
    63 const static CPolicyServer::TPolicyElement remoteFileEngineElements[] = 
       
    64         {
       
    65         // action = -1 ===> failing calls happens via CustomFailureActionL
       
    66         // File Server is always allowed based on its SID from that function
       
    67         //policy "0" for the Control API; fail call if NetworkServices and ReadDeviceData not present
       
    68         {_INIT_SECURITY_POLICY_C2(ECapabilityNetworkServices, ECapabilityReadDeviceData), -1},  
       
    69         //policy "1"; for the Access API, fail call if Network Services and AllFiles not prosent
       
    70         {_INIT_SECURITY_POLICY_C2(ECapabilityNetworkServices, ECapabilityAllFiles), -1} 
       
    71         };
       
    72 
       
    73 //Package all the above together into a policy
       
    74 const CPolicyServer::TPolicy remoteFileEnginePolicy =
       
    75         {
       
    76         CPolicyServer::EAlwaysPass, //specifies all connect attempts should pass
       
    77         remoteFileEngineRangeCount,	//number of ranges                                   
       
    78         remoteFileEngineRanges,	//ranges array
       
    79         remoteFileEngineElementsIndex,	//elements<->ranges index
       
    80         remoteFileEngineElements,		//array of elements
       
    81         };
       
    82 
       
    83 
       
    84 // DATA STRUCTURES
       
    85 TRfeEnv* CRsfwRfeServer::iEnvp;
       
    86 
       
    87 // ============================ MEMBER FUNCTIONS ==============================
       
    88 
       
    89 
       
    90 // ----------------------------------------------------------------------------
       
    91 // CRsfwRfeServer::CRsfwRfeServer
       
    92 // ----------------------------------------------------------------------------
       
    93 // 
       
    94 inline CRsfwRfeServer::CRsfwRfeServer(TInt aPriority, TServerType aType)
       
    95     :CPolicyServer(aPriority, remoteFileEnginePolicy, aType)
       
    96     {
       
    97     }
       
    98 
       
    99 // ----------------------------------------------------------------------------
       
   100 // CRsfwRfeServer::NewL
       
   101 // ----------------------------------------------------------------------------
       
   102 // 
       
   103 
       
   104 CRsfwRfeServer* CRsfwRfeServer::NewL()
       
   105     {
       
   106     CRsfwRfeServer* self = CRsfwRfeServer::NewLC();
       
   107     CleanupStack::Pop(self);
       
   108     return self;
       
   109     }
       
   110 
       
   111 // ----------------------------------------------------------------------------
       
   112 // CRsfwRfeServer::NewLC
       
   113 // ----------------------------------------------------------------------------
       
   114 // 
       
   115 
       
   116 CRsfwRfeServer* CRsfwRfeServer::NewLC()
       
   117     {
       
   118     CRsfwRfeServer* self = new (ELeave) CRsfwRfeServer(EPriorityNormal,
       
   119                                                ESharableSessions);
       
   120     CleanupStack::PushL(self);
       
   121     self->ConstructL();
       
   122     return self;
       
   123     }
       
   124 
       
   125 // ----------------------------------------------------------------------------
       
   126 // CRsfwRfeServer::ConstructL
       
   127 // ----------------------------------------------------------------------------
       
   128 // 
       
   129 
       
   130 void CRsfwRfeServer::ConstructL()
       
   131     {
       
   132     StartL(KRemoteFEName);
       
   133     DEBUGSTRING(("registered RFE name 0x%x", this));
       
   134 
       
   135     // Prepare the environment
       
   136     iEnvp = &iEnv;
       
   137     iDelayedShutdownTimer = CPeriodic::NewL(CActive::EPriorityLow);
       
   138     User::LeaveIfError(iEnvp->iFs.Connect());
       
   139     iEnvp->iRsfwConfig = CRsfwConfig::NewL(KCRUidRsfwCtrl);  
       
   140     
       
   141 	// Make cache root directory
       
   142    	PrepareCacheRootL();
       
   143    	// Load configuration
       
   144    	// Create volume table
       
   145    	iVolumes = CRsfwVolumeTable::NewL(this, iEnvp->iRsfwConfig);
       
   146     }
       
   147 
       
   148 // ----------------------------------------------------------------------------
       
   149 // CRsfwRfeServer::ThreadFunction
       
   150 // ----------------------------------------------------------------------------
       
   151 //
       
   152 TInt CRsfwRfeServer::ThreadFunction(TAny* /*aNone*/)
       
   153     {
       
   154     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   155     if (!cleanupStack)
       
   156         {
       
   157         PanicServer(ECreateTrapCleanup);
       
   158         }
       
   159 
       
   160 //  __UHEAP_MARK;
       
   161     TRAPD(err, ThreadFunctionL());
       
   162 //  __UHEAP_MARKENDC(0);
       
   163     if (err != KErrNone)
       
   164         {
       
   165         PanicServer(ESrvCreateServer);
       
   166         }
       
   167 
       
   168     delete cleanupStack;
       
   169     cleanupStack = NULL;
       
   170 
       
   171     return KErrNone;
       
   172     }
       
   173 
       
   174 // ----------------------------------------------------------------------------
       
   175 // CRsfwRfeServer::IncrementSessions
       
   176 // ----------------------------------------------------------------------------
       
   177 //
       
   178 void CRsfwRfeServer::IncrementSessions()
       
   179     {
       
   180     StopDelayedShutdownTimer();
       
   181     iSessionCount++;
       
   182     DEBUGSTRING(("+session count = %d", iSessionCount));
       
   183     }
       
   184 
       
   185 // ----------------------------------------------------------------------------
       
   186 // CRsfwRfeServer::DecrementSessions
       
   187 // ----------------------------------------------------------------------------
       
   188 //
       
   189 void CRsfwRfeServer::DecrementSessions()
       
   190     {
       
   191     iSessionCount--;
       
   192     // this debug output crashes the server for some reason
       
   193 //    DEBUGSTRING(("-session count = %d", iSessionCount));
       
   194     // Note that the event causing server to shut down
       
   195     // is not session count going to zero, as 
       
   196     // there are "permanent" session(s) from the File Server plugin.
       
   197     // (they would be closed when remote drives are unmounted, which never happens)
       
   198     // Instead, server shutdown is triggered by last connected volume
       
   199     // going to disconnect state, or inactivity timeout expires and
       
   200     // there are no open files.
       
   201     }
       
   202 
       
   203 // ----------------------------------------------------------------------------
       
   204 // CRsfwRfeServer::AllEnginesIdling
       
   205 // ----------------------------------------------------------------------------
       
   206 //
       
   207 void CRsfwRfeServer::AllEnginesIdling(TInt aTimeout)
       
   208     {
       
   209     if (!iShuttingDown)
       
   210         {
       
   211         DEBUGSTRING(("starting to shut down after %d seconds", aTimeout));
       
   212         if (aTimeout)
       
   213             {
       
   214             StartDelayedShutdownTimer(aTimeout);
       
   215             }
       
   216         else
       
   217             {
       
   218             ShutDown();
       
   219             }
       
   220         }
       
   221     }
       
   222 
       
   223 // ----------------------------------------------------------------------------
       
   224 // CRsfwRfeServer::ServiceRequested
       
   225 // ----------------------------------------------------------------------------
       
   226 //
       
   227 void CRsfwRfeServer::ServiceRequested()
       
   228     {
       
   229     StopDelayedShutdownTimer();
       
   230     }
       
   231 
       
   232 // ----------------------------------------------------------------------------
       
   233 // CRsfwRfeServer::RunError
       
   234 // ----------------------------------------------------------------------------
       
   235 //
       
   236 TInt CRsfwRfeServer::RunError(TInt aError)
       
   237     {
       
   238     if (aError == KErrBadDescriptor)
       
   239         {
       
   240         // A bad descriptor error implies a badly programmed client,
       
   241         // so panic it;
       
   242         // otherwise report the error to the client
       
   243         PanicClient(Message(), EBadDescriptor);
       
   244         }
       
   245     else
       
   246         {
       
   247         Message().Complete(aError);
       
   248         }
       
   249 
       
   250     // The leave will result in an early return from CServer::RunL(), skipping
       
   251     // the call to request another message. So do that now in order to keep the
       
   252     // server running.
       
   253     ReStart();
       
   254 
       
   255     return KErrNone;    // handled the error fully
       
   256     }
       
   257 
       
   258 
       
   259 // ----------------------------------------------------------------------------
       
   260 // CRsfwRfeServer::CustomFailureActionL
       
   261 // ----------------------------------------------------------------------------
       
   262 //
       
   263 CPolicyServer::TCustomResult CRsfwRfeServer::CustomFailureActionL(const RMessage2& aMsg, 
       
   264                                                    TInt /* aAction */, 
       
   265                                                    const TSecurityInfo& /*aMissing */)
       
   266     {
       
   267     TCustomResult result = EFail;
       
   268     TSecureId secId = aMsg.SecureId();
       
   269     if (secId = KFileServerSecureUid) 
       
   270         {
       
   271         result = EPass;
       
   272         }
       
   273     return result;
       
   274     }
       
   275 
       
   276 
       
   277 
       
   278 // ----------------------------------------------------------------------------
       
   279 // CRsfwRfeServer::PanicClient
       
   280 // ----------------------------------------------------------------------------
       
   281 //
       
   282 void CRsfwRfeServer::PanicClient(const RMessage2& aMessage, TRfePanic aPanic)
       
   283     {
       
   284     aMessage.Panic(KRfeServer, aPanic);
       
   285     }
       
   286 
       
   287 // ----------------------------------------------------------------------------
       
   288 // CRsfwRfeServer::PanicServer
       
   289 // ----------------------------------------------------------------------------
       
   290 //
       
   291 void CRsfwRfeServer::PanicServer(TRfePanic aPanic)
       
   292     {
       
   293     User::Panic(KRfeServer, aPanic);
       
   294     }
       
   295 
       
   296 // ----------------------------------------------------------------------------
       
   297 // CRsfwRfeServer::ThreadFunctionL
       
   298 // ----------------------------------------------------------------------------
       
   299 //
       
   300 void CRsfwRfeServer::ThreadFunctionL()
       
   301     {
       
   302 
       
   303     // Construct active scheduler
       
   304     CActiveScheduler* activeScheduler = new (ELeave) CActiveScheduler;
       
   305     CleanupStack::PushL(activeScheduler);
       
   306 
       
   307     // Install active scheduler.
       
   308     // We don't need to check whether an active scheduler is already installed
       
   309     // as this is a new thread, so there won't be one
       
   310     CActiveScheduler::Install(activeScheduler);
       
   311 
       
   312     // Change the name of the thread, so it is easier to recognize
       
   313     User::RenameThread(KRfeMain);
       
   314     // Construct our server
       
   315     CRsfwRfeServer::NewLC();    // anonymous
       
   316 
       
   317     RSemaphore semaphore;
       
   318     TInt err;
       
   319     err = semaphore.OpenGlobal(KRfeSemaphoreName);
       
   320     if (err == KErrNotFound)
       
   321         {
       
   322         err = semaphore.CreateGlobal(KRfeSemaphoreName, 0);
       
   323         }
       
   324     User::LeaveIfError(err);
       
   325 
       
   326     // Semaphore opened ok
       
   327     semaphore.Signal();
       
   328     semaphore.Close();
       
   329 
       
   330 #ifdef _DEBUG
       
   331     {
       
   332     TInt8* p = (TInt8*)User::Alloc(1);
       
   333     DEBUGSTRING(("Test alloc addr=0x%x", p));
       
   334     delete p;
       
   335     DEBUGSTRING(("Enter alloc count=%d", User::CountAllocCells()));
       
   336     TInt b;
       
   337     TInt a = User::Available(b);
       
   338     DEBUGSTRING(("Enter alloc avail=%d, biggest=%d", a, b));
       
   339     }
       
   340 #endif
       
   341 
       
   342     // Start handling requests
       
   343     CActiveScheduler::Start();
       
   344 #ifdef _DEBUG
       
   345     {
       
   346     DEBUGSTRING(("Exit alloc count=%d", User::CountAllocCells()));
       
   347     TInt b;
       
   348     TInt a = User::Available(b);
       
   349     DEBUGSTRING(("Exit alloc avail=%d, biggest=%d", a, b));
       
   350     }
       
   351 #endif
       
   352     CleanupStack::PopAndDestroy(2, activeScheduler);
       
   353     }
       
   354 
       
   355 // ----------------------------------------------------------------------------
       
   356 // CRsfwRfeServer::NewSessionL
       
   357 // ----------------------------------------------------------------------------
       
   358 //     
       
   359 CSession2* CRsfwRfeServer::NewSessionL(const TVersion &aVersion,
       
   360                                    const RMessage2&) const
       
   361     {
       
   362     // Check we're the right version
       
   363     if (!User::QueryVersionSupported(TVersion(KRfeMajorVersionNumber,
       
   364                                               KRfeMinorVersionNumber,
       
   365                                               KRfeBuildVersionNumber),
       
   366                                      aVersion))
       
   367         {
       
   368         User::Leave(KErrNotSupported);
       
   369         }
       
   370     // Make new session
       
   371     return CRsfwRfeSession::NewL(*const_cast<CRsfwRfeServer*> (this));
       
   372     }
       
   373 
       
   374 // ----------------------------------------------------------------------------
       
   375 // CRsfwRfeServer::PrepareCacheRootL
       
   376 // Get the cache path and create the directory if it does not exist
       
   377 // ----------------------------------------------------------------------------
       
   378 //   
       
   379 void CRsfwRfeServer::PrepareCacheRootL()
       
   380     {
       
   381     TInt err;
       
   382     err = iEnvp->iRsfwConfig->Get(RsfwConfigKeys::KCacheDirectoryPath,
       
   383                                   iEnvp->iCacheRoot);
       
   384     if (err == KErrNone)
       
   385         {
       
   386         TBuf<KMaxRsfwConfItemLength> driveString;
       
   387         if ((iEnvp->iCacheRoot.Length() < 2) || (iEnvp->iCacheRoot[1] != ':'))
       
   388             {
       
   389             err = iEnvp->iRsfwConfig->Get(RsfwConfigKeys::KRsfwDefaultDrive,
       
   390                                           driveString);
       
   391             if (err != KErrNone)
       
   392                 {
       
   393                 driveString.Copy(KRSFWDefaultDrive);
       
   394                 }
       
   395             }
       
   396         if (driveString.Length() < 2)
       
   397             {
       
   398             driveString.Append(':');
       
   399             }
       
   400         iEnvp->iCacheRoot.Insert(0, driveString);
       
   401         }
       
   402     else
       
   403         {
       
   404         HBufC* defaultcacheRoot = HBufC::NewL(KMaxPath);
       
   405         TPtr defaultCache(defaultcacheRoot->Des());
       
   406         defaultCache.Append(KRSFWDefaultDrive);
       
   407         defaultCache.Append(KCacheRootDefault);
       
   408         iEnvp->iCacheRoot.Copy(defaultCache);
       
   409         delete defaultcacheRoot;
       
   410         }
       
   411     RFs& fs = iEnvp->iFs;
       
   412     TUint att;
       
   413     TChar cacheDriveChar = iEnvp->iCacheRoot[0];
       
   414     User::LeaveIfError(fs.CharToDrive(cacheDriveChar, iEnvp->iCacheDrive));
       
   415     err = fs.Att(iEnvp->iCacheRoot, att);
       
   416     if (err != KErrNone)
       
   417         {
       
   418         // There was no prior cache root
       
   419         err = fs.MkDirAll(iEnvp->iCacheRoot);
       
   420         DEBUGSTRING(("Cache root creation failed with err=%d", err));
       
   421         User::LeaveIfError(err);
       
   422         }
       
   423     }
       
   424 
       
   425 // ----------------------------------------------------------------------------
       
   426 // CRsfwRfeServer::ShutDown
       
   427 // ----------------------------------------------------------------------------
       
   428 //
       
   429 void CRsfwRfeServer::ShutDown()
       
   430     {
       
   431     DEBUGSTRING(("shutting down"));
       
   432     iShuttingDown = ETrue;
       
   433     CActiveScheduler::Stop();
       
   434     delete iVolumes;
       
   435     iVolumes = NULL;
       
   436     delete iEnvp->iRsfwConfig;
       
   437     iEnvp->iRsfwConfig = NULL;
       
   438     iEnvp->iFs.Close();
       
   439     delete iDelayedShutdownTimer;
       
   440     iDelayedShutdownTimer = NULL;
       
   441     // REComSession::FinalClose must be called when everything else
       
   442     // related to ECom use has been deleted
       
   443     REComSession::FinalClose(); 
       
   444     DEBUGSTRING(("shut down"));
       
   445     }
       
   446 
       
   447 // ----------------------------------------------------------------------------
       
   448 // CRsfwRfeServer::StartDelayedShutdownTimer
       
   449 // ----------------------------------------------------------------------------
       
   450 //
       
   451 void CRsfwRfeServer::StartDelayedShutdownTimer(TInt aTimeout)
       
   452     {
       
   453     if (!iShuttingDown)
       
   454         {
       
   455         iDelayedShutdownTimer->Cancel();
       
   456         DEBUGSTRING(("shutting down in %d seconds",
       
   457                      aTimeout));
       
   458         TCallBack callBack(CRsfwRfeServer::DelayedShutdownTimerExpired, this);
       
   459         iDelayedShutdownTimer->Start(aTimeout * 1000000,
       
   460                                      aTimeout * 1000000,
       
   461                                      callBack);
       
   462         }
       
   463     }
       
   464 
       
   465 // ----------------------------------------------------------------------------
       
   466 // CRsfwRfeServer::StopDelayedShutdownTimer
       
   467 // ----------------------------------------------------------------------------
       
   468 //
       
   469 void CRsfwRfeServer::StopDelayedShutdownTimer()
       
   470     {
       
   471     if (iDelayedShutdownTimer)
       
   472         {
       
   473         iDelayedShutdownTimer->Cancel();
       
   474         }
       
   475     }
       
   476 
       
   477 // ----------------------------------------------------------------------------
       
   478 // CRsfwRfeServer::DelayedShutdownTimerExpired
       
   479 // ----------------------------------------------------------------------------
       
   480 //
       
   481 TInt CRsfwRfeServer::DelayedShutdownTimerExpired(TAny* aArg)
       
   482     {   
       
   483     CRsfwRfeServer* rfeServer = static_cast<CRsfwRfeServer*>(aArg);
       
   484     rfeServer->ShutDown();
       
   485     return 0;
       
   486     }
       
   487 
       
   488 // ----------------------------------------------------------------------------
       
   489 // E32Main
       
   490 // ----------------------------------------------------------------------------
       
   491 //
       
   492 TInt E32Main()
       
   493     {
       
   494     return CRsfwRfeServer::ThreadFunction(NULL);
       
   495     }
       
   496 
       
   497 
       
   498 // End of File