1 /* $NetBSD: error.c,v 1.34 2007/12/15 19:44:37 perry 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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)error.c 8.2 (Berkeley) 5/4/95"; 39 #else 40 __RCSID("$NetBSD: error.c,v 1.34 2007/12/15 19:44:37 perry Exp $"); 41 #endif 42 #endif /* not lint */ 43 44 /* 45 * Errors and exceptions. 46 */ 47 48 #include <signal.h> 49 #include <stdlib.h> 50 #include <unistd.h> 51 #include <errno.h> 52 #include <stdio.h> 53 #include <string.h> 54 55 #include "shell.h" 56 #include "main.h" 57 #include "options.h" 58 #include "output.h" 59 #include "error.h" 60 #include "show.h" 61 62 63 /* 64 * Code to handle exceptions in C. 65 */ 66 67 struct jmploc *handler; 68 int exception; 69 volatile int suppressint; 70 volatile int intpending; 71 char *commandname; 72 73 74 static void exverror(int, const char *, va_list) 75 __dead; 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(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. The call to _exit is necessary because 98 * there is a short period after a fork before the signal handlers are 99 * set to the appropriate value for the child. (The test for iflag is 100 * just defensive programming.) 101 */ 102 103 void 104 onint(void) 105 { 106 sigset_t nsigset; 107 108 if (suppressint) { 109 intpending = 1; 110 return; 111 } 112 intpending = 0; 113 sigemptyset(&nsigset); 114 sigprocmask(SIG_SETMASK, &nsigset, NULL); 115 if (rootshell && iflag) 116 exraise(EXINT); 117 else { 118 signal(SIGINT, SIG_DFL); 119 raise(SIGINT); 120 } 121 /* NOTREACHED */ 122 } 123 124 static void 125 exvwarning(int sv_errno, const char *msg, va_list ap) 126 { 127 /* Partially emulate line buffered output so that: 128 * printf '%d\n' 1 a 2 129 * and 130 * printf '%d %d %d\n' 1 a 2 131 * both generate sensible text when stdout and stderr are merged. 132 */ 133 if (output.nextc != output.buf && output.nextc[-1] == '\n') 134 flushout(&output); 135 if (commandname) 136 outfmt(&errout, "%s: ", commandname); 137 else 138 outfmt(&errout, "%s: ", getprogname()); 139 if (msg != NULL) { 140 doformat(&errout, msg, ap); 141 if (sv_errno >= 0) 142 outfmt(&errout, ": "); 143 } 144 if (sv_errno >= 0) 145 outfmt(&errout, "%s", strerror(sv_errno)); 146 out2c('\n'); 147 flushout(&errout); 148 } 149 150 /* 151 * Exverror is called to raise the error exception. If the second argument 152 * is not NULL then error prints an error message using printf style 153 * formatting. It then raises the error exception. 154 */ 155 static void 156 exverror(int cond, const char *msg, va_list ap) 157 { 158 CLEAR_PENDING_INT; 159 INTOFF; 160 161 #ifdef DEBUG 162 if (msg) { 163 TRACE(("exverror(%d, \"", cond)); 164 TRACEV((msg, ap)); 165 TRACE(("\") pid=%d\n", getpid())); 166 } else 167 TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid())); 168 #endif 169 if (msg) 170 exvwarning(-1, msg, ap); 171 172 flushall(); 173 exraise(cond); 174 /* NOTREACHED */ 175 } 176 177 178 void 179 error(const char *msg, ...) 180 { 181 va_list ap; 182 183 va_start(ap, msg); 184 exverror(EXERROR, msg, ap); 185 /* NOTREACHED */ 186 va_end(ap); 187 } 188 189 190 void 191 exerror(int cond, const char *msg, ...) 192 { 193 va_list ap; 194 195 va_start(ap, msg); 196 exverror(cond, msg, ap); 197 /* NOTREACHED */ 198 va_end(ap); 199 } 200 201 /* 202 * error/warning routines for external builtins 203 */ 204 205 void 206 sh_exit(int rval) 207 { 208 exerrno = rval & 255; 209 exraise(EXEXEC); 210 } 211 212 void 213 sh_err(int status, const char *fmt, ...) 214 { 215 va_list ap; 216 217 va_start(ap, fmt); 218 exvwarning(errno, fmt, ap); 219 va_end(ap); 220 sh_exit(status); 221 } 222 223 void 224 sh_verr(int status, const char *fmt, va_list ap) 225 { 226 exvwarning(errno, fmt, ap); 227 sh_exit(status); 228 } 229 230 void 231 sh_errx(int status, const char *fmt, ...) 232 { 233 va_list ap; 234 235 va_start(ap, fmt); 236 exvwarning(-1, fmt, ap); 237 va_end(ap); 238 sh_exit(status); 239 } 240 241 void 242 sh_verrx(int status, const char *fmt, va_list ap) 243 { 244 exvwarning(-1, fmt, ap); 245 sh_exit(status); 246 } 247 248 void 249 sh_warn(const char *fmt, ...) 250 { 251 va_list ap; 252 253 va_start(ap, fmt); 254 exvwarning(errno, fmt, ap); 255 va_end(ap); 256 } 257 258 void 259 sh_vwarn(const char *fmt, va_list ap) 260 { 261 exvwarning(errno, fmt, ap); 262 } 263 264 void 265 sh_warnx(const char *fmt, ...) 266 { 267 va_list ap; 268 269 va_start(ap, fmt); 270 exvwarning(-1, fmt, ap); 271 va_end(ap); 272 } 273 274 void 275 sh_vwarnx(const char *fmt, va_list ap) 276 { 277 exvwarning(-1, fmt, ap); 278 } 279 280 281 /* 282 * Table of error messages. 283 */ 284 285 struct errname { 286 short errcode; /* error number */ 287 short action; /* operation which encountered the error */ 288 const char *msg; /* text describing the error */ 289 }; 290 291 292 #define ALL (E_OPEN|E_CREAT|E_EXEC) 293 294 STATIC const struct errname errormsg[] = { 295 { EINTR, ALL, "interrupted" }, 296 { EACCES, ALL, "permission denied" }, 297 { EIO, ALL, "I/O error" }, 298 { EEXIST, ALL, "file exists" }, 299 { ENOENT, E_OPEN, "no such file" }, 300 { ENOENT, E_CREAT,"directory nonexistent" }, 301 { ENOENT, E_EXEC, "not found" }, 302 { ENOTDIR, E_OPEN, "no such file" }, 303 { ENOTDIR, E_CREAT,"directory nonexistent" }, 304 { ENOTDIR, E_EXEC, "not found" }, 305 { EISDIR, ALL, "is a directory" }, 306 #ifdef EMFILE 307 { EMFILE, ALL, "too many open files" }, 308 #endif 309 { ENFILE, ALL, "file table overflow" }, 310 { ENOSPC, ALL, "file system full" }, 311 #ifdef EDQUOT 312 { EDQUOT, ALL, "disk quota exceeded" }, 313 #endif 314 #ifdef ENOSR 315 { ENOSR, ALL, "no streams resources" }, 316 #endif 317 { ENXIO, ALL, "no such device or address" }, 318 { EROFS, ALL, "read-only file system" }, 319 { ETXTBSY, ALL, "text busy" }, 320 #ifdef EAGAIN 321 { EAGAIN, E_EXEC, "not enough memory" }, 322 #endif 323 { ENOMEM, ALL, "not enough memory" }, 324 #ifdef ENOLINK 325 { ENOLINK, ALL, "remote access failed" }, 326 #endif 327 #ifdef EMULTIHOP 328 { EMULTIHOP, ALL, "remote access failed" }, 329 #endif 330 #ifdef ECOMM 331 { ECOMM, ALL, "remote access failed" }, 332 #endif 333 #ifdef ESTALE 334 { ESTALE, ALL, "remote access failed" }, 335 #endif 336 #ifdef ETIMEDOUT 337 { ETIMEDOUT, ALL, "remote access failed" }, 338 #endif 339 #ifdef ELOOP 340 { ELOOP, ALL, "symbolic link loop" }, 341 #endif 342 #ifdef ENAMETOOLONG 343 { ENAMETOOLONG, ALL, "file name too long" }, 344 #endif 345 { E2BIG, E_EXEC, "argument list too long" }, 346 #ifdef ELIBACC 347 { ELIBACC, E_EXEC, "shared library missing" }, 348 #endif 349 { 0, 0, NULL }, 350 }; 351 352 353 /* 354 * Return a string describing an error. The returned string may be a 355 * pointer to a static buffer that will be overwritten on the next call. 356 * Action describes the operation that got the error. 357 */ 358 359 const char * 360 errmsg(int e, int action) 361 { 362 struct errname const *ep; 363 static char buf[12]; 364 365 for (ep = errormsg ; ep->errcode ; ep++) { 366 if (ep->errcode == e && (ep->action & action) != 0) 367 return ep->msg; 368 } 369 fmtstr(buf, sizeof buf, "error %d", e); 370 return buf; 371 } 372