Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/unix/file_access_unix_pipe.cpp
9903 views
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
31
#include "file_access_unix_pipe.h"
32
33
#if defined(UNIX_ENABLED)
34
35
#include "core/os/os.h"
36
#include "core/string/print_string.h"
37
38
#include <fcntl.h>
39
#include <sys/ioctl.h>
40
#include <sys/stat.h>
41
#include <sys/types.h>
42
#include <unistd.h>
43
#include <cerrno>
44
45
Error FileAccessUnixPipe::open_existing(int p_rfd, int p_wfd, bool p_blocking) {
46
// Open pipe using handles created by pipe(fd) call in the OS.execute_with_pipe.
47
_close();
48
49
path_src = String();
50
unlink_on_close = false;
51
ERR_FAIL_COND_V_MSG(fd[0] >= 0 || fd[1] >= 0, ERR_ALREADY_IN_USE, "Pipe is already in use.");
52
fd[0] = p_rfd;
53
fd[1] = p_wfd;
54
55
if (!p_blocking) {
56
fcntl(fd[0], F_SETFL, fcntl(fd[0], F_GETFL) | O_NONBLOCK);
57
fcntl(fd[1], F_SETFL, fcntl(fd[1], F_GETFL) | O_NONBLOCK);
58
}
59
60
last_error = OK;
61
return OK;
62
}
63
64
Error FileAccessUnixPipe::open_internal(const String &p_path, int p_mode_flags) {
65
_close();
66
67
path_src = p_path;
68
ERR_FAIL_COND_V_MSG(fd[0] >= 0 || fd[1] >= 0, ERR_ALREADY_IN_USE, "Pipe is already in use.");
69
70
path = String("/tmp/") + p_path.replace("pipe://", "").replace_char('/', '_');
71
const CharString path_utf8 = path.utf8();
72
73
struct stat st = {};
74
int err = stat(path_utf8.get_data(), &st);
75
if (err) {
76
if (mkfifo(path_utf8.get_data(), 0600) != 0) {
77
last_error = ERR_FILE_CANT_OPEN;
78
return last_error;
79
}
80
unlink_on_close = true;
81
} else {
82
ERR_FAIL_COND_V_MSG(!S_ISFIFO(st.st_mode), ERR_ALREADY_IN_USE, "Pipe name is already used by file.");
83
}
84
85
int f = ::open(path_utf8.get_data(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
86
if (f < 0) {
87
switch (errno) {
88
case ENOENT: {
89
last_error = ERR_FILE_NOT_FOUND;
90
} break;
91
default: {
92
last_error = ERR_FILE_CANT_OPEN;
93
} break;
94
}
95
return last_error;
96
}
97
98
// Set close on exec to avoid leaking it to subprocesses.
99
fd[0] = f;
100
fd[1] = f;
101
102
last_error = OK;
103
return OK;
104
}
105
106
void FileAccessUnixPipe::_close() {
107
if (fd[0] < 0) {
108
return;
109
}
110
111
if (fd[1] != fd[0]) {
112
::close(fd[1]);
113
}
114
::close(fd[0]);
115
fd[0] = -1;
116
fd[1] = -1;
117
118
if (unlink_on_close) {
119
::unlink(path.utf8().ptr());
120
}
121
unlink_on_close = false;
122
}
123
124
bool FileAccessUnixPipe::is_open() const {
125
return (fd[0] >= 0 || fd[1] >= 0);
126
}
127
128
String FileAccessUnixPipe::get_path() const {
129
return path_src;
130
}
131
132
String FileAccessUnixPipe::get_path_absolute() const {
133
return path_src;
134
}
135
136
uint64_t FileAccessUnixPipe::get_length() const {
137
ERR_FAIL_COND_V_MSG(fd[0] < 0, 0, "Pipe must be opened before use.");
138
139
int buf_rem = 0;
140
ERR_FAIL_COND_V(ioctl(fd[0], FIONREAD, &buf_rem) != 0, 0);
141
return buf_rem;
142
}
143
144
uint64_t FileAccessUnixPipe::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
145
ERR_FAIL_COND_V_MSG(fd[0] < 0, -1, "Pipe must be opened before use.");
146
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
147
148
ssize_t read = ::read(fd[0], p_dst, p_length);
149
if (read == -1) {
150
last_error = ERR_FILE_CANT_READ;
151
read = 0;
152
} else if (read != (ssize_t)p_length) {
153
last_error = ERR_FILE_CANT_READ;
154
} else {
155
last_error = OK;
156
}
157
return read;
158
}
159
160
Error FileAccessUnixPipe::get_error() const {
161
return last_error;
162
}
163
164
bool FileAccessUnixPipe::store_buffer(const uint8_t *p_src, uint64_t p_length) {
165
ERR_FAIL_COND_V_MSG(fd[1] < 0, false, "Pipe must be opened before use.");
166
ERR_FAIL_COND_V(!p_src && p_length > 0, false);
167
168
if (::write(fd[1], p_src, p_length) != (ssize_t)p_length) {
169
last_error = ERR_FILE_CANT_WRITE;
170
return false;
171
} else {
172
last_error = OK;
173
return true;
174
}
175
}
176
177
void FileAccessUnixPipe::close() {
178
_close();
179
}
180
181
FileAccessUnixPipe::~FileAccessUnixPipe() {
182
_close();
183
}
184
185
#endif // UNIX_ENABLED
186
187