3
* Copyright 2004--2009, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#include "talk/base/posix.h"
35
#include "talk/base/linuxfdwalk.h"
37
#include "talk/base/logging.h"
42
static void closefds(void *close_errors, int fd) {
44
// We leave stdin/out/err open to the browser's terminal, if any.
48
*static_cast<bool *>(close_errors) = true;
54
EXIT_FLAG_CHDIR_ERRORS = 1 << 0,
56
EXIT_FLAG_FDWALK_ERRORS = 1 << 1,
57
EXIT_FLAG_CLOSE_ERRORS = 1 << 2,
59
EXIT_FLAG_SECOND_FORK_FAILED = 1 << 3,
62
bool RunAsDaemon(const char *file, const char *const argv[]) {
63
// Fork intermediate child to daemonize.
66
LOG_ERR(LS_ERROR) << "fork()";
71
// We try to close all fds and change directory to /, but if that fails we
72
// keep going because it's not critical.
75
exit_code |= EXIT_FLAG_CHDIR_ERRORS;
78
bool close_errors = false;
79
if (fdwalk(&closefds, &close_errors) < 0) {
80
exit_code |= EXIT_FLAG_FDWALK_ERRORS;
83
exit_code |= EXIT_FLAG_CLOSE_ERRORS;
87
// Fork again to become a daemon.
89
// It is important that everything here use _exit() and not exit(), because
90
// exit() would call the destructors of all global variables in the whole
91
// process, which is both unnecessary and unsafe.
93
exit_code |= EXIT_FLAG_SECOND_FORK_FAILED;
94
_exit(exit_code); // if second fork failed
97
// Successfully daemonized. Run command.
98
// POSIX requires the args to be typed as non-const for historical
99
// reasons, but it mandates that the actual implementation be const, so
101
execvp(file, const_cast<char *const *>(argv));
102
_exit(255); // if execvp failed
106
// Successfully spawned process, but report any problems to the parent where
111
// Parent. Reap intermediate child.
113
pid_t child = waitpid(pid, &status, 0);
115
LOG_ERR(LS_ERROR) << "Error in waitpid()";
119
// Should never happen (see man page).
120
LOG(LS_ERROR) << "waitpid() chose wrong child???";
123
if (!WIFEXITED(status)) {
124
LOG(LS_ERROR) << "Intermediate child killed uncleanly"; // Probably crashed
128
int exit_code = WEXITSTATUS(status);
129
if (exit_code & EXIT_FLAG_CHDIR_ERRORS) {
130
LOG(LS_WARNING) << "Child reported probles calling chdir()";
133
if (exit_code & EXIT_FLAG_FDWALK_ERRORS) {
134
LOG(LS_WARNING) << "Child reported problems calling fdwalk()";
136
if (exit_code & EXIT_FLAG_CLOSE_ERRORS) {
137
LOG(LS_WARNING) << "Child reported problems calling close()";
140
if (exit_code & EXIT_FLAG_SECOND_FORK_FAILED) {
141
LOG(LS_ERROR) << "Failed to daemonize";
142
// This means the command was not launched, so failure.
148
} // namespace talk_base