File: trap.def

package info (click to toggle)
bash 5.3-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 43,860 kB
  • sloc: ansic: 134,738; sh: 8,866; yacc: 5,966; makefile: 4,697; perl: 4,105; asm: 48; awk: 23; sed: 16
file content (368 lines) | stat: -rw-r--r-- 10,412 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
This file is trap.def, from which is created trap.c.
It implements the builtin "trap" in Bash.

Copyright (C) 1987-2023 Free Software Foundation, Inc.

This file is part of GNU Bash, the Bourne Again SHell.

Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bash.  If not, see <http://www.gnu.org/licenses/>.

$PRODUCES trap.c

$BUILTIN trap
$FUNCTION trap_builtin
$SHORT_DOC trap [-Plp] [[action] signal_spec ...]
Trap signals and other events.

Defines and activates handlers to be run when the shell receives signals
or other conditions.

ACTION is a command to be read and executed when the shell receives the
signal(s) SIGNAL_SPEC.  If ACTION is absent (and a single SIGNAL_SPEC
is supplied) or `-', each specified signal is reset to its original
value.  If ACTION is the null string each SIGNAL_SPEC is ignored by the
shell and by the commands it invokes.

If a SIGNAL_SPEC is EXIT (0) ACTION is executed on exit from the shell.
If a SIGNAL_SPEC is DEBUG, ACTION is executed before every simple command
and selected other commands. If a SIGNAL_SPEC is RETURN, ACTION is
executed each time a shell function or a script run by the . or source
builtins finishes executing.  A SIGNAL_SPEC of ERR means to execute ACTION
each time a command's failure would cause the shell to exit when the -e
option is enabled.

If no arguments are supplied, trap prints the list of commands associated
with each trapped signal in a form that may be reused as shell input to
restore the same signal dispositions.

Options:
  -l	print a list of signal names and their corresponding numbers
  -p	display the trap commands associated with each SIGNAL_SPEC in a
		form that may be reused as shell input; or for all trapped
		signals if no arguments are supplied
  -P	display the trap commands associated with each SIGNAL_SPEC. At least
		one SIGNAL_SPEC must be supplied. -P and -p cannot be used
		together.

Each SIGNAL_SPEC is either a signal name in <signal.h> or a signal number.
Signal names are case insensitive and the SIG prefix is optional.  A
signal may be sent to the shell with "kill -signal $$".

Exit Status:
Returns success unless a SIGSPEC is invalid or an invalid option is given.
$END

#include <config.h>

#if defined (HAVE_UNISTD_H)
#  ifdef _MINIX
#    include <sys/types.h>
#  endif
#  include <unistd.h>
#endif

#include "../bashtypes.h"
#include <signal.h>
#include <stdio.h>
#include "../bashansi.h"

#include "../shell.h"
#include "../trap.h"
#include "common.h"
#include "bashgetopt.h"

static void showtrap (int, int);
static int display_traps (WORD_LIST *, int);

/* The trap command:

   trap <action> <sigspec ...>
   trap <signum ...>
   trap -l
   trap -p [sigspec ...]
   trap [--]

   Set things up so that ARG is executed when SIGNAL(s) N is received.
   If ARG is the empty string, then ignore the SIGNAL(s).  If there is
   no ARG, then set the trap for SIGNAL(s) to its original value.  Just
   plain "trap" means to print out the list of commands associated with
   each signal number.  Single arg of "-l" means list the signal names. */

/* Possible operations to perform on the list of signals.*/
#define SET 0			/* Set this signal to first_arg. */
#define REVERT 1		/* Revert to this signals original value. */
#define IGNORE 2		/* Ignore this signal. */

/* Flags saying how to display the trap list. */
#define DISP_TRAPCMD	1
#define DISP_ALLSIGS	2
#define DISP_ACTIONONLY	4

int
trap_builtin (WORD_LIST *list)
{
  int list_signal_names, result, opt, dflags;

  list_signal_names = dflags = 0;
  result = EXECUTION_SUCCESS;

  reset_internal_getopt ();
  while ((opt = internal_getopt (list, "lpP")) != -1)
    {
      switch (opt)
	{
	case 'l':
	  list_signal_names++;
	  break;
	case 'p':
	  dflags |= DISP_TRAPCMD;
	  break;
	case 'P':
	  dflags |= DISP_ACTIONONLY;
	  break;
	CASE_HELPOPT;
	default:
	  builtin_usage ();
	  return (EX_USAGE);
	}
    }
  list = loptend;

  opt = DSIG_NOCASE|DSIG_SIGPREFIX;	/* flags for decode_signal */

  if ((dflags & DISP_TRAPCMD) && (dflags & DISP_ACTIONONLY))
    {
      builtin_error ("cannot specify both -p and -P");
      return EX_USAGE;
    }
  if ((dflags & DISP_ACTIONONLY) && list == 0)
    {
      builtin_error ("-P requires at least one signal name");
      return EX_USAGE;
    }

  if (list_signal_names)
    return (sh_chkwrite (display_signal_list ((WORD_LIST *)NULL, 1)));
  else if (dflags || list == 0)
    {
      initialize_terminating_signals ();
      get_all_original_signals ();
      if (dflags & DISP_TRAPCMD)
	dflags |= posixly_correct ? DISP_ALLSIGS : 0;
      return (sh_chkwrite (display_traps (list, dflags)));
    }
  else
    {
      char *first_arg;
      int operation, sig, first_signal;

      operation = SET;
      first_arg = list->word->word;
      first_signal = first_arg && *first_arg && all_digits (first_arg) && signal_object_p (first_arg, opt);

      /* Backwards compatibility.  XXX - question about whether or not we
	 should throw an error if an all-digit argument doesn't correspond
	 to a valid signal number (e.g., if it's `50' on a system with only
	 32 signals).  */
      if (first_signal)
	operation = REVERT;
      /* When in posix mode, the historical behavior of looking for a
	 missing first argument is disabled.  To revert to the original
	 signal handling disposition, use `-' as the first argument. */
      else if (posixly_correct == 0 && first_arg && *first_arg &&
		(*first_arg != '-' || first_arg[1]) &&
		signal_object_p (first_arg, opt) && list->next == 0)
	operation = REVERT;
      else
	{
	  list = list->next;
	  if (list == 0)
	    {
	      builtin_usage ();
	      return (EX_USAGE);
	    }
	  else if (*first_arg == '\0')
	    operation = IGNORE;
	  else if (first_arg[0] == '-' && !first_arg[1])
	    operation = REVERT;
	}

      /* If we're in a command substitution, we haven't freed the trap strings
	 (though we reset the signal handlers).  If we're setting a trap to
	 handle a signal here, free the rest of the trap strings since they
	 don't apply any more. */
      if (subshell_environment & SUBSHELL_RESETTRAP)
	{
	  free_trap_strings ();
	  subshell_environment &= ~SUBSHELL_RESETTRAP;
	}

      while (list)
	{
	  sig = decode_signal (list->word->word, opt);

	  if (sig == NO_SIG)
	    {
	      sh_invalidsig (list->word->word);
	      result = EXECUTION_FAILURE;
	    }
	  else
	    {
	      switch (operation)
		{
		  case SET:
		    set_signal (sig, first_arg);
		    break;

		  case REVERT:
		    restore_default_signal (sig);

		    /* Signals that the shell treats specially need special
		       handling. */
		    switch (sig)
		      {
		      case SIGINT:
			/* XXX - should we do this if original disposition
			   was SIG_IGN? Yes, because setup_async_signals can
			   set the original disposition to SIG_IGN to keep
			   restore_signal from restoring the SIGINT
			   disposition from when the shell was invoked, and
			   we want to allow an asynchronous subshell to
			   use `trap' to restore the default disposition or
			   set a trap command. */
			if (interactive)
			  set_signal_handler (SIGINT, sigint_sighandler);
			/* special cases for interactive == 0 */
			else if (interactive_shell && (sourcelevel||running_trap||parse_and_execute_level))
			  set_signal_handler (SIGINT, sigint_sighandler);
			else
			  set_signal_handler (SIGINT, termsig_sighandler);
			break;

		      case SIGQUIT:
			/* Always ignore SIGQUIT, but allow a posix-mode shell
			   that is running asynchronously and has ignored
			   SIGQUIT to reset it to the default. POSIX interp 751. */
			if (posixly_correct && signal_is_async_ignored (SIGQUIT))
			  set_signal_handler (SIGQUIT, termsig_sighandler);
			else
			  set_signal_handler (SIGQUIT, SIG_IGN);
			break;
		      case SIGTERM:
#if defined (JOB_CONTROL)
		      case SIGTTIN:
		      case SIGTTOU:
		      case SIGTSTP:
#endif /* JOB_CONTROL */
			if (interactive)
			  set_signal_handler (sig, SIG_IGN);
			break;
		      }
		    break;

		  case IGNORE:
		    ignore_signal (sig);
		    break;
		}
	    }
	  list = list->next;
	}
    }

  return (result);
}

