author | Jon Chatten |
Thu, 25 Feb 2010 16:14:32 +0000 | |
branch | fix |
changeset 270 | 29b7de7b6f0d |
parent 240 | fbc6a8ad3957 |
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 |
||
19 |
||
20 |
||
21 |
#include <unistd.h> |
|
22 |
||
23 |
#include "process.h" |
|
24 |
#include "buffer.h" |
|
25 |
#include <stdlib.h> |
|
26 |
#include <string.h> |
|
27 |
#include <poll.h> |
|
28 |
#include <signal.h> |
|
29 |
#include <errno.h> |
|
30 |
#include <sys/wait.h> |
|
31 |
||
32 |
#include "log.h" |
|
33 |
||
34 |
||
35 |
proc *process_new(void) |
|
36 |
{ |
|
37 |
proc *p = malloc(sizeof(proc)); |
|
38 |
p->output = buffer_new(); |
|
39 |
if (!p->output) |
|
40 |
{ |
|
41 |
free(p); |
|
42 |
return NULL; |
|
43 |
} |
|
44 |
p->starttime = 0; |
|
45 |
p->endtime = 0; |
|
46 |
p->returncode = 1; |
|
47 |
p->pid = 0; |
|
48 |
p->causeofdeath = PROC_NORMALDEATH; |
|
49 |
||
50 |
return p; |
|
51 |
} |
|
52 |
||
53 |
void childsig(int sig) |
|
54 |
{ |
|
55 |
//wait(&stat_loc); |
|
56 |
DEBUG(("SIGCHLD\n")); |
|
57 |
} |
|
58 |
||
59 |
struct sigaction child_action; |
|
60 |
||
61 |
proc *process_run(char executable[], char *args[], int timeout) |
|
62 |
{ |
|
63 |
proc *p = process_new(); |
|
64 |
||
65 |
if (p == NULL) |
|
66 |
return NULL; |
|
67 |
||
68 |
char *text; |
|
69 |
int status; |
|
70 |
int stdout_p[2]; |
|
71 |
int stderr_p[2]; |
|
72 |
||
73 |
child_action.sa_handler = childsig; |
|
74 |
sigemptyset (&child_action.sa_mask); |
|
75 |
child_action.sa_flags = 0; |
|
76 |
sigaction (SIGCHLD, &child_action, NULL); |
|
77 |
||
78 |
pipe(stdout_p); |
|
79 |
pipe(stderr_p); |
|
80 |
||
81 |
pid_t child = fork(); |
|
82 |
if (child == 0) |
|
83 |
{ |
|
84 |
close(stdout_p[0]); |
|
85 |
dup2(stdout_p[1], 1); |
|
86 |
close(stdout_p[1]); |
|
87 |
||
88 |
close(stderr_p[0]); |
|
89 |
dup2(stderr_p[1], 2); |
|
90 |
close(stderr_p[1]); |
|
91 |
||
92 |
execvp(executable, args); |
|
93 |
exit(1); |
|
94 |
} else if (child == -1) { |
|
95 |
p->causeofdeath = PROC_SOMEODDDEATH; |
|
96 |
return p; |
|
97 |
} |
|
98 |
else |
|
99 |
{ |
|
100 |
close(stdout_p[1]); |
|
101 |
close(stderr_p[1]); |
|
102 |
p->pid = child; |
|
103 |
DEBUG(("child running\n")); |
|
104 |
} |
|
105 |
||
106 |
struct pollfd pf[2]; |
|
107 |
||
108 |
int pv; |
|
109 |
int have_status = 0; |
|
110 |
do |
|
111 |
{ |
|
112 |
pf[0].fd = stdout_p[0]; |
|
113 |
pf[1].fd = stderr_p[0]; |
|
114 |
pf[0].events = POLLIN; |
|
115 |
pf[0].revents = 0; |
|
116 |
pf[1].events = POLLIN; |
|
117 |
pf[1].revents = 0; |
|
118 |
DEBUG(("polling\n")); |
|
119 |
pv = poll(pf, 2, timeout); |
|
120 |
DEBUG(("polled %d\n", pv)); |
|
121 |
if (pv == -1) |
|
122 |
{ |
|
123 |
if (errno == EAGAIN) |
|
124 |
{ |
|
125 |
errno = 0; |
|
126 |
DEBUG(("errno: \n")); |
|
127 |
continue; |
|
128 |
} else { |
|
129 |
/* EINVAL - can't poll */ |
|
130 |
process_free(&p); |
|
131 |
return NULL; |
|
132 |
} |
|
133 |
} else if (pv == 0 ) { |
|
134 |
/* timeout */ |
|
135 |
DEBUG(("timeout: \n")); |
|
136 |
kill(p->pid, SIGTERM); |
|
137 |
p->causeofdeath = PROC_TIMEOUTDEATH; |
|
138 |
break; |
|
139 |
} |
|
140 |
||
141 |
if (pf[0].revents & POLLIN ) |
|
142 |
{ |
|
143 |
char *space = buffer_makespace(p->output, 1024); |
|
144 |
int nbytes = read(pf[0].fd, space, 1024); |
|
145 |
if (nbytes < 0) |
|
146 |
break; |
|
147 |
buffer_usespace(p->output, nbytes); |
|
148 |
} |
|
149 |
if (pf[1].revents & POLLIN ) |
|
150 |
{ |
|
151 |
char *space = buffer_makespace(p->output, 1024); |
|
152 |
int nbytes = read(pf[1].fd, space, 1024); |
|
153 |
if (nbytes < 0) |
|
154 |
break; |
|
155 |
buffer_usespace(p->output, nbytes); |
|
156 |
} |
|
157 |
if (pf[0].revents & (POLLERR | POLLHUP | POLLNVAL)) |
|
158 |
{ |
|
159 |
DEBUG(("stdout: pollerr %d\n", pf[0].revents)); |
|
160 |
break; |
|
161 |
} |
|
162 |
||
163 |
if ( pf[1].revents & (POLLERR | POLLHUP | POLLNVAL)) |
|
164 |
{ |
|
165 |
DEBUG(("stderr: pollerr %d\n", pf[1].revents)); |
|
166 |
break; |
|
167 |
} |
|
168 |
DEBUG(("events: %d %d \n", pf[0].revents, pf[1].revents)); |
|
169 |
} |
|
170 |
while (1); |
|
171 |
||
172 |
waitpid(p->pid, &status, 0); |
|
173 |
if (WIFEXITED(status)) |
|
174 |
{ |
|
175 |
p->causeofdeath = PROC_NORMALDEATH; |
|
176 |
p->returncode = WEXITSTATUS(status); |
|
177 |
DEBUG(("process exited normally \n")); |
|
178 |
} else { |
|
237
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
179 |
if (p->causeofdeath == PROC_TIMEOUTDEATH) |
3 | 180 |
p->returncode = 128; |
237
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
181 |
else { |
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
182 |
p->causeofdeath = PROC_SOMEODDDEATH; |
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
183 |
if (WIFSIGNALED(status)) |
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
184 |
p->returncode = WTERMSIG(status); |
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
185 |
else |
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
186 |
p->returncode = 128; |
dd11681bd6f3
SF bug 1861 : display informative messages when timeouts occur in build tasks.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
187 |
} |
3 | 188 |
DEBUG(("process terminated \n")); |
189 |
} |
|
190 |
||
191 |
return p; |
|
192 |
} |
|
193 |
||
194 |
void process_free(proc **pp) |
|
195 |
{ |
|
196 |
if (!pp) |
|
197 |
return; |
|
198 |
if (! *pp) |
|
199 |
return; |
|
200 |
||
201 |
if ((*pp)->output) |
|
202 |
buffer_free(&((*pp)->output)); |
|
203 |
||
204 |
free(*pp); |
|
205 |
||
206 |
*pp = NULL; |
|
207 |
} |