The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
7.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #include "linuxincludes.h"
  24. #include <dlfcn.h>
  25. #include <sys/file.h>
  26. #include <sys/types.h>
  27. #include <sys/ptrace.h>
  28. #include "../../../src/juce_core/basics/juce_StandardHeader.h"
  29. BEGIN_JUCE_NAMESPACE
  30. #include "../../../src/juce_core/threads/juce_CriticalSection.h"
  31. #include "../../../src/juce_core/threads/juce_WaitableEvent.h"
  32. #include "../../../src/juce_core/threads/juce_Thread.h"
  33. #include "../../../src/juce_core/threads/juce_Process.h"
  34. #include "../../../src/juce_core/io/files/juce_File.h"
  35. #include "../../../src/juce_core/basics/juce_SystemStats.h"
  36. //==============================================================================
  37. /*
  38. Note that a lot of methods that you'd expect to find in this file actually
  39. live in juce_posix_SharedCode.cpp!
  40. */
  41. #ifndef CPU_ISSET
  42. #undef SUPPORT_AFFINITIES
  43. #endif
  44. //==============================================================================
  45. void JUCE_API juce_threadEntryPoint (void*);
  46. void* threadEntryProc (void* value) throw()
  47. {
  48. // New threads start off as root when running suid
  49. Process::lowerPrivilege();
  50. juce_threadEntryPoint (value);
  51. return 0;
  52. }
  53. void* juce_createThread (void* userData) throw()
  54. {
  55. pthread_t handle = 0;
  56. if (pthread_create (&handle, 0, threadEntryProc, userData) == 0)
  57. {
  58. pthread_detach (handle);
  59. return (void*)handle;
  60. }
  61. return 0;
  62. }
  63. void juce_killThread (void* handle) throw()
  64. {
  65. if (handle != 0)
  66. pthread_cancel ((pthread_t)handle);
  67. }
  68. void juce_setCurrentThreadName (const String& /*name*/) throw()
  69. {
  70. }
  71. int Thread::getCurrentThreadId() throw()
  72. {
  73. return (int) pthread_self();
  74. }
  75. /*
  76. * This is all a bit non-ideal... the trouble is that on Linux you
  77. * need to call setpriority to affect the dynamic priority for
  78. * non-realtime processes, but this requires the pid, which is not
  79. * accessible from the pthread_t. We could get it by calling getpid
  80. * once each thread has started, but then we would need a list of
  81. * running threads etc etc.
  82. * Also there is no such thing as IDLE priority on Linux.
  83. * For the moment, map idle, low and normal process priorities to
  84. * SCHED_OTHER, with the thread priority ignored for these classes.
  85. * Map high priority processes to the lower half of the SCHED_RR
  86. * range, and realtime to the upper half
  87. */
  88. // priority 1 to 10 where 5=normal, 1=low. If the handle is 0, sets the
  89. // priority of the current thread
  90. void juce_setThreadPriority (void* handle, int priority) throw()
  91. {
  92. struct sched_param param;
  93. int policy, maxp, minp, pri;
  94. if (handle == 0)
  95. handle = (void*) pthread_self();
  96. if (pthread_getschedparam ((pthread_t) handle, &policy, &param) == 0
  97. && policy != SCHED_OTHER)
  98. {
  99. minp = sched_get_priority_min(policy);
  100. maxp = sched_get_priority_max(policy);
  101. pri = ((maxp - minp) / 2) * (priority - 1) / 9;
  102. if (param.__sched_priority >= (minp + (maxp - minp) / 2))
  103. // Realtime process priority
  104. param.__sched_priority = minp + ((maxp - minp) / 2) + pri;
  105. else
  106. // High process priority
  107. param.__sched_priority = minp + pri;
  108. param.sched_priority = jlimit (1, 127, 1 + (priority * 126) / 11);
  109. pthread_setschedparam ((pthread_t) handle, policy, &param);
  110. }
  111. }
  112. void Thread::setCurrentThreadAffinityMask (const uint32 affinityMask) throw()
  113. {
  114. #if SUPPORT_AFFINITIES
  115. cpu_set_t affinity;
  116. CPU_ZERO (&affinity);
  117. for (int i = 0; i < 32; ++i)
  118. if ((affinityMask & (1 << i)) != 0)
  119. CPU_SET (i, &affinity);
  120. /*
  121. N.B. If this line causes a compile error, then you've probably not got the latest
  122. version of glibc installed.
  123. If you don't want to update your copy of glibc and don't care about cpu affinities,
  124. then you can just disable all this stuff by removing the SUPPORT_AFFINITIES macro
  125. from the linuxincludes.h file.
  126. */
  127. sched_setaffinity (getpid(), sizeof (cpu_set_t), &affinity);
  128. sched_yield();
  129. #else
  130. /* affinities aren't supported because either the appropriate header files weren't found,
  131. or the SUPPORT_AFFINITIES macro was turned off in linuxheaders.h
  132. */
  133. jassertfalse
  134. #endif
  135. }
  136. void Thread::yield() throw()
  137. {
  138. sched_yield();
  139. }
  140. //==============================================================================
  141. // sets the process to 0=low priority, 1=normal, 2=high, 3=realtime
  142. void Process::setPriority (ProcessPriority prior)
  143. {
  144. struct sched_param param;
  145. int policy, maxp, minp;
  146. const int p = (int) prior;
  147. if (p <= 1)
  148. policy = SCHED_OTHER;
  149. else
  150. policy = SCHED_RR;
  151. minp = sched_get_priority_min (policy);
  152. maxp = sched_get_priority_max (policy);
  153. if (p < 2)
  154. param.__sched_priority = 0;
  155. else if (p == 2 )
  156. // Set to middle of lower realtime priority range
  157. param.__sched_priority = minp + (maxp - minp) / 4;
  158. else
  159. // Set to middle of higher realtime priority range
  160. param.__sched_priority = minp + (3 * (maxp - minp) / 4);
  161. pthread_setschedparam (pthread_self(), policy, &param);
  162. }
  163. void Process::terminate()
  164. {
  165. exit (0);
  166. }
  167. bool JUCE_CALLTYPE juce_isRunningUnderDebugger() throw()
  168. {
  169. static char testResult = 0;
  170. if (testResult == 0)
  171. {
  172. testResult = (char) ptrace (PT_TRACE_ME, 0, 0, 0);
  173. if (testResult >= 0)
  174. {
  175. ptrace (PT_DETACH, 0, (caddr_t) 1, 0);
  176. testResult = 1;
  177. }
  178. }
  179. return testResult < 0;
  180. }
  181. bool JUCE_CALLTYPE Process::isRunningUnderDebugger() throw()
  182. {
  183. return juce_isRunningUnderDebugger();
  184. }
  185. void Process::raisePrivilege()
  186. {
  187. // If running suid root, change effective user
  188. // to root
  189. if (geteuid() != 0 && getuid() == 0)
  190. {
  191. setreuid (geteuid(), getuid());
  192. setregid (getegid(), getgid());
  193. }
  194. }
  195. void Process::lowerPrivilege()
  196. {
  197. // If runing suid root, change effective user
  198. // back to real user
  199. if (geteuid() == 0 && getuid() != 0)
  200. {
  201. setreuid (geteuid(), getuid());
  202. setregid (getegid(), getgid());
  203. }
  204. }
  205. #if JUCE_BUILD_GUI_CLASSES
  206. void* Process::loadDynamicLibrary (const String& name)
  207. {
  208. return dlopen ((const char*) name.toUTF8(), RTLD_LOCAL | RTLD_NOW);
  209. }
  210. void Process::freeDynamicLibrary (void* handle)
  211. {
  212. dlclose(handle);
  213. }
  214. void* Process::getProcedureEntryPoint (void* libraryHandle, const String& procedureName)
  215. {
  216. return dlsym (libraryHandle, (const char*) procedureName);
  217. }
  218. #endif
  219. END_JUCE_NAMESPACE