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 #define _DEFAULT_SOURCE
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/lock.h>
40 #include <limits.h>
41 #include "pico-onexit.h"
42 #include "atexit.h"
43 
44 struct on_exit {
45     union on_exit_func  func;
46     void                *arg;
47     int                 kind;
48 };
49 
50 static struct on_exit on_exits[ATEXIT_MAX];
51 
52 int
_on_exit(int kind,union on_exit_func func,void * arg)53 _on_exit(int kind, union on_exit_func func, void *arg)
54 {
55 	int	ret = -1;
56 	int	o;
57 	__LIBC_LOCK();
58 	for (o = 0; o < ATEXIT_MAX; o++) {
59 		if (on_exits[o].kind == PICO_ONEXIT_EMPTY) {
60 			on_exits[o].func = func;
61 			on_exits[o].arg = arg;
62                         on_exits[o].kind = kind;
63 			ret = 0;
64 			break;
65 		}
66 	}
67 	__LIBC_UNLOCK();
68 	return ret;
69 }
70 
71 int
on_exit(void (* func)(int,void *),void * arg)72 on_exit (void (*func)(int, void *),void *arg)
73 {
74     union on_exit_func func_u = { .on_exit = func };
75     return _on_exit(PICO_ONEXIT_ONEXIT, func_u, arg);
76 }
77 
78 void
__call_exitprocs(int code,void * param)79 __call_exitprocs(int code, void *param)
80 {
81         (void) param;
82 	for (;;) {
83 		int	                i;
84                 union on_exit_func      func = {0};
85                 int                     kind = PICO_ONEXIT_EMPTY;
86 		void	                *arg = 0;
87 
88 		__LIBC_LOCK();
89 		for (i = ATEXIT_MAX - 1; i >= 0; i--) {
90                         kind = on_exits[i].kind;
91 			if (kind != PICO_ONEXIT_EMPTY) {
92                                 func = on_exits[i].func;
93                                 arg = on_exits[i].arg;
94                                 memset(&on_exits[i], '\0', sizeof(struct on_exit));
95 				break;
96 			}
97 		}
98 		__LIBC_UNLOCK();
99                 switch (kind) {
100                 case PICO_ONEXIT_EMPTY:
101                         return;
102                 case PICO_ONEXIT_ONEXIT:
103                         func.on_exit(code, arg);
104                         break;
105                 case PICO_ONEXIT_ATEXIT:
106                         func.atexit();
107                         break;
108                 case PICO_ONEXIT_CXA_ATEXIT:
109                         func.cxa_atexit(arg);
110                         break;
111                 }
112 	}
113 }
114