1*98b2eb36Skre /* $NetBSD: error.c,v 1.45 2023/03/19 17:55:57 kre Exp $ */
249f0ad86Scgd
361f28255Scgd /*-
437ed7877Sjtc * Copyright (c) 1991, 1993
537ed7877Sjtc * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * This code is derived from software contributed to Berkeley by
861f28255Scgd * Kenneth Almquist.
961f28255Scgd *
1061f28255Scgd * Redistribution and use in source and binary forms, with or without
1161f28255Scgd * modification, are permitted provided that the following conditions
1261f28255Scgd * are met:
1361f28255Scgd * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd * notice, this list of conditions and the following disclaimer.
1561f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd * notice, this list of conditions and the following disclaimer in the
1761f28255Scgd * documentation and/or other materials provided with the distribution.
18b5b29542Sagc * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd * may be used to endorse or promote products derived from this software
2061f28255Scgd * without specific prior written permission.
2161f28255Scgd *
2261f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd * SUCH DAMAGE.
3361f28255Scgd */
3461f28255Scgd
35cd799663Schristos #include <sys/cdefs.h>
3661f28255Scgd #ifndef lint
3749f0ad86Scgd #if 0
3807bae7edSchristos static char sccsid[] = "@(#)error.c 8.2 (Berkeley) 5/4/95";
3949f0ad86Scgd #else
40*98b2eb36Skre __RCSID("$NetBSD: error.c,v 1.45 2023/03/19 17:55:57 kre Exp $");
4149f0ad86Scgd #endif
4261f28255Scgd #endif /* not lint */
4361f28255Scgd
4461f28255Scgd /*
4561f28255Scgd * Errors and exceptions.
4661f28255Scgd */
4761f28255Scgd
487faabd58Schristos #include <signal.h>
49e2056eadSmatt #include <stdlib.h>
507faabd58Schristos #include <unistd.h>
517faabd58Schristos #include <errno.h>
52d84d3616Smycroft #include <stdio.h>
53c02b3bbdSchristos #include <string.h>
547faabd58Schristos
5561f28255Scgd #include "shell.h"
560faa1734Sdholland #include "eval.h" /* for commandname */
5761f28255Scgd #include "main.h"
5861f28255Scgd #include "options.h"
5961f28255Scgd #include "output.h"
6061f28255Scgd #include "error.h"
6107bae7edSchristos #include "show.h"
6261f28255Scgd
6361f28255Scgd
6461f28255Scgd /*
6561f28255Scgd * Code to handle exceptions in C.
6661f28255Scgd */
6761f28255Scgd
6861f28255Scgd struct jmploc *handler;
6961f28255Scgd int exception;
7061f28255Scgd volatile int suppressint;
7161f28255Scgd volatile int intpending;
724084f829Skre volatile int errors_suppressed;
734084f829Skre const char * volatile currentcontext;
744084f829Skre
7561f28255Scgd
7661f28255Scgd
7790ee124aSdogcow static void exverror(int, const char *, va_list) __dead;
782a9c11ddSchristos
7961f28255Scgd /*
8061f28255Scgd * Called to raise an exception. Since C doesn't include exceptions, we
8161f28255Scgd * just do a longjmp to the exception handler. The type of exception is
8261f28255Scgd * stored in the global variable "exception".
8361f28255Scgd */
8461f28255Scgd
8561f28255Scgd void
exraise(int e)86c02b3bbdSchristos exraise(int e)
875dad1439Scgd {
88f2dc4639Skre CTRACE(DBG_ERRS, ("exraise(%d)\n", e));
8961f28255Scgd if (handler == NULL)
9061f28255Scgd abort();
9161f28255Scgd exception = e;
9261f28255Scgd longjmp(handler->loc, 1);
9361f28255Scgd }
9461f28255Scgd
9561f28255Scgd
9661f28255Scgd /*
9761f28255Scgd * Called from trap.c when a SIGINT is received. (If the user specifies
9861f28255Scgd * that SIGINT is to be trapped or ignored using the trap builtin, then
9961f28255Scgd * this routine is not called.) Suppressint is nonzero when interrupts
10061f28255Scgd * are held using the INTOFF macro. The call to _exit is necessary because
10161f28255Scgd * there is a short period after a fork before the signal handlers are
10261f28255Scgd * set to the appropriate value for the child. (The test for iflag is
10361f28255Scgd * just defensive programming.)
10461f28255Scgd */
10561f28255Scgd
10661f28255Scgd void
onint(void)107c02b3bbdSchristos onint(void)
108c02b3bbdSchristos {
109b3df6303Skleink sigset_t nsigset;
1103e417cceSmycroft
11161f28255Scgd if (suppressint) {
112c02b3bbdSchristos intpending = 1;
11361f28255Scgd return;
11461f28255Scgd }
11561f28255Scgd intpending = 0;
116b3df6303Skleink sigemptyset(&nsigset);
117b3df6303Skleink sigprocmask(SIG_SETMASK, &nsigset, NULL);
11861f28255Scgd if (rootshell && iflag)
11961f28255Scgd exraise(EXINT);
1202e23138aSmycroft else {
1212e23138aSmycroft signal(SIGINT, SIG_DFL);
1222e23138aSmycroft raise(SIGINT);
1232e23138aSmycroft }
1249dc385beSmycroft /* NOTREACHED */
12561f28255Scgd }
12661f28255Scgd
12766dd2755Sjoerg static __printflike(2, 0) void
exvwarning(int sv_errno,const char * msg,va_list ap)128c02b3bbdSchristos exvwarning(int sv_errno, const char *msg, va_list ap)
129c02b3bbdSchristos {
130c02b3bbdSchristos /* Partially emulate line buffered output so that:
131c02b3bbdSchristos * printf '%d\n' 1 a 2
132c02b3bbdSchristos * and
133c02b3bbdSchristos * printf '%d %d %d\n' 1 a 2
134c02b3bbdSchristos * both generate sensible text when stdout and stderr are merged.
135c02b3bbdSchristos */
136a2d92a00Skre if (output.buf != NULL && output.nextc != output.buf &&
137a2d92a00Skre output.nextc[-1] == '\n')
138c02b3bbdSchristos flushout(&output);
1394084f829Skre
1404084f829Skre if (errors_suppressed)
1414084f829Skre return;
1424084f829Skre
143c02b3bbdSchristos if (commandname)
144c02b3bbdSchristos outfmt(&errout, "%s: ", commandname);
145ab28bedeSchristos else
146ab28bedeSchristos outfmt(&errout, "%s: ", getprogname());
1474084f829Skre if (currentcontext != NULL)
1484084f829Skre outfmt(&errout, "%s: ", currentcontext);
149c02b3bbdSchristos if (msg != NULL) {
150c02b3bbdSchristos doformat(&errout, msg, ap);
151c02b3bbdSchristos if (sv_errno >= 0)
152c02b3bbdSchristos outfmt(&errout, ": ");
153c02b3bbdSchristos }
154c02b3bbdSchristos if (sv_errno >= 0)
155c02b3bbdSchristos outfmt(&errout, "%s", strerror(sv_errno));
156c02b3bbdSchristos out2c('\n');
157c02b3bbdSchristos flushout(&errout);
158c02b3bbdSchristos }
15961f28255Scgd
16061f28255Scgd /*
161c02b3bbdSchristos * Exverror is called to raise the error exception. If the second argument
16261f28255Scgd * is not NULL then error prints an error message using printf style
16361f28255Scgd * formatting. It then raises the error exception.
16461f28255Scgd */
16566dd2755Sjoerg static __printflike(2, 0) void
exverror(int cond,const char * msg,va_list ap)166c02b3bbdSchristos exverror(int cond, const char *msg, va_list ap)
1672a9c11ddSchristos {
1682a9c11ddSchristos CLEAR_PENDING_INT;
1692a9c11ddSchristos INTOFF;
1702a9c11ddSchristos
1712a9c11ddSchristos #ifdef DEBUG
172e314f958Sdsl if (msg) {
1732eafa752Skre CTRACE(DBG_ERRS, ("exverror(%d, \"", cond));
1742eafa752Skre CTRACEV(DBG_ERRS, (msg, ap));
1752eafa752Skre CTRACE(DBG_ERRS, ("\") pid=%d\n", getpid()));
176e314f958Sdsl } else
1772eafa752Skre CTRACE(DBG_ERRS, ("exverror(%d, NULL) pid=%d\n", cond,
1782eafa752Skre getpid()));
1792a9c11ddSchristos #endif
180c02b3bbdSchristos if (msg)
181c02b3bbdSchristos exvwarning(-1, msg, ap);
182c02b3bbdSchristos
1832a9c11ddSchristos flushall();
1842a9c11ddSchristos exraise(cond);
1859dc385beSmycroft /* NOTREACHED */
1862a9c11ddSchristos }
1872a9c11ddSchristos
18861f28255Scgd
18961f28255Scgd void
error(const char * msg,...)1903d424690Schristos error(const char *msg, ...)
19161f28255Scgd {
19261f28255Scgd va_list ap;
1930b398b28Swiz
194919211f1Skre /*
195919211f1Skre * On error, we certainly never want exit(0)...
196919211f1Skre */
197919211f1Skre if (exerrno == 0)
198919211f1Skre exerrno = 1;
19961f28255Scgd va_start(ap, msg);
2002a9c11ddSchristos exverror(EXERROR, msg, ap);
201ee9e50eaSmycroft /* NOTREACHED */
20261f28255Scgd va_end(ap);
2032a9c11ddSchristos }
2042a9c11ddSchristos
2052a9c11ddSchristos
2062a9c11ddSchristos void
exerror(int cond,const char * msg,...)2073d424690Schristos exerror(int cond, const char *msg, ...)
2082a9c11ddSchristos {
2092a9c11ddSchristos va_list ap;
2100b398b28Swiz
2112a9c11ddSchristos va_start(ap, msg);
2122a9c11ddSchristos exverror(cond, msg, ap);
213ee9e50eaSmycroft /* NOTREACHED */
2142a9c11ddSchristos va_end(ap);
21561f28255Scgd }
21661f28255Scgd
217c02b3bbdSchristos /*
218c02b3bbdSchristos * error/warning routines for external builtins
219c02b3bbdSchristos */
220c02b3bbdSchristos
221c02b3bbdSchristos void
sh_exit(int rval)222c02b3bbdSchristos sh_exit(int rval)
223c02b3bbdSchristos {
224b8bee70dSkre VTRACE(DBG_ERRS|DBG_PROCS|DBG_CMDS, ("sh_exit(%d)\n", rval));
225c02b3bbdSchristos exerrno = rval & 255;
226c02b3bbdSchristos exraise(EXEXEC);
227c02b3bbdSchristos }
228c02b3bbdSchristos
229c02b3bbdSchristos void
sh_err(int status,const char * fmt,...)230c02b3bbdSchristos sh_err(int status, const char *fmt, ...)
231c02b3bbdSchristos {
232c02b3bbdSchristos va_list ap;
233c02b3bbdSchristos
234c02b3bbdSchristos va_start(ap, fmt);
235c02b3bbdSchristos exvwarning(errno, fmt, ap);
236c02b3bbdSchristos va_end(ap);
237c02b3bbdSchristos sh_exit(status);
238c02b3bbdSchristos }
239c02b3bbdSchristos
240c02b3bbdSchristos void
sh_verr(int status,const char * fmt,va_list ap)241c02b3bbdSchristos sh_verr(int status, const char *fmt, va_list ap)
242c02b3bbdSchristos {
243c02b3bbdSchristos exvwarning(errno, fmt, ap);
244c02b3bbdSchristos sh_exit(status);
245c02b3bbdSchristos }
246c02b3bbdSchristos
247c02b3bbdSchristos void
sh_errx(int status,const char * fmt,...)248c02b3bbdSchristos sh_errx(int status, const char *fmt, ...)
249c02b3bbdSchristos {
250c02b3bbdSchristos va_list ap;
251c02b3bbdSchristos
252c02b3bbdSchristos va_start(ap, fmt);
253c02b3bbdSchristos exvwarning(-1, fmt, ap);
254c02b3bbdSchristos va_end(ap);
255c02b3bbdSchristos sh_exit(status);
256c02b3bbdSchristos }
257c02b3bbdSchristos
258c02b3bbdSchristos void
sh_verrx(int status,const char * fmt,va_list ap)259c02b3bbdSchristos sh_verrx(int status, const char *fmt, va_list ap)
260c02b3bbdSchristos {
261c02b3bbdSchristos exvwarning(-1, fmt, ap);
262c02b3bbdSchristos sh_exit(status);
263c02b3bbdSchristos }
264c02b3bbdSchristos
265c02b3bbdSchristos void
sh_warn(const char * fmt,...)266c02b3bbdSchristos sh_warn(const char *fmt, ...)
267c02b3bbdSchristos {
268c02b3bbdSchristos va_list ap;
269c02b3bbdSchristos
270c02b3bbdSchristos va_start(ap, fmt);
271c02b3bbdSchristos exvwarning(errno, fmt, ap);
272c02b3bbdSchristos va_end(ap);
273c02b3bbdSchristos }
274c02b3bbdSchristos
275c02b3bbdSchristos void
sh_vwarn(const char * fmt,va_list ap)276c02b3bbdSchristos sh_vwarn(const char *fmt, va_list ap)
277c02b3bbdSchristos {
278c02b3bbdSchristos exvwarning(errno, fmt, ap);
279c02b3bbdSchristos }
280c02b3bbdSchristos
281c02b3bbdSchristos void
sh_warnx(const char * fmt,...)282c02b3bbdSchristos sh_warnx(const char *fmt, ...)
283c02b3bbdSchristos {
284c02b3bbdSchristos va_list ap;
285c02b3bbdSchristos
286c02b3bbdSchristos va_start(ap, fmt);
287c02b3bbdSchristos exvwarning(-1, fmt, ap);
288c02b3bbdSchristos va_end(ap);
289c02b3bbdSchristos }
290c02b3bbdSchristos
291c02b3bbdSchristos void
sh_vwarnx(const char * fmt,va_list ap)292c02b3bbdSchristos sh_vwarnx(const char *fmt, va_list ap)
293c02b3bbdSchristos {
294c02b3bbdSchristos exvwarning(-1, fmt, ap);
295c02b3bbdSchristos }
29661f28255Scgd
29761f28255Scgd
29861f28255Scgd /*
29961f28255Scgd * Table of error messages.
30061f28255Scgd */
30161f28255Scgd
30261f28255Scgd struct errname {
30361f28255Scgd short errcode; /* error number */
30461f28255Scgd short action; /* operation which encountered the error */
3053d424690Schristos const char *msg; /* text describing the error */
30661f28255Scgd };
30761f28255Scgd
30861f28255Scgd
30961f28255Scgd #define ALL (E_OPEN|E_CREAT|E_EXEC)
31061f28255Scgd
31161f28255Scgd STATIC const struct errname errormsg[] = {
31207bae7edSchristos { EINTR, ALL, "interrupted" },
313*98b2eb36Skre { EACCES, E_EXEC, "no execute permission" },
31407bae7edSchristos { EACCES, ALL, "permission denied" },
31507bae7edSchristos { EIO, ALL, "I/O error" },
316f629aa28Schristos { EEXIST, ALL, "file exists" },
31707bae7edSchristos { ENOENT, E_OPEN, "no such file" },
31807bae7edSchristos { ENOENT, E_CREAT,"directory nonexistent" },
31907bae7edSchristos { ENOENT, E_EXEC, "not found" },
32007bae7edSchristos { ENOTDIR, E_OPEN, "no such file" },
32107bae7edSchristos { ENOTDIR, E_CREAT,"directory nonexistent" },
32207bae7edSchristos { ENOTDIR, E_EXEC, "not found" },
32307bae7edSchristos { EISDIR, ALL, "is a directory" },
3246438e7dcSchristos #ifdef EMFILE
32507bae7edSchristos { EMFILE, ALL, "too many open files" },
32607bae7edSchristos #endif
32707bae7edSchristos { ENFILE, ALL, "file table overflow" },
32807bae7edSchristos { ENOSPC, ALL, "file system full" },
32961f28255Scgd #ifdef EDQUOT
33007bae7edSchristos { EDQUOT, ALL, "disk quota exceeded" },
33161f28255Scgd #endif
33261f28255Scgd #ifdef ENOSR
33307bae7edSchristos { ENOSR, ALL, "no streams resources" },
33461f28255Scgd #endif
33507bae7edSchristos { ENXIO, ALL, "no such device or address" },
33607bae7edSchristos { EROFS, ALL, "read-only file system" },
33707bae7edSchristos { ETXTBSY, ALL, "text busy" },
3386438e7dcSchristos #ifdef EAGAIN
33907bae7edSchristos { EAGAIN, E_EXEC, "not enough memory" },
34061f28255Scgd #endif
34107bae7edSchristos { ENOMEM, ALL, "not enough memory" },
34261f28255Scgd #ifdef ENOLINK
34307bae7edSchristos { ENOLINK, ALL, "remote access failed" },
34461f28255Scgd #endif
34561f28255Scgd #ifdef EMULTIHOP
34607bae7edSchristos { EMULTIHOP, ALL, "remote access failed" },
34761f28255Scgd #endif
34861f28255Scgd #ifdef ECOMM
34907bae7edSchristos { ECOMM, ALL, "remote access failed" },
35061f28255Scgd #endif
35161f28255Scgd #ifdef ESTALE
35207bae7edSchristos { ESTALE, ALL, "remote access failed" },
35361f28255Scgd #endif
35461f28255Scgd #ifdef ETIMEDOUT
35507bae7edSchristos { ETIMEDOUT, ALL, "remote access failed" },
35661f28255Scgd #endif
35761f28255Scgd #ifdef ELOOP
35807bae7edSchristos { ELOOP, ALL, "symbolic link loop" },
35961f28255Scgd #endif
3606ac601d8Sgarbled #ifdef ENAMETOOLONG
3616ac601d8Sgarbled { ENAMETOOLONG, ALL, "file name too long" },
3626ac601d8Sgarbled #endif
36307bae7edSchristos { E2BIG, E_EXEC, "argument list too long" },
36461f28255Scgd #ifdef ELIBACC
36507bae7edSchristos { ELIBACC, E_EXEC, "shared library missing" },
36661f28255Scgd #endif
36707bae7edSchristos { 0, 0, NULL },
36861f28255Scgd };
36961f28255Scgd
37061f28255Scgd
37161f28255Scgd /*
37261f28255Scgd * Return a string describing an error. The returned string may be a
37361f28255Scgd * pointer to a static buffer that will be overwritten on the next call.
37461f28255Scgd * Action describes the operation that got the error.
37561f28255Scgd */
37661f28255Scgd
3773d424690Schristos const char *
errmsg(int e,int action)378c02b3bbdSchristos errmsg(int e, int action)
3794ce0d34aScgd {
38061f28255Scgd struct errname const *ep;
38161f28255Scgd static char buf[12];
38261f28255Scgd
38361f28255Scgd for (ep = errormsg ; ep->errcode ; ep++) {
38461f28255Scgd if (ep->errcode == e && (ep->action & action) != 0)
38561f28255Scgd return ep->msg;
38661f28255Scgd }
38761f28255Scgd fmtstr(buf, sizeof buf, "error %d", e);
38861f28255Scgd return buf;
38961f28255Scgd }
390