Commit | Line | Data |
---|---|---|
826d496d MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
0fdd1e2c | 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
26296c48 | 4 | * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
fac6795d | 5 | * |
d14d33bf AM |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License, version 2 only, | |
8 | * as published by the Free Software Foundation. | |
91d76f53 | 9 | * |
d14d33bf AM |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
91d76f53 | 14 | * |
d14d33bf AM |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
fac6795d DG |
18 | */ |
19 | ||
6c1c0768 | 20 | #define _LGPL_SOURCE |
fac6795d DG |
21 | #include <getopt.h> |
22 | #include <grp.h> | |
23 | #include <limits.h> | |
0bb7724a | 24 | #include <paths.h> |
fac6795d DG |
25 | #include <pthread.h> |
26 | #include <signal.h> | |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
331744e3 | 30 | #include <inttypes.h> |
0fdd1e2c | 31 | #include <sys/mman.h> |
b73401da | 32 | #include <sys/mount.h> |
1e307fab | 33 | #include <sys/resource.h> |
fac6795d DG |
34 | #include <sys/socket.h> |
35 | #include <sys/stat.h> | |
36 | #include <sys/types.h> | |
0fdd1e2c | 37 | #include <sys/wait.h> |
5c827ce0 | 38 | #include <urcu/uatomic.h> |
fac6795d | 39 | #include <unistd.h> |
4f0b90ee | 40 | #include <ctype.h> |
fac6795d | 41 | |
990570ed | 42 | #include <common/common.h> |
d27c42b8 | 43 | #include <common/compat/socket.h> |
e8fa9fb0 | 44 | #include <common/compat/getenv.h> |
db758600 DG |
45 | #include <common/defaults.h> |
46 | #include <common/kernel-consumer/kernel-consumer.h> | |
50c8f484 | 47 | #include <common/futex.h> |
00e2e675 | 48 | #include <common/relayd/relayd.h> |
81b86775 | 49 | #include <common/utils.h> |
3ccdf997 | 50 | #include <common/daemonize.h> |
f40ef1d5 | 51 | #include <common/config/session-config.h> |
dcabc190 | 52 | #include <common/dynamic-buffer.h> |
76fcf151 | 53 | #include <lttng/event-internal.h> |
fac6795d | 54 | |
10a8a223 | 55 | #include "lttng-sessiond.h" |
7972aab2 | 56 | #include "buffer-registry.h" |
54d01ffb | 57 | #include "channel.h" |
2f77fc4b | 58 | #include "cmd.h" |
00e2e675 | 59 | #include "consumer.h" |
099e26bd | 60 | #include "context.h" |
54d01ffb | 61 | #include "event.h" |
4771f025 | 62 | #include "kernel.h" |
f1e16794 | 63 | #include "kernel-consumer.h" |
096102bd | 64 | #include "modprobe.h" |
0fdd1e2c | 65 | #include "shm.h" |
1e307fab | 66 | #include "ust-ctl.h" |
00e2e675 | 67 | #include "ust-consumer.h" |
8e68d1c8 | 68 | #include "utils.h" |
4063050c | 69 | #include "fd-limit.h" |
8782cc74 | 70 | #include "health-sessiond.h" |
8ac94142 | 71 | #include "testpoint.h" |
d0b96690 | 72 | #include "ust-thread.h" |
022d91ba | 73 | #include "agent-thread.h" |
fb198a11 | 74 | #include "save.h" |
ef367a93 | 75 | #include "load-session-thread.h" |
b3530820 JG |
76 | #include "notification-thread.h" |
77 | #include "notification-thread-commands.h" | |
db66e574 | 78 | #include "rotation-thread.h" |
0dbc2034 | 79 | #include "lttng-syscall.h" |
7c1d2758 | 80 | #include "agent.h" |
5e97de00 | 81 | #include "ht-cleanup.h" |
e6142f2e | 82 | #include "sessiond-config.h" |
432b38d7 | 83 | #include "timer.h" |
548468b9 | 84 | #include "thread.h" |
050e613c | 85 | #include "client.h" |
dd5875e7 | 86 | #include "dispatch.h" |
22b80785 | 87 | #include "register.h" |
cdffb210 | 88 | #include "manage-apps.h" |
45f6525f | 89 | #include "manage-kernel.h" |
ebaeda94 | 90 | |
4fc83d94 PP |
91 | static const char *help_msg = |
92 | #ifdef LTTNG_EMBED_HELP | |
93 | #include <lttng-sessiond.8.h> | |
94 | #else | |
95 | NULL | |
96 | #endif | |
97 | ; | |
98 | ||
fac6795d | 99 | const char *progname; |
c9cb3e7d | 100 | static int lockfile_fd = -1; |
3bd1e081 | 101 | |
0bb7724a DG |
102 | /* Set to 1 when a SIGUSR1 signal is received. */ |
103 | static int recv_child_signal; | |
104 | ||
26296c48 JG |
105 | /* Command line options */ |
106 | static const struct option long_options[] = { | |
0f5ea17c JG |
107 | { "client-sock", required_argument, 0, 'c' }, |
108 | { "apps-sock", required_argument, 0, 'a' }, | |
109 | { "kconsumerd-cmd-sock", required_argument, 0, '\0' }, | |
110 | { "kconsumerd-err-sock", required_argument, 0, '\0' }, | |
111 | { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' }, | |
112 | { "ustconsumerd32-err-sock", required_argument, 0, '\0' }, | |
113 | { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' }, | |
114 | { "ustconsumerd64-err-sock", required_argument, 0, '\0' }, | |
115 | { "consumerd32-path", required_argument, 0, '\0' }, | |
116 | { "consumerd32-libdir", required_argument, 0, '\0' }, | |
117 | { "consumerd64-path", required_argument, 0, '\0' }, | |
118 | { "consumerd64-libdir", required_argument, 0, '\0' }, | |
119 | { "daemonize", no_argument, 0, 'd' }, | |
120 | { "background", no_argument, 0, 'b' }, | |
121 | { "sig-parent", no_argument, 0, 'S' }, | |
122 | { "help", no_argument, 0, 'h' }, | |
123 | { "group", required_argument, 0, 'g' }, | |
124 | { "version", no_argument, 0, 'V' }, | |
125 | { "quiet", no_argument, 0, 'q' }, | |
126 | { "verbose", no_argument, 0, 'v' }, | |
127 | { "verbose-consumer", no_argument, 0, '\0' }, | |
128 | { "no-kernel", no_argument, 0, '\0' }, | |
129 | { "pidfile", required_argument, 0, 'p' }, | |
130 | { "agent-tcp-port", required_argument, 0, '\0' }, | |
131 | { "config", required_argument, 0, 'f' }, | |
132 | { "load", required_argument, 0, 'l' }, | |
133 | { "kmod-probes", required_argument, 0, '\0' }, | |
134 | { "extra-kmod-probes", required_argument, 0, '\0' }, | |
26296c48 JG |
135 | { NULL, 0, 0, 0 } |
136 | }; | |
137 | ||
138 | /* Command line options to ignore from configuration file */ | |
139 | static const char *config_ignore_options[] = { "help", "version", "config" }; | |
1d4b027a | 140 | |
099e26bd DG |
141 | /* |
142 | * This pipe is used to inform the thread managing application communication | |
143 | * that a command is queued and ready to be processed. | |
144 | */ | |
76d7553f | 145 | static int apps_cmd_pipe[2] = { -1, -1 }; |
53c51aaa | 146 | static int apps_cmd_notify_pipe[2] = { -1, -1 }; |
099e26bd | 147 | |
1d4b027a | 148 | /* Pthread, Mutexes and Semaphores */ |
ef367a93 | 149 | static pthread_t load_session_thread; |
5eb91c98 | 150 | |
099e26bd DG |
151 | /* |
152 | * UST registration command queue. This queue is tied with a futex and uses a N | |
153 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
154 | * | |
b22c5da8 DG |
155 | * The thread_registration_apps and thread_dispatch_ust_registration uses this |
156 | * queue along with the wait/wake scheme. The thread_manage_apps receives down | |
157 | * the line new application socket and monitors it for any I/O error or clean | |
158 | * close that triggers an unregistration of the application. | |
099e26bd DG |
159 | */ |
160 | static struct ust_cmd_queue ust_cmd_queue; | |
161 | ||
2f77fc4b DG |
162 | static const char *module_proc_lttng = "/proc/lttng"; |
163 | ||
ef367a93 | 164 | /* Load session thread information to operate. */ |
51bb4dbb | 165 | static struct load_session_thread_data *load_info; |
7c1d2758 | 166 | |
97bc1426 | 167 | /* |
51bb4dbb | 168 | * Section name to look for in the daemon configuration file. |
97bc1426 | 169 | */ |
51bb4dbb | 170 | static const char * const config_section_name = "sessiond"; |
5e97de00 | 171 | |
51bb4dbb JG |
172 | /* Am I root or not. Set to 1 if the daemon is running as root */ |
173 | static int is_root; | |
5eb91c98 | 174 | |
099e26bd DG |
175 | /* |
176 | * Stop all threads by closing the thread quit pipe. | |
177 | */ | |
cf3af59e MD |
178 | static void stop_threads(void) |
179 | { | |
5eb91c98 DG |
180 | int ret; |
181 | ||
cf3af59e MD |
182 | /* Stopping all threads */ |
183 | DBG("Terminating all threads"); | |
51bb4dbb | 184 | ret = sessiond_notify_quit_pipe(); |
5eb91c98 DG |
185 | if (ret < 0) { |
186 | ERR("write error on thread quit pipe"); | |
187 | } | |
cf3af59e MD |
188 | } |
189 | ||
e975f9f8 DG |
190 | /* |
191 | * Close every consumer sockets. | |
192 | */ | |
193 | static void close_consumer_sockets(void) | |
194 | { | |
195 | int ret; | |
196 | ||
197 | if (kconsumer_data.err_sock >= 0) { | |
198 | ret = close(kconsumer_data.err_sock); | |
199 | if (ret < 0) { | |
200 | PERROR("kernel consumer err_sock close"); | |
201 | } | |
202 | } | |
203 | if (ustconsumer32_data.err_sock >= 0) { | |
204 | ret = close(ustconsumer32_data.err_sock); | |
205 | if (ret < 0) { | |
a76cbd9f | 206 | PERROR("UST consumerd32 err_sock close"); |
e975f9f8 DG |
207 | } |
208 | } | |
209 | if (ustconsumer64_data.err_sock >= 0) { | |
210 | ret = close(ustconsumer64_data.err_sock); | |
211 | if (ret < 0) { | |
a76cbd9f | 212 | PERROR("UST consumerd64 err_sock close"); |
e975f9f8 DG |
213 | } |
214 | } | |
215 | if (kconsumer_data.cmd_sock >= 0) { | |
216 | ret = close(kconsumer_data.cmd_sock); | |
217 | if (ret < 0) { | |
218 | PERROR("kernel consumer cmd_sock close"); | |
219 | } | |
220 | } | |
221 | if (ustconsumer32_data.cmd_sock >= 0) { | |
222 | ret = close(ustconsumer32_data.cmd_sock); | |
223 | if (ret < 0) { | |
a76cbd9f | 224 | PERROR("UST consumerd32 cmd_sock close"); |
e975f9f8 DG |
225 | } |
226 | } | |
227 | if (ustconsumer64_data.cmd_sock >= 0) { | |
228 | ret = close(ustconsumer64_data.cmd_sock); | |
229 | if (ret < 0) { | |
a76cbd9f | 230 | PERROR("UST consumerd64 cmd_sock close"); |
e975f9f8 DG |
231 | } |
232 | } | |
b3530820 JG |
233 | if (kconsumer_data.channel_monitor_pipe >= 0) { |
234 | ret = close(kconsumer_data.channel_monitor_pipe); | |
235 | if (ret < 0) { | |
236 | PERROR("kernel consumer channel monitor pipe close"); | |
237 | } | |
238 | } | |
239 | if (ustconsumer32_data.channel_monitor_pipe >= 0) { | |
240 | ret = close(ustconsumer32_data.channel_monitor_pipe); | |
241 | if (ret < 0) { | |
242 | PERROR("UST consumerd32 channel monitor pipe close"); | |
243 | } | |
244 | } | |
245 | if (ustconsumer64_data.channel_monitor_pipe >= 0) { | |
246 | ret = close(ustconsumer64_data.channel_monitor_pipe); | |
247 | if (ret < 0) { | |
248 | PERROR("UST consumerd64 channel monitor pipe close"); | |
249 | } | |
250 | } | |
e975f9f8 DG |
251 | } |
252 | ||
4e4714cb JR |
253 | /* |
254 | * Wait on consumer process termination. | |
255 | * | |
256 | * Need to be called with the consumer data lock held or from a context | |
257 | * ensuring no concurrent access to data (e.g: cleanup). | |
258 | */ | |
259 | static void wait_consumer(struct consumer_data *consumer_data) | |
260 | { | |
261 | pid_t ret; | |
262 | int status; | |
263 | ||
264 | if (consumer_data->pid <= 0) { | |
265 | return; | |
266 | } | |
267 | ||
268 | DBG("Waiting for complete teardown of consumerd (PID: %d)", | |
269 | consumer_data->pid); | |
270 | ret = waitpid(consumer_data->pid, &status, 0); | |
271 | if (ret == -1) { | |
272 | PERROR("consumerd waitpid pid: %d", consumer_data->pid) | |
1640c24c | 273 | } else if (!WIFEXITED(status)) { |
4e4714cb JR |
274 | ERR("consumerd termination with error: %d", |
275 | WEXITSTATUS(ret)); | |
276 | } | |
277 | consumer_data->pid = 0; | |
278 | } | |
279 | ||
fac6795d | 280 | /* |
4a15001e | 281 | * Cleanup the session daemon's data structures. |
fac6795d | 282 | */ |
4a15001e | 283 | static void sessiond_cleanup(void) |
fac6795d | 284 | { |
ef599319 | 285 | int ret; |
48a86f68 | 286 | struct ltt_session_list *session_list = session_get_list(); |
fac6795d | 287 | |
4a15001e | 288 | DBG("Cleanup sessiond"); |
e07ae692 | 289 | |
4e449f3f MD |
290 | /* |
291 | * Close the thread quit pipe. It has already done its job, | |
292 | * since we are now called. | |
293 | */ | |
51bb4dbb | 294 | sessiond_close_quit_pipe(); |
53c51aaa JG |
295 | utils_close_pipe(apps_cmd_pipe); |
296 | utils_close_pipe(apps_cmd_notify_pipe); | |
45f6525f | 297 | utils_close_pipe(kernel_poll_pipe); |
2f77fc4b | 298 | |
c9a2957d JG |
299 | ret = remove(config.pid_file_path.value); |
300 | if (ret < 0) { | |
301 | PERROR("remove pidfile %s", config.pid_file_path.value); | |
35f90c40 DG |
302 | } |
303 | ||
e6142f2e JG |
304 | DBG("Removing sessiond and consumerd content of directory %s", |
305 | config.rundir.value); | |
8c6c56c2 MD |
306 | |
307 | /* sessiond */ | |
e6142f2e JG |
308 | DBG("Removing %s", config.pid_file_path.value); |
309 | (void) unlink(config.pid_file_path.value); | |
8c6c56c2 | 310 | |
e6142f2e JG |
311 | DBG("Removing %s", config.agent_port_file_path.value); |
312 | (void) unlink(config.agent_port_file_path.value); | |
cd9290dd | 313 | |
8c6c56c2 | 314 | /* kconsumerd */ |
e6142f2e JG |
315 | DBG("Removing %s", kconsumer_data.err_unix_sock_path); |
316 | (void) unlink(kconsumer_data.err_unix_sock_path); | |
317 | ||
318 | DBG("Removing directory %s", config.kconsumerd_path.value); | |
319 | (void) rmdir(config.kconsumerd_path.value); | |
8c6c56c2 MD |
320 | |
321 | /* ust consumerd 32 */ | |
e6142f2e JG |
322 | DBG("Removing %s", config.consumerd32_err_unix_sock_path.value); |
323 | (void) unlink(config.consumerd32_err_unix_sock_path.value); | |
324 | ||
325 | DBG("Removing directory %s", config.consumerd32_path.value); | |
326 | (void) rmdir(config.consumerd32_path.value); | |
8c6c56c2 MD |
327 | |
328 | /* ust consumerd 64 */ | |
e6142f2e JG |
329 | DBG("Removing %s", config.consumerd64_err_unix_sock_path.value); |
330 | (void) unlink(config.consumerd64_err_unix_sock_path.value); | |
331 | ||
332 | DBG("Removing directory %s", config.consumerd64_path.value); | |
333 | (void) rmdir(config.consumerd64_path.value); | |
5461b305 | 334 | |
6af972a2 | 335 | pthread_mutex_destroy(&session_list->lock); |
273ea72c | 336 | |
4e4714cb JR |
337 | wait_consumer(&kconsumer_data); |
338 | wait_consumer(&ustconsumer64_data); | |
339 | wait_consumer(&ustconsumer32_data); | |
340 | ||
6a4e4039 JG |
341 | DBG("Cleaning up all agent apps"); |
342 | agent_app_ht_clean(); | |
343 | ||
099e26bd | 344 | DBG("Closing all UST sockets"); |
56fff090 | 345 | ust_app_clean_list(); |
7972aab2 | 346 | buffer_reg_destroy_registries(); |
099e26bd | 347 | |
e6142f2e | 348 | if (is_root && !config.no_kernel) { |
4fba7219 | 349 | DBG2("Closing kernel fd"); |
a4b35e07 | 350 | if (kernel_tracer_fd >= 0) { |
76d7553f MD |
351 | ret = close(kernel_tracer_fd); |
352 | if (ret) { | |
353 | PERROR("close"); | |
354 | } | |
a4b35e07 | 355 | } |
2f50c8a3 | 356 | DBG("Unloading kernel modules"); |
096102bd | 357 | modprobe_remove_lttng_all(); |
834978fd | 358 | free(syscall_table); |
2f50c8a3 | 359 | } |
2f77fc4b | 360 | |
e975f9f8 DG |
361 | close_consumer_sockets(); |
362 | ||
ef367a93 JG |
363 | if (load_info) { |
364 | load_session_destroy_data(load_info); | |
365 | free(load_info); | |
366 | } | |
367 | ||
c9cb3e7d JG |
368 | /* |
369 | * We do NOT rmdir rundir because there are other processes | |
370 | * using it, for instance lttng-relayd, which can start in | |
371 | * parallel with this teardown. | |
372 | */ | |
4a15001e MD |
373 | } |
374 | ||
375 | /* | |
376 | * Cleanup the daemon's option data structures. | |
377 | */ | |
378 | static void sessiond_cleanup_options(void) | |
379 | { | |
380 | DBG("Cleaning up options"); | |
381 | ||
e6142f2e | 382 | sessiond_config_fini(&config); |
c9cb3e7d | 383 | |
7567352f | 384 | run_as_destroy_worker(); |
fac6795d DG |
385 | } |
386 | ||
a23ec3a7 DG |
387 | /* |
388 | * Signal pthread condition of the consumer data that the thread. | |
389 | */ | |
390 | static void signal_consumer_condition(struct consumer_data *data, int state) | |
391 | { | |
392 | pthread_mutex_lock(&data->cond_mutex); | |
393 | ||
394 | /* | |
395 | * The state is set before signaling. It can be any value, it's the waiter | |
396 | * job to correctly interpret this condition variable associated to the | |
397 | * consumer pthread_cond. | |
398 | * | |
399 | * A value of 0 means that the corresponding thread of the consumer data | |
400 | * was not started. 1 indicates that the thread has started and is ready | |
401 | * for action. A negative value means that there was an error during the | |
402 | * thread bootstrap. | |
403 | */ | |
404 | data->consumer_thread_is_ready = state; | |
405 | (void) pthread_cond_signal(&data->cond); | |
406 | ||
407 | pthread_mutex_unlock(&data->cond_mutex); | |
408 | } | |
409 | ||
1d4b027a | 410 | /* |
3bd1e081 | 411 | * This thread manage the consumer error sent back to the session daemon. |
1d4b027a | 412 | */ |
050e613c | 413 | void *thread_manage_consumer(void *data) |
1d4b027a | 414 | { |
42fc1d0b | 415 | int sock = -1, i, ret, pollfd, err = -1, should_quit = 0; |
5eb91c98 | 416 | uint32_t revents, nb_fd; |
1d4b027a | 417 | enum lttcomm_return_code code; |
5eb91c98 | 418 | struct lttng_poll_event events; |
3bd1e081 | 419 | struct consumer_data *consumer_data = data; |
b3530820 | 420 | struct consumer_socket *cmd_socket_wrapper = NULL; |
1d4b027a | 421 | |
3bd1e081 | 422 | DBG("[thread] Manage consumer started"); |
1d4b027a | 423 | |
34c1e15a MD |
424 | rcu_register_thread(); |
425 | rcu_thread_online(); | |
426 | ||
6c71277b | 427 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER); |
927ca06a | 428 | |
855060f8 | 429 | health_code_update(); |
9449cc75 | 430 | |
5eb91c98 | 431 | /* |
331744e3 JD |
432 | * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the |
433 | * metadata_sock. Nothing more will be added to this poll set. | |
5eb91c98 | 434 | */ |
331744e3 | 435 | ret = sessiond_set_thread_pollset(&events, 3); |
5eb91c98 | 436 | if (ret < 0) { |
76d7553f | 437 | goto error_poll; |
5eb91c98 | 438 | } |
273ea72c | 439 | |
edb8b045 DG |
440 | /* |
441 | * The error socket here is already in a listening state which was done | |
442 | * just before spawning this thread to avoid a race between the consumer | |
443 | * daemon exec trying to connect and the listen() call. | |
444 | */ | |
3bd1e081 | 445 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
5eb91c98 DG |
446 | if (ret < 0) { |
447 | goto error; | |
448 | } | |
449 | ||
840cb59c | 450 | health_code_update(); |
44a5e5eb | 451 | |
331744e3 | 452 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 453 | restart: |
a78af745 | 454 | health_poll_entry(); |
8ac94142 | 455 | |
e547b070 | 456 | if (testpoint(sessiond_thread_manage_consumer)) { |
6993eeb3 CB |
457 | goto error; |
458 | } | |
8ac94142 | 459 | |
5eb91c98 | 460 | ret = lttng_poll_wait(&events, -1); |
a78af745 | 461 | health_poll_exit(); |
273ea72c | 462 | if (ret < 0) { |
88f2b785 MD |
463 | /* |
464 | * Restart interrupted system call. | |
465 | */ | |
466 | if (errno == EINTR) { | |
467 | goto restart; | |
468 | } | |
273ea72c DG |
469 | goto error; |
470 | } | |
471 | ||
0d9c5d77 DG |
472 | nb_fd = ret; |
473 | ||
5eb91c98 DG |
474 | for (i = 0; i < nb_fd; i++) { |
475 | /* Fetch once the poll data */ | |
476 | revents = LTTNG_POLL_GETEV(&events, i); | |
477 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
478 | ||
840cb59c | 479 | health_code_update(); |
44a5e5eb | 480 | |
fd20dac9 MD |
481 | if (!revents) { |
482 | /* No activity for this FD (poll implementation). */ | |
483 | continue; | |
484 | } | |
485 | ||
5eb91c98 | 486 | /* Thread quit pipe has been closed. Killing thread. */ |
d0b96690 | 487 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
5eb91c98 | 488 | if (ret) { |
139ac872 MD |
489 | err = 0; |
490 | goto exit; | |
5eb91c98 DG |
491 | } |
492 | ||
493 | /* Event on the registration socket */ | |
3bd1e081 | 494 | if (pollfd == consumer_data->err_sock) { |
03e43155 MD |
495 | if (revents & LPOLLIN) { |
496 | continue; | |
497 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3bd1e081 | 498 | ERR("consumer err socket poll error"); |
5eb91c98 | 499 | goto error; |
03e43155 MD |
500 | } else { |
501 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
502 | goto error; | |
5eb91c98 DG |
503 | } |
504 | } | |
273ea72c DG |
505 | } |
506 | ||
3bd1e081 | 507 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
508 | if (sock < 0) { |
509 | goto error; | |
510 | } | |
511 | ||
b662582b DG |
512 | /* |
513 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
514 | * show must go on. | |
515 | */ | |
516 | (void) utils_set_fd_cloexec(sock); | |
517 | ||
840cb59c | 518 | health_code_update(); |
44a5e5eb | 519 | |
3bd1e081 | 520 | DBG2("Receiving code from consumer err_sock"); |
ee0b0061 | 521 | |
712ea556 | 522 | /* Getting status code from kconsumerd */ |
54d01ffb DG |
523 | ret = lttcomm_recv_unix_sock(sock, &code, |
524 | sizeof(enum lttcomm_return_code)); | |
1d4b027a DG |
525 | if (ret <= 0) { |
526 | goto error; | |
527 | } | |
528 | ||
840cb59c | 529 | health_code_update(); |
b3530820 | 530 | if (code != LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) { |
3bd1e081 | 531 | ERR("consumer error when waiting for SOCK_READY : %s", |
1d4b027a DG |
532 | lttcomm_get_readable_code(-code)); |
533 | goto error; | |
534 | } | |
535 | ||
b3530820 JG |
536 | /* Connect both command and metadata sockets. */ |
537 | consumer_data->cmd_sock = | |
538 | lttcomm_connect_unix_sock( | |
539 | consumer_data->cmd_unix_sock_path); | |
540 | consumer_data->metadata_fd = | |
541 | lttcomm_connect_unix_sock( | |
542 | consumer_data->cmd_unix_sock_path); | |
543 | if (consumer_data->cmd_sock < 0 || consumer_data->metadata_fd < 0) { | |
544 | PERROR("consumer connect cmd socket"); | |
545 | /* On error, signal condition and quit. */ | |
546 | signal_consumer_condition(consumer_data, -1); | |
547 | goto error; | |
548 | } | |
549 | ||
550 | consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd; | |
551 | ||
552 | /* Create metadata socket lock. */ | |
553 | consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t)); | |
554 | if (consumer_data->metadata_sock.lock == NULL) { | |
555 | PERROR("zmalloc pthread mutex"); | |
556 | goto error; | |
557 | } | |
558 | pthread_mutex_init(consumer_data->metadata_sock.lock, NULL); | |
559 | ||
560 | DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock); | |
561 | DBG("Consumer metadata socket ready (fd: %d)", | |
562 | consumer_data->metadata_fd); | |
563 | ||
564 | /* | |
565 | * Remove the consumerd error sock since we've established a connection. | |
566 | */ | |
3bd1e081 | 567 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
72079cae | 568 | if (ret < 0) { |
72079cae DG |
569 | goto error; |
570 | } | |
571 | ||
331744e3 | 572 | /* Add new accepted error socket. */ |
5eb91c98 DG |
573 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
574 | if (ret < 0) { | |
72079cae | 575 | goto error; |
5eb91c98 DG |
576 | } |
577 | ||
331744e3 | 578 | /* Add metadata socket that is successfully connected. */ |
4ce514c4 | 579 | ret = lttng_poll_add(&events, consumer_data->metadata_fd, |
331744e3 JD |
580 | LPOLLIN | LPOLLRDHUP); |
581 | if (ret < 0) { | |
582 | goto error; | |
583 | } | |
584 | ||
840cb59c | 585 | health_code_update(); |
44a5e5eb | 586 | |
b3530820 | 587 | /* |
62c43103 | 588 | * Transfer the write-end of the channel monitoring and rotate pipe |
82528808 | 589 | * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command. |
b3530820 JG |
590 | */ |
591 | cmd_socket_wrapper = consumer_allocate_socket(&consumer_data->cmd_sock); | |
592 | if (!cmd_socket_wrapper) { | |
593 | goto error; | |
594 | } | |
3e4dc117 | 595 | cmd_socket_wrapper->lock = &consumer_data->lock; |
b3530820 JG |
596 | |
597 | ret = consumer_send_channel_monitor_pipe(cmd_socket_wrapper, | |
598 | consumer_data->channel_monitor_pipe); | |
599 | if (ret) { | |
600 | goto error; | |
601 | } | |
62c43103 | 602 | |
b3530820 JG |
603 | /* Discard the socket wrapper as it is no longer needed. */ |
604 | consumer_destroy_socket(cmd_socket_wrapper); | |
605 | cmd_socket_wrapper = NULL; | |
606 | ||
607 | /* The thread is completely initialized, signal that it is ready. */ | |
608 | signal_consumer_condition(consumer_data, 1); | |
609 | ||
331744e3 | 610 | /* Infinite blocking call, waiting for transmission */ |
88f2b785 | 611 | restart_poll: |
331744e3 | 612 | while (1) { |
42fc1d0b DG |
613 | health_code_update(); |
614 | ||
615 | /* Exit the thread because the thread quit pipe has been triggered. */ | |
616 | if (should_quit) { | |
617 | /* Not a health error. */ | |
618 | err = 0; | |
619 | goto exit; | |
620 | } | |
621 | ||
331744e3 JD |
622 | health_poll_entry(); |
623 | ret = lttng_poll_wait(&events, -1); | |
624 | health_poll_exit(); | |
625 | if (ret < 0) { | |
626 | /* | |
627 | * Restart interrupted system call. | |
628 | */ | |
629 | if (errno == EINTR) { | |
630 | goto restart_poll; | |
631 | } | |
632 | goto error; | |
88f2b785 | 633 | } |
72079cae | 634 | |
331744e3 | 635 | nb_fd = ret; |
0d9c5d77 | 636 | |
331744e3 JD |
637 | for (i = 0; i < nb_fd; i++) { |
638 | /* Fetch once the poll data */ | |
639 | revents = LTTNG_POLL_GETEV(&events, i); | |
640 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
5eb91c98 | 641 | |
331744e3 | 642 | health_code_update(); |
44a5e5eb | 643 | |
fd20dac9 MD |
644 | if (!revents) { |
645 | /* No activity for this FD (poll implementation). */ | |
646 | continue; | |
647 | } | |
648 | ||
42fc1d0b DG |
649 | /* |
650 | * Thread quit pipe has been triggered, flag that we should stop | |
651 | * but continue the current loop to handle potential data from | |
652 | * consumer. | |
653 | */ | |
654 | should_quit = sessiond_check_thread_quit_pipe(pollfd, revents); | |
5eb91c98 | 655 | |
331744e3 JD |
656 | if (pollfd == sock) { |
657 | /* Event on the consumerd socket */ | |
03e43155 MD |
658 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
659 | && !(revents & LPOLLIN)) { | |
331744e3 JD |
660 | ERR("consumer err socket second poll error"); |
661 | goto error; | |
662 | } | |
663 | health_code_update(); | |
664 | /* Wait for any kconsumerd error */ | |
665 | ret = lttcomm_recv_unix_sock(sock, &code, | |
666 | sizeof(enum lttcomm_return_code)); | |
667 | if (ret <= 0) { | |
668 | ERR("consumer closed the command socket"); | |
669 | goto error; | |
670 | } | |
671 | ||
672 | ERR("consumer return code : %s", | |
673 | lttcomm_get_readable_code(-code)); | |
674 | ||
675 | goto exit; | |
4ce514c4 | 676 | } else if (pollfd == consumer_data->metadata_fd) { |
03e43155 MD |
677 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
678 | && !(revents & LPOLLIN)) { | |
679 | ERR("consumer err metadata socket second poll error"); | |
680 | goto error; | |
681 | } | |
331744e3 JD |
682 | /* UST metadata requests */ |
683 | ret = ust_consumer_metadata_request( | |
684 | &consumer_data->metadata_sock); | |
685 | if (ret < 0) { | |
686 | ERR("Handling metadata request"); | |
687 | goto error; | |
688 | } | |
5eb91c98 | 689 | } |
42fc1d0b | 690 | /* No need for an else branch all FDs are tested prior. */ |
5eb91c98 | 691 | } |
331744e3 | 692 | health_code_update(); |
5eb91c98 DG |
693 | } |
694 | ||
139ac872 | 695 | exit: |
1d4b027a | 696 | error: |
fdadac08 DG |
697 | /* |
698 | * We lock here because we are about to close the sockets and some other | |
92db7cdc DG |
699 | * thread might be using them so get exclusive access which will abort all |
700 | * other consumer command by other threads. | |
fdadac08 DG |
701 | */ |
702 | pthread_mutex_lock(&consumer_data->lock); | |
703 | ||
5c827ce0 DG |
704 | /* Immediately set the consumerd state to stopped */ |
705 | if (consumer_data->type == LTTNG_CONSUMER_KERNEL) { | |
706 | uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR); | |
707 | } else if (consumer_data->type == LTTNG_CONSUMER64_UST || | |
708 | consumer_data->type == LTTNG_CONSUMER32_UST) { | |
709 | uatomic_set(&ust_consumerd_state, CONSUMER_ERROR); | |
710 | } else { | |
711 | /* Code flow error... */ | |
712 | assert(0); | |
713 | } | |
714 | ||
76d7553f MD |
715 | if (consumer_data->err_sock >= 0) { |
716 | ret = close(consumer_data->err_sock); | |
717 | if (ret) { | |
718 | PERROR("close"); | |
719 | } | |
a76cbd9f | 720 | consumer_data->err_sock = -1; |
76d7553f MD |
721 | } |
722 | if (consumer_data->cmd_sock >= 0) { | |
723 | ret = close(consumer_data->cmd_sock); | |
724 | if (ret) { | |
725 | PERROR("close"); | |
726 | } | |
a76cbd9f | 727 | consumer_data->cmd_sock = -1; |
76d7553f | 728 | } |
96544455 SS |
729 | if (consumer_data->metadata_sock.fd_ptr && |
730 | *consumer_data->metadata_sock.fd_ptr >= 0) { | |
9363801e | 731 | ret = close(*consumer_data->metadata_sock.fd_ptr); |
331744e3 JD |
732 | if (ret) { |
733 | PERROR("close"); | |
734 | } | |
735 | } | |
76d7553f MD |
736 | if (sock >= 0) { |
737 | ret = close(sock); | |
738 | if (ret) { | |
739 | PERROR("close"); | |
740 | } | |
741 | } | |
273ea72c | 742 | |
3bd1e081 MD |
743 | unlink(consumer_data->err_unix_sock_path); |
744 | unlink(consumer_data->cmd_unix_sock_path); | |
fdadac08 | 745 | pthread_mutex_unlock(&consumer_data->lock); |
92db7cdc | 746 | |
fdadac08 | 747 | /* Cleanup metadata socket mutex. */ |
96544455 SS |
748 | if (consumer_data->metadata_sock.lock) { |
749 | pthread_mutex_destroy(consumer_data->metadata_sock.lock); | |
750 | free(consumer_data->metadata_sock.lock); | |
751 | } | |
5eb91c98 | 752 | lttng_poll_clean(&events); |
b3530820 JG |
753 | |
754 | if (cmd_socket_wrapper) { | |
755 | consumer_destroy_socket(cmd_socket_wrapper); | |
756 | } | |
76d7553f | 757 | error_poll: |
139ac872 | 758 | if (err) { |
840cb59c | 759 | health_error(); |
139ac872 MD |
760 | ERR("Health error occurred in %s", __func__); |
761 | } | |
8782cc74 | 762 | health_unregister(health_sessiond); |
76d7553f | 763 | DBG("consumer thread cleanup completed"); |
0177d773 | 764 | |
34c1e15a MD |
765 | rcu_thread_offline(); |
766 | rcu_unregister_thread(); | |
767 | ||
5eb91c98 | 768 | return NULL; |
099e26bd DG |
769 | } |
770 | ||
b73401da | 771 | /* |
096102bd | 772 | * Setup necessary data for kernel tracer action. |
b73401da | 773 | */ |
096102bd | 774 | static int init_kernel_tracer(void) |
b73401da DG |
775 | { |
776 | int ret; | |
b73401da | 777 | |
096102bd DG |
778 | /* Modprobe lttng kernel modules */ |
779 | ret = modprobe_lttng_control(); | |
b73401da | 780 | if (ret < 0) { |
b73401da DG |
781 | goto error; |
782 | } | |
783 | ||
096102bd DG |
784 | /* Open debugfs lttng */ |
785 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); | |
786 | if (kernel_tracer_fd < 0) { | |
787 | DBG("Failed to open %s", module_proc_lttng); | |
2f77fc4b | 788 | goto error_open; |
54d01ffb DG |
789 | } |
790 | ||
2f77fc4b | 791 | /* Validate kernel version */ |
88076e89 JD |
792 | ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version, |
793 | &kernel_tracer_abi_version); | |
2f77fc4b DG |
794 | if (ret < 0) { |
795 | goto error_version; | |
b551a063 | 796 | } |
54d01ffb | 797 | |
2f77fc4b DG |
798 | ret = modprobe_lttng_data(); |
799 | if (ret < 0) { | |
800 | goto error_modules; | |
54d01ffb DG |
801 | } |
802 | ||
6e21424e JR |
803 | ret = kernel_supports_ring_buffer_snapshot_sample_positions( |
804 | kernel_tracer_fd); | |
805 | if (ret < 0) { | |
806 | goto error_modules; | |
807 | } | |
808 | ||
809 | if (ret < 1) { | |
810 | WARN("Kernel tracer does not support buffer monitoring. " | |
811 | "The monitoring timer of channels in the kernel domain " | |
812 | "will be set to 0 (disabled)."); | |
813 | } | |
814 | ||
2f77fc4b DG |
815 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
816 | return 0; | |
817 | ||
818 | error_version: | |
819 | modprobe_remove_lttng_control(); | |
820 | ret = close(kernel_tracer_fd); | |
821 | if (ret) { | |
822 | PERROR("close"); | |
b551a063 | 823 | } |
2f77fc4b | 824 | kernel_tracer_fd = -1; |
f73fabfd | 825 | return LTTNG_ERR_KERN_VERSION; |
b551a063 | 826 | |
2f77fc4b DG |
827 | error_modules: |
828 | ret = close(kernel_tracer_fd); | |
829 | if (ret) { | |
830 | PERROR("close"); | |
b551a063 | 831 | } |
54d01ffb | 832 | |
2f77fc4b DG |
833 | error_open: |
834 | modprobe_remove_lttng_control(); | |
54d01ffb DG |
835 | |
836 | error: | |
2f77fc4b DG |
837 | WARN("No kernel tracer available"); |
838 | kernel_tracer_fd = -1; | |
839 | if (!is_root) { | |
f73fabfd | 840 | return LTTNG_ERR_NEED_ROOT_SESSIOND; |
2f77fc4b | 841 | } else { |
f73fabfd | 842 | return LTTNG_ERR_KERN_NA; |
2f77fc4b | 843 | } |
54d01ffb DG |
844 | } |
845 | ||
050e613c | 846 | static int string_match(const char *str1, const char *str2) |
54d01ffb | 847 | { |
050e613c | 848 | return (str1 && str2) && !strcmp(str1, str2); |
54d01ffb DG |
849 | } |
850 | ||
00e2e675 | 851 | /* |
050e613c JG |
852 | * Take an option from the getopt output and set it in the right variable to be |
853 | * used later. | |
36b588ed | 854 | * |
050e613c | 855 | * Return 0 on success else a negative value. |
00e2e675 | 856 | */ |
050e613c | 857 | static int set_option(int opt, const char *arg, const char *optname) |
00e2e675 | 858 | { |
050e613c | 859 | int ret = 0; |
fac6795d | 860 | |
c0232ea5 | 861 | if (string_match(optname, "client-sock") || opt == 'c') { |
66b2ce8e JG |
862 | if (!arg || *arg == '\0') { |
863 | ret = -EINVAL; | |
864 | goto end; | |
865 | } | |
e8fa9fb0 MD |
866 | if (lttng_is_setuid_setgid()) { |
867 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
868 | "-c, --client-sock"); | |
869 | } else { | |
e6142f2e JG |
870 | config_string_set(&config.client_unix_sock_path, |
871 | strdup(arg)); | |
872 | if (!config.client_unix_sock_path.value) { | |
873 | ret = -ENOMEM; | |
874 | PERROR("strdup"); | |
875 | } | |
e8fa9fb0 | 876 | } |
c0232ea5 | 877 | } else if (string_match(optname, "apps-sock") || opt == 'a') { |
66b2ce8e JG |
878 | if (!arg || *arg == '\0') { |
879 | ret = -EINVAL; | |
880 | goto end; | |
881 | } | |
e8fa9fb0 MD |
882 | if (lttng_is_setuid_setgid()) { |
883 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
884 | "-a, --apps-sock"); | |
885 | } else { | |
e6142f2e JG |
886 | config_string_set(&config.apps_unix_sock_path, |
887 | strdup(arg)); | |
888 | if (!config.apps_unix_sock_path.value) { | |
889 | ret = -ENOMEM; | |
890 | PERROR("strdup"); | |
891 | } | |
e8fa9fb0 | 892 | } |
c0232ea5 | 893 | } else if (string_match(optname, "daemonize") || opt == 'd') { |
e6142f2e | 894 | config.daemonize = true; |
c0232ea5 | 895 | } else if (string_match(optname, "background") || opt == 'b') { |
e6142f2e | 896 | config.background = true; |
c0232ea5 | 897 | } else if (string_match(optname, "group") || opt == 'g') { |
66b2ce8e JG |
898 | if (!arg || *arg == '\0') { |
899 | ret = -EINVAL; | |
900 | goto end; | |
901 | } | |
e8fa9fb0 MD |
902 | if (lttng_is_setuid_setgid()) { |
903 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
904 | "-g, --group"); | |
905 | } else { | |
e6142f2e JG |
906 | config_string_set(&config.tracing_group_name, |
907 | strdup(arg)); | |
908 | if (!config.tracing_group_name.value) { | |
e8fa9fb0 | 909 | ret = -ENOMEM; |
e6142f2e | 910 | PERROR("strdup"); |
e8fa9fb0 | 911 | } |
db322c4d | 912 | } |
c0232ea5 | 913 | } else if (string_match(optname, "help") || opt == 'h') { |
4fc83d94 | 914 | ret = utils_show_help(8, "lttng-sessiond", help_msg); |
8190767e | 915 | if (ret) { |
4fc83d94 | 916 | ERR("Cannot show --help for `lttng-sessiond`"); |
8190767e PP |
917 | perror("exec"); |
918 | } | |
919 | exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); | |
c0232ea5 | 920 | } else if (string_match(optname, "version") || opt == 'V') { |
26296c48 JG |
921 | fprintf(stdout, "%s\n", VERSION); |
922 | exit(EXIT_SUCCESS); | |
c0232ea5 | 923 | } else if (string_match(optname, "sig-parent") || opt == 'S') { |
e6142f2e | 924 | config.sig_parent = true; |
c0232ea5 | 925 | } else if (string_match(optname, "kconsumerd-err-sock")) { |
66b2ce8e JG |
926 | if (!arg || *arg == '\0') { |
927 | ret = -EINVAL; | |
928 | goto end; | |
929 | } | |
e8fa9fb0 MD |
930 | if (lttng_is_setuid_setgid()) { |
931 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
932 | "--kconsumerd-err-sock"); | |
933 | } else { | |
e6142f2e JG |
934 | config_string_set(&config.kconsumerd_err_unix_sock_path, |
935 | strdup(arg)); | |
936 | if (!config.kconsumerd_err_unix_sock_path.value) { | |
937 | ret = -ENOMEM; | |
938 | PERROR("strdup"); | |
939 | } | |
e8fa9fb0 | 940 | } |
c0232ea5 | 941 | } else if (string_match(optname, "kconsumerd-cmd-sock")) { |
66b2ce8e JG |
942 | if (!arg || *arg == '\0') { |
943 | ret = -EINVAL; | |
944 | goto end; | |
945 | } | |
e8fa9fb0 MD |
946 | if (lttng_is_setuid_setgid()) { |
947 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
948 | "--kconsumerd-cmd-sock"); | |
949 | } else { | |
e6142f2e JG |
950 | config_string_set(&config.kconsumerd_cmd_unix_sock_path, |
951 | strdup(arg)); | |
952 | if (!config.kconsumerd_cmd_unix_sock_path.value) { | |
953 | ret = -ENOMEM; | |
954 | PERROR("strdup"); | |
955 | } | |
e8fa9fb0 | 956 | } |
c0232ea5 | 957 | } else if (string_match(optname, "ustconsumerd64-err-sock")) { |
66b2ce8e JG |
958 | if (!arg || *arg == '\0') { |
959 | ret = -EINVAL; | |
960 | goto end; | |
961 | } | |
e8fa9fb0 MD |
962 | if (lttng_is_setuid_setgid()) { |
963 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
964 | "--ustconsumerd64-err-sock"); | |
965 | } else { | |
e6142f2e JG |
966 | config_string_set(&config.consumerd64_err_unix_sock_path, |
967 | strdup(arg)); | |
968 | if (!config.consumerd64_err_unix_sock_path.value) { | |
969 | ret = -ENOMEM; | |
970 | PERROR("strdup"); | |
971 | } | |
e8fa9fb0 | 972 | } |
c0232ea5 | 973 | } else if (string_match(optname, "ustconsumerd64-cmd-sock")) { |
66b2ce8e JG |
974 | if (!arg || *arg == '\0') { |
975 | ret = -EINVAL; | |
976 | goto end; | |
977 | } | |
e8fa9fb0 MD |
978 | if (lttng_is_setuid_setgid()) { |
979 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
980 | "--ustconsumerd64-cmd-sock"); | |
981 | } else { | |
e6142f2e JG |
982 | config_string_set(&config.consumerd64_cmd_unix_sock_path, |
983 | strdup(arg)); | |
984 | if (!config.consumerd64_cmd_unix_sock_path.value) { | |
985 | ret = -ENOMEM; | |
986 | PERROR("strdup"); | |
987 | } | |
e8fa9fb0 | 988 | } |
c0232ea5 | 989 | } else if (string_match(optname, "ustconsumerd32-err-sock")) { |
66b2ce8e JG |
990 | if (!arg || *arg == '\0') { |
991 | ret = -EINVAL; | |
992 | goto end; | |
993 | } | |
e8fa9fb0 MD |
994 | if (lttng_is_setuid_setgid()) { |
995 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
996 | "--ustconsumerd32-err-sock"); | |
997 | } else { | |
e6142f2e JG |
998 | config_string_set(&config.consumerd32_err_unix_sock_path, |
999 | strdup(arg)); | |
1000 | if (!config.consumerd32_err_unix_sock_path.value) { | |
1001 | ret = -ENOMEM; | |
1002 | PERROR("strdup"); | |
1003 | } | |
e8fa9fb0 | 1004 | } |
c0232ea5 | 1005 | } else if (string_match(optname, "ustconsumerd32-cmd-sock")) { |
66b2ce8e JG |
1006 | if (!arg || *arg == '\0') { |
1007 | ret = -EINVAL; | |
1008 | goto end; | |
1009 | } | |
e8fa9fb0 MD |
1010 | if (lttng_is_setuid_setgid()) { |
1011 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1012 | "--ustconsumerd32-cmd-sock"); | |
1013 | } else { | |
e6142f2e JG |
1014 | config_string_set(&config.consumerd32_cmd_unix_sock_path, |
1015 | strdup(arg)); | |
1016 | if (!config.consumerd32_cmd_unix_sock_path.value) { | |
1017 | ret = -ENOMEM; | |
1018 | PERROR("strdup"); | |
1019 | } | |
e8fa9fb0 | 1020 | } |
c0232ea5 | 1021 | } else if (string_match(optname, "no-kernel")) { |
e6142f2e | 1022 | config.no_kernel = true; |
c0232ea5 | 1023 | } else if (string_match(optname, "quiet") || opt == 'q') { |
6d9a9c65 | 1024 | config.quiet = true; |
c0232ea5 | 1025 | } else if (string_match(optname, "verbose") || opt == 'v') { |
26296c48 JG |
1026 | /* Verbose level can increase using multiple -v */ |
1027 | if (arg) { | |
13755a18 | 1028 | /* Value obtained from config file */ |
e6142f2e | 1029 | config.verbose = config_parse_value(arg); |
26296c48 | 1030 | } else { |
13755a18 | 1031 | /* -v used on command line */ |
e6142f2e | 1032 | config.verbose++; |
26296c48 | 1033 | } |
13755a18 | 1034 | /* Clamp value to [0, 3] */ |
e6142f2e JG |
1035 | config.verbose = config.verbose < 0 ? 0 : |
1036 | (config.verbose <= 3 ? config.verbose : 3); | |
c0232ea5 | 1037 | } else if (string_match(optname, "verbose-consumer")) { |
26296c48 | 1038 | if (arg) { |
e6142f2e | 1039 | config.verbose_consumer = config_parse_value(arg); |
26296c48 | 1040 | } else { |
e6142f2e | 1041 | config.verbose_consumer++; |
26296c48 | 1042 | } |
c0232ea5 | 1043 | } else if (string_match(optname, "consumerd32-path")) { |
66b2ce8e JG |
1044 | if (!arg || *arg == '\0') { |
1045 | ret = -EINVAL; | |
1046 | goto end; | |
1047 | } | |
e8fa9fb0 MD |
1048 | if (lttng_is_setuid_setgid()) { |
1049 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1050 | "--consumerd32-path"); | |
1051 | } else { | |
e6142f2e JG |
1052 | config_string_set(&config.consumerd32_bin_path, |
1053 | strdup(arg)); | |
1054 | if (!config.consumerd32_bin_path.value) { | |
e8fa9fb0 MD |
1055 | PERROR("strdup"); |
1056 | ret = -ENOMEM; | |
1057 | } | |
db322c4d | 1058 | } |
c0232ea5 | 1059 | } else if (string_match(optname, "consumerd32-libdir")) { |
66b2ce8e JG |
1060 | if (!arg || *arg == '\0') { |
1061 | ret = -EINVAL; | |
1062 | goto end; | |
1063 | } | |
e8fa9fb0 MD |
1064 | if (lttng_is_setuid_setgid()) { |
1065 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1066 | "--consumerd32-libdir"); | |
1067 | } else { | |
e6142f2e JG |
1068 | config_string_set(&config.consumerd32_lib_dir, |
1069 | strdup(arg)); | |
1070 | if (!config.consumerd32_lib_dir.value) { | |
e8fa9fb0 MD |
1071 | PERROR("strdup"); |
1072 | ret = -ENOMEM; | |
1073 | } | |
db322c4d | 1074 | } |
c0232ea5 | 1075 | } else if (string_match(optname, "consumerd64-path")) { |
66b2ce8e JG |
1076 | if (!arg || *arg == '\0') { |
1077 | ret = -EINVAL; | |
1078 | goto end; | |
1079 | } | |
e8fa9fb0 MD |
1080 | if (lttng_is_setuid_setgid()) { |
1081 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1082 | "--consumerd64-path"); | |
1083 | } else { | |
e6142f2e JG |
1084 | config_string_set(&config.consumerd64_bin_path, |
1085 | strdup(arg)); | |
1086 | if (!config.consumerd64_bin_path.value) { | |
e8fa9fb0 MD |
1087 | PERROR("strdup"); |
1088 | ret = -ENOMEM; | |
1089 | } | |
db322c4d | 1090 | } |
c0232ea5 | 1091 | } else if (string_match(optname, "consumerd64-libdir")) { |
66b2ce8e JG |
1092 | if (!arg || *arg == '\0') { |
1093 | ret = -EINVAL; | |
1094 | goto end; | |
1095 | } | |
e8fa9fb0 MD |
1096 | if (lttng_is_setuid_setgid()) { |
1097 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1098 | "--consumerd64-libdir"); | |
1099 | } else { | |
e6142f2e JG |
1100 | config_string_set(&config.consumerd64_lib_dir, |
1101 | strdup(arg)); | |
1102 | if (!config.consumerd64_lib_dir.value) { | |
e8fa9fb0 MD |
1103 | PERROR("strdup"); |
1104 | ret = -ENOMEM; | |
1105 | } | |
db322c4d | 1106 | } |
c0232ea5 | 1107 | } else if (string_match(optname, "pidfile") || opt == 'p') { |
66b2ce8e JG |
1108 | if (!arg || *arg == '\0') { |
1109 | ret = -EINVAL; | |
1110 | goto end; | |
1111 | } | |
e8fa9fb0 MD |
1112 | if (lttng_is_setuid_setgid()) { |
1113 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1114 | "-p, --pidfile"); | |
1115 | } else { | |
e6142f2e JG |
1116 | config_string_set(&config.pid_file_path, strdup(arg)); |
1117 | if (!config.pid_file_path.value) { | |
e8fa9fb0 MD |
1118 | PERROR("strdup"); |
1119 | ret = -ENOMEM; | |
1120 | } | |
db322c4d | 1121 | } |
c0232ea5 | 1122 | } else if (string_match(optname, "agent-tcp-port")) { |
66b2ce8e JG |
1123 | if (!arg || *arg == '\0') { |
1124 | ret = -EINVAL; | |
1125 | goto end; | |
1126 | } | |
e8fa9fb0 MD |
1127 | if (lttng_is_setuid_setgid()) { |
1128 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1129 | "--agent-tcp-port"); | |
1130 | } else { | |
1131 | unsigned long v; | |
4d076222 | 1132 | |
e8fa9fb0 MD |
1133 | errno = 0; |
1134 | v = strtoul(arg, NULL, 0); | |
1135 | if (errno != 0 || !isdigit(arg[0])) { | |
1136 | ERR("Wrong value in --agent-tcp-port parameter: %s", arg); | |
1137 | return -1; | |
1138 | } | |
1139 | if (v == 0 || v >= 65535) { | |
1140 | ERR("Port overflow in --agent-tcp-port parameter: %s", arg); | |
1141 | return -1; | |
1142 | } | |
2288467f JG |
1143 | config.agent_tcp_port.begin = config.agent_tcp_port.end = (int) v; |
1144 | DBG3("Agent TCP port set to non default: %i", (int) v); | |
26296c48 | 1145 | } |
c0232ea5 | 1146 | } else if (string_match(optname, "load") || opt == 'l') { |
66b2ce8e JG |
1147 | if (!arg || *arg == '\0') { |
1148 | ret = -EINVAL; | |
1149 | goto end; | |
1150 | } | |
e8fa9fb0 MD |
1151 | if (lttng_is_setuid_setgid()) { |
1152 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1153 | "-l, --load"); | |
1154 | } else { | |
e6142f2e JG |
1155 | config_string_set(&config.load_session_path, strdup(arg)); |
1156 | if (!config.load_session_path.value) { | |
e8fa9fb0 MD |
1157 | PERROR("strdup"); |
1158 | ret = -ENOMEM; | |
1159 | } | |
ef367a93 | 1160 | } |
c0232ea5 | 1161 | } else if (string_match(optname, "kmod-probes")) { |
66b2ce8e JG |
1162 | if (!arg || *arg == '\0') { |
1163 | ret = -EINVAL; | |
1164 | goto end; | |
1165 | } | |
e8fa9fb0 MD |
1166 | if (lttng_is_setuid_setgid()) { |
1167 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1168 | "--kmod-probes"); | |
1169 | } else { | |
e6142f2e JG |
1170 | config_string_set(&config.kmod_probes_list, strdup(arg)); |
1171 | if (!config.kmod_probes_list.value) { | |
e8fa9fb0 MD |
1172 | PERROR("strdup"); |
1173 | ret = -ENOMEM; | |
1174 | } | |
c9d42407 | 1175 | } |
c0232ea5 | 1176 | } else if (string_match(optname, "extra-kmod-probes")) { |
66b2ce8e JG |
1177 | if (!arg || *arg == '\0') { |
1178 | ret = -EINVAL; | |
1179 | goto end; | |
1180 | } | |
e8fa9fb0 MD |
1181 | if (lttng_is_setuid_setgid()) { |
1182 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1183 | "--extra-kmod-probes"); | |
1184 | } else { | |
e6142f2e JG |
1185 | config_string_set(&config.kmod_extra_probes_list, |
1186 | strdup(arg)); | |
1187 | if (!config.kmod_extra_probes_list.value) { | |
e8fa9fb0 MD |
1188 | PERROR("strdup"); |
1189 | ret = -ENOMEM; | |
1190 | } | |
db322c4d | 1191 | } |
c0232ea5 JG |
1192 | } else if (string_match(optname, "config") || opt == 'f') { |
1193 | /* This is handled in set_options() thus silent skip. */ | |
1194 | goto end; | |
1195 | } else { | |
26296c48 JG |
1196 | /* Unknown option or other error. |
1197 | * Error is printed by getopt, just return */ | |
1198 | ret = -1; | |
1199 | } | |
1200 | ||
f3dd7ce7 | 1201 | end: |
c5d350b2 JG |
1202 | if (ret == -EINVAL) { |
1203 | const char *opt_name = "unknown"; | |
1204 | int i; | |
1205 | ||
1206 | for (i = 0; i < sizeof(long_options) / sizeof(struct option); | |
1207 | i++) { | |
1208 | if (opt == long_options[i].val) { | |
1209 | opt_name = long_options[i].name; | |
1210 | break; | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | WARN("Invalid argument provided for option \"%s\", using default value.", | |
1215 | opt_name); | |
1216 | } | |
f3dd7ce7 | 1217 | |
26296c48 JG |
1218 | return ret; |
1219 | } | |
1220 | ||
1221 | /* | |
1222 | * config_entry_handler_cb used to handle options read from a config file. | |
f40ef1d5 | 1223 | * See config_entry_handler_cb comment in common/config/session-config.h for the |
26296c48 JG |
1224 | * return value conventions. |
1225 | */ | |
1226 | static int config_entry_handler(const struct config_entry *entry, void *unused) | |
1227 | { | |
1228 | int ret = 0, i; | |
1229 | ||
1230 | if (!entry || !entry->name || !entry->value) { | |
1231 | ret = -EINVAL; | |
1232 | goto end; | |
1233 | } | |
1234 | ||
1235 | /* Check if the option is to be ignored */ | |
1236 | for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) { | |
1237 | if (!strcmp(entry->name, config_ignore_options[i])) { | |
1238 | goto end; | |
1239 | } | |
1240 | } | |
1241 | ||
1242 | for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; | |
1243 | i++) { | |
1244 | ||
1245 | /* Ignore if not fully matched. */ | |
1246 | if (strcmp(entry->name, long_options[i].name)) { | |
1247 | continue; | |
1248 | } | |
1249 | ||
1250 | /* | |
1251 | * If the option takes no argument on the command line, we have to | |
1252 | * check if the value is "true". We support non-zero numeric values, | |
1253 | * true, on and yes. | |
1254 | */ | |
1255 | if (!long_options[i].has_arg) { | |
1256 | ret = config_parse_value(entry->value); | |
1257 | if (ret <= 0) { | |
1258 | if (ret) { | |
1259 | WARN("Invalid configuration value \"%s\" for option %s", | |
1260 | entry->value, entry->name); | |
1261 | } | |
1262 | /* False, skip boolean config option. */ | |
1263 | goto end; | |
4d076222 | 1264 | } |
26296c48 JG |
1265 | } |
1266 | ||
1267 | ret = set_option(long_options[i].val, entry->value, entry->name); | |
1268 | goto end; | |
1269 | } | |
1270 | ||
1271 | WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name); | |
1272 | ||
1273 | end: | |
1274 | return ret; | |
1275 | } | |
1276 | ||
1277 | /* | |
1278 | * daemon configuration loading and argument parsing | |
1279 | */ | |
1280 | static int set_options(int argc, char **argv) | |
1281 | { | |
1282 | int ret = 0, c = 0, option_index = 0; | |
1283 | int orig_optopt = optopt, orig_optind = optind; | |
1284 | char *optstring; | |
1285 | const char *config_path = NULL; | |
1286 | ||
1287 | optstring = utils_generate_optstring(long_options, | |
1288 | sizeof(long_options) / sizeof(struct option)); | |
1289 | if (!optstring) { | |
1290 | ret = -ENOMEM; | |
1291 | goto end; | |
1292 | } | |
1293 | ||
1294 | /* Check for the --config option */ | |
1295 | while ((c = getopt_long(argc, argv, optstring, long_options, | |
1296 | &option_index)) != -1) { | |
1297 | if (c == '?') { | |
1298 | ret = -EINVAL; | |
1299 | goto end; | |
1300 | } else if (c != 'f') { | |
1301 | /* if not equal to --config option. */ | |
1302 | continue; | |
1303 | } | |
1304 | ||
e8fa9fb0 MD |
1305 | if (lttng_is_setuid_setgid()) { |
1306 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", | |
1307 | "-f, --config"); | |
1308 | } else { | |
1309 | config_path = utils_expand_path(optarg); | |
1310 | if (!config_path) { | |
1311 | ERR("Failed to resolve path: %s", optarg); | |
1312 | } | |
26296c48 JG |
1313 | } |
1314 | } | |
1315 | ||
1316 | ret = config_get_section_entries(config_path, config_section_name, | |
1317 | config_entry_handler, NULL); | |
1318 | if (ret) { | |
1319 | if (ret > 0) { | |
1320 | ERR("Invalid configuration option at line %i", ret); | |
1321 | ret = -1; | |
1322 | } | |
1323 | goto end; | |
1324 | } | |
1325 | ||
1326 | /* Reset getopt's global state */ | |
1327 | optopt = orig_optopt; | |
1328 | optind = orig_optind; | |
1329 | while (1) { | |
c0232ea5 JG |
1330 | option_index = -1; |
1331 | /* | |
1332 | * getopt_long() will not set option_index if it encounters a | |
1333 | * short option. | |
1334 | */ | |
1335 | c = getopt_long(argc, argv, optstring, long_options, | |
1336 | &option_index); | |
26296c48 | 1337 | if (c == -1) { |
4d076222 DG |
1338 | break; |
1339 | } | |
26296c48 | 1340 | |
c0232ea5 JG |
1341 | /* |
1342 | * Pass NULL as the long option name if popt left the index | |
1343 | * unset. | |
1344 | */ | |
1345 | ret = set_option(c, optarg, | |
1346 | option_index < 0 ? NULL : | |
1347 | long_options[option_index].name); | |
26296c48 JG |
1348 | if (ret < 0) { |
1349 | break; | |
fac6795d DG |
1350 | } |
1351 | } | |
1352 | ||
26296c48 JG |
1353 | end: |
1354 | free(optstring); | |
1355 | return ret; | |
fac6795d DG |
1356 | } |
1357 | ||
f472090a JG |
1358 | /* |
1359 | * Create lockfile using the rundir and return its fd. | |
1360 | */ | |
1361 | static int create_lockfile(void) | |
1362 | { | |
1363 | return utils_create_lock_file(config.lock_file_path.value); | |
1364 | } | |
1365 | ||
fac6795d | 1366 | /* |
54d01ffb DG |
1367 | * Check if the global socket is available, and if a daemon is answering at the |
1368 | * other side. If yes, error is returned. | |
f472090a JG |
1369 | * |
1370 | * Also attempts to create and hold the lock file. | |
fac6795d | 1371 | */ |
cf3af59e | 1372 | static int check_existing_daemon(void) |
fac6795d | 1373 | { |
f472090a JG |
1374 | int ret = 0; |
1375 | ||
7d8234d9 | 1376 | /* Is there anybody out there ? */ |
099e26bd | 1377 | if (lttng_session_daemon_alive()) { |
f472090a JG |
1378 | ret = -EEXIST; |
1379 | goto end; | |
099e26bd | 1380 | } |
b09c7c76 | 1381 | |
f472090a JG |
1382 | lockfile_fd = create_lockfile(); |
1383 | if (lockfile_fd < 0) { | |
1384 | ret = -EEXIST; | |
1385 | goto end; | |
1386 | } | |
1387 | end: | |
1388 | return ret; | |
1389 | } | |
1390 | ||
1391 | static void sessiond_cleanup_lock_file(void) | |
1392 | { | |
1393 | int ret; | |
1394 | ||
1395 | /* | |
1396 | * Cleanup lock file by deleting it and finaly closing it which will | |
1397 | * release the file system lock. | |
1398 | */ | |
1399 | if (lockfile_fd >= 0) { | |
1400 | ret = remove(config.lock_file_path.value); | |
1401 | if (ret < 0) { | |
1402 | PERROR("remove lock file"); | |
1403 | } | |
1404 | ret = close(lockfile_fd); | |
1405 | if (ret < 0) { | |
1406 | PERROR("close lock file"); | |
1407 | } | |
1408 | } | |
fac6795d DG |
1409 | } |
1410 | ||
fac6795d | 1411 | /* |
d063d709 | 1412 | * Set the tracing group gid onto the client socket. |
5e16da05 | 1413 | * |
d063d709 | 1414 | * Race window between mkdir and chown is OK because we are going from more |
d1613cf5 | 1415 | * permissive (root.root) to less permissive (root.tracing). |
fac6795d | 1416 | */ |
be040666 | 1417 | static int set_permissions(char *rundir) |
fac6795d DG |
1418 | { |
1419 | int ret; | |
996b65c8 | 1420 | gid_t gid; |
fac6795d | 1421 | |
e6142f2e | 1422 | gid = utils_get_group_id(config.tracing_group_name.value); |
fac6795d | 1423 | |
d6f42150 | 1424 | /* Set lttng run dir */ |
be040666 | 1425 | ret = chown(rundir, 0, gid); |
d6f42150 | 1426 | if (ret < 0) { |
be040666 | 1427 | ERR("Unable to set group on %s", rundir); |
76d7553f | 1428 | PERROR("chown"); |
d6f42150 DG |
1429 | } |
1430 | ||
6c71277b MD |
1431 | /* |
1432 | * Ensure all applications and tracing group can search the run | |
1433 | * dir. Allow everyone to read the directory, since it does not | |
1434 | * buy us anything to hide its content. | |
1435 | */ | |
1436 | ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); | |
d1613cf5 JN |
1437 | if (ret < 0) { |
1438 | ERR("Unable to set permissions on %s", rundir); | |
76d7553f | 1439 | PERROR("chmod"); |
d1613cf5 JN |
1440 | } |
1441 | ||
d6f42150 | 1442 | /* lttng client socket path */ |
e6142f2e | 1443 | ret = chown(config.client_unix_sock_path.value, 0, gid); |
fac6795d | 1444 | if (ret < 0) { |
e6142f2e | 1445 | ERR("Unable to set group on %s", config.client_unix_sock_path.value); |
76d7553f | 1446 | PERROR("chown"); |
d6f42150 DG |
1447 | } |
1448 | ||
3bd1e081 | 1449 | /* kconsumer error socket path */ |
6c71277b | 1450 | ret = chown(kconsumer_data.err_unix_sock_path, 0, 0); |
d6f42150 | 1451 | if (ret < 0) { |
3bd1e081 | 1452 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
76d7553f | 1453 | PERROR("chown"); |
3bd1e081 MD |
1454 | } |
1455 | ||
7753dea8 | 1456 | /* 64-bit ustconsumer error socket path */ |
6c71277b | 1457 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0); |
7753dea8 MD |
1458 | if (ret < 0) { |
1459 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); | |
76d7553f | 1460 | PERROR("chown"); |
7753dea8 MD |
1461 | } |
1462 | ||
1463 | /* 32-bit ustconsumer compat32 error socket path */ | |
6c71277b | 1464 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0); |
3bd1e081 | 1465 | if (ret < 0) { |
7753dea8 | 1466 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
76d7553f | 1467 | PERROR("chown"); |
fac6795d DG |
1468 | } |
1469 | ||
d6f42150 | 1470 | DBG("All permissions are set"); |
e07ae692 | 1471 | |
fac6795d DG |
1472 | return ret; |
1473 | } | |
1474 | ||
d6f42150 | 1475 | /* |
d063d709 | 1476 | * Create the lttng run directory needed for all global sockets and pipe. |
d6f42150 | 1477 | */ |
e6142f2e | 1478 | static int create_lttng_rundir(void) |
d6f42150 DG |
1479 | { |
1480 | int ret; | |
1481 | ||
e6142f2e | 1482 | DBG3("Creating LTTng run directory: %s", config.rundir.value); |
67e40797 | 1483 | |
e6142f2e | 1484 | ret = mkdir(config.rundir.value, S_IRWXU); |
d6f42150 | 1485 | if (ret < 0) { |
b1f11e69 | 1486 | if (errno != EEXIST) { |
e6142f2e | 1487 | ERR("Unable to create %s", config.rundir.value); |
b1f11e69 DG |
1488 | goto error; |
1489 | } else { | |
1490 | ret = 0; | |
1491 | } | |
d6f42150 DG |
1492 | } |
1493 | ||
1494 | error: | |
1495 | return ret; | |
1496 | } | |
1497 | ||
1498 | /* | |
e6142f2e | 1499 | * Setup sockets and directory needed by the consumerds' communication with the |
d063d709 | 1500 | * session daemon. |
d6f42150 | 1501 | */ |
e6142f2e | 1502 | static int set_consumer_sockets(struct consumer_data *consumer_data) |
d6f42150 DG |
1503 | { |
1504 | int ret; | |
e6142f2e | 1505 | char *path = NULL; |
d6f42150 | 1506 | |
6c71277b | 1507 | switch (consumer_data->type) { |
7753dea8 | 1508 | case LTTNG_CONSUMER_KERNEL: |
e6142f2e | 1509 | path = config.kconsumerd_path.value; |
7753dea8 MD |
1510 | break; |
1511 | case LTTNG_CONSUMER64_UST: | |
e6142f2e | 1512 | path = config.consumerd64_path.value; |
7753dea8 MD |
1513 | break; |
1514 | case LTTNG_CONSUMER32_UST: | |
e6142f2e | 1515 | path = config.consumerd32_path.value; |
7753dea8 MD |
1516 | break; |
1517 | default: | |
1518 | ERR("Consumer type unknown"); | |
1519 | ret = -EINVAL; | |
1520 | goto error; | |
d6f42150 | 1521 | } |
e6142f2e | 1522 | assert(path); |
d6f42150 | 1523 | |
67e40797 DG |
1524 | DBG2("Creating consumer directory: %s", path); |
1525 | ||
6c71277b | 1526 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP); |
e7e1ef17 JR |
1527 | if (ret < 0 && errno != EEXIST) { |
1528 | PERROR("mkdir"); | |
1529 | ERR("Failed to create %s", path); | |
1530 | goto error; | |
d6f42150 | 1531 | } |
6c71277b | 1532 | if (is_root) { |
e6142f2e | 1533 | ret = chown(path, 0, utils_get_group_id(config.tracing_group_name.value)); |
6c71277b MD |
1534 | if (ret < 0) { |
1535 | ERR("Unable to set group on %s", path); | |
1536 | PERROR("chown"); | |
1537 | goto error; | |
1538 | } | |
1539 | } | |
d6f42150 | 1540 | |
e6142f2e | 1541 | /* Create the consumerd error unix socket */ |
3bd1e081 MD |
1542 | consumer_data->err_sock = |
1543 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); | |
1544 | if (consumer_data->err_sock < 0) { | |
1545 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); | |
d6f42150 DG |
1546 | ret = -1; |
1547 | goto error; | |
1548 | } | |
1549 | ||
a24f05ab MD |
1550 | /* |
1551 | * Set the CLOEXEC flag. Return code is useless because either way, the | |
1552 | * show must go on. | |
1553 | */ | |
1554 | ret = utils_set_fd_cloexec(consumer_data->err_sock); | |
1555 | if (ret < 0) { | |
1556 | PERROR("utils_set_fd_cloexec"); | |
1557 | /* continue anyway */ | |
1558 | } | |
1559 | ||
d6f42150 | 1560 | /* File permission MUST be 660 */ |
3bd1e081 | 1561 | ret = chmod(consumer_data->err_unix_sock_path, |
54d01ffb | 1562 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
d6f42150 | 1563 | if (ret < 0) { |
3bd1e081 | 1564 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
67e40797 | 1565 | PERROR("chmod"); |
d6f42150 DG |
1566 | goto error; |
1567 | } | |
1568 | ||
1569 | error: | |
1570 | return ret; | |
1571 | } | |
1572 | ||
fac6795d | 1573 | /* |
d063d709 | 1574 | * Signal handler for the daemon |
cf3af59e | 1575 | * |
54d01ffb DG |
1576 | * Simply stop all worker threads, leaving main() return gracefully after |
1577 | * joining all threads and calling cleanup(). | |
fac6795d DG |
1578 | */ |
1579 | static void sighandler(int sig) | |
1580 | { | |
1581 | switch (sig) { | |
cf3af59e | 1582 | case SIGINT: |
af87c45a | 1583 | DBG("SIGINT caught"); |
cf3af59e MD |
1584 | stop_threads(); |
1585 | break; | |
1586 | case SIGTERM: | |
af87c45a | 1587 | DBG("SIGTERM caught"); |
cf3af59e MD |
1588 | stop_threads(); |
1589 | break; | |
0bb7724a DG |
1590 | case SIGUSR1: |
1591 | CMM_STORE_SHARED(recv_child_signal, 1); | |
1592 | break; | |
cf3af59e MD |
1593 | default: |
1594 | break; | |
fac6795d | 1595 | } |
fac6795d DG |
1596 | } |
1597 | ||
1598 | /* | |
d063d709 | 1599 | * Setup signal handler for : |
1d4b027a | 1600 | * SIGINT, SIGTERM, SIGPIPE |
fac6795d | 1601 | */ |
1d4b027a | 1602 | static int set_signal_handler(void) |
fac6795d | 1603 | { |
1d4b027a DG |
1604 | int ret = 0; |
1605 | struct sigaction sa; | |
1606 | sigset_t sigset; | |
fac6795d | 1607 | |
1d4b027a | 1608 | if ((ret = sigemptyset(&sigset)) < 0) { |
76d7553f | 1609 | PERROR("sigemptyset"); |
1d4b027a DG |
1610 | return ret; |
1611 | } | |
d6f42150 | 1612 | |
1d4b027a DG |
1613 | sa.sa_mask = sigset; |
1614 | sa.sa_flags = 0; | |
0072e5e2 MD |
1615 | |
1616 | sa.sa_handler = sighandler; | |
1d4b027a | 1617 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
76d7553f | 1618 | PERROR("sigaction"); |
1d4b027a | 1619 | return ret; |
d6f42150 DG |
1620 | } |
1621 | ||
1d4b027a | 1622 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
76d7553f | 1623 | PERROR("sigaction"); |
1d4b027a | 1624 | return ret; |
d6f42150 | 1625 | } |
aaf26714 | 1626 | |
0072e5e2 | 1627 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
76d7553f | 1628 | PERROR("sigaction"); |
1d4b027a | 1629 | return ret; |
8c0faa1d DG |
1630 | } |
1631 | ||
0072e5e2 MD |
1632 | sa.sa_handler = SIG_IGN; |
1633 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
0bb7724a DG |
1634 | PERROR("sigaction"); |
1635 | return ret; | |
1636 | } | |
1637 | ||
1638 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); | |
1d4b027a DG |
1639 | |
1640 | return ret; | |
fac6795d DG |
1641 | } |
1642 | ||
f3ed775e | 1643 | /* |
d063d709 | 1644 | * Set open files limit to unlimited. This daemon can open a large number of |
514bb9f0 | 1645 | * file descriptors in order to consume multiple kernel traces. |
f3ed775e DG |
1646 | */ |
1647 | static void set_ulimit(void) | |
1648 | { | |
1649 | int ret; | |
1650 | struct rlimit lim; | |
1651 | ||
514bb9f0 | 1652 | /* The kernel does not allow an infinite limit for open files */ |
f3ed775e DG |
1653 | lim.rlim_cur = 65535; |
1654 | lim.rlim_max = 65535; | |
1655 | ||
1656 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
1657 | if (ret < 0) { | |
76d7553f | 1658 | PERROR("failed to set open files limit"); |
f3ed775e DG |
1659 | } |
1660 | } | |
1661 | ||
4a15001e | 1662 | static int write_pidfile(void) |
35f90c40 | 1663 | { |
e6142f2e | 1664 | return utils_create_pid_file(getpid(), config.pid_file_path.value); |
35f90c40 DG |
1665 | } |
1666 | ||
e6142f2e JG |
1667 | static int set_clock_plugin_env(void) |
1668 | { | |
1669 | int ret = 0; | |
1670 | char *env_value = NULL; | |
cd9290dd | 1671 | |
e6142f2e JG |
1672 | if (!config.lttng_ust_clock_plugin.value) { |
1673 | goto end; | |
1674 | } | |
1675 | ||
1676 | ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s", | |
1677 | config.lttng_ust_clock_plugin.value); | |
cd9290dd | 1678 | if (ret < 0) { |
e6142f2e JG |
1679 | PERROR("asprintf"); |
1680 | goto end; | |
cd9290dd DG |
1681 | } |
1682 | ||
e6142f2e JG |
1683 | ret = putenv(env_value); |
1684 | if (ret) { | |
1685 | free(env_value); | |
1686 | PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN"); | |
1687 | goto end; | |
1688 | } | |
cd9290dd | 1689 | |
e6142f2e JG |
1690 | DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"", |
1691 | config.lttng_ust_clock_plugin.value); | |
1692 | end: | |
4a15001e | 1693 | return ret; |
ef367a93 JG |
1694 | } |
1695 | ||
6af972a2 JG |
1696 | static void destroy_all_sessions_and_wait(void) |
1697 | { | |
1698 | struct ltt_session *session, *tmp; | |
1699 | struct ltt_session_list *session_list; | |
1700 | ||
1701 | session_list = session_get_list(); | |
1702 | DBG("Initiating destruction of all sessions"); | |
1703 | ||
1704 | if (!session_list) { | |
1705 | return; | |
1706 | } | |
1707 | ||
6af972a2 JG |
1708 | session_lock_list(); |
1709 | /* Initiate the destruction of all sessions. */ | |
1710 | cds_list_for_each_entry_safe(session, tmp, | |
1711 | &session_list->head, list) { | |
1712 | if (!session_get(session)) { | |
1713 | continue; | |
1714 | } | |
1715 | ||
1716 | session_lock(session); | |
1717 | if (session->destroyed) { | |
1718 | goto unlock_session; | |
1719 | } | |
1720 | (void) cmd_destroy_session(session, | |
1721 | notification_thread_handle); | |
1722 | unlock_session: | |
1723 | session_unlock(session); | |
1724 | session_put(session); | |
1725 | } | |
1726 | session_unlock_list(); | |
1727 | ||
1728 | /* Wait for the destruction of all sessions to complete. */ | |
1729 | DBG("Waiting for the destruction of all sessions to complete"); | |
1730 | session_list_wait_empty(); | |
1731 | DBG("Destruction of all sessions completed"); | |
1732 | } | |
1733 | ||
fac6795d DG |
1734 | /* |
1735 | * main | |
1736 | */ | |
1737 | int main(int argc, char **argv) | |
1738 | { | |
4a15001e | 1739 | int ret = 0, retval = 0; |
fac6795d | 1740 | void *status; |
e6142f2e | 1741 | const char *env_app_timeout; |
b3530820 JG |
1742 | struct lttng_pipe *ust32_channel_monitor_pipe = NULL, |
1743 | *ust64_channel_monitor_pipe = NULL, | |
1744 | *kernel_channel_monitor_pipe = NULL; | |
548468b9 | 1745 | struct lttng_thread *ht_cleanup_thread = NULL; |
a5ab9e05 | 1746 | struct timer_thread_parameters timer_thread_parameters; |
a430759b JG |
1747 | /* Rotation thread handle. */ |
1748 | struct rotation_thread_handle *rotation_thread_handle = NULL; | |
d086f507 JD |
1749 | /* Queue of rotation jobs populated by the sessiond-timer. */ |
1750 | struct rotation_thread_timer_queue *rotation_timer_queue = NULL; | |
050e613c | 1751 | struct lttng_thread *client_thread = NULL; |
fac6795d | 1752 | |
335a95b7 MD |
1753 | init_kernel_workarounds(); |
1754 | ||
f6a9efaa DG |
1755 | rcu_register_thread(); |
1756 | ||
4a15001e MD |
1757 | if (set_signal_handler()) { |
1758 | retval = -1; | |
1759 | goto exit_set_signal_handler; | |
0bb7724a DG |
1760 | } |
1761 | ||
82528808 | 1762 | if (timer_signal_init()) { |
d086f507 JD |
1763 | retval = -1; |
1764 | goto exit_set_signal_handler; | |
1765 | } | |
1766 | ||
12744796 DG |
1767 | page_size = sysconf(_SC_PAGESIZE); |
1768 | if (page_size < 0) { | |
1769 | PERROR("sysconf _SC_PAGESIZE"); | |
1770 | page_size = LONG_MAX; | |
1771 | WARN("Fallback page size to %ld", page_size); | |
1772 | } | |
1773 | ||
e6142f2e JG |
1774 | ret = sessiond_config_init(&config); |
1775 | if (ret) { | |
1776 | retval = -1; | |
1777 | goto exit_set_signal_handler; | |
1778 | } | |
1779 | ||
2788b494 JR |
1780 | /* |
1781 | * Init config from environment variables. | |
1782 | * Command line option override env configuration per-doc. Do env first. | |
1783 | */ | |
1784 | sessiond_config_apply_env_config(&config); | |
1785 | ||
4a15001e MD |
1786 | /* |
1787 | * Parse arguments and load the daemon configuration file. | |
1788 | * | |
1789 | * We have an exit_options exit path to free memory reserved by | |
1790 | * set_options. This is needed because the rest of sessiond_cleanup() | |
1791 | * depends on ht_cleanup_thread, which depends on lttng_daemonize, which | |
1792 | * depends on set_options. | |
1793 | */ | |
fac6795d | 1794 | progname = argv[0]; |
4a15001e MD |
1795 | if (set_options(argc, argv)) { |
1796 | retval = -1; | |
1797 | goto exit_options; | |
fac6795d DG |
1798 | } |
1799 | ||
e6142f2e JG |
1800 | /* |
1801 | * Resolve all paths received as arguments, configuration option, or | |
1802 | * through environment variable as absolute paths. This is necessary | |
1803 | * since daemonizing causes the sessiond's current working directory | |
1804 | * to '/'. | |
1805 | */ | |
1806 | ret = sessiond_config_resolve_paths(&config); | |
1807 | if (ret) { | |
1808 | goto exit_options; | |
1809 | } | |
1810 | ||
1811 | /* Apply config. */ | |
1812 | lttng_opt_verbose = config.verbose; | |
1813 | lttng_opt_quiet = config.quiet; | |
1814 | kconsumer_data.err_unix_sock_path = | |
1815 | config.kconsumerd_err_unix_sock_path.value; | |
1816 | kconsumer_data.cmd_unix_sock_path = | |
1817 | config.kconsumerd_cmd_unix_sock_path.value; | |
1818 | ustconsumer32_data.err_unix_sock_path = | |
1819 | config.consumerd32_err_unix_sock_path.value; | |
1820 | ustconsumer32_data.cmd_unix_sock_path = | |
1821 | config.consumerd32_cmd_unix_sock_path.value; | |
1822 | ustconsumer64_data.err_unix_sock_path = | |
1823 | config.consumerd64_err_unix_sock_path.value; | |
1824 | ustconsumer64_data.cmd_unix_sock_path = | |
1825 | config.consumerd64_cmd_unix_sock_path.value; | |
1826 | set_clock_plugin_env(); | |
1827 | ||
1828 | sessiond_config_log(&config); | |
1829 | ||
f472090a JG |
1830 | if (create_lttng_rundir()) { |
1831 | retval = -1; | |
1832 | goto exit_options; | |
1833 | } | |
1834 | ||
1835 | /* Abort launch if a session daemon is already running. */ | |
1836 | if (check_existing_daemon()) { | |
1837 | ERR("A session daemon is already running."); | |
1838 | retval = -1; | |
1839 | goto exit_options; | |
1840 | } | |
1841 | ||
fac6795d | 1842 | /* Daemonize */ |
e6142f2e | 1843 | if (config.daemonize || config.background) { |
ceed52b5 MD |
1844 | int i; |
1845 | ||
72dd7491 | 1846 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, |
e6142f2e | 1847 | !config.background); |
53094c05 | 1848 | if (ret < 0) { |
4a15001e MD |
1849 | retval = -1; |
1850 | goto exit_options; | |
53094c05 | 1851 | } |
0bb7724a | 1852 | |
ceed52b5 | 1853 | /* |
0bb7724a | 1854 | * We are in the child. Make sure all other file descriptors are |
4a15001e | 1855 | * closed, in case we are called with more opened file |
f472090a | 1856 | * descriptors than the standard ones and the lock file. |
ceed52b5 MD |
1857 | */ |
1858 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
f472090a JG |
1859 | if (i == lockfile_fd) { |
1860 | continue; | |
1861 | } | |
ceed52b5 MD |
1862 | (void) close(i); |
1863 | } | |
1864 | } | |
1865 | ||
7567352f MD |
1866 | if (run_as_create_worker(argv[0]) < 0) { |
1867 | goto exit_create_run_as_worker_cleanup; | |
1868 | } | |
1869 | ||
4a15001e MD |
1870 | /* |
1871 | * Starting from here, we can create threads. This needs to be after | |
1872 | * lttng_daemonize due to RCU. | |
1873 | */ | |
1874 | ||
1875 | /* | |
1876 | * Initialize the health check subsystem. This call should set the | |
1877 | * appropriate time values. | |
1878 | */ | |
1879 | health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); | |
1880 | if (!health_sessiond) { | |
1881 | PERROR("health_app_create error"); | |
1882 | retval = -1; | |
1883 | goto exit_health_sessiond_cleanup; | |
1884 | } | |
1885 | ||
4a15001e | 1886 | /* Create thread to clean up RCU hash tables */ |
548468b9 JG |
1887 | ht_cleanup_thread = launch_ht_cleanup_thread(); |
1888 | if (!ht_cleanup_thread) { | |
4a15001e MD |
1889 | retval = -1; |
1890 | goto exit_ht_cleanup; | |
1891 | } | |
1892 | ||
ceed52b5 | 1893 | /* Create thread quit pipe */ |
51bb4dbb | 1894 | if (sessiond_init_thread_quit_pipe()) { |
4a15001e MD |
1895 | retval = -1; |
1896 | goto exit_init_data; | |
fac6795d DG |
1897 | } |
1898 | ||
1899 | /* Check if daemon is UID = 0 */ | |
1900 | is_root = !getuid(); | |
fac6795d | 1901 | if (is_root) { |
67e40797 | 1902 | /* Create global run dir with root access */ |
67e40797 | 1903 | |
b3530820 JG |
1904 | kernel_channel_monitor_pipe = lttng_pipe_open(0); |
1905 | if (!kernel_channel_monitor_pipe) { | |
1906 | ERR("Failed to create kernel consumer channel monitor pipe"); | |
1907 | retval = -1; | |
1908 | goto exit_init_data; | |
1909 | } | |
1910 | kconsumer_data.channel_monitor_pipe = | |
1911 | lttng_pipe_release_writefd( | |
1912 | kernel_channel_monitor_pipe); | |
1913 | if (kconsumer_data.channel_monitor_pipe < 0) { | |
1914 | retval = -1; | |
1915 | goto exit_init_data; | |
1916 | } | |
fac6795d DG |
1917 | } |
1918 | ||
5c827ce0 DG |
1919 | /* Set consumer initial state */ |
1920 | kernel_consumerd_state = CONSUMER_STOPPED; | |
1921 | ust_consumerd_state = CONSUMER_STOPPED; | |
1922 | ||
b3530820 JG |
1923 | ust32_channel_monitor_pipe = lttng_pipe_open(0); |
1924 | if (!ust32_channel_monitor_pipe) { | |
1925 | ERR("Failed to create 32-bit user space consumer channel monitor pipe"); | |
1926 | retval = -1; | |
1927 | goto exit_init_data; | |
1928 | } | |
1929 | ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd( | |
1930 | ust32_channel_monitor_pipe); | |
1931 | if (ustconsumer32_data.channel_monitor_pipe < 0) { | |
1932 | retval = -1; | |
1933 | goto exit_init_data; | |
1934 | } | |
62c43103 | 1935 | |
d086f507 | 1936 | /* |
82528808 JG |
1937 | * The rotation_thread_timer_queue structure is shared between the |
1938 | * sessiond timer thread and the rotation thread. The main thread keeps | |
1939 | * its ownership and destroys it when both threads have been joined. | |
d086f507 | 1940 | */ |
82528808 | 1941 | rotation_timer_queue = rotation_thread_timer_queue_create(); |
d086f507 JD |
1942 | if (!rotation_timer_queue) { |
1943 | retval = -1; | |
1944 | goto exit_init_data; | |
1945 | } | |
a5ab9e05 JG |
1946 | timer_thread_parameters.rotation_thread_job_queue = |
1947 | rotation_timer_queue; | |
67e40797 | 1948 | |
b3530820 JG |
1949 | ust64_channel_monitor_pipe = lttng_pipe_open(0); |
1950 | if (!ust64_channel_monitor_pipe) { | |
1951 | ERR("Failed to create 64-bit user space consumer channel monitor pipe"); | |
1952 | retval = -1; | |
1953 | goto exit_init_data; | |
1954 | } | |
1955 | ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd( | |
1956 | ust64_channel_monitor_pipe); | |
1957 | if (ustconsumer64_data.channel_monitor_pipe < 0) { | |
1958 | retval = -1; | |
1959 | goto exit_init_data; | |
1960 | } | |
847177cd | 1961 | |
1427f9b2 DG |
1962 | /* |
1963 | * Init UST app hash table. Alloc hash table before this point since | |
1964 | * cleanup() can get called after that point. | |
1965 | */ | |
4a15001e | 1966 | if (ust_app_ht_alloc()) { |
ddbeb0f6 | 1967 | ERR("Failed to allocate UST app hash table"); |
4a15001e MD |
1968 | retval = -1; |
1969 | goto exit_init_data; | |
1970 | } | |
1427f9b2 | 1971 | |
6a4e4039 JG |
1972 | /* |
1973 | * Initialize agent app hash table. We allocate the hash table here | |
1974 | * since cleanup() can get called after this point. | |
1975 | */ | |
1976 | if (agent_app_ht_alloc()) { | |
1977 | ERR("Failed to allocate Agent app hash table"); | |
4a15001e MD |
1978 | retval = -1; |
1979 | goto exit_init_data; | |
f20baf8e DG |
1980 | } |
1981 | ||
a88df331 DG |
1982 | /* |
1983 | * These actions must be executed as root. We do that *after* setting up | |
1984 | * the sockets path because we MUST make the check for another daemon using | |
1985 | * those paths *before* trying to set the kernel consumer sockets and init | |
1986 | * kernel tracer. | |
1987 | */ | |
1988 | if (is_root) { | |
e6142f2e | 1989 | if (set_consumer_sockets(&kconsumer_data)) { |
4a15001e MD |
1990 | retval = -1; |
1991 | goto exit_init_data; | |
7753dea8 MD |
1992 | } |
1993 | ||
a88df331 | 1994 | /* Setup kernel tracer */ |
e6142f2e | 1995 | if (!config.no_kernel) { |
4fba7219 | 1996 | init_kernel_tracer(); |
89588270 DG |
1997 | if (kernel_tracer_fd >= 0) { |
1998 | ret = syscall_init_table(); | |
1999 | if (ret < 0) { | |
4a15001e MD |
2000 | ERR("Unable to populate syscall table. " |
2001 | "Syscall tracing won't work " | |
2002 | "for this session daemon."); | |
89588270 | 2003 | } |
834978fd | 2004 | } |
4fba7219 | 2005 | } |
a88df331 DG |
2006 | |
2007 | /* Set ulimit for open files */ | |
2008 | set_ulimit(); | |
fac6795d | 2009 | } |
4063050c MD |
2010 | /* init lttng_fd tracking must be done after set_ulimit. */ |
2011 | lttng_fd_init(); | |
fac6795d | 2012 | |
e6142f2e | 2013 | if (set_consumer_sockets(&ustconsumer64_data)) { |
4a15001e MD |
2014 | retval = -1; |
2015 | goto exit_init_data; | |
67e40797 DG |
2016 | } |
2017 | ||
e6142f2e | 2018 | if (set_consumer_sockets(&ustconsumer32_data)) { |
4a15001e MD |
2019 | retval = -1; |
2020 | goto exit_init_data; | |
67e40797 DG |
2021 | } |
2022 | ||
fac6795d | 2023 | /* Set credentials to socket */ |
e6142f2e | 2024 | if (is_root && set_permissions(config.rundir.value)) { |
4a15001e MD |
2025 | retval = -1; |
2026 | goto exit_init_data; | |
fac6795d DG |
2027 | } |
2028 | ||
5b8719f5 | 2029 | /* Get parent pid if -S, --sig-parent is specified. */ |
e6142f2e | 2030 | if (config.sig_parent) { |
5b8719f5 DG |
2031 | ppid = getppid(); |
2032 | } | |
2033 | ||
7a485870 | 2034 | /* Setup the kernel pipe for waking up the kernel thread */ |
e6142f2e | 2035 | if (is_root && !config.no_kernel) { |
4a15001e MD |
2036 | if (utils_create_pipe_cloexec(kernel_poll_pipe)) { |
2037 | retval = -1; | |
2038 | goto exit_init_data; | |
6620da75 | 2039 | } |
7a485870 DG |
2040 | } |
2041 | ||
099e26bd | 2042 | /* Setup the thread apps communication pipe. */ |
4a15001e MD |
2043 | if (utils_create_pipe_cloexec(apps_cmd_pipe)) { |
2044 | retval = -1; | |
2045 | goto exit_init_data; | |
099e26bd DG |
2046 | } |
2047 | ||
d0b96690 | 2048 | /* Setup the thread apps notify communication pipe. */ |
4a15001e MD |
2049 | if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) { |
2050 | retval = -1; | |
2051 | goto exit_init_data; | |
d0b96690 DG |
2052 | } |
2053 | ||
7972aab2 DG |
2054 | /* Initialize global buffer per UID and PID registry. */ |
2055 | buffer_reg_init_uid_registry(); | |
2056 | buffer_reg_init_pid_registry(); | |
2057 | ||
099e26bd | 2058 | /* Init UST command queue. */ |
8bdee6e2 | 2059 | cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail); |
099e26bd | 2060 | |
2f77fc4b | 2061 | cmd_init(); |
00e2e675 | 2062 | |
ae9e45b3 DG |
2063 | /* Check for the application socket timeout env variable. */ |
2064 | env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV); | |
2065 | if (env_app_timeout) { | |
e6142f2e | 2066 | config.app_socket_timeout = atoi(env_app_timeout); |
ae9e45b3 | 2067 | } else { |
e6142f2e | 2068 | config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT; |
ae9e45b3 DG |
2069 | } |
2070 | ||
4a15001e MD |
2071 | ret = write_pidfile(); |
2072 | if (ret) { | |
2073 | ERR("Error in write_pidfile"); | |
2074 | retval = -1; | |
2075 | goto exit_init_data; | |
2076 | } | |
35f90c40 | 2077 | |
554831e7 MD |
2078 | /* Initialize communication library */ |
2079 | lttcomm_init(); | |
4a15001e | 2080 | /* Initialize TCP timeout values */ |
d831c249 | 2081 | lttcomm_inet_init(); |
554831e7 | 2082 | |
ef367a93 | 2083 | if (load_session_init_data(&load_info) < 0) { |
4a15001e MD |
2084 | retval = -1; |
2085 | goto exit_init_data; | |
ef367a93 | 2086 | } |
e6142f2e | 2087 | load_info->path = config.load_session_path.value; |
ef367a93 | 2088 | |
12b4fb37 | 2089 | /* Create health-check thread. */ |
225221a8 | 2090 | if (!launch_health_management_thread()) { |
4a15001e | 2091 | retval = -1; |
44a5e5eb DG |
2092 | goto exit_health; |
2093 | } | |
2094 | ||
b3530820 JG |
2095 | /* notification_thread_data acquires the pipes' read side. */ |
2096 | notification_thread_handle = notification_thread_handle_create( | |
2097 | ust32_channel_monitor_pipe, | |
2098 | ust64_channel_monitor_pipe, | |
8898b0da | 2099 | kernel_channel_monitor_pipe); |
b3530820 JG |
2100 | if (!notification_thread_handle) { |
2101 | retval = -1; | |
2102 | ERR("Failed to create notification thread shared data"); | |
b3530820 JG |
2103 | goto exit_notification; |
2104 | } | |
2105 | ||
2106 | /* Create notification thread. */ | |
8898b0da | 2107 | if (!launch_notification_thread(notification_thread_handle)) { |
b3530820 | 2108 | retval = -1; |
b3530820 JG |
2109 | goto exit_notification; |
2110 | } | |
2111 | ||
d086f507 | 2112 | /* Create timer thread. */ |
a5ab9e05 | 2113 | if (!launch_timer_thread(&timer_thread_parameters)) { |
d086f507 | 2114 | retval = -1; |
d086f507 JD |
2115 | goto exit_notification; |
2116 | } | |
d086f507 | 2117 | |
db66e574 JD |
2118 | /* rotation_thread_data acquires the pipes' read side. */ |
2119 | rotation_thread_handle = rotation_thread_handle_create( | |
90936dcf | 2120 | rotation_timer_queue, |
8898b0da | 2121 | notification_thread_handle); |
db66e574 JD |
2122 | if (!rotation_thread_handle) { |
2123 | retval = -1; | |
2124 | ERR("Failed to create rotation thread shared data"); | |
2125 | stop_threads(); | |
2126 | goto exit_rotation; | |
2127 | } | |
db66e574 JD |
2128 | |
2129 | /* Create rotation thread. */ | |
a430759b | 2130 | if (!launch_rotation_thread(rotation_thread_handle)) { |
db66e574 | 2131 | retval = -1; |
db66e574 JD |
2132 | goto exit_rotation; |
2133 | } | |
2134 | ||
cf3af59e | 2135 | /* Create thread to manage the client socket */ |
050e613c JG |
2136 | client_thread = launch_client_thread(); |
2137 | if (!client_thread) { | |
4a15001e | 2138 | retval = -1; |
cf3af59e MD |
2139 | goto exit_client; |
2140 | } | |
fac6795d | 2141 | |
dd5875e7 JG |
2142 | if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1], |
2143 | apps_cmd_notify_pipe[1])) { | |
4a15001e | 2144 | retval = -1; |
099e26bd DG |
2145 | goto exit_dispatch; |
2146 | } | |
2147 | ||
2148 | /* Create thread to manage application registration. */ | |
22b80785 | 2149 | if (!launch_application_registration_thread(&ust_cmd_queue)) { |
4a15001e | 2150 | retval = -1; |
099e26bd DG |
2151 | goto exit_reg_apps; |
2152 | } | |
2153 | ||
cf3af59e | 2154 | /* Create thread to manage application socket */ |
cdffb210 | 2155 | if (!launch_application_management_thread(apps_cmd_pipe[0])) { |
4a15001e | 2156 | retval = -1; |
d0b96690 DG |
2157 | goto exit_apps; |
2158 | } | |
2159 | ||
2160 | /* Create thread to manage application notify socket */ | |
53c51aaa | 2161 | if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) { |
4a15001e | 2162 | retval = -1; |
9563b0ad | 2163 | goto exit_apps_notify; |
cf3af59e | 2164 | } |
fac6795d | 2165 | |
7baf7871 JG |
2166 | /* Create agent management thread. */ |
2167 | if (!launch_agent_management_thread()) { | |
4a15001e | 2168 | retval = -1; |
022d91ba | 2169 | goto exit_agent_reg; |
4d076222 DG |
2170 | } |
2171 | ||
6620da75 | 2172 | /* Don't start this thread if kernel tracing is not requested nor root */ |
e6142f2e | 2173 | if (is_root && !config.no_kernel) { |
6620da75 | 2174 | /* Create kernel thread to manage kernel event */ |
45f6525f | 2175 | if (!launch_kernel_management_thread(kernel_poll_pipe[0])) { |
4a15001e | 2176 | retval = -1; |
6620da75 DG |
2177 | goto exit_kernel; |
2178 | } | |
ef367a93 | 2179 | } |
7a485870 | 2180 | |
4a15001e | 2181 | /* Create session loading thread. */ |
1a1a34b4 MJ |
2182 | ret = pthread_create(&load_session_thread, default_pthread_attr(), |
2183 | thread_load_session, load_info); | |
4a15001e MD |
2184 | if (ret) { |
2185 | errno = ret; | |
2186 | PERROR("pthread_create load_session_thread"); | |
2187 | retval = -1; | |
b3530820 | 2188 | stop_threads(); |
4a15001e MD |
2189 | goto exit_load_session; |
2190 | } | |
2191 | ||
2192 | /* | |
2193 | * This is where we start awaiting program completion (e.g. through | |
2194 | * signal that asks threads to teardown). | |
2195 | */ | |
2196 | ||
2197 | ret = pthread_join(load_session_thread, &status); | |
2198 | if (ret) { | |
2199 | errno = ret; | |
2200 | PERROR("pthread_join load_session_thread"); | |
2201 | retval = -1; | |
2202 | } | |
6af972a2 JG |
2203 | |
2204 | /* Initiate teardown once activity occurs on the quit pipe. */ | |
2205 | sessiond_wait_for_quit_pipe(-1U); | |
050e613c JG |
2206 | |
2207 | /* | |
2208 | * Ensure that the client thread is no longer accepting new commands, | |
2209 | * which could cause new sessions to be created. | |
2210 | */ | |
2211 | if (!lttng_thread_shutdown(client_thread)) { | |
2212 | ERR("Failed to shutdown the client thread, continuing teardown"); | |
2213 | lttng_thread_put(client_thread); | |
2214 | client_thread = NULL; | |
2215 | } | |
2216 | ||
6af972a2 | 2217 | destroy_all_sessions_and_wait(); |
4a15001e | 2218 | exit_load_session: |
cf3af59e | 2219 | exit_kernel: |
022d91ba | 2220 | exit_agent_reg: |
9563b0ad | 2221 | exit_apps_notify: |
cf3af59e | 2222 | exit_apps: |
099e26bd | 2223 | exit_reg_apps: |
099e26bd | 2224 | exit_dispatch: |
b3530820 | 2225 | exit_client: |
db66e574 | 2226 | exit_rotation: |
b3530820 | 2227 | exit_notification: |
548468b9 | 2228 | lttng_thread_list_shutdown_orphans(); |
b3530820 | 2229 | exit_health: |
4a15001e | 2230 | exit_init_data: |
050e613c JG |
2231 | if (client_thread) { |
2232 | lttng_thread_put(client_thread); | |
2233 | } | |
2234 | ||
4d62fbf8 MD |
2235 | /* |
2236 | * Wait for all pending call_rcu work to complete before tearing | |
2237 | * down data structures. call_rcu worker may be trying to | |
2238 | * perform lookups in those structures. | |
2239 | */ | |
2240 | rcu_barrier(); | |
4a15001e MD |
2241 | /* |
2242 | * sessiond_cleanup() is called when no other thread is running, except | |
2243 | * the ht_cleanup thread, which is needed to destroy the hash tables. | |
2244 | */ | |
2245 | rcu_thread_online(); | |
2246 | sessiond_cleanup(); | |
06f525de | 2247 | |
c4d5de9d JG |
2248 | /* |
2249 | * Ensure all prior call_rcu are done. call_rcu callbacks may push | |
2250 | * hash tables to the ht_cleanup thread. Therefore, we ensure that | |
2251 | * the queue is empty before shutting down the clean-up thread. | |
2252 | */ | |
2253 | rcu_barrier(); | |
2254 | ||
548468b9 JG |
2255 | if (ht_cleanup_thread) { |
2256 | lttng_thread_shutdown(ht_cleanup_thread); | |
2257 | lttng_thread_put(ht_cleanup_thread); | |
2258 | } | |
2259 | ||
a430759b JG |
2260 | rcu_thread_offline(); |
2261 | rcu_unregister_thread(); | |
2262 | ||
2263 | if (rotation_thread_handle) { | |
2264 | rotation_thread_handle_destroy(rotation_thread_handle); | |
2265 | } | |
2266 | ||
d086f507 JD |
2267 | /* |
2268 | * After the rotation and timer thread have quit, we can safely destroy | |
2269 | * the rotation_timer_queue. | |
2270 | */ | |
82528808 | 2271 | rotation_thread_timer_queue_destroy(rotation_timer_queue); |
8898b0da JG |
2272 | /* |
2273 | * The teardown of the notification system is performed after the | |
2274 | * session daemon's teardown in order to allow it to be notified | |
2275 | * of the active session and channels at the moment of the teardown. | |
2276 | */ | |
2277 | if (notification_thread_handle) { | |
2278 | notification_thread_handle_destroy(notification_thread_handle); | |
2279 | } | |
b3530820 JG |
2280 | lttng_pipe_destroy(ust32_channel_monitor_pipe); |
2281 | lttng_pipe_destroy(ust64_channel_monitor_pipe); | |
2282 | lttng_pipe_destroy(kernel_channel_monitor_pipe); | |
0b2dc8df | 2283 | exit_ht_cleanup: |
4a15001e | 2284 | |
4a15001e MD |
2285 | health_app_destroy(health_sessiond); |
2286 | exit_health_sessiond_cleanup: | |
7567352f | 2287 | exit_create_run_as_worker_cleanup: |
4a15001e MD |
2288 | |
2289 | exit_options: | |
f472090a | 2290 | sessiond_cleanup_lock_file(); |
4a15001e MD |
2291 | sessiond_cleanup_options(); |
2292 | ||
2293 | exit_set_signal_handler: | |
2294 | if (!retval) { | |
cf3af59e | 2295 | exit(EXIT_SUCCESS); |
4a15001e MD |
2296 | } else { |
2297 | exit(EXIT_FAILURE); | |
67e40797 | 2298 | } |
fac6795d | 2299 | } |