Main Page | Modules | Namespace List | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages | Examples

apr_proc_stub.c

Go to the documentation of this file.
00001 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
00002  * applicable.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <kernel/OS.h>
00018 #include <stdlib.h>
00019 #include <unistd.h>
00020 #include <stdio.h>
00021 
00022 struct pipefd {
00023         int in;
00024         int out;
00025         int err;
00026 };
00027 
00028 int main(int argc, char *argv[]) {
00029 /* we expect the following...
00030  * 
00031  * argv[0] = this stub
00032  * argv[1] = directory to run in...
00033  * argv[2] = progname to execute
00034  * rest of arguments to be passed to program
00035  */
00036         char *progname = argv[2];
00037         char *directory = argv[1];
00038         struct pipefd *pfd;
00039         thread_id sender;
00040         void *buffer;
00041         char ** newargs;
00042         int i = 0;
00043         
00044         newargs = (char**)malloc(sizeof(char*) * (argc - 1));
00045   
00046         buffer = (void*)malloc(sizeof(struct pipefd));
00047         /* this will block until we get the data */
00048         receive_data(&sender, buffer, sizeof(struct pipefd));
00049         pfd = (struct pipefd*)buffer;
00050         
00051         if (pfd->in > STDERR_FILENO) {
00052                 if (dup2(pfd->in, STDIN_FILENO) != STDIN_FILENO) return (-1);
00053             close (pfd->in);
00054         }
00055         if (pfd->out > STDERR_FILENO) {
00056                 if (dup2(pfd->out, STDOUT_FILENO) != STDOUT_FILENO) return (-1);
00057             close (pfd->out);
00058         }
00059         if (pfd->err > STDERR_FILENO) {
00060                 if (dup2(pfd->err, STDERR_FILENO) != STDERR_FILENO) return (-1);
00061             close (pfd->err);
00062         }
00063 
00064         for     (i=3;i<=argc;i++){
00065             newargs[i-3] = argv[i];
00066         }
00067         
00068         /* tell the caller we're OK to start */
00069         send_data(sender,1,NULL,0);
00070 
00071         if (directory != NULL)
00072                 chdir(directory);
00073         execve (progname, newargs, environ);
00074 
00075         return (-1);
00076 }