os_unix.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*************************************************************************/
  2. /* os_unix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "os_unix.h"
  30. #ifdef UNIX_ENABLED
  31. #include "memory_pool_static_malloc.h"
  32. #include "os/memory_pool_dynamic_static.h"
  33. #include "thread_posix.h"
  34. #include "semaphore_posix.h"
  35. #include "mutex_posix.h"
  36. #include "core/os/thread_dummy.h"
  37. //#include "core/io/file_access_buffered_fa.h"
  38. #include "file_access_unix.h"
  39. #include "dir_access_unix.h"
  40. #include "tcp_server_posix.h"
  41. #include "stream_peer_tcp_posix.h"
  42. #include <stdarg.h>
  43. #include <sys/time.h>
  44. #include <sys/wait.h>
  45. #include <stdlib.h>
  46. #include <signal.h>
  47. #include <string.h>
  48. #include <poll.h>
  49. #include <errno.h>
  50. #include <assert.h>
  51. #include "globals.h"
  52. 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) {
  53. if (p_rationale && p_rationale[0]) {
  54. print("\E[1;31;40mERROR: %s: \E[1;37;40m%s\n",p_function,p_rationale);
  55. print("\E[0;31;40m At: %s:%i.\E[0;0;37m\n",p_file,p_line);
  56. } else {
  57. print("\E[1;31;40mERROR: %s: \E[1;37;40m%s\n",p_function,p_code);
  58. print("\E[0;31;40m At: %s:%i.\E[0;0;37m\n",p_file,p_line);
  59. }
  60. }
  61. void OS_Unix::debug_break() {
  62. assert(false);
  63. };
  64. int OS_Unix::get_audio_driver_count() const {
  65. return 1;
  66. }
  67. const char * OS_Unix::get_audio_driver_name(int p_driver) const {
  68. return "dummy";
  69. }
  70. int OS_Unix::unix_initialize_audio(int p_audio_driver) {
  71. return 0;
  72. }
  73. static MemoryPoolStaticMalloc *mempool_static=NULL;
  74. static MemoryPoolDynamicStatic *mempool_dynamic=NULL;
  75. void OS_Unix::initialize_core() {
  76. #ifdef NO_PTHREADS
  77. ThreadDummy::make_default();
  78. SemaphoreDummy::make_default();
  79. MutexDummy::make_default();
  80. #else
  81. ThreadPosix::make_default();
  82. SemaphorePosix::make_default();
  83. MutexPosix::make_default();
  84. #endif
  85. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES);
  86. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_USERDATA);
  87. FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_FILESYSTEM);
  88. //FileAccessBufferedFA<FileAccessUnix>::make_default();
  89. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_RESOURCES);
  90. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
  91. DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM);
  92. #ifndef NO_NETWORK
  93. TCPServerPosix::make_default();
  94. StreamPeerTCPPosix::make_default();
  95. IP_Unix::make_default();
  96. #endif
  97. mempool_static = new MemoryPoolStaticMalloc;
  98. mempool_dynamic = memnew( MemoryPoolDynamicStatic );
  99. ticks_start=0;
  100. ticks_start=get_ticks_usec();
  101. }
  102. void OS_Unix::finalize_core() {
  103. if (mempool_dynamic)
  104. memdelete( mempool_dynamic );
  105. if (mempool_static)
  106. delete mempool_static;
  107. }
  108. void OS_Unix::vprint(const char* p_format, va_list p_list,bool p_stder) {
  109. if (p_stder) {
  110. vfprintf(stderr,p_format,p_list);
  111. fflush(stderr);
  112. } else {
  113. vprintf(p_format,p_list);
  114. fflush(stdout);
  115. }
  116. }
  117. void OS_Unix::print(const char *p_format, ... ) {
  118. va_list argp;
  119. va_start(argp, p_format);
  120. vprintf(p_format, argp );
  121. va_end(argp);
  122. }
  123. void OS_Unix::alert(const String& p_alert,const String& p_title) {
  124. fprintf(stderr,"ERROR: %s\n",p_alert.utf8().get_data());
  125. }
  126. static int has_data(FILE* p_fd, int timeout_usec = 0) {
  127. fd_set readset;
  128. int fd = fileno(p_fd);
  129. FD_ZERO(&readset);
  130. FD_SET(fd, &readset);
  131. timeval time;
  132. time.tv_sec = 0;
  133. time.tv_usec = timeout_usec;
  134. int res = 0;//select(fd + 1, &readset, NULL, NULL, &time);
  135. return res > 0;
  136. };
  137. String OS_Unix::get_stdin_string(bool p_block) {
  138. String ret;
  139. if (p_block) {
  140. char buff[1024];
  141. ret = stdin_buf + fgets(buff,1024,stdin);
  142. stdin_buf = "";
  143. return ret;
  144. };
  145. while (has_data(stdin)) {
  146. char ch;
  147. read(fileno(stdin), &ch, 1);
  148. if (ch == '\n') {
  149. ret = stdin_buf;
  150. stdin_buf = "";
  151. return ret;
  152. } else {
  153. char str[2] = { ch, 0 };
  154. stdin_buf += str;
  155. };
  156. };
  157. return "";
  158. }
  159. String OS_Unix::get_name() {
  160. return "Unix";
  161. }
  162. uint64_t OS_Unix::get_unix_time() const {
  163. return time(NULL);
  164. };
  165. OS::Date OS_Unix::get_date() const {
  166. time_t t=time(NULL);
  167. struct tm *lt=localtime(&t);
  168. Date ret;
  169. ret.year=1900+lt->tm_year;
  170. ret.month=(Month)lt->tm_mon;
  171. ret.day=lt->tm_mday;
  172. ret.weekday=(Weekday)lt->tm_wday;
  173. ret.dst=lt->tm_isdst;
  174. return ret;
  175. }
  176. OS::Time OS_Unix::get_time() const {
  177. time_t t=time(NULL);
  178. struct tm *lt=localtime(&t);
  179. Time ret;
  180. ret.hour=lt->tm_hour;
  181. ret.min=lt->tm_min;
  182. ret.sec=lt->tm_sec;
  183. return ret;
  184. }
  185. void OS_Unix::delay_usec(uint32_t p_usec) const {
  186. usleep(p_usec);
  187. }
  188. uint64_t OS_Unix::get_ticks_usec() const {
  189. struct timeval tv_now;
  190. gettimeofday(&tv_now,NULL);
  191. uint64_t longtime = (uint64_t)tv_now.tv_usec + (uint64_t)tv_now.tv_sec*1000000L;
  192. longtime-=ticks_start;
  193. return longtime;
  194. }
  195. 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) {
  196. if (p_blocking && r_pipe) {
  197. String argss;
  198. argss="\""+p_path+"\"";
  199. for(int i=0;i<p_arguments.size();i++) {
  200. argss+=String(" \"")+p_arguments[i]+"\"";
  201. }
  202. argss+=" 2>/dev/null"; //silence stderr
  203. FILE* f=popen(argss.utf8().get_data(),"r");
  204. ERR_FAIL_COND_V(!f,ERR_CANT_OPEN);
  205. char buf[65535];
  206. while(fgets(buf,65535,f)) {
  207. (*r_pipe)+=buf;
  208. }
  209. int rv = pclose(f);
  210. if (r_exitcode)
  211. *r_exitcode=rv;
  212. return OK;
  213. }
  214. pid_t pid = fork();
  215. ERR_FAIL_COND_V(pid<0,ERR_CANT_FORK);
  216. //print("execute: %s\n",p_path.utf8().get_data());
  217. if (pid==0) {
  218. // is child
  219. Vector<CharString> cs;
  220. cs.push_back(p_path.utf8());
  221. for(int i=0;i<p_arguments.size();i++)
  222. cs.push_back(p_arguments[i].utf8());
  223. Vector<char*> args;
  224. for(int i=0;i<cs.size();i++)
  225. args.push_back((char*)cs[i].get_data());// shitty C cast
  226. args.push_back(0);
  227. execv(p_path.utf8().get_data(),&args[0]);
  228. // still alive? something failed..
  229. fprintf(stderr,"**ERROR** OS_Unix::execute - Could not create child process while executing: %s\n",p_path.utf8().get_data());
  230. abort();
  231. }
  232. if (p_blocking) {
  233. int status;
  234. pid_t rpid = waitpid(pid,&status,0);
  235. if (r_exitcode)
  236. *r_exitcode=WEXITSTATUS(status);
  237. print("returned: %i, waiting for: %i\n",rpid,pid);
  238. } else {
  239. if (r_child_id)
  240. *r_child_id=pid;
  241. }
  242. return OK;
  243. }
  244. Error OS_Unix::kill(const ProcessID& p_pid) {
  245. int ret = ::kill(p_pid,SIGKILL);
  246. return ret?ERR_INVALID_PARAMETER:OK;
  247. }
  248. int OS_Unix::get_process_ID() const {
  249. return getpid();
  250. };
  251. bool OS_Unix::has_environment(const String& p_var) const {
  252. return getenv(p_var.utf8().get_data())!=NULL;
  253. }
  254. String OS_Unix::get_locale() const {
  255. if (!has_environment("LANG"))
  256. return "en";
  257. String locale = get_environment("LANG");
  258. int tp = locale.find(".");
  259. if (tp!=-1)
  260. locale=locale.substr(0,tp);
  261. return locale;
  262. }
  263. Error OS_Unix::set_cwd(const String& p_cwd) {
  264. if (chdir(p_cwd.utf8().get_data())!=0)
  265. return ERR_CANT_OPEN;
  266. return OK;
  267. }
  268. String OS_Unix::get_environment(const String& p_var) const {
  269. if (getenv(p_var.utf8().get_data()))
  270. return getenv(p_var.utf8().get_data());
  271. return "";
  272. }
  273. int OS_Unix::get_processor_count() const {
  274. return sysconf(_SC_NPROCESSORS_CONF);
  275. }
  276. String OS_Unix::get_data_dir() const {
  277. String an = Globals::get_singleton()->get("application/name");
  278. if (an!="") {
  279. if (has_environment("HOME")) {
  280. return get_environment("HOME")+"/."+an;
  281. }
  282. }
  283. return Globals::get_singleton()->get_resource_path();
  284. }
  285. String OS_Unix::get_executable_path() const {
  286. #ifdef __linux__
  287. //fix for running from a symlink
  288. char buf[256];
  289. memset(buf,0,256);
  290. readlink("/proc/self/exe", buf, sizeof(buf));
  291. //print_line("Exec path is:"+String(buf));
  292. String b;
  293. b.parse_utf8(buf);
  294. if (b=="") {
  295. WARN_PRINT("Couldn't get executable path from /proc/self/exe, using argv[0]");
  296. return OS::get_executable_path();
  297. }
  298. return b;
  299. #else
  300. ERR_PRINT("Warning, don't know how to obtain executable path on this OS! Please override this function properly.");
  301. return OS::get_executable_path();
  302. #endif
  303. }
  304. #endif