blob: 4115046c26281bfc845cf1305fd81b96a81a9c71 [file] [log] [blame]
Eric Andersenb183dfa2001-03-19 19:24:06 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenb183dfa2001-03-19 19:24:06 +00006 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenb183dfa2001-03-19 19:24:06 +00008 */
9
Eric Andersenb183dfa2001-03-19 19:24:06 +000010#include "libbb.h"
11
Denis Vlasenkoa9801652006-09-07 16:20:03 +000012int die_sleep;
Denis Vlasenko80d14be2007-04-10 23:03:30 +000013#if ENABLE_FEATURE_PREFER_APPLETS
Denis Vlasenko3f3aa2a2007-04-09 21:35:07 +000014jmp_buf die_jmp;
Denis Vlasenko335b63d2007-04-10 21:38:30 +000015#endif
Denis Vlasenkoa9801652006-09-07 16:20:03 +000016
Denis Vlasenko335b63d2007-04-10 21:38:30 +000017void xfunc_die(void)
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000018{
Denis Vlasenko3f3aa2a2007-04-09 21:35:07 +000019 if (die_sleep) {
Denis Vlasenko80d14be2007-04-10 23:03:30 +000020 if (ENABLE_FEATURE_PREFER_APPLETS && die_sleep < 0) {
Denis Vlasenko335b63d2007-04-10 21:38:30 +000021 /* Special case. We arrive here if NOFORK applet
22 * calls xfunc, which then decides to die.
23 * We don't die, but jump instead back to caller.
24 * NOFORK applets still cannot carelessly call xfuncs:
25 * p = xmalloc(10);
26 * q = xmalloc(10); // BUG! if this dies, we leak p!
27 */
28 /* -111 means "zero" (longjmp can't pass 0)
29 * spawn_and_wait() catches -111. */
30 longjmp(die_jmp, xfunc_error_retval ? xfunc_error_retval : -111);
31 }
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000032 sleep(die_sleep);
Denis Vlasenko3f3aa2a2007-04-09 21:35:07 +000033 }
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000034 exit(xfunc_error_retval);
35}
36
Rob Landleydfba7412006-03-06 20:47:33 +000037void bb_error_msg_and_die(const char *s, ...)
Eric Andersenb183dfa2001-03-19 19:24:06 +000038{
39 va_list p;
40
41 va_start(p, s);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000042 bb_verror_msg(s, p, NULL);
Eric Andersenb183dfa2001-03-19 19:24:06 +000043 va_end(p);
Denis Vlasenko335b63d2007-04-10 21:38:30 +000044 xfunc_die();
Eric Andersenb183dfa2001-03-19 19:24:06 +000045}