X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Utils%2FDaemon%2FDaemon.cc;h=152186768576eec4327ae0403a2d17d48fdb47b4;hb=a66bffcc77f006b22190e1c27cbe0cd75fc4501c;hp=d008c384c060c4cfd1c7b206013e89c7fa0b4fe4;hpb=b41b8147c7a2d40e2f69471e183840be8a0b95da;p=senf.git diff --git a/Utils/Daemon/Daemon.cc b/Utils/Daemon/Daemon.cc index d008c38..1521867 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) +// Copyright (C) 2007 +// 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 @@ -38,6 +38,7 @@ #include #include #include +#include #include "../Exception.hh" #include "../membind.hh" @@ -45,16 +46,22 @@ #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// -#define LIBC_CALL(fn, args) if (fn args < 0) throwErrno(#fn "()") -#define LIBC_CALL_RV(var, fn, args) int var (fn args); if (var < 0) throwErrno(#fn "()") +#define LIBC_CALL(fn, args) if (fn args < 0) throw SystemException(#fn "()") +#define LIBC_CALL_RV(var, fn, args) int var (fn args); if (var < 0) throw SystemException(#fn "()") /////////////////////////////////////////////////////////////////////////// // senf::Daemon prefix_ senf::Daemon::~Daemon() { - if (! pidfile_.empty()) - LIBC_CALL( ::unlink, (pidfile_.c_str()) ); + if (! pidfile_.empty()) { + try { + LIBC_CALL( ::unlink, (pidfile_.c_str()) ); + } catch (Exception e) { + // e << "; could not unlink " << pidfile_.c_str(); + // throw; + } + } } prefix_ void senf::Daemon::daemonize(bool v) @@ -67,6 +74,14 @@ prefix_ bool senf::Daemon::daemon() return daemonize_; } +prefix_ int senf::Daemon::argc() { + return argc_; +} + +prefix_ char const ** senf::Daemon::argv() { + return argv_; +} + prefix_ void senf::Daemon::consoleLog(std::string const & path, StdStream which) { switch (which) { @@ -83,7 +98,7 @@ prefix_ void senf::Daemon::openLog() if (! stdoutLog_.empty()) { fd = ::open(stdoutLog_.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0666); if (fd < 0) - throwErrno("::open()"); + throw SystemException("::open()"); stdout_ = fd; } if (stderrLog_ == stdoutLog_) @@ -91,7 +106,7 @@ prefix_ void senf::Daemon::openLog() else if (! stderrLog_.empty()) { fd = ::open(stdoutLog_.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0666); if (fd < 0) - throwErrno("::open()"); + throw SystemException("::open()"); stderr_ = fd; } } @@ -139,7 +154,7 @@ prefix_ void senf::Daemon::detach() while (! signaled) { ::sigsuspend(&waitsig); if (errno != EINTR) - throwErrno("::sigsuspend()"); + throw SystemException("::sigsuspend()"); } LIBC_CALL( ::sigaction, (SIGUSR1, &oldact, 0) ); @@ -151,15 +166,15 @@ prefix_ void senf::Daemon::detach() namespace { /* Purposely *not* derived from std::exception */ - struct DaemonFailureException { - DaemonFailureException(unsigned c) : code(c) {} + struct DaemonExitException { + DaemonExitException(unsigned c) : code(c) {} unsigned code; }; } -prefix_ void senf::Daemon::fail(unsigned code) +prefix_ void senf::Daemon::exit(unsigned code) { - throw DaemonFailureException(code); + throw DaemonExitException(code); } prefix_ int senf::Daemon::start(int argc, char const ** argv) @@ -182,11 +197,11 @@ prefix_ int senf::Daemon::start(int argc, char const ** argv) main(); } - catch (DaemonFailureException & e) { - return e.code > 0 ? e.code : 1; + catch (DaemonExitException & e) { + return e.code; } -#ifdef NDEBUG +#ifndef SENF_DEBUG catch (std::exception & e) { std::cerr << "\n*** Fatal exception: " << e.what() << std::endl; @@ -223,7 +238,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); @@ -307,6 +323,7 @@ prefix_ bool senf::Daemon::pidfileCreate() // was some race condition, probably over NFS. std::string tempname; + boost::format linkErrorFormat("; could not link \"%1%\" to \"%2%\"."); { char hostname[HOST_NAME_MAX+1]; @@ -325,7 +342,8 @@ prefix_ bool senf::Daemon::pidfileCreate() if (::link(tempname.c_str(), pidfile_.c_str()) < 0) { if (errno != EEXIST) - throwErrno("::link()"); + throw SystemException("::link()") + << linkErrorFormat % pidfile_.c_str() % tempname.c_str(); } else { struct ::stat s; @@ -355,7 +373,9 @@ prefix_ bool senf::Daemon::pidfileCreate() LIBC_CALL( ::unlink, (tempname.c_str() )); if (::link(pidfile_.c_str(), tempname.c_str()) < 0) { - if (errno != ENOENT) throwErrno("::link()"); + if (errno != ENOENT) + throw SystemException("::link()") + << linkErrorFormat % tempname.c_str() % pidfile_.c_str(); // Hmm ... the pidfile mysteriously disappeared ... try again. continue; } @@ -418,7 +438,7 @@ prefix_ void senf::detail::DaemonWatcher::pipeClosed(int id) if (sigChld_) childDied(); // does not return if (::kill(childPid_, SIGUSR1) < 0) - if (errno != ESRCH) throwErrno("::kill()"); + if (errno != ESRCH) throw SystemException("::kill()"); Scheduler::instance().timeout( Scheduler::instance().eventTime() + ClockService::seconds(1), senf::membind(&DaemonWatcher::childOk, this)); @@ -435,7 +455,7 @@ prefix_ void senf::detail::DaemonWatcher::sigChld() prefix_ void senf::detail::DaemonWatcher::childDied() { int status (0); - if (::waitpid(childPid_,&status,0) < 0) throwErrno("::waitpid()"); + if (::waitpid(childPid_,&status,0) < 0) throw SystemException("::waitpid()"); if (WIFSIGNALED(status)) { ::signal(WTERMSIG(status),SIG_DFL); ::kill(::getpid(), WTERMSIG(status)); @@ -486,7 +506,7 @@ prefix_ void senf::detail::DaemonWatcher::Forwarder::readData(Scheduler::EventId while (1) { n = ::read(src_,buf,1024); if (n<0) { - if (errno != EINTR) throwErrno("::read()"); + if (errno != EINTR) throw SystemException("::read()"); } else break; } @@ -530,7 +550,7 @@ prefix_ void senf::detail::DaemonWatcher::Forwarder::writeData(Scheduler::EventI int w (::write(target->fd, buf, n)); if (w < 0) { - if (errno != EINTR) throwErrno("::write()"); + if (errno != EINTR) throw SystemException("::write()"); return; } target->offset += w;