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 * @(#)error.c 8.2 (Berkeley) 5/4/95 37 * $FreeBSD: src/bin/sh/error.c,v 1.33 2010/12/21 20:47:06 jilles Exp $ 38 */ 39 40 /* 41 * Errors and exceptions. 42 */ 43 44 #include "shell.h" 45 #include "main.h" 46 #include "options.h" 47 #include "output.h" 48 #include "error.h" 49 #include "eval.h" /* defines commandname */ 50 #include "nodes.h" /* show.h needs nodes.h */ 51 #include "show.h" 52 #include "trap.h" 53 #include <signal.h> 54 #include <stdlib.h> 55 #include <unistd.h> 56 #include <errno.h> 57 58 59 /* 60 * Code to handle exceptions in C. 61 */ 62 63 struct jmploc *handler; 64 volatile sig_atomic_t exception; 65 volatile sig_atomic_t suppressint; 66 volatile sig_atomic_t intpending; 67 68 69 static void exverror(int, const char *, va_list) __printf0like(2, 0) __dead2; 70 71 /* 72 * Called to raise an exception. Since C doesn't include exceptions, we 73 * just do a longjmp to the exception handler. The type of exception is 74 * stored in the global variable "exception". 75 * 76 * Interrupts are disabled; they should be reenabled when the exception is 77 * caught. 78 */ 79 80 void 81 exraise(int e) 82 { 83 INTOFF; 84 if (handler == NULL) 85 abort(); 86 exception = e; 87 longjmp(handler->loc, 1); 88 } 89 90 91 /* 92 * Called from trap.c when a SIGINT is received. (If the user specifies 93 * that SIGINT is to be trapped or ignored using the trap builtin, then 94 * this routine is not called.) Suppressint is nonzero when interrupts 95 * are held using the INTOFF macro. If SIGINTs are not suppressed and 96 * the shell is not a root shell, then we want to be terminated if we 97 * get here, as if we were terminated directly by a SIGINT. Arrange for 98 * this here. 99 */ 100 101 void 102 onint(void) 103 { 104 sigset_t sigs; 105 106 /* 107 * The !in_dotrap here is safe. The only way we can arrive here 108 * with in_dotrap set is that a trap handler set SIGINT to SIG_DFL 109 * and killed itself. 110 */ 111 112 if (suppressint && !in_dotrap) { 113 intpending++; 114 return; 115 } 116 intpending = 0; 117 sigemptyset(&sigs); 118 sigprocmask(SIG_SETMASK, &sigs, NULL); 119 120 /* 121 * This doesn't seem to be needed, since main() emits a newline. 122 */ 123 #if 0 124 if (tcgetpgrp(0) == getpid()) 125 write(STDERR_FILENO, "\n", 1); 126 #endif 127 if (rootshell && iflag) 128 exraise(EXINT); 129 else { 130 signal(SIGINT, SIG_DFL); 131 kill(getpid(), SIGINT); 132 } 133 } 134 135 136 static void 137 vwarning(const char *msg, va_list ap) 138 { 139 if (commandname) 140 outfmt(out2, "%s: ", commandname); 141 doformat(out2, msg, ap); 142 out2fmt_flush("\n"); 143 } 144 145 146 void 147 warning(const char *msg, ...) 148 { 149 va_list ap; 150 va_start(ap, msg); 151 vwarning(msg, ap); 152 va_end(ap); 153 } 154 155 156 /* 157 * Exverror is called to raise the error exception. If the first argument 158 * is not NULL then error prints an error message using printf style 159 * formatting. It then raises the error exception. 160 */ 161 static void 162 exverror(int cond, const char *msg, va_list ap) 163 { 164 /* 165 * An interrupt trumps an error. Certain places catch error 166 * exceptions or transform them to a plain nonzero exit code 167 * in child processes, and if an error exception can be handled, 168 * an interrupt can be handled as well. 169 * 170 * exraise() will disable interrupts for the exception handler. 171 */ 172 FORCEINTON; 173 174 #ifdef DEBUG 175 if (msg) 176 TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid())); 177 else 178 TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid())); 179 #endif 180 if (msg) 181 vwarning(msg, ap); 182 flushall(); 183 exraise(cond); 184 } 185 186 187 void 188 error(const char *msg, ...) 189 { 190 va_list ap; 191 va_start(ap, msg); 192 exverror(EXERROR, msg, ap); 193 va_end(ap); 194 } 195 196 197 void 198 exerror(int cond, const char *msg, ...) 199 { 200 va_list ap; 201 va_start(ap, msg); 202 exverror(cond, msg, ap); 203 va_end(ap); 204 } 205