xref: /netbsd-src/bin/sh/error.c (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /*	$NetBSD: error.c,v 1.39 2016/06/01 02:50:02 kre Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Kenneth Almquist.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)error.c	8.2 (Berkeley) 5/4/95";
39 #else
40 __RCSID("$NetBSD: error.c,v 1.39 2016/06/01 02:50:02 kre Exp $");
41 #endif
42 #endif /* not lint */
43 
44 /*
45  * Errors and exceptions.
46  */
47 
48 #include <signal.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <errno.h>
52 #include <stdio.h>
53 #include <string.h>
54 
55 #include "shell.h"
56 #include "eval.h" /* for commandname */
57 #include "main.h"
58 #include "options.h"
59 #include "output.h"
60 #include "error.h"
61 #include "show.h"
62 
63 
64 /*
65  * Code to handle exceptions in C.
66  */
67 
68 struct jmploc *handler;
69 int exception;
70 volatile int suppressint;
71 volatile int intpending;
72 
73 
74 static void exverror(int, const char *, va_list) __dead;
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 
82 void
83 exraise(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.  The call to _exit is necessary because
97  * there is a short period after a fork before the signal handlers are
98  * set to the appropriate value for the child.  (The test for iflag is
99  * just defensive programming.)
100  */
101 
102 void
103 onint(void)
104 {
105 	sigset_t nsigset;
106 
107 	if (suppressint) {
108 		intpending = 1;
109 		return;
110 	}
111 	intpending = 0;
112 	sigemptyset(&nsigset);
113 	sigprocmask(SIG_SETMASK, &nsigset, NULL);
114 	if (rootshell && iflag)
115 		exraise(EXINT);
116 	else {
117 		signal(SIGINT, SIG_DFL);
118 		raise(SIGINT);
119 	}
120 	/* NOTREACHED */
121 }
122 
123 static __printflike(2, 0) void
124 exvwarning(int sv_errno, const char *msg, va_list ap)
125 {
126 	/* Partially emulate line buffered output so that:
127 	 *	printf '%d\n' 1 a 2
128 	 * and
129 	 *	printf '%d %d %d\n' 1 a 2
130 	 * both generate sensible text when stdout and stderr are merged.
131 	 */
132 	if (output.nextc != output.buf && output.nextc[-1] == '\n')
133 		flushout(&output);
134 	if (commandname)
135 		outfmt(&errout, "%s: ", commandname);
136 	else
137 		outfmt(&errout, "%s: ", getprogname());
138 	if (msg != NULL) {
139 		doformat(&errout, msg, ap);
140 		if (sv_errno >= 0)
141 			outfmt(&errout, ": ");
142 	}
143 	if (sv_errno >= 0)
144 		outfmt(&errout, "%s", strerror(sv_errno));
145 	out2c('\n');
146 	flushout(&errout);
147 }
148 
149 /*
150  * Exverror is called to raise the error exception.  If the second argument
151  * is not NULL then error prints an error message using printf style
152  * formatting.  It then raises the error exception.
153  */
154 static __printflike(2, 0) void
155 exverror(int cond, const char *msg, va_list ap)
156 {
157 	CLEAR_PENDING_INT;
158 	INTOFF;
159 
160 #ifdef DEBUG
161 	if (msg) {
162 		TRACE(("exverror(%d, \"", cond));
163 		TRACEV((msg, ap));
164 		TRACE(("\") pid=%d\n", getpid()));
165 	} else
166 		TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
167 #endif
168 	if (msg)
169 		exvwarning(-1, msg, ap);
170 
171 	flushall();
172 	exraise(cond);
173 	/* NOTREACHED */
174 }
175 
176 
177 void
178 error(const char *msg, ...)
179 {
180 	va_list ap;
181 
182 	/*
183 	 * On error, we certainly never want exit(0)...
184 	 */
185 	if (exerrno == 0)
186 		exerrno = 1;
187 	va_start(ap, msg);
188 	exverror(EXERROR, msg, ap);
189 	/* NOTREACHED */
190 	va_end(ap);
191 }
192 
193 
194 void
195 exerror(int cond, const char *msg, ...)
196 {
197 	va_list ap;
198 
199 	va_start(ap, msg);
200 	exverror(cond, msg, ap);
201 	/* NOTREACHED */
202 	va_end(ap);
203 }
204 
205 /*
206  * error/warning routines for external builtins
207  */
208 
209 void
210 sh_exit(int rval)
211 {
212 	exerrno = rval & 255;
213 	exraise(EXEXEC);
214 }
215 
216 void
217 sh_err(int status, const char *fmt, ...)
218 {
219 	va_list ap;
220 
221 	va_start(ap, fmt);
222 	exvwarning(errno, fmt, ap);
223 	va_end(ap);
224 	sh_exit(status);
225 }
226 
227 void
228 sh_verr(int status, const char *fmt, va_list ap)
229 {
230 	exvwarning(errno, fmt, ap);
231 	sh_exit(status);
232 }
233 
234 void
235 sh_errx(int status, const char *fmt, ...)
236 {
237 	va_list ap;
238 
239 	va_start(ap, fmt);
240 	exvwarning(-1, fmt, ap);
241 	va_end(ap);
242 	sh_exit(status);
243 }
244 
245 void
246 sh_verrx(int status, const char *fmt, va_list ap)
247 {
248 	exvwarning(-1, fmt, ap);
249 	sh_exit(status);
250 }
251 
252 void
253 sh_warn(const char *fmt, ...)
254 {
255 	va_list ap;
256 
257 	va_start(ap, fmt);
258 	exvwarning(errno, fmt, ap);
259 	va_end(ap);
260 }
261 
262 void
263 sh_vwarn(const char *fmt, va_list ap)
264 {
265 	exvwarning(errno, fmt, ap);
266 }
267 
268 void
269 sh_warnx(const char *fmt, ...)
270 {
271 	va_list ap;
272 
273 	va_start(ap, fmt);
274 	exvwarning(-1, fmt, ap);
275 	va_end(ap);
276 }
277 
278 void
279 sh_vwarnx(const char *fmt, va_list ap)
280 {
281 	exvwarning(-1, fmt, ap);
282 }
283 
284 
285 /*
286  * Table of error messages.
287  */
288 
289 struct errname {
290 	short errcode;		/* error number */
291 	short action;		/* operation which encountered the error */
292 	const char *msg;	/* text describing the error */
293 };
294 
295 
296 #define ALL (E_OPEN|E_CREAT|E_EXEC)
297 
298 STATIC const struct errname errormsg[] = {
299 	{ EINTR,	ALL,	"interrupted" },
300 	{ EACCES,	ALL,	"permission denied" },
301 	{ EIO,		ALL,	"I/O error" },
302 	{ EEXIST,	ALL,	"file exists" },
303 	{ ENOENT,	E_OPEN,	"no such file" },
304 	{ ENOENT,	E_CREAT,"directory nonexistent" },
305 	{ ENOENT,	E_EXEC,	"not found" },
306 	{ ENOTDIR,	E_OPEN,	"no such file" },
307 	{ ENOTDIR,	E_CREAT,"directory nonexistent" },
308 	{ ENOTDIR,	E_EXEC,	"not found" },
309 	{ EISDIR,	ALL,	"is a directory" },
310 #ifdef EMFILE
311 	{ EMFILE,	ALL,	"too many open files" },
312 #endif
313 	{ ENFILE,	ALL,	"file table overflow" },
314 	{ ENOSPC,	ALL,	"file system full" },
315 #ifdef EDQUOT
316 	{ EDQUOT,	ALL,	"disk quota exceeded" },
317 #endif
318 #ifdef ENOSR
319 	{ ENOSR,	ALL,	"no streams resources" },
320 #endif
321 	{ ENXIO,	ALL,	"no such device or address" },
322 	{ EROFS,	ALL,	"read-only file system" },
323 	{ ETXTBSY,	ALL,	"text busy" },
324 #ifdef EAGAIN
325 	{ EAGAIN,	E_EXEC,	"not enough memory" },
326 #endif
327 	{ ENOMEM,	ALL,	"not enough memory" },
328 #ifdef ENOLINK
329 	{ ENOLINK,	ALL,	"remote access failed" },
330 #endif
331 #ifdef EMULTIHOP
332 	{ EMULTIHOP,	ALL,	"remote access failed" },
333 #endif
334 #ifdef ECOMM
335 	{ ECOMM,	ALL,	"remote access failed" },
336 #endif
337 #ifdef ESTALE
338 	{ ESTALE,	ALL,	"remote access failed" },
339 #endif
340 #ifdef ETIMEDOUT
341 	{ ETIMEDOUT,	ALL,	"remote access failed" },
342 #endif
343 #ifdef ELOOP
344 	{ ELOOP,	ALL,	"symbolic link loop" },
345 #endif
346 #ifdef ENAMETOOLONG
347 	{ ENAMETOOLONG,	ALL,	"file name too long" },
348 #endif
349 	{ E2BIG,	E_EXEC,	"argument list too long" },
350 #ifdef ELIBACC
351 	{ ELIBACC,	E_EXEC,	"shared library missing" },
352 #endif
353 	{ 0,		0,	NULL },
354 };
355 
356 
357 /*
358  * Return a string describing an error.  The returned string may be a
359  * pointer to a static buffer that will be overwritten on the next call.
360  * Action describes the operation that got the error.
361  */
362 
363 const char *
364 errmsg(int e, int action)
365 {
366 	struct errname const *ep;
367 	static char buf[12];
368 
369 	for (ep = errormsg ; ep->errcode ; ep++) {
370 		if (ep->errcode == e && (ep->action & action) != 0)
371 			return ep->msg;
372 	}
373 	fmtstr(buf, sizeof buf, "error %d", e);
374 	return buf;
375 }
376