error.c revision 100351
1104476Ssam/*-
2104476Ssam * Copyright (c) 1991, 1993
3139825Simp *	The Regents of the University of California.  All rights reserved.
4104476Ssam *
5104476Ssam * This code is derived from software contributed to Berkeley by
6104476Ssam * Kenneth Almquist.
7104476Ssam *
8104476Ssam * Redistribution and use in source and binary forms, with or without
9104476Ssam * modification, are permitted provided that the following conditions
10104476Ssam * are met:
11104476Ssam * 1. Redistributions of source code must retain the above copyright
12104476Ssam *    notice, this list of conditions and the following disclaimer.
13104476Ssam * 2. Redistributions in binary form must reproduce the above copyright
14104476Ssam *    notice, this list of conditions and the following disclaimer in the
15104476Ssam *    documentation and/or other materials provided with the distribution.
16104476Ssam * 3. All advertising materials mentioning features or use of this software
17104476Ssam *    must display the following acknowledgement:
18104476Ssam *	This product includes software developed by the University of
19104476Ssam *	California, Berkeley and its contributors.
20104476Ssam * 4. Neither the name of the University nor the names of its contributors
21104476Ssam *    may be used to endorse or promote products derived from this software
22104476Ssam *    without specific prior written permission.
23104476Ssam *
24104476Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25104476Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26104476Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27104476Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28104476Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29104476Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30104476Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31104476Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32104476Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33104476Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34104476Ssam * SUCH DAMAGE.
35104476Ssam */
36104476Ssam
37104476Ssam#ifndef lint
38104476Ssam#if 0
39104476Ssamstatic char sccsid[] = "@(#)error.c	8.2 (Berkeley) 5/4/95";
40104476Ssam#endif
41104476Ssam#endif /* not lint */
42#include <sys/cdefs.h>
43__FBSDID("$FreeBSD: head/bin/sh/error.c 100351 2002-07-19 08:09:04Z tjr $");
44
45/*
46 * Errors and exceptions.
47 */
48
49#include "shell.h"
50#include "main.h"
51#include "options.h"
52#include "output.h"
53#include "error.h"
54#include "nodes.h" /* show.h needs nodes.h */
55#include "show.h"
56#include "trap.h"
57#include <signal.h>
58#include <stdlib.h>
59#include <unistd.h>
60#include <errno.h>
61
62
63/*
64 * Code to handle exceptions in C.
65 */
66
67struct jmploc *handler;
68volatile sig_atomic_t exception;
69volatile sig_atomic_t suppressint;
70volatile sig_atomic_t intpending;
71char *commandname;
72
73
74static void exverror(int, const char *, va_list) __printf0like(2, 0);
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
82void
83exraise(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.  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
99 * this here.
100 */
101
102void
103onint(void)
104{
105	sigset_t sigset;
106
107	/*
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
110	 * and killed itself.
111	 */
112
113	if (suppressint && !in_dotrap) {
114		intpending++;
115		return;
116	}
117	intpending = 0;
118	sigemptyset(&sigset);
119	sigprocmask(SIG_SETMASK, &sigset, NULL);
120
121	/*
122	 * This doesn't seem to be needed, since main() emits a newline.
123	 */
124#if 0
125	if (tcgetpgrp(0) == getpid())
126		write(STDERR_FILENO, "\n", 1);
127#endif
128	if (rootshell && iflag)
129		exraise(EXINT);
130	else {
131		signal(SIGINT, SIG_DFL);
132		kill(getpid(), SIGINT);
133	}
134}
135
136
137/*
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.
141 */
142static void
143exverror(int cond, const char *msg, va_list ap)
144{
145	CLEAR_PENDING_INT;
146	INTOFF;
147
148#ifdef DEBUG
149	if (msg)
150		TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid()));
151	else
152		TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
153#endif
154	if (msg) {
155		if (commandname)
156			outfmt(&errout, "%s: ", commandname);
157		doformat(&errout, msg, ap);
158		out2c('\n');
159	}
160	flushall();
161	exraise(cond);
162}
163
164
165void
166error(const char *msg, ...)
167{
168	va_list ap;
169	va_start(ap, msg);
170	exverror(EXERROR, msg, ap);
171	va_end(ap);
172}
173
174
175void
176exerror(int cond, const char *msg, ...)
177{
178	va_list ap;
179	va_start(ap, msg);
180	exverror(cond, msg, ap);
181	va_end(ap);
182}
183
184
185
186/*
187 * Table of error messages.
188 */
189
190struct errname {
191	short errcode;		/* error number */
192	short action;		/* operation which encountered the error */
193	char *msg;		/* text describing the error */
194};
195
196
197#define ALL (E_OPEN|E_CREAT|E_EXEC)
198
199STATIC 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" },
210#ifdef notdef
211	{ EMFILE,	ALL,	"too many open files" },
212#endif
213	{ ENFILE,	ALL,	"file table overflow" },
214	{ ENOSPC,	ALL,	"filesystem full" },
215#ifdef EDQUOT
216	{ EDQUOT,	ALL,	"disk quota exceeded" },
217#endif
218#ifdef ENOSR
219	{ ENOSR,	ALL,	"no streams resources" },
220#endif
221	{ ENXIO,	ALL,	"no such device or address" },
222	{ EROFS,	ALL,	"read-only filesystem" },
223	{ ETXTBSY,	ALL,	"text busy" },
224	{ ENOMEM,	ALL,	"not enough memory" },
225#ifdef ENOLINK
226	{ ENOLINK,	ALL,	"remote access failed" },
227#endif
228#ifdef EMULTIHOP
229	{ EMULTIHOP,	ALL,	"remote access failed" },
230#endif
231#ifdef ECOMM
232	{ ECOMM,	ALL,	"remote access failed" },
233#endif
234#ifdef ESTALE
235	{ ESTALE,	ALL,	"remote access failed" },
236#endif
237#ifdef ETIMEDOUT
238	{ ETIMEDOUT,	ALL,	"remote access failed" },
239#endif
240#ifdef ELOOP
241	{ ELOOP,	ALL,	"symbolic link loop" },
242#endif
243	{ E2BIG,	E_EXEC,	"argument list too long" },
244#ifdef ELIBACC
245	{ ELIBACC,	E_EXEC,	"shared library missing" },
246#endif
247	{ EEXIST,	E_CREAT, "file exists" },
248	{ 0,		0,	NULL },
249};
250
251
252/*
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.
256 */
257
258char *
259errmsg(int e, int action)
260{
261	struct errname const *ep;
262	static char buf[12];
263
264	for (ep = errormsg ; ep->errcode ; ep++) {
265		if (ep->errcode == e && (ep->action & action) != 0)
266			return ep->msg;
267	}
268	fmtstr(buf, sizeof buf, "error %d", e);
269	return buf;
270}
271