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 33 #ifndef lint 34 #endif /* not lint */ 35 #include <sys/cdefs.h> 36 /* 37 * Errors and exceptions. 38 */ 39 40 #include "shell.h" 41 #include "eval.h" 42 #include "main.h" 43 #include "options.h" 44 #include "output.h" 45 #include "error.h" 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 verrorwithstatus(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 re-enabled 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 and not suppressed, or when 89 * an interrupt is pending and interrupts are re-enabled using INTON. 90 * (If the user specifies that SIGINT is to be trapped or ignored using the 91 * trap builtin, then this routine is not called.) Suppressint is nonzero 92 * when interrupts are held using the INTOFF macro. If SIGINTs are not 93 * suppressed and the shell is not a root shell, then we want to be 94 * terminated if we get here, as if we were terminated directly by a SIGINT. 95 * Arrange for this here. 96 */ 97 98 void 99 onint(void) 100 { 101 sigset_t sigs; 102 103 intpending = 0; 104 sigemptyset(&sigs); 105 sigprocmask(SIG_SETMASK, &sigs, NULL); 106 107 /* 108 * This doesn't seem to be needed, since main() emits a newline. 109 */ 110 #if 0 111 if (tcgetpgrp(0) == getpid()) 112 write(STDERR_FILENO, "\n", 1); 113 #endif 114 if (rootshell && iflag) 115 exraise(EXINT); 116 else { 117 signal(SIGINT, SIG_DFL); 118 kill(getpid(), SIGINT); 119 _exit(128 + SIGINT); 120 } 121 } 122 123 124 static void 125 vwarning(const char *msg, va_list ap) 126 { 127 if (commandname) 128 outfmt(out2, "%s: ", commandname); 129 else if (arg0) 130 outfmt(out2, "%s: ", arg0); 131 doformat(out2, msg, ap); 132 out2fmt_flush("\n"); 133 } 134 135 136 void 137 warning(const char *msg, ...) 138 { 139 va_list ap; 140 va_start(ap, msg); 141 vwarning(msg, ap); 142 va_end(ap); 143 } 144 145 146 /* 147 * Exverror is called to raise the error exception. If the first argument 148 * is not NULL then error prints an error message using printf style 149 * formatting. It then raises the error exception. 150 */ 151 static void 152 verrorwithstatus(int status, const char *msg, va_list ap) 153 { 154 /* 155 * An interrupt trumps an error. Certain places catch error 156 * exceptions or transform them to a plain nonzero exit code 157 * in child processes, and if an error exception can be handled, 158 * an interrupt can be handled as well. 159 * 160 * exraise() will disable interrupts for the exception handler. 161 */ 162 FORCEINTON; 163 164 #ifdef DEBUG 165 if (msg) 166 TRACE(("verrorwithstatus(%d, \"%s\") pid=%d\n", 167 status, msg, getpid())); 168 else 169 TRACE(("verrorwithstatus(%d, NULL) pid=%d\n", 170 status, getpid())); 171 #endif 172 if (msg) 173 vwarning(msg, ap); 174 flushall(); 175 exitstatus = status; 176 exraise(EXERROR); 177 } 178 179 180 void 181 error(const char *msg, ...) 182 { 183 va_list ap; 184 va_start(ap, msg); 185 verrorwithstatus(2, msg, ap); 186 va_end(ap); 187 } 188 189 190 void 191 errorwithstatus(int status, const char *msg, ...) 192 { 193 va_list ap; 194 va_start(ap, msg); 195 verrorwithstatus(status, msg, ap); 196 va_end(ap); 197 } 198