pipe-posix.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2023 Lain Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdio.h>
  17. #include <sys/wait.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <spawn.h>
  21. #include <fcntl.h>
  22. #include "bmem.h"
  23. #include "pipe.h"
  24. struct os_process_pipe {
  25. bool read_pipe;
  26. int pid;
  27. FILE *file;
  28. FILE *err_file;
  29. };
  30. os_process_pipe_t *os_process_pipe_create_internal(const char *bin, char **argv,
  31. const char *type)
  32. {
  33. struct os_process_pipe process_pipe = {0};
  34. struct os_process_pipe *out;
  35. posix_spawn_file_actions_t file_actions;
  36. if (!bin || !argv || !type) {
  37. return NULL;
  38. }
  39. process_pipe.read_pipe = *type == 'r';
  40. int mainfds[2] = {0};
  41. int errfds[2] = {0};
  42. if (pipe(mainfds) != 0) {
  43. return NULL;
  44. }
  45. if (pipe(errfds) != 0) {
  46. close(mainfds[0]);
  47. close(mainfds[1]);
  48. return NULL;
  49. }
  50. if (posix_spawn_file_actions_init(&file_actions) != 0) {
  51. close(mainfds[0]);
  52. close(mainfds[1]);
  53. close(errfds[0]);
  54. close(errfds[1]);
  55. return NULL;
  56. }
  57. fcntl(mainfds[0], F_SETFD, FD_CLOEXEC);
  58. fcntl(mainfds[1], F_SETFD, FD_CLOEXEC);
  59. fcntl(errfds[0], F_SETFD, FD_CLOEXEC);
  60. fcntl(errfds[1], F_SETFD, FD_CLOEXEC);
  61. if (process_pipe.read_pipe) {
  62. posix_spawn_file_actions_addclose(&file_actions, mainfds[0]);
  63. if (mainfds[1] != STDOUT_FILENO) {
  64. posix_spawn_file_actions_adddup2(
  65. &file_actions, mainfds[1], STDOUT_FILENO);
  66. posix_spawn_file_actions_addclose(&file_actions,
  67. mainfds[0]);
  68. }
  69. } else {
  70. if (mainfds[0] != STDIN_FILENO) {
  71. posix_spawn_file_actions_adddup2(
  72. &file_actions, mainfds[0], STDIN_FILENO);
  73. posix_spawn_file_actions_addclose(&file_actions,
  74. mainfds[1]);
  75. }
  76. }
  77. posix_spawn_file_actions_addclose(&file_actions, errfds[0]);
  78. posix_spawn_file_actions_adddup2(&file_actions, errfds[1],
  79. STDERR_FILENO);
  80. int pid;
  81. int ret = posix_spawn(&pid, bin, &file_actions, NULL,
  82. (char *const *)argv, NULL);
  83. posix_spawn_file_actions_destroy(&file_actions);
  84. if (ret != 0) {
  85. close(mainfds[0]);
  86. close(mainfds[1]);
  87. close(errfds[0]);
  88. close(errfds[1]);
  89. return NULL;
  90. }
  91. close(errfds[1]);
  92. process_pipe.err_file = fdopen(errfds[0], "r");
  93. if (process_pipe.read_pipe) {
  94. close(mainfds[1]);
  95. process_pipe.file = fdopen(mainfds[0], "r");
  96. } else {
  97. close(mainfds[0]);
  98. process_pipe.file = fdopen(mainfds[1], "w");
  99. }
  100. process_pipe.pid = pid;
  101. out = bmalloc(sizeof(os_process_pipe_t));
  102. *out = process_pipe;
  103. return out;
  104. }
  105. os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
  106. const char *type)
  107. {
  108. if (!cmd_line)
  109. return NULL;
  110. char *argv[3] = {"-c", (char *)cmd_line, NULL};
  111. return os_process_pipe_create_internal("/bin/sh", argv, type);
  112. }
  113. os_process_pipe_t *os_process_pipe_create2(const os_process_args_t *args,
  114. const char *type)
  115. {
  116. char **argv = os_process_args_get_argv(args);
  117. return os_process_pipe_create_internal(argv[0], argv, type);
  118. }
  119. int os_process_pipe_destroy(os_process_pipe_t *pp)
  120. {
  121. int ret = 0;
  122. if (pp) {
  123. int status;
  124. fclose(pp->file);
  125. pp->file = NULL;
  126. fclose(pp->err_file);
  127. pp->err_file = NULL;
  128. do {
  129. ret = waitpid(pp->pid, &status, 0);
  130. } while (ret == -1 && errno == EINTR);
  131. if (WIFEXITED(status))
  132. ret = (int)(char)WEXITSTATUS(status);
  133. bfree(pp);
  134. }
  135. return ret;
  136. }
  137. size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len)
  138. {
  139. if (!pp) {
  140. return 0;
  141. }
  142. if (!pp->read_pipe) {
  143. return 0;
  144. }
  145. return fread(data, 1, len, pp->file);
  146. }
  147. size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data,
  148. size_t len)
  149. {
  150. if (!pp) {
  151. return 0;
  152. }
  153. return fread(data, 1, len, pp->err_file);
  154. }
  155. size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data,
  156. size_t len)
  157. {
  158. if (!pp) {
  159. return 0;
  160. }
  161. if (pp->read_pipe) {
  162. return 0;
  163. }
  164. size_t written = 0;
  165. while (written < len) {
  166. size_t ret = fwrite(data + written, 1, len - written, pp->file);
  167. if (!ret)
  168. return written;
  169. written += ret;
  170. }
  171. return written;
  172. }