init.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* Copyright (C) 2002-2007, 2008, 2009 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <limits.h>
  18. #include <signal.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <sys/param.h>
  22. #include <sys/resource.h>
  23. #include <pthreadP.h>
  24. #include <atomic.h>
  25. #include <ldsodefs.h>
  26. #include <tls.h>
  27. #include <fork.h>
  28. #include <version.h>
  29. #include <smp.h>
  30. #include <lowlevellock.h>
  31. #include <bits/kernel-features.h>
  32. #include <stdio.h>
  33. /* Size and alignment of static TLS block. */
  34. size_t __static_tls_size;
  35. size_t __static_tls_align_m1;
  36. #ifndef __ASSUME_SET_ROBUST_LIST
  37. /* Negative if we do not have the system call and we can use it. */
  38. int __set_robust_list_avail;
  39. # define set_robust_list_not_avail() \
  40. __set_robust_list_avail = -1
  41. #else
  42. # define set_robust_list_not_avail() do { } while (0)
  43. #endif
  44. #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  45. /* Nonzero if we do not have FUTEX_CLOCK_REALTIME. */
  46. int __have_futex_clock_realtime;
  47. # define __set_futex_clock_realtime() \
  48. __have_futex_clock_realtime = 1
  49. #else
  50. #define __set_futex_clock_realtime() do { } while (0)
  51. #endif
  52. /* Version of the library, used in libthread_db to detect mismatches. */
  53. static const char nptl_version[] __attribute_used__ = VERSION;
  54. /* For asynchronous cancellation we use a signal. This is the handler. */
  55. static void
  56. sigcancel_handler (int sig, siginfo_t *si, void *ctx)
  57. {
  58. /* Safety check. It would be possible to call this function for
  59. other signals and send a signal from another process. This is not
  60. correct and might even be a security problem. Try to catch as
  61. many incorrect invocations as possible. */
  62. if (sig != SIGCANCEL
  63. || si->si_pid != getpid()
  64. || si->si_code != SI_TKILL)
  65. return;
  66. struct pthread *self = THREAD_SELF;
  67. int oldval = THREAD_GETMEM (self, cancelhandling);
  68. while (1)
  69. {
  70. /* We are canceled now. When canceled by another thread this flag
  71. is already set but if the signal is directly send (internally or
  72. from another process) is has to be done here. */
  73. int newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
  74. if (oldval == newval || (oldval & EXITING_BITMASK) != 0)
  75. /* Already canceled or exiting. */
  76. break;
  77. int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
  78. oldval);
  79. if (curval == oldval)
  80. {
  81. /* Set the return value. */
  82. THREAD_SETMEM (self, result, PTHREAD_CANCELED);
  83. /* Make sure asynchronous cancellation is still enabled. */
  84. if ((newval & CANCELTYPE_BITMASK) != 0)
  85. /* Run the registered destructors and terminate the thread. */
  86. __do_cancel ();
  87. break;
  88. }
  89. oldval = curval;
  90. }
  91. }
  92. struct xid_command *__xidcmd attribute_hidden;
  93. /* For asynchronous cancellation we use a signal. This is the handler. */
  94. static void
  95. sighandler_setxid (int sig, siginfo_t *si, void *ctx)
  96. {
  97. /* Safety check. It would be possible to call this function for
  98. other signals and send a signal from another process. This is not
  99. correct and might even be a security problem. Try to catch as
  100. many incorrect invocations as possible. */
  101. if (sig != SIGSETXID
  102. || si->si_pid != getpid()
  103. || si->si_code != SI_TKILL)
  104. return;
  105. INTERNAL_SYSCALL_DECL (err);
  106. INTERNAL_SYSCALL_NCS (__xidcmd->syscall_no, err, 3, __xidcmd->id[0],
  107. __xidcmd->id[1], __xidcmd->id[2]);
  108. /* Reset the SETXID flag. */
  109. struct pthread *self = THREAD_SELF;
  110. int flags, newval;
  111. do
  112. {
  113. flags = THREAD_GETMEM (self, cancelhandling);
  114. newval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling,
  115. flags & ~SETXID_BITMASK, flags);
  116. }
  117. while (flags != newval);
  118. /* And release the futex. */
  119. self->setxid_futex = 1;
  120. lll_futex_wake (&self->setxid_futex, 1, LLL_PRIVATE);
  121. if (atomic_decrement_val (&__xidcmd->cntr) == 0)
  122. lll_futex_wake (&__xidcmd->cntr, 1, LLL_PRIVATE);
  123. }
  124. /* When using __thread for this, we do it in libc so as not
  125. to give libpthread its own TLS segment just for this. */
  126. extern void **__libc_dl_error_tsd (void) __attribute__ ((const));
  127. /* This can be set by the debugger before initialization is complete. */
  128. static bool __nptl_initial_report_events __attribute_used__;
  129. void __pthread_initialize_minimal_internal (void) attribute_hidden;
  130. void
  131. __pthread_initialize_minimal_internal (void)
  132. {
  133. static int initialized = 0;
  134. if (initialized)
  135. return;
  136. initialized = 1;
  137. /* Minimal initialization of the thread descriptor. */
  138. struct pthread *pd = THREAD_SELF;
  139. INTERNAL_SYSCALL_DECL (err);
  140. pd->tid = INTERNAL_SYSCALL (set_tid_address, err, 1, &pd->tid);
  141. THREAD_SETMEM (pd, specific[0], &pd->specific_1stblock[0]);
  142. THREAD_SETMEM (pd, user_stack, true);
  143. if (LLL_LOCK_INITIALIZER != 0)
  144. THREAD_SETMEM (pd, lock, LLL_LOCK_INITIALIZER);
  145. /* Initialize the robust mutex data. */
  146. #ifdef __PTHREAD_MUTEX_HAVE_PREV
  147. pd->robust_prev = &pd->robust_head;
  148. #endif
  149. pd->robust_head.list = &pd->robust_head;
  150. #ifdef __NR_set_robust_list
  151. pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock)
  152. - offsetof (pthread_mutex_t,
  153. __data.__list.__next));
  154. int res = INTERNAL_SYSCALL (set_robust_list, err, 2, &pd->robust_head,
  155. sizeof (struct robust_list_head));
  156. if (INTERNAL_SYSCALL_ERROR_P (res, err))
  157. #endif
  158. set_robust_list_not_avail ();
  159. #ifndef __ASSUME_PRIVATE_FUTEX
  160. /* Private futexes are always used (at least internally) so that
  161. doing the test once this early is beneficial. */
  162. {
  163. int word = 0;
  164. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_futex_time64)
  165. word = INTERNAL_SYSCALL (futex_time64, err, 3, &word,
  166. FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1);
  167. #else
  168. word = INTERNAL_SYSCALL (futex, err, 3, &word,
  169. FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1);
  170. #endif
  171. if (!INTERNAL_SYSCALL_ERROR_P (word, err))
  172. THREAD_SETMEM (pd, header.private_futex, FUTEX_PRIVATE_FLAG);
  173. }
  174. /* Private futexes have been introduced earlier than the
  175. FUTEX_CLOCK_REALTIME flag. We don't have to run the test if we
  176. know the former are not supported. This also means we know the
  177. kernel will return ENOSYS for unknown operations. */
  178. if (THREAD_GETMEM (pd, header.private_futex) != 0)
  179. #endif
  180. #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  181. {
  182. int word = 0;
  183. /* NB: the syscall actually takes six parameters. The last is the
  184. bit mask. But since we will not actually wait at all the value
  185. is irrelevant. Given that passing six parameters is difficult
  186. on some architectures we just pass whatever random value the
  187. calling convention calls for to the kernel. It causes no harm. */
  188. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_futex_time64)
  189. word = INTERNAL_SYSCALL (futex_time64, err, 5, &word,
  190. FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME
  191. | FUTEX_PRIVATE_FLAG, 1, NULL, 0);
  192. #else
  193. word = INTERNAL_SYSCALL (futex, err, 5, &word,
  194. FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME
  195. | FUTEX_PRIVATE_FLAG, 1, NULL, 0);
  196. #endif
  197. assert (INTERNAL_SYSCALL_ERROR_P (word, err));
  198. if (INTERNAL_SYSCALL_ERRNO (word, err) != ENOSYS)
  199. __set_futex_clock_realtime ();
  200. }
  201. #endif
  202. /* Set initial thread's stack block from 0 up to __libc_stack_end.
  203. It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
  204. purposes this is good enough. */
  205. THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end);
  206. /* Initialize the list of all running threads with the main thread. */
  207. INIT_LIST_HEAD (&__stack_user);
  208. list_add (&pd->list, &__stack_user);
  209. /* Before initializing __stack_user, the debugger could not find us and
  210. had to set __nptl_initial_report_events. Propagate its setting. */
  211. THREAD_SETMEM (pd, report_events, __nptl_initial_report_events);
  212. /* Install the cancellation signal handler. If for some reason we
  213. cannot install the handler we do not abort. Maybe we should, but
  214. it is only asynchronous cancellation which is affected. */
  215. struct sigaction sa;
  216. sa.sa_sigaction = sigcancel_handler;
  217. sa.sa_flags = SA_SIGINFO;
  218. __sigemptyset (&sa.sa_mask);
  219. (void) __libc_sigaction (SIGCANCEL, &sa, NULL);
  220. /* Install the handle to change the threads' uid/gid. */
  221. sa.sa_sigaction = sighandler_setxid;
  222. sa.sa_flags = SA_SIGINFO | SA_RESTART;
  223. (void) __libc_sigaction (SIGSETXID, &sa, NULL);
  224. /* The parent process might have left the signals blocked. Just in
  225. case, unblock it. We reuse the signal mask in the sigaction
  226. structure. It is already cleared. */
  227. __sigaddset (&sa.sa_mask, SIGCANCEL);
  228. __sigaddset (&sa.sa_mask, SIGSETXID);
  229. (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &sa.sa_mask,
  230. NULL, _NSIG / 8);
  231. /* Get the size of the static and alignment requirements for the TLS
  232. block. */
  233. size_t static_tls_align;
  234. _dl_get_tls_static_info (&__static_tls_size, &static_tls_align);
  235. /* Make sure the size takes all the alignments into account. */
  236. if (STACK_ALIGN > static_tls_align)
  237. static_tls_align = STACK_ALIGN;
  238. __static_tls_align_m1 = static_tls_align - 1;
  239. __static_tls_size = roundup (__static_tls_size, static_tls_align);
  240. /* Determine the default allowed stack size. This is the size used
  241. in case the user does not specify one. */
  242. struct rlimit limit;
  243. if (getrlimit (RLIMIT_STACK, &limit) != 0
  244. || limit.rlim_cur == RLIM_INFINITY)
  245. /* The system limit is not usable. Use a user-specified or
  246. architecture-specific default. */
  247. limit.rlim_cur = __PTHREADS_STACK_DEFAULT_SIZE__;
  248. if (limit.rlim_cur < PTHREAD_STACK_MIN)
  249. /* The system limit is unusably small.
  250. Use the minimal size acceptable. */
  251. limit.rlim_cur = PTHREAD_STACK_MIN;
  252. /* Do not exceed the user-specified or architecture-specific default */
  253. if (limit.rlim_cur > __PTHREADS_STACK_DEFAULT_SIZE__)
  254. limit.rlim_cur = __PTHREADS_STACK_DEFAULT_SIZE__;
  255. /* Make sure it meets the minimum size that allocate_stack
  256. (allocatestack.c) will demand, which depends on the page size. */
  257. #ifdef SHARED
  258. extern size_t GLRO(dl_pagesize);
  259. const uintptr_t pagesz = GLRO(dl_pagesize);
  260. #else
  261. const uintptr_t pagesz = sysconf (_SC_PAGESIZE);
  262. #endif
  263. const size_t minstack = pagesz + __static_tls_size + MINIMAL_REST_STACK;
  264. if (limit.rlim_cur < minstack)
  265. limit.rlim_cur = minstack;
  266. /* Round the resource limit up to page size. */
  267. limit.rlim_cur = (limit.rlim_cur + pagesz - 1) & -pagesz;
  268. __default_stacksize = limit.rlim_cur;
  269. #ifdef SHARED
  270. /* Transfer the old value from the dynamic linker's internal location. */
  271. *__libc_dl_error_tsd () = *(*GL(dl_error_catch_tsd)) ();
  272. GL(dl_error_catch_tsd) = &__libc_dl_error_tsd;
  273. #endif
  274. GL(dl_init_static_tls) = &__pthread_init_static_tls;
  275. /* Register the fork generation counter with the libc. */
  276. #ifndef TLS_MULTIPLE_THREADS_IN_TCB
  277. __libc_multiple_threads_ptr =
  278. #endif
  279. __libc_pthread_init (&__fork_generation, __reclaim_stacks);
  280. /* Determine whether the machine is SMP or not. */
  281. __is_smp = is_smp_system ();
  282. /* uClibc-specific stdio initialization for threads. */
  283. {
  284. FILE *fp;
  285. _stdio_user_locking = 0; /* 2 if threading not initialized */
  286. for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen) {
  287. if (fp->__user_locking != 1) {
  288. fp->__user_locking = 0;
  289. }
  290. }
  291. }
  292. }
  293. strong_alias (__pthread_initialize_minimal_internal,
  294. __pthread_initialize_minimal)
  295. size_t
  296. __pthread_get_minstack (const pthread_attr_t *attr)
  297. {
  298. return __static_tls_size + PTHREAD_STACK_MIN;
  299. }