x86: Add _CET_ENDBR to functions in dl-tlsdesc.S
[glibc.git] / nptl / register-atfork.c
blob5ff1c1be8cd0432c45f9726625922e44c1c1b173
1 /* Copyright (C) 2002-2018 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <fork.h>
23 #include <atomic.h>
25 #define DYNARRAY_ELEMENT struct fork_handler
26 #define DYNARRAY_STRUCT fork_handler_list
27 #define DYNARRAY_PREFIX fork_handler_list_
28 #define DYNARRAY_INITIAL_SIZE 48
29 #include <malloc/dynarray-skeleton.c>
31 static struct fork_handler_list fork_handlers;
32 static bool fork_handler_init = false;
34 static int atfork_lock = LLL_LOCK_INITIALIZER;
36 int
37 __register_atfork (void (*prepare) (void), void (*parent) (void),
38 void (*child) (void), void *dso_handle)
40 lll_lock (atfork_lock, LLL_PRIVATE);
42 if (!fork_handler_init)
44 fork_handler_list_init (&fork_handlers);
45 fork_handler_init = true;
48 struct fork_handler *newp = fork_handler_list_emplace (&fork_handlers);
49 if (newp != NULL)
51 newp->prepare_handler = prepare;
52 newp->parent_handler = parent;
53 newp->child_handler = child;
54 newp->dso_handle = dso_handle;
57 /* Release the lock. */
58 lll_unlock (atfork_lock, LLL_PRIVATE);
60 return newp == NULL ? ENOMEM : 0;
62 libc_hidden_def (__register_atfork)
64 static struct fork_handler *
65 fork_handler_list_find (struct fork_handler_list *fork_handlers,
66 void *dso_handle)
68 for (size_t i = 0; i < fork_handler_list_size (fork_handlers); i++)
70 struct fork_handler *elem = fork_handler_list_at (fork_handlers, i);
71 if (elem->dso_handle == dso_handle)
72 return elem;
74 return NULL;
77 void
78 __unregister_atfork (void *dso_handle)
80 lll_lock (atfork_lock, LLL_PRIVATE);
82 struct fork_handler *first = fork_handler_list_find (&fork_handlers,
83 dso_handle);
84 /* Removing is done by shifting the elements in the way the elements
85 that are not to be removed appear in the beginning in dynarray.
86 This avoid the quadradic run-time if a naive strategy to remove and
87 shift one element at time. */
88 if (first != NULL)
90 struct fork_handler *new_end = first;
91 first++;
92 for (; first != fork_handler_list_end (&fork_handlers); ++first)
94 if (first->dso_handle != dso_handle)
96 *new_end = *first;
97 ++new_end;
101 ptrdiff_t removed = first - new_end;
102 for (size_t i = 0; i < removed; i++)
103 fork_handler_list_remove_last (&fork_handlers);
106 lll_unlock (atfork_lock, LLL_PRIVATE);
109 void
110 __run_fork_handlers (enum __run_fork_handler_type who)
112 struct fork_handler *runp;
114 if (who == atfork_run_prepare)
116 lll_lock (atfork_lock, LLL_PRIVATE);
117 size_t sl = fork_handler_list_size (&fork_handlers);
118 for (size_t i = sl; i > 0; i--)
120 runp = fork_handler_list_at (&fork_handlers, i - 1);
121 if (runp->prepare_handler != NULL)
122 runp->prepare_handler ();
125 else
127 size_t sl = fork_handler_list_size (&fork_handlers);
128 for (size_t i = 0; i < sl; i++)
130 runp = fork_handler_list_at (&fork_handlers, i);
131 if (who == atfork_run_child && runp->child_handler)
132 runp->child_handler ();
133 else if (who == atfork_run_parent && runp->parent_handler)
134 runp->parent_handler ();
136 lll_unlock (atfork_lock, LLL_PRIVATE);
141 libc_freeres_fn (free_mem)
143 lll_lock (atfork_lock, LLL_PRIVATE);
145 fork_handler_list_free (&fork_handlers);
147 lll_unlock (atfork_lock, LLL_PRIVATE);