pex-djgpp.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* Utilities to execute a program in a subprocess (possibly linked by pipes
  2. with other subprocesses), and wait for it. DJGPP specialization.
  3. Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2005
  4. Free Software Foundation, Inc.
  5. This file is part of the libiberty library.
  6. Libiberty is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10. Libiberty is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with libiberty; see the file COPYING.LIB. If not,
  16. write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  17. Boston, MA 02110-1301, USA. */
  18. #include "pex-common.h"
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #ifdef NEED_DECLARATION_ERRNO
  22. extern int errno;
  23. #endif
  24. #ifdef HAVE_STDLIB_H
  25. #include <stdlib.h>
  26. #endif
  27. #include <string.h>
  28. #include <fcntl.h>
  29. #include <unistd.h>
  30. #include <sys/stat.h>
  31. #include <process.h>
  32. /* Use ECHILD if available, otherwise use EINVAL. */
  33. #ifdef ECHILD
  34. #define PWAIT_ERROR ECHILD
  35. #else
  36. #define PWAIT_ERROR EINVAL
  37. #endif
  38. static int pex_djgpp_open_read (struct pex_obj *, const char *, int);
  39. static int pex_djgpp_open_write (struct pex_obj *, const char *, int, int);
  40. static pid_t pex_djgpp_exec_child (struct pex_obj *, int, const char *,
  41. char * const *, char * const *,
  42. int, int, int, int,
  43. const char **, int *);
  44. static int pex_djgpp_close (struct pex_obj *, int);
  45. static pid_t pex_djgpp_wait (struct pex_obj *, pid_t, int *, struct pex_time *,
  46. int, const char **, int *);
  47. /* The list of functions we pass to the common routines. */
  48. const struct pex_funcs funcs =
  49. {
  50. pex_djgpp_open_read,
  51. pex_djgpp_open_write,
  52. pex_djgpp_exec_child,
  53. pex_djgpp_close,
  54. pex_djgpp_wait,
  55. NULL, /* pipe */
  56. NULL, /* fdopenr */
  57. NULL, /* fdopenw */
  58. NULL /* cleanup */
  59. };
  60. /* Return a newly initialized pex_obj structure. */
  61. struct pex_obj *
  62. pex_init (int flags, const char *pname, const char *tempbase)
  63. {
  64. /* DJGPP does not support pipes. */
  65. flags &= ~ PEX_USE_PIPES;
  66. return pex_init_common (flags, pname, tempbase, &funcs);
  67. }
  68. /* Open a file for reading. */
  69. static int
  70. pex_djgpp_open_read (struct pex_obj *obj ATTRIBUTE_UNUSED,
  71. const char *name, int binary)
  72. {
  73. return open (name, O_RDONLY | (binary ? O_BINARY : O_TEXT));
  74. }
  75. /* Open a file for writing. */
  76. static int
  77. pex_djgpp_open_write (struct pex_obj *obj ATTRIBUTE_UNUSED,
  78. const char *name, int binary, int append)
  79. {
  80. /* Note that we can't use O_EXCL here because gcc may have already
  81. created the temporary file via make_temp_file. */
  82. if (append)
  83. return -1;
  84. return open (name,
  85. (O_WRONLY | O_CREAT | O_TRUNC
  86. | (binary ? O_BINARY : O_TEXT)),
  87. S_IRUSR | S_IWUSR);
  88. }
  89. /* Close a file. */
  90. static int
  91. pex_djgpp_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd)
  92. {
  93. return close (fd);
  94. }
  95. /* Execute a child. */
  96. static pid_t
  97. pex_djgpp_exec_child (struct pex_obj *obj, int flags, const char *executable,
  98. char * const * argv, char * const * env,
  99. int in, int out, int errdes,
  100. int toclose ATTRIBUTE_UNUSED, const char **errmsg,
  101. int *err)
  102. {
  103. int org_in, org_out, org_errdes;
  104. int status;
  105. int *statuses;
  106. org_in = -1;
  107. org_out = -1;
  108. org_errdes = -1;
  109. if (in != STDIN_FILE_NO)
  110. {
  111. org_in = dup (STDIN_FILE_NO);
  112. if (org_in < 0)
  113. {
  114. *err = errno;
  115. *errmsg = "dup";
  116. return (pid_t) -1;
  117. }
  118. if (dup2 (in, STDIN_FILE_NO) < 0)
  119. {
  120. *err = errno;
  121. *errmsg = "dup2";
  122. return (pid_t) -1;
  123. }
  124. if (close (in) < 0)
  125. {
  126. *err = errno;
  127. *errmsg = "close";
  128. return (pid_t) -1;
  129. }
  130. }
  131. if (out != STDOUT_FILE_NO)
  132. {
  133. org_out = dup (STDOUT_FILE_NO);
  134. if (org_out < 0)
  135. {
  136. *err = errno;
  137. *errmsg = "dup";
  138. return (pid_t) -1;
  139. }
  140. if (dup2 (out, STDOUT_FILE_NO) < 0)
  141. {
  142. *err = errno;
  143. *errmsg = "dup2";
  144. return (pid_t) -1;
  145. }
  146. if (close (out) < 0)
  147. {
  148. *err = errno;
  149. *errmsg = "close";
  150. return (pid_t) -1;
  151. }
  152. }
  153. if (errdes != STDERR_FILE_NO
  154. || (flags & PEX_STDERR_TO_STDOUT) != 0)
  155. {
  156. org_errdes = dup (STDERR_FILE_NO);
  157. if (org_errdes < 0)
  158. {
  159. *err = errno;
  160. *errmsg = "dup";
  161. return (pid_t) -1;
  162. }
  163. if (dup2 ((flags & PEX_STDERR_TO_STDOUT) != 0 ? STDOUT_FILE_NO : errdes,
  164. STDERR_FILE_NO) < 0)
  165. {
  166. *err = errno;
  167. *errmsg = "dup2";
  168. return (pid_t) -1;
  169. }
  170. if (errdes != STDERR_FILE_NO)
  171. {
  172. if (close (errdes) < 0)
  173. {
  174. *err = errno;
  175. *errmsg = "close";
  176. return (pid_t) -1;
  177. }
  178. }
  179. }
  180. if (env)
  181. status = (((flags & PEX_SEARCH) != 0 ? spawnvpe : spawnve)
  182. (P_WAIT, executable, argv, env));
  183. else
  184. status = (((flags & PEX_SEARCH) != 0 ? spawnvp : spawnv)
  185. (P_WAIT, executable, argv));
  186. if (status == -1)
  187. {
  188. *err = errno;
  189. *errmsg = ((flags & PEX_SEARCH) != 0) ? "spawnvp" : "spawnv";
  190. }
  191. if (in != STDIN_FILE_NO)
  192. {
  193. if (dup2 (org_in, STDIN_FILE_NO) < 0)
  194. {
  195. *err = errno;
  196. *errmsg = "dup2";
  197. return (pid_t) -1;
  198. }
  199. if (close (org_in) < 0)
  200. {
  201. *err = errno;
  202. *errmsg = "close";
  203. return (pid_t) -1;
  204. }
  205. }
  206. if (out != STDOUT_FILE_NO)
  207. {
  208. if (dup2 (org_out, STDOUT_FILE_NO) < 0)
  209. {
  210. *err = errno;
  211. *errmsg = "dup2";
  212. return (pid_t) -1;
  213. }
  214. if (close (org_out) < 0)
  215. {
  216. *err = errno;
  217. *errmsg = "close";
  218. return (pid_t) -1;
  219. }
  220. }
  221. if (errdes != STDERR_FILE_NO
  222. || (flags & PEX_STDERR_TO_STDOUT) != 0)
  223. {
  224. if (dup2 (org_errdes, STDERR_FILE_NO) < 0)
  225. {
  226. *err = errno;
  227. *errmsg = "dup2";
  228. return (pid_t) -1;
  229. }
  230. if (close (org_errdes) < 0)
  231. {
  232. *err = errno;
  233. *errmsg = "close";
  234. return (pid_t) -1;
  235. }
  236. }
  237. /* Save the exit status for later. When we are called, obj->count
  238. is the number of children which have executed before this
  239. one. */
  240. statuses = (int *) obj->sysdep;
  241. statuses = XRESIZEVEC (int, statuses, obj->count + 1);
  242. statuses[obj->count] = status;
  243. obj->sysdep = (void *) statuses;
  244. return (pid_t) obj->count;
  245. }
  246. /* Wait for a child process to complete. Actually the child process
  247. has already completed, and we just need to return the exit
  248. status. */
  249. static pid_t
  250. pex_djgpp_wait (struct pex_obj *obj, pid_t pid, int *status,
  251. struct pex_time *time, int done ATTRIBUTE_UNUSED,
  252. const char **errmsg ATTRIBUTE_UNUSED,
  253. int *err ATTRIBUTE_UNUSED)
  254. {
  255. int *statuses;
  256. if (time != NULL)
  257. memset (time, 0, sizeof *time);
  258. statuses = (int *) obj->sysdep;
  259. *status = statuses[pid];
  260. return 0;
  261. }