1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright © 2021 Keith Packard
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 *
18 * 3. Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33 * OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/lock.h>
39 #include "pico-onexit.h"
40
41 struct on_exit {
42 union on_exit_func func;
43 void *arg;
44 int kind;
45 };
46
47 static struct on_exit on_exits[ATEXIT_MAX];
48
49 int
_on_exit(int kind,union on_exit_func func,void * arg)50 _on_exit(int kind, union on_exit_func func, void *arg)
51 {
52 int ret = -1;
53 int o;
54 __LIBC_LOCK();
55 for (o = 0; o < ATEXIT_MAX; o++) {
56 if (on_exits[o].kind == PICO_ONEXIT_EMPTY) {
57 on_exits[o].func = func;
58 on_exits[o].arg = arg;
59 on_exits[o].kind = kind;
60 ret = 0;
61 break;
62 }
63 }
64 __LIBC_UNLOCK();
65 return ret;
66 }
67
68 int
on_exit(void (* func)(int,void *),void * arg)69 on_exit (void (*func)(int, void *),void *arg)
70 {
71 union on_exit_func func_u = { .on_exit = func };
72 return _on_exit(PICO_ONEXIT_ONEXIT, func_u, arg);
73 }
74
75 void
__call_exitprocs(int code,void * param)76 __call_exitprocs(int code, void *param)
77 {
78 (void) param;
79 for (;;) {
80 int i;
81 union on_exit_func func = {0};
82 int kind = PICO_ONEXIT_EMPTY;
83 void *arg = 0;
84
85 __LIBC_LOCK();
86 for (i = ATEXIT_MAX - 1; i >= 0; i--) {
87 kind = on_exits[i].kind;
88 if (kind != PICO_ONEXIT_EMPTY) {
89 func = on_exits[i].func;
90 arg = on_exits[i].arg;
91 memset(&on_exits[i], '\0', sizeof(struct on_exit));
92 break;
93 }
94 }
95 __LIBC_UNLOCK();
96 switch (kind) {
97 case PICO_ONEXIT_EMPTY:
98 return;
99 case PICO_ONEXIT_ONEXIT:
100 func.on_exit(code, arg);
101 break;
102 case PICO_ONEXIT_ATEXIT:
103 func.atexit();
104 break;
105 case PICO_ONEXIT_CXA_ATEXIT:
106 func.cxa_atexit(arg);
107 break;
108 }
109 }
110 }
111