Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
| 11 | |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 12 | int die_sleep; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 13 | #if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_HUSH |
Denis Vlasenko | 3f3aa2a | 2007-04-09 21:35:07 +0000 | [diff] [blame] | 14 | jmp_buf die_jmp; |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 15 | #endif |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 17 | void xfunc_die(void) |
Denis Vlasenko | 9b1381f | 2007-01-03 02:56:00 +0000 | [diff] [blame] | 18 | { |
Denis Vlasenko | 3f3aa2a | 2007-04-09 21:35:07 +0000 | [diff] [blame] | 19 | if (die_sleep) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 20 | if ((ENABLE_FEATURE_PREFER_APPLETS || ENABLE_HUSH) |
| 21 | && die_sleep < 0 |
| 22 | ) { |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 23 | /* Special case. We arrive here if NOFORK applet |
| 24 | * calls xfunc, which then decides to die. |
| 25 | * We don't die, but jump instead back to caller. |
| 26 | * NOFORK applets still cannot carelessly call xfuncs: |
| 27 | * p = xmalloc(10); |
| 28 | * q = xmalloc(10); // BUG! if this dies, we leak p! |
| 29 | */ |
Denis Vlasenko | b055001 | 2007-05-24 12:18:16 +0000 | [diff] [blame] | 30 | /* -2222 means "zero" (longjmp can't pass 0) |
| 31 | * run_nofork_applet() catches -2222. */ |
| 32 | longjmp(die_jmp, xfunc_error_retval ? xfunc_error_retval : -2222); |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 33 | } |
Denis Vlasenko | 9b1381f | 2007-01-03 02:56:00 +0000 | [diff] [blame] | 34 | sleep(die_sleep); |
Denis Vlasenko | 3f3aa2a | 2007-04-09 21:35:07 +0000 | [diff] [blame] | 35 | } |
Denis Vlasenko | 9b1381f | 2007-01-03 02:56:00 +0000 | [diff] [blame] | 36 | exit(xfunc_error_retval); |
| 37 | } |
| 38 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 39 | void bb_error_msg_and_die(const char *s, ...) |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 40 | { |
| 41 | va_list p; |
| 42 | |
| 43 | va_start(p, s); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 44 | bb_verror_msg(s, p, NULL); |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 45 | va_end(p); |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 46 | xfunc_die(); |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 47 | } |