diff options
Diffstat (limited to 'drivers/unix')
-rw-r--r-- | drivers/unix/dir_access_unix.cpp | 87 | ||||
-rw-r--r-- | drivers/unix/dir_access_unix.h | 6 | ||||
-rw-r--r-- | drivers/unix/file_access_unix.cpp | 67 | ||||
-rw-r--r-- | drivers/unix/file_access_unix.h | 13 | ||||
-rw-r--r-- | drivers/unix/ip_unix.cpp | 29 | ||||
-rw-r--r-- | drivers/unix/ip_unix.h | 8 | ||||
-rw-r--r-- | drivers/unix/net_socket_posix.cpp | 77 | ||||
-rw-r--r-- | drivers/unix/net_socket_posix.h | 11 | ||||
-rw-r--r-- | drivers/unix/os_unix.cpp | 227 | ||||
-rw-r--r-- | drivers/unix/os_unix.h | 58 | ||||
-rw-r--r-- | drivers/unix/rw_lock_posix.cpp | 15 | ||||
-rw-r--r-- | drivers/unix/rw_lock_posix.h | 5 | ||||
-rw-r--r-- | drivers/unix/syslog_logger.cpp | 31 | ||||
-rw-r--r-- | drivers/unix/syslog_logger.h | 4 | ||||
-rw-r--r-- | drivers/unix/thread_posix.cpp | 28 | ||||
-rw-r--r-- | drivers/unix/thread_posix.h | 5 |
16 files changed, 315 insertions, 356 deletions
diff --git a/drivers/unix/dir_access_unix.cpp b/drivers/unix/dir_access_unix.cpp index 00103684c7..5dc039afd9 100644 --- a/drivers/unix/dir_access_unix.cpp +++ b/drivers/unix/dir_access_unix.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -32,9 +32,9 @@ #if defined(UNIX_ENABLED) || defined(LIBC_FILEIO_ENABLED) -#include "core/list.h" #include "core/os/memory.h" -#include "core/print_string.h" +#include "core/string/print_string.h" +#include "core/templates/list.h" #include <errno.h> #include <stdio.h> @@ -50,12 +50,10 @@ #endif DirAccess *DirAccessUnix::create_fs() { - return memnew(DirAccessUnix); } Error DirAccessUnix::list_dir_begin() { - list_dir_end(); //close any previous dir opening! //char real_current_dir_name[2048]; //is this enough?! @@ -63,18 +61,19 @@ Error DirAccessUnix::list_dir_begin() { //chdir(current_path.utf8().get_data()); dir_stream = opendir(current_dir.utf8().get_data()); //chdir(real_current_dir_name); - if (!dir_stream) + if (!dir_stream) { return ERR_CANT_OPEN; //error! + } return OK; } bool DirAccessUnix::file_exists(String p_file) { - GLOBAL_LOCK_FUNCTION - if (p_file.is_rel_path()) + if (p_file.is_rel_path()) { p_file = current_dir.plus_file(p_file); + } p_file = fix_path(p_file); @@ -89,11 +88,11 @@ bool DirAccessUnix::file_exists(String p_file) { } bool DirAccessUnix::dir_exists(String p_dir) { - GLOBAL_LOCK_FUNCTION - if (p_dir.is_rel_path()) + if (p_dir.is_rel_path()) { p_dir = get_current_dir().plus_file(p_dir); + } p_dir = fix_path(p_dir); @@ -104,9 +103,9 @@ bool DirAccessUnix::dir_exists(String p_dir) { } uint64_t DirAccessUnix::get_modified_time(String p_file) { - - if (p_file.is_rel_path()) + if (p_file.is_rel_path()) { p_file = current_dir.plus_file(p_file); + } p_file = fix_path(p_file); @@ -116,16 +115,15 @@ uint64_t DirAccessUnix::get_modified_time(String p_file) { if (success) { return flags.st_mtime; } else { - ERR_FAIL_V(0); }; return 0; }; String DirAccessUnix::get_next() { - - if (!dir_stream) + if (!dir_stream) { return ""; + } dirent *entry = readdir(dir_stream); @@ -154,25 +152,23 @@ String DirAccessUnix::get_next() { _cisdir = (entry->d_type == DT_DIR); } - _cishidden = (fname != "." && fname != ".." && fname.begins_with(".")); + _cishidden = is_hidden(fname); return fname; } bool DirAccessUnix::current_is_dir() const { - return _cisdir; } bool DirAccessUnix::current_is_hidden() const { - return _cishidden; } void DirAccessUnix::list_dir_end() { - - if (dir_stream) + if (dir_stream) { closedir(dir_stream); + } dir_stream = nullptr; _cisdir = false; } @@ -198,7 +194,6 @@ static bool _filter_drive(struct mntent *mnt) { #endif static void _get_drives(List<String> *list) { - #if defined(HAVE_MNTENT) && defined(X11_ENABLED) // Check /etc/mtab for the list of mounted partitions FILE *mtab = setmntent("/etc/mtab", "r"); @@ -252,7 +247,6 @@ static void _get_drives(List<String> *list) { } int DirAccessUnix::get_drive_count() { - List<String> list; _get_drives(&list); @@ -260,7 +254,6 @@ int DirAccessUnix::get_drive_count() { } String DirAccessUnix::get_drive(int p_drive) { - List<String> list; _get_drives(&list); @@ -270,16 +263,15 @@ String DirAccessUnix::get_drive(int p_drive) { } bool DirAccessUnix::drives_are_shortcuts() { - return true; } Error DirAccessUnix::make_dir(String p_dir) { - GLOBAL_LOCK_FUNCTION - if (p_dir.is_rel_path()) + if (p_dir.is_rel_path()) { p_dir = get_current_dir().plus_file(p_dir); + } p_dir = fix_path(p_dir); @@ -298,7 +290,6 @@ Error DirAccessUnix::make_dir(String p_dir) { } Error DirAccessUnix::change_dir(String p_dir) { - GLOBAL_LOCK_FUNCTION p_dir = fix_path(p_dir); @@ -307,8 +298,9 @@ Error DirAccessUnix::change_dir(String p_dir) { String prev_dir; char real_current_dir_name[2048]; ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == nullptr, ERR_BUG); - if (prev_dir.parse_utf8(real_current_dir_name)) + if (prev_dir.parse_utf8(real_current_dir_name)) { prev_dir = real_current_dir_name; //no utf8, maybe latin? + } // try_dir is the directory we are trying to change into String try_dir = ""; @@ -343,28 +335,28 @@ Error DirAccessUnix::change_dir(String p_dir) { } String DirAccessUnix::get_current_dir(bool p_include_drive) { - String base = _get_root_path(); if (base != "") { - String bd = current_dir.replace_first(base, ""); - if (bd.begins_with("/")) + if (bd.begins_with("/")) { return _get_root_string() + bd.substr(1, bd.length()); - else + } else { return _get_root_string() + bd; + } } return current_dir; } Error DirAccessUnix::rename(String p_path, String p_new_path) { - - if (p_path.is_rel_path()) + if (p_path.is_rel_path()) { p_path = get_current_dir().plus_file(p_path); + } p_path = fix_path(p_path); - if (p_new_path.is_rel_path()) + if (p_new_path.is_rel_path()) { p_new_path = get_current_dir().plus_file(p_new_path); + } p_new_path = fix_path(p_new_path); @@ -372,28 +364,28 @@ Error DirAccessUnix::rename(String p_path, String p_new_path) { } Error DirAccessUnix::remove(String p_path) { - - if (p_path.is_rel_path()) + if (p_path.is_rel_path()) { p_path = get_current_dir().plus_file(p_path); + } p_path = fix_path(p_path); struct stat flags; - if ((stat(p_path.utf8().get_data(), &flags) != 0)) + if ((stat(p_path.utf8().get_data(), &flags) != 0)) { return FAILED; + } - if (S_ISDIR(flags.st_mode)) + if (S_ISDIR(flags.st_mode)) { return ::rmdir(p_path.utf8().get_data()) == 0 ? OK : FAILED; - else + } else { return ::unlink(p_path.utf8().get_data()) == 0 ? OK : FAILED; + } } size_t DirAccessUnix::get_space_left() { - #ifndef NO_STATVFS struct statvfs vfs; if (statvfs(current_dir.utf8().get_data(), &vfs) != 0) { - return 0; }; @@ -408,8 +400,11 @@ String DirAccessUnix::get_filesystem_type() const { return ""; //TODO this should be implemented } -DirAccessUnix::DirAccessUnix() { +bool DirAccessUnix::is_hidden(const String &p_name) { + return p_name != "." && p_name != ".." && p_name.begins_with("."); +} +DirAccessUnix::DirAccessUnix() { dir_stream = nullptr; _cisdir = false; @@ -418,14 +413,14 @@ DirAccessUnix::DirAccessUnix() { // set current directory to an absolute path of the current directory char real_current_dir_name[2048]; ERR_FAIL_COND(getcwd(real_current_dir_name, 2048) == nullptr); - if (current_dir.parse_utf8(real_current_dir_name)) + if (current_dir.parse_utf8(real_current_dir_name)) { current_dir = real_current_dir_name; + } change_dir(current_dir); } DirAccessUnix::~DirAccessUnix() { - list_dir_end(); } diff --git a/drivers/unix/dir_access_unix.h b/drivers/unix/dir_access_unix.h index b403d8e356..b70df1ca02 100644 --- a/drivers/unix/dir_access_unix.h +++ b/drivers/unix/dir_access_unix.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -41,7 +41,6 @@ #include <unistd.h> class DirAccessUnix : public DirAccess { - DIR *dir_stream; static DirAccess *create_fs(); @@ -52,6 +51,7 @@ class DirAccessUnix : public DirAccess { protected: virtual String fix_unicode_name(const char *p_name) const { return String::utf8(p_name); } + virtual bool is_hidden(const String &p_name); public: virtual Error list_dir_begin(); ///< This starts dir listing diff --git a/drivers/unix/file_access_unix.cpp b/drivers/unix/file_access_unix.cpp index 4aa408a1f0..6b24a85ff6 100644 --- a/drivers/unix/file_access_unix.cpp +++ b/drivers/unix/file_access_unix.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -33,7 +33,7 @@ #if defined(UNIX_ENABLED) || defined(LIBC_FILEIO_ENABLED) #include "core/os/os.h" -#include "core/print_string.h" +#include "core/string/print_string.h" #include <sys/stat.h> #include <sys/types.h> @@ -63,38 +63,37 @@ #endif void FileAccessUnix::check_errors() const { - ERR_FAIL_COND_MSG(!f, "File must be opened before use."); if (feof(f)) { - last_error = ERR_FILE_EOF; } } Error FileAccessUnix::_open(const String &p_path, int p_mode_flags) { - - if (f) + if (f) { fclose(f); + } f = nullptr; path_src = p_path; path = fix_path(p_path); - //printf("opening %ls, %i\n", path.c_str(), Memory::get_static_mem_usage()); + //printf("opening %s, %i\n", path.utf8().get_data(), Memory::get_static_mem_usage()); ERR_FAIL_COND_V_MSG(f, ERR_ALREADY_IN_USE, "File is already in use."); const char *mode_string; - if (p_mode_flags == READ) + if (p_mode_flags == READ) { mode_string = "rb"; - else if (p_mode_flags == WRITE) + } else if (p_mode_flags == WRITE) { mode_string = "wb"; - else if (p_mode_flags == READ_WRITE) + } else if (p_mode_flags == READ_WRITE) { mode_string = "rb+"; - else if (p_mode_flags == WRITE_READ) + } else if (p_mode_flags == WRITE_READ) { mode_string = "wb+"; - else + } else { return ERR_INVALID_PARAMETER; + } /* pretty much every implementation that uses fopen as primary backend (unix-compatible mostly) supports utf8 encoding */ @@ -150,9 +149,9 @@ Error FileAccessUnix::_open(const String &p_path, int p_mode_flags) { } void FileAccessUnix::close() { - - if (!f) + if (!f) { return; + } fclose(f); f = nullptr; @@ -174,39 +173,35 @@ void FileAccessUnix::close() { } bool FileAccessUnix::is_open() const { - return (f != nullptr); } String FileAccessUnix::get_path() const { - return path_src; } String FileAccessUnix::get_path_absolute() const { - return path; } void FileAccessUnix::seek(size_t p_position) { - ERR_FAIL_COND_MSG(!f, "File must be opened before use."); last_error = OK; - if (fseek(f, p_position, SEEK_SET)) + if (fseek(f, p_position, SEEK_SET)) { check_errors(); + } } void FileAccessUnix::seek_end(int64_t p_position) { - ERR_FAIL_COND_MSG(!f, "File must be opened before use."); - if (fseek(f, p_position, SEEK_END)) + if (fseek(f, p_position, SEEK_END)) { check_errors(); + } } size_t FileAccessUnix::get_position() const { - ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); long pos = ftell(f); @@ -218,7 +213,6 @@ size_t FileAccessUnix::get_position() const { } size_t FileAccessUnix::get_len() const { - ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); long pos = ftell(f); @@ -232,12 +226,10 @@ size_t FileAccessUnix::get_len() const { } bool FileAccessUnix::eof_reached() const { - return last_error == ERR_FILE_EOF; } uint8_t FileAccessUnix::get_8() const { - ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); uint8_t b; if (fread(&b, 1, 1, f) == 0) { @@ -248,7 +240,6 @@ uint8_t FileAccessUnix::get_8() const { } int FileAccessUnix::get_buffer(uint8_t *p_dst, int p_length) const { - ERR_FAIL_COND_V_MSG(!f, -1, "File must be opened before use."); int read = fread(p_dst, 1, p_length, f); check_errors(); @@ -256,44 +247,41 @@ int FileAccessUnix::get_buffer(uint8_t *p_dst, int p_length) const { }; Error FileAccessUnix::get_error() const { - return last_error; } void FileAccessUnix::flush() { - ERR_FAIL_COND_MSG(!f, "File must be opened before use."); fflush(f); } void FileAccessUnix::store_8(uint8_t p_dest) { - 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_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); } bool FileAccessUnix::file_exists(const String &p_path) { - int err; struct stat st; String filename = fix_path(p_path); // Does the name exist at all? err = stat(filename.utf8().get_data(), &st); - if (err) + if (err) { return false; + } #ifdef UNIX_ENABLED // See if we have access to the file - if (access(filename.utf8().get_data(), F_OK)) + if (access(filename.utf8().get_data(), F_OK)) { return false; + } #else if (_access(filename.utf8().get_data(), 4) == -1) return false; @@ -310,7 +298,6 @@ bool FileAccessUnix::file_exists(const String &p_path) { } uint64_t FileAccessUnix::_get_modified_time(const String &p_file) { - String file = fix_path(p_file); struct stat flags; int err = stat(file.utf8().get_data(), &flags); @@ -323,7 +310,6 @@ uint64_t FileAccessUnix::_get_modified_time(const String &p_file) { } uint32_t FileAccessUnix::_get_unix_permissions(const String &p_file) { - String file = fix_path(p_file); struct stat flags; int err = stat(file.utf8().get_data(), &flags); @@ -336,7 +322,6 @@ uint32_t FileAccessUnix::_get_unix_permissions(const String &p_file) { } Error FileAccessUnix::_set_unix_permissions(const String &p_file, uint32_t p_permissions) { - String file = fix_path(p_file); int err = chmod(file.utf8().get_data(), p_permissions); @@ -348,20 +333,12 @@ Error FileAccessUnix::_set_unix_permissions(const String &p_file, uint32_t p_per } FileAccess *FileAccessUnix::create_libc() { - return memnew(FileAccessUnix); } CloseNotificationFunc FileAccessUnix::close_notification_func = nullptr; -FileAccessUnix::FileAccessUnix() : - f(nullptr), - flags(0), - last_error(OK) { -} - FileAccessUnix::~FileAccessUnix() { - close(); } diff --git a/drivers/unix/file_access_unix.h b/drivers/unix/file_access_unix.h index 8116f72345..998fad7909 100644 --- a/drivers/unix/file_access_unix.h +++ b/drivers/unix/file_access_unix.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -41,11 +41,10 @@ typedef void (*CloseNotificationFunc)(const String &p_file, int p_flags); class FileAccessUnix : public FileAccess { - - FILE *f; - int flags; + FILE *f = nullptr; + int flags = 0; void check_errors() const; - mutable Error last_error; + mutable Error last_error = OK; String save_path; String path; String path_src; @@ -84,7 +83,7 @@ public: virtual uint32_t _get_unix_permissions(const String &p_file); virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions); - FileAccessUnix(); + FileAccessUnix() {} virtual ~FileAccessUnix(); }; diff --git a/drivers/unix/ip_unix.cpp b/drivers/unix/ip_unix.cpp index 5e3dedfc2f..8ec1de4386 100644 --- a/drivers/unix/ip_unix.cpp +++ b/drivers/unix/ip_unix.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -76,7 +76,6 @@ #endif static IP_Address _sockaddr2ip(struct sockaddr *p_addr) { - IP_Address ip; if (p_addr->sa_family == AF_INET) { @@ -91,9 +90,8 @@ static IP_Address _sockaddr2ip(struct sockaddr *p_addr) { }; IP_Address IP_Unix::_resolve_hostname(const String &p_hostname, Type p_type) { - struct addrinfo hints; - struct addrinfo *result; + struct addrinfo *result = nullptr; memset(&hints, 0, sizeof(struct addrinfo)); if (p_type == TYPE_IPV4) { @@ -115,8 +113,9 @@ IP_Address IP_Unix::_resolve_hostname(const String &p_hostname, Type p_type) { if (result == nullptr || result->ai_addr == nullptr) { ERR_PRINT("Invalid response from getaddrinfo"); - if (result) + if (result) { freeaddrinfo(result); + } return IP_Address(); }; @@ -132,7 +131,6 @@ IP_Address IP_Unix::_resolve_hostname(const String &p_hostname, Type p_type) { #if defined(UWP_ENABLED) void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const { - using namespace Windows::Networking; using namespace Windows::Networking::Connectivity; @@ -140,7 +138,6 @@ void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) co auto hostnames = NetworkInformation::GetHostNames(); for (int i = 0; i < hostnames->Size; i++) { - auto hostname = hostnames->GetAt(i); if (hostname->Type != HostNameType::Ipv4 && hostname->Type != HostNameType::Ipv6) @@ -167,12 +164,10 @@ void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) co #else void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const { - ULONG buf_size = 1024; IP_ADAPTER_ADDRESSES *addrs; while (true) { - addrs = (IP_ADAPTER_ADDRESSES *)memalloc(buf_size); int err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME, nullptr, addrs, &buf_size); @@ -190,7 +185,6 @@ void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) co IP_ADAPTER_ADDRESSES *adapter = addrs; while (adapter != nullptr) { - Interface_Info info; info.name = adapter->AdapterName; info.name_friendly = adapter->FriendlyName; @@ -218,7 +212,6 @@ void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) co #else // UNIX void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const { - struct ifaddrs *ifAddrStruct = nullptr; struct ifaddrs *ifa = nullptr; int family; @@ -226,13 +219,15 @@ void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) co getifaddrs(&ifAddrStruct); for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) { - if (!ifa->ifa_addr) + if (!ifa->ifa_addr) { continue; + } family = ifa->ifa_addr->sa_family; - if (family != AF_INET && family != AF_INET6) + if (family != AF_INET && family != AF_INET6) { continue; + } Map<String, Interface_Info>::Element *E = r_interfaces->find(ifa->ifa_name); if (!E) { @@ -248,17 +243,17 @@ void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) co info.ip_addresses.push_front(_sockaddr2ip(ifa->ifa_addr)); } - if (ifAddrStruct != nullptr) freeifaddrs(ifAddrStruct); + if (ifAddrStruct != nullptr) { + freeifaddrs(ifAddrStruct); + } } #endif void IP_Unix::make_default() { - _create = _create_unix; } IP *IP_Unix::_create_unix() { - return memnew(IP_Unix); } diff --git a/drivers/unix/ip_unix.h b/drivers/unix/ip_unix.h index 0580facac2..ca2ee17f4e 100644 --- a/drivers/unix/ip_unix.h +++ b/drivers/unix/ip_unix.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -38,12 +38,12 @@ class IP_Unix : public IP { GDCLASS(IP_Unix, IP); - virtual IP_Address _resolve_hostname(const String &p_hostname, IP::Type p_type); + virtual IP_Address _resolve_hostname(const String &p_hostname, IP::Type p_type) override; static IP *_create_unix(); public: - virtual void get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const; + virtual void get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const override; static void make_default(); IP_Unix(); diff --git a/drivers/unix/net_socket_posix.cpp b/drivers/unix/net_socket_posix.cpp index 7c6543c3a2..19753943c8 100644 --- a/drivers/unix/net_socket_posix.cpp +++ b/drivers/unix/net_socket_posix.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -96,7 +96,6 @@ #endif size_t NetSocketPosix::_set_addr_storage(struct sockaddr_storage *p_addr, const IP_Address &p_ip, uint16_t p_port, IP::Type p_ip_type) { - memset(p_addr, 0, sizeof(struct sockaddr_storage)); if (p_ip_type == IP::TYPE_IPV6 || p_ip_type == IP::TYPE_ANY) { // IPv6 socket @@ -132,16 +131,13 @@ size_t NetSocketPosix::_set_addr_storage(struct sockaddr_storage *p_addr, const } void NetSocketPosix::_set_ip_port(struct sockaddr_storage *p_addr, IP_Address &r_ip, uint16_t &r_port) { - if (p_addr->ss_family == AF_INET) { - struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr; r_ip.set_ipv4((uint8_t *)&(addr4->sin_addr.s_addr)); r_port = ntohs(addr4->sin_port); } else if (p_addr->ss_family == AF_INET6) { - struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr; r_ip.set_ipv6(addr6->sin6_addr.s6_addr); @@ -173,9 +169,7 @@ void NetSocketPosix::cleanup() { } NetSocketPosix::NetSocketPosix() : - _sock(SOCK_EMPTY), - _ip_type(IP::TYPE_NONE), - _is_stream(false) { + _sock(SOCK_EMPTY) { } NetSocketPosix::~NetSocketPosix() { @@ -202,12 +196,15 @@ NetSocketPosix::NetError NetSocketPosix::_get_socket_error() const { print_verbose("Socket error: " + itos(err)); return ERR_NET_OTHER; #else - if (errno == EISCONN) + if (errno == EISCONN) { return ERR_NET_IS_CONNECTED; - if (errno == EINPROGRESS || errno == EALREADY) + } + if (errno == EINPROGRESS || errno == EALREADY) { return ERR_NET_IN_PROGRESS; - if (errno == EAGAIN || errno == EWOULDBLOCK) + } + if (errno == EAGAIN || errno == EWOULDBLOCK) { return ERR_NET_WOULD_BLOCK; + } print_verbose("Socket error: " + itos(errno)); return ERR_NET_OTHER; #endif @@ -218,7 +215,6 @@ NetSocketPosix::NetError NetSocketPosix::_get_socket_error() const { #endif bool NetSocketPosix::_can_use_ip(const IP_Address &p_ip, const bool p_for_bind) const { - if (p_for_bind && !(p_ip.is_valid() || p_ip.is_wildcard())) { return false; } else if (!p_for_bind && !p_ip.is_valid()) { @@ -230,7 +226,6 @@ bool NetSocketPosix::_can_use_ip(const IP_Address &p_ip, const bool p_for_bind) } _FORCE_INLINE_ Error NetSocketPosix::_change_multicast_group(IP_Address p_ip, String p_if_name, bool p_add) { - ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED); ERR_FAIL_COND_V(!_can_use_ip(p_ip, false), ERR_INVALID_PARAMETER); @@ -246,16 +241,19 @@ _FORCE_INLINE_ Error NetSocketPosix::_change_multicast_group(IP_Address p_ip, St IP::get_singleton()->get_local_interfaces(&if_info); for (Map<String, IP::Interface_Info>::Element *E = if_info.front(); E; E = E->next()) { IP::Interface_Info &c = E->get(); - if (c.name != p_if_name) + if (c.name != p_if_name) { continue; + } - if_v6id = (uint32_t)c.index.to_int64(); - if (type == IP::TYPE_IPV6) + if_v6id = (uint32_t)c.index.to_int(); + if (type == IP::TYPE_IPV6) { break; // IPv6 uses index. + } for (List<IP_Address>::Element *F = c.ip_addresses.front(); F; F = F->next()) { - if (!F->get().is_ipv4()) + if (!F->get().is_ipv4()) { continue; // Wrong IP type + } if_ip = F->get(); break; } @@ -350,11 +348,11 @@ Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) { // recv/recvfrom and an ICMP reply was received from a previous send/sendto. unsigned long disable = 0; if (ioctlsocket(_sock, SIO_UDP_CONNRESET, &disable) == SOCKET_ERROR) { - print_verbose("Unable to turn off UDP WSAECONNRESET behaviour on Windows"); + print_verbose("Unable to turn off UDP WSAECONNRESET behavior on Windows"); } if (ioctlsocket(_sock, SIO_UDP_NETRESET, &disable) == SOCKET_ERROR) { // This feature seems not to be supported on wine. - print_verbose("Unable to turn off UDP WSAENETRESET behaviour on Windows"); + print_verbose("Unable to turn off UDP WSAENETRESET behavior on Windows"); } } #endif @@ -369,9 +367,9 @@ Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) { } void NetSocketPosix::close() { - - if (_sock != SOCK_EMPTY) + if (_sock != SOCK_EMPTY) { SOCK_CLOSE(_sock); + } _sock = SOCK_EMPTY; _ip_type = IP::TYPE_NONE; @@ -379,7 +377,6 @@ void NetSocketPosix::close() { } Error NetSocketPosix::bind(IP_Address p_addr, uint16_t p_port) { - ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED); ERR_FAIL_COND_V(!_can_use_ip(p_addr, true), ERR_INVALID_PARAMETER); @@ -410,7 +407,6 @@ Error NetSocketPosix::listen(int p_max_pending) { } Error NetSocketPosix::connect_to_host(IP_Address p_host, uint16_t p_port) { - ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED); ERR_FAIL_COND_V(!_can_use_ip(p_host, false), ERR_INVALID_PARAMETER); @@ -418,7 +414,6 @@ Error NetSocketPosix::connect_to_host(IP_Address p_host, uint16_t p_port) { size_t addr_size = _set_addr_storage(&addr, p_host, p_port, _ip_type); if (SOCK_CONNECT(_sock, (struct sockaddr *)&addr, addr_size) != 0) { - NetError err = _get_socket_error(); switch (err) { @@ -440,7 +435,6 @@ Error NetSocketPosix::connect_to_host(IP_Address p_host, uint16_t p_port) { } Error NetSocketPosix::poll(PollType p_type, int p_timeout) const { - ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED); #if defined(WINDOWS_ENABLED) @@ -521,8 +515,9 @@ Error NetSocketPosix::poll(PollType p_type, int p_timeout) const { return FAILED; } - if (ret == 0) + if (ret == 0) { return ERR_BUSY; + } return OK; #endif @@ -535,8 +530,9 @@ Error NetSocketPosix::recv(uint8_t *p_buffer, int p_len, int &r_read) { if (r_read < 0) { NetError err = _get_socket_error(); - if (err == ERR_NET_WOULD_BLOCK) + if (err == ERR_NET_WOULD_BLOCK) { return ERR_BUSY; + } return FAILED; } @@ -555,8 +551,9 @@ Error NetSocketPosix::recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IP_Add if (r_read < 0) { NetError err = _get_socket_error(); - if (err == ERR_NET_WOULD_BLOCK) + if (err == ERR_NET_WOULD_BLOCK) { return ERR_BUSY; + } return FAILED; } @@ -582,15 +579,17 @@ Error NetSocketPosix::send(const uint8_t *p_buffer, int p_len, int &r_sent) { int flags = 0; #ifdef MSG_NOSIGNAL - if (_is_stream) + if (_is_stream) { flags = MSG_NOSIGNAL; + } #endif r_sent = ::send(_sock, SOCK_CBUF(p_buffer), p_len, flags); if (r_sent < 0) { NetError err = _get_socket_error(); - if (err == ERR_NET_WOULD_BLOCK) + if (err == ERR_NET_WOULD_BLOCK) { return ERR_BUSY; + } return FAILED; } @@ -607,8 +606,9 @@ Error NetSocketPosix::sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP if (r_sent < 0) { NetError err = _get_socket_error(); - if (err == ERR_NET_WOULD_BLOCK) + if (err == ERR_NET_WOULD_BLOCK) { return ERR_BUSY; + } return FAILED; } @@ -619,8 +619,9 @@ Error NetSocketPosix::sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP Error NetSocketPosix::set_broadcasting_enabled(bool p_enabled) { ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED); // IPv6 has no broadcast support. - if (_ip_type == IP::TYPE_IPV6) + if (_ip_type == IP::TYPE_IPV6) { return ERR_UNAVAILABLE; + } int par = p_enabled ? 1 : 0; if (setsockopt(_sock, SOL_SOCKET, SO_BROADCAST, SOCK_CBUF(&par), sizeof(int)) != 0) { @@ -639,14 +640,16 @@ void NetSocketPosix::set_blocking_enabled(bool p_enabled) { ret = SOCK_IOCTL(_sock, FIONBIO, &par); #else int opts = fcntl(_sock, F_GETFL); - if (p_enabled) + if (p_enabled) { ret = fcntl(_sock, F_SETFL, opts & ~O_NONBLOCK); - else + } else { ret = fcntl(_sock, F_SETFL, opts | O_NONBLOCK); + } #endif - if (ret != 0) + if (ret != 0) { WARN_PRINT("Unable to change non-block mode"); + } } void NetSocketPosix::set_ipv6_only_enabled(bool p_enabled) { @@ -701,7 +704,6 @@ bool NetSocketPosix::is_open() const { } int NetSocketPosix::get_available_bytes() const { - ERR_FAIL_COND_V(!is_open(), -1); unsigned long len; @@ -715,7 +717,6 @@ int NetSocketPosix::get_available_bytes() const { } Ref<NetSocket> NetSocketPosix::accept(IP_Address &r_ip, uint16_t &r_port) { - Ref<NetSocket> out; ERR_FAIL_COND_V(!is_open(), out); diff --git a/drivers/unix/net_socket_posix.h b/drivers/unix/net_socket_posix.h index 0a19967265..cc6af661c8 100644 --- a/drivers/unix/net_socket_posix.h +++ b/drivers/unix/net_socket_posix.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -45,11 +45,10 @@ #endif class NetSocketPosix : public NetSocket { - private: - SOCKET_TYPE _sock; - IP::Type _ip_type; - bool _is_stream; + SOCKET_TYPE _sock; // NOLINT - the default value is defined in the .cpp + IP::Type _ip_type = IP::TYPE_NONE; + bool _is_stream = false; enum NetError { ERR_NET_WOULD_BLOCK, diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index 53c60951b7..d94c2126ef 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -32,10 +32,10 @@ #ifdef UNIX_ENABLED +#include "core/config/project_settings.h" #include "core/debugger/engine_debugger.h" #include "core/debugger/script_debugger.h" #include "core/os/thread_dummy.h" -#include "core/project_settings.h" #include "drivers/unix/dir_access_unix.h" #include "drivers/unix/file_access_unix.h" #include "drivers/unix/net_socket_posix.h" @@ -48,7 +48,7 @@ #include <mach/mach_time.h> #endif -#if defined(__FreeBSD__) || defined(__OpenBSD__) +#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) #include <sys/param.h> #include <sys/sysctl.h> #endif @@ -91,20 +91,19 @@ static void _setup_clock() { #endif void OS_Unix::debug_break() { - assert(false); }; static void handle_interrupt(int sig) { - if (!EngineDebugger::is_active()) + if (!EngineDebugger::is_active()) { return; + } EngineDebugger::get_script_debugger()->set_depth(-1); EngineDebugger::get_script_debugger()->set_lines_left(1); } void OS_Unix::initialize_debugging() { - if (EngineDebugger::is_active()) { struct sigaction action; memset(&action, 0, sizeof(action)); @@ -114,12 +113,10 @@ void OS_Unix::initialize_debugging() { } int OS_Unix::unix_initialize_audio(int p_audio_driver) { - return 0; } void OS_Unix::initialize_core() { - #ifdef NO_THREADS ThreadDummy::make_default(); RWLockDummy::make_default(); @@ -130,7 +127,6 @@ void OS_Unix::initialize_core() { FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES); FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA); FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_FILESYSTEM); - //FileAccessBufferedFA<FileAccessUnix>::make_default(); DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES); DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA); DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM); @@ -144,17 +140,14 @@ void OS_Unix::initialize_core() { } void OS_Unix::finalize_core() { - NetSocketPosix::cleanup(); } void OS_Unix::alert(const String &p_alert, const String &p_title) { - fprintf(stderr, "ERROR: %s\n", p_alert.utf8().get_data()); } String OS_Unix::get_stdin_string(bool p_block) { - if (p_block) { char buff[1024]; String ret = stdin_buf + fgets(buff, 1024, stdin); @@ -166,74 +159,64 @@ String OS_Unix::get_stdin_string(bool p_block) { } String OS_Unix::get_name() const { - return "Unix"; } -uint64_t OS_Unix::get_unix_time() const { - - return time(nullptr); -}; - -uint64_t OS_Unix::get_system_time_secs() const { - struct timeval tv_now; - gettimeofday(&tv_now, nullptr); - return uint64_t(tv_now.tv_sec); -} - -uint64_t OS_Unix::get_system_time_msecs() const { +double OS_Unix::get_unix_time() const { struct timeval tv_now; gettimeofday(&tv_now, nullptr); - return uint64_t(tv_now.tv_sec) * 1000 + uint64_t(tv_now.tv_usec) / 1000; -} + return (double)tv_now.tv_sec + double(tv_now.tv_usec) / 1000000; +}; OS::Date OS_Unix::get_date(bool utc) const { - time_t t = time(nullptr); - struct tm *lt; - if (utc) - lt = gmtime(&t); - else - lt = localtime(&t); + struct tm lt; + if (utc) { + gmtime_r(&t, <); + } else { + localtime_r(&t, <); + } Date ret; - ret.year = 1900 + lt->tm_year; + ret.year = 1900 + lt.tm_year; // Index starting at 1 to match OS_Unix::get_date // and Windows SYSTEMTIME and tm_mon follows the typical structure // of 0-11, noted here: http://www.cplusplus.com/reference/ctime/tm/ - ret.month = (Month)(lt->tm_mon + 1); - ret.day = lt->tm_mday; - ret.weekday = (Weekday)lt->tm_wday; - ret.dst = lt->tm_isdst; + ret.month = (Month)(lt.tm_mon + 1); + ret.day = lt.tm_mday; + ret.weekday = (Weekday)lt.tm_wday; + ret.dst = lt.tm_isdst; return ret; } OS::Time OS_Unix::get_time(bool utc) const { time_t t = time(nullptr); - struct tm *lt; - if (utc) - lt = gmtime(&t); - else - lt = localtime(&t); + struct tm lt; + if (utc) { + gmtime_r(&t, <); + } else { + localtime_r(&t, <); + } Time ret; - ret.hour = lt->tm_hour; - ret.min = lt->tm_min; - ret.sec = lt->tm_sec; + ret.hour = lt.tm_hour; + ret.min = lt.tm_min; + ret.sec = lt.tm_sec; get_time_zone_info(); return ret; } OS::TimeZoneInfo OS_Unix::get_time_zone_info() const { time_t t = time(nullptr); - struct tm *lt = localtime(&t); + struct tm lt; + localtime_r(&t, <); char name[16]; - strftime(name, 16, "%Z", lt); + strftime(name, 16, "%Z", <); name[15] = 0; TimeZoneInfo ret; ret.name = name; char bias_buf[16]; - strftime(bias_buf, 16, "%z", lt); + strftime(bias_buf, 16, "%z", <); int bias; bias_buf[15] = 0; sscanf(bias_buf, "%d", &bias); @@ -241,22 +224,25 @@ OS::TimeZoneInfo OS_Unix::get_time_zone_info() const { // convert from ISO 8601 (1 minute=1, 1 hour=100) to minutes int hour = (int)bias / 100; int minutes = bias % 100; - if (bias < 0) + if (bias < 0) { ret.bias = hour * 60 - minutes; - else + } else { ret.bias = hour * 60 + minutes; + } return ret; } void OS_Unix::delay_usec(uint32_t p_usec) const { - - struct timespec rem = { static_cast<time_t>(p_usec / 1000000), (static_cast<long>(p_usec) % 1000000) * 1000 }; - while (nanosleep(&rem, &rem) == EINTR) { + struct timespec requested = { static_cast<time_t>(p_usec / 1000000), (static_cast<long>(p_usec) % 1000000) * 1000 }; + struct timespec remaining; + while (nanosleep(&requested, &remaining) == -1 && errno == EINTR) { + requested.tv_sec = remaining.tv_sec; + requested.tv_nsec = remaining.tv_nsec; } } -uint64_t OS_Unix::get_ticks_usec() const { +uint64_t OS_Unix::get_ticks_usec() const { #if defined(__APPLE__) uint64_t longtime = mach_absolute_time() * _clock_scale; #else @@ -271,36 +257,27 @@ uint64_t OS_Unix::get_ticks_usec() const { return longtime; } -Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex) { - +Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex) { #ifdef __EMSCRIPTEN__ // Don't compile this code at all to avoid undefined references. // Actual virtual call goes to OS_JavaScript. ERR_FAIL_V(ERR_BUG); #else - if (p_blocking && r_pipe) { - - String argss; - argss = "\"" + p_path + "\""; - + if (r_pipe) { + String command = "\"" + p_path + "\""; for (int i = 0; i < p_arguments.size(); i++) { - - argss += String(" \"") + p_arguments[i] + "\""; + command += String(" \"") + p_arguments[i] + "\""; } - if (read_stderr) { - argss += " 2>&1"; // Read stderr too + command += " 2>&1"; // Include stderr } else { - argss += " 2>/dev/null"; //silence stderr + command += " 2>/dev/null"; // Silence stderr } - FILE *f = popen(argss.utf8().get_data(), "r"); - - ERR_FAIL_COND_V_MSG(!f, ERR_CANT_OPEN, "Cannot pipe stream from process running with following arguments '" + argss + "'."); + FILE *f = popen(command.utf8().get_data(), "r"); + ERR_FAIL_COND_V_MSG(!f, ERR_CANT_OPEN, "Cannot create pipe from command: " + command); char buf[65535]; - while (fgets(buf, 65535, f)) { - if (p_pipe_mutex) { p_pipe_mutex->lock(); } @@ -310,9 +287,10 @@ Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, bo } } int rv = pclose(f); - if (r_exitcode) - *r_exitcode = WEXITSTATUS(rv); + if (r_exitcode) { + *r_exitcode = WEXITSTATUS(rv); + } return OK; } @@ -320,49 +298,75 @@ Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, bo ERR_FAIL_COND_V(pid < 0, ERR_CANT_FORK); if (pid == 0) { - // is child - - if (!p_blocking) { - // For non blocking calls, create a new session-ID so parent won't wait for it. - // This ensures the process won't go zombie at end. - setsid(); - } - + // The child process Vector<CharString> cs; cs.push_back(p_path.utf8()); - for (int i = 0; i < p_arguments.size(); i++) + for (int i = 0; i < p_arguments.size(); i++) { cs.push_back(p_arguments[i].utf8()); + } Vector<char *> args; - for (int i = 0; i < cs.size(); i++) + for (int i = 0; i < cs.size(); i++) { args.push_back((char *)cs[i].get_data()); + } args.push_back(0); execvp(p_path.utf8().get_data(), &args[0]); - // still alive? something failed.. - fprintf(stderr, "**ERROR** OS_Unix::execute - Could not create child process while executing: %s\n", p_path.utf8().get_data()); - abort(); + // The execvp() function only returns if an error occurs. + ERR_PRINT("Could not create child process: " + p_path); + raise(SIGKILL); + } + + int status; + waitpid(pid, &status, 0); + if (r_exitcode) { + *r_exitcode = WIFEXITED(status) ? WEXITSTATUS(status) : status; } + return OK; +#endif +} + +Error OS_Unix::create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id) { +#ifdef __EMSCRIPTEN__ + // Don't compile this code at all to avoid undefined references. + // Actual virtual call goes to OS_JavaScript. + ERR_FAIL_V(ERR_BUG); +#else + pid_t pid = fork(); + ERR_FAIL_COND_V(pid < 0, ERR_CANT_FORK); - if (p_blocking) { + if (pid == 0) { + // The new process + // Create a new session-ID so parent won't wait for it. + // This ensures the process won't go zombie at the end. + setsid(); - int status; - waitpid(pid, &status, 0); - if (r_exitcode) - *r_exitcode = WEXITSTATUS(status); + Vector<CharString> cs; + cs.push_back(p_path.utf8()); + for (int i = 0; i < p_arguments.size(); i++) { + cs.push_back(p_arguments[i].utf8()); + } - } else { + Vector<char *> args; + for (int i = 0; i < cs.size(); i++) { + args.push_back((char *)cs[i].get_data()); + } + args.push_back(0); - if (r_child_id) - *r_child_id = pid; + execvp(p_path.utf8().get_data(), &args[0]); + // The execvp() function only returns if an error occurs. + ERR_PRINT("Could not create child process: " + p_path); + raise(SIGKILL); } + if (r_child_id) { + *r_child_id = pid; + } return OK; #endif } Error OS_Unix::kill(const ProcessID &p_pid) { - int ret = ::kill(p_pid, SIGKILL); if (!ret) { //avoid zombie process @@ -373,29 +377,27 @@ Error OS_Unix::kill(const ProcessID &p_pid) { } int OS_Unix::get_process_id() const { - return getpid(); }; bool OS_Unix::has_environment(const String &p_var) const { - return getenv(p_var.utf8().get_data()) != nullptr; } String OS_Unix::get_locale() const { - - if (!has_environment("LANG")) + if (!has_environment("LANG")) { return "en"; + } String locale = get_environment("LANG"); int tp = locale.find("."); - if (tp != -1) + if (tp != -1) { locale = locale.substr(0, tp); + } return locale; } Error OS_Unix::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) { - String path = p_path; if (FileAccess::exists(path) && path.is_rel_path()) { @@ -442,32 +444,29 @@ Error OS_Unix::get_dynamic_library_symbol_handle(void *p_library_handle, const S } Error OS_Unix::set_cwd(const String &p_cwd) { - - if (chdir(p_cwd.utf8().get_data()) != 0) + if (chdir(p_cwd.utf8().get_data()) != 0) { return ERR_CANT_OPEN; + } return OK; } String OS_Unix::get_environment(const String &p_var) const { - - if (getenv(p_var.utf8().get_data())) + if (getenv(p_var.utf8().get_data())) { return getenv(p_var.utf8().get_data()); + } return ""; } bool OS_Unix::set_environment(const String &p_var, const String &p_value) const { - return setenv(p_var.utf8().get_data(), p_value.utf8().get_data(), /* overwrite: */ true) == 0; } int OS_Unix::get_processor_count() const { - return sysconf(_SC_NPROCESSORS_CONF); } String OS_Unix::get_user_data_dir() const { - String appname = get_safe_dir_name(ProjectSettings::get_singleton()->get("application/config/name")); if (appname != "") { bool use_custom_dir = ProjectSettings::get_singleton()->get("application/config/use_custom_user_dir"); @@ -486,7 +485,6 @@ String OS_Unix::get_user_data_dir() const { } String OS_Unix::get_executable_path() const { - #ifdef __linux__ //fix for running from a symlink char buf[256]; @@ -501,7 +499,7 @@ String OS_Unix::get_executable_path() const { return OS::get_executable_path(); } return b; -#elif defined(__OpenBSD__) +#elif defined(__OpenBSD__) || defined(__NetBSD__) char resolved_path[MAXPATHLEN]; realpath(OS::get_executable_path().utf8().get_data(), resolved_path); @@ -544,10 +542,11 @@ void UnixTerminalLogger::log_error(const char *p_function, const char *p_file, i } const char *err_details; - if (p_rationale && p_rationale[0]) + if (p_rationale && p_rationale[0]) { err_details = p_rationale; - else + } else { err_details = p_code; + } // Disable color codes if stdout is not a TTY. // This prevents Godot from writing ANSI escape codes when redirecting diff --git a/drivers/unix/os_unix.h b/drivers/unix/os_unix.h index 90679ddf1d..6c79d984e9 100644 --- a/drivers/unix/os_unix.h +++ b/drivers/unix/os_unix.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -37,7 +37,6 @@ #include "drivers/unix/ip_unix.h" class OS_Unix : public OS { - protected: // UNIX only handles the core functions. // inheriting platforms under unix (eg. X11) should handle the rest @@ -46,7 +45,7 @@ protected: virtual int unix_initialize_audio(int p_audio_driver); //virtual Error initialize(int p_video_driver,int p_audio_driver); - virtual void finalize_core(); + virtual void finalize_core() override; String stdin_buf; @@ -54,7 +53,7 @@ public: OS_Unix(); virtual void alert(const String &p_alert, const String &p_title = "ALERT!"); - virtual String get_stdin_string(bool p_block); + virtual String get_stdin_string(bool p_block) override; //virtual void set_mouse_show(bool p_show); //virtual void set_mouse_grab(bool p_grab); @@ -66,41 +65,40 @@ public: //virtual VideoMode get_video_mode() const; //virtual void get_fullscreen_mode_list(List<VideoMode> *p_list) const; - virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false); - virtual Error close_dynamic_library(void *p_library_handle); - virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle, bool p_optional = false); + virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false) override; + virtual Error close_dynamic_library(void *p_library_handle) override; + virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle, bool p_optional = false) override; - virtual Error set_cwd(const String &p_cwd); + virtual Error set_cwd(const String &p_cwd) override; - virtual String get_name() const; + virtual String get_name() const override; - virtual Date get_date(bool utc) const; - virtual Time get_time(bool utc) const; - virtual TimeZoneInfo get_time_zone_info() const; + virtual Date get_date(bool utc) const override; + virtual Time get_time(bool utc) const override; + virtual TimeZoneInfo get_time_zone_info() const override; - virtual uint64_t get_unix_time() const; - virtual uint64_t get_system_time_secs() const; - virtual uint64_t get_system_time_msecs() const; + virtual double get_unix_time() const override; - virtual void delay_usec(uint32_t p_usec) const; - virtual uint64_t get_ticks_usec() const; + virtual void delay_usec(uint32_t p_usec) const override; + virtual uint64_t get_ticks_usec() const override; - virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking = true, ProcessID *r_child_id = nullptr, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr); - virtual Error kill(const ProcessID &p_pid); - virtual int get_process_id() const; + virtual Error execute(const String &p_path, const List<String> &p_arguments, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr) override; + virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr) override; + virtual Error kill(const ProcessID &p_pid) override; + virtual int get_process_id() const override; - virtual bool has_environment(const String &p_var) const; - virtual String get_environment(const String &p_var) const; - virtual bool set_environment(const String &p_var, const String &p_value) const; - virtual String get_locale() const; + virtual bool has_environment(const String &p_var) const override; + virtual String get_environment(const String &p_var) const override; + virtual bool set_environment(const String &p_var, const String &p_value) const override; + virtual String get_locale() const override; - virtual int get_processor_count() const; + virtual int get_processor_count() const override; - virtual void debug_break(); - virtual void initialize_debugging(); + virtual void debug_break() override; + virtual void initialize_debugging() override; - virtual String get_executable_path() const; - virtual String get_user_data_dir() const; + virtual String get_executable_path() const override; + virtual String get_user_data_dir() const override; }; class UnixTerminalLogger : public StdLogger { diff --git a/drivers/unix/rw_lock_posix.cpp b/drivers/unix/rw_lock_posix.cpp index f219a0905c..af3ca3a597 100644 --- a/drivers/unix/rw_lock_posix.cpp +++ b/drivers/unix/rw_lock_posix.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -32,12 +32,11 @@ #include "rw_lock_posix.h" -#include "core/error_macros.h" +#include "core/error/error_macros.h" #include "core/os/memory.h" #include <stdio.h> void RWLockPosix::read_lock() { - int err = pthread_rwlock_rdlock(&rwlock); if (err != 0) { perror("Acquiring lock failed"); @@ -46,12 +45,10 @@ void RWLockPosix::read_lock() { } void RWLockPosix::read_unlock() { - pthread_rwlock_unlock(&rwlock); } Error RWLockPosix::read_try_lock() { - if (pthread_rwlock_tryrdlock(&rwlock) != 0) { return ERR_BUSY; } else { @@ -60,13 +57,11 @@ Error RWLockPosix::read_try_lock() { } void RWLockPosix::write_lock() { - int err = pthread_rwlock_wrlock(&rwlock); ERR_FAIL_COND(err != 0); } void RWLockPosix::write_unlock() { - pthread_rwlock_unlock(&rwlock); } @@ -79,23 +74,19 @@ Error RWLockPosix::write_try_lock() { } RWLock *RWLockPosix::create_func_posix() { - return memnew(RWLockPosix); } void RWLockPosix::make_default() { - create_func = create_func_posix; } RWLockPosix::RWLockPosix() { - //rwlock=PTHREAD_RWLOCK_INITIALIZER; fails on OSX pthread_rwlock_init(&rwlock, nullptr); } RWLockPosix::~RWLockPosix() { - pthread_rwlock_destroy(&rwlock); } diff --git a/drivers/unix/rw_lock_posix.h b/drivers/unix/rw_lock_posix.h index b12d373db5..aecb2e18ab 100644 --- a/drivers/unix/rw_lock_posix.h +++ b/drivers/unix/rw_lock_posix.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -37,7 +37,6 @@ #include <pthread.h> class RWLockPosix : public RWLock { - pthread_rwlock_t rwlock; static RWLock *create_func_posix(); diff --git a/drivers/unix/syslog_logger.cpp b/drivers/unix/syslog_logger.cpp index dc9112bf14..423ddac793 100644 --- a/drivers/unix/syslog_logger.cpp +++ b/drivers/unix/syslog_logger.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,7 +31,7 @@ #ifdef UNIX_ENABLED #include "syslog_logger.h" -#include "core/print_string.h" +#include "core/string/print_string.h" #include <syslog.h> void SyslogLogger::logv(const char *p_format, va_list p_list, bool p_err) { @@ -49,18 +49,29 @@ void SyslogLogger::print_error(const char *p_function, const char *p_file, int p const char *err_type = "**ERROR**"; switch (p_type) { - case ERR_ERROR: err_type = "**ERROR**"; break; - case ERR_WARNING: err_type = "**WARNING**"; break; - case ERR_SCRIPT: err_type = "**SCRIPT ERROR**"; break; - case ERR_SHADER: err_type = "**SHADER ERROR**"; break; - default: ERR_PRINT("Unknown error type"); break; + case ERR_ERROR: + err_type = "**ERROR**"; + break; + case ERR_WARNING: + err_type = "**WARNING**"; + break; + case ERR_SCRIPT: + err_type = "**SCRIPT ERROR**"; + break; + case ERR_SHADER: + err_type = "**SHADER ERROR**"; + break; + default: + ERR_PRINT("Unknown error type"); + break; } const char *err_details; - if (p_rationale && *p_rationale) + if (p_rationale && *p_rationale) { err_details = p_rationale; - else + } else { err_details = p_code; + } syslog(p_type == ERR_WARNING ? LOG_WARNING : LOG_ERR, "%s: %s\n At: %s:%i:%s() - %s", err_type, err_details, p_file, p_line, p_function, p_code); } diff --git a/drivers/unix/syslog_logger.h b/drivers/unix/syslog_logger.h index 52da12481f..d9f7f2ff99 100644 --- a/drivers/unix/syslog_logger.h +++ b/drivers/unix/syslog_logger.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ diff --git a/drivers/unix/thread_posix.cpp b/drivers/unix/thread_posix.cpp index c227aec6d6..5c7a546b29 100644 --- a/drivers/unix/thread_posix.cpp +++ b/drivers/unix/thread_posix.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -29,17 +29,17 @@ /*************************************************************************/ #include "thread_posix.h" -#include "core/script_language.h" #if (defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED)) && !defined(NO_THREADS) +#include "core/object/script_language.h" +#include "core/os/memory.h" +#include "core/templates/safe_refcount.h" + #ifdef PTHREAD_BSD_SET_NAME #include <pthread_np.h> #endif -#include "core/os/memory.h" -#include "core/safe_refcount.h" - static void _thread_id_key_destr_callback(void *p_value) { memdelete(static_cast<Thread::ID *>(p_value)); } @@ -54,17 +54,14 @@ pthread_key_t ThreadPosix::thread_id_key = _create_thread_id_key(); Thread::ID ThreadPosix::next_thread_id = 0; Thread::ID ThreadPosix::get_id() const { - return id; } Thread *ThreadPosix::create_thread_posix() { - return memnew(ThreadPosix); } void *ThreadPosix::thread_callback(void *userdata) { - ThreadPosix *t = reinterpret_cast<ThreadPosix *>(userdata); t->id = atomic_increment(&next_thread_id); pthread_setspecific(thread_id_key, (void *)memnew(ID(t->id))); @@ -79,7 +76,6 @@ void *ThreadPosix::thread_callback(void *userdata) { } Thread *ThreadPosix::create_func_posix(ThreadCreateCallback p_callback, void *p_user, const Settings &) { - ThreadPosix *tr = memnew(ThreadPosix); tr->callback = p_callback; tr->user = p_user; @@ -91,19 +87,20 @@ Thread *ThreadPosix::create_func_posix(ThreadCreateCallback p_callback, void *p_ return tr; } -Thread::ID ThreadPosix::get_thread_id_func_posix() { +Thread::ID ThreadPosix::get_thread_id_func_posix() { void *value = pthread_getspecific(thread_id_key); - if (value) + if (value) { return *static_cast<ID *>(value); + } ID new_id = atomic_increment(&next_thread_id); pthread_setspecific(thread_id_key, (void *)memnew(ID(new_id))); return new_id; } -void ThreadPosix::wait_to_finish_func_posix(Thread *p_thread) { +void ThreadPosix::wait_to_finish_func_posix(Thread *p_thread) { ThreadPosix *tp = static_cast<ThreadPosix *>(p_thread); ERR_FAIL_COND(!tp); ERR_FAIL_COND(tp->pthread == 0); @@ -113,7 +110,6 @@ void ThreadPosix::wait_to_finish_func_posix(Thread *p_thread) { } Error ThreadPosix::set_name_func_posix(const String &p_name) { - #ifdef PTHREAD_NO_RENAME return ERR_UNAVAILABLE; @@ -130,6 +126,8 @@ Error ThreadPosix::set_name_func_posix(const String &p_name) { #ifdef PTHREAD_BSD_SET_NAME pthread_set_name_np(running_thread, p_name.utf8().get_data()); int err = 0; // Open/FreeBSD ignore errors in this function +#elif defined(PTHREAD_NETBSD_SET_NAME) + int err = pthread_setname_np(running_thread, "%s", const_cast<char *>(p_name.utf8().get_data())); #else int err = pthread_setname_np(running_thread, p_name.utf8().get_data()); #endif // PTHREAD_BSD_SET_NAME @@ -142,7 +140,6 @@ Error ThreadPosix::set_name_func_posix(const String &p_name) { }; void ThreadPosix::make_default() { - create_func = create_func_posix; get_thread_id_func = get_thread_id_func_posix; wait_to_finish_func = wait_to_finish_func_posix; @@ -150,7 +147,6 @@ void ThreadPosix::make_default() { } ThreadPosix::ThreadPosix() { - pthread = 0; } diff --git a/drivers/unix/thread_posix.h b/drivers/unix/thread_posix.h index e852dcf3d5..fa2037e1a2 100644 --- a/drivers/unix/thread_posix.h +++ b/drivers/unix/thread_posix.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -38,7 +38,6 @@ #include <sys/types.h> class ThreadPosix : public Thread { - static pthread_key_t thread_id_key; static ID next_thread_id; |