2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* os_unix.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "os_unix.h"
|
|
|
|
|
|
|
|
#ifdef UNIX_ENABLED
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "core/os/thread_dummy.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "memory_pool_static_malloc.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "mutex_posix.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "os/memory_pool_dynamic_static.h"
|
|
|
|
#include "semaphore_posix.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "thread_posix.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//#include "core/io/file_access_buffered_fa.h"
|
|
|
|
#include "dir_access_unix.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "file_access_unix.h"
|
2014-11-12 14:23:23 +00:00
|
|
|
#include "packet_peer_udp_posix.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "stream_peer_tcp_posix.h"
|
|
|
|
#include "tcp_server_posix.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-04-20 07:49:48 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#endif
|
|
|
|
|
2015-01-27 09:01:37 +00:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "globals.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <signal.h>
|
2014-02-10 01:10:30 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-03-18 23:36:26 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/wait.h>
|
2015-06-30 14:28:43 +00:00
|
|
|
|
|
|
|
extern bool _print_error_enabled;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void OS_Unix::print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-06-30 14:28:43 +00:00
|
|
|
if (!_print_error_enabled)
|
|
|
|
return;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const char *err_details;
|
2015-11-05 19:13:05 +00:00
|
|
|
if (p_rationale && p_rationale[0])
|
2017-03-18 23:36:26 +00:00
|
|
|
err_details = p_rationale;
|
2015-11-05 19:13:05 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
err_details = p_code;
|
2015-11-05 19:13:05 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
switch (p_type) {
|
2015-11-05 19:13:05 +00:00
|
|
|
case ERR_ERROR:
|
2017-03-18 23:36:26 +00:00
|
|
|
print("\E[1;31mERROR: %s: \E[0m\E[1m%s\n", p_function, err_details);
|
|
|
|
print("\E[0;31m At: %s:%i.\E[0m\n", p_file, p_line);
|
2015-11-05 19:13:05 +00:00
|
|
|
break;
|
|
|
|
case ERR_WARNING:
|
2017-03-18 23:36:26 +00:00
|
|
|
print("\E[1;33mWARNING: %s: \E[0m\E[1m%s\n", p_function, err_details);
|
|
|
|
print("\E[0;33m At: %s:%i.\E[0m\n", p_file, p_line);
|
2015-11-05 19:13:05 +00:00
|
|
|
break;
|
|
|
|
case ERR_SCRIPT:
|
2017-03-18 23:36:26 +00:00
|
|
|
print("\E[1;35mSCRIPT ERROR: %s: \E[0m\E[1m%s\n", p_function, err_details);
|
|
|
|
print("\E[0;35m At: %s:%i.\E[0m\n", p_file, p_line);
|
2015-11-05 19:13:05 +00:00
|
|
|
break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_Unix::debug_break() {
|
|
|
|
|
|
|
|
assert(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
int OS_Unix::get_audio_driver_count() const {
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
const char *OS_Unix::get_audio_driver_name(int p_driver) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return "dummy";
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int OS_Unix::unix_initialize_audio(int p_audio_driver) {
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
|
|
|
|
static MemoryPoolStaticMalloc *mempool_static = NULL;
|
|
|
|
static MemoryPoolDynamicStatic *mempool_dynamic = NULL;
|
|
|
|
|
2017-02-09 21:01:45 +00:00
|
|
|
// Very simple signal handler to reap processes where ::execute was called with
|
|
|
|
// !p_blocking
|
|
|
|
void handle_sigchld(int sig) {
|
|
|
|
int saved_errno = errno;
|
2017-03-18 23:36:26 +00:00
|
|
|
while (waitpid((pid_t)(-1), 0, WNOHANG) > 0) {
|
|
|
|
}
|
2017-02-09 21:01:45 +00:00
|
|
|
errno = saved_errno;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void OS_Unix::initialize_core() {
|
|
|
|
|
|
|
|
#ifdef NO_PTHREADS
|
|
|
|
ThreadDummy::make_default();
|
|
|
|
SemaphoreDummy::make_default();
|
|
|
|
MutexDummy::make_default();
|
|
|
|
#else
|
2017-03-18 23:36:26 +00:00
|
|
|
ThreadPosix::make_default();
|
2014-02-10 01:10:30 +00:00
|
|
|
SemaphorePosix::make_default();
|
2017-03-18 23:36:26 +00:00
|
|
|
MutexPosix::make_default();
|
2014-02-10 01:10:30 +00:00
|
|
|
#endif
|
|
|
|
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);
|
|
|
|
|
|
|
|
#ifndef NO_NETWORK
|
|
|
|
TCPServerPosix::make_default();
|
|
|
|
StreamPeerTCPPosix::make_default();
|
2014-11-12 14:23:23 +00:00
|
|
|
PacketPeerUDPPosix::make_default();
|
2014-02-10 01:10:30 +00:00
|
|
|
IP_Unix::make_default();
|
|
|
|
#endif
|
|
|
|
mempool_static = new MemoryPoolStaticMalloc;
|
2017-03-18 23:36:26 +00:00
|
|
|
mempool_dynamic = memnew(MemoryPoolDynamicStatic);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ticks_start = 0;
|
|
|
|
ticks_start = get_ticks_usec();
|
2017-02-09 21:01:45 +00:00
|
|
|
|
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_handler = &handle_sigchld;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
|
|
|
|
if (sigaction(SIGCHLD, &sa, 0) == -1) {
|
|
|
|
perror("ERROR sigaction() failed:");
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OS_Unix::finalize_core() {
|
|
|
|
|
|
|
|
if (mempool_dynamic)
|
2017-03-18 23:36:26 +00:00
|
|
|
memdelete(mempool_dynamic);
|
2016-01-04 10:46:16 +00:00
|
|
|
delete mempool_static;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void OS_Unix::vprint(const char *p_format, va_list p_list, bool p_stder) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (p_stder) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
vfprintf(stderr, p_format, p_list);
|
2014-02-10 01:10:30 +00:00
|
|
|
fflush(stderr);
|
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
vprintf(p_format, p_list);
|
2014-02-10 01:10:30 +00:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void OS_Unix::print(const char *p_format, ...) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
va_list argp;
|
|
|
|
va_start(argp, p_format);
|
2017-03-18 23:36:26 +00:00
|
|
|
vprintf(p_format, argp);
|
2014-02-10 01:10:30 +00:00
|
|
|
va_end(argp);
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void OS_Unix::alert(const String &p_alert, const String &p_title) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
fprintf(stderr, "ERROR: %s\n", p_alert.utf8().get_data());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
static int has_data(FILE *p_fd, int timeout_usec = 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
fd_set readset;
|
|
|
|
int fd = fileno(p_fd);
|
|
|
|
FD_ZERO(&readset);
|
|
|
|
FD_SET(fd, &readset);
|
|
|
|
timeval time;
|
|
|
|
time.tv_sec = 0;
|
|
|
|
time.tv_usec = timeout_usec;
|
2017-03-18 23:36:26 +00:00
|
|
|
int res = 0; //select(fd + 1, &readset, NULL, NULL, &time);
|
2014-02-10 01:10:30 +00:00
|
|
|
return res > 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
String OS_Unix::get_stdin_string(bool p_block) {
|
|
|
|
|
|
|
|
String ret;
|
|
|
|
if (p_block) {
|
|
|
|
char buff[1024];
|
2017-03-18 23:36:26 +00:00
|
|
|
ret = stdin_buf + fgets(buff, 1024, stdin);
|
2014-02-10 01:10:30 +00:00
|
|
|
stdin_buf = "";
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
while (has_data(stdin)) {
|
|
|
|
|
|
|
|
char ch;
|
|
|
|
read(fileno(stdin), &ch, 1);
|
|
|
|
if (ch == '\n') {
|
|
|
|
ret = stdin_buf;
|
|
|
|
stdin_buf = "";
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
char str[2] = { ch, 0 };
|
|
|
|
stdin_buf += str;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
String OS_Unix::get_name() {
|
|
|
|
|
|
|
|
return "Unix";
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t OS_Unix::get_unix_time() const {
|
|
|
|
|
|
|
|
return time(NULL);
|
|
|
|
};
|
|
|
|
|
2016-01-10 21:24:55 +00:00
|
|
|
uint64_t OS_Unix::get_system_time_secs() const {
|
2015-08-06 17:29:33 +00:00
|
|
|
struct timeval tv_now;
|
2015-08-06 18:08:48 +00:00
|
|
|
gettimeofday(&tv_now, NULL);
|
2015-09-09 20:24:38 +00:00
|
|
|
//localtime(&tv_now.tv_usec);
|
2015-09-10 16:10:23 +00:00
|
|
|
//localtime((const long *)&tv_now.tv_usec);
|
2016-01-10 21:24:55 +00:00
|
|
|
return uint64_t(tv_now.tv_sec);
|
2015-08-06 17:29:33 +00:00
|
|
|
}
|
|
|
|
|
2015-06-06 01:40:56 +00:00
|
|
|
OS::Date OS_Unix::get_date(bool utc) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
time_t t = time(NULL);
|
2015-06-06 01:40:56 +00:00
|
|
|
struct tm *lt;
|
|
|
|
if (utc)
|
2017-03-18 23:36:26 +00:00
|
|
|
lt = gmtime(&t);
|
2015-06-06 01:40:56 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
lt = localtime(&t);
|
2014-02-10 01:10:30 +00:00
|
|
|
Date ret;
|
2017-03-18 23:36:26 +00:00
|
|
|
ret.year = 1900 + lt->tm_year;
|
2016-03-13 02:13:57 +00:00
|
|
|
// Index starting at 1 to match OS_Unix::get_date
|
2017-03-18 23:36:26 +00:00
|
|
|
// and Windows SYSTEMTIME and tm_mon follows the typical structure
|
2016-03-13 02:13:57 +00:00
|
|
|
// of 0-11, noted here: http://www.cplusplus.com/reference/ctime/tm/
|
2017-03-18 23:36:26 +00:00
|
|
|
ret.month = (Month)(lt->tm_mon + 1);
|
|
|
|
ret.day = lt->tm_mday;
|
|
|
|
ret.weekday = (Weekday)lt->tm_wday;
|
|
|
|
ret.dst = lt->tm_isdst;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2016-03-16 04:17:10 +00:00
|
|
|
|
2015-06-06 01:40:56 +00:00
|
|
|
OS::Time OS_Unix::get_time(bool utc) const {
|
2017-03-18 23:36:26 +00:00
|
|
|
time_t t = time(NULL);
|
2015-06-06 01:40:56 +00:00
|
|
|
struct tm *lt;
|
|
|
|
if (utc)
|
2017-03-18 23:36:26 +00:00
|
|
|
lt = gmtime(&t);
|
2015-06-06 01:40:56 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
lt = localtime(&t);
|
2014-02-10 01:10:30 +00:00
|
|
|
Time ret;
|
2017-03-18 23:36:26 +00:00
|
|
|
ret.hour = lt->tm_hour;
|
|
|
|
ret.min = lt->tm_min;
|
|
|
|
ret.sec = lt->tm_sec;
|
2015-06-06 03:35:38 +00:00
|
|
|
get_time_zone_info();
|
2014-02-10 01:10:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2015-06-06 03:35:38 +00:00
|
|
|
|
|
|
|
OS::TimeZoneInfo OS_Unix::get_time_zone_info() const {
|
|
|
|
time_t t = time(NULL);
|
|
|
|
struct tm *lt = localtime(&t);
|
|
|
|
char name[16];
|
|
|
|
strftime(name, 16, "%Z", lt);
|
|
|
|
name[15] = 0;
|
|
|
|
TimeZoneInfo ret;
|
|
|
|
ret.name = name;
|
|
|
|
|
|
|
|
char bias_buf[16];
|
|
|
|
strftime(bias_buf, 16, "%z", lt);
|
|
|
|
int bias;
|
|
|
|
bias_buf[15] = 0;
|
|
|
|
sscanf(bias_buf, "%d", &bias);
|
|
|
|
|
|
|
|
// convert from ISO 8601 (1 minute=1, 1 hour=100) to minutes
|
|
|
|
int hour = (int)bias / 100;
|
|
|
|
int minutes = bias % 100;
|
|
|
|
if (bias < 0)
|
|
|
|
ret.bias = hour * 60 - minutes;
|
|
|
|
else
|
|
|
|
ret.bias = hour * 60 + minutes;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void OS_Unix::delay_usec(uint32_t p_usec) const {
|
|
|
|
|
|
|
|
usleep(p_usec);
|
|
|
|
}
|
|
|
|
uint64_t OS_Unix::get_ticks_usec() const {
|
|
|
|
|
|
|
|
struct timeval tv_now;
|
2017-03-18 23:36:26 +00:00
|
|
|
gettimeofday(&tv_now, NULL);
|
|
|
|
|
|
|
|
uint64_t longtime = (uint64_t)tv_now.tv_usec + (uint64_t)tv_now.tv_sec * 1000000L;
|
|
|
|
longtime -= ticks_start;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return longtime;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
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) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (p_blocking && r_pipe) {
|
|
|
|
|
|
|
|
String argss;
|
2017-03-18 23:36:26 +00:00
|
|
|
argss = "\"" + p_path + "\"";
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p_arguments.size(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
argss += String(" \"") + p_arguments[i] + "\"";
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
argss += " 2>/dev/null"; //silence stderr
|
|
|
|
FILE *f = popen(argss.utf8().get_data(), "r");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!f, ERR_CANT_OPEN);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
char buf[65535];
|
2017-03-18 23:36:26 +00:00
|
|
|
while (fgets(buf, 65535, f)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
(*r_pipe) += buf;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int rv = pclose(f);
|
|
|
|
if (r_exitcode)
|
2017-03-18 23:36:26 +00:00
|
|
|
*r_exitcode = rv;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(pid < 0, ERR_CANT_FORK);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (pid == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
// is child
|
|
|
|
Vector<CharString> cs;
|
|
|
|
cs.push_back(p_path.utf8());
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p_arguments.size(); i++)
|
2014-02-10 01:10:30 +00:00
|
|
|
cs.push_back(p_arguments[i].utf8());
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector<char *> args;
|
|
|
|
for (int i = 0; i < cs.size(); i++)
|
|
|
|
args.push_back((char *)cs[i].get_data()); // shitty C cast
|
2014-02-10 01:10:30 +00:00
|
|
|
args.push_back(0);
|
|
|
|
|
2015-01-27 09:01:37 +00:00
|
|
|
#ifdef __FreeBSD__
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_path.find("/")) {
|
2015-01-27 09:01:37 +00:00
|
|
|
// exec name contains path so use it
|
2017-03-18 23:36:26 +00:00
|
|
|
execv(p_path.utf8().get_data(), &args[0]);
|
|
|
|
} else {
|
2015-01-27 09:01:37 +00:00
|
|
|
// use program name and search through PATH to find it
|
2017-03-18 23:36:26 +00:00
|
|
|
execvp(getprogname(), &args[0]);
|
2015-01-27 09:01:37 +00:00
|
|
|
}
|
|
|
|
#else
|
2017-03-18 23:36:26 +00:00
|
|
|
execv(p_path.utf8().get_data(), &args[0]);
|
2015-01-27 09:01:37 +00:00
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
// still alive? something failed..
|
2017-03-18 23:36:26 +00:00
|
|
|
fprintf(stderr, "**ERROR** OS_Unix::execute - Could not create child process while executing: %s\n", p_path.utf8().get_data());
|
2014-02-10 01:10:30 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_blocking) {
|
|
|
|
|
|
|
|
int status;
|
2017-03-18 23:36:26 +00:00
|
|
|
pid_t rpid = waitpid(pid, &status, 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (r_exitcode)
|
2017-03-18 23:36:26 +00:00
|
|
|
*r_exitcode = WEXITSTATUS(status);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
if (r_child_id)
|
2017-03-18 23:36:26 +00:00
|
|
|
*r_child_id = pid;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Error OS_Unix::kill(const ProcessID &p_pid) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int ret = ::kill(p_pid, SIGKILL);
|
2014-12-17 01:31:57 +00:00
|
|
|
if (!ret) {
|
|
|
|
//avoid zombie process
|
|
|
|
int st;
|
2017-03-18 23:36:26 +00:00
|
|
|
::waitpid(p_pid, &st, 0);
|
2014-12-17 01:31:57 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
return ret ? ERR_INVALID_PARAMETER : OK;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-05 15:39:30 +00:00
|
|
|
int OS_Unix::get_process_ID() const {
|
|
|
|
|
|
|
|
return getpid();
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool OS_Unix::has_environment(const String &p_var) const {
|
2014-04-05 15:39:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return getenv(p_var.utf8().get_data()) != NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String OS_Unix::get_locale() const {
|
|
|
|
|
|
|
|
if (!has_environment("LANG"))
|
|
|
|
return "en";
|
|
|
|
|
|
|
|
String locale = get_environment("LANG");
|
|
|
|
int tp = locale.find(".");
|
2017-03-18 23:36:26 +00:00
|
|
|
if (tp != -1)
|
|
|
|
locale = locale.substr(0, tp);
|
2014-02-10 01:10:30 +00:00
|
|
|
return locale;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Error OS_Unix::set_cwd(const String &p_cwd) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (chdir(p_cwd.utf8().get_data()) != 0)
|
2014-02-10 01:10:30 +00:00
|
|
|
return ERR_CANT_OPEN;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String OS_Unix::get_environment(const String &p_var) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (getenv(p_var.utf8().get_data()))
|
|
|
|
return getenv(p_var.utf8().get_data());
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
int OS_Unix::get_processor_count() const {
|
|
|
|
|
|
|
|
return sysconf(_SC_NPROCESSORS_CONF);
|
|
|
|
}
|
|
|
|
|
|
|
|
String OS_Unix::get_data_dir() const {
|
|
|
|
|
2016-06-02 18:39:37 +00:00
|
|
|
String an = get_safe_application_name();
|
2017-03-18 23:36:26 +00:00
|
|
|
if (an != "") {
|
2014-09-19 21:39:50 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (has_environment("HOME")) {
|
|
|
|
|
2014-09-19 21:39:50 +00:00
|
|
|
bool use_godot = Globals::get_singleton()->get("application/use_shared_user_dir");
|
|
|
|
if (use_godot)
|
2017-03-18 23:36:26 +00:00
|
|
|
return get_environment("HOME") + "/.godot/app_userdata/" + an;
|
2014-09-19 21:39:50 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
return get_environment("HOME") + "/." + an;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Globals::get_singleton()->get_resource_path();
|
|
|
|
}
|
|
|
|
|
2015-11-30 01:35:59 +00:00
|
|
|
String OS_Unix::get_installed_templates_path() const {
|
2017-03-18 23:36:26 +00:00
|
|
|
String p = get_global_settings_path();
|
|
|
|
if (p != "")
|
|
|
|
return p + "/templates/";
|
2015-11-30 01:35:59 +00:00
|
|
|
else
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
String OS_Unix::get_executable_path() const {
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
//fix for running from a symlink
|
|
|
|
char buf[256];
|
2017-03-18 23:36:26 +00:00
|
|
|
memset(buf, 0, 256);
|
2014-02-10 01:10:30 +00:00
|
|
|
readlink("/proc/self/exe", buf, sizeof(buf));
|
2014-02-15 05:01:39 +00:00
|
|
|
String b;
|
|
|
|
b.parse_utf8(buf);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (b == "") {
|
2014-02-10 01:10:30 +00:00
|
|
|
WARN_PRINT("Couldn't get executable path from /proc/self/exe, using argv[0]");
|
|
|
|
return OS::get_executable_path();
|
|
|
|
}
|
|
|
|
return b;
|
2015-01-27 09:01:37 +00:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
char resolved_path[MAXPATHLEN];
|
|
|
|
|
|
|
|
realpath(OS::get_executable_path().utf8().get_data(), resolved_path);
|
|
|
|
|
|
|
|
return String(resolved_path);
|
2016-04-20 07:49:48 +00:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
char temp_path[1];
|
2017-03-18 23:36:26 +00:00
|
|
|
uint32_t buff_size = 1;
|
2016-04-20 07:49:48 +00:00
|
|
|
_NSGetExecutablePath(temp_path, &buff_size);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
char *resolved_path = new char[buff_size + 1];
|
2016-04-20 07:49:48 +00:00
|
|
|
|
|
|
|
if (_NSGetExecutablePath(resolved_path, &buff_size) == 1)
|
|
|
|
WARN_PRINT("MAXPATHLEN is too small");
|
|
|
|
|
|
|
|
String path(resolved_path);
|
|
|
|
delete[] resolved_path;
|
|
|
|
|
|
|
|
return path;
|
2014-02-10 01:10:30 +00:00
|
|
|
#else
|
|
|
|
ERR_PRINT("Warning, don't know how to obtain executable path on this OS! Please override this function properly.");
|
|
|
|
return OS::get_executable_path();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|