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.
26 #include <urcu/system.h>
28 #include <src/common/daemonize.h>
29 #include <src/common/error.h>
31 int lttng_daemonize(pid_t
*child_ppid
, int *completion_flag
,
37 /* Get parent pid of this process. */
38 *child_ppid
= getppid();
44 } else if (pid
== 0) {
51 * Get the newly created parent pid so we can signal
52 * that process when we are ready to operate.
54 *child_ppid
= getppid();
63 * Try to change directory to /. If we can't well at
72 fd
= open(_PATH_DEVNULL
, O_RDWR
, 0);
74 PERROR("open %s", _PATH_DEVNULL
);
76 * Let 0, 1 and 2 open since we can't
77 * bind them to /dev/null.
80 (void) dup2(fd
, STDIN_FILENO
);
81 (void) dup2(fd
, STDOUT_FILENO
);
82 (void) dup2(fd
, STDERR_FILENO
);
96 * Waiting for child to notify this parent that it can
97 * exit. Note that sleep() is interrupted before the 1
98 * second delay as soon as the signal is received, so it
99 * will not cause visible delay for the user.
101 while (!CMM_LOAD_SHARED(*completion_flag
)) {
106 * Check if child exists without blocking. If
107 * so, we have to stop this parent process and
110 ret
= waitpid(pid
, &status
, WNOHANG
);
111 if (ret
< 0 || (ret
!= 0 && WIFEXITED(status
))) {
112 /* The child exited somehow or was not valid. */
119 * From this point on, the parent can exit and the child
120 * is now an operationnal session daemon ready to serve
121 * clients and applications.
This page took 0.032468 seconds and 5 git commands to generate.