3a006134b2bb9f5902b531b84c7d263315f81ae9
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.
27 #include <urcu/system.h>
29 #include <src/common/daemonize.h>
30 #include <src/common/error.h>
33 int lttng_daemonize(pid_t
*child_ppid
, int *completion_flag
,
39 /* Get parent pid of this process. */
40 *child_ppid
= getppid();
46 } else if (pid
== 0) {
53 * Get the newly created parent pid so we can signal
54 * that process when we are ready to operate.
56 *child_ppid
= getppid();
65 * Try to change directory to /. If we can't well at
74 fd
= open(_PATH_DEVNULL
, O_RDWR
, 0);
76 PERROR("open %s", _PATH_DEVNULL
);
78 * Let 0, 1 and 2 open since we can't
79 * bind them to /dev/null.
82 (void) dup2(fd
, STDIN_FILENO
);
83 (void) dup2(fd
, STDOUT_FILENO
);
84 (void) dup2(fd
, STDERR_FILENO
);
98 * Waiting for child to notify this parent that it can
99 * exit. Note that sleep() is interrupted before the 1
100 * second delay as soon as the signal is received, so it
101 * will not cause visible delay for the user.
103 while (!CMM_LOAD_SHARED(*completion_flag
)) {
108 * Check if child exists without blocking. If
109 * so, we have to stop this parent process and
112 ret
= waitpid(pid
, &status
, WNOHANG
);
113 if (ret
< 0 || (ret
!= 0 && WIFEXITED(status
))) {
114 /* The child exited somehow or was not valid. */
121 * From this point on, the parent can exit and the child
122 * is now an operationnal session daemon ready to serve
123 * clients and applications.
This page took 0.032128 seconds and 3 git commands to generate.