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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)error.c 8.2 (Berkeley) 5/4/95 33 * $FreeBSD: head/bin/sh/error.c 216622 2010-12-21 20:47:06Z jilles $ 34 */ 35 36 /* 37 * Errors and exceptions. 38 */ 39 40 #include "shell.h" 41 #include "main.h" 42 #include "options.h" 43 #include "output.h" 44 #include "error.h" 45 #include "eval.h" /* defines commandname */ 46 #include "nodes.h" /* show.h needs nodes.h */ 47 #include "show.h" 48 #include "trap.h" 49 #include <signal.h> 50 #include <stdlib.h> 51 #include <unistd.h> 52 #include <errno.h> 53 54 55 /* 56 * Code to handle exceptions in C. 57 */ 58 59 struct jmploc *handler; 60 volatile sig_atomic_t exception; 61 volatile sig_atomic_t suppressint; 62 volatile sig_atomic_t intpending; 63 64 65 static void exverror(int, const char *, va_list) __printf0like(2, 0) __dead2; 66 67 /* 68 * Called to raise an exception. Since C doesn't include exceptions, we 69 * just do a longjmp to the exception handler. The type of exception is 70 * stored in the global variable "exception". 71 * 72 * Interrupts are disabled; they should be reenabled when the exception is 73 * caught. 74 */ 75 76 void 77 exraise(int e) 78 { 79 INTOFF; 80 if (handler == NULL) 81 abort(); 82 exception = e; 83 longjmp(handler->loc, 1); 84 } 85 86 87 /* 88 * Called from trap.c when a SIGINT is received. (If the user specifies 89 * that SIGINT is to be trapped or ignored using the trap builtin, then 90 * this routine is not called.) Suppressint is nonzero when interrupts 91 * are held using the INTOFF macro. If SIGINTs are not suppressed and 92 * the shell is not a root shell, then we want to be terminated if we 93 * get here, as if we were terminated directly by a SIGINT. Arrange for 94 * this here. 95 */ 96 97 void 98 onint(void) 99 { 100 sigset_t sigs; 101 102 /* 103 * The !in_dotrap here is safe. The only way we can arrive here 104 * with in_dotrap set is that a trap handler set SIGINT to SIG_DFL 105 * and killed itself. 106 */ 107 108 if (suppressint && !in_dotrap) { 109 intpending++; 110 return; 111 } 112 intpending = 0; 113 sigemptyset(&sigs); 114 sigprocmask(SIG_SETMASK, &sigs, NULL); 115 116 /* 117 * This doesn't seem to be needed, since main() emits a newline. 118 */ 119 #if 0 120 if (tcgetpgrp(0) == getpid()) 121 write(STDERR_FILENO, "\n", 1); 122 #endif 123 if (rootshell && iflag) 124 exraise(EXINT); 125 else { 126 signal(SIGINT, SIG_DFL); 127 kill(getpid(), SIGINT); 128 } 129 } 130 131 132 static void 133 vwarning(const char *msg, va_list ap) 134 { 135 if (commandname) 136 outfmt(out2, "%s: ", commandname); 137 doformat(out2, msg, ap); 138 out2fmt_flush("\n"); 139 } 140 141 142 void 143 warning(const char *msg, ...) 144 { 145 va_list ap; 146 va_start(ap, msg); 147 vwarning(msg, ap); 148 va_end(ap); 149 } 150 151 152 /* 153 * Exverror is called to raise the error exception. If the first argument 154 * is not NULL then error prints an error message using printf style 155 * formatting. It then raises the error exception. 156 */ 157 static void 158 exverror(int cond, const char *msg, va_list ap) 159 { 160 /* 161 * An interrupt trumps an error. Certain places catch error 162 * exceptions or transform them to a plain nonzero exit code 163 * in child processes, and if an error exception can be handled, 164 * an interrupt can be handled as well. 165 * 166 * exraise() will disable interrupts for the exception handler. 167 */ 168 FORCEINTON; 169 170 #ifdef DEBUG 171 if (msg) 172 TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid())); 173 else 174 TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid())); 175 #endif 176 if (msg) 177 vwarning(msg, ap); 178 flushall(); 179 exraise(cond); 180 } 181 182 183 void 184 error(const char *msg, ...) 185 { 186 va_list ap; 187 va_start(ap, msg); 188 exverror(EXERROR, msg, ap); 189 va_end(ap); 190 } 191 192 193 void 194 exerror(int cond, const char *msg, ...) 195 { 196 va_list ap; 197 va_start(ap, msg); 198 exverror(cond, msg, ap); 199 va_end(ap); 200 } 201