00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 #ifndef IH_SENF_Utils_Daemon_Daemon_
00027 #define IH_SENF_Utils_Daemon_Daemon_ 1
00028
00029
00030 #include <deque>
00031 #include <senf/boost_intrusive/iset.hpp>
00032 #include <senf/boost_intrusive/iset_hook.hpp>
00033 #include <boost/utility.hpp>
00034 #include <boost/function.hpp>
00035 #include <senf/Scheduler/Scheduler.hh>
00036
00037
00038
00039 namespace senf {
00040 namespace detail {
00041
00043 class DaemonWatcher
00044 : boost::noncopyable
00045 {
00046 public:
00047
00048 DaemonWatcher(int pid, int coutpipe, int cerrpipe, int stdout, int stderr);
00049
00050 void run();
00051
00052 private:
00053
00054 class Forwarder
00055 {
00056 public:
00057 typedef boost::function<void ()> Callback;
00058
00059 Forwarder(int src, Callback cb);
00060 ~Forwarder();
00061
00062 void addTarget(int fd);
00063
00064 private:
00065
00066
00067
00068
00069 typedef std::deque<char> Buffer;
00070 struct TargetListTag;
00071 typedef boost::intrusive::ilist_base_hook<TargetListTag> TargetListBase;
00072
00073 struct Target : public TargetListBase
00074 {
00075 Target(Forwarder & fwd, int fd);
00076
00077 int fd;
00078 Buffer::size_type offset;
00079 scheduler::FdEvent writeevent;
00080 };
00081 typedef boost::intrusive::ilist<TargetListBase::value_traits<Target>,false> Targets;
00082
00083 struct DestroyDelete
00084 {
00085 template <class T>
00086 void operator()(T * t) { delete t; }
00087 };
00088
00089 void readData(int event);
00090 void writeData(int event, Target * target);
00091
00092 Buffer buffer_;
00093 int src_;
00094 Targets targets_;
00095 Callback cb_;
00096 scheduler::FdEvent readevent_;
00097 };
00098
00099 void pipeClosed(int id);
00100 void sigChld(siginfo_t const &);
00101 void childDied();
00102 void childOk();
00103
00104 int childPid_;
00105 int coutpipe_;
00106 int cerrpipe_;
00107 int stdout_;
00108 int stderr_;
00109 bool sigChld_;
00110
00111 scheduler::SignalEvent cldSignal_;
00112 scheduler::TimerEvent timer_;
00113 Forwarder coutForwarder_;
00114 Forwarder cerrForwarder_;
00115 };
00116
00117 }}
00118
00119
00120 #endif
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131