error.c revision 1.43 1 /* $NetBSD: error.c,v 1.43 2019/02/04 11:16:41 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.43 2019/02/04 11:16:41 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 volatile int errors_suppressed;
73 const char * volatile currentcontext;
74
75
76
77 static void exverror(int, const char *, va_list) __dead;
78
79 /*
80 * Called to raise an exception. Since C doesn't include exceptions, we
81 * just do a longjmp to the exception handler. The type of exception is
82 * stored in the global variable "exception".
83 */
84
85 void
86 exraise(int e)
87 {
88 CTRACE(DBG_ERRS, ("exraise(%d)\n", e));
89 if (handler == NULL)
90 abort();
91 exception = e;
92 longjmp(handler->loc, 1);
93 }
94
95
96 /*
97 * Called from trap.c when a SIGINT is received. (If the user specifies
98 * that SIGINT is to be trapped or ignored using the trap builtin, then
99 * this routine is not called.) Suppressint is nonzero when interrupts
100 * are held using the INTOFF macro. The call to _exit is necessary because
101 * there is a short period after a fork before the signal handlers are
102 * set to the appropriate value for the child. (The test for iflag is
103 * just defensive programming.)
104 */
105
106 void
107 onint(void)
108 {
109 sigset_t nsigset;
110
111 if (suppressint) {
112 intpending = 1;
113 return;
114 }
115 intpending = 0;
116 sigemptyset(&nsigset);
117 sigprocmask(SIG_SETMASK, &nsigset, NULL);
118 if (rootshell && iflag)
119 exraise(EXINT);
120 else {
121 signal(SIGINT, SIG_DFL);
122 raise(SIGINT);
123 }
124 /* NOTREACHED */
125 }
126
127 static __printflike(2, 0) void
128 exvwarning(int sv_errno, const char *msg, va_list ap)
129 {
130 /* Partially emulate line buffered output so that:
131 * printf '%d\n' 1 a 2
132 * and
133 * printf '%d %d %d\n' 1 a 2
134 * both generate sensible text when stdout and stderr are merged.
135 */
136 if (output.buf != NULL && output.nextc != output.buf &&
137 output.nextc[-1] == '\n')
138 flushout(&output);
139
140 if (errors_suppressed)
141 return;
142
143 if (commandname)
144 outfmt(&errout, "%s: ", commandname);
145 else
146 outfmt(&errout, "%s: ", getprogname());
147 if (currentcontext != NULL)
148 outfmt(&errout, "%s: ", currentcontext);
149 if (msg != NULL) {
150 doformat(&errout, msg, ap);
151 if (sv_errno >= 0)
152 outfmt(&errout, ": ");
153 }
154 if (sv_errno >= 0)
155 outfmt(&errout, "%s", strerror(sv_errno));
156 out2c('\n');
157 flushout(&errout);
158 }
159
160 /*
161 * Exverror is called to raise the error exception. If the second argument
162 * is not NULL then error prints an error message using printf style
163 * formatting. It then raises the error exception.
164 */
165 static __printflike(2, 0) void
166 exverror(int cond, const char *msg, va_list ap)
167 {
168 CLEAR_PENDING_INT;
169 INTOFF;
170
171 #ifdef DEBUG
172 if (msg) {
173 CTRACE(DBG_ERRS, ("exverror(%d, \"", cond));
174 CTRACEV(DBG_ERRS, (msg, ap));
175 CTRACE(DBG_ERRS, ("\") pid=%d\n", getpid()));
176 } else
177 CTRACE(DBG_ERRS, ("exverror(%d, NULL) pid=%d\n", cond,
178 getpid()));
179 #endif
180 if (msg)
181 exvwarning(-1, msg, ap);
182
183 flushall();
184 exraise(cond);
185 /* NOTREACHED */
186 }
187
188
189 void
190 error(const char *msg, ...)
191 {
192 va_list ap;
193
194 /*
195 * On error, we certainly never want exit(0)...
196 */
197 if (exerrno == 0)
198 exerrno = 1;
199 va_start(ap, msg);
200 exverror(EXERROR, msg, ap);
201 /* NOTREACHED */
202 va_end(ap);
203 }
204
205
206 void
207 exerror(int cond, const char *msg, ...)
208 {
209 va_list ap;
210
211 va_start(ap, msg);
212 exverror(cond, msg, ap);
213 /* NOTREACHED */
214 va_end(ap);
215 }
216
217 /*
218 * error/warning routines for external builtins
219 */
220
221 void
222 sh_exit(int rval)
223 {
224 exerrno = rval & 255;
225 exraise(EXEXEC);
226 }
227
228 void
229 sh_err(int status, const char *fmt, ...)
230 {
231 va_list ap;
232
233 va_start(ap, fmt);
234 exvwarning(errno, fmt, ap);
235 va_end(ap);
236 sh_exit(status);
237 }
238
239 void
240 sh_verr(int status, const char *fmt, va_list ap)
241 {
242 exvwarning(errno, fmt, ap);
243 sh_exit(status);
244 }
245
246 void
247 sh_errx(int status, const char *fmt, ...)
248 {
249 va_list ap;
250
251 va_start(ap, fmt);
252 exvwarning(-1, fmt, ap);
253 va_end(ap);
254 sh_exit(status);
255 }
256
257 void
258 sh_verrx(int status, const char *fmt, va_list ap)
259 {
260 exvwarning(-1, fmt, ap);
261 sh_exit(status);
262 }
263
264 void
265 sh_warn(const char *fmt, ...)
266 {
267 va_list ap;
268
269 va_start(ap, fmt);
270 exvwarning(errno, fmt, ap);
271 va_end(ap);
272 }
273
274 void
275 sh_vwarn(const char *fmt, va_list ap)
276 {
277 exvwarning(errno, fmt, ap);
278 }
279
280 void
281 sh_warnx(const char *fmt, ...)
282 {
283 va_list ap;
284
285 va_start(ap, fmt);
286 exvwarning(-1, fmt, ap);
287 va_end(ap);
288 }
289
290 void
291 sh_vwarnx(const char *fmt, va_list ap)
292 {
293 exvwarning(-1, fmt, ap);
294 }
295
296
297 /*
298 * Table of error messages.
299 */
300
301 struct errname {
302 short errcode; /* error number */
303 short action; /* operation which encountered the error */
304 const char *msg; /* text describing the error */
305 };
306
307
308 #define ALL (E_OPEN|E_CREAT|E_EXEC)
309
310 STATIC const struct errname errormsg[] = {
311 { EINTR, ALL, "interrupted" },
312 { EACCES, ALL, "permission denied" },
313 { EIO, ALL, "I/O error" },
314 { EEXIST, ALL, "file exists" },
315 { ENOENT, E_OPEN, "no such file" },
316 { ENOENT, E_CREAT,"directory nonexistent" },
317 { ENOENT, E_EXEC, "not found" },
318 { ENOTDIR, E_OPEN, "no such file" },
319 { ENOTDIR, E_CREAT,"directory nonexistent" },
320 { ENOTDIR, E_EXEC, "not found" },
321 { EISDIR, ALL, "is a directory" },
322 #ifdef EMFILE
323 { EMFILE, ALL, "too many open files" },
324 #endif
325 { ENFILE, ALL, "file table overflow" },
326 { ENOSPC, ALL, "file system full" },
327 #ifdef EDQUOT
328 { EDQUOT, ALL, "disk quota exceeded" },
329 #endif
330 #ifdef ENOSR
331 { ENOSR, ALL, "no streams resources" },
332 #endif
333 { ENXIO, ALL, "no such device or address" },
334 { EROFS, ALL, "read-only file system" },
335 { ETXTBSY, ALL, "text busy" },
336 #ifdef EAGAIN
337 { EAGAIN, E_EXEC, "not enough memory" },
338 #endif
339 { ENOMEM, ALL, "not enough memory" },
340 #ifdef ENOLINK
341 { ENOLINK, ALL, "remote access failed" },
342 #endif
343 #ifdef EMULTIHOP
344 { EMULTIHOP, ALL, "remote access failed" },
345 #endif
346 #ifdef ECOMM
347 { ECOMM, ALL, "remote access failed" },
348 #endif
349 #ifdef ESTALE
350 { ESTALE, ALL, "remote access failed" },
351 #endif
352 #ifdef ETIMEDOUT
353 { ETIMEDOUT, ALL, "remote access failed" },
354 #endif
355 #ifdef ELOOP
356 { ELOOP, ALL, "symbolic link loop" },
357 #endif
358 #ifdef ENAMETOOLONG
359 { ENAMETOOLONG, ALL, "file name too long" },
360 #endif
361 { E2BIG, E_EXEC, "argument list too long" },
362 #ifdef ELIBACC
363 { ELIBACC, E_EXEC, "shared library missing" },
364 #endif
365 { 0, 0, NULL },
366 };
367
368
369 /*
370 * Return a string describing an error. The returned string may be a
371 * pointer to a static buffer that will be overwritten on the next call.
372 * Action describes the operation that got the error.
373 */
374
375 const char *
376 errmsg(int e, int action)
377 {
378 struct errname const *ep;
379 static char buf[12];
380
381 for (ep = errormsg ; ep->errcode ; ep++) {
382 if (ep->errcode == e && (ep->action & action) != 0)
383 return ep->msg;
384 }
385 fmtstr(buf, sizeof buf, "error %d", e);
386 return buf;
387 }
388