securityanddataprivacytools/securitytools/certapp/api/certapp-api.c
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2008 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 the License "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 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <stdio.h>
       
    20 #include <stdlib.h>
       
    21 #include <string.h>
       
    22 #include <errno.h>
       
    23 
       
    24 #include "certapp-api.h"
       
    25 
       
    26 #define CERTAPP "certapp"
       
    27 typedef const char *ArgPtr;
       
    28 
       
    29 #ifdef __LINUX__
       
    30 #include <unistd.h>
       
    31 #include <sys/wait.h>
       
    32 #else
       
    33 #include <process.h>
       
    34 #endif
       
    35 
       
    36 #ifndef BULLSEYE_OFF
       
    37 #ifdef _BullseyeCoverage
       
    38 #define BULLSEYE_OFF "BullseyeCoverage save off";
       
    39 #define BULLSEYE_RESTORE "BullseyeCoverage restore";
       
    40 #else
       
    41 #define BULLSEYE_OFF 
       
    42 #define BULLSEYE_RESTORE 
       
    43 #endif
       
    44 #endif
       
    45 
       
    46 static const char * const OPT_PROGRESS = "--progress=";
       
    47 static const char * const OPT_ERRORS = "--errors=";
       
    48 
       
    49 int RunCertApp(const char *aProgress, const char *aErrors,
       
    50 			   int argc, char **argv)
       
    51 {
       
    52 	int ret = -1;
       
    53 BULLSEYE_OFF
       
    54 	if(argc<0) abort(); // Bad argument
       
    55 BULLSEYE_RESTORE
       
    56 
       
    57 	int newArgc = argc+4;
       
    58 	const char **newArgv = (const char **)malloc(sizeof(ArgPtr)*newArgc);
       
    59 
       
    60 	const char *progFile = (aProgress) ? (aProgress) : ("-");
       
    61 	const char *errorsFile = (aErrors) ? (aErrors) : ("-");
       
    62 
       
    63 	char *progress=(char *)malloc(strlen(OPT_PROGRESS)+strlen(progFile)+1);
       
    64 	strcpy(progress, OPT_PROGRESS);
       
    65 	strcat(progress, progFile);
       
    66 
       
    67 	char *errors=(char *)malloc(strlen(OPT_ERRORS)+strlen(errorsFile)+1);
       
    68 	strcpy(errors, OPT_ERRORS);
       
    69 	strcat(errors, errorsFile);
       
    70 	
       
    71 	newArgv[0] = CERTAPP;
       
    72 	newArgv[1] = progress;
       
    73 	newArgv[2] = errors;
       
    74 	int i=0;
       
    75 	for(i=0; i<argc; ++i)
       
    76 		{
       
    77 		newArgv[i+3] = argv[i];
       
    78 		}
       
    79 	newArgv[newArgc-1] = 0; // Terminate newArgv array
       
    80 
       
    81 #ifdef __LINUX__
       
    82 	//
       
    83 	// Linux version to run certapp
       
    84 	//
       
    85 	pid_t pid = vfork();
       
    86 	if(pid == -1)
       
    87 		{
       
    88 		// vfork call failed
       
    89 		printf("Failed to run %s\n", CERTAPP);
       
    90 		return errno;
       
    91 		}
       
    92 	if(pid == 0)
       
    93 		{
       
    94 		// Child side of vfork
       
    95 		// Exec certapp
       
    96 		
       
    97 		execvp(CERTAPP, (char * const *)newArgv);
       
    98 		// Only get here if the exec call failed...
       
    99 		switch(errno)
       
   100 			{
       
   101 			case ENOENT:
       
   102 				fprintf(stderr, "*** Could not find certapp executable to launch!\n");
       
   103 				break;
       
   104 
       
   105 			default:
       
   106 				fprintf(stderr, "*** Failed to launch certapp, execvp called failed with error code%d !\n", errno);
       
   107 				break;
       
   108 			}
       
   109 		exit(errno); 
       
   110 		}
       
   111 
       
   112 	// Parent side of vfork
       
   113 	
       
   114 	// Wait for certapp to finish
       
   115 	for(;;)
       
   116 		{
       
   117 		// Block until our child to exits
       
   118 		int waitStatus = waitpid(pid, &ret, 0);
       
   119 		if(waitStatus == pid)
       
   120 			{
       
   121 			// Our child process exited and ret contains its status
       
   122 			break; // Done
       
   123 			}
       
   124 		if(waitStatus != -1)
       
   125 			{
       
   126 			// Should never happen
       
   127 			break;
       
   128 			}
       
   129 		// Decode errno
       
   130 		if(errno == EINTR)
       
   131 			{
       
   132 			// Signal handler interrupted us - re-issue waitpid call
       
   133 			continue;
       
   134 			}
       
   135 		// Error
       
   136 		ret = errno;
       
   137 		break;
       
   138 		};
       
   139 #else
       
   140 	//
       
   141 	// Windows version to run certapp
       
   142 	//
       
   143 	ret = _spawnvp(_P_WAIT, CERTAPP, newArgv);
       
   144 	
       
   145 #endif
       
   146 
       
   147 	free(progress);
       
   148 	free(errors);
       
   149 	free(newArgv);
       
   150 
       
   151 	return ret;
       
   152 }
       
   153 
       
   154 // End of file
       
   155