X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Utils%2FDaemon%2FDaemon.cc;h=2cba370fd9c18d9f8607233c9b8d380f2818e81f;hb=63b650afb727cce067817ce9451eecb932446bf3;hp=34f476a1b21f397885787b01a87c4abfe95a6b20;hpb=9e69297ff7a7bcd9d952a38e35bbf124cb7f1dda;p=senf.git diff --git a/Utils/Daemon/Daemon.cc b/Utils/Daemon/Daemon.cc index 34f476a..2cba370 100644 --- a/Utils/Daemon/Daemon.cc +++ b/Utils/Daemon/Daemon.cc @@ -1,8 +1,8 @@ // $Id$ // // Copyright (C) 2007 -// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) -// Kompetenzzentrum fuer NETwork research (NET) +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY // Stefan Bund // // This program is free software; you can redistribute it and/or modify @@ -110,7 +110,7 @@ namespace { prefix_ void senf::Daemon::detach() { - if (daemonize_) { + if (daemonize_ && ! detached_) { // Wow .. ouch .. // To ensure all data is written to the console log file in the correct order, we suspend // execution here until the parent process tells us to continue via SIGUSR1: We block @@ -144,20 +144,30 @@ prefix_ void senf::Daemon::detach() LIBC_CALL( ::sigaction, (SIGUSR1, &oldact, 0) ); LIBC_CALL( ::sigprocmask, (SIG_SETMASK, &oldsig, 0) ); + + detached_ = true; } } +namespace { + /* Purposely *not* derived from std::exception */ + struct DaemonExitException { + DaemonExitException(unsigned c) : code(c) {} + unsigned code; + }; +} + +prefix_ void senf::Daemon::exit(unsigned code) +{ + throw DaemonExitException(code); +} + prefix_ int senf::Daemon::start(int argc, char const ** argv) { argc_ = argc; argv_ = argv; -# ifdef NDEBUG - try { - -# endif - configure(); if (daemonize_) { @@ -171,10 +181,13 @@ prefix_ int senf::Daemon::start(int argc, char const ** argv) } main(); + } + catch (DaemonExitException & e) { + return e.code; + } -# ifdef NDEBUG +#ifdef NDEBUG - } catch (std::exception & e) { std::cerr << "\n*** Fatal exception: " << e.what() << std::endl; return 1; @@ -210,7 +223,8 @@ prefix_ void senf::Daemon::configure() std::string::size_type komma (arg.find(',')); if (komma == std::string::npos) { boost::trim(arg); - consoleLog(arg); + if (arg == std::string("none")) consoleLog(""); + else if (!arg.empty()) consoleLog(arg); } else { std::string arg1 (arg,0,komma); std::string arg2 (arg,komma+1);