Lunchbox  1.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
daemon.h
1 
2 /* Copyright (c) 2012, Stefan.Eilemann@epfl.ch
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 2.1 as published
6  * by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 #ifndef LUNCHBOX_DAEMON_H
19 #define LUNCHBOX_DAEMON_H
20 
21 #include <lunchbox/debug.h>
22 #ifndef _MSC_VER
23 # include <unistd.h>
24 #endif
25 
26 namespace lunchbox
27 {
28 
39 inline bool daemonize()
40 {
41 #ifdef _MSC_VER
42  LBABORT( "Not implemented" );
43  return false;
44 #else
45  const pid_t pid = fork();
46  if( pid < 0 )
47  {
48  LBWARN << "Fork failed: " << sysError << std::endl;
49  return false;
50  }
51 
52  if( pid > 0 ) // parent
53  ::exit( EXIT_SUCCESS );
54 
55  const pid_t sid = setsid(); // child, create new seesion
56  if( sid < 0 )
57  {
58  LBWARN << "setsid failed: " << sysError << std::endl;
59  return false;
60  }
61 
62  // Close the standard file
63  ::close( STDIN_FILENO );
64  ::close( STDOUT_FILENO );
65  ::close( STDERR_FILENO );
66  return true;
67 #endif
68 }
69 }
70 
71 #endif //LUNCHBOX_DAEMON_H
LUNCHBOX_API std::ostream & sysError(std::ostream &os)
Print a textual description of the current system error.
LUNCHBOX_API bool exit()
De-initialize the Lunchbox base classes.
bool daemonize()
Turn the calling process into a daemon.
Definition: daemon.h:39
#define LBWARN
Output a warning message to the per-thread Log stream.
Definition: log.h:194