From: g0dil Date: Tue, 14 Nov 2006 13:21:11 +0000 (+0000) Subject: Add very simple daemon helpers X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=25027a3bde8be9313e1f4ca80aa903e2da47266a;p=senf.git Add very simple daemon helpers git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@156 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Utils/DaemonTools.cc b/Utils/DaemonTools.cc new file mode 100644 index 0000000..0dc3f0b --- /dev/null +++ b/Utils/DaemonTools.cc @@ -0,0 +1,51 @@ +// $Id$ +// +// Copyright (C) 2006 + +// Definition of non-inline non-template functions + +#include "DaemonTools.hh" +//#include "DaemonTools.ih" + +// Custom includes +#include +#include +#include +#include +#include + +#include "Exception.hh" + +//#include "DaemonTools.mpp" +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +prefix_ void satcom::lib::daemonize() +{ + int pid = fork(); + if (pid < 0) + throw satcom::lib::SystemException("fork",errno); + if (pid > 0) + ::_exit(0); + if (::setsid() < 0) + throw satcom::lib::SystemException("setsid",errno); +} + +prefix_ void satcom::lib::redirect_stdio(std::string const & path) +{ + int fd = ::open(path.c_str(),O_RDWR); + if (fd < 0) throw satcom::lib::SystemException("open",errno); + if (dup2(fd,0) < 0) throw satcom::lib::SystemException("dup2",errno); + if (dup2(fd,1) < 0) throw satcom::lib::SystemException("dup2",errno); + if (dup2(fd,2) < 0) throw satcom::lib::SystemException("dup2",errno); + if (::close(fd) < 0) throw satcom::lib::SystemException("close",errno); +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ +//#include "DaemonTools.mpp" + + +// Local Variables: +// mode: c++ +// End: diff --git a/Utils/DaemonTools.hh b/Utils/DaemonTools.hh new file mode 100644 index 0000000..e96ade6 --- /dev/null +++ b/Utils/DaemonTools.hh @@ -0,0 +1,32 @@ +// $Id$ +// +// Copyright (C) 2006 + +#ifndef HH_DaemonTools_ +#define HH_DaemonTools_ 1 + +// Custom includes +#include + +//#include "DaemonTools.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace satcom { +namespace lib { + + void daemonize(); + void redirect_stdio(std::string const & path = "/dev/null"); + +}} + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "DaemonTools.cci" +//#include "DaemonTools.ct" +//#include "DaemonTools.cti" +//#include "DaemonTools.mpp" +#endif + + +// Local Variables: +// mode: c++ +// End: