error.c revision 211281
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3436150Scharnier#if 0 3536150Scharnierstatic char sccsid[] = "@(#)error.c 8.2 (Berkeley) 5/4/95"; 3636150Scharnier#endif 371556Srgrimes#endif /* not lint */ 3899110Sobrien#include <sys/cdefs.h> 3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/error.c 211281 2010-08-13 13:36:18Z jilles $"); 401556Srgrimes 411556Srgrimes/* 421556Srgrimes * Errors and exceptions. 431556Srgrimes */ 441556Srgrimes 451556Srgrimes#include "shell.h" 461556Srgrimes#include "main.h" 471556Srgrimes#include "options.h" 481556Srgrimes#include "output.h" 491556Srgrimes#include "error.h" 5053891Scracauer#include "nodes.h" /* show.h needs nodes.h */ 5117987Speter#include "show.h" 5238521Scracauer#include "trap.h" 531556Srgrimes#include <signal.h> 5478732Sdd#include <stdlib.h> 5517987Speter#include <unistd.h> 561556Srgrimes#include <errno.h> 571556Srgrimes 581556Srgrimes 591556Srgrimes/* 601556Srgrimes * Code to handle exceptions in C. 611556Srgrimes */ 621556Srgrimes 631556Srgrimesstruct jmploc *handler; 6438521Scracauervolatile sig_atomic_t exception; 6538530Scracauervolatile sig_atomic_t suppressint; 6638521Scracauervolatile sig_atomic_t intpending; 671556Srgrimeschar *commandname; 681556Srgrimes 691556Srgrimes 70200967Sjillesstatic void exverror(int, const char *, va_list) __printf0like(2, 0) __dead2; 7120425Ssteve 721556Srgrimes/* 731556Srgrimes * Called to raise an exception. Since C doesn't include exceptions, we 741556Srgrimes * just do a longjmp to the exception handler. The type of exception is 751556Srgrimes * stored in the global variable "exception". 76199660Sjilles * 77199660Sjilles * Interrupts are disabled; they should be reenabled when the exception is 78199660Sjilles * caught. 791556Srgrimes */ 801556Srgrimes 811556Srgrimesvoid 8290111Simpexraise(int e) 8317987Speter{ 84199660Sjilles INTOFF; 851556Srgrimes if (handler == NULL) 861556Srgrimes abort(); 871556Srgrimes exception = e; 881556Srgrimes longjmp(handler->loc, 1); 891556Srgrimes} 901556Srgrimes 911556Srgrimes 921556Srgrimes/* 931556Srgrimes * Called from trap.c when a SIGINT is received. (If the user specifies 941556Srgrimes * that SIGINT is to be trapped or ignored using the trap builtin, then 9538536Scracauer * this routine is not called.) Suppressint is nonzero when interrupts 9638521Scracauer * are held using the INTOFF macro. If SIGINTs are not suppressed and 9738521Scracauer * the shell is not a root shell, then we want to be terminated if we 9838521Scracauer * get here, as if we were terminated directly by a SIGINT. Arrange for 9938521Scracauer * this here. 1001556Srgrimes */ 1011556Srgrimes 1021556Srgrimesvoid 10390111Simponint(void) 10490111Simp{ 105211281Sjilles sigset_t sigs; 10617987Speter 10738536Scracauer /* 10838536Scracauer * The !in_dotrap here is safe. The only way we can arrive here 10938536Scracauer * with in_dotrap set is that a trap handler set SIGINT to SIG_DFL 11038521Scracauer * and killed itself. 11138521Scracauer */ 11238521Scracauer 11338521Scracauer if (suppressint && !in_dotrap) { 1141556Srgrimes intpending++; 1151556Srgrimes return; 1161556Srgrimes } 1171556Srgrimes intpending = 0; 118211281Sjilles sigemptyset(&sigs); 119211281Sjilles sigprocmask(SIG_SETMASK, &sigs, NULL); 12038521Scracauer 12138536Scracauer /* 12238536Scracauer * This doesn't seem to be needed, since main() emits a newline. 12338536Scracauer */ 12438521Scracauer#if 0 125155301Sschweikh if (tcgetpgrp(0) == getpid()) 12638521Scracauer write(STDERR_FILENO, "\n", 1); 12738521Scracauer#endif 1281556Srgrimes if (rootshell && iflag) 1291556Srgrimes exraise(EXINT); 13038521Scracauer else { 13138521Scracauer signal(SIGINT, SIG_DFL); 13238521Scracauer kill(getpid(), SIGINT); 13338521Scracauer } 1341556Srgrimes} 1351556Srgrimes 1361556Srgrimes 1371556Srgrimes/* 13820425Ssteve * Exverror is called to raise the error exception. If the first argument 1391556Srgrimes * is not NULL then error prints an error message using printf style 1401556Srgrimes * formatting. It then raises the error exception. 1411556Srgrimes */ 14220425Sstevestatic void 14390111Simpexverror(int cond, const char *msg, va_list ap) 14420425Ssteve{ 145199660Sjilles /* 146199660Sjilles * An interrupt trumps an error. Certain places catch error 147199660Sjilles * exceptions or transform them to a plain nonzero exit code 148199660Sjilles * in child processes, and if an error exception can be handled, 149199660Sjilles * an interrupt can be handled as well. 150199660Sjilles * 151199660Sjilles * exraise() will disable interrupts for the exception handler. 152199660Sjilles */ 153199660Sjilles FORCEINTON; 1541556Srgrimes 15520425Ssteve#ifdef DEBUG 15620425Ssteve if (msg) 15720425Ssteve TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid())); 15820425Ssteve else 15920425Ssteve TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid())); 16020425Ssteve#endif 16120425Ssteve if (msg) { 16220425Ssteve if (commandname) 163201366Sjilles outfmt(out2, "%s: ", commandname); 164201366Sjilles doformat(out2, msg, ap); 16520425Ssteve out2c('\n'); 16620425Ssteve } 16720425Ssteve flushall(); 16820425Ssteve exraise(cond); 16920425Ssteve} 17020425Ssteve 17120425Ssteve 1721556Srgrimesvoid 17375577Skriserror(const char *msg, ...) 17417987Speter{ 1751556Srgrimes va_list ap; 17620425Ssteve va_start(ap, msg); 17720425Ssteve exverror(EXERROR, msg, ap); 17820425Ssteve va_end(ap); 17920425Ssteve} 18017987Speter 18120425Ssteve 18220425Sstevevoid 18375577Skrisexerror(int cond, const char *msg, ...) 18420425Ssteve{ 18520425Ssteve va_list ap; 1861556Srgrimes va_start(ap, msg); 18720425Ssteve exverror(cond, msg, ap); 1881556Srgrimes va_end(ap); 1891556Srgrimes} 190