2 * atexit.c --- Clean things up when we exit normally.
4 * Copyright Oracle, 2014
5 * Author Darrick J. Wong <darrick.wong@oracle.com>
8 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
13 #ifndef _LARGEFILE_SOURCE
14 #define _LARGEFILE_SOURCE
16 #ifndef _LARGEFILE64_SOURCE
17 #define _LARGEFILE64_SOURCE
32 static struct exit_data *items;
33 static size_t nr_items;
35 static void handle_exit(void)
39 for (ed = items + nr_items - 1; ed >= items; ed--) {
45 ext2fs_free_mem(&items);
50 * Schedule a function to be called at (normal) program termination.
51 * If you want this to be called during a signal exit, you must capture
52 * the signal and call exit() yourself!
54 errcode_t ext2fs_add_exit_fn(ext2_exit_fn func, void *data)
56 struct exit_data *ed, *free_ed = NULL;
61 return EXT2_ET_INVALID_ARGUMENT;
63 for (x = 0, ed = items; x < nr_items; x++, ed++) {
64 if (ed->func == func && ed->data == data)
65 return EXT2_ET_FILE_EXISTS;
77 ret = atexit(handle_exit);
82 ret = ext2fs_resize_mem(0, (nr_items + 1) * sizeof(struct exit_data),
87 items[nr_items].func = func;
88 items[nr_items].data = data;
94 /* Remove a function from the exit cleanup list. */
95 errcode_t ext2fs_remove_exit_fn(ext2_exit_fn func, void *data)
101 return EXT2_ET_INVALID_ARGUMENT;
103 for (x = 0, ed = items; x < nr_items; x++, ed++) {
104 if (ed->func == NULL)
106 if (ed->func == func && ed->data == data) {
107 size_t sz = (nr_items - (x + 1)) *
108 sizeof(struct exit_data);
109 memmove(ed, ed + 1, sz);
110 memset(items + nr_items - 1, 0,
111 sizeof(struct exit_data));