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.h>
16 #include <urcu/system.h>
18 #include <common/daemonize.h>
19 #include <common/error.h>
22 int lttng_daemonize(pid_t
*child_ppid
, int *completion_flag
,
28 /* Get parent pid of this process. */
29 *child_ppid
= getppid();
35 } else if (pid
== 0) {
42 * Get the newly created parent pid so we can signal
43 * that process when we are ready to operate.
45 *child_ppid
= getppid();
54 * Try to change directory to /. If we can't well at
63 fd
= open(_PATH_DEVNULL
, O_RDWR
, 0);
65 PERROR("open %s", _PATH_DEVNULL
);
67 * Let 0, 1 and 2 open since we can't
68 * bind them to /dev/null.
71 (void) dup2(fd
, STDIN_FILENO
);
72 (void) dup2(fd
, STDOUT_FILENO
);
73 (void) dup2(fd
, STDERR_FILENO
);
87 * Waiting for child to notify this parent that it can
88 * exit. Note that sleep() is interrupted before the 1
89 * second delay as soon as the signal is received, so it
90 * will not cause visible delay for the user.
92 while (!CMM_LOAD_SHARED(*completion_flag
)) {
97 * Check if child exists without blocking. If
98 * so, we have to stop this parent process and
101 ret
= waitpid(pid
, &status
, WNOHANG
);
102 if (ret
< 0 || (ret
!= 0 && WIFEXITED(status
))) {
103 /* The child exited somehow or was not valid. */
110 * From this point on, the parent can exit and the child
111 * is now an operational session daemon ready to serve
112 * clients and applications.
This page took 0.043427 seconds and 5 git commands to generate.