1 // include/Daemon.hh
2 // This file is part of libpbe; see http://decimail.org
3 // (C) 2004-2007 Philip Endecott
4 
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef libpbe_Daemon_hh
20 #define libpbe_Daemon_hh
21 
22 #include <syslog.h>
23 #include <string>
24 
25 #include "FileDescriptor.hh"
26 #include "Mutex.hh"
27 #include "Condition.hh"
28 
29 // TODO:
30 // Sending EOF doesn't do anything (sigpipe?)
31 // Worry about other signals
32 // Spurious thread on first connection (is it syslog???)
33 
34 
35 class Daemon {
36 public:
37   static const int default_max_sessions = 25;
38 
39   Daemon(short p,
40 	 std::string pn,
41 	 int sf = LOG_LOCAL0,
42 	 std::string u="",
43 	 std::string d="",
44          int max_sessions_=0,
45          bool accept_local_only_=false);
46   virtual ~Daemon();
47 
48   void run_interactively(void);
49   void run_as_daemon(bool background = true);
50   void run_default(void);
51 
startup(void)52   virtual void startup(void) {};
53   virtual void session(pbe::FileDescriptor& in_fd, pbe::FileDescriptor& out_fd) = 0;
54 
55 private:
56   const short port;
57   const std::string progname;
58   const int syslog_facility;
59   const std::string username;
60   const std::string dir;
61   const int max_sessions;
62   const bool accept_local_only;
63 
64   typedef pbe::Mutex<> n_sessions_mutex_t;
65   n_sessions_mutex_t n_sessions_mutex;
66   typedef pbe::Condition n_sessions_condition_t;
67   n_sessions_condition_t n_sessions_condition;
68   int n_sessions;
69 
70 public: // really private
71   void decrement_session_count(void);
72 };
73 
74 
75 #endif
76