bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / threadproc / beos / proc.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr_arch_threadproc.h"
18 #include "apr_strings.h"
19
20 struct send_pipe {
21         int in;
22         int out;
23         int err;
24 };
25
26 APR_DECLARE(apr_status_t) apr_procattr_create(apr_procattr_t **new, apr_pool_t *pool)
27 {
28     (*new) = (apr_procattr_t *)apr_palloc(pool, 
29               sizeof(apr_procattr_t));
30
31     if ((*new) == NULL) {
32         return APR_ENOMEM;
33     }
34     (*new)->pool = pool;
35     (*new)->parent_in = NULL;
36     (*new)->child_in = NULL;
37     (*new)->parent_out = NULL;
38     (*new)->child_out = NULL;
39     (*new)->parent_err = NULL;
40     (*new)->child_err = NULL;
41     (*new)->currdir = NULL; 
42     (*new)->cmdtype = APR_PROGRAM;
43     (*new)->detached = 0;
44     return APR_SUCCESS;
45 }
46
47 APR_DECLARE(apr_status_t) apr_procattr_io_set(apr_procattr_t *attr, apr_int32_t in, 
48                                               apr_int32_t out, apr_int32_t err)
49 {
50     apr_status_t status;
51     if (in != 0) {
52         if ((status = apr_file_pipe_create(&attr->child_in, &attr->parent_in, 
53                                    attr->pool)) != APR_SUCCESS) {
54             return status;
55         }
56         switch (in) {
57         case APR_FULL_BLOCK:
58             apr_file_pipe_timeout_set(attr->child_in, -1);
59             apr_file_pipe_timeout_set(attr->parent_in, -1);
60             break;
61         case APR_PARENT_BLOCK:
62             apr_file_pipe_timeout_set(attr->child_in, -1);
63             break;
64         case APR_CHILD_BLOCK:
65             apr_file_pipe_timeout_set(attr->parent_in, -1);
66             break;
67         default:
68             break;
69         }
70     } 
71     if (out) {
72         if ((status = apr_file_pipe_create(&attr->parent_out, &attr->child_out, 
73                                    attr->pool)) != APR_SUCCESS) {
74             return status;
75         }
76         switch (out) {
77         case APR_FULL_BLOCK:
78             apr_file_pipe_timeout_set(attr->child_out, -1);
79             apr_file_pipe_timeout_set(attr->parent_out, -1);       
80             break;
81         case APR_PARENT_BLOCK:
82             apr_file_pipe_timeout_set(attr->child_out, -1);
83             break;
84         case APR_CHILD_BLOCK:
85             apr_file_pipe_timeout_set(attr->parent_out, -1);
86             break;
87         default:
88             break;
89         }
90     } 
91     if (err) {
92         if ((status = apr_file_pipe_create(&attr->parent_err, &attr->child_err, 
93                                    attr->pool)) != APR_SUCCESS) {
94             return status;
95         }
96         switch (err) {
97         case APR_FULL_BLOCK:
98             apr_file_pipe_timeout_set(attr->child_err, -1);
99             apr_file_pipe_timeout_set(attr->parent_err, -1);
100             break;
101         case APR_PARENT_BLOCK:
102             apr_file_pipe_timeout_set(attr->child_err, -1);
103             break;
104         case APR_CHILD_BLOCK:
105             apr_file_pipe_timeout_set(attr->parent_err, -1);
106             break;
107         default:
108             break;
109         }
110     } 
111     return APR_SUCCESS;
112 }
113
114 APR_DECLARE(apr_status_t) apr_procattr_dir_set(apr_procattr_t *attr, 
115                                                const char *dir) 
116 {
117     char * cwd;
118     if (dir[0] != '/') {
119         cwd = (char*)malloc(sizeof(char) * PATH_MAX);
120         getcwd(cwd, PATH_MAX);
121         attr->currdir = (char *)apr_pstrcat(attr->pool, cwd, "/", dir, NULL);
122         free(cwd);
123     } else {
124         attr->currdir = (char *)apr_pstrdup(attr->pool, dir);
125     }
126     if (attr->currdir) {
127         return APR_SUCCESS;
128     }
129     return APR_ENOMEM;
130 }
131
132 APR_DECLARE(apr_status_t) apr_procattr_cmdtype_set(apr_procattr_t *attr,
133                                                    apr_cmdtype_e cmd) 
134 {
135     attr->cmdtype = cmd;
136     return APR_SUCCESS;
137 }
138
139 APR_DECLARE(apr_status_t) apr_procattr_detach_set(apr_procattr_t *attr, apr_int32_t detach) 
140 {
141     attr->detached = detach;
142     return APR_SUCCESS;
143 }
144
145 APR_DECLARE(apr_status_t) apr_proc_fork(apr_proc_t *proc, apr_pool_t *pool)
146 {
147     int pid;
148     
149     if ((pid = fork()) < 0) {
150         return errno;
151     }
152     else if (pid == 0) {
153         proc->pid = pid;
154         proc->in = NULL; 
155         proc->out = NULL; 
156         proc->err = NULL; 
157         return APR_INCHILD;
158     }
159     proc->pid = pid;
160     proc->in = NULL; 
161     proc->out = NULL; 
162     proc->err = NULL; 
163     return APR_INPARENT;
164 }
165
166 APR_DECLARE(apr_status_t) apr_procattr_child_errfn_set(apr_procattr_t *attr,
167                                                        apr_child_errfn_t *errfn)
168 {
169     /* won't ever be called on this platform, so don't save the function pointer */
170     return APR_SUCCESS;
171 }
172
173 APR_DECLARE(apr_status_t) apr_procattr_error_check_set(apr_procattr_t *attr,
174                                                        apr_int32_t chk)
175 {
176     /* won't ever be used on this platform, so don't save the flag */
177     return APR_SUCCESS;
178 }
179
180 APR_DECLARE(apr_status_t) apr_procattr_addrspace_set(apr_procattr_t *attr,
181                                                        apr_int32_t addrspace)
182 {
183     /* won't ever be used on this platform, so don't save the flag */
184     return APR_SUCCESS;
185 }
186
187 APR_DECLARE(apr_status_t) apr_proc_create(apr_proc_t *new, const char *progname, 
188                                           const char * const *args,
189                                           const char * const *env, 
190                                           apr_procattr_t *attr, apr_pool_t *pool)
191 {
192     int i=0,nargs=0;
193     char **newargs = NULL;
194     thread_id newproc, sender;
195     struct send_pipe *sp;        
196     char * dir = NULL;
197             
198     sp = (struct send_pipe *)apr_palloc(pool, sizeof(struct send_pipe));
199
200     new->in = attr->parent_in;
201     new->err = attr->parent_err;
202     new->out = attr->parent_out;
203         sp->in  = attr->child_in  ? attr->child_in->filedes  : -1;
204         sp->out = attr->child_out ? attr->child_out->filedes : -1;
205         sp->err = attr->child_err ? attr->child_err->filedes : -1;
206
207     i = 0;
208     while (args && args[i]) {
209         i++;
210     }
211
212         newargs = (char**)malloc(sizeof(char *) * (i + 4));
213         newargs[0] = strdup("/boot/home/config/bin/apr_proc_stub");
214     if (attr->currdir == NULL) {
215         /* we require the directory , so use a temp. variable */
216         dir = malloc(sizeof(char) * PATH_MAX);
217         getcwd(dir, PATH_MAX);
218         newargs[1] = strdup(dir);
219         free(dir);
220     } else {
221         newargs[1] = strdup(attr->currdir);
222     }
223     newargs[2] = strdup(progname);
224     i=0;nargs = 3;
225
226     while (args && args[i]) {
227         newargs[nargs] = strdup(args[i]);
228         i++;nargs++;
229     }
230     newargs[nargs] = NULL;
231
232     /* ### we should be looking at attr->cmdtype in here... */
233
234     newproc = load_image(nargs, (const char**)newargs, (const char**)env);
235
236     /* load_image copies the data so now we can free it... */
237     while (--nargs >= 0)
238         free (newargs[nargs]);
239     free(newargs);
240         
241     if ( newproc < B_NO_ERROR) {
242         return errno;
243     }
244
245     resume_thread(newproc);
246
247     if (attr->child_in) {
248         apr_file_close(attr->child_in);
249     }
250     if (attr->child_out) {
251         apr_file_close(attr->child_out);
252     }
253     if (attr->child_err) {
254         apr_file_close(attr->child_err);
255     }
256
257     send_data(newproc, 0, (void*)sp, sizeof(struct send_pipe));
258     new->pid = newproc;
259
260     /* before we go charging on we need the new process to get to a 
261      * certain point.  When it gets there it'll let us know and we
262      * can carry on. */
263     receive_data(&sender, (void*)NULL,0);
264     
265     return APR_SUCCESS;
266 }
267
268 APR_DECLARE(apr_status_t) apr_proc_wait_all_procs(apr_proc_t *proc,
269                                                   int *exitcode,
270                                                   apr_exit_why_e *exitwhy,
271                                                   apr_wait_how_e waithow, 
272                                                   apr_pool_t *p)
273 {
274     proc->pid = -1;
275     return apr_proc_wait(proc, exitcode, exitwhy, waithow);
276
277
278 APR_DECLARE(apr_status_t) apr_proc_wait(apr_proc_t *proc,
279                                         int *exitcode, 
280                                         apr_exit_why_e *exitwhy,
281                                         apr_wait_how_e waithow)
282 {
283     pid_t pstatus;
284     int waitpid_options = WUNTRACED;
285     int exit_int;
286     int ignore;
287     apr_exit_why_e ignorewhy;
288
289     if (exitcode == NULL) {
290         exitcode = &ignore;
291     }
292     if (exitwhy == NULL) {
293         exitwhy = &ignorewhy;
294     }
295
296     if (waithow != APR_WAIT) {
297         waitpid_options |= WNOHANG;
298     }
299     
300     if ((pstatus = waitpid(proc->pid, &exit_int, waitpid_options)) > 0) {
301         proc->pid = pstatus;
302         if (WIFEXITED(exit_int)) {
303             *exitwhy = APR_PROC_EXIT;
304             *exitcode = WEXITSTATUS(exit_int);
305         }
306         else if (WIFSIGNALED(exit_int)) {
307             *exitwhy = APR_PROC_SIGNAL;
308             *exitcode = WTERMSIG(exit_int);
309         }
310         else {
311             /* unexpected condition */
312             return APR_EGENERAL;
313         }
314         return APR_CHILD_DONE;
315     }
316     else if (pstatus == 0) {
317         return APR_CHILD_NOTDONE;
318     }
319     return errno;
320
321
322 APR_DECLARE(apr_status_t) apr_procattr_child_in_set(apr_procattr_t *attr, apr_file_t *child_in,
323                                    apr_file_t *parent_in)
324 {
325     if (attr->child_in == NULL && attr->parent_in == NULL)
326         apr_file_pipe_create(&attr->child_in, &attr->parent_in, attr->pool);
327
328     if (child_in != NULL)
329         apr_file_dup(&attr->child_in, child_in, attr->pool);
330
331     if (parent_in != NULL)
332         apr_file_dup(&attr->parent_in, parent_in, attr->pool);
333
334     return APR_SUCCESS;
335 }
336
337 APR_DECLARE(apr_status_t) apr_procattr_child_out_set(apr_procattr_t *attr, apr_file_t *child_out,
338                                                      apr_file_t *parent_out)
339 {
340     if (attr->child_out == NULL && attr->parent_out == NULL)
341         apr_file_pipe_create(&attr->child_out, &attr->parent_out, attr->pool);
342
343     if (child_out != NULL)
344         apr_file_dup(&attr->child_out, child_out, attr->pool);
345
346     if (parent_out != NULL)
347         apr_file_dup(&attr->parent_out, parent_out, attr->pool);
348
349     return APR_SUCCESS;
350 }
351
352 APR_DECLARE(apr_status_t) apr_procattr_child_err_set(apr_procattr_t *attr, apr_file_t *child_err,
353                                                      apr_file_t *parent_err)
354 {
355     if (attr->child_err == NULL && attr->parent_err == NULL)
356         apr_file_pipe_create(&attr->child_err, &attr->parent_err, attr->pool);
357
358     if (child_err != NULL)
359         apr_file_dup(&attr->child_err, child_err, attr->pool);
360
361     if (parent_err != NULL)
362         apr_file_dup(&attr->parent_err, parent_err, attr->pool);
363
364     return APR_SUCCESS;
365 }
366
367 APR_DECLARE(apr_status_t) apr_procattr_limit_set(apr_procattr_t *attr, apr_int32_t what, 
368                                                   void *limit)
369 {
370     return APR_ENOTIMPL;
371 }