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