pipe-posix.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. extern char **environ;
  25. struct os_process_pipe {
  26. bool read_pipe;
  27. int pid;
  28. FILE *file;
  29. FILE *err_file;
  30. };
  31. os_process_pipe_t *os_process_pipe_create_internal(const char *bin, char **argv, 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(&file_actions, mainfds[1], STDOUT_FILENO);
  65. posix_spawn_file_actions_addclose(&file_actions, mainfds[0]);
  66. }
  67. } else {
  68. if (mainfds[0] != STDIN_FILENO) {
  69. posix_spawn_file_actions_adddup2(&file_actions, mainfds[0], STDIN_FILENO);
  70. posix_spawn_file_actions_addclose(&file_actions, mainfds[1]);
  71. }
  72. }
  73. posix_spawn_file_actions_addclose(&file_actions, errfds[0]);
  74. posix_spawn_file_actions_adddup2(&file_actions, errfds[1], STDERR_FILENO);
  75. int pid;
  76. int ret = posix_spawn(&pid, bin, &file_actions, NULL, (char *const *)argv, environ);
  77. posix_spawn_file_actions_destroy(&file_actions);
  78. if (ret != 0) {
  79. close(mainfds[0]);
  80. close(mainfds[1]);
  81. close(errfds[0]);
  82. close(errfds[1]);
  83. return NULL;
  84. }
  85. close(errfds[1]);
  86. process_pipe.err_file = fdopen(errfds[0], "r");
  87. if (process_pipe.read_pipe) {
  88. close(mainfds[1]);
  89. process_pipe.file = fdopen(mainfds[0], "r");
  90. } else {
  91. close(mainfds[0]);
  92. process_pipe.file = fdopen(mainfds[1], "w");
  93. }
  94. process_pipe.pid = pid;
  95. out = bmalloc(sizeof(os_process_pipe_t));
  96. *out = process_pipe;
  97. return out;
  98. }
  99. os_process_pipe_t *os_process_pipe_create(const char *cmd_line, const char *type)
  100. {
  101. if (!cmd_line)
  102. return NULL;
  103. char *argv[4] = {"sh", "-c", (char *)cmd_line, NULL};
  104. return os_process_pipe_create_internal("/bin/sh", argv, type);
  105. }
  106. os_process_pipe_t *os_process_pipe_create2(const os_process_args_t *args, const char *type)
  107. {
  108. char **argv = os_process_args_get_argv(args);
  109. return os_process_pipe_create_internal(argv[0], argv, type);
  110. }
  111. int os_process_pipe_destroy(os_process_pipe_t *pp)
  112. {
  113. int ret = 0;
  114. if (pp) {
  115. int status;
  116. fclose(pp->file);
  117. pp->file = NULL;
  118. fclose(pp->err_file);
  119. pp->err_file = NULL;
  120. do {
  121. ret = waitpid(pp->pid, &status, 0);
  122. } while (ret == -1 && errno == EINTR);
  123. if (WIFEXITED(status))
  124. ret = (int)(char)WEXITSTATUS(status);
  125. bfree(pp);
  126. }
  127. return ret;
  128. }
  129. size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len)
  130. {
  131. if (!pp) {
  132. return 0;
  133. }
  134. if (!pp->read_pipe) {
  135. return 0;
  136. }
  137. return fread(data, 1, len, pp->file);
  138. }
  139. size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data, size_t len)
  140. {
  141. if (!pp) {
  142. return 0;
  143. }
  144. return fread(data, 1, len, pp->err_file);
  145. }
  146. size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data, size_t len)
  147. {
  148. if (!pp) {
  149. return 0;
  150. }
  151. if (pp->read_pipe) {
  152. return 0;
  153. }
  154. size_t written = 0;
  155. while (written < len) {
  156. size_t ret = fwrite(data + written, 1, len - written, pp->file);
  157. if (!ret)
  158. return written;
  159. written += ret;
  160. }
  161. return written;
  162. }