file_access_unix_pipe.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**************************************************************************/
  2. /* file_access_unix_pipe.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "file_access_unix_pipe.h"
  31. #if defined(UNIX_ENABLED)
  32. #include "core/os/os.h"
  33. #include "core/string/print_string.h"
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <sys/ioctl.h>
  37. #include <sys/stat.h>
  38. #include <sys/types.h>
  39. #include <unistd.h>
  40. Error FileAccessUnixPipe::open_existing(int p_rfd, int p_wfd, bool p_blocking) {
  41. // Open pipe using handles created by pipe(fd) call in the OS.execute_with_pipe.
  42. _close();
  43. path_src = String();
  44. unlink_on_close = false;
  45. ERR_FAIL_COND_V_MSG(fd[0] >= 0 || fd[1] >= 0, ERR_ALREADY_IN_USE, "Pipe is already in use.");
  46. fd[0] = p_rfd;
  47. fd[1] = p_wfd;
  48. if (!p_blocking) {
  49. fcntl(fd[0], F_SETFL, fcntl(fd[0], F_GETFL) | O_NONBLOCK);
  50. fcntl(fd[1], F_SETFL, fcntl(fd[1], F_GETFL) | O_NONBLOCK);
  51. }
  52. last_error = OK;
  53. return OK;
  54. }
  55. Error FileAccessUnixPipe::open_internal(const String &p_path, int p_mode_flags) {
  56. _close();
  57. path_src = p_path;
  58. ERR_FAIL_COND_V_MSG(fd[0] >= 0 || fd[1] >= 0, ERR_ALREADY_IN_USE, "Pipe is already in use.");
  59. path = String("/tmp/") + p_path.replace("pipe://", "").replace("/", "_");
  60. const CharString path_utf8 = path.utf8();
  61. struct stat st = {};
  62. int err = stat(path_utf8.get_data(), &st);
  63. if (err) {
  64. if (mkfifo(path_utf8.get_data(), 0600) != 0) {
  65. last_error = ERR_FILE_CANT_OPEN;
  66. return last_error;
  67. }
  68. unlink_on_close = true;
  69. } else {
  70. ERR_FAIL_COND_V_MSG(!S_ISFIFO(st.st_mode), ERR_ALREADY_IN_USE, "Pipe name is already used by file.");
  71. }
  72. int f = ::open(path_utf8.get_data(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
  73. if (f < 0) {
  74. switch (errno) {
  75. case ENOENT: {
  76. last_error = ERR_FILE_NOT_FOUND;
  77. } break;
  78. default: {
  79. last_error = ERR_FILE_CANT_OPEN;
  80. } break;
  81. }
  82. return last_error;
  83. }
  84. // Set close on exec to avoid leaking it to subprocesses.
  85. fd[0] = f;
  86. fd[1] = f;
  87. last_error = OK;
  88. return OK;
  89. }
  90. void FileAccessUnixPipe::_close() {
  91. if (fd[0] < 0) {
  92. return;
  93. }
  94. if (fd[1] != fd[0]) {
  95. ::close(fd[1]);
  96. }
  97. ::close(fd[0]);
  98. fd[0] = -1;
  99. fd[1] = -1;
  100. if (unlink_on_close) {
  101. ::unlink(path.utf8().ptr());
  102. }
  103. unlink_on_close = false;
  104. }
  105. bool FileAccessUnixPipe::is_open() const {
  106. return (fd[0] >= 0 || fd[1] >= 0);
  107. }
  108. String FileAccessUnixPipe::get_path() const {
  109. return path_src;
  110. }
  111. String FileAccessUnixPipe::get_path_absolute() const {
  112. return path_src;
  113. }
  114. uint64_t FileAccessUnixPipe::get_length() const {
  115. ERR_FAIL_COND_V_MSG(fd[0] < 0, 0, "Pipe must be opened before use.");
  116. int buf_rem = 0;
  117. ERR_FAIL_COND_V(ioctl(fd[0], FIONREAD, &buf_rem) != 0, 0);
  118. return buf_rem;
  119. }
  120. uint64_t FileAccessUnixPipe::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  121. ERR_FAIL_COND_V_MSG(fd[0] < 0, -1, "Pipe must be opened before use.");
  122. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  123. ssize_t read = ::read(fd[0], p_dst, p_length);
  124. if (read == -1) {
  125. last_error = ERR_FILE_CANT_READ;
  126. read = 0;
  127. } else if (read != (ssize_t)p_length) {
  128. last_error = ERR_FILE_CANT_READ;
  129. } else {
  130. last_error = OK;
  131. }
  132. return read;
  133. }
  134. Error FileAccessUnixPipe::get_error() const {
  135. return last_error;
  136. }
  137. bool FileAccessUnixPipe::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  138. ERR_FAIL_COND_V_MSG(fd[1] < 0, false, "Pipe must be opened before use.");
  139. ERR_FAIL_COND_V(!p_src && p_length > 0, false);
  140. if (::write(fd[1], p_src, p_length) != (ssize_t)p_length) {
  141. last_error = ERR_FILE_CANT_WRITE;
  142. return false;
  143. } else {
  144. last_error = OK;
  145. return true;
  146. }
  147. }
  148. void FileAccessUnixPipe::close() {
  149. _close();
  150. }
  151. FileAccessUnixPipe::~FileAccessUnixPipe() {
  152. _close();
  153. }
  154. #endif // UNIX_ENABLED