Add daemonize() to libcommon
[lttng-tools.git] / src / common / daemonize.c
CommitLineData
99ed9c43
MD
1/*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2014 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
8 *
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
12 * more details.
13 *
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.
17 */
18
19#define _GNU_SOURCE
20#include <unistd.h>
21#include <paths.h>
22#include <fcntl.h>
23#include <wait.h>
24#include <stdlib.h>
25
26#include <urcu/system.h>
27
28#include <src/common/daemonize.h>
29#include <src/common/error.h>
30
31int lttng_daemonize(pid_t *child_ppid, int *completion_flag,
32 int close_fds)
33{
34 int ret;
35 pid_t pid;
36
37 /* Get parent pid of this process. */
38 *child_ppid = getppid();
39
40 pid = fork();
41 if (pid < 0) {
42 PERROR("fork");
43 goto error;
44 } else if (pid == 0) {
45 int fd;
46 pid_t sid;
47
48 /* Child */
49
50 /*
51 * Get the newly created parent pid so we can signal
52 * that process when we are ready to operate.
53 */
54 *child_ppid = getppid();
55
56 sid = setsid();
57 if (sid < 0) {
58 PERROR("setsid");
59 goto error;
60 }
61
62 /*
63 * Try to change directory to /. If we can't well at
64 * least notify.
65 */
66 ret = chdir("/");
67 if (ret < 0) {
68 PERROR("chdir");
69 }
70
71 if (close_fds) {
72 fd = open(_PATH_DEVNULL, O_RDWR, 0);
73 if (fd < 0) {
74 PERROR("open %s", _PATH_DEVNULL);
75 /*
76 * Let 0, 1 and 2 open since we can't
77 * bind them to /dev/null.
78 */
79 } else {
80 (void) dup2(fd, STDIN_FILENO);
81 (void) dup2(fd, STDOUT_FILENO);
82 (void) dup2(fd, STDERR_FILENO);
83 if (fd > 2) {
84 ret = close(fd);
85 if (ret < 0) {
86 PERROR("close");
87 }
88 }
89 }
90 }
91 goto end;
92 } else {
93 /* Parent */
94
95 /*
96 * Waiting for child to notify this parent that it can
97 * exit. Note that sleep() is interrupted before the 1
98 * second delay as soon as the signal is received, so it
99 * will not cause visible delay for the user.
100 */
101 while (!CMM_LOAD_SHARED(*completion_flag)) {
102 int status;
103 pid_t ret;
104
105 /*
106 * Check if child exists without blocking. If
107 * so, we have to stop this parent process and
108 * return an error.
109 */
110 ret = waitpid(pid, &status, WNOHANG);
111 if (ret < 0 || (ret != 0 && WIFEXITED(status))) {
112 /* The child exited somehow or was not valid. */
113 goto error;
114 }
115 sleep(1);
116 }
117
118 /*
119 * From this point on, the parent can exit and the child
120 * is now an operationnal session daemon ready to serve
121 * clients and applications.
122 */
123 exit(EXIT_SUCCESS);
124 }
125
126end:
127 return 0;
128
129error:
130 return -1;
131}
This page took 0.026803 seconds and 4 git commands to generate.