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 #endif /* not lint */ 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 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(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(int e) 84 { 85 if (handler == NULL) 86 abort(); 87 exception = e; 88 longjmp(handler->loc, 1); 89 } 90 91 92 /* 93 * Called from trap.c when a SIGINT is received. (If the user specifies 94 * that SIGINT is to be trapped or ignored using the trap builtin, then 95 * this routine is not called.) Suppressint is nonzero when interrupts 96 * are held using the INTOFF macro. If SIGINTs are not suppressed and 97 * the shell is not a root shell, then we want to be terminated if we 98 * get here, as if we were terminated directly by a SIGINT. Arrange for 99 * this here. 100 */ 101 102 void 103 onint(void) 104 { 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(int cond, const char *msg, va_list ap) 144 { 145 CLEAR_PENDING_INT; 146 INTOFF; 147 148 #ifdef DEBUG 149 if (msg) 150 TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid())); 151 else 152 TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid())); 153 #endif 154 if (msg) { 155 if (commandname) 156 outfmt(&errout, "%s: ", commandname); 157 doformat(&errout, msg, ap); 158 out2c('\n'); 159 } 160 flushall(); 161 exraise(cond); 162 } 163 164 165 void 166 error(const char *msg, ...) 167 { 168 va_list ap; 169 va_start(ap, msg); 170 exverror(EXERROR, msg, ap); 171 va_end(ap); 172 } 173 174 175 void 176 exerror(int cond, const char *msg, ...) 177 { 178 va_list ap; 179 va_start(ap, msg); 180 exverror(cond, msg, ap); 181 va_end(ap); 182 } 183 184 185 186 /* 187 * Table of error messages. 188 */ 189 190 struct errname { 191 short errcode; /* error number */ 192 short action; /* operation which encountered the error */ 193 char *msg; /* text describing the error */ 194 }; 195 196 197 #define ALL (E_OPEN|E_CREAT|E_EXEC) 198 199 STATIC const struct errname errormsg[] = { 200 { EINTR, ALL, "interrupted" }, 201 { EACCES, ALL, "permission denied" }, 202 { EIO, ALL, "I/O error" }, 203 { ENOENT, E_OPEN, "no such file" }, 204 { ENOENT, E_CREAT,"directory nonexistent" }, 205 { ENOENT, E_EXEC, "not found" }, 206 { ENOTDIR, E_OPEN, "no such file" }, 207 { ENOTDIR, E_CREAT,"directory nonexistent" }, 208 { ENOTDIR, E_EXEC, "not found" }, 209 { EISDIR, ALL, "is a directory" }, 210 #ifdef notdef 211 { EMFILE, ALL, "too many open files" }, 212 #endif 213 { ENFILE, ALL, "file table overflow" }, 214 { ENOSPC, ALL, "filesystem full" }, 215 #ifdef EDQUOT 216 { EDQUOT, ALL, "disk quota exceeded" }, 217 #endif 218 #ifdef ENOSR 219 { ENOSR, ALL, "no streams resources" }, 220 #endif 221 { ENXIO, ALL, "no such device or address" }, 222 { EROFS, ALL, "read-only filesystem" }, 223 { ETXTBSY, ALL, "text busy" }, 224 { ENOMEM, ALL, "not enough memory" }, 225 #ifdef ENOLINK 226 { ENOLINK, ALL, "remote access failed" }, 227 #endif 228 #ifdef EMULTIHOP 229 { EMULTIHOP, ALL, "remote access failed" }, 230 #endif 231 #ifdef ECOMM 232 { ECOMM, ALL, "remote access failed" }, 233 #endif 234 #ifdef ESTALE 235 { ESTALE, ALL, "remote access failed" }, 236 #endif 237 #ifdef ETIMEDOUT 238 { ETIMEDOUT, ALL, "remote access failed" }, 239 #endif 240 #ifdef ELOOP 241 { ELOOP, ALL, "symbolic link loop" }, 242 #endif 243 { E2BIG, E_EXEC, "argument list too long" }, 244 #ifdef ELIBACC 245 { ELIBACC, E_EXEC, "shared library missing" }, 246 #endif 247 { EEXIST, E_CREAT, "file exists" }, 248 { 0, 0, NULL }, 249 }; 250 251 252 /* 253 * Return a string describing an error. The returned string may be a 254 * pointer to a static buffer that will be overwritten on the next call. 255 * Action describes the operation that got the error. 256 */ 257 258 char * 259 errmsg(int e, int action) 260 { 261 struct errname const *ep; 262 static char buf[12]; 263 264 for (ep = errormsg ; ep->errcode ; ep++) { 265 if (ep->errcode == e && (ep->action & action) != 0) 266 return ep->msg; 267 } 268 fmtstr(buf, sizeof buf, "error %d", e); 269 return buf; 270 } 271