pexecute.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Utilities to execute a program in a subprocess (possibly linked by pipes
  2. with other subprocesses), and wait for it.
  3. Copyright (C) 2004-2022 Free Software Foundation, Inc.
  4. This file is part of the libiberty library.
  5. Libiberty is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. Libiberty is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with libiberty; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  16. Boston, MA 02110-1301, USA. */
  17. /* pexecute is an old routine. This implementation uses the newer
  18. pex_init/pex_run/pex_get_status/pex_free routines. Don't use
  19. pexecute in new code. Use the newer routines instead. */
  20. #include "config.h"
  21. #include "libiberty.h"
  22. #ifdef HAVE_STDLIB_H
  23. #include <stdlib.h>
  24. #endif
  25. /* We only permit a single pexecute chain to execute at a time. This
  26. was always true anyhow, though it wasn't documented. */
  27. static struct pex_obj *pex;
  28. static int idx;
  29. int
  30. pexecute (const char *program, char * const *argv, const char *pname,
  31. const char *temp_base, char **errmsg_fmt, char **errmsg_arg,
  32. int flags)
  33. {
  34. const char *errmsg;
  35. int err;
  36. if ((flags & PEXECUTE_FIRST) != 0)
  37. {
  38. if (pex != NULL)
  39. {
  40. *errmsg_fmt = (char *) "pexecute already in progress";
  41. *errmsg_arg = NULL;
  42. return -1;
  43. }
  44. pex = pex_init (PEX_USE_PIPES, pname, temp_base);
  45. idx = 0;
  46. }
  47. else
  48. {
  49. if (pex == NULL)
  50. {
  51. *errmsg_fmt = (char *) "pexecute not in progress";
  52. *errmsg_arg = NULL;
  53. return -1;
  54. }
  55. }
  56. errmsg = pex_run (pex,
  57. (((flags & PEXECUTE_LAST) != 0 ? PEX_LAST : 0)
  58. | ((flags & PEXECUTE_SEARCH) != 0 ? PEX_SEARCH : 0)),
  59. program, argv, NULL, NULL, &err);
  60. if (errmsg != NULL)
  61. {
  62. *errmsg_fmt = (char *) errmsg;
  63. *errmsg_arg = NULL;
  64. return -1;
  65. }
  66. /* Instead of a PID, we just return a one-based index into the
  67. status values. We avoid zero just because the old pexecute would
  68. never return it. */
  69. return ++idx;
  70. }
  71. int
  72. pwait (int pid, int *status, int flags ATTRIBUTE_UNUSED)
  73. {
  74. /* The PID returned by pexecute is one-based. */
  75. --pid;
  76. if (pex == NULL || pid < 0 || pid >= idx)
  77. return -1;
  78. if (pid == 0 && idx == 1)
  79. {
  80. if (!pex_get_status (pex, 1, status))
  81. return -1;
  82. }
  83. else
  84. {
  85. int *vector;
  86. vector = XNEWVEC (int, idx);
  87. if (!pex_get_status (pex, idx, vector))
  88. {
  89. free (vector);
  90. return -1;
  91. }
  92. *status = vector[pid];
  93. free (vector);
  94. }
  95. /* Assume that we are done after the caller has retrieved the last
  96. exit status. The original implementation did not require that
  97. the exit statuses be retrieved in order, but this implementation
  98. does. */
  99. if (pid + 1 == idx)
  100. {
  101. pex_free (pex);
  102. pex = NULL;
  103. idx = 0;
  104. }
  105. return pid + 1;
  106. }