diff options
| -rw-r--r-- | src/lib/libc/stdlib/Makefile.inc | 6 | ||||
| -rw-r--r-- | src/lib/libc/stdlib/atexit.c | 39 | ||||
| -rw-r--r-- | src/lib/libc/stdlib/atexit.h | 9 | ||||
| -rw-r--r-- | src/lib/libc/stdlib/thread_atexit.c | 49 |
4 files changed, 64 insertions, 39 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc index e754e09196..55b8018522 100644 --- a/src/lib/libc/stdlib/Makefile.inc +++ b/src/lib/libc/stdlib/Makefile.inc | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | # $OpenBSD: Makefile.inc,v 1.63 2017/03/24 16:15:31 otto Exp $ | 1 | # $OpenBSD: Makefile.inc,v 1.64 2017/12/16 20:06:55 guenther Exp $ |
| 2 | 2 | ||
| 3 | # stdlib sources | 3 | # stdlib sources |
| 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib | 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib |
| @@ -10,7 +10,9 @@ SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \ | |||
| 10 | merge.c posix_pty.c qsort.c radixsort.c rand.c random.c \ | 10 | merge.c posix_pty.c qsort.c radixsort.c rand.c random.c \ |
| 11 | realpath.c remque.c setenv.c strtoimax.c \ | 11 | realpath.c remque.c setenv.c strtoimax.c \ |
| 12 | strtol.c strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c \ | 12 | strtol.c strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c \ |
| 13 | system.c tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c \ | 13 | system.c \ |
| 14 | tfind.c thread_atexit.c tsearch.c \ | ||
| 15 | _rand48.c drand48.c erand48.c jrand48.c \ | ||
| 14 | lcong48.c lrand48.c mrand48.c nrand48.c seed48.c srand48.c \ | 16 | lcong48.c lrand48.c mrand48.c nrand48.c seed48.c srand48.c \ |
| 15 | _Exit.c icdb.c | 17 | _Exit.c icdb.c |
| 16 | 18 | ||
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c index fe4d5bc79f..ea9dd129c1 100644 --- a/src/lib/libc/stdlib/atexit.c +++ b/src/lib/libc/stdlib/atexit.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: atexit.c,v 1.26 2017/12/05 21:11:10 kettenis Exp $ */ | 1 | /* $OpenBSD: atexit.c,v 1.27 2017/12/16 20:06:56 guenther Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2002 Daniel Hartmeier | 3 | * Copyright (c) 2002 Daniel Hartmeier |
| 4 | * All rights reserved. | 4 | * All rights reserved. |
| @@ -31,24 +31,14 @@ | |||
| 31 | 31 | ||
| 32 | #include <sys/types.h> | 32 | #include <sys/types.h> |
| 33 | #include <sys/mman.h> | 33 | #include <sys/mman.h> |
| 34 | #include <dlfcn.h> | ||
| 35 | #include <elf.h> | ||
| 36 | #pragma weak _DYNAMIC | ||
| 37 | #include <stdlib.h> | 34 | #include <stdlib.h> |
| 38 | #include <string.h> | 35 | #include <string.h> |
| 36 | #include <tib.h> | ||
| 39 | #include <unistd.h> | 37 | #include <unistd.h> |
| 38 | |||
| 40 | #include "atexit.h" | 39 | #include "atexit.h" |
| 41 | #include "atfork.h" | 40 | #include "atfork.h" |
| 42 | #include "thread_private.h" | 41 | #include "thread_private.h" |
| 43 | #include "tib.h" | ||
| 44 | |||
| 45 | typeof(dlctl) dlctl asm("_dlctl") __attribute__((weak)); | ||
| 46 | |||
| 47 | struct thread_atexit_fn { | ||
| 48 | void (*func)(void *); | ||
| 49 | void *arg; | ||
| 50 | struct thread_atexit_fn *next; | ||
| 51 | }; | ||
| 52 | 42 | ||
| 53 | struct atexit *__atexit; | 43 | struct atexit *__atexit; |
| 54 | static int restartloop; | 44 | static int restartloop; |
| @@ -133,29 +123,6 @@ atexit(void (*fn)(void)) | |||
| 133 | } | 123 | } |
| 134 | DEF_STRONG(atexit); | 124 | DEF_STRONG(atexit); |
| 135 | 125 | ||
| 136 | __weak_alias(__cxa_thread_atexit, __cxa_thread_atexit_impl); | ||
| 137 | |||
| 138 | int | ||
| 139 | __cxa_thread_atexit_impl(void (*func)(void *), void *arg, void *dso) | ||
| 140 | { | ||
| 141 | struct thread_atexit_fn *fnp; | ||
| 142 | struct tib *tib = TIB_GET(); | ||
| 143 | |||
| 144 | fnp = calloc(1, sizeof(struct thread_atexit_fn)); | ||
| 145 | if (fnp == NULL) | ||
| 146 | return -1; | ||
| 147 | |||
| 148 | if (_DYNAMIC) | ||
| 149 | dlctl(NULL, DL_REFERENCE, dso); | ||
| 150 | |||
| 151 | fnp->func = func; | ||
| 152 | fnp->arg = arg; | ||
| 153 | fnp->next = tib->tib_atexit; | ||
| 154 | tib->tib_atexit = fnp; | ||
| 155 | |||
| 156 | return 0; | ||
| 157 | } | ||
| 158 | |||
| 159 | void | 126 | void |
| 160 | _thread_finalize(void) | 127 | _thread_finalize(void) |
| 161 | { | 128 | { |
diff --git a/src/lib/libc/stdlib/atexit.h b/src/lib/libc/stdlib/atexit.h index d9bfed852f..f2fa7bd83f 100644 --- a/src/lib/libc/stdlib/atexit.h +++ b/src/lib/libc/stdlib/atexit.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: atexit.h,v 1.11 2017/12/05 13:45:31 kettenis Exp $ */ | 1 | /* $OpenBSD: atexit.h,v 1.12 2017/12/16 20:06:56 guenther Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 2002 Daniel Hartmeier | 4 | * Copyright (c) 2002 Daniel Hartmeier |
| @@ -41,6 +41,13 @@ struct atexit { | |||
| 41 | } fns[1]; /* the table itself */ | 41 | } fns[1]; /* the table itself */ |
| 42 | }; | 42 | }; |
| 43 | 43 | ||
| 44 | /* a chain of these are hung off each thread's TIB's tib_atexit member */ | ||
| 45 | struct thread_atexit_fn { | ||
| 46 | void (*func)(void *); | ||
| 47 | void *arg; | ||
| 48 | struct thread_atexit_fn *next; | ||
| 49 | }; | ||
| 50 | |||
| 44 | __BEGIN_HIDDEN_DECLS | 51 | __BEGIN_HIDDEN_DECLS |
| 45 | extern struct atexit *__atexit; /* points to head of LIFO stack */ | 52 | extern struct atexit *__atexit; /* points to head of LIFO stack */ |
| 46 | __END_HIDDEN_DECLS | 53 | __END_HIDDEN_DECLS |
diff --git a/src/lib/libc/stdlib/thread_atexit.c b/src/lib/libc/stdlib/thread_atexit.c new file mode 100644 index 0000000000..2e00428eba --- /dev/null +++ b/src/lib/libc/stdlib/thread_atexit.c | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | /* $OpenBSD: thread_atexit.c,v 1.1 2017/12/16 20:06:56 guenther Exp $ */ | ||
| 2 | /* | ||
| 3 | * Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org> | ||
| 4 | * | ||
| 5 | * Permission to use, copy, modify, and distribute this software for any | ||
| 6 | * purpose with or without fee is hereby granted, provided that the above | ||
| 7 | * copyright notice and this permission notice appear in all copies. | ||
| 8 | * | ||
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <dlfcn.h> | ||
| 19 | #include <elf.h> | ||
| 20 | #pragma weak _DYNAMIC | ||
| 21 | #include <stdlib.h> | ||
| 22 | #include <tib.h> | ||
| 23 | |||
| 24 | #include "atexit.h" | ||
| 25 | |||
| 26 | typeof(dlctl) dlctl asm("_dlctl") __attribute__((weak)); | ||
| 27 | |||
| 28 | __weak_alias(__cxa_thread_atexit, __cxa_thread_atexit_impl); | ||
| 29 | |||
| 30 | int | ||
| 31 | __cxa_thread_atexit_impl(void (*func)(void *), void *arg, void *dso) | ||
| 32 | { | ||
| 33 | struct thread_atexit_fn *fnp; | ||
| 34 | struct tib *tib = TIB_GET(); | ||
| 35 | |||
| 36 | fnp = calloc(1, sizeof(struct thread_atexit_fn)); | ||
| 37 | if (fnp == NULL) | ||
| 38 | return -1; | ||
| 39 | |||
| 40 | if (_DYNAMIC) | ||
| 41 | dlctl(NULL, DL_REFERENCE, dso); | ||
| 42 | |||
| 43 | fnp->func = func; | ||
| 44 | fnp->arg = arg; | ||
| 45 | fnp->next = tib->tib_atexit; | ||
| 46 | tib->tib_atexit = fnp; | ||
| 47 | |||
| 48 | return 0; | ||
| 49 | } | ||
