1 /* $NetBSD: error.c,v 1.41 2017/07/24 12:35:12 kre 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.41 2017/07/24 12:35:12 kre 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 "eval.h" /* for commandname */ 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 73 74 static void exverror(int, const char *, va_list) __dead; 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. The call to _exit is necessary because 97 * there is a short period after a fork before the signal handlers are 98 * set to the appropriate value for the child. (The test for iflag is 99 * just defensive programming.) 100 */ 101 102 void 103 onint(void) 104 { 105 sigset_t nsigset; 106 107 if (suppressint) { 108 intpending = 1; 109 return; 110 } 111 intpending = 0; 112 sigemptyset(&nsigset); 113 sigprocmask(SIG_SETMASK, &nsigset, NULL); 114 if (rootshell && iflag) 115 exraise(EXINT); 116 else { 117 signal(SIGINT, SIG_DFL); 118 raise(SIGINT); 119 } 120 /* NOTREACHED */ 121 } 122 123 static __printflike(2, 0) void 124 exvwarning(int sv_errno, const char *msg, va_list ap) 125 { 126 /* Partially emulate line buffered output so that: 127 * printf '%d\n' 1 a 2 128 * and 129 * printf '%d %d %d\n' 1 a 2 130 * both generate sensible text when stdout and stderr are merged. 131 */ 132 if (output.buf != NULL && output.nextc != output.buf && 133 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 __printflike(2, 0) 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 CTRACE(DBG_ERRS, ("exverror(%d, \"", cond)); 164 CTRACEV(DBG_ERRS, (msg, ap)); 165 CTRACE(DBG_ERRS, ("\") pid=%d\n", getpid())); 166 } else 167 CTRACE(DBG_ERRS, ("exverror(%d, NULL) pid=%d\n", cond, 168 getpid())); 169 #endif 170 if (msg) 171 exvwarning(-1, msg, ap); 172 173 flushall(); 174 exraise(cond); 175 /* NOTREACHED */ 176 } 177 178 179 void 180 error(const char *msg, ...) 181 { 182 va_list ap; 183 184 /* 185 * On error, we certainly never want exit(0)... 186 */ 187 if (exerrno == 0) 188 exerrno = 1; 189 va_start(ap, msg); 190 exverror(EXERROR, msg, ap); 191 /* NOTREACHED */ 192 va_end(ap); 193 } 194 195 196 void 197 exerror(int cond, const char *msg, ...) 198 { 199 va_list ap; 200 201 va_start(ap, msg); 202 exverror(cond, msg, ap); 203 /* NOTREACHED */ 204 va_end(ap); 205 } 206 207 /* 208 * error/warning routines for external builtins 209 */ 210 211 void 212 sh_exit(int rval) 213 { 214 exerrno = rval & 255; 215 exraise(EXEXEC); 216 } 217 218 void 219 sh_err(int status, const char *fmt, ...) 220 { 221 va_list ap; 222 223 va_start(ap, fmt); 224 exvwarning(errno, fmt, ap); 225 va_end(ap); 226 sh_exit(status); 227 } 228 229 void 230 sh_verr(int status, const char *fmt, va_list ap) 231 { 232 exvwarning(errno, fmt, ap); 233 sh_exit(status); 234 } 235 236 void 237 sh_errx(int status, const char *fmt, ...) 238 { 239 va_list ap; 240 241 va_start(ap, fmt); 242 exvwarning(-1, fmt, ap); 243 va_end(ap); 244 sh_exit(status); 245 } 246 247 void 248 sh_verrx(int status, const char *fmt, va_list ap) 249 { 250 exvwarning(-1, fmt, ap); 251 sh_exit(status); 252 } 253 254 void 255 sh_warn(const char *fmt, ...) 256 { 257 va_list ap; 258 259 va_start(ap, fmt); 260 exvwarning(errno, fmt, ap); 261 va_end(ap); 262 } 263 264 void 265 sh_vwarn(const char *fmt, va_list ap) 266 { 267 exvwarning(errno, fmt, ap); 268 } 269 270 void 271 sh_warnx(const char *fmt, ...) 272 { 273 va_list ap; 274 275 va_start(ap, fmt); 276 exvwarning(-1, fmt, ap); 277 va_end(ap); 278 } 279 280 void 281 sh_vwarnx(const char *fmt, va_list ap) 282 { 283 exvwarning(-1, fmt, ap); 284 } 285 286 287 /* 288 * Table of error messages. 289 */ 290 291 struct errname { 292 short errcode; /* error number */ 293 short action; /* operation which encountered the error */ 294 const char *msg; /* text describing the error */ 295 }; 296 297 298 #define ALL (E_OPEN|E_CREAT|E_EXEC) 299 300 STATIC const struct errname errormsg[] = { 301 { EINTR, ALL, "interrupted" }, 302 { EACCES, ALL, "permission denied" }, 303 { EIO, ALL, "I/O error" }, 304 { EEXIST, ALL, "file exists" }, 305 { ENOENT, E_OPEN, "no such file" }, 306 { ENOENT, E_CREAT,"directory nonexistent" }, 307 { ENOENT, E_EXEC, "not found" }, 308 { ENOTDIR, E_OPEN, "no such file" }, 309 { ENOTDIR, E_CREAT,"directory nonexistent" }, 310 { ENOTDIR, E_EXEC, "not found" }, 311 { EISDIR, ALL, "is a directory" }, 312 #ifdef EMFILE 313 { EMFILE, ALL, "too many open files" }, 314 #endif 315 { ENFILE, ALL, "file table overflow" }, 316 { ENOSPC, ALL, "file system full" }, 317 #ifdef EDQUOT 318 { EDQUOT, ALL, "disk quota exceeded" }, 319 #endif 320 #ifdef ENOSR 321 { ENOSR, ALL, "no streams resources" }, 322 #endif 323 { ENXIO, ALL, "no such device or address" }, 324 { EROFS, ALL, "read-only file system" }, 325 { ETXTBSY, ALL, "text busy" }, 326 #ifdef EAGAIN 327 { EAGAIN, E_EXEC, "not enough memory" }, 328 #endif 329 { ENOMEM, ALL, "not enough memory" }, 330 #ifdef ENOLINK 331 { ENOLINK, ALL, "remote access failed" }, 332 #endif 333 #ifdef EMULTIHOP 334 { EMULTIHOP, ALL, "remote access failed" }, 335 #endif 336 #ifdef ECOMM 337 { ECOMM, ALL, "remote access failed" }, 338 #endif 339 #ifdef ESTALE 340 { ESTALE, ALL, "remote access failed" }, 341 #endif 342 #ifdef ETIMEDOUT 343 { ETIMEDOUT, ALL, "remote access failed" }, 344 #endif 345 #ifdef ELOOP 346 { ELOOP, ALL, "symbolic link loop" }, 347 #endif 348 #ifdef ENAMETOOLONG 349 { ENAMETOOLONG, ALL, "file name too long" }, 350 #endif 351 { E2BIG, E_EXEC, "argument list too long" }, 352 #ifdef ELIBACC 353 { ELIBACC, E_EXEC, "shared library missing" }, 354 #endif 355 { 0, 0, NULL }, 356 }; 357 358 359 /* 360 * Return a string describing an error. The returned string may be a 361 * pointer to a static buffer that will be overwritten on the next call. 362 * Action describes the operation that got the error. 363 */ 364 365 const char * 366 errmsg(int e, int action) 367 { 368 struct errname const *ep; 369 static char buf[12]; 370 371 for (ep = errormsg ; ep->errcode ; ep++) { 372 if (ep->errcode == e && (ep->action & action) != 0) 373 return ep->msg; 374 } 375 fmtstr(buf, sizeof buf, "error %d", e); 376 return buf; 377 } 378