file_access_unix_pipe.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/stat.h>
  37. #include <sys/types.h>
  38. #include <unistd.h>
  39. Error FileAccessUnixPipe::open_existing(int p_rfd, int p_wfd, bool p_blocking) {
  40. // Open pipe using handles created by pipe(fd) call in the OS.execute_with_pipe.
  41. _close();
  42. path_src = String();
  43. unlink_on_close = false;
  44. ERR_FAIL_COND_V_MSG(fd[0] >= 0 || fd[1] >= 0, ERR_ALREADY_IN_USE, "Pipe is already in use.");
  45. fd[0] = p_rfd;
  46. fd[1] = p_wfd;
  47. if (!p_blocking) {
  48. fcntl(fd[0], F_SETFL, fcntl(fd[0], F_GETFL) | O_NONBLOCK);
  49. fcntl(fd[1], F_SETFL, fcntl(fd[1], F_GETFL) | O_NONBLOCK);
  50. }
  51. last_error = OK;
  52. return OK;
  53. }
  54. Error FileAccessUnixPipe::open_internal(const String &p_path, int p_mode_flags) {
  55. _close();
  56. path_src = p_path;
  57. ERR_FAIL_COND_V_MSG(fd[0] >= 0 || fd[1] >= 0, ERR_ALREADY_IN_USE, "Pipe is already in use.");
  58. path = String("/tmp/") + p_path.replace("pipe://", "").replace("/", "_");
  59. struct stat st = {};
  60. int err = stat(path.utf8().get_data(), &st);
  61. if (err) {
  62. if (mkfifo(path.utf8().get_data(), 0666) != 0) {
  63. last_error = ERR_FILE_CANT_OPEN;
  64. return last_error;
  65. }
  66. unlink_on_close = true;
  67. } else {
  68. ERR_FAIL_COND_V_MSG(!S_ISFIFO(st.st_mode), ERR_ALREADY_IN_USE, "Pipe name is already used by file.");
  69. }
  70. int f = ::open(path.utf8().get_data(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
  71. if (f < 0) {
  72. switch (errno) {
  73. case ENOENT: {
  74. last_error = ERR_FILE_NOT_FOUND;
  75. } break;
  76. default: {
  77. last_error = ERR_FILE_CANT_OPEN;
  78. } break;
  79. }
  80. return last_error;
  81. }
  82. // Set close on exec to avoid leaking it to subprocesses.
  83. fd[0] = f;
  84. fd[1] = f;
  85. last_error = OK;
  86. return OK;
  87. }
  88. void FileAccessUnixPipe::_close() {
  89. if (fd[0] < 0) {
  90. return;
  91. }
  92. if (fd[1] != fd[0]) {
  93. ::close(fd[1]);
  94. }
  95. ::close(fd[0]);
  96. fd[0] = -1;
  97. fd[1] = -1;
  98. if (unlink_on_close) {
  99. ::unlink(path.utf8().ptr());
  100. }
  101. unlink_on_close = false;
  102. }
  103. bool FileAccessUnixPipe::is_open() const {
  104. return (fd[0] >= 0 || fd[1] >= 0);
  105. }
  106. String FileAccessUnixPipe::get_path() const {
  107. return path_src;
  108. }
  109. String FileAccessUnixPipe::get_path_absolute() const {
  110. return path_src;
  111. }
  112. uint64_t FileAccessUnixPipe::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  113. ERR_FAIL_COND_V_MSG(fd[0] < 0, -1, "Pipe must be opened before use.");
  114. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  115. ssize_t read = ::read(fd[0], p_dst, p_length);
  116. if (read == -1) {
  117. last_error = ERR_FILE_CANT_READ;
  118. read = 0;
  119. } else if (read != (ssize_t)p_length) {
  120. last_error = ERR_FILE_CANT_READ;
  121. } else {
  122. last_error = OK;
  123. }
  124. return read;
  125. }
  126. Error FileAccessUnixPipe::get_error() const {
  127. return last_error;
  128. }
  129. void FileAccessUnixPipe::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  130. ERR_FAIL_COND_MSG(fd[1] < 0, "Pipe must be opened before use.");
  131. ERR_FAIL_COND(!p_src && p_length > 0);
  132. if (::write(fd[1], p_src, p_length) != (ssize_t)p_length) {
  133. last_error = ERR_FILE_CANT_WRITE;
  134. } else {
  135. last_error = OK;
  136. }
  137. }
  138. void FileAccessUnixPipe::close() {
  139. _close();
  140. }
  141. FileAccessUnixPipe::~FileAccessUnixPipe() {
  142. _close();
  143. }
  144. #endif // UNIX_ENABLED