2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char sccsid[] = "@(#)error.c 8.2 (Berkeley) 5/4/95";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD: src/bin/sh/error.c,v 1.15.2.4 2002/08/27 01:36:28 tjr Exp $");
46 * Errors and exceptions.
54 #include "nodes.h" /* show.h needs nodes.h */
64 * Code to handle exceptions in C.
67 struct jmploc *handler;
68 volatile sig_atomic_t exception;
69 volatile sig_atomic_t suppressint;
70 volatile sig_atomic_t intpending;
74 static void exverror(int, const char *, va_list) __printf0like(2, 0);
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".
88 longjmp(handler->loc, 1);
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. If SIGINTs are not suppressed and
97 * the shell is not a root shell, then we want to be terminated if we
98 * get here, as if we were terminated directly by a SIGINT. Arrange for
108 * The !in_dotrap here is safe. The only way we can arrive here
109 * with in_dotrap set is that a trap handler set SIGINT to SIG_DFL
113 if (suppressint && !in_dotrap) {
118 sigemptyset(&sigset);
119 sigprocmask(SIG_SETMASK, &sigset, NULL);
122 * This doesn't seem to be needed, since main() emits a newline.
125 if (tcgetpgrp(0) == getpid())
126 write(STDERR_FILENO, "\n", 1);
128 if (rootshell && iflag)
131 signal(SIGINT, SIG_DFL);
132 kill(getpid(), SIGINT);
138 * Exverror is called to raise the error exception. If the first argument
139 * is not NULL then error prints an error message using printf style
140 * formatting. It then raises the error exception.
143 exverror(int cond, const char *msg, va_list ap)
150 TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid()));
152 TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
156 outfmt(&errout, "%s: ", commandname);
157 doformat(&errout, msg, ap);
166 error(const char *msg, ...)
170 exverror(EXERROR, msg, ap);
176 exerror(int cond, const char *msg, ...)
180 exverror(cond, msg, ap);
187 * Table of error messages.
191 short errcode; /* error number */
192 short action; /* operation which encountered the error */
193 char *msg; /* text describing the error */
197 #define ALL (E_OPEN|E_CREAT|E_EXEC)
199 STATIC const struct errname errormsg[] = {
200 { EINTR, ALL, "interrupted" },
201 { EACCES, ALL, "permission denied" },
202 { EIO, ALL, "I/O error" },
203 { ENOENT, E_OPEN, "no such file" },
204 { ENOENT, E_CREAT,"directory nonexistent" },
205 { ENOENT, E_EXEC, "not found" },
206 { ENOTDIR, E_OPEN, "no such file" },
207 { ENOTDIR, E_CREAT,"directory nonexistent" },
208 { ENOTDIR, E_EXEC, "not found" },
209 { EISDIR, ALL, "is a directory" },
211 { EMFILE, ALL, "too many open files" },
213 { ENFILE, ALL, "file table overflow" },
214 { ENOSPC, ALL, "file system full" },
216 { EDQUOT, ALL, "disk quota exceeded" },
219 { ENOSR, ALL, "no streams resources" },
221 { ENXIO, ALL, "no such device or address" },
222 { EROFS, ALL, "read-only file system" },
223 { ETXTBSY, ALL, "text busy" },
224 { ENOMEM, ALL, "not enough memory" },
226 { ENOLINK, ALL, "remote access failed" },
229 { EMULTIHOP, ALL, "remote access failed" },
232 { ECOMM, ALL, "remote access failed" },
235 { ESTALE, ALL, "remote access failed" },
238 { ETIMEDOUT, ALL, "remote access failed" },
241 { ELOOP, ALL, "symbolic link loop" },
243 { E2BIG, E_EXEC, "argument list too long" },
245 { ELIBACC, E_EXEC, "shared library missing" },
247 { EEXIST, E_CREAT, "file exists" },
253 * Return a string describing an error. The returned string may be a
254 * pointer to a static buffer that will be overwritten on the next call.
255 * Action describes the operation that got the error.
259 errmsg(int e, int action)
261 struct errname const *ep;
264 for (ep = errormsg ; ep->errcode ; ep++) {
265 if (ep->errcode == e && (ep->action & action) != 0)
268 fmtstr(buf, sizeof buf, "error %d", e);