1
/* $NetBSD: error.c,v 1.31 2003/08/07 09:05:30 agc Exp $ */
4
* Copyright (c) 1991, 1993
5
* The Regents of the University of California. All rights reserved.
7
* This code is derived from software contributed to Berkeley by
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
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.
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
36
#include <sys/cdefs.h>
43
static char sccsid[] = "@(#)error.c 8.2 (Berkeley) 5/4/95";
45
__RCSID("$NetBSD: error.c,v 1.31 2003/08/07 09:05:30 agc Exp $");
50
* Errors and exceptions.
69
* Code to handle exceptions in C.
72
struct jmploc *handler;
74
volatile int suppressint;
75
volatile int intpending;
79
static void exverror(int, const char *, va_list)
80
__attribute__((__noreturn__));
83
* Called to raise an exception. Since C doesn't include exceptions, we
84
* just do a longjmp to the exception handler. The type of exception is
85
* stored in the global variable "exception".
94
longjmp(handler->loc, 1);
99
* Called from trap.c when a SIGINT is received. (If the user specifies
100
* that SIGINT is to be trapped or ignored using the trap builtin, then
101
* this routine is not called.) Suppressint is nonzero when interrupts
102
* are held using the INTOFF macro. The call to _exit is necessary because
103
* there is a short period after a fork before the signal handlers are
104
* set to the appropriate value for the child. (The test for iflag is
105
* just defensive programming.)
118
sigemptyset(&nsigset);
119
sigprocmask(SIG_SETMASK, &nsigset, NULL);
120
if (rootshell && iflag)
123
bsd_signal(SIGINT, SIG_DFL);
130
exvwarning(int sv_errno, const char *msg, va_list ap)
132
/* Partially emulate line buffered output so that:
133
* printf '%d\n' 1 a 2
135
* printf '%d %d %d\n' 1 a 2
136
* both generate sensible text when stdout and stderr are merged.
138
if (output.nextc != output.buf && output.nextc[-1] == '\n')
141
outfmt(&errout, "%s: ", commandname);
143
doformat(&errout, msg, ap);
145
outfmt(&errout, ": ");
148
outfmt(&errout, "%s", strerror(sv_errno));
154
* Exverror is called to raise the error exception. If the second argument
155
* is not NULL then error prints an error message using printf style
156
* formatting. It then raises the error exception.
159
exverror(int cond, const char *msg, va_list ap)
168
TRACE(("exverror(%d, \"", cond));
169
TRACEV((msg, ap_tmp));
170
TRACE(("\") pid=%d\n", getpid()));
173
TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
176
exvwarning(-1, msg, ap);
185
error(const char *msg, ...)
190
exverror(EXERROR, msg, ap);
197
exerror(int cond, const char *msg, ...)
202
exverror(cond, msg, ap);
208
* error/warning routines for external builtins
214
exerrno = rval & 255;
219
sh_err(int status, const char *fmt, ...)
224
exvwarning(errno, fmt, ap);
230
sh_verr(int status, const char *fmt, va_list ap)
232
exvwarning(errno, fmt, ap);
237
sh_errx(int status, const char *fmt, ...)
242
exvwarning(-1, fmt, ap);
248
sh_verrx(int status, const char *fmt, va_list ap)
250
exvwarning(-1, fmt, ap);
255
sh_warn(const char *fmt, ...)
260
exvwarning(errno, fmt, ap);
265
sh_vwarn(const char *fmt, va_list ap)
267
exvwarning(errno, fmt, ap);
271
sh_warnx(const char *fmt, ...)
276
exvwarning(-1, fmt, ap);
281
sh_vwarnx(const char *fmt, va_list ap)
283
exvwarning(-1, fmt, ap);
288
* Table of error messages.
292
short errcode; /* error number */
293
short action; /* operation which encountered the error */
294
const char *msg; /* text describing the error */
298
#define ALL (E_OPEN|E_CREAT|E_EXEC)
300
STATIC const struct errname errormsg[] = {
301
{ EINTR, ALL, "interrupted" },
302
{ EACCES, ALL, "permission denied" },
303
{ EIO, ALL, "I/O error" },
304
{ EEXIST, ALL, "file exists" },
305
{ ENOENT, E_OPEN, "no such file" },
306
{ ENOENT, E_CREAT,"directory nonexistent" },
307
{ ENOENT, E_EXEC, "not found" },
308
{ ENOTDIR, E_OPEN, "no such file" },
309
{ ENOTDIR, E_CREAT,"directory nonexistent" },
310
{ ENOTDIR, E_EXEC, "not found" },
311
{ EISDIR, ALL, "is a directory" },
313
{ EMFILE, ALL, "too many open files" },
315
{ ENFILE, ALL, "file table overflow" },
316
{ ENOSPC, ALL, "file system full" },
318
{ EDQUOT, ALL, "disk quota exceeded" },
321
{ ENOSR, ALL, "no streams resources" },
323
{ ENXIO, ALL, "no such device or address" },
324
{ EROFS, ALL, "read-only file system" },
325
{ ETXTBSY, ALL, "text busy" },
327
{ EAGAIN, E_EXEC, "not enough memory" },
329
{ ENOMEM, ALL, "not enough memory" },
331
{ ENOLINK, ALL, "remote access failed" },
334
{ EMULTIHOP, ALL, "remote access failed" },
337
{ ECOMM, ALL, "remote access failed" },
340
{ ESTALE, ALL, "remote access failed" },
343
{ ETIMEDOUT, ALL, "remote access failed" },
346
{ ELOOP, ALL, "symbolic link loop" },
348
{ E2BIG, E_EXEC, "argument list too long" },
350
{ ELIBACC, E_EXEC, "shared library missing" },
357
* Return a string describing an error. The returned string may be a
358
* pointer to a static buffer that will be overwritten on the next call.
359
* Action describes the operation that got the error.
363
errmsg(int e, int action)
365
struct errname const *ep;
368
for (ep = errormsg ; ep->errcode ; ep++) {
369
if (ep->errcode == e && (ep->action & action) != 0)
372
fmtstr(buf, sizeof buf, "error %d", e);