Back to index

glibc  2.9
libc_pthread_init.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <unistd.h>
00021 #include <list.h>
00022 #include <fork.h>
00023 #include <dl-sysdep.h>
00024 #include <tls.h>
00025 #include <string.h>
00026 #include <pthreadP.h>
00027 #include <bits/libc-lock.h>
00028 #include <sysdep.h>
00029 #include <ldsodefs.h>
00030 
00031 
00032 #ifdef TLS_MULTIPLE_THREADS_IN_TCB
00033 void
00034 #else
00035 extern int __libc_multiple_threads attribute_hidden;
00036 
00037 int *
00038 #endif
00039 __libc_pthread_init (ptr, reclaim, functions)
00040      unsigned long int *ptr;
00041      void (*reclaim) (void);
00042      const struct pthread_functions *functions;
00043 {
00044   /* Remember the pointer to the generation counter in libpthread.  */
00045   __fork_generation_pointer = ptr;
00046 
00047   /* Called by a child after fork.  */
00048   __register_atfork (NULL, NULL, reclaim, NULL);
00049 
00050 #ifdef SHARED
00051   /* Copy the function pointers into an array in libc.  This enables
00052      access with just one memory reference but moreso, it prevents
00053      hijacking the function pointers with just one pointer change.  We
00054      "encrypt" the function pointers since we cannot write-protect the
00055      array easily enough.  */
00056   union ptrhack
00057   {
00058     struct pthread_functions pf;
00059     void *parr[1];
00060   } const *src;
00061   union ptrhack *dest;
00062 # define NPTRS (sizeof (struct pthread_functions) / sizeof (void *))
00063 
00064   src = (const void *) functions;
00065   dest = (void *) &__libc_pthread_functions;
00066 
00067   for (size_t cnt = 0; cnt < NPTRS; ++cnt)
00068     {
00069       void *p = src->parr[cnt];
00070       PTR_MANGLE (p);
00071       dest->parr[cnt] = p;
00072     }
00073   __libc_pthread_functions_init = 1;
00074 #endif
00075 
00076 #ifndef TLS_MULTIPLE_THREADS_IN_TCB
00077   return &__libc_multiple_threads;
00078 #endif
00079 }
00080 
00081 #ifdef SHARED
00082 libc_freeres_fn (freeres_libptread)
00083 {
00084   if (__libc_pthread_functions_init)
00085     PTHFCT_CALL (ptr_freeres, ());
00086 }
00087 #endif