init.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. word = INTERNAL_SYSCALL (futex, err, 3, &word,
  165. FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1);
  166. if (!INTERNAL_SYSCALL_ERROR_P (word, err))
  167. THREAD_SETMEM (pd, header.private_futex, FUTEX_PRIVATE_FLAG);
  168. }
  169. /* Private futexes have been introduced earlier than the
  170. FUTEX_CLOCK_REALTIME flag. We don't have to run the test if we
  171. know the former are not supported. This also means we know the
  172. kernel will return ENOSYS for unknown operations. */
  173. if (THREAD_GETMEM (pd, header.private_futex) != 0)
  174. #endif
  175. #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  176. {
  177. int word = 0;
  178. /* NB: the syscall actually takes six parameters. The last is the
  179. bit mask. But since we will not actually wait at all the value
  180. is irrelevant. Given that passing six parameters is difficult
  181. on some architectures we just pass whatever random value the
  182. calling convention calls for to the kernel. It causes no harm. */
  183. word = INTERNAL_SYSCALL (futex, err, 5, &word,
  184. FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME
  185. | FUTEX_PRIVATE_FLAG, 1, NULL, 0);
  186. assert (INTERNAL_SYSCALL_ERROR_P (word, err));
  187. if (INTERNAL_SYSCALL_ERRNO (word, err) != ENOSYS)
  188. __set_futex_clock_realtime ();
  189. }
  190. #endif
  191. /* Set initial thread's stack block from 0 up to __libc_stack_end.
  192. It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
  193. purposes this is good enough. */
  194. THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end);
  195. /* Initialize the list of all running threads with the main thread. */
  196. INIT_LIST_HEAD (&__stack_user);
  197. list_add (&pd->list, &__stack_user);
  198. /* Before initializing __stack_user, the debugger could not find us and
  199. had to set __nptl_initial_report_events. Propagate its setting. */
  200. THREAD_SETMEM (pd, report_events, __nptl_initial_report_events);
  201. /* Install the cancellation signal handler. If for some reason we
  202. cannot install the handler we do not abort. Maybe we should, but
  203. it is only asynchronous cancellation which is affected. */
  204. struct sigaction sa;
  205. sa.sa_sigaction = sigcancel_handler;
  206. sa.sa_flags = SA_SIGINFO;
  207. __sigemptyset (&sa.sa_mask);
  208. (void) __libc_sigaction (SIGCANCEL, &sa, NULL);
  209. /* Install the handle to change the threads' uid/gid. */
  210. sa.sa_sigaction = sighandler_setxid;
  211. sa.sa_flags = SA_SIGINFO | SA_RESTART;
  212. (void) __libc_sigaction (SIGSETXID, &sa, NULL);
  213. /* The parent process might have left the signals blocked. Just in
  214. case, unblock it. We reuse the signal mask in the sigaction
  215. structure. It is already cleared. */
  216. __sigaddset (&sa.sa_mask, SIGCANCEL);
  217. __sigaddset (&sa.sa_mask, SIGSETXID);
  218. (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &sa.sa_mask,
  219. NULL, _NSIG / 8);
  220. /* Get the size of the static and alignment requirements for the TLS
  221. block. */
  222. size_t static_tls_align;
  223. _dl_get_tls_static_info (&__static_tls_size, &static_tls_align);
  224. /* Make sure the size takes all the alignments into account. */
  225. if (STACK_ALIGN > static_tls_align)
  226. static_tls_align = STACK_ALIGN;
  227. __static_tls_align_m1 = static_tls_align - 1;
  228. __static_tls_size = roundup (__static_tls_size, static_tls_align);
  229. /* Determine the default allowed stack size. This is the size used
  230. in case the user does not specify one. */
  231. struct rlimit limit;
  232. if (getrlimit (RLIMIT_STACK, &limit) != 0
  233. || limit.rlim_cur == RLIM_INFINITY)
  234. /* The system limit is not usable. Use a user-specified or
  235. architecture-specific default. */
  236. limit.rlim_cur = __PTHREADS_STACK_DEFAULT_SIZE__;
  237. if (limit.rlim_cur < PTHREAD_STACK_MIN)
  238. /* The system limit is unusably small.
  239. Use the minimal size acceptable. */
  240. limit.rlim_cur = PTHREAD_STACK_MIN;
  241. /* Do not exceed the user-specified or architecture-specific default */
  242. if (limit.rlim_cur > __PTHREADS_STACK_DEFAULT_SIZE__)
  243. limit.rlim_cur = __PTHREADS_STACK_DEFAULT_SIZE__;
  244. /* Make sure it meets the minimum size that allocate_stack
  245. (allocatestack.c) will demand, which depends on the page size. */
  246. #ifdef SHARED
  247. extern size_t GLRO(dl_pagesize);
  248. const uintptr_t pagesz = GLRO(dl_pagesize);
  249. #else
  250. const uintptr_t pagesz = sysconf (_SC_PAGESIZE);
  251. #endif
  252. const size_t minstack = pagesz + __static_tls_size + MINIMAL_REST_STACK;
  253. if (limit.rlim_cur < minstack)
  254. limit.rlim_cur = minstack;
  255. /* Round the resource limit up to page size. */
  256. limit.rlim_cur = (limit.rlim_cur + pagesz - 1) & -pagesz;
  257. __default_stacksize = limit.rlim_cur;
  258. #ifdef SHARED
  259. /* Transfer the old value from the dynamic linker's internal location. */
  260. *__libc_dl_error_tsd () = *(*GL(dl_error_catch_tsd)) ();
  261. GL(dl_error_catch_tsd) = &__libc_dl_error_tsd;
  262. #endif
  263. GL(dl_init_static_tls) = &__pthread_init_static_tls;
  264. /* Register the fork generation counter with the libc. */
  265. #ifndef TLS_MULTIPLE_THREADS_IN_TCB
  266. __libc_multiple_threads_ptr =
  267. #endif
  268. __libc_pthread_init (&__fork_generation, __reclaim_stacks);
  269. /* Determine whether the machine is SMP or not. */
  270. __is_smp = is_smp_system ();
  271. /* uClibc-specific stdio initialization for threads. */
  272. {
  273. FILE *fp;
  274. _stdio_user_locking = 0; /* 2 if threading not initialized */
  275. for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen) {
  276. if (fp->__user_locking != 1) {
  277. fp->__user_locking = 0;
  278. }
  279. }
  280. }
  281. }
  282. strong_alias (__pthread_initialize_minimal_internal,
  283. __pthread_initialize_minimal)
  284. size_t
  285. __pthread_get_minstack (const pthread_attr_t *attr)
  286. {
  287. return __static_tls_size + PTHREAD_STACK_MIN;
  288. }