error.c revision 38530
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38#if 0
39static char sccsid[] = "@(#)error.c	8.2 (Berkeley) 5/4/95";
40#endif
41static const char rcsid[] =
42	"$Id: error.c,v 1.11 1998/08/24 10:20:36 cracauer Exp $";
43#endif /* not lint */
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 "show.h"
55#include "trap.h"
56#include <signal.h>
57#include <unistd.h>
58#include <errno.h>
59
60
61/*
62 * Code to handle exceptions in C.
63 */
64
65struct jmploc *handler;
66volatile sig_atomic_t exception;
67volatile sig_atomic_t suppressint;
68volatile sig_atomic_t intpending;
69char *commandname;
70
71
72static void exverror __P((int, char *, va_list));
73
74/*
75 * Called to raise an exception.  Since C doesn't include exceptions, we
76 * just do a longjmp to the exception handler.  The type of exception is
77 * stored in the global variable "exception".
78 */
79
80void
81exraise(e)
82	int e;
83{
84	if (handler == NULL)
85		abort();
86	exception = e;
87	longjmp(handler->loc, 1);
88}
89
90
91/*
92 * Called from trap.c when a SIGINT is received.  (If the user specifies
93 * that SIGINT is to be trapped or ignored using the trap builtin, then
94 * this routine is not called.)  Supressint is nonzero when interrupts
95 * are held using the INTOFF macro.  If SIGINTs are not suppressed and
96 * the shell is not a root shell, then we want to be terminated if we
97 * get here, as if we were terminated directly by a SIGINT.  Arrange for
98 * this here.
99 */
100
101void
102onint() {
103	sigset_t sigset;
104
105	/* The !in_dotrap is save. The only way we can arrive here with
106	 * in_dotrap set is that a trap handler set SIGINT to default
107	 * and killed itself.
108	 */
109
110	if (suppressint && !in_dotrap) {
111		intpending++;
112		return;
113	}
114	intpending = 0;
115	sigemptyset(&sigset);
116	sigprocmask(SIG_SETMASK, &sigset, NULL);
117
118	/* This doesn't seem to be needed. Note that main emit a newline
119	 * as well.
120         */
121#if 0
122	if (tcgetpgrp(0) == getpid())
123		write(STDERR_FILENO, "\n", 1);
124#endif
125	if (rootshell && iflag)
126		exraise(EXINT);
127	else {
128		signal(SIGINT, SIG_DFL);
129		kill(getpid(), SIGINT);
130	}
131}
132
133
134/*
135 * Exverror is called to raise the error exception.  If the first argument
136 * is not NULL then error prints an error message using printf style
137 * formatting.  It then raises the error exception.
138 */
139static void
140exverror(cond, msg, ap)
141	int cond;
142	char *msg;
143	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
165#ifdef __STDC__
166void
167error(char *msg, ...)
168#else
169void
170error(va_alist)
171	va_dcl
172#endif
173{
174#ifndef __STDC__
175	char *msg;
176#endif
177	va_list ap;
178#ifdef __STDC__
179	va_start(ap, msg);
180#else
181	va_start(ap);
182	msg = va_arg(ap, char *);
183#endif
184	exverror(EXERROR, msg, ap);
185	va_end(ap);
186}
187
188
189#ifdef __STDC__
190void
191exerror(int cond, char *msg, ...)
192#else
193void
194exerror(va_alist)
195	va_dcl
196#endif
197{
198#ifndef __STDC__
199	int cond;
200	char *msg;
201#endif
202	va_list ap;
203#ifdef __STDC__
204	va_start(ap, msg);
205#else
206	va_start(ap);
207	cond = va_arg(ap, int);
208	msg = va_arg(ap, char *);
209#endif
210	exverror(cond, msg, ap);
211	va_end(ap);
212}
213
214
215
216/*
217 * Table of error messages.
218 */
219
220struct errname {
221	short errcode;		/* error number */
222	short action;		/* operation which encountered the error */
223	char *msg;		/* text describing the error */
224};
225
226
227#define ALL (E_OPEN|E_CREAT|E_EXEC)
228
229STATIC const struct errname errormsg[] = {
230	{ EINTR,	ALL,	"interrupted" },
231	{ EACCES,	ALL,	"permission denied" },
232	{ EIO,		ALL,	"I/O error" },
233	{ ENOENT,	E_OPEN,	"no such file" },
234	{ ENOENT,	E_CREAT,"directory nonexistent" },
235	{ ENOENT,	E_EXEC,	"not found" },
236	{ ENOTDIR,	E_OPEN,	"no such file" },
237	{ ENOTDIR,	E_CREAT,"directory nonexistent" },
238	{ ENOTDIR,	E_EXEC,	"not found" },
239	{ EISDIR,	ALL,	"is a directory" },
240#ifdef notdef
241	{ EMFILE,	ALL,	"too many open files" },
242#endif
243	{ ENFILE,	ALL,	"file table overflow" },
244	{ ENOSPC,	ALL,	"file system full" },
245#ifdef EDQUOT
246	{ EDQUOT,	ALL,	"disk quota exceeded" },
247#endif
248#ifdef ENOSR
249	{ ENOSR,	ALL,	"no streams resources" },
250#endif
251	{ ENXIO,	ALL,	"no such device or address" },
252	{ EROFS,	ALL,	"read-only file system" },
253	{ ETXTBSY,	ALL,	"text busy" },
254#ifdef SYSV
255	{ EAGAIN,	E_EXEC,	"not enough memory" },
256#endif
257	{ ENOMEM,	ALL,	"not enough memory" },
258#ifdef ENOLINK
259	{ ENOLINK,	ALL,	"remote access failed" },
260#endif
261#ifdef EMULTIHOP
262	{ EMULTIHOP,	ALL,	"remote access failed" },
263#endif
264#ifdef ECOMM
265	{ ECOMM,	ALL,	"remote access failed" },
266#endif
267#ifdef ESTALE
268	{ ESTALE,	ALL,	"remote access failed" },
269#endif
270#ifdef ETIMEDOUT
271	{ ETIMEDOUT,	ALL,	"remote access failed" },
272#endif
273#ifdef ELOOP
274	{ ELOOP,	ALL,	"symbolic link loop" },
275#endif
276	{ E2BIG,	E_EXEC,	"argument list too long" },
277#ifdef ELIBACC
278	{ ELIBACC,	E_EXEC,	"shared library missing" },
279#endif
280	{ 0,		0,	NULL },
281};
282
283
284/*
285 * Return a string describing an error.  The returned string may be a
286 * pointer to a static buffer that will be overwritten on the next call.
287 * Action describes the operation that got the error.
288 */
289
290char *
291errmsg(e, action)
292	int e;
293	int action;
294{
295	struct errname const *ep;
296	static char buf[12];
297
298	for (ep = errormsg ; ep->errcode ; ep++) {
299		if (ep->errcode == e && (ep->action & action) != 0)
300			return ep->msg;
301	}
302	fmtstr(buf, sizeof buf, "error %d", e);
303	return buf;
304}
305