static void
showtrap (int i, int show_default)
{
  char *t, *p, *sn;
  int free_t;

  free_t = 1;
  p = trap_list[i];
  if (p == (char *)DEFAULT_SIG && signal_is_hard_ignored (i) == 0)
    {
      if (show_default)
	t = "-";
      else
	return;
      free_t = 0;
    }
  else if (signal_is_hard_ignored (i))
    t = (char *)NULL;
  else
    t = (p == (char *)IGNORE_SIG) ? (char *)NULL : sh_single_quote (p);

  sn = signal_name (i);
  /* Make sure that signals whose names are unknown (for whatever reason)
     are printed as signal numbers. */
  if (STREQN (sn, "SIGJUNK", 7) || STREQN (sn, "unknown", 7))
    printf ("trap -- %s %d\n", t ? t : "''", i);
  else if (posixly_correct)
    {
      if (STREQN (sn, "SIG", 3))
	printf ("trap -- %s %s\n", t ? t : "''", sn+3);
      else
	printf ("trap -- %s %s\n", t ? t : "''", sn);
    }
  else
    printf ("trap -- %s %s\n", t ? t : "''", sn);

  if (free_t)
    FREE (t);
}

/* Flags saying how to display the trap list. */
#define DISP_TRAPCMD    1
#define DISP_ALLSIGS    2
#define DISP_ACTIONONLY 4


static int
display_traps (WORD_LIST *list, int flags)
{
  int result, i, show_all;

  show_all = flags & DISP_ALLSIGS;
  if (list == 0)
    {
      for (i = 0; i < BASH_NSIG; i++)
	showtrap (i, show_all);
      return (EXECUTION_SUCCESS);
    }

  for (result = EXECUTION_SUCCESS; list; list = list->next)
    {
      i = decode_signal (list->word->word, DSIG_NOCASE|DSIG_SIGPREFIX);
      if (i == NO_SIG)
	{
	  sh_invalidsig (list->word->word);
	  result = EXECUTION_FAILURE;
	}
      else if (flags & DISP_ACTIONONLY)
	{
	  char *t;
	  t = trap_list[i];
	  if (t == (char *)DEFAULT_SIG || signal_is_hard_ignored (i))
	    continue;
	  else if (t == (char *)IGNORE_SIG)
	    t = "";
	  printf ("%s\n", t);
	}
      else
	showtrap (i, show_all);
    }

  return (result);
}