summaryrefslogtreecommitdiff
path: root/drivers/unix
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/unix')
-rw-r--r--drivers/unix/file_access_unix.cpp51
-rw-r--r--drivers/unix/ip_unix.cpp2
-rw-r--r--drivers/unix/net_socket_posix.cpp26
-rw-r--r--drivers/unix/net_socket_posix.h1
-rw-r--r--drivers/unix/os_unix.cpp8
-rw-r--r--drivers/unix/semaphore_posix.cpp2
-rw-r--r--drivers/unix/semaphore_posix.h2
7 files changed, 70 insertions, 22 deletions
diff --git a/drivers/unix/file_access_unix.cpp b/drivers/unix/file_access_unix.cpp
index 071734eb48..e3026f9fd9 100644
--- a/drivers/unix/file_access_unix.cpp
+++ b/drivers/unix/file_access_unix.cpp
@@ -56,9 +56,15 @@
#define S_ISREG(m) ((m)&S_IFREG)
#endif
+#ifndef NO_FCNTL
+#include <fcntl.h>
+#else
+#include <sys/ioctl.h>
+#endif
+
void FileAccessUnix::check_errors() const {
- ERR_FAIL_COND(!f);
+ ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
if (feof(f)) {
@@ -76,7 +82,7 @@ Error FileAccessUnix::_open(const String &p_path, int p_mode_flags) {
path = fix_path(p_path);
//printf("opening %ls, %i\n", path.c_str(), Memory::get_static_mem_usage());
- ERR_FAIL_COND_V(f, ERR_ALREADY_IN_USE);
+ ERR_FAIL_COND_V_MSG(f, ERR_ALREADY_IN_USE, "File is already in use.");
const char *mode_string;
if (p_mode_flags == READ)
@@ -123,11 +129,24 @@ Error FileAccessUnix::_open(const String &p_path, int p_mode_flags) {
} break;
}
return last_error;
- } else {
- last_error = OK;
- flags = p_mode_flags;
- return OK;
}
+
+ // Set close on exec to avoid leaking it to subprocesses.
+ int fd = fileno(f);
+
+ if (fd != -1) {
+#if defined(NO_FCNTL)
+ unsigned long par = 0;
+ ioctl(fd, FIOCLEX, &par);
+#else
+ int opts = fcntl(fd, F_GETFD);
+ fcntl(fd, F_SETFD, opts | FD_CLOEXEC);
+#endif
+ }
+
+ last_error = OK;
+ flags = p_mode_flags;
+ return OK;
}
void FileAccessUnix::close() {
@@ -171,7 +190,7 @@ String FileAccessUnix::get_path_absolute() const {
void FileAccessUnix::seek(size_t p_position) {
- ERR_FAIL_COND(!f);
+ ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
last_error = OK;
if (fseek(f, p_position, SEEK_SET))
@@ -180,7 +199,7 @@ void FileAccessUnix::seek(size_t p_position) {
void FileAccessUnix::seek_end(int64_t p_position) {
- ERR_FAIL_COND(!f);
+ ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
if (fseek(f, p_position, SEEK_END))
check_errors();
@@ -188,7 +207,7 @@ void FileAccessUnix::seek_end(int64_t p_position) {
size_t FileAccessUnix::get_position() const {
- ERR_FAIL_COND_V(!f, 0);
+ ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
long pos = ftell(f);
if (pos < 0) {
@@ -200,7 +219,7 @@ size_t FileAccessUnix::get_position() const {
size_t FileAccessUnix::get_len() const {
- ERR_FAIL_COND_V(!f, 0);
+ ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
long pos = ftell(f);
ERR_FAIL_COND_V(pos < 0, 0);
@@ -219,7 +238,7 @@ bool FileAccessUnix::eof_reached() const {
uint8_t FileAccessUnix::get_8() const {
- ERR_FAIL_COND_V(!f, 0);
+ ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
uint8_t b;
if (fread(&b, 1, 1, f) == 0) {
check_errors();
@@ -230,7 +249,7 @@ uint8_t FileAccessUnix::get_8() const {
int FileAccessUnix::get_buffer(uint8_t *p_dst, int p_length) const {
- ERR_FAIL_COND_V(!f, -1);
+ ERR_FAIL_COND_V_MSG(!f, -1, "File must be opened before use.");
int read = fread(p_dst, 1, p_length, f);
check_errors();
return read;
@@ -243,18 +262,20 @@ Error FileAccessUnix::get_error() const {
void FileAccessUnix::flush() {
- ERR_FAIL_COND(!f);
+ ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
fflush(f);
}
void FileAccessUnix::store_8(uint8_t p_dest) {
- ERR_FAIL_COND(!f);
+ ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
ERR_FAIL_COND(fwrite(&p_dest, 1, 1, f) != 1);
}
void FileAccessUnix::store_buffer(const uint8_t *p_src, int p_length) {
- ERR_FAIL_COND(!f);
+
+ ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
+ ERR_FAIL_COND(!p_src);
ERR_FAIL_COND((int)fwrite(p_src, 1, p_length, f) != p_length);
}
diff --git a/drivers/unix/ip_unix.cpp b/drivers/unix/ip_unix.cpp
index ac7195abc1..cf47cdc7e8 100644
--- a/drivers/unix/ip_unix.cpp
+++ b/drivers/unix/ip_unix.cpp
@@ -152,7 +152,7 @@ void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) co
Interface_Info info;
info.name = name;
info.name_friendly = hostname->DisplayName->Data();
- info.index = 0;
+ info.index = String::num_uint64(0);
E = r_interfaces->insert(name, info);
ERR_CONTINUE(!E);
}
diff --git a/drivers/unix/net_socket_posix.cpp b/drivers/unix/net_socket_posix.cpp
index 6a57a2e562..5f99a40c79 100644
--- a/drivers/unix/net_socket_posix.cpp
+++ b/drivers/unix/net_socket_posix.cpp
@@ -30,6 +30,7 @@
#include "net_socket_posix.h"
+#ifndef UNIX_SOCKET_UNAVAILABLE
#if defined(UNIX_ENABLED)
#include <errno.h>
@@ -69,6 +70,7 @@
#define SOCK_CBUF(x) x
#define SOCK_IOCTL ioctl
#define SOCK_CLOSE ::close
+#define SOCK_CONNECT(p_sock, p_addr, p_addr_len) ::connect(p_sock, p_addr, p_addr_len)
/* Windows */
#elif defined(WINDOWS_ENABLED)
@@ -82,6 +84,9 @@
#define SOCK_CBUF(x) (const char *)(x)
#define SOCK_IOCTL ioctlsocket
#define SOCK_CLOSE closesocket
+// connect is broken on windows under certain conditions, reasons unknown:
+// See https://github.com/godotengine/webrtc-native/issues/6
+#define SOCK_CONNECT(p_sock, p_addr, p_addr_len) ::WSAConnect(p_sock, p_addr, p_addr_len, NULL, NULL, NULL, NULL)
// Workaround missing flag in MinGW
#if defined(__MINGW32__) && !defined(SIO_UDP_NETRESET)
@@ -280,6 +285,21 @@ void NetSocketPosix::_set_socket(SOCKET_TYPE p_sock, IP::Type p_ip_type, bool p_
_sock = p_sock;
_ip_type = p_ip_type;
_is_stream = p_is_stream;
+ // Disable descriptor sharing with subprocesses.
+ _set_close_exec_enabled(true);
+}
+
+void NetSocketPosix::_set_close_exec_enabled(bool p_enabled) {
+#ifndef WINDOWS_ENABLED
+ // Enable close on exec to avoid sharing with subprocesses. Off by default on Windows.
+#if defined(NO_FCNTL)
+ unsigned long par = p_enabled ? 1 : 0;
+ SOCK_IOCTL(_sock, FIOCLEX, &par);
+#else
+ int opts = fcntl(_sock, F_GETFD);
+ fcntl(_sock, F_SETFD, opts | FD_CLOEXEC);
+#endif
+#endif
}
Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) {
@@ -320,6 +340,9 @@ Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) {
_is_stream = p_sock_type == TYPE_TCP;
+ // Disable descriptor sharing with subprocesses.
+ _set_close_exec_enabled(true);
+
#if defined(WINDOWS_ENABLED)
if (!_is_stream) {
// Disable windows feature/bug reporting WSAECONNRESET/WSAENETRESET when
@@ -390,7 +413,7 @@ Error NetSocketPosix::connect_to_host(IP_Address p_host, uint16_t p_port) {
struct sockaddr_storage addr;
size_t addr_size = _set_addr_storage(&addr, p_host, p_port, _ip_type);
- if (::connect(_sock, (struct sockaddr *)&addr, addr_size) != 0) {
+ if (SOCK_CONNECT(_sock, (struct sockaddr *)&addr, addr_size) != 0) {
NetError err = _get_socket_error();
@@ -691,3 +714,4 @@ Error NetSocketPosix::join_multicast_group(const IP_Address &p_multi_address, St
Error NetSocketPosix::leave_multicast_group(const IP_Address &p_multi_address, String p_if_name) {
return _change_multicast_group(p_multi_address, p_if_name, false);
}
+#endif
diff --git a/drivers/unix/net_socket_posix.h b/drivers/unix/net_socket_posix.h
index 40406b241a..e549ea1d6a 100644
--- a/drivers/unix/net_socket_posix.h
+++ b/drivers/unix/net_socket_posix.h
@@ -61,6 +61,7 @@ private:
NetError _get_socket_error();
void _set_socket(SOCKET_TYPE p_sock, IP::Type p_ip_type, bool p_is_stream);
_FORCE_INLINE_ Error _change_multicast_group(IP_Address p_ip, String p_if_name, bool p_add);
+ _FORCE_INLINE_ void _set_close_exec_enabled(bool p_enabled);
protected:
static NetSocket *_create_func();
diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp
index ab5590dba4..25dee6aedb 100644
--- a/drivers/unix/os_unix.cpp
+++ b/drivers/unix/os_unix.cpp
@@ -126,7 +126,9 @@ void OS_Unix::initialize_core() {
RWLockDummy::make_default();
#else
ThreadPosix::make_default();
+#if !defined(OSX_ENABLED) && !defined(IPHONE_ENABLED)
SemaphorePosix::make_default();
+#endif
MutexPosix::make_default();
RWLockPosix::make_default();
#endif
@@ -187,7 +189,7 @@ uint64_t OS_Unix::get_system_time_secs() const {
uint64_t OS_Unix::get_system_time_msecs() const {
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
- return uint64_t(tv_now.tv_sec * 1000 + tv_now.tv_usec / 1000);
+ return uint64_t(tv_now.tv_sec) * 1000 + uint64_t(tv_now.tv_usec) / 1000;
}
OS::Date OS_Unix::get_date(bool utc) const {
@@ -298,7 +300,7 @@ Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, bo
}
FILE *f = popen(argss.utf8().get_data(), "r");
- ERR_FAIL_COND_V(!f, ERR_CANT_OPEN);
+ ERR_FAIL_COND_V_MSG(!f, ERR_CANT_OPEN, "Cannot pipe stream from process running with following arguments '" + argss + "'.");
char buf[65535];
@@ -314,7 +316,7 @@ Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, bo
}
int rv = pclose(f);
if (r_exitcode)
- *r_exitcode = rv;
+ *r_exitcode = WEXITSTATUS(rv);
return OK;
}
diff --git a/drivers/unix/semaphore_posix.cpp b/drivers/unix/semaphore_posix.cpp
index 5aa51d77d1..fc2d5b0dfe 100644
--- a/drivers/unix/semaphore_posix.cpp
+++ b/drivers/unix/semaphore_posix.cpp
@@ -30,7 +30,7 @@
#include "semaphore_posix.h"
-#if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED)
+#if (defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED)) && !defined(OSX_ENABLED) && !defined(IPHONE_ENABLED)
#include "core/os/memory.h"
#include <errno.h>
diff --git a/drivers/unix/semaphore_posix.h b/drivers/unix/semaphore_posix.h
index 83e75c9a82..8aff01fc27 100644
--- a/drivers/unix/semaphore_posix.h
+++ b/drivers/unix/semaphore_posix.h
@@ -33,7 +33,7 @@
#include "core/os/semaphore.h"
-#if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED)
+#if (defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED)) && !defined(OSX_ENABLED) && !defined(IPHONE_ENABLED)
#include <semaphore.h>