PECengine/CoreUtilsLib2/SrvSrc/PEngServerStarter.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2005 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:  Generic thread safe server starter.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include "PEngServerStarter.h"
       
    21 #include "PEngServerStarterDefs.h"
       
    22 #include "TPEngServerParams.h"
       
    23 #include "PresenceDebugPrint.h"
       
    24 
       
    25 #include <E32STD.H>
       
    26 #include <f32file.h>
       
    27 #include <data_caging_path_literals.hrh>
       
    28 
       
    29 
       
    30 _LIT( KPEngPathDelimiter, "\\" );
       
    31 
       
    32 
       
    33 /**
       
    34  * RSessionBase accessor to give to the PEngServerStarter
       
    35  * access to RSessionBase::CreateSession().
       
    36  *
       
    37  * @since 2.6
       
    38  */
       
    39 class RPEngSessionBaseAccessor : public RSessionBase
       
    40     {
       
    41     public: // Constructor
       
    42 
       
    43         /**
       
    44          * C++ constructor.
       
    45          *
       
    46          * @since 2.6
       
    47          */
       
    48         inline RPEngSessionBaseAccessor()
       
    49             {
       
    50             }
       
    51 
       
    52     public: // New functions
       
    53 
       
    54 
       
    55         /**
       
    56          * Calls the RSessionBase::CreateSession().
       
    57          *
       
    58          * @since 2.6
       
    59          * @param aServer See RSessionBase::CreateSession().
       
    60          * @param aVersion See RSessionBase::CreateSession().
       
    61          * @param aAsyncMessageSlots See RSessionBase::CreateSession().
       
    62          * @return See RSessionBase::CreateSession().
       
    63          */
       
    64         inline TInt CreateSession( const TDesC& aServer,
       
    65                                    const TVersion& aVersion,
       
    66                                    TInt aAsyncMessageSlots )
       
    67             {
       
    68             return RSessionBase::CreateSession( aServer,
       
    69                                                 aVersion,
       
    70                                                 aAsyncMessageSlots );
       
    71             }
       
    72     };
       
    73 
       
    74 
       
    75 
       
    76 
       
    77 
       
    78 // ============================= LOCAL FUNCTIONS ===============================
       
    79 
       
    80 
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // GenerateFullServerExe()
       
    84 // Local helper.
       
    85 // Generates full server executable name and path.
       
    86 //
       
    87 //
       
    88 //
       
    89 // Param: aServerName - A plain server name for which to generate the fullname.
       
    90 // Param: aFullServerPath - On the function return contains full
       
    91 //        server path and name.
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 void GenerateFullServerExe( const TDesC& aServerName, TFileName& aFullServerPath )
       
    95     {
       
    96         {
       
    97         //Get drive (C:)
       
    98         TFileName dllPath;
       
    99         Dll::FileName( dllPath );
       
   100         aFullServerPath.Copy( TParsePtrC( dllPath ).Drive() );
       
   101         }
       
   102 
       
   103 
       
   104     //Get path (\Xxxx\Xxxx)
       
   105     aFullServerPath.Append( KDC_PROGRAMS_DIR );
       
   106 
       
   107     //Fix the path delimeter if missing from path
       
   108     TPtrC pathDelim = aFullServerPath.Right( KPEngPathDelimiter().Length() );
       
   109     if ( pathDelim != KPEngPathDelimiter )
       
   110         {
       
   111         aFullServerPath.Append( KPEngPathDelimiter );
       
   112         }
       
   113 
       
   114 
       
   115     //Server name + extension (aServer.EXT)
       
   116     aFullServerPath.Append( aServerName );
       
   117     aFullServerPath.Append( KExtDelimiter );
       
   118 
       
   119     aFullServerPath.Append( KServerNameExtExe );
       
   120 
       
   121     PENG_DP( D_PENG_LIT( "GenerateFullServerExe() [%S]" ), &aFullServerPath );
       
   122     }
       
   123 
       
   124 
       
   125 
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 // ProcessRunning()
       
   129 // Local helper.
       
   130 // Checks is there server process started from the given
       
   131 // exe file (exe or dll).
       
   132 //
       
   133 //
       
   134 // Param: aFullServerExe - Server exe which running status to check.
       
   135 //        NOTE!! In THUMB the process name is the name portion of the
       
   136 //        filename from which the executable is loaded. In WINS the
       
   137 //        thread name is similarly the name portion of the dll filename
       
   138 //        (See CreateWinsThread() below) ==> thus here must be used
       
   139 //        also the exe/dll as parameter.
       
   140 //
       
   141 // Return: KErrNotFound - no matching running process found.
       
   142 //         KErrNone - one matching running process found.
       
   143 //         KErrGeneral - more than one running process found.
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 TInt ProcessRunning( const TDesC& aFullServerExe )
       
   147     {
       
   148     TFindProcess find;
       
   149     RProcess process;
       
   150 
       
   151 
       
   152     //Initialize the find
       
   153     TFullName name( TParsePtrC( aFullServerExe ).Name() );
       
   154     name.Append( KMatchAny );
       
   155     find.Find( name );
       
   156 
       
   157 
       
   158     PENG_DP( D_PENG_LIT( "ProcessRunning() [%S]" ), &name );
       
   159 
       
   160     //loop through all of matching processes
       
   161     TInt runningCount = 0;
       
   162     while ( find.Next( name ) == KErrNone )
       
   163         {
       
   164         TInt error = process.Open( find );
       
   165         if ( error != KErrNone )
       
   166             {
       
   167             //if can't open, the process is .. not .. valid
       
   168             PENG_DP( D_PENG_LIT( "ProcessRunning() - Couldn't open process [%S], error[%d]" ), &name, error );
       
   169             continue;
       
   170             }
       
   171 
       
   172         TExitType exitType = process.ExitType();
       
   173         process.Close();
       
   174 
       
   175         PENG_DP( D_PENG_LIT( "ProcessRunning() - Server process %d found [%S], ExitType[%d] ==> Running[%d]" ),
       
   176                  runningCount, &name, exitType, ( exitType == EExitPending ) );
       
   177 
       
   178         //check if the processes is running
       
   179         if ( exitType == EExitPending )
       
   180             {
       
   181             runningCount++;
       
   182             }
       
   183         }
       
   184 
       
   185 
       
   186     PENG_DP( D_PENG_LIT( "ProcessRunning() [%d] matches found" ), runningCount );
       
   187     switch ( runningCount )
       
   188         {
       
   189         case 0: //No server running
       
   190             {
       
   191             return KErrNotFound;
       
   192             }
       
   193 
       
   194         case 1: //One server running
       
   195             {
       
   196             return KErrNone;
       
   197             }
       
   198 
       
   199         default:
       
   200             {
       
   201             //More than one server instance running
       
   202             return KErrGeneral;
       
   203             }
       
   204         }
       
   205     }
       
   206 
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // DoLaunchServer()
       
   210 // Local helper.
       
   211 // Launches the server process and waits it startup.
       
   212 //
       
   213 // Param: aFullServerExe - The dll / exe from which to launch the server process.
       
   214 //        aServerName - The server name to identify the server
       
   215 //        aParam1 & 2 - Client given parameters to give to created process.
       
   216 //
       
   217 // Return: System standard error code.
       
   218 //
       
   219 // -----------------------------------------------------------------------------
       
   220 //
       
   221 TInt DoLaunchServer( const TDesC& aFullServerExe,
       
   222                      const TDesC& aServerName,
       
   223                      TInt aParam1,
       
   224                      TInt aParam2 )
       
   225     {
       
   226     PENG_DP( D_PENG_LIT( "DoLaunchServer() [%S] as [%S]" ), &aFullServerExe, &aServerName );
       
   227 
       
   228     TInt error( KErrNone );
       
   229     TPEngServerParams startParams( aServerName, aParam1, aParam2 );
       
   230 
       
   231 
       
   232     //Create thread / process according the platform
       
   233     RProcess process;
       
   234     error = process.Create( aFullServerExe, startParams.AsCommandLine() );
       
   235 
       
   236 
       
   237     if ( error != KErrNone )
       
   238         {
       
   239         return error;
       
   240         }
       
   241 
       
   242 
       
   243     //and execute the process and wait it's startup
       
   244     TRequestStatus rendezvousStatus;
       
   245     process.Rendezvous( rendezvousStatus );
       
   246 
       
   247     PENG_DP( D_PENG_LIT( "DoLaunchServer() - Waiting for startup or die..." ) );
       
   248     process.Resume();
       
   249     User::WaitForRequest( rendezvousStatus );               // CSI: 94 #
       
   250     error = rendezvousStatus.Int();
       
   251 
       
   252     if ( ( error == KErrNone ) &&
       
   253          ( process.ExitType() == EExitPending ) )
       
   254         {
       
   255         //Startup signalled from process ==> server successfully started
       
   256         PENG_DP( D_PENG_LIT( "DoLaunchServer() - Server started" ) );
       
   257         }
       
   258 
       
   259     else
       
   260         {
       
   261         //Something failed in server startup
       
   262         TExitCategoryName exitCategory = KNullDesC();
       
   263         exitCategory = process.ExitCategory();
       
   264         PENG_DP( D_PENG_LIT( "DoLaunchServer() - Startup failed: ExitReason[%S, %d], Error[%d]" ),
       
   265                  &exitCategory, process.ExitReason(), error );
       
   266 
       
   267         if ( error == KErrNone )
       
   268             {
       
   269             error = KErrServerTerminated;
       
   270             }
       
   271         }
       
   272 
       
   273     process.Close();
       
   274 
       
   275     return error;
       
   276     }
       
   277 
       
   278 
       
   279 
       
   280 
       
   281 // ============================ MEMBER FUNCTIONS ===============================
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // PEngServerStarter::LaunchServer()
       
   285 // Public member function for clients to launch the server process.
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 EXPORT_C TInt PEngServerStarter::LaunchServer( const TDesC& aServerExeBaseName,
       
   289                                                const TDesC& aServerName,
       
   290                                                TInt aParam1,
       
   291                                                TInt aParam2 )
       
   292     {
       
   293     PENG_DP( D_PENG_LIT( "PEngServerStarter::LaunchServer() [%S]" ), &aServerExeBaseName );
       
   294 
       
   295     RMutex launchMutex;
       
   296 
       
   297         {
       
   298         // Dynamic mutex name used to allow code share.
       
   299         TName launchMutexName( TParsePtrC( aServerExeBaseName ).Name() );
       
   300         launchMutexName.Append( KPEngLaunchMutexNameExtension );
       
   301 
       
   302         // Open or Create mutex to serialize to access to server startup code.
       
   303         // Way below is race condition safe.
       
   304         TInt error( KErrNotFound );
       
   305         while ( error == KErrNotFound )
       
   306             {
       
   307             error = launchMutex.CreateGlobal( launchMutexName );
       
   308             if ( error != KErrAlreadyExists )
       
   309                 {
       
   310                 break;
       
   311                 }
       
   312             error = launchMutex.OpenGlobal( launchMutexName );
       
   313             }
       
   314 
       
   315         if ( error != KErrNone )
       
   316             {
       
   317             return error;
       
   318             }
       
   319         }
       
   320 
       
   321 
       
   322     //Determine the drive for executable
       
   323     TFileName fullServerExe;
       
   324     GenerateFullServerExe( aServerExeBaseName, fullServerExe );
       
   325 
       
   326     TInt error;
       
   327     launchMutex.Wait();
       
   328         {
       
   329         //Serialized section
       
   330         error = ProcessRunning( fullServerExe );
       
   331         if ( error == KErrNotFound )
       
   332             {
       
   333             //server not running
       
   334             error = DoLaunchServer( fullServerExe,
       
   335                                     aServerName,
       
   336                                     aParam1,
       
   337                                     aParam2 );
       
   338             }
       
   339         }
       
   340 
       
   341     launchMutex.Signal();
       
   342     launchMutex.Close();
       
   343 
       
   344     PENG_DP( D_PENG_LIT( "PEngServerStarter::LaunchServer() done[%d]" ), error );
       
   345     return error;
       
   346     }
       
   347 
       
   348 
       
   349 
       
   350 // -----------------------------------------------------------------------------
       
   351 // PEngServerStarter::ConnectServer()
       
   352 // Public member function for clients to connect to server.
       
   353 // -----------------------------------------------------------------------------
       
   354 //
       
   355 EXPORT_C TInt PEngServerStarter::ConnectServer( RSessionBase& aSession,
       
   356                                                 const TDesC& aServerName,
       
   357                                                 const TVersion& aVersion,
       
   358                                                 TInt aAsyncMessageSlots,
       
   359                                                 const TDesC& aServerExeBaseName,
       
   360                                                 TInt aParam1,
       
   361                                                 TInt aParam2 )
       
   362     {
       
   363     if ( aSession.Handle() != KNullHandle )
       
   364         {
       
   365         return KErrInUse;
       
   366         }
       
   367 
       
   368     TInt err = KErrGeneral;
       
   369     TInt wait = KPEngSrvConnRetryWait;
       
   370     for ( TInt tries = 0 ; tries < KPEngSrvConnTries ; tries++ )
       
   371         {
       
   372         RPEngSessionBaseAccessor acc;
       
   373         err = acc.CreateSession( aServerName, aVersion, aAsyncMessageSlots );
       
   374         aSession = acc; //session ownership is now on client
       
   375 
       
   376         if ( err == KErrNone ||
       
   377              ( err != KErrNotFound && err != KErrServerTerminated ) )
       
   378             {
       
   379             break; // connected ok or something else than missing server
       
   380             }
       
   381 
       
   382         // if server not found, try to connect more times
       
   383         if ( ( err == KErrNotFound ) && ( tries < 4 ) )
       
   384             {
       
   385             continue;
       
   386             }
       
   387 
       
   388         err = LaunchServer( aServerExeBaseName, aServerName, aParam1, aParam2 );
       
   389         if ( err != KErrNone )
       
   390             {
       
   391             break; //launch failed
       
   392             }
       
   393 
       
   394 
       
   395         if ( tries > 0 )
       
   396             {
       
   397             //2nd or subsequent try - qive some time for server to startup
       
   398             PENG_DP( D_PENG_LIT( "PEngServerStarter::ConnectServer() - giving time for startup [%d]" ), wait );
       
   399             User::After( wait );    // CSI: 92 #
       
   400             wait = wait + wait;     //On next round wait longer
       
   401             }
       
   402         }
       
   403 
       
   404 
       
   405     PENG_DP( D_PENG_LIT( "PEngServerStarter::ConnectServer( %d )" ), err );
       
   406     return err;
       
   407     }
       
   408 
       
   409 
       
   410 
       
   411 // End of file
       
   412