Build fix: specialization of template in different namespace
[lttng-tools.git] / src / common / daemonize.cpp
CommitLineData
99ed9c43 1/*
ab5be9fa
MJ
2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
99ed9c43 4 *
ab5be9fa 5 * SPDX-License-Identifier: GPL-2.0-only
99ed9c43 6 *
99ed9c43
MD
7 */
8
6c1c0768 9#define _LGPL_SOURCE
99ed9c43 10#include <unistd.h>
c9e313bc 11#include <common/compat/paths.hpp>
99ed9c43 12#include <fcntl.h>
0452bf08 13#include <sys/wait.h>
99ed9c43
MD
14#include <stdlib.h>
15
16#include <urcu/system.h>
17
c9e313bc
SM
18#include <common/daemonize.hpp>
19#include <common/error.hpp>
99ed9c43
MD
20
21int lttng_daemonize(pid_t *child_ppid, int *completion_flag,
22 int close_fds)
23{
99ed9c43
MD
24 pid_t pid;
25
26 /* Get parent pid of this process. */
27 *child_ppid = getppid();
28
29 pid = fork();
30 if (pid < 0) {
31 PERROR("fork");
32 goto error;
33 } else if (pid == 0) {
34 int fd;
35 pid_t sid;
44635d77 36 int ret;
99ed9c43
MD
37
38 /* Child */
39
40 /*
41 * Get the newly created parent pid so we can signal
42 * that process when we are ready to operate.
43 */
44 *child_ppid = getppid();
45
46 sid = setsid();
47 if (sid < 0) {
48 PERROR("setsid");
49 goto error;
50 }
51
52 /*
53 * Try to change directory to /. If we can't well at
54 * least notify.
55 */
56 ret = chdir("/");
57 if (ret < 0) {
58 PERROR("chdir");
59 }
60
61 if (close_fds) {
62 fd = open(_PATH_DEVNULL, O_RDWR, 0);
63 if (fd < 0) {
64 PERROR("open %s", _PATH_DEVNULL);
65 /*
66 * Let 0, 1 and 2 open since we can't
67 * bind them to /dev/null.
68 */
69 } else {
70 (void) dup2(fd, STDIN_FILENO);
71 (void) dup2(fd, STDOUT_FILENO);
72 (void) dup2(fd, STDERR_FILENO);
73 if (fd > 2) {
74 ret = close(fd);
75 if (ret < 0) {
76 PERROR("close");
77 }
78 }
79 }
80 }
81 goto end;
82 } else {
83 /* Parent */
84
85 /*
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.
90 */
91 while (!CMM_LOAD_SHARED(*completion_flag)) {
92 int status;
93 pid_t ret;
94
95 /*
96 * Check if child exists without blocking. If
97 * so, we have to stop this parent process and
98 * return an error.
99 */
100 ret = waitpid(pid, &status, WNOHANG);
101 if (ret < 0 || (ret != 0 && WIFEXITED(status))) {
102 /* The child exited somehow or was not valid. */
103 goto error;
104 }
105 sleep(1);
106 }
107
108 /*
109 * From this point on, the parent can exit and the child
f3630ec4 110 * is now an operational session daemon ready to serve
99ed9c43
MD
111 * clients and applications.
112 */
113 exit(EXIT_SUCCESS);
114 }
115
116end:
117 return 0;
118
119error:
120 return -1;
121}
This page took 0.079048 seconds and 4 git commands to generate.