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 #if 0 39 static char sccsid[] = "@(#)error.c 8.2 (Berkeley) 5/4/95"; 40 #endif 41 static const char rcsid[] = 42 "$FreeBSD$"; 43 #endif /* not lint */ 44 45 /* 46 * Errors and exceptions. 47 */ 48 49 #include "shell.h" 50 #include "main.h" 51 #include "options.h" 52 #include "output.h" 53 #include "error.h" 54 #include "nodes.h" /* show.h needs nodes.h */ 55 #include "show.h" 56 #include "trap.h" 57 #include <signal.h> 58 #include <stdlib.h> 59 #include <unistd.h> 60 #include <errno.h> 61 62 63 /* 64 * Code to handle exceptions in C. 65 */ 66 67 struct jmploc *handler; 68 volatile sig_atomic_t exception; 69 volatile sig_atomic_t suppressint; 70 volatile sig_atomic_t intpending; 71 char *commandname; 72 73 74 static void exverror __P((int, const char *, va_list)) __printf0like(2, 0); 75 76 /* 77 * Called to raise an exception. Since C doesn't include exceptions, we 78 * just do a longjmp to the exception handler. The type of exception is 79 * stored in the global variable "exception". 80 */ 81 82 void 83 exraise(e) 84 int e; 85 { 86 if (handler == NULL) 87 abort(); 88 exception = e; 89 longjmp(handler->loc, 1); 90 } 91 92 93 /* 94 * Called from trap.c when a SIGINT is received. (If the user specifies 95 * that SIGINT is to be trapped or ignored using the trap builtin, then 96 * this routine is not called.) Suppressint is nonzero when interrupts 97 * are held using the INTOFF macro. If SIGINTs are not suppressed and 98 * the shell is not a root shell, then we want to be terminated if we 99 * get here, as if we were terminated directly by a SIGINT. Arrange for 100 * this here. 101 */ 102 103 void 104 onint() { 105 sigset_t sigset; 106 107 /* 108 * The !in_dotrap here is safe. The only way we can arrive here 109 * with in_dotrap set is that a trap handler set SIGINT to SIG_DFL 110 * and killed itself. 111 */ 112 113 if (suppressint && !in_dotrap) { 114 intpending++; 115 return; 116 } 117 intpending = 0; 118 sigemptyset(&sigset); 119 sigprocmask(SIG_SETMASK, &sigset, NULL); 120 121 /* 122 * This doesn't seem to be needed, since main() emits a newline. 123 */ 124 #if 0 125 if (tcgetpgrp(0) == getpid()) 126 write(STDERR_FILENO, "\n", 1); 127 #endif 128 if (rootshell && iflag) 129 exraise(EXINT); 130 else { 131 signal(SIGINT, SIG_DFL); 132 kill(getpid(), SIGINT); 133 } 134 } 135 136 137 /* 138 * Exverror is called to raise the error exception. If the first argument 139 * is not NULL then error prints an error message using printf style 140 * formatting. It then raises the error exception. 141 */ 142 static void 143 exverror(cond, msg, ap) 144 int cond; 145 const char *msg; 146 va_list ap; 147 { 148 CLEAR_PENDING_INT; 149 INTOFF; 150 151 #ifdef DEBUG 152 if (msg) 153 TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid())); 154 else 155 TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid())); 156 #endif 157 if (msg) { 158 if (commandname) 159 outfmt(&errout, "%s: ", commandname); 160 doformat(&errout, msg, ap); 161 out2c('\n'); 162 } 163 flushall(); 164 exraise(cond); 165 } 166 167 168 #ifdef __STDC__ 169 void 170 error(const char *msg, ...) 171 #else 172 void 173 error(va_alist) 174 va_dcl 175 #endif 176 { 177 #ifndef __STDC__ 178 const char *msg; 179 #endif 180 va_list ap; 181 #ifdef __STDC__ 182 va_start(ap, msg); 183 #else 184 va_start(ap); 185 msg = va_arg(ap, char *); 186 #endif 187 exverror(EXERROR, msg, ap); 188 va_end(ap); 189 } 190 191 192 #ifdef __STDC__ 193 void 194 exerror(int cond, const char *msg, ...) 195 #else 196 void 197 exerror(va_alist) 198 va_dcl 199 #endif 200 { 201 #ifndef __STDC__ 202 int cond; 203 char *msg; 204 #endif 205 va_list ap; 206 #ifdef __STDC__ 207 va_start(ap, msg); 208 #else 209 va_start(ap); 210 cond = va_arg(ap, int); 211 msg = va_arg(ap, char *); 212 #endif 213 exverror(cond, msg, ap); 214 va_end(ap); 215 } 216 217 218 219 /* 220 * Table of error messages. 221 */ 222 223 struct errname { 224 short errcode; /* error number */ 225 short action; /* operation which encountered the error */ 226 char *msg; /* text describing the error */ 227 }; 228 229 230 #define ALL (E_OPEN|E_CREAT|E_EXEC) 231 232 STATIC const struct errname errormsg[] = { 233 { EINTR, ALL, "interrupted" }, 234 { EACCES, ALL, "permission denied" }, 235 { EIO, ALL, "I/O error" }, 236 { ENOENT, E_OPEN, "no such file" }, 237 { ENOENT, E_CREAT,"directory nonexistent" }, 238 { ENOENT, E_EXEC, "not found" }, 239 { ENOTDIR, E_OPEN, "no such file" }, 240 { ENOTDIR, E_CREAT,"directory nonexistent" }, 241 { ENOTDIR, E_EXEC, "not found" }, 242 { EISDIR, ALL, "is a directory" }, 243 #ifdef notdef 244 { EMFILE, ALL, "too many open files" }, 245 #endif 246 { ENFILE, ALL, "file table overflow" }, 247 { ENOSPC, ALL, "file system full" }, 248 #ifdef EDQUOT 249 { EDQUOT, ALL, "disk quota exceeded" }, 250 #endif 251 #ifdef ENOSR 252 { ENOSR, ALL, "no streams resources" }, 253 #endif 254 { ENXIO, ALL, "no such device or address" }, 255 { EROFS, ALL, "read-only file system" }, 256 { ETXTBSY, ALL, "text busy" }, 257 #ifdef SYSV 258 { EAGAIN, E_EXEC, "not enough memory" }, 259 #endif 260 { ENOMEM, ALL, "not enough memory" }, 261 #ifdef ENOLINK 262 { ENOLINK, ALL, "remote access failed" }, 263 #endif 264 #ifdef EMULTIHOP 265 { EMULTIHOP, ALL, "remote access failed" }, 266 #endif 267 #ifdef ECOMM 268 { ECOMM, ALL, "remote access failed" }, 269 #endif 270 #ifdef ESTALE 271 { ESTALE, ALL, "remote access failed" }, 272 #endif 273 #ifdef ETIMEDOUT 274 { ETIMEDOUT, ALL, "remote access failed" }, 275 #endif 276 #ifdef ELOOP 277 { ELOOP, ALL, "symbolic link loop" }, 278 #endif 279 { E2BIG, E_EXEC, "argument list too long" }, 280 #ifdef ELIBACC 281 { ELIBACC, E_EXEC, "shared library missing" }, 282 #endif 283 { 0, 0, NULL }, 284 }; 285 286 287 /* 288 * Return a string describing an error. The returned string may be a 289 * pointer to a static buffer that will be overwritten on the next call. 290 * Action describes the operation that got the error. 291 */ 292 293 char * 294 errmsg(e, action) 295 int e; 296 int action; 297 { 298 struct errname const *ep; 299 static char buf[12]; 300 301 for (ep = errormsg ; ep->errcode ; ep++) { 302 if (ep->errcode == e && (ep->action & action) != 0) 303 return ep->msg; 304 } 305 fmtstr(buf, sizeof buf, "error %d", e); 306 return buf; 307 } 308