|
Packit |
6c4009 |
/* Copyright (C) 2002-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <fork.h>
|
|
Packit |
6c4009 |
#include <atomic.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define DYNARRAY_ELEMENT struct fork_handler
|
|
Packit |
6c4009 |
#define DYNARRAY_STRUCT fork_handler_list
|
|
Packit |
6c4009 |
#define DYNARRAY_PREFIX fork_handler_list_
|
|
Packit |
6c4009 |
#define DYNARRAY_INITIAL_SIZE 48
|
|
Packit |
6c4009 |
#include <malloc/dynarray-skeleton.c>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static struct fork_handler_list fork_handlers;
|
|
Packit |
6c4009 |
static bool fork_handler_init = false;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int atfork_lock = LLL_LOCK_INITIALIZER;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
__register_atfork (void (*prepare) (void), void (*parent) (void),
|
|
Packit |
6c4009 |
void (*child) (void), void *dso_handle)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
lll_lock (atfork_lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (!fork_handler_init)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
fork_handler_list_init (&fork_handlers);
|
|
Packit |
6c4009 |
fork_handler_init = true;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct fork_handler *newp = fork_handler_list_emplace (&fork_handlers);
|
|
Packit |
6c4009 |
if (newp != NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
newp->prepare_handler = prepare;
|
|
Packit |
6c4009 |
newp->parent_handler = parent;
|
|
Packit |
6c4009 |
newp->child_handler = child;
|
|
Packit |
6c4009 |
newp->dso_handle = dso_handle;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Release the lock. */
|
|
Packit |
6c4009 |
lll_unlock (atfork_lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return newp == NULL ? ENOMEM : 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
libc_hidden_def (__register_atfork)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static struct fork_handler *
|
|
Packit |
6c4009 |
fork_handler_list_find (struct fork_handler_list *fork_handlers,
|
|
Packit |
6c4009 |
void *dso_handle)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
for (size_t i = 0; i < fork_handler_list_size (fork_handlers); i++)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct fork_handler *elem = fork_handler_list_at (fork_handlers, i);
|
|
Packit |
6c4009 |
if (elem->dso_handle == dso_handle)
|
|
Packit |
6c4009 |
return elem;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
return NULL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
void
|
|
Packit |
6c4009 |
__unregister_atfork (void *dso_handle)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
lll_lock (atfork_lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct fork_handler *first = fork_handler_list_find (&fork_handlers,
|
|
Packit |
6c4009 |
dso_handle);
|
|
Packit |
6c4009 |
/* Removing is done by shifting the elements in the way the elements
|
|
Packit |
6c4009 |
that are not to be removed appear in the beginning in dynarray.
|
|
Packit |
6c4009 |
This avoid the quadradic run-time if a naive strategy to remove and
|
|
Packit |
6c4009 |
shift one element at time. */
|
|
Packit |
6c4009 |
if (first != NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct fork_handler *new_end = first;
|
|
Packit |
6c4009 |
first++;
|
|
Packit |
6c4009 |
for (; first != fork_handler_list_end (&fork_handlers); ++first)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (first->dso_handle != dso_handle)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
*new_end = *first;
|
|
Packit |
6c4009 |
++new_end;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ptrdiff_t removed = first - new_end;
|
|
Packit |
6c4009 |
for (size_t i = 0; i < removed; i++)
|
|
Packit |
6c4009 |
fork_handler_list_remove_last (&fork_handlers);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
lll_unlock (atfork_lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
void
|
|
Packit Service |
541783 |
__run_fork_handlers (enum __run_fork_handler_type who)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct fork_handler *runp;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (who == atfork_run_prepare)
|
|
Packit |
6c4009 |
{
|
|
Packit Service |
541783 |
lll_lock (atfork_lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
size_t sl = fork_handler_list_size (&fork_handlers);
|
|
Packit |
6c4009 |
for (size_t i = sl; i > 0; i--)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
runp = fork_handler_list_at (&fork_handlers, i - 1);
|
|
Packit |
6c4009 |
if (runp->prepare_handler != NULL)
|
|
Packit |
6c4009 |
runp->prepare_handler ();
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
size_t sl = fork_handler_list_size (&fork_handlers);
|
|
Packit |
6c4009 |
for (size_t i = 0; i < sl; i++)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
runp = fork_handler_list_at (&fork_handlers, i);
|
|
Packit |
6c4009 |
if (who == atfork_run_child && runp->child_handler)
|
|
Packit |
6c4009 |
runp->child_handler ();
|
|
Packit |
6c4009 |
else if (who == atfork_run_parent && runp->parent_handler)
|
|
Packit |
6c4009 |
runp->parent_handler ();
|
|
Packit |
6c4009 |
}
|
|
Packit Service |
541783 |
lll_unlock (atfork_lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
libc_freeres_fn (free_mem)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
lll_lock (atfork_lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
fork_handler_list_free (&fork_handlers);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
lll_unlock (atfork_lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
}
|