emailservices/emailservermonitor/src/emailservermonitorutilities.cpp
changeset 0 8466d47a6819
child 10 f5907b1a1053
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2009 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: 
       
    15 * Implementation of EmailServerMonitor utility functions
       
    16 *
       
    17 */
       
    18 //  Include Files  
       
    19 
       
    20 #include <e32base.h>
       
    21 #include <e32std.h>
       
    22 #include <e32des16.h>                   // Descriptors
       
    23 
       
    24 #include "emailtrace.h"
       
    25 #include "emailservermonitorutilities.h"
       
    26 
       
    27 // One second as micro seconds
       
    28 const TInt KEmailServerMonitorOneSecond = 1000000;
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // Wait some time to give other applications and servers some time to shut
       
    32 // down themselves gracefully
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 void Wait( TInt aWaitTimeInSeconds /*= KDefaultTimeToWaitInSeconds*/ )
       
    36     {
       
    37     FUNC_LOG;
       
    38     User::After( aWaitTimeInSeconds * KEmailServerMonitorOneSecond );
       
    39     }
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // Checks if the process is already running
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 TBool IsProcessRunning( TSecureId aUid, RProcess* aProcess /* = NULL */ )
       
    46     {
       
    47     FUNC_LOG;
       
    48     
       
    49     // Get our own UID, if we are checking wheter our own process is running,
       
    50     // we are basically checking is there some other instance already running.
       
    51     // So it's allowed to have one process running (ourselves), but not more.
       
    52     // In normal case already one instance is counted as running.
       
    53     TInt numberOfInstancesRunning = 0;
       
    54     TInt numberOfInstancesAllowed = 0;
       
    55     RProcess ownProcess;
       
    56     if( ownProcess.SecureId() == aUid )
       
    57         {
       
    58         numberOfInstancesAllowed++;
       
    59         }
       
    60     ownProcess.Close();
       
    61     
       
    62     TFileName fileName;
       
    63     TFindProcess finder;
       
    64     while ( finder.Next( fileName ) == KErrNone )
       
    65         {
       
    66         RProcess process;
       
    67         TInt outcome = process.Open( fileName );
       
    68         if ( outcome != KErrNone )
       
    69             {
       
    70             INFO( "Could not open process. Checking next one." );
       
    71             continue;
       
    72             }
       
    73         
       
    74         // Check is this the searched process and is the process running
       
    75         if ( process.SecureId() == aUid && 
       
    76              process.ExitType() == EExitPending )
       
    77             {
       
    78             numberOfInstancesRunning++;
       
    79 
       
    80             if ( numberOfInstancesRunning > numberOfInstancesAllowed )
       
    81                 {
       
    82                 // We either found second instance of ourselves or first
       
    83                 // instance of other searched process
       
    84                 process.Close();
       
    85                 // If process handle given, update it with found process
       
    86                 if( aProcess )
       
    87                     {
       
    88                     aProcess->Close();
       
    89                     TInt error = aProcess->Open( fileName );
       
    90                     // If process handle was given, it's assumed to be
       
    91                     // up-to-date if we return success, so we need to return
       
    92                     // failure if we can't reopen any of the found process'.
       
    93                     // This should be very very rare case.
       
    94                     if( error != KErrNone )
       
    95                         {
       
    96                         continue;
       
    97                         }
       
    98                     }
       
    99                 return ETrue;
       
   100                 }
       
   101             }
       
   102         
       
   103         process.Close();
       
   104         }
       
   105     
       
   106     return EFalse;
       
   107     }