Lunchbox  1.17.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
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 {
38 inline bool daemonize()
39 {
40 #ifdef _MSC_VER
41  LBABORT("Not implemented");
42  return false;
43 #else
44  const pid_t pid = fork();
45  if (pid < 0)
46  {
47  LBWARN << "Fork failed: " << sysError << std::endl;
48  return false;
49  }
50 
51  if (pid > 0) // parent
52  ::exit(EXIT_SUCCESS);
53 
54  const pid_t sid = setsid(); // child, create new seesion
55  if (sid < 0)
56  {
57  LBWARN << "setsid failed: " << sysError << std::endl;
58  return false;
59  }
60 
61  // Close the standard file
62  ::close(STDIN_FILENO);
63  ::close(STDOUT_FILENO);
64  ::close(STDERR_FILENO);
65  return true;
66 #endif
67 }
68 }
69 
70 #endif // LUNCHBOX_DAEMON_H
bool exit()
De-initialize the Lunchbox base classes.
bool fork(const std::string &command)
Execute the given command in a new process.
std::ostream & sysError(std::ostream &os)
Print a textual description of the current system error.
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:29
bool daemonize()
Turn the calling process into a daemon.
Definition: daemon.h:38
#define LBWARN
Definition: log.h:197