cpproc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* cpproc.c -
  2. Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. #include "config.h"
  32. #include <jni.h>
  33. #include "cpproc.h"
  34. #include <signal.h>
  35. #include <sys/types.h>
  36. #include <sys/wait.h>
  37. #include <unistd.h>
  38. #include <errno.h>
  39. #include <stdlib.h>
  40. static void close_all_fds(int *fds, int numFds)
  41. {
  42. int i;
  43. for (i = 0; i < numFds; i++)
  44. close(fds[i]);
  45. }
  46. int cpproc_forkAndExec (char * const *commandLine, char * const * newEnviron,
  47. int *fds, int pipe_count, pid_t *out_pid, const char *wd)
  48. {
  49. int local_fds[6];
  50. int i;
  51. pid_t pid;
  52. for (i = 0; i < (pipe_count * 2); i += 2)
  53. {
  54. if (pipe(&local_fds[i]) < 0)
  55. {
  56. int err = errno;
  57. close_all_fds(local_fds, i);
  58. return err;
  59. }
  60. }
  61. pid = fork();
  62. switch (pid)
  63. {
  64. case 0:
  65. dup2(local_fds[0], 0);
  66. dup2(local_fds[3], 1);
  67. if (pipe_count == 3)
  68. dup2(local_fds[5], 2);
  69. else
  70. dup2(1, 2);
  71. close_all_fds(local_fds, pipe_count * 2);
  72. i = chdir(wd);
  73. /* FIXME: Handle the return value */
  74. if (newEnviron == NULL)
  75. execvp(commandLine[0], commandLine);
  76. else
  77. execve(commandLine[0], commandLine, newEnviron);
  78. abort();
  79. break;
  80. case -1:
  81. {
  82. int err = errno;
  83. close_all_fds(local_fds, pipe_count * 2);
  84. return err;
  85. }
  86. default:
  87. close(local_fds[0]);
  88. close(local_fds[3]);
  89. if (pipe_count == 3)
  90. close(local_fds[5]);
  91. fds[0] = local_fds[1];
  92. fds[1] = local_fds[2];
  93. fds[2] = local_fds[4];
  94. *out_pid = pid;
  95. return 0;
  96. }
  97. /* keep compiler happy */
  98. return 0;
  99. }
  100. int cpproc_waitpid (pid_t pid, int *status, pid_t *outpid, int options)
  101. {
  102. pid_t wp = waitpid(pid, status, options);
  103. if (wp < 0)
  104. return errno;
  105. *outpid = wp;
  106. return 0;
  107. }
  108. int cpproc_kill (pid_t pid, int signal)
  109. {
  110. if (kill(pid, signal) < 0)
  111. return errno;
  112. return 0;
  113. }