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