author | timothy.murphy@nokia.com |
Mon, 01 Mar 2010 16:23:33 +0000 | |
branch | fix |
changeset 289 | 91bbfbf8c43e |
parent 287 | 4c84bb0f4c67 |
child 353 | bb0bded637e9 |
permissions | -rw-r--r-- |
3 | 1 |
/* |
240
fbc6a8ad3957
SF Bug 1861 - update log schema, fix some copyright notices
timothy.murphy@nokia.com
parents:
237
diff
changeset
|
2 |
* Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies). |
3 | 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 |
||
282
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
19 |
#ifdef HAS_WINSOCK2 |
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
20 |
#include <winsock2.h> |
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
21 |
#include <ws2tcpip.h> |
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
22 |
#define WIN32_LEAN_AND_MEAN |
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
23 |
#endif |
3 | 24 |
|
25 |
||
26 |
#include <stdlib.h> |
|
27 |
#include <unistd.h> |
|
28 |
#include <string.h> |
|
29 |
#include <stdio.h> |
|
30 |
||
31 |
#include <sys/types.h> |
|
32 |
#include <sys/stat.h> |
|
33 |
#include <fcntl.h> |
|
34 |
#include <stdarg.h> |
|
35 |
||
36 |
#include "talon_process.h" |
|
37 |
#include "sema.h" |
|
38 |
#include "buffer.h" |
|
39 |
#include "../config.h" |
|
40 |
||
252
6846d05399b6
SF Bug 2000 - test code for commandline parsing.
raptorbot <raptorbot@systemstesthead.symbian.intra>
parents:
251
diff
changeset
|
41 |
#ifdef HAS_GETCOMMANDLINE |
6846d05399b6
SF Bug 2000 - test code for commandline parsing.
raptorbot <raptorbot@systemstesthead.symbian.intra>
parents:
251
diff
changeset
|
42 |
#include "chomp.h" |
6846d05399b6
SF Bug 2000 - test code for commandline parsing.
raptorbot <raptorbot@systemstesthead.symbian.intra>
parents:
251
diff
changeset
|
43 |
#endif |
6846d05399b6
SF Bug 2000 - test code for commandline parsing.
raptorbot <raptorbot@systemstesthead.symbian.intra>
parents:
251
diff
changeset
|
44 |
|
3 | 45 |
/* The output semaphore. */ |
46 |
sbs_semaphore talon_sem; |
|
47 |
||
48 |
#define TALON_ATTEMPT_STRMAX 32 |
|
49 |
#define RECIPETAG_STRMAX 2048 |
|
237
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
50 |
#define STATUS_STRMAX 120 |
3 | 51 |
|
52 |
#define TALONDELIMITER '|' |
|
53 |
#define VARNAMEMAX 100 |
|
54 |
#define VARVALMAX 1024 |
|
55 |
||
282
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
56 |
#define HOSTNAME_MAX 100 |
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
57 |
|
3 | 58 |
|
59 |
#include "log.h" |
|
60 |
||
61 |
#ifdef HAS_MSVCRT |
|
62 |
/* Make all output handling binary */ |
|
63 |
unsigned int _CRT_fmode = _O_BINARY; |
|
64 |
#endif |
|
65 |
||
282
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
66 |
|
3 | 67 |
double getseconds(void) |
68 |
{ |
|
69 |
struct timeval tp; |
|
70 |
gettimeofday(&tp, NULL); |
|
71 |
||
72 |
return (double)tp.tv_sec + ((double)tp.tv_usec)/1000000.0L; |
|
73 |
} |
|
74 |
||
75 |
void talon_setenv(char name[], char val[]) |
|
76 |
{ |
|
77 |
#if defined(HAS_GETENVIRONMENTVARIABLE) |
|
78 |
SetEnvironmentVariableA(name,val); |
|
79 |
#elif defined(HAS_GETENV) |
|
80 |
setenv(name,val, 1); |
|
81 |
#else |
|
82 |
# error "Need a function for setting environment variables" |
|
83 |
#endif |
|
84 |
} |
|
85 |
||
86 |
||
87 |
#define TALON_MAXENV 4096 |
|
88 |
char * talon_getenv(char name[]) |
|
89 |
{ |
|
90 |
#if defined(HAS_SETENV) |
|
91 |
char *val = getenv(name); |
|
92 |
char *dest = NULL; |
|
93 |
||
94 |
if (val) |
|
95 |
{ |
|
96 |
dest = malloc(strlen(val) + 1); |
|
97 |
if (dest) |
|
98 |
{ |
|
99 |
strcpy(dest,val); |
|
100 |
} |
|
101 |
} |
|
102 |
return dest; |
|
103 |
#elif defined(HAS_SETENVIRONMENTVARIABLE) |
|
104 |
char *val = malloc(TALON_MAXENV); |
|
105 |
if (0 != GetEnvironmentVariableA(name,val,TALON_MAXENV-1)) |
|
106 |
return val; |
|
107 |
else |
|
108 |
return NULL; |
|
109 |
#else |
|
110 |
# error "Need a function for setting environment variables" |
|
111 |
#endif |
|
112 |
} |
|
113 |
||
114 |
void prependattributes(buffer *b, char *attributes) |
|
115 |
{ |
|
116 |
char recipetag[RECIPETAG_STRMAX]; |
|
117 |
char *rt; |
|
118 |
char envvarname[VARNAMEMAX]; |
|
119 |
char *att; |
|
120 |
||
121 |
||
122 |
strcpy(recipetag, "<recipe "); |
|
123 |
rt = recipetag + 8; |
|
124 |
||
125 |
att = attributes; |
|
126 |
while (*att != '\0' && rt < &recipetag[RECIPETAG_STRMAX-1]) |
|
127 |
{ |
|
128 |
if ( *att == '$' ) |
|
129 |
{ |
|
130 |
int e; |
|
131 |
char *v; |
|
132 |
/* insert the value of an environent variable */ |
|
133 |
att++; |
|
134 |
e = 0; |
|
135 |
do { |
|
136 |
envvarname[e++] = *att; |
|
137 |
att++; |
|
138 |
} while ( e < (VARNAMEMAX-1) && (isalnum(*att) || *att == '_')); |
|
139 |
envvarname[e] = '\0'; |
|
283
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
140 |
/* DEBUG(("envvarname: %s\n", envvarname));*/ |
3 | 141 |
v = talon_getenv(envvarname); |
142 |
if (v) |
|
143 |
{ |
|
144 |
/* DEBUG((" value: %s\n", v)); */ |
|
145 |
char *oldv=v; |
|
146 |
while (*v != '\0' && rt < &recipetag[RECIPETAG_STRMAX-1]) |
|
147 |
{ |
|
148 |
*rt = *v; |
|
149 |
rt++; v++; |
|
150 |
} |
|
151 |
free(oldv); |
|
152 |
} |
|
153 |
} else { |
|
154 |
*rt = *att; |
|
155 |
rt++; att++; |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
char *finish = ">\n<![CDATA[\n"; |
|
160 |
||
161 |
while (*finish != '\0' && rt < &recipetag[RECIPETAG_STRMAX-1]) |
|
162 |
{ |
|
163 |
*rt = *finish; |
|
164 |
*rt++; finish++; |
|
165 |
} |
|
166 |
||
167 |
*rt = '\0'; |
|
168 |
||
169 |
buffer_prepend(b, recipetag, strlen(recipetag)); |
|
170 |
} |
|
171 |
||
172 |
/* read a recipe string from a temporary file. |
|
173 |
* |
|
174 |
* We only expect to do this for very long command lines on |
|
175 |
* Windows. So allocate the maximum size for CreateProcess on |
|
176 |
* Win32 (32768 bytes) and error if the file is longer than that. |
|
177 |
* |
|
178 |
*/ |
|
179 |
char *read_recipe_from_file(const char *filename) |
|
180 |
{ |
|
181 |
FILE *fp = fopen(filename, "r"); |
|
182 |
||
183 |
if (!fp) |
|
184 |
{ |
|
185 |
error("talon: error: could not read '%s'\n", filename); |
|
186 |
return NULL; |
|
187 |
} |
|
188 |
||
189 |
int max_length = 32768; |
|
190 |
char *recipe = (char*)malloc(max_length); |
|
191 |
||
192 |
if (!recipe) |
|
193 |
{ |
|
194 |
error("talon: error: could not allocate memory to read '%s'\n", filename); |
|
195 |
return NULL; |
|
196 |
} |
|
197 |
int position = 0; |
|
198 |
||
199 |
/* read the file one character at a time */ |
|
200 |
int c; |
|
201 |
while ((c = getc(fp)) != EOF) |
|
202 |
{ |
|
203 |
switch (c) |
|
204 |
{ |
|
205 |
case '\r': |
|
206 |
case '\n': |
|
207 |
/* ignore newlines */ |
|
208 |
break; |
|
209 |
||
210 |
case '"': |
|
211 |
/* double quotes have to be escaped so they aren't lost later */ |
|
212 |
if (position < max_length) |
|
213 |
recipe[position++] = '\\'; |
|
214 |
if (position < max_length) |
|
215 |
recipe[position++] = c; |
|
216 |
break; |
|
217 |
||
218 |
default: |
|
219 |
if (position < max_length) |
|
220 |
recipe[position++] = c; |
|
221 |
break; |
|
222 |
} |
|
223 |
} |
|
224 |
fclose(fp); |
|
225 |
||
226 |
/* add a terminating \0 */ |
|
227 |
if (position < max_length) |
|
228 |
recipe[position] = '\0'; |
|
229 |
else |
|
230 |
{ |
|
231 |
error("talon: error: command longer than 32768 in '%s'\n", filename); |
|
232 |
return NULL; |
|
233 |
} |
|
234 |
return recipe; |
|
235 |
} |
|
236 |
||
237 |
int main(int argc, char *argv[]) |
|
238 |
{ |
|
239 |
/* find the argument to -c then strip the talon related front section */ |
|
240 |
||
241 |
char *recipe = NULL; |
|
242 |
int talon_returncode = 0; |
|
243 |
||
282
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
244 |
#ifdef HAS_WINSOCK2 |
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
245 |
WSADATA wsaData; |
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
246 |
|
287
4c84bb0f4c67
spelling mistake. Remove unused variable
timothy.murphy@nokia.com
parents:
285
diff
changeset
|
247 |
WSAStartup(MAKEWORD(2,2), &wsaData); |
282
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
248 |
|
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
249 |
/* We ignore the result as we are only doing this to use gethostname |
287
4c84bb0f4c67
spelling mistake. Remove unused variable
timothy.murphy@nokia.com
parents:
285
diff
changeset
|
250 |
and if that fails then leaving the host attribute blank is perfectly |
282
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
251 |
acceptable. |
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
252 |
*/ |
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
253 |
|
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
254 |
#endif |
99ac65572b14
fix: use gethostname() to set host attribute as this works on a cluster whereas reading the COMPUTERNAME env var doesn't.
timothy.murphy@nokia.com
parents:
255
diff
changeset
|
255 |
|
3 | 256 |
#ifdef HAS_GETCOMMANDLINE |
257 |
char *commandline= GetCommandLine(); |
|
258 |
/* |
|
259 |
* The command line should be either, |
|
260 |
* talon -c "some shell commands" |
|
261 |
* or |
|
262 |
* talon shell_script_file |
|
263 |
* |
|
264 |
* talon could be an absolute path and may have a .exe extension. |
|
265 |
*/ |
|
251
64208ed747d4
SF Bug 2000 - Talon fails when installed in a path containing the string '-c' (windows only)
raptorbot <raptorbot@systemstesthead.symbian.intra>
parents:
237
diff
changeset
|
266 |
|
64208ed747d4
SF Bug 2000 - Talon fails when installed in a path containing the string '-c' (windows only)
raptorbot <raptorbot@systemstesthead.symbian.intra>
parents:
237
diff
changeset
|
267 |
|
64208ed747d4
SF Bug 2000 - Talon fails when installed in a path containing the string '-c' (windows only)
raptorbot <raptorbot@systemstesthead.symbian.intra>
parents:
237
diff
changeset
|
268 |
recipe = chompCommand(commandline); |
3 | 269 |
if (recipe) |
270 |
{ |
|
271 |
/* there was a -c so extract the quoted commands */ |
|
272 |
||
273 |
int recipelen = strlen(recipe); |
|
274 |
if (recipelen > 0 && recipe[recipelen - 1] == '"') |
|
275 |
recipe[recipelen - 1] = '\0'; /* remove trailing quote */ |
|
276 |
} |
|
277 |
else |
|
278 |
{ |
|
279 |
/* there was no -c so extract the argument as a filename */ |
|
280 |
||
281 |
recipe = strstr(commandline, "talon"); |
|
282 |
if (recipe) |
|
283 |
{ |
|
284 |
/* find the first space */ |
|
285 |
while (!isspace(*recipe) && *recipe != '\0') |
|
286 |
recipe++; |
|
287 |
/* skip past the spaces */ |
|
288 |
while (isspace(*recipe)) |
|
289 |
recipe++; |
|
290 |
||
291 |
recipe = read_recipe_from_file(recipe); |
|
292 |
||
293 |
if (!recipe) |
|
294 |
{ |
|
295 |
error("talon: error: bad script file in shell call '%s'\n", commandline); |
|
296 |
return 1; |
|
297 |
} |
|
298 |
} |
|
299 |
else |
|
300 |
{ |
|
301 |
error("talon: error: no 'talon' in shell call '%s'\n", commandline); |
|
302 |
return 1; |
|
303 |
} |
|
304 |
} |
|
305 |
#else |
|
306 |
/* |
|
307 |
* The command line should be either, |
|
308 |
* talon -c "some shell commands" |
|
309 |
* or |
|
310 |
* talon shell_script_file |
|
311 |
* |
|
312 |
* talon could be an absolute path and may have a .exe extension. |
|
313 |
*/ |
|
314 |
switch (argc) |
|
315 |
{ |
|
316 |
case 2: |
|
317 |
recipe = read_recipe_from_file(argv[1]); |
|
318 |
break; |
|
319 |
case 3: |
|
320 |
if (strcmp("-c", argv[1]) != 0) |
|
321 |
{ |
|
322 |
error("talon: error: %s\n", "usage is 'talon -c command' or 'talon script_filename'"); |
|
323 |
return 1; |
|
324 |
} |
|
325 |
recipe = argv[2]; |
|
326 |
break; |
|
327 |
default: |
|
328 |
error("talon: error: %s\n", "usage is 'talon -c command' or 'talon script_filename'"); |
|
329 |
return 1; |
|
330 |
} |
|
331 |
#endif |
|
332 |
||
333 |
/* did we get a recipe at all? */ |
|
334 |
if (!recipe) |
|
335 |
{ |
|
336 |
error("talon: error: %s", "no recipe supplied to the shell.\n"); |
|
337 |
return 1; |
|
338 |
} |
|
339 |
||
340 |
/* remove any leading white space on the recipe */ |
|
341 |
while (isspace(*recipe)) |
|
342 |
recipe++; |
|
343 |
||
344 |
/* turn debugging on? */ |
|
345 |
char *debugstr=talon_getenv("TALON_DEBUG"); |
|
346 |
||
347 |
if (debugstr) |
|
348 |
{ |
|
349 |
loglevel=LOGDEBUG; |
|
350 |
free(debugstr); debugstr=NULL; |
|
351 |
} |
|
352 |
||
353 |
DEBUG(("talon: recipe: %s\n", recipe)); |
|
354 |
||
283
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
355 |
/* Make sure that the agent's hostname can be put into the host attribute */ |
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
356 |
char hostname[HOSTNAME_MAX]; |
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
357 |
int hostresult=0; |
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
358 |
|
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
359 |
hostresult = gethostname(hostname, HOSTNAME_MAX-1); |
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
360 |
if (0 != hostresult) |
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
361 |
{ |
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
362 |
DEBUG(("talon: failed to get hostname: %d\n", hostresult)); |
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
363 |
hostname[0] = '\0'; |
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
364 |
} |
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
365 |
|
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
366 |
talon_setenv("HOSTNAME", hostname); |
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
367 |
DEBUG(("talon: setenv: hostname: %s\n", hostname)); |
a5284ec02d61
fix: remove unnecessary changes. Reorganise gethostname stuff so it can be debugged.
timothy.murphy@nokia.com
parents:
282
diff
changeset
|
368 |
|
3 | 369 |
|
370 |
char varname[VARNAMEMAX]; |
|
371 |
char varval[VARVALMAX]; |
|
372 |
int dotagging = 0; |
|
373 |
int force_descramble_off = 0; |
|
374 |
||
375 |
char *rp = recipe; |
|
376 |
if (*rp == TALONDELIMITER) { |
|
377 |
dotagging = 1; |
|
378 |
||
379 |
/* there are some talon-specific settings |
|
380 |
* in the command which must be stripped */ |
|
381 |
rp++; |
|
382 |
char *out = varname; |
|
383 |
char *stopout = varname + VARNAMEMAX - 1; |
|
384 |
DEBUG(("talon: parameters found\n")); |
|
385 |
while (*rp != '\0') |
|
386 |
{ |
|
387 |
||
388 |
switch (*rp) { |
|
389 |
case '=': |
|
390 |
*out = '\0'; |
|
391 |
DEBUG(("talon: varname: %s\n",varname)); |
|
392 |
out = varval; |
|
393 |
stopout = varval + VARVALMAX - 1; |
|
394 |
break; |
|
395 |
case ';': |
|
396 |
*out = '\0'; |
|
397 |
DEBUG(("talon: varval: %s\n",varval)); |
|
398 |
talon_setenv(varname, varval); |
|
399 |
out = varname; |
|
400 |
stopout = varname + VARNAMEMAX - 1; |
|
401 |
break; |
|
402 |
default: |
|
403 |
*out = *rp; |
|
404 |
if (out < stopout) |
|
405 |
out++; |
|
406 |
break; |
|
407 |
} |
|
408 |
||
409 |
if (*rp == TALONDELIMITER) |
|
410 |
{ |
|
411 |
rp++; |
|
412 |
break; |
|
413 |
} |
|
414 |
||
415 |
rp++; |
|
416 |
} |
|
417 |
} else { |
|
418 |
/* This is probably a $(shell) statement |
|
419 |
* in make so no descrambling needed and |
|
420 |
* tags are definitely not wanted as they |
|
421 |
* would corrupt the expected output*/ |
|
422 |
force_descramble_off = 1; |
|
423 |
} |
|
424 |
||
425 |
||
426 |
/* Now take settings from the environment (having potentially modified it) */ |
|
427 |
if (talon_getenv("TALON_DEBUG")) |
|
428 |
loglevel=LOGDEBUG; |
|
429 |
||
430 |
||
431 |
int enverrors = 0; |
|
432 |
||
433 |
char *shell = talon_getenv("TALON_SHELL"); |
|
434 |
if (!shell) |
|
435 |
{ |
|
436 |
error("error: %s", "TALON_SHELL not set in environment\n"); |
|
437 |
enverrors++; |
|
438 |
} |
|
439 |
||
440 |
int timeout = -1; |
|
441 |
char *timeout_str = talon_getenv("TALON_TIMEOUT"); |
|
442 |
if (timeout_str) |
|
443 |
{ |
|
444 |
timeout = atoi(timeout_str); |
|
445 |
free(timeout_str); timeout_str = NULL; |
|
446 |
} |
|
447 |
||
448 |
char *buildid = talon_getenv("TALON_BUILDID"); |
|
449 |
if (!buildid) |
|
450 |
{ |
|
451 |
error("error: %s", "TALON_BUILDID not set in environment\n"); |
|
452 |
enverrors++; |
|
453 |
} |
|
454 |
||
455 |
char *attributes = talon_getenv("TALON_RECIPEATTRIBUTES"); |
|
456 |
if (!attributes) |
|
457 |
{ |
|
458 |
error("error: %s", "TALON_RECIPEATTRIBUTES not set in environment\n"); |
|
459 |
enverrors++; |
|
460 |
} |
|
461 |
||
462 |
||
463 |
int max_retries = 0; |
|
464 |
char *retries_str = talon_getenv("TALON_RETRIES"); |
|
465 |
if (retries_str) |
|
466 |
{ |
|
467 |
max_retries = atoi(retries_str); |
|
468 |
free(retries_str); retries_str = NULL; |
|
469 |
} |
|
470 |
||
471 |
||
472 |
int descramble = 0; |
|
473 |
if (! force_descramble_off ) |
|
474 |
{ |
|
475 |
char *descramblestr = talon_getenv("TALON_DESCRAMBLE"); |
|
476 |
if (descramblestr) |
|
477 |
{ |
|
478 |
if (*descramblestr == '0') |
|
479 |
descramble = 0; |
|
480 |
else |
|
481 |
descramble = 1; |
|
482 |
||
483 |
free(descramblestr); descramblestr = NULL; |
|
484 |
} |
|
485 |
} |
|
486 |
||
487 |
||
488 |
||
489 |
/* Talon can look in a flags variable to alter it's behaviour */ |
|
490 |
int force_success = 0; |
|
491 |
char *flags_str = talon_getenv("TALON_FLAGS"); |
|
492 |
if (flags_str) |
|
493 |
{ |
|
494 |
int c; |
|
495 |
for (c=0; flags_str[c] !=0; c++) |
|
496 |
flags_str[c] = tolower(flags_str[c]); |
|
497 |
||
498 |
if (strstr(flags_str, "forcesuccess")) |
|
499 |
force_success = 1; |
|
500 |
||
501 |
/* don't put <recipe> or <CDATA<[[ tags around the output. e.g. if it's XML already*/ |
|
502 |
if (strstr(flags_str, "rawoutput")) |
|
503 |
{ |
|
504 |
dotagging = 0; |
|
505 |
} |
|
506 |
||
507 |
free(flags_str); flags_str = NULL; |
|
508 |
} |
|
509 |
||
510 |
/* Talon subprocesses need to have the "correct" shell variable set. */ |
|
511 |
talon_setenv("SHELL", shell); |
|
512 |
||
513 |
/* we have allowed some errors to build up so that the user |
|
514 |
* can see all of them before we stop and force the user |
|
515 |
* to fix them |
|
516 |
*/ |
|
517 |
if (enverrors) |
|
518 |
{ |
|
519 |
return 1; |
|
520 |
} |
|
521 |
||
522 |
||
523 |
/* Run the recipe repeatedly until the retry count expires or |
|
524 |
* it succeeds. |
|
525 |
*/ |
|
526 |
int attempt = 0, retries = max_retries; |
|
527 |
proc *p = NULL; |
|
528 |
||
529 |
char *args[5]; |
|
530 |
||
531 |
char *qrp=rp; |
|
532 |
||
533 |
#ifdef HAS_GETCOMMANDLINE |
|
534 |
/* re-quote the argument to -c since this helps windows deal with it */ |
|
535 |
int qrpsize = strlen(rp) + 3; |
|
536 |
qrp = malloc(qrpsize); |
|
537 |
qrp[0] = '"'; |
|
538 |
strcpy(&qrp[1], rp); |
|
539 |
qrp[qrpsize-2] = '"'; |
|
540 |
qrp[qrpsize-1] = '\0'; |
|
541 |
#endif |
|
542 |
||
543 |
int index = 0; |
|
544 |
args[index++] = shell; |
|
545 |
||
546 |
if (dotagging) /* don't do bash -x for non-tagged commands e.g. $(shell output) */ |
|
547 |
args[index++] = "-x"; |
|
548 |
||
549 |
args[index++] = "-c"; |
|
550 |
args[index++] = qrp; |
|
551 |
args[index++] = NULL; |
|
552 |
||
553 |
/* get the semaphore ready */ |
|
554 |
talon_sem.name = buildid; |
|
555 |
talon_sem.timeout = timeout; |
|
556 |
do |
|
557 |
{ |
|
558 |
char talon_attempt[TALON_ATTEMPT_STRMAX]; |
|
559 |
double start_time = getseconds(); |
|
560 |
||
561 |
attempt++; |
|
562 |
||
563 |
snprintf(talon_attempt, TALON_ATTEMPT_STRMAX-1, "%d", attempt); |
|
564 |
talon_attempt[TALON_ATTEMPT_STRMAX - 1] = '\0'; |
|
565 |
||
566 |
talon_setenv("TALON_ATTEMPT", talon_attempt); |
|
567 |
||
568 |
p = process_run(shell, args, timeout); |
|
569 |
||
570 |
double end_time = getseconds(); |
|
571 |
||
572 |
if (p) |
|
573 |
{ |
|
574 |
char status[STATUS_STRMAX]; |
|
575 |
char timestat[STATUS_STRMAX]; |
|
576 |
||
577 |
talon_returncode = p->returncode; |
|
578 |
||
579 |
if (dotagging) |
|
580 |
{ |
|
237
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
581 |
char *flagsstr = force_success == 0 ? "" : " flags='FORCESUCCESS'"; |
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
582 |
char *reasonstr = "" ; |
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
583 |
|
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
584 |
if (p->causeofdeath == PROC_TIMEOUTDEATH) |
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
585 |
reasonstr = " reason='timeout'"; |
3 | 586 |
|
587 |
if (p->returncode != 0) |
|
588 |
{ |
|
589 |
char *exitstr = retries > 0 ? "retry" : "failed"; |
|
237
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
590 |
snprintf(status, STATUS_STRMAX - 1, "\n<status exit='%s' code='%d' attempt='%d'%s%s />", exitstr, p->returncode, attempt, flagsstr, reasonstr ); |
3 | 591 |
} else { |
237
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
592 |
snprintf(status, STATUS_STRMAX - 1, "\n<status exit='ok' attempt='%d'%s%s />", attempt, flagsstr, reasonstr ); |
3 | 593 |
} |
594 |
status[STATUS_STRMAX-1] = '\0'; |
|
595 |
||
596 |
snprintf(timestat, STATUS_STRMAX - 1, "<time start='%.5f' elapsed='%.3f' />",start_time, end_time-start_time ); |
|
597 |
timestat[STATUS_STRMAX-1] = '\0'; |
|
598 |
||
599 |
prependattributes(p->output, attributes); |
|
600 |
||
601 |
if (dotagging) { |
|
602 |
} |
|
603 |
buffer_append(p->output, "\n]]>", 4); |
|
604 |
buffer_append(p->output, timestat, strlen(timestat)); |
|
605 |
buffer_append(p->output, status, strlen(status)); |
|
606 |
buffer_append(p->output, "\n</recipe>\n", 11); |
|
607 |
} |
|
608 |
||
609 |
unsigned int iterator = 0; |
|
610 |
byteblock *bb; |
|
611 |
||
612 |
if (descramble) |
|
613 |
sema_wait(&talon_sem); |
|
614 |
while ((bb = buffer_getbytes(p->output, &iterator))) |
|
615 |
{ |
|
616 |
write(STDOUT_FILENO, &bb->byte0, bb->fill); |
|
617 |
} |
|
618 |
if (descramble) |
|
619 |
sema_release(&talon_sem); |
|
620 |
||
621 |
||
622 |
if (p->returncode == 0 || force_success) |
|
623 |
{ |
|
624 |
process_free(&p); |
|
625 |
break; |
|
626 |
} |
|
627 |
||
628 |
process_free(&p); |
|
629 |
} else { |
|
630 |
error("error: failed to run shell: %s: check the SHELL environment variable.\n", args[0]); |
|
631 |
return 1; |
|
632 |
} |
|
633 |
||
634 |
retries--; |
|
635 |
} |
|
636 |
while (retries >= 0); |
|
637 |
||
638 |
if (buildid) free(buildid); buildid = NULL; |
|
639 |
if (attributes) free(attributes); attributes = NULL; |
|
640 |
if (shell) free(shell); shell = NULL; |
|
641 |
||
642 |
if (force_success) |
|
643 |
return 0; |
|
644 |
else |
|
645 |
return talon_returncode; |
|
646 |
} |