1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kenneth Almquist. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 static char sccsid[] = "@(#)error.c 8.2 (Berkeley) 5/4/95"; 39 #endif /* not lint */ 40 41 /* 42 * Errors and exceptions. 43 */ 44 45 #include "shell.h" 46 #include "main.h" 47 #include "options.h" 48 #include "output.h" 49 #include "error.h" 50 #include "show.h" 51 #include <signal.h> 52 #include <unistd.h> 53 #include <errno.h> 54 55 56 /* 57 * Code to handle exceptions in C. 58 */ 59 60 struct jmploc *handler; 61 int exception; 62 volatile int suppressint; 63 volatile int intpending; 64 char *commandname; 65 66 67 /* 68 * Called to raise an exception. Since C doesn't include exceptions, we 69 * just do a longjmp to the exception handler. The type of exception is 70 * stored in the global variable "exception". 71 */ 72 73 void 74 exraise(e) 75 int e; 76 { 77 if (handler == NULL) 78 abort(); 79 exception = e; 80 longjmp(handler->loc, 1); 81 } 82 83 84 /* 85 * Called from trap.c when a SIGINT is received. (If the user specifies 86 * that SIGINT is to be trapped or ignored using the trap builtin, then 87 * this routine is not called.) Suppressint is nonzero when interrupts 88 * are held using the INTOFF macro. The call to _exit is necessary because 89 * there is a short period after a fork before the signal handlers are 90 * set to the appropriate value for the child. (The test for iflag is 91 * just defensive programming.) 92 */ 93 94 void 95 onint() { 96 sigset_t sigset; 97 98 if (suppressint) { 99 intpending++; 100 return; 101 } 102 intpending = 0; 103 sigemptyset(&sigset); 104 sigprocmask(SIG_SETMASK, &sigset, NULL); 105 if (rootshell && iflag) 106 exraise(EXINT); 107 else 108 _exit(128 + SIGINT); 109 } 110 111 112 113 void 114 error2(a, b) 115 char *a, *b; 116 { 117 error("%s: %s", a, b); 118 } 119 120 121 /* 122 * Error is called to raise the error exception. If the first argument 123 * is not NULL then error prints an error message using printf style 124 * formatting. It then raises the error exception. 125 */ 126 127 #if __STDC__ 128 void 129 error(char *msg, ...) 130 #else 131 void 132 error(va_alist) 133 va_dcl 134 #endif 135 { 136 #if !__STDC__ 137 char *msg; 138 #endif 139 va_list ap; 140 CLEAR_PENDING_INT; 141 INTOFF; 142 143 #if __STDC__ 144 va_start(ap, msg); 145 #else 146 va_start(ap); 147 msg = va_arg(ap, char *); 148 #endif 149 #ifdef DEBUG 150 if (msg) 151 TRACE(("error(\"%s\") pid=%d\n", msg, getpid())); 152 else 153 TRACE(("error(NULL) pid=%d\n", getpid())); 154 #endif 155 if (msg) { 156 if (commandname) 157 outfmt(&errout, "%s: ", commandname); 158 doformat(&errout, msg, ap); 159 out2c('\n'); 160 } 161 va_end(ap); 162 flushall(); 163 exraise(EXERROR); 164 } 165 166 167 168 /* 169 * Table of error messages. 170 */ 171 172 struct errname { 173 short errcode; /* error number */ 174 short action; /* operation which encountered the error */ 175 char *msg; /* text describing the error */ 176 }; 177 178 179 #define ALL (E_OPEN|E_CREAT|E_EXEC) 180 181 STATIC const struct errname errormsg[] = { 182 { EINTR, ALL, "interrupted" }, 183 { EACCES, ALL, "permission denied" }, 184 { EIO, ALL, "I/O error" }, 185 { ENOENT, E_OPEN, "no such file" }, 186 { ENOENT, E_CREAT,"directory nonexistent" }, 187 { ENOENT, E_EXEC, "not found" }, 188 { ENOTDIR, E_OPEN, "no such file" }, 189 { ENOTDIR, E_CREAT,"directory nonexistent" }, 190 { ENOTDIR, E_EXEC, "not found" }, 191 { EISDIR, ALL, "is a directory" }, 192 #ifdef notdef 193 { EMFILE, ALL, "too many open files" }, 194 #endif 195 { ENFILE, ALL, "file table overflow" }, 196 { ENOSPC, ALL, "file system full" }, 197 #ifdef EDQUOT 198 { EDQUOT, ALL, "disk quota exceeded" }, 199 #endif 200 #ifdef ENOSR 201 { ENOSR, ALL, "no streams resources" }, 202 #endif 203 { ENXIO, ALL, "no such device or address" }, 204 { EROFS, ALL, "read-only file system" }, 205 { ETXTBSY, ALL, "text busy" }, 206 #ifdef SYSV 207 { EAGAIN, E_EXEC, "not enough memory" }, 208 #endif 209 { ENOMEM, ALL, "not enough memory" }, 210 #ifdef ENOLINK 211 { ENOLINK, ALL, "remote access failed" }, 212 #endif 213 #ifdef EMULTIHOP 214 { EMULTIHOP, ALL, "remote access failed" }, 215 #endif 216 #ifdef ECOMM 217 { ECOMM, ALL, "remote access failed" }, 218 #endif 219 #ifdef ESTALE 220 { ESTALE, ALL, "remote access failed" }, 221 #endif 222 #ifdef ETIMEDOUT 223 { ETIMEDOUT, ALL, "remote access failed" }, 224 #endif 225 #ifdef ELOOP 226 { ELOOP, ALL, "symbolic link loop" }, 227 #endif 228 { E2BIG, E_EXEC, "argument list too long" }, 229 #ifdef ELIBACC 230 { ELIBACC, E_EXEC, "shared library missing" }, 231 #endif 232 { 0, 0, NULL }, 233 }; 234 235 236 /* 237 * Return a string describing an error. The returned string may be a 238 * pointer to a static buffer that will be overwritten on the next call. 239 * Action describes the operation that got the error. 240 */ 241 242 char * 243 errmsg(e, action) 244 int e; 245 int action; 246 { 247 struct errname const *ep; 248 static char buf[12]; 249 250 for (ep = errormsg ; ep->errcode ; ep++) { 251 if (ep->errcode == e && (ep->action & action) != 0) 252 return ep->msg; 253 } 254 fmtstr(buf, sizeof buf, "error %d", e); 255 return buf; 256 } 257