* Versions.def: Add GLIBC_2.6 to libpthread.
[glibc.git] / nptl / sysdeps / unix / sysv / linux / register-atfork.c
blobcb5b2b832f0c16c59235b0496c588db1f80cd24d
1 /* Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <fork.h>
26 /* Lock to protect allocation and deallocation of fork handlers. */
27 lll_lock_t __fork_lock = LLL_LOCK_INITIALIZER;
30 /* Number of pre-allocated handler entries. */
31 #define NHANDLER 48
33 /* Memory pool for fork handler structures. */
34 static struct fork_handler_pool
36 struct fork_handler_pool *next;
37 struct fork_handler mem[NHANDLER];
38 } fork_handler_pool;
41 static struct fork_handler *
42 fork_handler_alloc (void)
44 struct fork_handler_pool *runp = &fork_handler_pool;
45 struct fork_handler *result = NULL;
46 unsigned int i;
50 /* Search for an empty entry. */
51 for (i = 0; i < NHANDLER; ++i)
52 if (runp->mem[i].refcntr == 0)
53 goto found;
55 while ((runp = runp->next) != NULL);
57 /* We have to allocate a new entry. */
58 runp = (struct fork_handler_pool *) calloc (1, sizeof (*runp));
59 if (runp != NULL)
61 /* Enqueue the new memory pool into the list. */
62 runp->next = fork_handler_pool.next;
63 fork_handler_pool.next = runp;
65 /* We use the last entry on the page. This means when we start
66 searching from the front the next time we will find the first
67 entry unused. */
68 i = NHANDLER - 1;
70 found:
71 result = &runp->mem[i];
72 result->refcntr = 1;
73 result->need_signal = 0;
76 return result;
80 int
81 __register_atfork (prepare, parent, child, dso_handle)
82 void (*prepare) (void);
83 void (*parent) (void);
84 void (*child) (void);
85 void *dso_handle;
87 /* Get the lock to not conflict with other allocations. */
88 lll_lock (__fork_lock);
90 struct fork_handler *newp = fork_handler_alloc ();
92 if (newp != NULL)
94 /* Initialize the new record. */
95 newp->prepare_handler = prepare;
96 newp->parent_handler = parent;
97 newp->child_handler = child;
98 newp->dso_handle = dso_handle;
100 newp->next = __fork_handlers;
101 __fork_handlers = newp;
104 /* Release the lock. */
105 lll_unlock (__fork_lock);
107 return newp == NULL ? ENOMEM : 0;
109 libc_hidden_def (__register_atfork)
112 libc_freeres_fn (free_mem)
114 /* Get the lock to not conflict with running forks. */
115 lll_lock (__fork_lock);
117 /* No more fork handlers. */
118 __fork_handlers = NULL;
120 /* Free eventually alloated memory blocks for the object pool. */
121 struct fork_handler_pool *runp = fork_handler_pool.next;
123 memset (&fork_handler_pool, '\0', sizeof (fork_handler_pool));
125 /* Release the lock. */
126 lll_unlock (__fork_lock);
128 /* We can free the memory after releasing the lock. */
129 while (runp != NULL)
131 struct fork_handler_pool *oldp = runp;
132 runp = runp->next;
133 free (oldp);