pex-common.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Utilities to execute a program in a subprocess (possibly linked by pipes
  2. with other subprocesses), and wait for it. Shared logic.
  3. Copyright (C) 1996-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. #ifndef PEX_COMMON_H
  18. #define PEX_COMMON_H
  19. #include "config.h"
  20. #include "libiberty.h"
  21. #include <stdio.h>
  22. /* pid_t is may defined by config.h or sys/types.h needs to be
  23. included. */
  24. #if !defined(pid_t) && defined(HAVE_SYS_TYPES_H)
  25. #include <sys/types.h>
  26. #endif
  27. #define install_error_msg "installation problem, cannot exec `%s'"
  28. /* stdin file number. */
  29. #define STDIN_FILE_NO 0
  30. /* stdout file number. */
  31. #define STDOUT_FILE_NO 1
  32. /* stderr file number. */
  33. #define STDERR_FILE_NO 2
  34. /* value of `pipe': port index for reading. */
  35. #define READ_PORT 0
  36. /* value of `pipe': port index for writing. */
  37. #define WRITE_PORT 1
  38. /* The structure used by pex_init and friends. */
  39. struct pex_obj
  40. {
  41. /* Flags. */
  42. int flags;
  43. /* Name of calling program, for error messages. */
  44. const char *pname;
  45. /* Base name to use for temporary files. */
  46. const char *tempbase;
  47. /* Pipe to use as stdin for next process. */
  48. int next_input;
  49. /* File name to use as stdin for next process. */
  50. char *next_input_name;
  51. /* Whether next_input_name was allocated using malloc. */
  52. int next_input_name_allocated;
  53. /* If not -1, stderr pipe from the last process. */
  54. int stderr_pipe;
  55. /* Number of child processes. */
  56. int count;
  57. /* PIDs of child processes; array allocated using malloc. */
  58. pid_t *children;
  59. /* Exit statuses of child processes; array allocated using malloc. */
  60. int *status;
  61. /* Time used by child processes; array allocated using malloc. */
  62. struct pex_time *time;
  63. /* Number of children we have already waited for. */
  64. int number_waited;
  65. /* FILE created by pex_input_file. */
  66. FILE *input_file;
  67. /* FILE created by pex_read_output. */
  68. FILE *read_output;
  69. /* FILE created by pex_read_err. */
  70. FILE *read_err;
  71. /* Number of temporary files to remove. */
  72. int remove_count;
  73. /* List of temporary files to remove; array allocated using malloc
  74. of strings allocated using malloc. */
  75. char **remove;
  76. /* Pointers to system dependent functions. */
  77. const struct pex_funcs *funcs;
  78. /* For use by system dependent code. */
  79. void *sysdep;
  80. };
  81. /* Functions passed to pex_run_common. */
  82. struct pex_funcs
  83. {
  84. /* Open file NAME for reading. If BINARY is non-zero, open in
  85. binary mode. Return >= 0 on success, -1 on error. */
  86. int (*open_read) (struct pex_obj *, const char */* name */, int /* binary */);
  87. /* Open file NAME for writing. If BINARY is non-zero, open in
  88. binary mode. Return >= 0 on success, -1 on error. */
  89. int (*open_write) (struct pex_obj *, const char */* name */,
  90. int /* binary */, int /* append */);
  91. /* Execute a child process. FLAGS, EXECUTABLE, ARGV, ERR are from
  92. pex_run. IN, OUT, ERRDES, TOCLOSE are all descriptors, from
  93. open_read, open_write, or pipe, or they are one of STDIN_FILE_NO,
  94. STDOUT_FILE_NO or STDERR_FILE_NO; if IN, OUT, and ERRDES are not
  95. STD*_FILE_NO, they should be closed. If the descriptor TOCLOSE
  96. is not -1, and the system supports pipes, TOCLOSE should be
  97. closed in the child process. The function should handle the
  98. PEX_STDERR_TO_STDOUT flag. Return >= 0 on success, or -1 on
  99. error and set *ERRMSG and *ERR. */
  100. pid_t (*exec_child) (struct pex_obj *, int /* flags */,
  101. const char */* executable */, char * const * /* argv */,
  102. char * const * /* env */,
  103. int /* in */, int /* out */, int /* errdes */,
  104. int /* toclose */, const char **/* errmsg */,
  105. int */* err */);
  106. /* Close a descriptor. Return 0 on success, -1 on error. */
  107. int (*close) (struct pex_obj *, int);
  108. /* Wait for a child to complete, returning exit status in *STATUS
  109. and time in *TIME (if it is not null). CHILD is from fork. DONE
  110. is 1 if this is called via pex_free. ERRMSG and ERR are as in
  111. fork. Return 0 on success, -1 on error. */
  112. pid_t (*wait) (struct pex_obj *, pid_t /* child */, int * /* status */,
  113. struct pex_time * /* time */, int /* done */,
  114. const char ** /* errmsg */, int * /* err */);
  115. /* Create a pipe (only called if PEX_USE_PIPES is set) storing two
  116. descriptors in P[0] and P[1]. If BINARY is non-zero, open in
  117. binary mode. Return 0 on success, -1 on error. */
  118. int (*pipe) (struct pex_obj *, int * /* p */, int /* binary */);
  119. /* Get a FILE pointer to read from a file descriptor (only called if
  120. PEX_USE_PIPES is set). If BINARY is non-zero, open in binary
  121. mode. Return pointer on success, NULL on error. */
  122. FILE * (*fdopenr) (struct pex_obj *, int /* fd */, int /* binary */);
  123. /* Get a FILE pointer to write to the file descriptor FD (only
  124. called if PEX_USE_PIPES is set). If BINARY is non-zero, open in
  125. binary mode. Arrange for FD not to be inherited by the child
  126. processes. Return pointer on success, NULL on error. */
  127. FILE * (*fdopenw) (struct pex_obj *, int /* fd */, int /* binary */);
  128. /* Free any system dependent data associated with OBJ. May be
  129. NULL if there is nothing to do. */
  130. void (*cleanup) (struct pex_obj *);
  131. };
  132. extern struct pex_obj *pex_init_common (int, const char *, const char *,
  133. const struct pex_funcs *);
  134. #endif