-WARNS6 cleanup (229 warnings)
[dragonfly.git] / bin / sh / trap.c
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * @(#)trap.c   8.5 (Berkeley) 6/5/95
37  * $FreeBSD: src/bin/sh/trap.c,v 1.20.2.2 2002/08/27 01:36:28 tjr Exp $
38  * $DragonFly: src/bin/sh/trap.c,v 1.4 2004/01/30 18:21:18 dillon Exp $
39  */
40
41 #include <signal.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44
45 #include "shell.h"
46 #include "main.h"
47 #include "nodes.h"      /* for other headers */
48 #include "eval.h"
49 #include "jobs.h"
50 #include "show.h"
51 #include "options.h"
52 #include "syntax.h"
53 #include "output.h"
54 #include "memalloc.h"
55 #include "error.h"
56 #include "trap.h"
57 #include "mystring.h"
58 #include "myhistedit.h"
59
60
61 /*
62  * Sigmode records the current value of the signal handlers for the various
63  * modes.  A value of zero means that the current handler is not known.
64  * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
65  */
66
67 #define S_DFL 1                 /* default signal handling (SIG_DFL) */
68 #define S_CATCH 2               /* signal is caught */
69 #define S_IGN 3                 /* signal is ignored (SIG_IGN) */
70 #define S_HARD_IGN 4            /* signal is ignored permanently */
71 #define S_RESET 5               /* temporary - to reset a hard ignored sig */
72
73
74 MKINIT char sigmode[NSIG];      /* current value of signal */
75 int pendingsigs;                /* indicates some signal received */
76 int in_dotrap;                  /* do we execute in a trap handler? */
77 static char *volatile trap[NSIG];       /* trap handler commands */
78 static volatile sig_atomic_t gotsig[NSIG]; 
79                                 /* indicates specified signal received */
80 static int ignore_sigchld;      /* Used while handling SIGCHLD traps. */
81 volatile sig_atomic_t gotwinch;
82
83 static int getsigaction(int, sig_t *);
84
85
86 /*
87  * Map a string to a signal number.
88  *
89  * Note: the signal number may exceed NSIG.
90  */
91 static int
92 sigstring_to_signum(char *sig)
93 {
94
95         if (is_number(sig)) {
96                 int signo;
97
98                 signo = atoi(sig);
99                 return ((signo >= 0 && signo < NSIG) ? signo : (-1));
100         } else if (strcasecmp(sig, "exit") == 0) {
101                 return (0);
102         } else {
103                 int n;
104
105                 if (strncasecmp(sig, "sig", 3) == 0)
106                         sig += 3;
107                 for (n = 1; n < sys_nsig; n++) {
108                         if (sys_signame[n] && strcasecmp(sys_signame[n], sig) == 0)
109                                 return (n);
110                 }
111         }
112         return (-1);
113 }
114
115
116 /*
117  * Print a list of valid signal names.
118  */
119 static void
120 printsignals(void)
121 {
122         int n;
123         int outlen = 0;
124
125         for (n = 1; n < sys_nsig; n++) {
126                 if (sys_signame[n]) {
127                         out1fmt("%s", sys_signame[n]);
128                         outlen += strlen(sys_signame[n]);
129                 } else {
130                         out1fmt("%d", n);
131                         outlen += 3;    /* good enough */
132                 }
133                 ++outlen;
134                 if (outlen > 70 || n == sys_nsig - 1) {
135                         out1str("\n");
136                         outlen = 0;
137                 } else {
138                         out1c(' ');
139                 }
140         }
141 }
142
143
144 /*
145  * The trap builtin.
146  */
147 int
148 trapcmd(int argc, char **argv)
149 {
150         char *action;
151         int signo;
152
153         if (argc <= 1) {
154                 for (signo = 0 ; signo < sys_nsig; signo++) {
155                         if (signo < NSIG && trap[signo] != NULL) {
156                                 if (signo == 0) {
157                                         out1fmt("trap -- '%s' %s\n", 
158                                                 trap[signo], "exit");
159                                 } else if (sys_signame[signo]) {
160                                         out1fmt("trap -- '%s' %s\n", 
161                                                 trap[signo],
162                                                 sys_signame[signo]);
163                                 } else {
164                                         out1fmt("trap -- '%s' %d\n", 
165                                                 trap[signo], signo);
166                                 }
167                         }
168                 }
169                 return 0;
170         }
171         action = NULL;
172         if (*++argv && strcmp(*argv, "--") == 0)
173                 argv++;
174         if (*argv && sigstring_to_signum(*argv) == -1) {
175                 if ((*argv)[0] != '-') {
176                         action = *argv;
177                         argv++;
178                 } else if ((*argv)[1] == '\0') {
179                         argv++;
180                 } else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') {
181                         printsignals();
182                         return 0;
183                 } else {
184                         error("bad option %s", *argv);
185                 }
186         }
187         while (*argv) {
188                 if ((signo = sigstring_to_signum(*argv)) == -1)
189                         error("bad signal %s", *argv);
190                 INTOFF;
191                 if (action)
192                         action = savestr(action);
193                 if (trap[signo])
194                         ckfree(trap[signo]);
195                 trap[signo] = action;
196                 if (signo != 0)
197                         setsignal(signo);
198                 INTON;
199                 argv++;
200         }
201         return 0;
202 }
203
204
205 /*
206  * Clear traps on a fork.
207  */
208 void
209 clear_traps(void)
210 {
211         char *volatile *tp;
212
213         for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) {
214                 if (*tp && **tp) {      /* trap not NULL or SIG_IGN */
215                         INTOFF;
216                         ckfree(*tp);
217                         *tp = NULL;
218                         if (tp != &trap[0])
219                                 setsignal(tp - trap);
220                         INTON;
221                 }
222         }
223 }
224
225
226 /*
227  * Set the signal handler for the specified signal.  The routine figures
228  * out what it should be set to.
229  */
230 void
231 setsignal(int signo)
232 {
233         int action;
234         sig_t sig, sigact = SIG_DFL;
235         char *t;
236
237         if ((t = trap[signo]) == NULL)
238                 action = S_DFL;
239         else if (*t != '\0')
240                 action = S_CATCH;
241         else
242                 action = S_IGN;
243         if (action == S_DFL) {
244                 switch (signo) {
245                 case SIGINT:
246                         action = S_CATCH;
247                         break;
248                 case SIGQUIT:
249 #ifdef DEBUG
250                         {
251                         extern int debug;
252
253                         if (debug)
254                                 break;
255                         }
256 #endif
257                         action = S_CATCH;
258                         break;
259                 case SIGTERM:
260                         if (rootshell && iflag)
261                                 action = S_IGN;
262                         break;
263 #if JOBS
264                 case SIGTSTP:
265                 case SIGTTOU:
266                         if (rootshell && mflag)
267                                 action = S_IGN;
268                         break;
269 #endif
270 #ifndef NO_HISTORY
271                 case SIGWINCH:
272                         if (rootshell && iflag)
273                                 action = S_CATCH;
274                         break;
275 #endif
276                 }
277         }
278
279         t = &sigmode[signo];
280         if (*t == 0) {
281                 /*
282                  * current setting unknown
283                  */
284                 if (!getsigaction(signo, &sigact)) {
285                         /*
286                          * Pretend it worked; maybe we should give a warning
287                          * here, but other shells don't. We don't alter
288                          * sigmode, so that we retry every time.
289                          */
290                         return;
291                 }
292                 if (sigact == SIG_IGN) {
293                         if (mflag && (signo == SIGTSTP ||
294                              signo == SIGTTIN || signo == SIGTTOU)) {
295                                 *t = S_IGN;     /* don't hard ignore these */
296                         } else
297                                 *t = S_HARD_IGN;
298                 } else {
299                         *t = S_RESET;   /* force to be set */
300                 }
301         }
302         if (*t == S_HARD_IGN || *t == action)
303                 return;
304         switch (action) {
305                 case S_DFL:     sigact = SIG_DFL;       break;
306                 case S_CATCH:   sigact = onsig;         break;
307                 case S_IGN:     sigact = SIG_IGN;       break;
308         }
309         *t = action;
310         sig = signal(signo, sigact);
311         if (sig != SIG_ERR && action == S_CATCH)
312                 siginterrupt(signo, 1);
313 }
314
315
316 /*
317  * Return the current setting for sig w/o changing it.
318  */
319 static int
320 getsigaction(int signo, sig_t *sigact)
321 {
322         struct sigaction sa;
323
324         if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
325                 return 0;
326         *sigact = (sig_t) sa.sa_handler;
327         return 1;
328 }
329
330
331 /*
332  * Ignore a signal.
333  */
334 void
335 ignoresig(int signo)
336 {
337
338         if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) {
339                 signal(signo, SIG_IGN);
340         }
341         sigmode[signo] = S_HARD_IGN;
342 }
343
344
345 #ifdef mkinit
346 INCLUDE <signal.h>
347 INCLUDE "trap.h"
348
349 SHELLPROC {
350         char *sm;
351
352         clear_traps();
353         for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
354                 if (*sm == S_IGN)
355                         *sm = S_HARD_IGN;
356         }
357 }
358 #endif
359
360
361 /*
362  * Signal handler.
363  */
364 void
365 onsig(int signo)
366 {
367
368         if (signo == SIGINT && trap[SIGINT] == NULL) {
369                 onint();
370                 return;
371         }
372
373         if (signo != SIGCHLD || !ignore_sigchld)
374                 gotsig[signo] = 1;
375         pendingsigs++;
376
377         /* If we are currently in a wait builtin, prepare to break it */
378         if ((signo == SIGINT || signo == SIGQUIT) && in_waitcmd != 0)
379                 breakwaitcmd = 1;
380         /* 
381          * If a trap is set, not ignored and not the null command, we need 
382          * to make sure traps are executed even when a child blocks signals.
383          */
384         if (Tflag &&
385             trap[signo] != NULL && 
386             ! trap[signo][0] == '\0' &&
387             ! (trap[signo][0] == ':' && trap[signo][1] == '\0'))
388                 breakwaitcmd = 1;
389
390 #ifndef NO_HISTORY
391         if (signo == SIGWINCH)
392                 gotwinch = 1;
393 #endif
394 }
395
396
397 /*
398  * Called to execute a trap.  Perhaps we should avoid entering new trap
399  * handlers while we are executing a trap handler.
400  */
401 void
402 dotrap(void)
403 {
404         int i;
405         int savestatus;
406
407         in_dotrap++;
408         for (;;) {
409                 for (i = 1; i < NSIG; i++) {
410                         if (gotsig[i]) {
411                                 gotsig[i] = 0;
412                                 if (trap[i]) {
413                                         /*
414                                          * Ignore SIGCHLD to avoid infinite recursion
415                                          * if the trap action does a fork.
416                                          */
417                                         if (i == SIGCHLD)
418                                                 ignore_sigchld++;
419                                         savestatus = exitstatus;
420                                         evalstring(trap[i]);
421                                         exitstatus = savestatus;
422                                         if (i == SIGCHLD)
423                                                 ignore_sigchld--;
424                                 }
425                                 break;
426                         }
427                 }
428                 if (i >= NSIG)
429                         break;
430         }
431         in_dotrap--;
432         pendingsigs = 0;
433 }
434
435
436 /*
437  * Controls whether the shell is interactive or not.
438  */
439 void
440 setinteractive(int on)
441 {
442         static int is_interactive = -1;
443
444         if (on == is_interactive)
445                 return;
446         setsignal(SIGINT);
447         setsignal(SIGQUIT);
448         setsignal(SIGTERM);
449 #ifndef NO_HISTORY
450         setsignal(SIGWINCH);
451 #endif
452         is_interactive = on;
453 }
454
455
456 /*
457  * Called to exit the shell.
458  */
459 void
460 exitshell(int status)
461 {
462         struct jmploc loc1, loc2;
463         char *p;
464
465         TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
466         if (setjmp(loc1.loc)) {
467                 goto l1;
468         }
469         if (setjmp(loc2.loc)) {
470                 goto l2;
471         }
472         handler = &loc1;
473         if ((p = trap[0]) != NULL && *p != '\0') {
474                 trap[0] = NULL;
475                 evalstring(p);
476         }
477 l1:   handler = &loc2;                  /* probably unnecessary */
478         flushall();
479 #if JOBS
480         setjobctl(0);
481 #endif
482 l2:   _exit(status);
483 }