Example #1
0
// -----------------------------------------------------------------------------
// RDRMHelper::StartServer
// -----------------------------------------------------------------------------
//
TInt RDRMHelper::StartServer()
{
    RThread currentThread;
    const TUint32 secureIdAsTuint32( currentThread.SecureId() );
    TRACE2( "RDRMHelper::StartServer(): currentThread: 0x%08x",
            secureIdAsTuint32 );
    if ( currentThread.SecureId() == KServerSecureId )
    {
        // HelperServer cannot connect to itself.
        return KErrCouldNotConnect;
    }

    TInt result( 0 );

    TFindServer findHelperServer( KDRMHelperServerName );
    TFullName name;

    result = findHelperServer.Next( name );
    TRACE2( "RDRMHelper::StartServer(): result: %d", result );
    if ( result == KErrNone )
    {
        // Server already running
        return KErrNone;
    }

    RSemaphore semaphoreStarting;
    TInt semaphoreExist( semaphoreStarting.CreateGlobal(
                             KDRMHelperServerSemaphoreStartingName, 0 ) );
    TRACE2( "RDRMHelper::StartServer(): semaphoreExist: %d", semaphoreExist );

    if( semaphoreExist != KErrAlreadyExists && semaphoreExist != KErrNone )
    {
        return semaphoreExist;
    }

    // Semaphore exists, wait until server is finished with it's tasks
    if ( semaphoreExist == KErrAlreadyExists )
    {
        TInt openErr( semaphoreStarting.OpenGlobal(
                          KDRMHelperServerSemaphoreStartingName) );
        TRACE2( "RDRMHelper::StartServer(): openErr: %d", openErr );
        if ( openErr != KErrNone )
        {
            return openErr;
        }

        TRACE( "RDRMHelper::StartServer(): semaphoreStarting.Wait()" );
        semaphoreStarting.Wait();
        TRACE( "RDRMHelper::StartServer(): semaphoreStarting.Wait() - returning" );
    }
    else
    {
        RSemaphore semaphoreStartServer;
        TInt result( semaphoreStartServer.CreateGlobal(
                         KDRMHelperServerSemaphoreName, 0 ) );
        TRACE2( "RDRMHelper::StartServer(): 2 result: %d", result );
        if ( result != KErrNone )
        {
            semaphoreStarting.Close();
            return result;
        }

        result = CreateServerProcess();
        TRACE2( "RDRMHelper::StartServer(): 3 result: %d", result );
        if ( result != KErrNone )
        {
            return  result;
        }

        semaphoreStartServer.Wait();
        semaphoreStartServer.Close();

        TRACE( "RDRMHelper::StartServer(): semaphoreStarting.Signal()" );

        semaphoreStarting.Signal();
        semaphoreStarting.Close();
    }

    TRACE( "RDRMHelper::StartServer(): return KErrNone" );
    return  KErrNone;
}
Example #2
0
void SemaphoreRelease(void* Handle,int n)
{
	RSemaphore p;
	p.SetHandle((int)Handle);
	p.Signal(n);			
}
// -----------------------------------------------------------------------------
// RUsbPnClient::ConnectL
// (other items were commented in a header).
// -----------------------------------------------------------------------------
//
EXPORT_C void RUsbPnClient::ConnectL()
    {
    OstTrace0( TRACE_BORDER, RUSBPNCLIENT_CONNECTL_ENTRY, "RUsbPnClient::ConnectL" );
    A_TRACE( ( _T( "RUsbPnClient::ConnectL()" ) ) );

    TInt err( KErrNone );

    // Create USB Phonet Link Server session
    err = CreateSession( KUsbPnServerName, TVersion(1,0,0));

    if ( err == KErrNotFound )
        {
        // Session not created

        // Find lock semaphore for server process creation
        TFindSemaphore lock( KUsbPnServerName );
        TFullName semaphoreName;
        err = lock.Next( semaphoreName );

        if ( err == KErrNotFound )
            {
            // Lock is not enabled
            OstTrace0( TRACE_INTERNALS, RUSBPNCLIENT_CONNECTL, "RUsbPnClient::ConnectL() - semaphore not found, start server" );
            E_TRACE( ( _L( "RUsbPnClient::ConnectL() - semaphore not found, start server" ) ) );

            RSemaphore startLock;

            // Create lock
            User::LeaveIfError( startLock.CreateGlobal( KUsbPnServerName, 0, EOwnerProcess ) );


            /********************************************/
            /* Start the USB Phonet Link process process */
            TRequestStatus status;
            RProcess server;

            User::LeaveIfError( server.Create( KUsbPnServerName, TPtrC( NULL, 0),
                         EOwnerThread ) );

            server.Rendezvous( status );

            if( status != KRequestPending )
              {
              server.Kill(0);  // Abort startup
              }
            else
              {
              server.Resume();  // Logon OK -> start the server
              }

            OstTrace0( TRACE_INTERNALS, RUSBPNCLIENT_CONNECTL_DUP1, "RUsbPnClient::ConnectL() - waiting server response" );
            E_TRACE( ( _T( "RUsbPnClient::ConnectL() - waiting server response" ) ) );
            User::WaitForRequest( status );  // Wait for start or death

            // we can't use the 'exit reason' if the server panicked as this
            // is the panic 'reason' and may be '0' which cannot be distinguished
            // from KErrNone
            TInt err = status.Int();
            if (err == KErrNone && (server.ExitType() == EExitPanic || server.ExitType() == EExitKill))
              {
              err = KErrServerTerminated;
              }

            server.Close();

            if( err )
                {
                OstTrace1( TRACE_INTERNALS, RUSBPNCLIENT_CONNECTL_DUP2, "RUsbPnClient::ConnectL() - waiting server response status; err=%d", err );
                E_TRACE( ( _T( "RUsbPnClient::ConnectL() - waiting server response status: %d" ), err ) );
                TRACE_ASSERT_ALWAYS;
                User::LeaveIfError( err );
                }
            /* End of starting process */
            /********************************************/

            OstTrace0( TRACE_INTERNALS, RUSBPNCLIENT_CONNECTL_DUP3, "RUsbPnClient::ConnectL() - server is started, signal other clients" );
            E_TRACE( ( _L( "RUsbPnClient::ConnectL() - server is started, signal other clients" ) ) );
            // Signal other clients
            startLock.Signal( KMaxTInt );

            // Close semaphore
            startLock.Close();
            }
        else
            {
            // Lock is enabled

            RSemaphore startLock;

            // Open lock semaphore
            User::LeaveIfError( startLock.Open( lock ) );

            OstTrace0( TRACE_INTERNALS, RUSBPNCLIENT_CONNECTL_DUP4, "RUsbPnClient::ConnectL() - server is starting, wait for signal" );
            E_TRACE( ( _L( "RUsbPnClient::ConnectL() - server is starting, wait for signal" ) ) );
            // Wait for signal
            startLock.Wait();
            OstTrace0( TRACE_INTERNALS, RUSBPNCLIENT_CONNECTL_DUP5, "RUsbPnClient::ConnectL() - signal received" );
            E_TRACE( ( _L( "RUsbPnClient::ConnectL() - signal received" ) ) );

            // Close semaphore
            startLock.Close();

            }

        // Create USB Phonet Link server session
        OstTrace0( TRACE_INTERNALS, RUSBPNCLIENT_CONNECTL_DUP6, "RUsbPnClient::ConnectL() - Create session" );
        E_TRACE( ( _L( "RUsbPnClient::ConnectL() - Create session" ) ) );

        User::LeaveIfError( CreateSession( KUsbPnServerName, TVersion(1,0,0) ) );

        OstTrace0( TRACE_INTERNALS, RUSBPNCLIENT_CONNECTL_DUP7, "RUsbPnClient::ConnectL() - session created1" );
        E_TRACE( ( _L( "RUsbPnClient::ConnectL() - session created1" ) ) );
        }
    else if ( err )
        {
        OstTrace0( TRACE_INTERNALS, RUSBPNCLIENT_CONNECTL_DUP8, "RUsbPnClient::ConnectL() - session not created, reason uknown" );
        E_TRACE( ( _L( "RUsbPnClient::ConnectL() - session not created, reason uknown" ) ) );            
        TRACE_ASSERT_ALWAYS;
        User::Leave( err );
        }
    else
        {
        OstTrace0( TRACE_INTERNALS, RUSBPNCLIENT_CONNECTL_DUP9, "RUsbPnClient::ConnectL() - session created2" );
        E_TRACE( ( _T( "RUsbPnClient::ConnectL() - session created2" ) ) );
        }
    OstTrace0( TRACE_BORDER, RUSBPNCLIENT_CONNECTL_EXIT, "RUsbPnClient::ConnectL() - return void" );
    A_TRACE( ( _T( "RUsbPnClient::ConnectL() - return void" ) ) );
    }
