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