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