pipe-posix.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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, const char *type)
  31. {
  32. struct os_process_pipe process_pipe = {0};
  33. struct os_process_pipe *out;
  34. posix_spawn_file_actions_t file_actions;
  35. if (!bin || !argv || !type) {
  36. return NULL;
  37. }
  38. process_pipe.read_pipe = *type == 'r';
  39. int mainfds[2] = {0};
  40. int errfds[2] = {0};
  41. if (pipe(mainfds) != 0) {
  42. return NULL;
  43. }
  44. if (pipe(errfds) != 0) {
  45. close(mainfds[0]);
  46. close(mainfds[1]);
  47. return NULL;
  48. }
  49. if (posix_spawn_file_actions_init(&file_actions) != 0) {
  50. close(mainfds[0]);
  51. close(mainfds[1]);
  52. close(errfds[0]);
  53. close(errfds[1]);
  54. return NULL;
  55. }
  56. fcntl(mainfds[0], F_SETFD, FD_CLOEXEC);
  57. fcntl(mainfds[1], F_SETFD, FD_CLOEXEC);
  58. fcntl(errfds[0], F_SETFD, FD_CLOEXEC);
  59. fcntl(errfds[1], F_SETFD, FD_CLOEXEC);
  60. if (process_pipe.read_pipe) {
  61. posix_spawn_file_actions_addclose(&file_actions, mainfds[0]);
  62. if (mainfds[1] != STDOUT_FILENO) {
  63. posix_spawn_file_actions_adddup2(&file_actions, mainfds[1], STDOUT_FILENO);
  64. posix_spawn_file_actions_addclose(&file_actions, mainfds[0]);
  65. }
  66. } else {
  67. if (mainfds[0] != STDIN_FILENO) {
  68. posix_spawn_file_actions_adddup2(&file_actions, mainfds[0], STDIN_FILENO);
  69. posix_spawn_file_actions_addclose(&file_actions, mainfds[1]);
  70. }
  71. }
  72. posix_spawn_file_actions_addclose(&file_actions, errfds[0]);
  73. posix_spawn_file_actions_adddup2(&file_actions, errfds[1], STDERR_FILENO);
  74. int pid;
  75. int ret = posix_spawn(&pid, bin, &file_actions, NULL, (char *const *)argv, NULL);
  76. posix_spawn_file_actions_destroy(&file_actions);
  77. if (ret != 0) {
  78. close(mainfds[0]);
  79. close(mainfds[1]);
  80. close(errfds[0]);
  81. close(errfds[1]);
  82. return NULL;
  83. }
  84. close(errfds[1]);
  85. process_pipe.err_file = fdopen(errfds[0], "r");
  86. if (process_pipe.read_pipe) {
  87. close(mainfds[1]);
  88. process_pipe.file = fdopen(mainfds[0], "r");
  89. } else {
  90. close(mainfds[0]);
  91. process_pipe.file = fdopen(mainfds[1], "w");
  92. }
  93. process_pipe.pid = pid;
  94. out = bmalloc(sizeof(os_process_pipe_t));
  95. *out = process_pipe;
  96. return out;
  97. }
  98. os_process_pipe_t *os_process_pipe_create(const char *cmd_line, const char *type)
  99. {
  100. if (!cmd_line)
  101. return NULL;
  102. char *argv[3] = {"-c", (char *)cmd_line, NULL};
  103. return os_process_pipe_create_internal("/bin/sh", argv, type);
  104. }
  105. os_process_pipe_t *os_process_pipe_create2(const os_process_args_t *args, const char *type)
  106. {
  107. char **argv = os_process_args_get_argv(args);
  108. return os_process_pipe_create_internal(argv[0], argv, type);
  109. }
  110. int os_process_pipe_destroy(os_process_pipe_t *pp)
  111. {
  112. int ret = 0;
  113. if (pp) {
  114. int status;
  115. fclose(pp->file);
  116. pp->file = NULL;
  117. fclose(pp->err_file);
  118. pp->err_file = NULL;
  119. do {
  120. ret = waitpid(pp->pid, &status, 0);
  121. } while (ret == -1 && errno == EINTR);
  122. if (WIFEXITED(status))
  123. ret = (int)(char)WEXITSTATUS(status);
  124. bfree(pp);
  125. }
  126. return ret;
  127. }
  128. size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len)
  129. {
  130. if (!pp) {
  131. return 0;
  132. }
  133. if (!pp->read_pipe) {
  134. return 0;
  135. }
  136. return fread(data, 1, len, pp->file);
  137. }
  138. size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data, size_t len)
  139. {
  140. if (!pp) {
  141. return 0;
  142. }
  143. return fread(data, 1, len, pp->err_file);
  144. }
  145. size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data, size_t len)
  146. {
  147. if (!pp) {
  148. return 0;
  149. }
  150. if (pp->read_pipe) {
  151. return 0;
  152. }
  153. size_t written = 0;
  154. while (written < len) {
  155. size_t ret = fwrite(data + written, 1, len - written, pp->file);
  156. if (!ret)
  157. return written;
  158. written += ret;
  159. }
  160. return written;
  161. }