testexecmgmt/ucc/Source/HostExecuteAsync/CSHostexecuteasync.cpp
changeset 0 3da2a79470a7
equal deleted inserted replaced
-1:000000000000 0:3da2a79470a7
       
     1 /*
       
     2 * Copyright (c) 2005-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 * This file was autogenerated by rpcgen, but should be modified by the developer.
       
    16 * Make sure you don't use the -component_mod flag in future or this file will be overwritten.
       
    17 * Thu Oct 16 14:41:55 2003
       
    18 * System Includes
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 #include <stdio.h>
       
    26 #include <signal.h>
       
    27 
       
    28 
       
    29 /****************************************************************************************
       
    30  * 
       
    31  * Local Includes
       
    32  * 
       
    33  ***************************************************************************************/
       
    34 #include "CSvcHostexecuteasync.h"
       
    35 #include "CSHostexecuteasync.h"
       
    36 #include "../include/standard_unix.h"
       
    37 #include "../include/strncpynt.h"
       
    38 
       
    39 /****************************************************************************************
       
    40  * 
       
    41  * Implementation
       
    42  * 
       
    43  ***************************************************************************************/
       
    44 CSHostexecuteasync::CSHostexecuteasync()
       
    45 {
       
    46 	iProcess = NULL;
       
    47 }
       
    48 
       
    49 CSHostexecuteasync::~CSHostexecuteasync()
       
    50 {
       
    51 	assert( iProcess == NULL );
       
    52 }
       
    53 
       
    54 int CSHostexecuteasync::GetKey()
       
    55 {
       
    56 	return iKey;
       
    57 }
       
    58 
       
    59 void CSHostexecuteasync::SetKey( int aKey )
       
    60 {
       
    61 	iKey = aKey;
       
    62 }
       
    63 
       
    64 
       
    65 /****************************************************************************************
       
    66  * 
       
    67  * PUBLIC FUNCTION: cstr_startprocess
       
    68  * 
       
    69  ***************************************************************************************/
       
    70 TResult CSHostexecuteasync::cstr_startprocess( char *aArgs )
       
    71 {
       
    72 	TResult rv = { ERR_NONE, 0, 0 };
       
    73 	int errcode;
       
    74 	TCAProcessError perr;
       
    75 
       
    76 	// create a process object
       
    77 	iProcess = new CAProcess();
       
    78 	assert( iProcess != NULL );
       
    79 
       
    80 	// start the process running
       
    81 	perr = iProcess->StartProcess( aArgs, &errcode, true, true, false );
       
    82 	if( perr != CAE_NONE ) {
       
    83 		rv.iStandardResult = ERR_START_PROCESS_ERROR;
       
    84 		rv.iExtendedCode = (int)perr;
       
    85 		rv.iSystemError = errcode;
       
    86 		delete iProcess;
       
    87 		iProcess = NULL;
       
    88 		return rv;
       
    89 	}
       
    90 
       
    91 	// done - success
       
    92 	return rv;
       
    93 }
       
    94 
       
    95 
       
    96 /****************************************************************************************
       
    97  * 
       
    98  * PUBLIC FUNCTION: dstr_removeprocess
       
    99  * 
       
   100  ***************************************************************************************/
       
   101 TResult CSHostexecuteasync::dstr_removeprocess( int aArgs, int *aDeleteInstance )
       
   102 {
       
   103 	TResult rv = { ERR_NONE, 0, 0 };
       
   104 	TCAProcessError perr;
       
   105 	TProcessStatus pstatus;
       
   106 
       
   107 	// make sure that we still have a process
       
   108 	assert( iProcess != NULL );
       
   109 	
       
   110 	// get the status of the process to make sure we are ok
       
   111 	perr = iProcess->GetProcessStatus( &pstatus );
       
   112 	assert( perr == CAE_NONE );
       
   113 	if( pstatus == PS_STARTED ) {
       
   114 	  *aDeleteInstance = 0;
       
   115 	  rv.iStandardResult = ERR_INVALIDSTATE;
       
   116 	  return rv;
       
   117 	}
       
   118 
       
   119 	// ok - remove the process
       
   120 	delete iProcess;
       
   121 	iProcess = NULL;
       
   122 
       
   123 	// done - success
       
   124 	*aDeleteInstance = 1; 
       
   125 	return rv;
       
   126 }
       
   127 
       
   128 
       
   129 /****************************************************************************************
       
   130  * 
       
   131  * PUBLIC FUNCTION: killprocess
       
   132  * 
       
   133  ***************************************************************************************/
       
   134 TResult CSHostexecuteasync::killprocess( int aArgs )
       
   135 {
       
   136 	return StopProcessWithSignal( SIGKILL );
       
   137 }
       
   138 
       
   139 
       
   140 /****************************************************************************************
       
   141  * 
       
   142  * PUBLIC FUNCTION: stopprocess
       
   143  * 
       
   144  ***************************************************************************************/
       
   145 TResult CSHostexecuteasync::stopprocess( int aArgs )
       
   146 {
       
   147 	return StopProcessWithSignal( SIGTERM );
       
   148 }
       
   149 
       
   150 
       
   151 /****************************************************************************************
       
   152  * 
       
   153  * PUBLIC FUNCTION: getprocessinfo
       
   154  * 
       
   155  ***************************************************************************************/
       
   156 THostExecuteAsyncProcessInfo CSHostexecuteasync::getprocessinfo( int aArgs )
       
   157 {
       
   158 	string command_line;
       
   159 	THostExecuteAsyncProcessInfo rv;
       
   160 	TCAProcessError perr;
       
   161 	TProcessStatus pstatus = PS_INVALID;
       
   162 	TProcessExitReason preason = ER_INVALID;
       
   163 	int exitcode = 0;
       
   164 
       
   165 	// check the state
       
   166 	assert( iProcess != NULL );
       
   167 
       
   168 	// interrogate the process	
       
   169 	perr = iProcess->GetProcessStatus( &pstatus );
       
   170 	assert( perr == CAE_NONE );
       
   171 	if( (pstatus == PS_STOPPED) || (pstatus == PS_ABANDONNED) ) {
       
   172 		perr = iProcess->GetExitReason( &preason );
       
   173 		assert( perr == CAE_NONE );
       
   174 	}
       
   175 	if( (pstatus == PS_STOPPED) || (pstatus == PS_ABANDONNED) ) {
       
   176 		perr = iProcess->GetExitCode( &exitcode );
       
   177 		assert( perr == CAE_NONE );
       
   178 	}
       
   179 	command_line = iProcess->GetCommandString();
       
   180 
       
   181 	// copy into the rv
       
   182 	rv.iErrorCode = ERR_NONE;
       
   183 	rv.iErrorDetail = 0;
       
   184 	rv.iProcessStatus = (int)pstatus;
       
   185 	rv.iProcessExitReason = (int)preason;
       
   186 	rv.iExitCode = exitcode;
       
   187 	STRNCPY_NULL_TERMINATE( rv.iCommandLine, command_line.c_str(), MAXCOMMANDLINE );
       
   188 
       
   189 	// done - success
       
   190 	return rv;
       
   191 }
       
   192 
       
   193 
       
   194 /****************************************************************************************
       
   195  * 
       
   196  * PUBLIC FUNCTION: getstandardoutput
       
   197  * 
       
   198  ***************************************************************************************/
       
   199 TVarData CSHostexecuteasync::getstandardoutput( int aArgs )
       
   200 {
       
   201 	TVarData rv;
       
   202 	TCAProcessError perr;
       
   203 	int output_read_count, error_read_count, read_error;
       
   204 	string output;
       
   205 
       
   206 	// since we create in the constructor and stop in the destructor there must be a process
       
   207 	assert( iProcess != NULL );
       
   208 
       
   209 	// poll for new output - ignore errors since there may be some buffered data anyway
       
   210 	perr = iProcess->PollProcessForNewOutput( 0, &output_read_count, &error_read_count );
       
   211 
       
   212 	// now read it into a string
       
   213 	output_read_count = iProcess->GetRecordedStandardOutput( &output, &read_error );
       
   214 	if( output_read_count == 0 ) {
       
   215 	  rv.TVarData_val = NULL;
       
   216 	  rv.TVarData_len = 0;
       
   217 	  return rv;
       
   218 	}
       
   219 
       
   220 	// copy it into a buffer
       
   221 	rv.TVarData_val = (char*)malloc( output.size() + 1 );
       
   222 	rv.TVarData_len = output.size() + 1;
       
   223 	memcpy( rv.TVarData_val, output.c_str(), output.size() );
       
   224 	rv.TVarData_val[output.size()] = 0;
       
   225 	return rv;
       
   226 }
       
   227 
       
   228 
       
   229 /****************************************************************************************
       
   230  * 
       
   231  * PUBLIC FUNCTION: getstandarderror
       
   232  * 
       
   233  ***************************************************************************************/
       
   234 TVarData CSHostexecuteasync::getstandarderror( int aArgs )
       
   235 {
       
   236 	TVarData rv;
       
   237 	TCAProcessError perr;
       
   238 	int output_read_count, error_read_count, read_error;
       
   239 	string stderror;
       
   240 
       
   241 	// since we create in the constructor and stop in the destructor there must be a process
       
   242 	assert( iProcess != NULL );
       
   243 
       
   244 	// poll for new output - ignore errors since there may be some buffered data anyway
       
   245 	perr = iProcess->PollProcessForNewOutput( 0, &output_read_count, &error_read_count );
       
   246 
       
   247 	// now read it into a string
       
   248 	error_read_count = iProcess->GetRecordedStandardError( &stderror, &read_error );
       
   249 	if( error_read_count == 0 ) {
       
   250 	  rv.TVarData_len = 0;
       
   251 	  rv.TVarData_val = NULL;
       
   252 	  return rv;
       
   253 	}
       
   254 
       
   255 	// copy it into a buffer
       
   256 	rv.TVarData_val = (char*)malloc( stderror.size() + 1 );
       
   257 	rv.TVarData_len = stderror.size() + 1;
       
   258 	memcpy( rv.TVarData_val, stderror.c_str(), stderror.size() );
       
   259 	rv.TVarData_val[stderror.size()] = 0;
       
   260 	return rv;
       
   261 }
       
   262 
       
   263 
       
   264 /****************************************************************************************
       
   265  * 
       
   266  * PRIVATE FUNCTION: StopProcessWithSignal
       
   267  * 
       
   268  ***************************************************************************************/
       
   269 TResult CSHostexecuteasync::StopProcessWithSignal( int aSignal )
       
   270 {
       
   271 	TResult rv = { ERR_NONE, 0, 0 };
       
   272 	TCAProcessError perr;
       
   273 	TProcessStatus pstatus;
       
   274 
       
   275 	// since we create in the constructor and stop in the destructor there must be a process
       
   276 	assert( iProcess != NULL );
       
   277 
       
   278 	// if the process isn't running then it has died outside the scope of this controller, 
       
   279 	// clean up the state, return an error since this is noteworthy and should be either 
       
   280 	// expected or not happen.
       
   281 	perr = iProcess->GetProcessStatus( &pstatus );
       
   282 	assert( perr == CAE_NONE );
       
   283 	if( pstatus != PS_STARTED ) {
       
   284 	  assert( (pstatus == PS_STOPPED) || (pstatus == PS_ABANDONNED) );
       
   285 	  rv.iStandardResult = ERR_PROCESS_TERMINATED_OUTSIDE_SCOPE;
       
   286 	  return rv;
       
   287 	}
       
   288 
       
   289 	// request the process to stop
       
   290 	perr = iProcess->RequestStop( aSignal );
       
   291 	if( perr != CAE_NONE ) {
       
   292 	  rv.iStandardResult = ERR_STOP_PROCESS_ERROR;
       
   293 	  rv.iExtendedCode = (int)perr;
       
   294 	  return rv;
       
   295 	}
       
   296 
       
   297 	// wait for the process to stop
       
   298 	perr = iProcess->WaitForProcessToTerminate( -1 );
       
   299 	if( perr != CAE_NONE ) {
       
   300 	  rv.iStandardResult = ERR_WAIT_PROCESS_ERROR;
       
   301 	  rv.iExtendedCode = (int)perr;
       
   302 	  return rv;
       
   303 	}
       
   304 
       
   305 	// done - success
       
   306 	return rv;
       
   307 }