blob: 85a10b578c58fb1d9fefd9a00379fd2943d44b4a [file] [log] [blame]
Jari Aalto31859422009-01-12 13:36:28 +00001/* unwind_prot.c - a simple unwind-protect system for internal variables */
2
Jari Aalto726f6381996-08-26 18:22:31 +00003/* I can't stand it anymore! Please can't we just write the
4 whole Unix system in lisp or something? */
5
Jari Aalto31859422009-01-12 13:36:28 +00006/* Copyright (C) 1987-2009 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00007
Jari Aalto31859422009-01-12 13:36:28 +00008 This file is part of GNU Bash, the Bourne Again SHell.
Jari Aalto726f6381996-08-26 18:22:31 +00009
Jari Aalto31859422009-01-12 13:36:28 +000010 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.
Jari Aalto726f6381996-08-26 18:22:31 +000014
Jari Aalto31859422009-01-12 13:36:28 +000015 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.
Jari Aalto726f6381996-08-26 18:22:31 +000019
Jari Aalto31859422009-01-12 13:36:28 +000020 You should have received a copy of the GNU General Public License
21 along with Bash. If not, see <http://www.gnu.org/licenses/>.
22*/
Jari Aalto726f6381996-08-26 18:22:31 +000023
24/* **************************************************************** */
25/* */
26/* Unwind Protection Scheme for Bash */
27/* */
28/* **************************************************************** */
Jari Aalto726f6381996-08-26 18:22:31 +000029#include "config.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000030
31#include "bashtypes.h"
Jari Aaltod166f041997-06-05 14:59:13 +000032#include "bashansi.h"
33
Jari Aaltoccc6cda1996-12-23 17:02:34 +000034#if defined (HAVE_UNISTD_H)
35# include <unistd.h>
36#endif
37
Jari Aaltof73dda02001-11-13 17:56:06 +000038#if STDC_HEADERS
39# include <stddef.h>
40#endif
41
42#ifndef offsetof
43# define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
44#endif
45
Jari Aalto726f6381996-08-26 18:22:31 +000046#include "command.h"
47#include "general.h"
48#include "unwind_prot.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000049#include "sig.h"
Chet Rameyac50fba2014-02-26 09:36:43 -050050#include "quit.h"
Chet Ramey495aee42011-11-22 19:11:26 -050051#include "error.h" /* for internal_warning */
Jari Aalto726f6381996-08-26 18:22:31 +000052
Jari Aaltof73dda02001-11-13 17:56:06 +000053/* Structure describing a saved variable and the value to restore it to. */
Jari Aaltod166f041997-06-05 14:59:13 +000054typedef struct {
Jari Aaltof73dda02001-11-13 17:56:06 +000055 char *variable;
Jari Aaltod166f041997-06-05 14:59:13 +000056 int size;
Jari Aaltof73dda02001-11-13 17:56:06 +000057 char desired_setting[1]; /* actual size is `size' */
Jari Aaltod166f041997-06-05 14:59:13 +000058} SAVED_VAR;
59
Jari Aaltof73dda02001-11-13 17:56:06 +000060/* If HEAD.CLEANUP is null, then ARG.V contains a tag to throw back to.
61 If HEAD.CLEANUP is restore_variable, then SV.V contains the saved
62 variable. Otherwise, call HEAD.CLEANUP (ARG.V) to clean up. */
63typedef union uwp {
64 struct uwp_head {
65 union uwp *next;
66 Function *cleanup;
67 } head;
68 struct {
69 struct uwp_head uwp_head;
70 char *v;
71 } arg;
72 struct {
73 struct uwp_head uwp_head;
74 SAVED_VAR v;
75 } sv;
76} UNWIND_ELT;
77
Jari Aalto7117c2d2002-07-17 14:10:11 +000078
Jari Aaltof73dda02001-11-13 17:56:06 +000079static void without_interrupts __P((VFunction *, char *, char *));
80static void unwind_frame_discard_internal __P((char *, char *));
81static void unwind_frame_run_internal __P((char *, char *));
82static void add_unwind_protect_internal __P((Function *, char *));
83static void remove_unwind_protect_internal __P((char *, char *));
84static void run_unwind_protects_internal __P((char *, char *));
85static void clear_unwind_protects_internal __P((char *, char *));
86static inline void restore_variable __P((SAVED_VAR *));
87static void unwind_protect_mem_internal __P((char *, char *));
Jari Aalto726f6381996-08-26 18:22:31 +000088
89static UNWIND_ELT *unwind_protect_list = (UNWIND_ELT *)NULL;
90
Jari Aalto7117c2d2002-07-17 14:10:11 +000091#define uwpalloc(elt) (elt) = (UNWIND_ELT *)xmalloc (sizeof (UNWIND_ELT))
92#define uwpfree(elt) free(elt)
Jari Aalto726f6381996-08-26 18:22:31 +000093
94/* Run a function without interrupts. This relies on the fact that the
95 FUNCTION cannot change the value of interrupt_immediately. (I.e., does
96 not call QUIT (). */
97static void
98without_interrupts (function, arg1, arg2)
99 VFunction *function;
100 char *arg1, *arg2;
101{
102 int old_interrupt_immediately;
103
104 old_interrupt_immediately = interrupt_immediately;
105 interrupt_immediately = 0;
106
107 (*function)(arg1, arg2);
108
109 interrupt_immediately = old_interrupt_immediately;
110}
111
112/* Start the beginning of a region. */
113void
114begin_unwind_frame (tag)
115 char *tag;
116{
117 add_unwind_protect ((Function *)NULL, tag);
118}
119
120/* Discard the unwind protects back to TAG. */
121void
122discard_unwind_frame (tag)
123 char *tag;
124{
125 if (unwind_protect_list)
126 without_interrupts (unwind_frame_discard_internal, tag, (char *)NULL);
127}
128
129/* Run the unwind protects back to TAG. */
130void
131run_unwind_frame (tag)
132 char *tag;
133{
134 if (unwind_protect_list)
135 without_interrupts (unwind_frame_run_internal, tag, (char *)NULL);
136}
137
138/* Add the function CLEANUP with ARG to the list of unwindable things. */
139void
140add_unwind_protect (cleanup, arg)
141 Function *cleanup;
142 char *arg;
143{
144 without_interrupts (add_unwind_protect_internal, (char *)cleanup, arg);
145}
146
147/* Remove the top unwind protect from the list. */
148void
149remove_unwind_protect ()
150{
151 if (unwind_protect_list)
152 without_interrupts
153 (remove_unwind_protect_internal, (char *)NULL, (char *)NULL);
154}
155
156/* Run the list of cleanup functions in unwind_protect_list. */
157void
158run_unwind_protects ()
159{
160 if (unwind_protect_list)
161 without_interrupts
162 (run_unwind_protects_internal, (char *)NULL, (char *)NULL);
163}
164
Jari Aalto28ef6c32001-04-06 19:14:31 +0000165/* Erase the unwind-protect list. If flags is 1, free the elements. */
166void
167clear_unwind_protect_list (flags)
168 int flags;
169{
Jari Aaltof73dda02001-11-13 17:56:06 +0000170 char *flag;
171
Jari Aalto28ef6c32001-04-06 19:14:31 +0000172 if (unwind_protect_list)
Jari Aaltof73dda02001-11-13 17:56:06 +0000173 {
174 flag = flags ? "" : (char *)NULL;
175 without_interrupts
176 (clear_unwind_protects_internal, flag, (char *)NULL);
177 }
Jari Aalto28ef6c32001-04-06 19:14:31 +0000178}
179
Jari Aalto31859422009-01-12 13:36:28 +0000180int
181have_unwind_protects ()
182{
183 return (unwind_protect_list != 0);
184}
185
Jari Aalto726f6381996-08-26 18:22:31 +0000186/* **************************************************************** */
187/* */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000188/* The Actual Functions */
Jari Aalto726f6381996-08-26 18:22:31 +0000189/* */
190/* **************************************************************** */
191
192static void
193add_unwind_protect_internal (cleanup, arg)
194 Function *cleanup;
195 char *arg;
196{
197 UNWIND_ELT *elt;
198
Jari Aalto7117c2d2002-07-17 14:10:11 +0000199 uwpalloc (elt);
Jari Aaltof73dda02001-11-13 17:56:06 +0000200 elt->head.next = unwind_protect_list;
201 elt->head.cleanup = cleanup;
202 elt->arg.v = arg;
Jari Aalto726f6381996-08-26 18:22:31 +0000203 unwind_protect_list = elt;
204}
205
206static void
Jari Aalto28ef6c32001-04-06 19:14:31 +0000207remove_unwind_protect_internal (ignore1, ignore2)
208 char *ignore1, *ignore2;
Jari Aalto726f6381996-08-26 18:22:31 +0000209{
Jari Aaltod166f041997-06-05 14:59:13 +0000210 UNWIND_ELT *elt;
Jari Aalto726f6381996-08-26 18:22:31 +0000211
Jari Aaltod166f041997-06-05 14:59:13 +0000212 elt = unwind_protect_list;
Jari Aalto726f6381996-08-26 18:22:31 +0000213 if (elt)
214 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000215 unwind_protect_list = unwind_protect_list->head.next;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000216 uwpfree (elt);
Jari Aalto726f6381996-08-26 18:22:31 +0000217 }
218}
219
220static void
Jari Aalto28ef6c32001-04-06 19:14:31 +0000221run_unwind_protects_internal (ignore1, ignore2)
222 char *ignore1, *ignore2;
Jari Aalto726f6381996-08-26 18:22:31 +0000223{
Jari Aaltof73dda02001-11-13 17:56:06 +0000224 unwind_frame_run_internal ((char *) NULL, (char *) NULL);
Jari Aalto726f6381996-08-26 18:22:31 +0000225}
226
227static void
Jari Aalto28ef6c32001-04-06 19:14:31 +0000228clear_unwind_protects_internal (flag, ignore)
229 char *flag, *ignore;
230{
Jari Aaltof73dda02001-11-13 17:56:06 +0000231 if (flag)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000232 {
233 while (unwind_protect_list)
234 remove_unwind_protect_internal ((char *)NULL, (char *)NULL);
235 }
236 unwind_protect_list = (UNWIND_ELT *)NULL;
237}
238
239static void
240unwind_frame_discard_internal (tag, ignore)
241 char *tag, *ignore;
Jari Aalto726f6381996-08-26 18:22:31 +0000242{
243 UNWIND_ELT *elt;
Chet Ramey495aee42011-11-22 19:11:26 -0500244 int found;
Jari Aalto726f6381996-08-26 18:22:31 +0000245
Chet Ramey495aee42011-11-22 19:11:26 -0500246 found = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000247 while (elt = unwind_protect_list)
248 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000249 unwind_protect_list = unwind_protect_list->head.next;
250 if (elt->head.cleanup == 0 && (STREQ (elt->arg.v, tag)))
Jari Aalto726f6381996-08-26 18:22:31 +0000251 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000252 uwpfree (elt);
Chet Ramey495aee42011-11-22 19:11:26 -0500253 found = 1;
Jari Aalto726f6381996-08-26 18:22:31 +0000254 break;
255 }
256 else
Jari Aalto7117c2d2002-07-17 14:10:11 +0000257 uwpfree (elt);
Jari Aalto726f6381996-08-26 18:22:31 +0000258 }
Chet Ramey495aee42011-11-22 19:11:26 -0500259
260 if (found == 0)
261 internal_warning ("unwind_frame_discard: %s: frame not found", tag);
Jari Aalto726f6381996-08-26 18:22:31 +0000262}
263
Jari Aaltof73dda02001-11-13 17:56:06 +0000264/* Restore the value of a variable, based on the contents of SV.
265 sv->desired_setting is a block of memory SIZE bytes long holding the
266 value itself. This block of memory is copied back into the variable. */
267static inline void
268restore_variable (sv)
269 SAVED_VAR *sv;
270{
271 FASTCOPY (sv->desired_setting, sv->variable, sv->size);
272}
273
Jari Aalto726f6381996-08-26 18:22:31 +0000274static void
Jari Aalto28ef6c32001-04-06 19:14:31 +0000275unwind_frame_run_internal (tag, ignore)
276 char *tag, *ignore;
Jari Aalto726f6381996-08-26 18:22:31 +0000277{
278 UNWIND_ELT *elt;
Chet Ramey495aee42011-11-22 19:11:26 -0500279 int found;
Jari Aalto726f6381996-08-26 18:22:31 +0000280
Chet Ramey495aee42011-11-22 19:11:26 -0500281 found = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000282 while (elt = unwind_protect_list)
283 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000284 unwind_protect_list = elt->head.next;
Jari Aalto726f6381996-08-26 18:22:31 +0000285
286 /* If tag, then compare. */
Chet Ramey495aee42011-11-22 19:11:26 -0500287 if (elt->head.cleanup == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000288 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000289 if (tag && STREQ (elt->arg.v, tag))
Jari Aalto726f6381996-08-26 18:22:31 +0000290 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000291 uwpfree (elt);
Chet Ramey495aee42011-11-22 19:11:26 -0500292 found = 1;
Jari Aalto726f6381996-08-26 18:22:31 +0000293 break;
294 }
Jari Aalto726f6381996-08-26 18:22:31 +0000295 }
296 else
297 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000298 if (elt->head.cleanup == (Function *) restore_variable)
299 restore_variable (&elt->sv.v);
300 else
301 (*(elt->head.cleanup)) (elt->arg.v);
Jari Aalto726f6381996-08-26 18:22:31 +0000302 }
Jari Aaltof73dda02001-11-13 17:56:06 +0000303
Jari Aalto7117c2d2002-07-17 14:10:11 +0000304 uwpfree (elt);
Jari Aalto726f6381996-08-26 18:22:31 +0000305 }
Chet Ramey495aee42011-11-22 19:11:26 -0500306 if (tag && found == 0)
307 internal_warning ("unwind_frame_run: %s: frame not found", tag);
Jari Aalto726f6381996-08-26 18:22:31 +0000308}
309
Jari Aaltod166f041997-06-05 14:59:13 +0000310static void
Jari Aaltof73dda02001-11-13 17:56:06 +0000311unwind_protect_mem_internal (var, psize)
312 char *var;
313 char *psize;
Jari Aaltod166f041997-06-05 14:59:13 +0000314{
Jari Aaltof73dda02001-11-13 17:56:06 +0000315 int size, allocated;
316 UNWIND_ELT *elt;
Jari Aalto726f6381996-08-26 18:22:31 +0000317
Jari Aaltof73dda02001-11-13 17:56:06 +0000318 size = *(int *) psize;
319 allocated = size + offsetof (UNWIND_ELT, sv.v.desired_setting[0]);
320 elt = (UNWIND_ELT *)xmalloc (allocated);
321 elt->head.next = unwind_protect_list;
322 elt->head.cleanup = (Function *) restore_variable;
323 elt->sv.v.variable = var;
324 elt->sv.v.size = size;
325 FASTCOPY (var, elt->sv.v.desired_setting, size);
326 unwind_protect_list = elt;
Jari Aalto726f6381996-08-26 18:22:31 +0000327}
328
329/* Save the value of a variable so it will be restored when unwind-protects
Jari Aaltof73dda02001-11-13 17:56:06 +0000330 are run. VAR is a pointer to the variable. SIZE is the size in
331 bytes of VAR. */
Jari Aalto726f6381996-08-26 18:22:31 +0000332void
Jari Aaltof73dda02001-11-13 17:56:06 +0000333unwind_protect_mem (var, size)
334 char *var;
Jari Aalto726f6381996-08-26 18:22:31 +0000335 int size;
336{
Jari Aaltof73dda02001-11-13 17:56:06 +0000337 without_interrupts (unwind_protect_mem_internal, var, (char *) &size);
Jari Aalto726f6381996-08-26 18:22:31 +0000338}
Chet Ramey495aee42011-11-22 19:11:26 -0500339
340#if defined (DEBUG)
341#include <stdio.h>
342
343void
344print_unwind_protect_tags ()
345{
346 UNWIND_ELT *elt;
347
348 elt = unwind_protect_list;
349 while (elt)
350 {
351 unwind_protect_list = unwind_protect_list->head.next;
352 if (elt->head.cleanup == 0)
353 fprintf(stderr, "tag: %s\n", elt->arg.v);
354 elt = unwind_protect_list;
355 }
356}
357#endif