4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief Daemon non-inline non-template implementation */
35 #include <sys/types.h>
48 #include <boost/algorithm/string/predicate.hpp>
49 #include <boost/algorithm/string/trim.hpp>
50 #include <boost/format.hpp>
51 #include <senf/Utils/Exception.hh>
52 #include <senf/Utils/membind.hh>
53 #include <senf/Utils/Backtrace.hh>
54 #include <senf/Utils/signalnames.hh>
59 //#include "Daemon.mpp"
61 //-/////////////////////////////////////////////////////////////////////////////////////////////////
63 #define LIBC_CALL(fn, args) if (fn args < 0) \
64 SENF_THROW_SYSTEM_EXCEPTION(#fn "()")
66 #define LIBC_CALL_RV(var, fn, args) \
67 int var (fn args); if (var < 0) SENF_THROW_SYSTEM_EXCEPTION(#fn "()")
69 //-/////////////////////////////////////////////////////////////////////////////////////////////////
72 prefix_ senf::Daemon::~Daemon()
74 if (pidfileCreated_) {
76 LIBC_CALL( ::unlink, (pidfile_.c_str()) );
77 } catch (Exception & e) {
78 // e << "; could not unlink " << pidfile_.c_str();
84 prefix_ void senf::Daemon::daemonize(bool v)
89 prefix_ bool senf::Daemon::daemon()
94 prefix_ int senf::Daemon::argc()
99 prefix_ char const ** senf::Daemon::argv()
107 bool operator()(std::string const & str) const {
108 return str == "--no-daemon"
109 || boost::starts_with(str, std::string("--pid-file="))
110 || boost::starts_with(str, std::string("--console-log="));
115 prefix_ void senf::Daemon::removeDaemonArgs()
117 char const ** last (std::remove_if(argv_+1, argv_+argc_, IsDaemonOpt()));
119 argc_ = last - argv_;
122 prefix_ void senf::Daemon::consoleLog(std::string const & path, StdStream which)
125 case StdOut : stdoutLog_ = path; break;
126 case StdErr : stderrLog_ = path; break;
127 case Both : stdoutLog_ = path; stderrLog_ = path; break;
132 prefix_ void senf::Daemon::openLog()
135 if (! stdoutLog_.empty()) {
136 fd = ::open(stdoutLog_.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0666);
138 SENF_THROW_SYSTEM_EXCEPTION("")
139 << " Could not open \"" << stdoutLog_ << "\" for redirecting stdout.";
142 if (! stderrLog_.empty()) {
143 if (stderrLog_ == stdoutLog_) {
146 SENF_THROW_SYSTEM_EXCEPTION("::dup()");
149 fd = ::open(stdoutLog_.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0666);
151 SENF_THROW_SYSTEM_EXCEPTION("")
152 << " Could not open \"" << stderrLog_ << "\" for redirecting stderr.";
158 prefix_ void senf::Daemon::logReopen()
160 if (! stdoutLog_.empty()) {
161 int fd (::open(stdoutLog_.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0666));
164 if (::dup2(fd, 1) < 0)
166 if (stderrLog_ == stdoutLog_) {
167 if (::dup2(fd, 2) < 0)
172 if (! stderrLog_.empty()) {
173 int fd (::open(stderrLog_.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0666));
176 if (::dup2(fd, 2) < 0)
183 (senf::log::CRITICAL)
184 ("log-file reopen failed: (" << errno << ") " << ::strerror(errno)) );
187 prefix_ void senf::Daemon::pidFile(std::string const & f)
193 bool signaled (false);
199 prefix_ void senf::Daemon::detach()
201 if (daemonize_ && ! detached_) {
203 // To ensure all data is written to the console log file in the correct order, we suspend
204 // execution here until the parent process tells us to continue via SIGUSR1: We block
205 // SIGUSR1 and install our own signal handler saving the old handler and signal mask. Then
206 // we close stdin/stderr which will send a HUP condition to the parent process. We wait for
207 // SIGUSR1 and reinstall the old signal mask and action.
210 ::sigemptyset(&usrsig);
211 LIBC_CALL( ::sigaddset, (&usrsig, SIGUSR1) );
212 LIBC_CALL( ::sigprocmask, (SIG_BLOCK, &usrsig, &oldsig) );
213 struct ::sigaction oldact;
214 struct ::sigaction usract;
215 ::memset(&usract, 0, sizeof(usract));
216 usract.sa_handler = &waitusr;
217 LIBC_CALL( ::sigaction, (SIGUSR1, &usract, &oldact) );
218 ::sigset_t waitsig (oldsig);
219 LIBC_CALL( ::sigdelset, (&waitsig, SIGUSR1) );
221 LIBC_CALL_RV( nul, ::open, ("/dev/null", O_WRONLY) );
222 LIBC_CALL( ::dup2, (stdout_ == -1 ? nul : stdout_, 1) );
223 LIBC_CALL( ::dup2, (stderr_ == -1 ? nul : stderr_, 2) );
224 LIBC_CALL( ::close, (nul) );
228 ::sigsuspend(&waitsig);
230 SENF_THROW_SYSTEM_EXCEPTION("::sigsuspend()");
233 LIBC_CALL( ::sigaction, (SIGUSR1, &oldact, 0) );
234 LIBC_CALL( ::sigprocmask, (SIG_SETMASK, &oldsig, 0) );
241 /* Purposely *not* derived from std::exception */
242 struct DaemonExitException {
243 DaemonExitException(unsigned c) : code(c) {}
248 prefix_ void senf::Daemon::exit(unsigned code)
250 throw DaemonExitException(code);
253 prefix_ int senf::Daemon::start(int argc, char const ** argv)
265 installSighandlers();
266 if (! pidfile_.empty()) {
268 pidfileCreated_ = true;
270 std::cerr << "PID file '" << pidfile_
271 << "' creation failed. Daemon running ?" << std::endl;
278 catch (DaemonExitException & e) {
284 catch (std::exception & e) {
285 std::cerr << "\n*** Fatal exception: " << e.what() << "\n" << std::endl;
289 std::cerr << "\n*** Fatal exception: (unknown)" << "\n" << std::endl;
298 prefix_ senf::Daemon & senf::Daemon::instance()
300 BOOST_ASSERT( instance_ );
304 //-/////////////////////////////////////////////////////////////////////////////////////////////////
307 prefix_ senf::Daemon::Daemon()
308 : argc_(0), argv_(0), daemonize_(true), stdout_(-1), stderr_(-1), pidfile_(""),
309 pidfileCreated_(false), detached_(false)
311 BOOST_ASSERT( ! instance_ );
315 senf::Daemon * senf::Daemon::instance_ (0);
317 //-/////////////////////////////////////////////////////////////////////////////////////////////////
320 prefix_ void senf::Daemon::configure()
322 // int i (not unsigned) since argc_ is int ...
323 for (int i (1); i<argc_; ++i) {
324 std::string argv (argv_[i]);
325 if (argv == "--no-daemon")
327 else if (boost::starts_with(argv, "--console-log=")) {
328 std::string arg (argv.substr(14u));
329 std::string::size_type komma (arg.find(','));
330 if (komma == std::string::npos) {
332 if (arg == std::string("none")) consoleLog("");
333 else if (!arg.empty()) consoleLog(arg);
335 std::string arg1 (arg,0,komma);
336 std::string arg2 (arg,komma+1);
339 if (arg1 == std::string("none")) consoleLog("",StdOut);
340 else if (! arg1.empty() ) consoleLog(arg1, StdOut);
341 if (arg2 == std::string("none")) consoleLog("",StdErr);
342 else if (! arg2.empty() ) consoleLog(arg2, StdErr);
345 else if (boost::starts_with(argv, "--pid-file="))
346 pidFile(std::string(std::string(argv_[i]), 11u));
350 prefix_ void senf::Daemon::main()
357 prefix_ void senf::Daemon::init()
360 prefix_ void senf::Daemon::run()
363 prefix_ void senf::Daemon::fork()
368 LIBC_CALL_RV( nul, ::open, ("/dev/null", O_RDONLY) );
369 LIBC_CALL( ::dup2, (nul, 0) );
370 LIBC_CALL( ::close, (nul) );
371 LIBC_CALL( ::pipe, (coutpipe) );
372 LIBC_CALL( ::pipe, (cerrpipe) );
374 // We need to block the SIGCHLD signal here so we don't miss it, if the child
378 ::sigemptyset(&cldsig);
379 LIBC_CALL( ::sigaddset, (&cldsig, SIGCHLD) );
380 LIBC_CALL( ::sigprocmask, (SIG_BLOCK, &cldsig, &oldsig) );
382 if (! senf::scheduler::empty() ) {
386 "Scheduler not empty before fork(). THIS MUST NOT HAPPEN.\n"
387 "The scheduler will be reinitialized by the fork() and lose all registrations.\n\n";
388 senf::scheduler::detail::EventManager::instance().listEvents(std::cerr);
390 "\n*** WARNING ***\n"
394 LIBC_CALL_RV( pid, ::fork, () );
399 LIBC_CALL( ::dup2, (coutpipe[1],1) );
400 LIBC_CALL( ::dup2, (cerrpipe[1],2) );
401 LIBC_CALL( ::close, (coutpipe[0]) );
402 LIBC_CALL( ::close, (coutpipe[1]) );
403 LIBC_CALL( ::close, (cerrpipe[0]) );
404 LIBC_CALL( ::close, (cerrpipe[1]) );
405 LIBC_CALL( ::setsid, () );
406 LIBC_CALL( ::sigprocmask, (SIG_SETMASK, &oldsig, 0) );
408 senf::scheduler::restart();
412 // Ouch ... ensure, the daemon watcher does not remove the pidfile ...
415 LIBC_CALL( ::close, (coutpipe[1]) );
416 LIBC_CALL( ::close, (cerrpipe[1]) );
418 senf::scheduler::restart();
420 detail::DaemonWatcher watcher (pid, coutpipe[0], cerrpipe[0], stdout_, stderr_);
426 prefix_ bool senf::Daemon::pidfileCreate()
428 // Create temporary file pidfile_.hostname.pid and hard-link it to pidfile_ If the hardlink
429 // fails, the pidfile exists. If the link count of the temporary file is not 2 after this, there
430 // was some race condition, probably over NFS.
432 std::string tempname;
433 boost::format linkErrorFormat(" Could not link \"%1%\" to \"%2%\".");
436 char hostname[HOST_NAME_MAX+1];
437 LIBC_CALL( ::gethostname, (hostname, HOST_NAME_MAX+1) );
438 hostname[HOST_NAME_MAX] = 0;
439 std::stringstream tempname_s;
440 tempname_s << pidfile_ << "." << hostname << "." << ::getpid();
441 tempname = tempname_s.str();
446 std::ofstream pidf (tempname.c_str());
448 SENF_THROW_SYSTEM_EXCEPTION("")
449 << " Could not open pidfile \"" << tempname << "\" for output.";
450 pidf << ::getpid() << std::endl;
452 SENF_THROW_SYSTEM_EXCEPTION("")
453 << " Could not write to pidfile \"" << tempname << "\".";
456 if (::link(tempname.c_str(), pidfile_.c_str()) < 0) {
458 SENF_THROW_SYSTEM_EXCEPTION("") << linkErrorFormat % pidfile_ % tempname;
462 LIBC_CALL( ::stat, (tempname.c_str(), &s) );
463 LIBC_CALL( ::unlink, (tempname.c_str()) );
464 return s.st_nlink == 2;
467 // pidfile exists. Check, whether the pid in the pidfile still exists.
470 std::ifstream pidf (pidfile_.c_str());
471 if ( ! (pidf >> old_pid)
473 || ::kill(old_pid, 0) >= 0
474 || errno == EPERM ) {
475 LIBC_CALL( ::unlink, (tempname.c_str()) );
480 // If we reach this point, the pid file exists but the process mentioned within the
481 // pid file does *not* exists. We assume, the pid file to be stale.
483 // I hope, the following procedure is without race condition: We remove our generated
484 // temporary pid file and recreate it as hard-link to the old pid file. Now we check, that
485 // the hard-link count of this file is 2. If it is not, we terminate, since someone else
486 // must have already created his hardlink. We then truncate the file and write our pid.
488 LIBC_CALL( ::unlink, (tempname.c_str() ));
489 if (::link(pidfile_.c_str(), tempname.c_str()) < 0) {
491 SENF_THROW_SYSTEM_EXCEPTION("") << linkErrorFormat % tempname % pidfile_;
492 // Hmm ... the pidfile mysteriously disappeared ... try again.
498 LIBC_CALL( ::stat, (tempname.c_str(), &s) );
499 if (s.st_nlink != 2) {
500 LIBC_CALL( ::unlink, (tempname.c_str()) );
506 std::ofstream pidf (tempname.c_str());
507 pidf << ::getpid() << std::endl;
510 LIBC_CALL( ::unlink, (tempname.c_str()) );
520 void fatalSignalsHandler(int sig, ::siginfo_t * info, void * arg)
522 // ::ucontext_t * ucontext = static_cast<ucontext_t*>(arg);
523 std::cerr << "\n" << "Signal " << senf::signalName(sig) << '(' << sig << ')'
527 std::cerr << "Invalid memory access at " << info->si_addr << "\n";
528 #ifdef SENF_BACKTRACE
529 static void * entries[SENF_DEBUG_BACKTRACE_NUMCALLERS];
530 int nEntries( ::backtrace(entries, SENF_DEBUG_BACKTRACE_NUMCALLERS) );
532 // Hack the callers address into the backtrace
533 // entries[1] = reinterpret_cast<void *>(ucontext->uc_mcontext.gregs[REG_EIP]);
535 std::cerr << "Backtrace:\n";
536 senf::formatBacktrace(std::cerr, entries, nEntries);
537 std::cerr << "-- \n";
538 #endif //SENF_BACKTRACE
539 if (sig != SIGUSR2) {
540 ::signal(sig, SIG_DFL);
541 ::kill(::getpid(), sig);
550 void sighupHandler(int sig)
552 senf::Daemon::instance().logReopen();
556 prefix_ void senf::Daemon::installSighandlers()
558 struct ::sigaction sa;
560 ::sigemptyset(&sa.sa_mask);
561 sa.sa_handler = &sighupHandler;
562 sa.sa_flags = SA_RESTART;
564 ::sigaction(SIGHUP, &sa, NULL);
566 sa.sa_handler = SIG_IGN;
567 ::sigaction(SIGPIPE, &sa, NULL);
570 sa.sa_sigaction = &fatalSignalsHandler;
571 sa.sa_flags = SA_RESTART | SA_SIGINFO;
573 ::sigaction(SIGILL, &sa, NULL);
574 ::sigaction(SIGTRAP, &sa, NULL);
575 ::sigaction(SIGABRT, &sa, NULL);
576 ::sigaction(SIGFPE, &sa, NULL);
577 ::sigaction(SIGBUS, &sa, NULL);
578 ::sigaction(SIGSEGV, &sa, NULL);
579 #ifdef SIGSTKFLT //SIGSTKFLT is used for stack faults on coprocessors. That condition doesn't exist on MIPS
580 ::sigaction(SIGSTKFLT, &sa, NULL);
582 ::sigaction(SIGSYS, &sa, NULL);
583 ::sigaction(SIGUSR2, &sa, NULL);
587 //-/////////////////////////////////////////////////////////////////////////////////////////////////
588 // senf::detail::DaemonWatcher
590 prefix_ senf::detail::DaemonWatcher::DaemonWatcher(int pid, int coutpipe, int cerrpipe,
591 int stdout, int stderr)
592 : childPid_(pid), coutpipe_(coutpipe), cerrpipe_(cerrpipe), stdout_(stdout),
593 stderr_(stderr), sigChld_(false),
594 cldSignal_ (SIGCHLD, senf::membind(&DaemonWatcher::sigChld, this)),
595 timer_ ("senf::detail::DaemonWatcher::childOk", senf::membind(&DaemonWatcher::childOk, this)),
596 coutForwarder_(coutpipe_, boost::bind(&DaemonWatcher::pipeClosed, this, 1)),
597 cerrForwarder_(cerrpipe_, boost::bind(&DaemonWatcher::pipeClosed, this, 2))
599 coutForwarder_.addTarget(1);
601 coutForwarder_.addTarget(stdout_);
602 cerrForwarder_.addTarget(2);
604 cerrForwarder_.addTarget(stderr_);
607 prefix_ void senf::detail::DaemonWatcher::run()
609 scheduler::process();
612 //-/////////////////////////////////////////////////////////////////////////////////////////////////
615 prefix_ void senf::detail::DaemonWatcher::pipeClosed(int id)
618 case 1 : coutpipe_ = -1; break;
619 case 2 : cerrpipe_ = -1; break;
622 if (coutpipe_ == -1 && cerrpipe_ == -1) {
624 childDied(); // does not return
625 if (::kill(childPid_, SIGUSR1) < 0 && errno != ESRCH)
626 SENF_THROW_SYSTEM_EXCEPTION("::kill()");
627 timer_.timeout(scheduler::eventTime() + ClockService::seconds(1));
631 prefix_ void senf::detail::DaemonWatcher::sigChld(siginfo_t const &)
634 if (coutpipe_ == -1 && cerrpipe_ == -1)
635 childDied(); // does not return
638 prefix_ void senf::detail::DaemonWatcher::childDied()
641 if (::waitpid(childPid_,&status,0) < 0) SENF_THROW_SYSTEM_EXCEPTION("::waitpid()");
642 if (WIFSIGNALED(status)) {
643 ::signal(WTERMSIG(status),SIG_DFL);
644 ::kill(::getpid(), WTERMSIG(status));
645 // should not be reached
648 if (WEXITSTATUS(status) == 0)
650 ::_exit(WEXITSTATUS(status));
653 prefix_ void senf::detail::DaemonWatcher::childOk()
655 scheduler::terminate();
658 //-/////////////////////////////////////////////////////////////////////////////////////////////////
659 // senf::detail::DaemonWatcher::Forwarder
661 prefix_ senf::detail::DaemonWatcher::Forwarder::Forwarder(int src, Callback cb)
662 : src_(src), cb_(cb),
663 readevent_("senf::detail::DaemonWatcher::Forwarder::readevent", senf::membind(&Forwarder::readData, this),
664 src_, scheduler::FdEvent::EV_READ)
667 prefix_ senf::detail::DaemonWatcher::Forwarder::~Forwarder()
669 targets_.clear_and_destroy(DestroyDelete());
672 prefix_ void senf::detail::DaemonWatcher::Forwarder::addTarget(int fd)
674 targets_.push_back(*(new Target(*this, fd)));
677 prefix_ void senf::detail::DaemonWatcher::Forwarder::readData(int event)
683 n = ::read(src_,buf,1024);
686 SENF_THROW_SYSTEM_EXCEPTION("::read()");
696 readevent_.disable();
700 if (targets_.empty())
703 for (Targets::iterator i (targets_.begin()); i != targets_.end(); ++i)
704 if (i->offset >= buffer_.size())
705 i->writeevent.enable();
707 buffer_.insert(buffer_.end(), buf, buf+n);
710 prefix_ void senf::detail::DaemonWatcher::Forwarder::writeData(int event, Target * target)
712 if (event != scheduler::FdEvent::EV_WRITE) {
713 // Broken pipe while writing data ? Not much, we can do here, we just drop the data
714 targets_.erase_and_destroy(Targets::current(*target),DestroyDelete());
715 if (targets_.empty() && src_ == -1)
721 int n (buffer_.size() - target->offset > 1024 ? 1024 : buffer_.size() - target->offset);
722 std::copy(buffer_.begin() + target->offset, buffer_.begin() + target->offset + n, buf);
724 int w (::write(target->fd, buf, n));
726 if (errno != EINTR) SENF_THROW_SYSTEM_EXCEPTION("::write()");
731 n = std::min_element(
732 targets_.begin(), targets_.end(),
733 boost::bind(&Target::offset, _1) < boost::bind(&Target::offset, _2))->offset;
735 buffer_.erase(buffer_.begin(), buffer_.begin()+n);
737 for (Targets::iterator i (targets_.begin()); i != targets_.end(); ++i)
740 if (target->offset >= buffer_.size())
741 target->writeevent.disable();
742 if (src_ == -1 && (buffer_.empty() || targets_.empty()))
749 //-/////////////////////////////////////////////////////////////////////////////////////////////////
751 //#include "Daemon.mpp"
757 // comment-column: 40
758 // c-file-style: "senf"
759 // indent-tabs-mode: nil
760 // ispell-local-dictionary: "american"
761 // compile-command: "scons -u test"