1
/* $Id: cleanup.c,v 1.1.1.1 2001/05/12 23:00:06 cegger Exp $
2
******************************************************************************
4
LibGG - Functions for adding and removing cleanup callbacks
6
Copyright (C) 1998 Marcus Sundberg [marcus@ggi-project.org]
8
Permission is hereby granted, free of charge, to any person obtaining a
9
copy of this software and associated documentation files (the "Software"),
10
to deal in the Software without restriction, including without limitation
11
the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
and/or sell copies of the Software, and to permit persons to whom the
13
Software is furnished to do so, subject to the following conditions:
15
The above copyright notice and this permission notice shall be included in
16
all copies or substantial portions of the Software.
18
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21
THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
******************************************************************************
39
typedef struct funclist {
42
struct funclist *next;
45
static funclist *firstfunc = NULL;
46
static ggcleanup_func *currentfunc = NULL;
47
static int is_registered = 0;
48
static int force_exit = 0;
51
static void do_cleanup(void)
53
funclist *curr = firstfunc, *prev;
55
while (curr != NULL) {
56
currentfunc = curr->func;
57
curr->func(curr->arg);
61
/* Make sure we don't call the function again */
62
ggUnregisterCleanup(prev->func, prev->arg);
66
static void cleanup_function(void)
76
static void cleanup_function_ret(int retval)
88
#if defined(HAVE_SIGNAL) || defined(HAVE_SIGACTION)
90
typedef void (ggsighandler)(int);
94
ggsighandler *oldhandler;
96
struct sigaction oldsa;
101
static gg_siginfo siglist[] = {
109
{ SIGQUIT, SIG_ERR },
115
{ SIGTRAP, SIG_ERR },
118
{ SIGABRT, SIG_ERR },
127
{ SIGSEGV, SIG_ERR },
130
{ SIGPIPE, SIG_ERR },
133
{ SIGALRM, SIG_ERR },
136
{ SIGTERM, SIG_ERR },
139
{ SIGSTKFLT, SIG_ERR },
144
#if defined(SIGPOLL) && (SIGPOLL != SIGIO)
145
{ SIGPOLL, SIG_ERR },
148
{ SIGXCPU, SIG_ERR },
151
{ SIGXFSZ, SIG_ERR },
154
{ SIGVTALRM, SIG_ERR },
157
{ SIGPROF, SIG_ERR },
163
{ SIGLOST, SIG_ERR },
166
{ SIGUNUSED, SIG_ERR }
170
#define SIGLIST_LEN sizeof(siglist)/sizeof(gg_siginfo)
173
static void sighandler(int signum)
176
ggsighandler *oldfunc = NULL;
178
for (i = 0; i < SIGLIST_LEN; i++) {
179
if (siglist[i].sig == signum) {
180
if (siglist[i].oldhandler == SIG_IGN) {
181
signal(signum, sighandler);
184
if (siglist[i].oldhandler != SIG_DFL) {
185
oldfunc = siglist[i].oldhandler;
193
fprintf(stderr, "Terminating on signal %d\n", signum);
194
cleanup_function_ret(signum);
199
#ifdef HAVE_SIGACTION
201
static void setsa(struct sigaction *sa, ggsighandler *func)
203
sa->sa_handler = func;
205
sigemptyset(&sa->sa_mask);
208
static void unregister_sighandler(void)
212
for (i = 0; i < SIGLIST_LEN; i++) {
215
if (siglist[i].oldhandler == SIG_ERR) continue;
216
if (sigaction(siglist[i].sig, NULL, &sa) != 0) continue;
217
if (sa.sa_flags == 0 && sa.sa_handler == sighandler) {
218
sigaction(siglist[i].sig, &siglist[i].oldsa, NULL);
220
/* Someone else has changed this signal action, so
221
we shouldn't touch it */
224
siglist[i].oldhandler = SIG_ERR;
228
static void register_sighandler(void)
232
for (i = 0; i < SIGLIST_LEN; i++) {
233
if (sigaction(siglist[i].sig, NULL, &siglist[i].oldsa) != 0) {
236
if (siglist[i].oldsa.sa_handler == SIG_DFL ||
237
siglist[i].oldsa.sa_handler == SIG_IGN) {
240
setsa(&sa, sighandler);
241
if (sigaction(siglist[i].sig, &sa, NULL) != 0) {
244
siglist[i].oldhandler = siglist[i].oldsa.sa_handler;
249
#else /* HAVE_SIGACTION */
251
static void unregister_sighandler(void)
255
for (i = 0; i < SIGLIST_LEN; i++) {
256
ggsighandler *oldfunc;
258
if (siglist[i].oldhandler == SIG_ERR) continue;
259
oldfunc = signal(siglist[i].sig, siglist[i].oldhandler);
260
if (oldfunc != sighandler) {
261
/* Someone else has changed this signal handler, so
262
we shouldn't touch it */
263
signal(siglist[i].sig, oldfunc);
265
siglist[i].oldhandler = SIG_ERR;
269
static void register_sighandler(void)
273
for (i = 0; i < SIGLIST_LEN; i++) {
274
ggsighandler *oldfunc;
276
oldfunc = signal(siglist[i].sig, sighandler);
277
if (oldfunc != SIG_DFL &&
278
oldfunc != SIG_IGN) {
279
/* If there's a sighandler registered already we don't
281
if (oldfunc != SIG_ERR) {
282
signal(siglist[i].sig, oldfunc);
286
siglist[i].oldhandler = oldfunc;
289
#endif /* HAVE_SIGACTION */
291
#else /* HAVE_SIGNAL || HAVE_SIGACTION */
293
#define register_sighandler() /* empty */
294
#define unregister_sighandler() /* empty */
296
#endif /* HAVE_SIGNAL || HAVE_SIGACTION */
298
int ggRegisterCleanup(ggcleanup_func *func, void *arg)
303
ggLock(_gg_global_mutex);
304
register_sighandler();
305
if (!is_registered) {
306
if (atexit(cleanup_function) != 0) {
312
if ((newlist = malloc(sizeof(funclist))) == NULL) {
316
newlist->func = func;
319
/* Insert first in list */
320
newlist->next = firstfunc;
324
ggUnlock(_gg_global_mutex);
330
int ggUnregisterCleanup(ggcleanup_func *func, void *arg)
332
funclist *curr = firstfunc, *prev = NULL;
334
if (func == currentfunc) {
335
/* Don't unregister a function while it is being called */
338
ggLock(_gg_global_mutex);
339
while (curr != NULL) {
340
if (curr->func == func && curr->arg == arg) {
341
if (curr == firstfunc) firstfunc = curr->next;
342
else prev->next = curr->next;
344
if (firstfunc == NULL) unregister_sighandler();
345
ggUnlock(_gg_global_mutex);
352
ggUnlock(_gg_global_mutex);
353
return GGI_ENOTALLOC;
357
void ggCleanupForceExit(void)