apr_proc_stub.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include <kernel/OS.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. struct pipefd {
  21. int in;
  22. int out;
  23. int err;
  24. };
  25. int main(int argc, char *argv[]) {
  26. /* we expect the following...
  27. *
  28. * argv[0] = this stub
  29. * argv[1] = directory to run in...
  30. * argv[2] = progname to execute
  31. * rest of arguments to be passed to program
  32. */
  33. char *progname = argv[2];
  34. char *directory = argv[1];
  35. struct pipefd *pfd;
  36. thread_id sender;
  37. void *buffer;
  38. char ** newargs;
  39. int i = 0;
  40. newargs = (char**)malloc(sizeof(char*) * (argc - 1));
  41. buffer = (void*)malloc(sizeof(struct pipefd));
  42. /* this will block until we get the data */
  43. receive_data(&sender, buffer, sizeof(struct pipefd));
  44. pfd = (struct pipefd*)buffer;
  45. if (pfd->in > STDERR_FILENO) {
  46. if (dup2(pfd->in, STDIN_FILENO) != STDIN_FILENO) return (-1);
  47. close (pfd->in);
  48. }
  49. if (pfd->out > STDERR_FILENO) {
  50. if (dup2(pfd->out, STDOUT_FILENO) != STDOUT_FILENO) return (-1);
  51. close (pfd->out);
  52. }
  53. if (pfd->err > STDERR_FILENO) {
  54. if (dup2(pfd->err, STDERR_FILENO) != STDERR_FILENO) return (-1);
  55. close (pfd->err);
  56. }
  57. for (i=3;i<=argc;i++){
  58. newargs[i-3] = argv[i];
  59. }
  60. /* tell the caller we're OK to start */
  61. send_data(sender,1,NULL,0);
  62. if (directory != NULL)
  63. chdir(directory);
  64. execve (progname, newargs, environ);
  65. return (-1);
  66. }