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