Home | History | Annotate | Line # | Download | only in sh
error.c revision 1.40
      1 /*	$NetBSD: error.c,v 1.40 2017/07/05 20:00:27 kre Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Kenneth Almquist.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)error.c	8.2 (Berkeley) 5/4/95";
     39 #else
     40 __RCSID("$NetBSD: error.c,v 1.40 2017/07/05 20:00:27 kre Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 /*
     45  * Errors and exceptions.
     46  */
     47 
     48 #include <signal.h>
     49 #include <stdlib.h>
     50 #include <unistd.h>
     51 #include <errno.h>
     52 #include <stdio.h>
     53 #include <string.h>
     54 
     55 #include "shell.h"
     56 #include "eval.h" /* for commandname */
     57 #include "main.h"
     58 #include "options.h"
     59 #include "output.h"
     60 #include "error.h"
     61 #include "show.h"
     62 
     63 
     64 /*
     65  * Code to handle exceptions in C.
     66  */
     67 
     68 struct jmploc *handler;
     69 int exception;
     70 volatile int suppressint;
     71 volatile int intpending;
     72 
     73 
     74 static void exverror(int, const char *, va_list) __dead;
     75 
     76 /*
     77  * Called to raise an exception.  Since C doesn't include exceptions, we
     78  * just do a longjmp to the exception handler.  The type of exception is
     79  * stored in the global variable "exception".
     80  */
     81 
     82 void
     83 exraise(int e)
     84 {
     85 	if (handler == NULL)
     86 		abort();
     87 	exception = e;
     88 	longjmp(handler->loc, 1);
     89 }
     90 
     91 
     92 /*
     93  * Called from trap.c when a SIGINT is received.  (If the user specifies
     94  * that SIGINT is to be trapped or ignored using the trap builtin, then
     95  * this routine is not called.)  Suppressint is nonzero when interrupts
     96  * are held using the INTOFF macro.  The call to _exit is necessary because
     97  * there is a short period after a fork before the signal handlers are
     98  * set to the appropriate value for the child.  (The test for iflag is
     99  * just defensive programming.)
    100  */
    101 
    102 void
    103 onint(void)
    104 {
    105 	sigset_t nsigset;
    106 
    107 	if (suppressint) {
    108 		intpending = 1;
    109 		return;
    110 	}
    111 	intpending = 0;
    112 	sigemptyset(&nsigset);
    113 	sigprocmask(SIG_SETMASK, &nsigset, NULL);
    114 	if (rootshell && iflag)
    115 		exraise(EXINT);
    116 	else {
    117 		signal(SIGINT, SIG_DFL);
    118 		raise(SIGINT);
    119 	}
    120 	/* NOTREACHED */
    121 }
    122 
    123 static __printflike(2, 0) void
    124 exvwarning(int sv_errno, const char *msg, va_list ap)
    125 {
    126 	/* Partially emulate line buffered output so that:
    127 	 *	printf '%d\n' 1 a 2
    128 	 * and
    129 	 *	printf '%d %d %d\n' 1 a 2
    130 	 * both generate sensible text when stdout and stderr are merged.
    131 	 */
    132 	if (output.nextc != output.buf && output.nextc[-1] == '\n')
    133 		flushout(&output);
    134 	if (commandname)
    135 		outfmt(&errout, "%s: ", commandname);
    136 	else
    137 		outfmt(&errout, "%s: ", getprogname());
    138 	if (msg != NULL) {
    139 		doformat(&errout, msg, ap);
    140 		if (sv_errno >= 0)
    141 			outfmt(&errout, ": ");
    142 	}
    143 	if (sv_errno >= 0)
    144 		outfmt(&errout, "%s", strerror(sv_errno));
    145 	out2c('\n');
    146 	flushout(&errout);
    147 }
    148 
    149 /*
    150  * Exverror is called to raise the error exception.  If the second argument
    151  * is not NULL then error prints an error message using printf style
    152  * formatting.  It then raises the error exception.
    153  */
    154 static __printflike(2, 0) void
    155 exverror(int cond, const char *msg, va_list ap)
    156 {
    157 	CLEAR_PENDING_INT;
    158 	INTOFF;
    159 
    160 #ifdef DEBUG
    161 	if (msg) {
    162 		CTRACE(DBG_ERRS, ("exverror(%d, \"", cond));
    163 		CTRACEV(DBG_ERRS, (msg, ap));
    164 		CTRACE(DBG_ERRS, ("\") pid=%d\n", getpid()));
    165 	} else
    166 		CTRACE(DBG_ERRS, ("exverror(%d, NULL) pid=%d\n", cond,
    167 		    getpid()));
    168 #endif
    169 	if (msg)
    170 		exvwarning(-1, msg, ap);
    171 
    172 	flushall();
    173 	exraise(cond);
    174 	/* NOTREACHED */
    175 }
    176 
    177 
    178 void
    179 error(const char *msg, ...)
    180 {
    181 	va_list ap;
    182 
    183 	/*
    184 	 * On error, we certainly never want exit(0)...
    185 	 */
    186 	if (exerrno == 0)
    187 		exerrno = 1;
    188 	va_start(ap, msg);
    189 	exverror(EXERROR, msg, ap);
    190 	/* NOTREACHED */
    191 	va_end(ap);
    192 }
    193 
    194 
    195 void
    196 exerror(int cond, const char *msg, ...)
    197 {
    198 	va_list ap;
    199 
    200 	va_start(ap, msg);
    201 	exverror(cond, msg, ap);
    202 	/* NOTREACHED */
    203 	va_end(ap);
    204 }
    205 
    206 /*
    207  * error/warning routines for external builtins
    208  */
    209 
    210 void
    211 sh_exit(int rval)
    212 {
    213 	exerrno = rval & 255;
    214 	exraise(EXEXEC);
    215 }
    216 
    217 void
    218 sh_err(int status, const char *fmt, ...)
    219 {
    220 	va_list ap;
    221 
    222 	va_start(ap, fmt);
    223 	exvwarning(errno, fmt, ap);
    224 	va_end(ap);
    225 	sh_exit(status);
    226 }
    227 
    228 void
    229 sh_verr(int status, const char *fmt, va_list ap)
    230 {
    231 	exvwarning(errno, fmt, ap);
    232 	sh_exit(status);
    233 }
    234 
    235 void
    236 sh_errx(int status, const char *fmt, ...)
    237 {
    238 	va_list ap;
    239 
    240 	va_start(ap, fmt);
    241 	exvwarning(-1, fmt, ap);
    242 	va_end(ap);
    243 	sh_exit(status);
    244 }
    245 
    246 void
    247 sh_verrx(int status, const char *fmt, va_list ap)
    248 {
    249 	exvwarning(-1, fmt, ap);
    250 	sh_exit(status);
    251 }
    252 
    253 void
    254 sh_warn(const char *fmt, ...)
    255 {
    256 	va_list ap;
    257 
    258 	va_start(ap, fmt);
    259 	exvwarning(errno, fmt, ap);
    260 	va_end(ap);
    261 }
    262 
    263 void
    264 sh_vwarn(const char *fmt, va_list ap)
    265 {
    266 	exvwarning(errno, fmt, ap);
    267 }
    268 
    269 void
    270 sh_warnx(const char *fmt, ...)
    271 {
    272 	va_list ap;
    273 
    274 	va_start(ap, fmt);
    275 	exvwarning(-1, fmt, ap);
    276 	va_end(ap);
    277 }
    278 
    279 void
    280 sh_vwarnx(const char *fmt, va_list ap)
    281 {
    282 	exvwarning(-1, fmt, ap);
    283 }
    284 
    285 
    286 /*
    287  * Table of error messages.
    288  */
    289 
    290 struct errname {
    291 	short errcode;		/* error number */
    292 	short action;		/* operation which encountered the error */
    293 	const char *msg;	/* text describing the error */
    294 };
    295 
    296 
    297 #define ALL (E_OPEN|E_CREAT|E_EXEC)
    298 
    299 STATIC const struct errname errormsg[] = {
    300 	{ EINTR,	ALL,	"interrupted" },
    301 	{ EACCES,	ALL,	"permission denied" },
    302 	{ EIO,		ALL,	"I/O error" },
    303 	{ EEXIST,	ALL,	"file exists" },
    304 	{ ENOENT,	E_OPEN,	"no such file" },
    305 	{ ENOENT,	E_CREAT,"directory nonexistent" },
    306 	{ ENOENT,	E_EXEC,	"not found" },
    307 	{ ENOTDIR,	E_OPEN,	"no such file" },
    308 	{ ENOTDIR,	E_CREAT,"directory nonexistent" },
    309 	{ ENOTDIR,	E_EXEC,	"not found" },
    310 	{ EISDIR,	ALL,	"is a directory" },
    311 #ifdef EMFILE
    312 	{ EMFILE,	ALL,	"too many open files" },
    313 #endif
    314 	{ ENFILE,	ALL,	"file table overflow" },
    315 	{ ENOSPC,	ALL,	"file system full" },
    316 #ifdef EDQUOT
    317 	{ EDQUOT,	ALL,	"disk quota exceeded" },
    318 #endif
    319 #ifdef ENOSR
    320 	{ ENOSR,	ALL,	"no streams resources" },
    321 #endif
    322 	{ ENXIO,	ALL,	"no such device or address" },
    323 	{ EROFS,	ALL,	"read-only file system" },
    324 	{ ETXTBSY,	ALL,	"text busy" },
    325 #ifdef EAGAIN
    326 	{ EAGAIN,	E_EXEC,	"not enough memory" },
    327 #endif
    328 	{ ENOMEM,	ALL,	"not enough memory" },
    329 #ifdef ENOLINK
    330 	{ ENOLINK,	ALL,	"remote access failed" },
    331 #endif
    332 #ifdef EMULTIHOP
    333 	{ EMULTIHOP,	ALL,	"remote access failed" },
    334 #endif
    335 #ifdef ECOMM
    336 	{ ECOMM,	ALL,	"remote access failed" },
    337 #endif
    338 #ifdef ESTALE
    339 	{ ESTALE,	ALL,	"remote access failed" },
    340 #endif
    341 #ifdef ETIMEDOUT
    342 	{ ETIMEDOUT,	ALL,	"remote access failed" },
    343 #endif
    344 #ifdef ELOOP
    345 	{ ELOOP,	ALL,	"symbolic link loop" },
    346 #endif
    347 #ifdef ENAMETOOLONG
    348 	{ ENAMETOOLONG,	ALL,	"file name too long" },
    349 #endif
    350 	{ E2BIG,	E_EXEC,	"argument list too long" },
    351 #ifdef ELIBACC
    352 	{ ELIBACC,	E_EXEC,	"shared library missing" },
    353 #endif
    354 	{ 0,		0,	NULL },
    355 };
    356 
    357 
    358 /*
    359  * Return a string describing an error.  The returned string may be a
    360  * pointer to a static buffer that will be overwritten on the next call.
    361  * Action describes the operation that got the error.
    362  */
    363 
    364 const char *
    365 errmsg(int e, int action)
    366 {
    367 	struct errname const *ep;
    368 	static char buf[12];
    369 
    370 	for (ep = errormsg ; ep->errcode ; ep++) {
    371 		if (ep->errcode == e && (ep->action & action) != 0)
    372 			return ep->msg;
    373 	}
    374 	fmtstr(buf, sizeof buf, "error %d", e);
    375 	return buf;
    376 }
    377