1 /* unwind_prot.c - a simple unwind-protect system for internal variables */
3 /* I can't stand it anymore! Please can't we just write the
4 whole Unix system in lisp or something? */
6 /* Copyright (C) 1987-2021 Free Software Foundation, Inc.
8 This file is part of GNU Bash, the Bourne Again SHell.
10 Bash is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 Bash is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with Bash. If not, see <http://www.gnu.org/licenses/>.
24 /* **************************************************************** */
26 /* Unwind Protection Scheme for Bash */
28 /* **************************************************************** */
31 #include "bashtypes.h"
34 #if defined (HAVE_UNISTD_H)
38 #if defined (HAVE_STDDEF_H)
43 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
48 #include "unwind_prot.h"
51 #include "bashintl.h" /* for _() */
52 #include "error.h" /* for internal_warning */
55 /* Structure describing a saved variable and the value to restore it to. */
59 char desired_setting
[1]; /* actual size is `size' */
62 /* If HEAD.CLEANUP is null, then ARG.V contains a tag to throw back to.
63 If HEAD.CLEANUP is restore_variable, then SV.V contains the saved
64 variable. Otherwise, call HEAD.CLEANUP (ARG.V) to clean up. */
71 struct uwp_head uwp_head
;
75 struct uwp_head uwp_head
;
80 static void without_interrupts
PARAMS((VFunction
*, char *, char *));
81 static void unwind_frame_discard_internal
PARAMS((char *, char *));
82 static void unwind_frame_run_internal
PARAMS((char *, char *));
83 static void add_unwind_protect_internal
PARAMS((Function
*, char *));
84 static void remove_unwind_protect_internal
PARAMS((char *, char *));
85 static void run_unwind_protects_internal
PARAMS((char *, char *));
86 static void clear_unwind_protects_internal
PARAMS((char *, char *));
87 static inline void restore_variable
PARAMS((SAVED_VAR
*));
88 static void unwind_protect_mem_internal
PARAMS((char *, char *));
90 static UNWIND_ELT
*unwind_protect_list
= (UNWIND_ELT
*)NULL
;
92 /* Allocating from a cache of unwind-protect elements */
93 #define UWCACHESIZE 128
95 sh_obj_cache_t uwcache
= {0, 0, 0};
98 #define uwpalloc(elt) (elt) = (UNWIND_ELT *)xmalloc (sizeof (UNWIND_ELT))
99 #define uwpfree(elt) free(elt)
101 #define uwpalloc(elt) ocache_alloc (uwcache, UNWIND_ELT, elt)
102 #define uwpfree(elt) ocache_free (uwcache, UNWIND_ELT, elt)
108 ocache_create (uwcache
, UNWIND_ELT
, UWCACHESIZE
);
111 /* Run a function without interrupts. This relies on the fact that the
112 FUNCTION cannot call QUIT (). */
114 without_interrupts (function
, arg1
, arg2
)
118 (*function
)(arg1
, arg2
);
121 /* Start the beginning of a region. */
123 begin_unwind_frame (tag
)
126 add_unwind_protect ((Function
*)NULL
, tag
);
129 /* Discard the unwind protects back to TAG. */
131 discard_unwind_frame (tag
)
134 if (unwind_protect_list
)
135 without_interrupts (unwind_frame_discard_internal
, tag
, (char *)NULL
);
138 /* Run the unwind protects back to TAG. */
140 run_unwind_frame (tag
)
143 if (unwind_protect_list
)
144 without_interrupts (unwind_frame_run_internal
, tag
, (char *)NULL
);
147 /* Add the function CLEANUP with ARG to the list of unwindable things. */
149 add_unwind_protect (cleanup
, arg
)
153 without_interrupts (add_unwind_protect_internal
, (char *)cleanup
, arg
);
156 /* Remove the top unwind protect from the list. */
158 remove_unwind_protect ()
160 if (unwind_protect_list
)
162 (remove_unwind_protect_internal
, (char *)NULL
, (char *)NULL
);
165 /* Run the list of cleanup functions in unwind_protect_list. */
167 run_unwind_protects ()
169 if (unwind_protect_list
)
171 (run_unwind_protects_internal
, (char *)NULL
, (char *)NULL
);
174 /* Erase the unwind-protect list. If flags is 1, free the elements. */
176 clear_unwind_protect_list (flags
)
181 if (unwind_protect_list
)
183 flag
= flags
? "" : (char *)NULL
;
185 (clear_unwind_protects_internal
, flag
, (char *)NULL
);
190 have_unwind_protects ()
192 return (unwind_protect_list
!= 0);
196 unwind_protect_tag_on_stack (tag
)
201 elt
= unwind_protect_list
;
204 if (elt
->head
.cleanup
== 0 && STREQ (elt
->arg
.v
, tag
))
206 elt
= elt
->head
.next
;
211 /* **************************************************************** */
213 /* The Actual Functions */
215 /* **************************************************************** */
218 add_unwind_protect_internal (cleanup
, arg
)
225 elt
->head
.next
= unwind_protect_list
;
226 elt
->head
.cleanup
= cleanup
;
228 unwind_protect_list
= elt
;
232 remove_unwind_protect_internal (ignore1
, ignore2
)
233 char *ignore1
, *ignore2
;
237 elt
= unwind_protect_list
;
240 unwind_protect_list
= unwind_protect_list
->head
.next
;
246 run_unwind_protects_internal (ignore1
, ignore2
)
247 char *ignore1
, *ignore2
;
249 unwind_frame_run_internal ((char *) NULL
, (char *) NULL
);
253 clear_unwind_protects_internal (flag
, ignore
)
258 while (unwind_protect_list
)
259 remove_unwind_protect_internal ((char *)NULL
, (char *)NULL
);
261 unwind_protect_list
= (UNWIND_ELT
*)NULL
;
265 unwind_frame_discard_internal (tag
, ignore
)
272 while (elt
= unwind_protect_list
)
274 unwind_protect_list
= unwind_protect_list
->head
.next
;
275 if (elt
->head
.cleanup
== 0 && (STREQ (elt
->arg
.v
, tag
)))
286 internal_warning (_("unwind_frame_discard: %s: frame not found"), tag
);
289 /* Restore the value of a variable, based on the contents of SV.
290 sv->desired_setting is a block of memory SIZE bytes long holding the
291 value itself. This block of memory is copied back into the variable. */
293 restore_variable (sv
)
296 FASTCOPY (sv
->desired_setting
, sv
->variable
, sv
->size
);
300 unwind_frame_run_internal (tag
, ignore
)
307 while (elt
= unwind_protect_list
)
309 unwind_protect_list
= elt
->head
.next
;
311 /* If tag, then compare. */
312 if (elt
->head
.cleanup
== 0)
314 if (tag
&& STREQ (elt
->arg
.v
, tag
))
323 if (elt
->head
.cleanup
== (Function
*) restore_variable
)
324 restore_variable (&elt
->sv
.v
);
326 (*(elt
->head
.cleanup
)) (elt
->arg
.v
);
331 if (tag
&& found
== 0)
332 internal_warning (_("unwind_frame_run: %s: frame not found"), tag
);
336 unwind_protect_mem_internal (var
, psize
)
343 size
= *(int *) psize
;
344 allocated
= size
+ offsetof (UNWIND_ELT
, sv
.v
.desired_setting
[0]);
345 if (allocated
< sizeof (UNWIND_ELT
))
346 allocated
= sizeof (UNWIND_ELT
);
347 elt
= (UNWIND_ELT
*)xmalloc (allocated
);
348 elt
->head
.next
= unwind_protect_list
;
349 elt
->head
.cleanup
= (Function
*) restore_variable
;
350 elt
->sv
.v
.variable
= var
;
351 elt
->sv
.v
.size
= size
;
352 FASTCOPY (var
, elt
->sv
.v
.desired_setting
, size
);
353 unwind_protect_list
= elt
;
356 /* Save the value of a variable so it will be restored when unwind-protects
357 are run. VAR is a pointer to the variable. SIZE is the size in
360 unwind_protect_mem (var
, size
)
364 without_interrupts (unwind_protect_mem_internal
, var
, (char *) &size
);
371 print_unwind_protect_tags ()
375 elt
= unwind_protect_list
;
378 if (elt
->head
.cleanup
== 0)
379 fprintf(stderr
, "tag: %s\n", elt
->arg
.v
);
380 elt
= elt
->head
.next
;