1 /* $OpenBSD: atfork.c,v 1.4 2022/12/27 17:10:06 jmc Exp $ */
2
3 /*
4 * Copyright (c) 2008 Kurt Miller <kurt@openbsd.org>
5 * Copyright (c) 2008 Philip Guenther <guenther@openbsd.org>
6 * Copyright (c) 2003 Daniel Eischen <deischen@freebsd.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Neither the name of the author nor the names of any co-contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD: /repoman/r/ncvs/src/lib/libc_r/uthread/uthread_atfork.c,v 1.1 2004/12/10 03:36:45 grog Exp $
31 */
32
33 #include <errno.h>
34 #include <pthread.h>
35 #include <stdlib.h>
36
37 #include "thread_private.h"
38 #include "atfork.h"
39
40 int _thread_atfork(void (*_prepare)(void), void (*_parent)(void),
41 void (*_child)(void), void *_dso);
42 PROTO_NORMAL(_thread_atfork);
43
44 int
_thread_atfork(void (* prepare)(void),void (* parent)(void),void (* child)(void),void * dso)45 _thread_atfork(void (*prepare)(void), void (*parent)(void),
46 void (*child)(void), void *dso)
47 {
48 struct atfork_fn *af;
49
50 if ((af = malloc(sizeof *af)) == NULL)
51 return (ENOMEM);
52
53 af->fn_prepare = prepare;
54 af->fn_parent = parent;
55 af->fn_child = child;
56 af->fn_dso = dso;
57 _ATFORK_LOCK();
58 TAILQ_INSERT_TAIL(&_atfork_list, af, fn_next);
59 _ATFORK_UNLOCK();
60 return (0);
61 }
62 DEF_STRONG(_thread_atfork);
63
64 /*
65 * Copy of pthread_atfork() used by libc and anything statically linked
66 * into the executable. This passes NULL for the dso, so the callbacks
67 * are never removed by dlclose()
68 */
69 int
pthread_atfork(void (* prep)(void),void (* parent)(void),void (* child)(void))70 pthread_atfork(void (*prep)(void), void (*parent)(void), void (*child)(void))
71 {
72 return (_thread_atfork(prep, parent, child, NULL));
73 }
74