diff options
author | Christian Pointner <equinox@anytun.org> | 2009-02-12 02:27:18 +0000 |
---|---|---|
committer | Christian Pointner <equinox@anytun.org> | 2009-02-12 02:27:18 +0000 |
commit | 3fa1265df98ef125c1b70f7f2d384ca119070f02 (patch) | |
tree | 5d00b3476b3ad225870c6e290752426a228f4f01 | |
parent | init script cleanup (diff) |
improved daemonizing
-rw-r--r-- | src/daemon.hpp | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/daemon.hpp b/src/daemon.hpp index cc95337..4b59661 100644 --- a/src/daemon.hpp +++ b/src/daemon.hpp @@ -120,11 +120,35 @@ void daemonize() pid_t pid; pid = fork(); + if(pid < 0) { + std::stringstream msg; + msg << "daemonizing failed at fork(): " << LogErrno(errno) << ", exitting"; + throw std::runtime_error(msg.str()); + } if(pid) exit(0); - setsid(); + + umask(0); + + if(setsid() < 0) { + std::stringstream msg; + msg << "daemonizing failed at setsid(): " << LogErrno(errno) << ", exitting"; + throw std::runtime_error(msg.str()); + } + pid = fork(); + if(pid < 0) { + std::stringstream msg; + msg << "daemonizing failed at fork(): " << LogErrno(errno) << ", exitting"; + throw std::runtime_error(msg.str()); + } if(pid) exit(0); + if ((chdir("/")) < 0) { + std::stringstream msg; + msg << "daemonizing failed at chdir(): " << LogErrno(errno) << ", exitting"; + throw std::runtime_error(msg.str()); + } + // std::cout << "running in background now..." << std::endl; int fd; |