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