OmniEvents
daemon_unix.h
Go to the documentation of this file.
1 // Package : omniEvents
2 // daemon_unix.h Created : 2004/06/26
3 // Author : Alex Tingle
4 //
5 // Copyright (C) 2004 Alex Tingle.
6 //
7 // This file is part of the omniEvents application.
8 //
9 // omniEvents is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // omniEvents is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 
24 #ifndef OMNIEVENTS__DAEMON_UNIX_H
25 #define OMNIEVENTS__DAEMON_UNIX_H
26 
27 #ifdef __WIN32__
28 # error("This file is not intended for Windows.")
29 #endif
30 
31 namespace OmniEvents {
32 
38 {
39 public:
40  static DaemonImpl _inst;
41 
42  char* _tracefile;
43  bool _foreground;
44  char* _pidfile;
45  int _pipe[2];
46  bool _havePidfile;
47  bool _haveParent;
48  bool _haveSyslog;
49 
50  DaemonImpl();
51  ~DaemonImpl();
52 
53  void tracefile(const char* val);
54  void pidfile(const char* val);
55  void foreground(bool val);
56 
58  void initialize(int&,char**&);
59 
63  void daemonize();
64 
69  void runningOk();
70 
74  void shutdown(int status);
75 
81  static void log(const char* message);
82 
83 private:
85  void fork();
86 
90  void redirectStreamsTo(const char* filename);
91 
95  int openFileFor(int fd, const char* filename, int flags);
96 
100  void checkPidfileOrShutdown();
101 
102  void writePidfile();
103 
108  int waitForChild();
109 
111  void notifyParent(int status);
112 };
113 
114 } // end namespace OmniEvents
115 
116 #endif // OMNIEVENTS__DAEMON_UNIX_H
Utility class that contains various methods for running omniEvents as a Unix daemon.
Definition: daemon_unix.h:38
void runningOk()
Called to signal that all startup operations have completed OK.
Definition: daemon_unix.cc:215
void foreground(bool val)
Set _foreground.
Definition: daemon_unix.cc:137
void tracefile(const char *val)
Set _tracefile.
Definition: daemon_unix.cc:131
int openFileFor(int fd, const char *filename, int flags)
Opens a (new?) file called 'filename' for writing, and uses it to hijack stream 'fd'.
Definition: daemon_unix.cc:446
int _pipe[2]
Unnamed pipe for child->parent comms.
Definition: daemon_unix.h:45
void checkPidfileOrShutdown()
If pidfile exists & contains a running process then shutdown() (Unix).
Definition: daemon_unix.cc:298
void pidfile(const char *val)
Set _pidfile.
Definition: daemon_unix.cc:143
int waitForChild()
Called by the parent process (Unix).
Definition: daemon_unix.cc:460
void notifyParent(int status)
Tells the parent to exit with the given status (Unix).
Definition: daemon_unix.cc:477
static void log(const char *message)
Callback, used as a parameter to omniORB::setLogFunction().
Definition: daemon_unix.cc:265
char * _tracefile
The tracefile name (if any).
Definition: daemon_unix.h:42
bool _foreground
TRUE for debug mode (run in foreground)
Definition: daemon_unix.h:43
void daemonize()
Puts the current process into the background.
Definition: daemon_unix.cc:158
void initialize(int &, char **&)
Does nothing on Unix.
Definition: daemon_unix.cc:152
bool _haveSyslog
Should we close syslog before quitting?
Definition: daemon_unix.h:48
char * _pidfile
The pidfile name (if any).
Definition: daemon_unix.h:44
void redirectStreamsTo(const char *filename)
Redirect stdout & stderr to filename.
Definition: daemon_unix.cc:426
void shutdown(int status)
Exit handler called (indirectly) by ::on_exit() - shuts down the daemon.
Definition: daemon_unix.cc:236
void fork()
Performs the actual fork.
Definition: daemon_unix.cc:378
bool _haveParent
Is there a parent for us to clean up?
Definition: daemon_unix.h:47
static DaemonImpl _inst
Definition: daemon_unix.h:40
bool _havePidfile
Is there a pidfile for us to clean up?
Definition: daemon_unix.h:46