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
11 #include <common/compat/paths.hpp>
16 #include <urcu/system.h>
18 #include <common/daemonize.hpp>
19 #include <common/error.hpp>
21 int lttng_daemonize(pid_t
*child_ppid
, int *completion_flag
,
26 /* Get parent pid of this process. */
27 *child_ppid
= getppid();
33 } else if (pid
== 0) {
41 * Get the newly created parent pid so we can signal
42 * that process when we are ready to operate.
44 *child_ppid
= getppid();
53 * Try to change directory to /. If we can't well at
62 fd
= open(_PATH_DEVNULL
, O_RDWR
, 0);
64 PERROR("open %s", _PATH_DEVNULL
);
66 * Let 0, 1 and 2 open since we can't
67 * bind them to /dev/null.
70 (void) dup2(fd
, STDIN_FILENO
);
71 (void) dup2(fd
, STDOUT_FILENO
);
72 (void) dup2(fd
, STDERR_FILENO
);
86 * Waiting for child to notify this parent that it can
87 * exit. Note that sleep() is interrupted before the 1
88 * second delay as soon as the signal is received, so it
89 * will not cause visible delay for the user.
91 while (!CMM_LOAD_SHARED(*completion_flag
)) {
96 * Check if child exists without blocking. If
97 * so, we have to stop this parent process and
100 ret
= waitpid(pid
, &status
, WNOHANG
);
101 if (ret
< 0 || (ret
!= 0 && WIFEXITED(status
))) {
102 /* The child exited somehow or was not valid. */
109 * From this point on, the parent can exit and the child
110 * is now an operational session daemon ready to serve
111 * clients and applications.
This page took 0.036132 seconds and 4 git commands to generate.