/*
-------------------------------------------------------------------------------

    Class: -

    Method: StartSession

    Description: This works and used only in EKA2 environment and used from
                 STIF TestFramework internally.
                 Executable module uses defined capabilities(PlatSec's
                 Capability model) to start session. Function for starting
                 the TestServer and Testmodule/TestClass.
    
                 Starts a new server. Server will be running its own
                 thread and this functions returns when server is up and
                 running or server start-up fails.

    Parameters: None
    
    Return Values: TInt Error code / KErrNone

    Errors/Exceptions: None

    Status: Proposal

-------------------------------------------------------------------------------
*/
EXPORT_C TInt StartSession()
    {
    //__UHEAP_MARK;

    // Get module name from command line
	const TInt length = User::CommandLineLength();
    HBufC* cmdLine = HBufC::New( length );
    
    if ( cmdLine == NULL )
        {
        //__UHEAP_MARKEND;
        return KErrNoMemory;
        }

    TPtr moduleName = cmdLine->Des();
    User::CommandLine( moduleName );

    RDebug::Print(_L("StartSession() Received data [%S]"), &moduleName);

    // Extract semaphore name passed in data    
    TInt index = moduleName.Find(_L(" "));
    RDebug::Print(_L("StartSession() Space separator found at position [%d]"), index);
    TPtrC semaphoreName = moduleName.Mid(index + 1);
    moduleName = moduleName.Left(index);

    RDebug::Print(_L("StartSession() Extracted module name [%S] and sempahore name [%S]"), &moduleName, &semaphoreName);
   
    // Open start-up synchronization semaphore
    RSemaphore startup;
    RDebug::Print(_L(" Opening start-up semaphore"));
//    TName semaphoreName = _L("startupSemaphore");
//    semaphoreName.Append( moduleName );
    
    TInt res = startup.OpenGlobal(semaphoreName);
    RDebug::Print(_L("Opening result %d"), res);    


    TFileName serverName;
    TInt r = StartNewServer ( moduleName, serverName, EFalse, startup);    

    if ( r ==   KErrAlreadyExists )
        {        
        // Ok, server was already started
        RDebug::Print(_L("Server already started, signaling semaphore and exiting"));
        startup.Signal();        
        //__UHEAP_MARKEND;
        
        delete cmdLine;
        
        return KErrNone;
        }
    else
        {       
        RDebug::Print(_L("Server is finished, code %d"), r);
        }

    //__UHEAP_MARKEND;

	// Program execution never comes here, because StartNewServer doesn't return
	// unless server is already started.
	
    delete cmdLine;
    
    // Return start-up result.
    return r;

    }
