sbsv2/raptor/util/talon/talon.c
changeset 13 c327db0664bb
parent 2 39c28ec933dd
equal deleted inserted replaced
12:5e7562f67577 13:c327db0664bb
     1 /*
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    14 * Description: 
    14 * Description: 
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 
    18 
       
    19 #ifdef HAS_WINSOCK2
       
    20 #include <winsock2.h>
       
    21 #include <ws2tcpip.h>
       
    22 #define WIN32_LEAN_AND_MEAN
       
    23 #endif
    19 
    24 
    20 
    25 
    21 #include <stdlib.h>
    26 #include <stdlib.h>
    22 #include <unistd.h>
    27 #include <unistd.h>
    23 #include <string.h>
    28 #include <string.h>
    31 #include "talon_process.h"
    36 #include "talon_process.h"
    32 #include "sema.h"
    37 #include "sema.h"
    33 #include "buffer.h"
    38 #include "buffer.h"
    34 #include "../config.h"
    39 #include "../config.h"
    35 
    40 
       
    41 #ifdef HAS_GETCOMMANDLINE
       
    42 #include "chomp.h"
       
    43 #endif
       
    44 
    36 /* The output semaphore. */
    45 /* The output semaphore. */
    37 sbs_semaphore talon_sem;
    46 sbs_semaphore talon_sem;
    38 
    47 
    39 #define TALON_ATTEMPT_STRMAX 32
    48 #define TALON_ATTEMPT_STRMAX 32
    40 #define RECIPETAG_STRMAX 2048
    49 #define RECIPETAG_STRMAX 2048
    41 #define STATUS_STRMAX 100
    50 #define STATUS_STRMAX 120
    42 
    51 
    43 #define TALONDELIMITER '|'
    52 #define TALONDELIMITER '|'
    44 #define VARNAMEMAX 100
    53 #define VARNAMEMAX 100
    45 #define VARVALMAX 1024
    54 #define VARVALMAX 1024
    46 
    55 
       
    56 #define HOSTNAME_MAX 100
       
    57 
    47 
    58 
    48 #include "log.h"
    59 #include "log.h"
    49 
    60 
    50 #ifdef HAS_MSVCRT
    61 #ifdef HAS_MSVCRT
    51 /* Make all output handling binary */
    62 /* Make all output handling binary */
    52 unsigned int _CRT_fmode = _O_BINARY;
    63 unsigned int _CRT_fmode = _O_BINARY;
    53 #endif
    64 #endif
       
    65 
    54 
    66 
    55 double getseconds(void)
    67 double getseconds(void)
    56 {
    68 {
    57 	struct timeval tp;
    69 	struct timeval tp;
    58 	gettimeofday(&tp, NULL);
    70 	gettimeofday(&tp, NULL);
   123 			do {
   135 			do {
   124 				envvarname[e++] = *att;
   136 				envvarname[e++] = *att;
   125 				att++;
   137 				att++;
   126 			} while ( e < (VARNAMEMAX-1) && (isalnum(*att) || *att == '_'));
   138 			} while ( e < (VARNAMEMAX-1) && (isalnum(*att) || *att == '_'));
   127 			envvarname[e] = '\0';
   139 			envvarname[e] = '\0';
   128 /* DEBUG(("envvarname: %s\n", envvarname)); */
   140 			/* DEBUG(("envvarname: %s\n", envvarname));*/ 
   129 			v = talon_getenv(envvarname);
   141 			v = talon_getenv(envvarname);
   130 			if (v)
   142 			if (v)
   131 			{
   143 			{
   132 				/* DEBUG(("     value: %s\n", v)); */
   144 				/* DEBUG(("     value: %s\n", v)); */
   133 				char *oldv=v;
   145 				char *oldv=v;
   227 	/* find the argument to -c then strip the talon related front section */
   239 	/* find the argument to -c then strip the talon related front section */
   228 
   240 
   229 	char *recipe = NULL;
   241 	char *recipe = NULL;
   230 	int talon_returncode = 0;
   242 	int talon_returncode = 0;
   231 
   243 
       
   244 #ifdef HAS_WINSOCK2
       
   245 	WSADATA wsaData;
       
   246 
       
   247 	WSAStartup(MAKEWORD(2,2), &wsaData);
       
   248 
       
   249 	/* We ignore the result as we are only doing this to use gethostname
       
   250 	   and if that fails then leaving the host attribute blank is perfectly
       
   251 	   acceptable.
       
   252 	*/
       
   253 
       
   254 #endif
       
   255 
   232 #ifdef HAS_GETCOMMANDLINE
   256 #ifdef HAS_GETCOMMANDLINE
   233 	char *commandline= GetCommandLine();
   257 	char *commandline= GetCommandLine();
   234 	DEBUG(("talon: commandline: %s\n", commandline));
       
   235 	/*
   258 	/*
   236 	 * The command line should be either,
   259 	 * The command line should be either,
   237 	 * talon -c "some shell commands"
   260 	 * talon -c "some shell commands"
   238 	 * or
   261 	 * or
   239 	 * talon shell_script_file
   262 	 * talon shell_script_file
   240 	 *
   263 	 *
   241 	 * talon could be an absolute path and may have a .exe extension.
   264 	 * talon could be an absolute path and may have a .exe extension.
   242 	 */
   265 	 */
   243 	recipe = strstr(commandline, "-c");
   266 
       
   267 	
       
   268 	recipe = chompCommand(commandline);
   244 	if (recipe)
   269 	if (recipe)
   245 	{
   270 	{
   246 		/* there was a -c so extract the quoted commands */
   271 		/* there was a -c so extract the quoted commands */
   247 
   272 
   248 		while (*recipe != '"' && *recipe != '\0')
       
   249 			recipe++;
       
   250 
       
   251 		if (*recipe != '"')    /* we found -c but no following quote */
       
   252 		{
       
   253 			error("talon: error: unquoted recipe in shell call '%s'\n", commandline);
       
   254 			return 1;
       
   255 		}
       
   256 		recipe++;
       
   257 		
       
   258 		int recipelen = strlen(recipe);
   273 		int recipelen = strlen(recipe);
   259 		if (recipelen > 0 && recipe[recipelen - 1] == '"')
   274 		if (recipelen > 0 && recipe[recipelen - 1] == '"')
   260 			recipe[recipelen - 1] = '\0'; /* remove trailing quote */
   275 			recipe[recipelen - 1] = '\0'; /* remove trailing quote */
   261 	}
   276 	}
   262 	else
   277 	else
   334 		loglevel=LOGDEBUG;
   349 		loglevel=LOGDEBUG;
   335 		free(debugstr); debugstr=NULL;
   350 		free(debugstr); debugstr=NULL;
   336 	}
   351 	}
   337 
   352 
   338 	DEBUG(("talon: recipe: %s\n", recipe));
   353 	DEBUG(("talon: recipe: %s\n", recipe));
       
   354 
       
   355 	/* Make sure that the agent's hostname can be put into the host attribute */
       
   356 	char hostname[HOSTNAME_MAX];
       
   357 	int hostresult=0;
       
   358 	
       
   359 	hostresult = gethostname(hostname, HOSTNAME_MAX-1);
       
   360 	if (0 != hostresult)
       
   361 	{
       
   362 		DEBUG(("talon: failed to get hostname: %d\n", hostresult));
       
   363 		hostname[0] = '\0';
       
   364 	}
       
   365 
       
   366 	talon_setenv("HOSTNAME", hostname);
       
   367 	DEBUG(("talon: setenv: hostname: %s\n", hostname));
   339 
   368 
   340 	
   369 	
   341 	char varname[VARNAMEMAX];
   370 	char varname[VARNAMEMAX];
   342 	char varval[VARVALMAX];
   371 	char varval[VARVALMAX];
   343 	int dotagging = 0; 
   372 	int dotagging = 0; 
   547 
   576 
   548 			talon_returncode = p->returncode;
   577 			talon_returncode = p->returncode;
   549 
   578 
   550 			if (dotagging) 
   579 			if (dotagging) 
   551 			{
   580 			{
   552 				char *forcesuccessstr = force_success == 0 ? "" : " forcesuccess='FORCESUCCESS'";
   581 				char *flagsstr = force_success == 0 ? "" : " flags='FORCESUCCESS'";
       
   582 				char *reasonstr = "" ;
       
   583 
       
   584 				if (p->causeofdeath == PROC_TIMEOUTDEATH)
       
   585 					reasonstr = " reason='timeout'";
   553 
   586 
   554 				if (p->returncode != 0)
   587 				if (p->returncode != 0)
   555 				{
   588 				{
   556 					char *exitstr = retries > 0 ? "retry" : "failed";
   589 					char *exitstr = (force_success || retries <= 0) ? "failed" : "retry";
   557 					snprintf(status, STATUS_STRMAX - 1, "\n<status exit='%s' code='%d' attempt='%d'%s />", exitstr, p->returncode, attempt, forcesuccessstr );
   590 					snprintf(status, STATUS_STRMAX - 1, "\n<status exit='%s' code='%d' attempt='%d'%s%s />", exitstr, p->returncode, attempt, flagsstr, reasonstr );
   558 				} else {
   591 				} else {
   559 					snprintf(status, STATUS_STRMAX - 1, "\n<status exit='ok' attempt='%d'%s />", attempt, forcesuccessstr );
   592 					snprintf(status, STATUS_STRMAX - 1, "\n<status exit='ok' attempt='%d'%s%s />", attempt, flagsstr, reasonstr );
   560 				}
   593 				}
   561 				status[STATUS_STRMAX-1] = '\0';
   594 				status[STATUS_STRMAX-1] = '\0';
   562 	
   595 	
   563 				snprintf(timestat, STATUS_STRMAX - 1, "<time start='%.5f' elapsed='%.3f' />",start_time, end_time-start_time );
   596 				snprintf(timestat, STATUS_STRMAX - 1, "<time start='%.5f' elapsed='%.3f' />",start_time, end_time-start_time );
   564 				timestat[STATUS_STRMAX-1] = '\0';
   597 				timestat[STATUS_STRMAX-1] = '\0';