2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2014 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <common/compat/paths.h>
26 #include <urcu/system.h>
28 #include <common/daemonize.h>
29 #include <common/error.h>
32 int lttng_daemonize(pid_t
*child_ppid
, int *completion_flag
,
38 /* Get parent pid of this process. */
39 *child_ppid
= getppid();
45 } else if (pid
== 0) {
52 * Get the newly created parent pid so we can signal
53 * that process when we are ready to operate.
55 *child_ppid
= getppid();
64 * Try to change directory to /. If we can't well at
73 fd
= open(_PATH_DEVNULL
, O_RDWR
, 0);
75 PERROR("open %s", _PATH_DEVNULL
);
77 * Let 0, 1 and 2 open since we can't
78 * bind them to /dev/null.
81 (void) dup2(fd
, STDIN_FILENO
);
82 (void) dup2(fd
, STDOUT_FILENO
);
83 (void) dup2(fd
, STDERR_FILENO
);
97 * Waiting for child to notify this parent that it can
98 * exit. Note that sleep() is interrupted before the 1
99 * second delay as soon as the signal is received, so it
100 * will not cause visible delay for the user.
102 while (!CMM_LOAD_SHARED(*completion_flag
)) {
107 * Check if child exists without blocking. If
108 * so, we have to stop this parent process and
111 ret
= waitpid(pid
, &status
, WNOHANG
);
112 if (ret
< 0 || (ret
!= 0 && WIFEXITED(status
))) {
113 /* The child exited somehow or was not valid. */
120 * From this point on, the parent can exit and the child
121 * is now an operationnal session daemon ready to serve
122 * clients and applications.
This page took 0.033332 seconds and 4 git commands to generate.