TInt CMultiTargetAgent::LaunchTargetsInOrderL()
{
    RDebug::Printf( "CMultiTargetAgent::LaunchTargetsInOrderL" );

    RBuf launcher;
    CleanupClosePushL( launcher );
    launcher.CreateL( KLauncherExe() );

    RBuf launcherOptions;
    CleanupClosePushL( launcherOptions );
    launcherOptions.CreateL( KTargetOptions().Length() + 2 );
    launcherOptions.Format( KTargetOptions(), (TUint)ENormalExit );

    RDebug::Printf( ">LaunchProcess()" );
    RProcess launcherProc;
    CleanupClosePushL( launcherProc );

    TInt ret = LaunchProcess( launcherProc, launcher, launcherOptions );
    RDebug::Printf( "<LaunchProcess() ret %d", ret );

    CleanupStack::PopAndDestroy( &launcherProc ); // launcherProc
    CleanupStack::PopAndDestroy( &launcherOptions ); // launcherOptions
    CleanupStack::PopAndDestroy( &launcher ); //launcher

    test( ret == KErrNone );

    RSemaphore launchSemaphore;
    CleanupClosePushL( launchSemaphore );

    TFindSemaphore launchSemFinder( KLaunchSemaphoreNameSearchString );
    TFullName semaphoreResult;
    ret = launchSemFinder.Next(semaphoreResult);
    RDebug::Printf( ">  Find Launch Semaphote.Next ret=%d, %lS", ret, &semaphoreResult );
    test( ret == KErrNone );

    ret = launchSemaphore.OpenGlobal( semaphoreResult );
    RDebug::Printf( "> OpenGlobal semaphore ret=%d", ret );
    test( ret == KErrNone );

    TBool thisLaunchCompleted;

    test.Next(_L("LaunchTargetsInOrderL\n"));
    for( TInt numLaunches = KNumLaunches; numLaunches > 0; numLaunches-- )
    {
        for( TInt numApps = KNumApps; numApps > 0; numApps-- )
        {
            thisLaunchCompleted = EFalse;
            // This will trigger the launcher app to launch the next target
            RDebug::Printf( " >Semaphore.Signal app=%d, launch=%d", numApps, numLaunches);
            launchSemaphore.Signal();

            RBuf8 tgt8Name;
            CleanupClosePushL( tgt8Name );

            RBuf tgtCollapseName;
            CleanupClosePushL( tgtCollapseName );

            tgtCollapseName.CreateL( iTargets[numApps-1] );
            tgt8Name.CreateL( tgtCollapseName.Collapse() );


            while( ! thisLaunchCompleted )
            {
                RDebug::Printf( ">GetEvent app %d for %S", numApps, &tgt8Name );
                iServSession.GetEvent( iTargets[numApps-1], iStatus, iEventPtr );

                // Wait for the target to get started.
                RDebug::Printf( " >Wait for event from target app=%d, launch=%d\n", numApps, numLaunches);
                User::WaitForRequest( iStatus );
                RDebug::Printf( " <Wait for request returned with status %d", iStatus.Int() );
                test( iStatus==KErrNone );

                RDebug::Printf( " > Got iEventType =%d, app=%d", iEventInfo.iEventType, numApps );
                switch( iEventInfo.iEventType )
                {
                case EEventsAddProcess:
                {
                    RDebug::Printf( "Got EEventsAddProcess" );
                    TPtrC8 exeNamePtr8( iEventInfo.iAddProcessInfo.iFileName, iEventInfo.iAddProcessInfo.iFileNameLength );

                    RBuf8 exeName8;
                    CleanupClosePushL( exeName8 );
                    exeName8.CreateL( exeNamePtr8 );
                    RDebug::Printf( " from event: exeName8=%S", &exeName8 );
                    CleanupStack::PopAndDestroy( &exeName8 );

                    RBuf8 compareName8;
                    CleanupClosePushL( compareName8 );
                    compareName8.CreateL( KTargetExeName().Length() + 10 );
                    compareName8.Format( KTargetExeName(), numApps );
                    RDebug::Printf( " comparing to: compareName8=%S", &compareName8 );

                    test( compareName8.CompareC( exeNamePtr8 ) == 0 );
                    CleanupStack::PopAndDestroy( &compareName8 );

                    RDebug::Printf( "Testing if event process id is valid" );
                    test( iEventInfo.iProcessIdValid );
                    RDebug::Printf( "Got iEventInfo.iProcessId=%d", I64LOW( iEventInfo.iProcessId ) );

                    RProcess targetProc;
                    ret = targetProc.Open( TProcessId( iEventInfo.iProcessId ) );
                    RDebug::Printf( "RProcess open ret=%d",ret );
                    targetProc.Close();
                    test( ret == KErrNone );

                    break;
                }//EEventsAddProcess

                case EEventsStartThread:
                {
                    RDebug::Printf( "Got EEventsStartThread" );

                    TPtrC8 exeNamePtr8( iEventInfo.iStartThreadInfo.iFileName, iEventInfo.iStartThreadInfo.iFileNameLength );
                    RBuf8 exe8Name;
                    CleanupClosePushL( exe8Name );
                    exe8Name.CreateL( exeNamePtr8 );
                    RDebug::Printf( " from event: exeName8=%S", &exe8Name );
                    CleanupStack::PopAndDestroy( &exe8Name );

                    test( tgt8Name.CompareC( exeNamePtr8 ) == 0 );

                    RDebug::Printf( "Testing if event process id is valid" );
                    test( iEventInfo.iProcessIdValid );
                    RDebug::Printf( "Got iEventInfo.iProcessId=%d", I64LOW( iEventInfo.iProcessId ) );

                    RDebug::Printf( "Testing if event thread id is valid" );
                    test( iEventInfo.iThreadIdValid );
                    RDebug::Printf( "Got iEventInfo.iThreadId=%d", I64LOW( iEventInfo.iThreadId ) );

                    RThread targetThread;
                    CleanupClosePushL( targetThread );

                    ret = targetThread.Open( TThreadId( iEventInfo.iThreadId ) );
                    RDebug::Printf( "RThread open ret=%d", ret );
                    test( ret == KErrNone );

                    test( iEventInfo.iThreadId == targetThread.Id() );

                    RDebug::Printf( "Resuming thread for app=%d, id=%d", numApps, I64LOW( targetThread.Id() ));
                    ret = iServSession.ResumeThread( iEventInfo.iThreadId );
                    CleanupStack::PopAndDestroy( &targetThread );

                    test( ret == KErrNone );

                    ret = iServSession.ResumeThread( iEventInfo.iThreadId );
                    break;
                }//case EEventsStartThread

                case ( EEventsRemoveProcess ):
                {
                    RDebug::Printf( "*** Got EEventsRemoveProcess. app%d has exited. Moving on to next app", numApps );
                    thisLaunchCompleted = ETrue;
                    break;
                }

                default :
                    RDebug::Printf( "Got unknown event" );
                    test( EFalse );
                    break;
                }
            }//while

            CleanupStack::PopAndDestroy( &tgtCollapseName ); // tgtCollapseName
            CleanupStack::PopAndDestroy( &tgt8Name ); // tgt8Name
        }
    }

    launchSemaphore.Signal();

    CleanupStack::PopAndDestroy( &launchSemaphore ); // launchSemaphore

    for( TInt i = iTargets.Count()-1; i>=0; i-- )
    {
        RDebug::Printf( "Closing target %d", i );
        iTargets[ i ].Close();
    }

    iTargets.Close();

    return KErrNone;
}