blob: 0e99a03cfa96ce204a751f35fdf4cdd096f71669 [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 Vlasenko54e7ffb2007-04-21 00:03:36 +000013#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_HUSH
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 Vlasenko54e7ffb2007-04-21 00:03:36 +000020 if ((ENABLE_FEATURE_PREFER_APPLETS || ENABLE_HUSH)
21 && die_sleep < 0
22 ) {
Denis Vlasenko335b63d2007-04-10 21:38:30 +000023 /* 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 Vlasenkob0550012007-05-24 12:18:16 +000030 /* -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 Vlasenko335b63d2007-04-10 21:38:30 +000033 }
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000034 sleep(die_sleep);
Denis Vlasenko3f3aa2a2007-04-09 21:35:07 +000035 }
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000036 exit(xfunc_error_retval);
37}
38
Rob Landleydfba7412006-03-06 20:47:33 +000039void bb_error_msg_and_die(const char *s, ...)
Eric Andersenb183dfa2001-03-19 19:24:06 +000040{
41 va_list p;
42
43 va_start(p, s);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000044 bb_verror_msg(s, p, NULL);
Eric Andersenb183dfa2001-03-19 19:24:06 +000045 va_end(p);
Denis Vlasenko335b63d2007-04-10 21:38:30 +000046 xfunc_die();
Eric Andersenb183dfa2001-03-19 19:24:06 +000047}