2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
10 #include <common/compat/paths.hpp>
11 #include <common/daemonize.hpp>
12 #include <common/error.hpp>
18 #include <urcu/system.h>
20 int lttng_daemonize(pid_t
*child_ppid
, int *completion_flag
, int close_fds
)
24 /* Get parent pid of this process. */
25 *child_ppid
= getppid();
31 } else if (pid
== 0) {
39 * Get the newly created parent pid so we can signal
40 * that process when we are ready to operate.
42 *child_ppid
= getppid();
51 * Try to change directory to /. If we can't well at
60 fd
= open(_PATH_DEVNULL
, O_RDWR
, 0);
62 PERROR("open %s", _PATH_DEVNULL
);
64 * Let 0, 1 and 2 open since we can't
65 * bind them to /dev/null.
68 (void) dup2(fd
, STDIN_FILENO
);
69 (void) dup2(fd
, STDOUT_FILENO
);
70 (void) dup2(fd
, STDERR_FILENO
);
84 * Waiting for child to notify this parent that it can
85 * exit. Note that sleep() is interrupted before the 1
86 * second delay as soon as the signal is received, so it
87 * will not cause visible delay for the user.
89 while (!CMM_LOAD_SHARED(*completion_flag
)) {
94 * Check if child exists without blocking. If
95 * so, we have to stop this parent process and
98 ret
= waitpid(pid
, &status
, WNOHANG
);
99 if (ret
< 0 || (ret
!= 0 && WIFEXITED(status
))) {
100 /* The child exited somehow or was not valid. */
107 * From this point on, the parent can exit and the child
108 * is now an operational session daemon ready to serve
109 * clients and applications.
This page took 0.037589 seconds and 4 git commands to generate.