Launch the rotation thread using lttng_thread
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
CommitLineData
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
FD
52#include <common/dynamic-buffer.h>
53#include <lttng/userspace-probe-internal.h>
76fcf151 54#include <lttng/event-internal.h>
fac6795d 55
10a8a223 56#include "lttng-sessiond.h"
7972aab2 57#include "buffer-registry.h"
54d01ffb 58#include "channel.h"
2f77fc4b 59#include "cmd.h"
00e2e675 60#include "consumer.h"
099e26bd 61#include "context.h"
54d01ffb 62#include "event.h"
4771f025 63#include "kernel.h"
f1e16794 64#include "kernel-consumer.h"
096102bd 65#include "modprobe.h"
0fdd1e2c 66#include "shm.h"
1e307fab 67#include "ust-ctl.h"
00e2e675 68#include "ust-consumer.h"
8e68d1c8 69#include "utils.h"
4063050c 70#include "fd-limit.h"
8782cc74 71#include "health-sessiond.h"
8ac94142 72#include "testpoint.h"
d0b96690 73#include "ust-thread.h"
022d91ba 74#include "agent-thread.h"
fb198a11 75#include "save.h"
ef367a93 76#include "load-session-thread.h"
b3530820
JG
77#include "notification-thread.h"
78#include "notification-thread-commands.h"
db66e574 79#include "rotation-thread.h"
0dbc2034 80#include "lttng-syscall.h"
7c1d2758 81#include "agent.h"
5e97de00 82#include "ht-cleanup.h"
e6142f2e 83#include "sessiond-config.h"
8e319828 84#include "timer.h"
a3707772 85#include "thread.h"
ebaeda94 86
4fc83d94
PP
87static const char *help_msg =
88#ifdef LTTNG_EMBED_HELP
89#include <lttng-sessiond.8.h>
90#else
91NULL
92#endif
93;
94
fac6795d 95const char *progname;
c9cb3e7d 96static int lockfile_fd = -1;
3bd1e081 97
0bb7724a
DG
98/* Set to 1 when a SIGUSR1 signal is received. */
99static int recv_child_signal;
100
88076e89
JD
101static struct lttng_kernel_tracer_version kernel_tracer_version;
102static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version;
103
a23ec3a7
DG
104/*
105 * Consumer daemon specific control data. Every value not initialized here is
106 * set to 0 by the static definition.
107 */
3bd1e081
MD
108static struct consumer_data kconsumer_data = {
109 .type = LTTNG_CONSUMER_KERNEL,
03550b58
MD
110 .err_sock = -1,
111 .cmd_sock = -1,
b3530820 112 .channel_monitor_pipe = -1,
173af62f
DG
113 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
114 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
115 .cond = PTHREAD_COND_INITIALIZER,
116 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
3bd1e081 117};
7753dea8
MD
118static struct consumer_data ustconsumer64_data = {
119 .type = LTTNG_CONSUMER64_UST,
03550b58
MD
120 .err_sock = -1,
121 .cmd_sock = -1,
b3530820 122 .channel_monitor_pipe = -1,
173af62f
DG
123 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
124 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
125 .cond = PTHREAD_COND_INITIALIZER,
126 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
7753dea8
MD
127};
128static struct consumer_data ustconsumer32_data = {
129 .type = LTTNG_CONSUMER32_UST,
03550b58
MD
130 .err_sock = -1,
131 .cmd_sock = -1,
b3530820 132 .channel_monitor_pipe = -1,
173af62f
DG
133 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
134 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
135 .cond = PTHREAD_COND_INITIALIZER,
136 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
3bd1e081
MD
137};
138
26296c48
JG
139/* Command line options */
140static const struct option long_options[] = {
0f5ea17c
JG
141 { "client-sock", required_argument, 0, 'c' },
142 { "apps-sock", required_argument, 0, 'a' },
143 { "kconsumerd-cmd-sock", required_argument, 0, '\0' },
144 { "kconsumerd-err-sock", required_argument, 0, '\0' },
145 { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' },
146 { "ustconsumerd32-err-sock", required_argument, 0, '\0' },
147 { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' },
148 { "ustconsumerd64-err-sock", required_argument, 0, '\0' },
149 { "consumerd32-path", required_argument, 0, '\0' },
150 { "consumerd32-libdir", required_argument, 0, '\0' },
151 { "consumerd64-path", required_argument, 0, '\0' },
152 { "consumerd64-libdir", required_argument, 0, '\0' },
153 { "daemonize", no_argument, 0, 'd' },
154 { "background", no_argument, 0, 'b' },
155 { "sig-parent", no_argument, 0, 'S' },
156 { "help", no_argument, 0, 'h' },
157 { "group", required_argument, 0, 'g' },
158 { "version", no_argument, 0, 'V' },
159 { "quiet", no_argument, 0, 'q' },
160 { "verbose", no_argument, 0, 'v' },
161 { "verbose-consumer", no_argument, 0, '\0' },
162 { "no-kernel", no_argument, 0, '\0' },
163 { "pidfile", required_argument, 0, 'p' },
164 { "agent-tcp-port", required_argument, 0, '\0' },
165 { "config", required_argument, 0, 'f' },
166 { "load", required_argument, 0, 'l' },
167 { "kmod-probes", required_argument, 0, '\0' },
168 { "extra-kmod-probes", required_argument, 0, '\0' },
26296c48
JG
169 { NULL, 0, 0, 0 }
170};
171
172/* Command line options to ignore from configuration file */
173static const char *config_ignore_options[] = { "help", "version", "config" };
174
26c9d55e 175/* Shared between threads */
099e26bd 176static int dispatch_thread_exit;
fac6795d 177
1d4b027a 178/* Sockets and FDs */
a4b35e07
MD
179static int client_sock = -1;
180static int apps_sock = -1;
1d4b027a 181
099e26bd
DG
182/*
183 * This pipe is used to inform the thread managing application communication
184 * that a command is queued and ready to be processed.
185 */
76d7553f 186static int apps_cmd_pipe[2] = { -1, -1 };
099e26bd 187
1d4b027a 188/* Pthread, Mutexes and Semaphores */
1d4b027a 189static pthread_t apps_thread;
d0b96690 190static pthread_t apps_notify_thread;
099e26bd 191static pthread_t reg_apps_thread;
1d4b027a 192static pthread_t client_thread;
7a485870 193static pthread_t kernel_thread;
099e26bd 194static pthread_t dispatch_thread;
022d91ba 195static pthread_t agent_reg_thread;
ef367a93 196static pthread_t load_session_thread;
d086f507 197static pthread_t timer_thread;
5eb91c98 198
099e26bd
DG
199/*
200 * UST registration command queue. This queue is tied with a futex and uses a N
201 * wakers / 1 waiter implemented and detailed in futex.c/.h
202 *
b22c5da8
DG
203 * The thread_registration_apps and thread_dispatch_ust_registration uses this
204 * queue along with the wait/wake scheme. The thread_manage_apps receives down
205 * the line new application socket and monitors it for any I/O error or clean
206 * close that triggers an unregistration of the application.
099e26bd
DG
207 */
208static struct ust_cmd_queue ust_cmd_queue;
209
2f77fc4b
DG
210static const char *module_proc_lttng = "/proc/lttng";
211
5c827ce0
DG
212/*
213 * Consumer daemon state which is changed when spawning it, killing it or in
214 * case of a fatal error.
215 */
216enum consumerd_state {
217 CONSUMER_STARTED = 1,
218 CONSUMER_STOPPED = 2,
219 CONSUMER_ERROR = 3,
220};
221
222/*
223 * This consumer daemon state is used to validate if a client command will be
224 * able to reach the consumer. If not, the client is informed. For instance,
225 * doing a "lttng start" when the consumer state is set to ERROR will return an
226 * error to the client.
227 *
228 * The following example shows a possible race condition of this scheme:
229 *
230 * consumer thread error happens
231 * client cmd arrives
232 * client cmd checks state -> still OK
233 * consumer thread exit, sets error
234 * client cmd try to talk to consumer
235 * ...
236 *
237 * However, since the consumer is a different daemon, we have no way of making
238 * sure the command will reach it safely even with this state flag. This is why
239 * we consider that up to the state validation during command processing, the
240 * command is safe. After that, we can not guarantee the correctness of the
241 * client request vis-a-vis the consumer.
242 */
243static enum consumerd_state ust_consumerd_state;
244static enum consumerd_state kernel_consumerd_state;
245
ef367a93 246/* Load session thread information to operate. */
a7333da7 247static struct load_session_thread_data *load_info;
7c1d2758 248
97bc1426 249/*
a7333da7 250 * Section name to look for in the daemon configuration file.
97bc1426 251 */
a7333da7 252static const char * const config_section_name = "sessiond";
5e97de00 253
a7333da7
JG
254/* Am I root or not. Set to 1 if the daemon is running as root */
255static int is_root;
5eb91c98 256
099e26bd
DG
257/*
258 * Stop all threads by closing the thread quit pipe.
259 */
cf3af59e
MD
260static void stop_threads(void)
261{
5eb91c98
DG
262 int ret;
263
cf3af59e
MD
264 /* Stopping all threads */
265 DBG("Terminating all threads");
a7333da7 266 ret = sessiond_notify_quit_pipe();
5eb91c98
DG
267 if (ret < 0) {
268 ERR("write error on thread quit pipe");
269 }
270
099e26bd 271 /* Dispatch thread */
26c9d55e 272 CMM_STORE_SHARED(dispatch_thread_exit, 1);
099e26bd 273 futex_nto1_wake(&ust_cmd_queue.futex);
cf3af59e
MD
274}
275
e975f9f8
DG
276/*
277 * Close every consumer sockets.
278 */
279static void close_consumer_sockets(void)
280{
281 int ret;
282
283 if (kconsumer_data.err_sock >= 0) {
284 ret = close(kconsumer_data.err_sock);
285 if (ret < 0) {
286 PERROR("kernel consumer err_sock close");
287 }
288 }
289 if (ustconsumer32_data.err_sock >= 0) {
290 ret = close(ustconsumer32_data.err_sock);
291 if (ret < 0) {
a76cbd9f 292 PERROR("UST consumerd32 err_sock close");
e975f9f8
DG
293 }
294 }
295 if (ustconsumer64_data.err_sock >= 0) {
296 ret = close(ustconsumer64_data.err_sock);
297 if (ret < 0) {
a76cbd9f 298 PERROR("UST consumerd64 err_sock close");
e975f9f8
DG
299 }
300 }
301 if (kconsumer_data.cmd_sock >= 0) {
302 ret = close(kconsumer_data.cmd_sock);
303 if (ret < 0) {
304 PERROR("kernel consumer cmd_sock close");
305 }
306 }
307 if (ustconsumer32_data.cmd_sock >= 0) {
308 ret = close(ustconsumer32_data.cmd_sock);
309 if (ret < 0) {
a76cbd9f 310 PERROR("UST consumerd32 cmd_sock close");
e975f9f8
DG
311 }
312 }
313 if (ustconsumer64_data.cmd_sock >= 0) {
314 ret = close(ustconsumer64_data.cmd_sock);
315 if (ret < 0) {
a76cbd9f 316 PERROR("UST consumerd64 cmd_sock close");
e975f9f8
DG
317 }
318 }
b3530820
JG
319 if (kconsumer_data.channel_monitor_pipe >= 0) {
320 ret = close(kconsumer_data.channel_monitor_pipe);
321 if (ret < 0) {
322 PERROR("kernel consumer channel monitor pipe close");
323 }
324 }
325 if (ustconsumer32_data.channel_monitor_pipe >= 0) {
326 ret = close(ustconsumer32_data.channel_monitor_pipe);
327 if (ret < 0) {
328 PERROR("UST consumerd32 channel monitor pipe close");
329 }
330 }
331 if (ustconsumer64_data.channel_monitor_pipe >= 0) {
332 ret = close(ustconsumer64_data.channel_monitor_pipe);
333 if (ret < 0) {
334 PERROR("UST consumerd64 channel monitor pipe close");
335 }
336 }
e975f9f8
DG
337}
338
4e4714cb
JR
339/*
340 * Wait on consumer process termination.
341 *
342 * Need to be called with the consumer data lock held or from a context
343 * ensuring no concurrent access to data (e.g: cleanup).
344 */
345static void wait_consumer(struct consumer_data *consumer_data)
346{
347 pid_t ret;
348 int status;
349
350 if (consumer_data->pid <= 0) {
351 return;
352 }
353
354 DBG("Waiting for complete teardown of consumerd (PID: %d)",
355 consumer_data->pid);
356 ret = waitpid(consumer_data->pid, &status, 0);
357 if (ret == -1) {
358 PERROR("consumerd waitpid pid: %d", consumer_data->pid)
1640c24c 359 } else if (!WIFEXITED(status)) {
4e4714cb
JR
360 ERR("consumerd termination with error: %d",
361 WEXITSTATUS(ret));
362 }
363 consumer_data->pid = 0;
364}
365
fac6795d 366/*
4a15001e 367 * Cleanup the session daemon's data structures.
fac6795d 368 */
4a15001e 369static void sessiond_cleanup(void)
fac6795d 370{
ef599319 371 int ret;
e32d7f27 372 struct ltt_session_list *session_list = session_get_list();
fac6795d 373
4a15001e 374 DBG("Cleanup sessiond");
e07ae692 375
4e449f3f
MD
376 /*
377 * Close the thread quit pipe. It has already done its job,
378 * since we are now called.
379 */
a7333da7 380 sessiond_close_quit_pipe();
2f77fc4b 381
c9a2957d
JG
382 ret = remove(config.pid_file_path.value);
383 if (ret < 0) {
384 PERROR("remove pidfile %s", config.pid_file_path.value);
35f90c40
DG
385 }
386
e6142f2e
JG
387 DBG("Removing sessiond and consumerd content of directory %s",
388 config.rundir.value);
8c6c56c2
MD
389
390 /* sessiond */
e6142f2e
JG
391 DBG("Removing %s", config.pid_file_path.value);
392 (void) unlink(config.pid_file_path.value);
8c6c56c2 393
e6142f2e
JG
394 DBG("Removing %s", config.agent_port_file_path.value);
395 (void) unlink(config.agent_port_file_path.value);
cd9290dd 396
8c6c56c2 397 /* kconsumerd */
e6142f2e
JG
398 DBG("Removing %s", kconsumer_data.err_unix_sock_path);
399 (void) unlink(kconsumer_data.err_unix_sock_path);
400
401 DBG("Removing directory %s", config.kconsumerd_path.value);
402 (void) rmdir(config.kconsumerd_path.value);
8c6c56c2
MD
403
404 /* ust consumerd 32 */
e6142f2e
JG
405 DBG("Removing %s", config.consumerd32_err_unix_sock_path.value);
406 (void) unlink(config.consumerd32_err_unix_sock_path.value);
407
408 DBG("Removing directory %s", config.consumerd32_path.value);
409 (void) rmdir(config.consumerd32_path.value);
8c6c56c2
MD
410
411 /* ust consumerd 64 */
e6142f2e
JG
412 DBG("Removing %s", config.consumerd64_err_unix_sock_path.value);
413 (void) unlink(config.consumerd64_err_unix_sock_path.value);
414
415 DBG("Removing directory %s", config.consumerd64_path.value);
416 (void) rmdir(config.consumerd64_path.value);
5461b305 417
99d688f2 418 pthread_mutex_destroy(&session_list->lock);
273ea72c 419
4e4714cb
JR
420 wait_consumer(&kconsumer_data);
421 wait_consumer(&ustconsumer64_data);
422 wait_consumer(&ustconsumer32_data);
423
6a4e4039
JG
424 DBG("Cleaning up all agent apps");
425 agent_app_ht_clean();
426
099e26bd 427 DBG("Closing all UST sockets");
56fff090 428 ust_app_clean_list();
7972aab2 429 buffer_reg_destroy_registries();
099e26bd 430
e6142f2e 431 if (is_root && !config.no_kernel) {
4fba7219 432 DBG2("Closing kernel fd");
a4b35e07 433 if (kernel_tracer_fd >= 0) {
76d7553f
MD
434 ret = close(kernel_tracer_fd);
435 if (ret) {
436 PERROR("close");
437 }
a4b35e07 438 }
2f50c8a3 439 DBG("Unloading kernel modules");
096102bd 440 modprobe_remove_lttng_all();
834978fd 441 free(syscall_table);
2f50c8a3 442 }
2f77fc4b 443
e975f9f8
DG
444 close_consumer_sockets();
445
ef367a93
JG
446 if (load_info) {
447 load_session_destroy_data(load_info);
448 free(load_info);
449 }
450
c9cb3e7d
JG
451 /*
452 * We do NOT rmdir rundir because there are other processes
453 * using it, for instance lttng-relayd, which can start in
454 * parallel with this teardown.
455 */
4a15001e
MD
456}
457
458/*
459 * Cleanup the daemon's option data structures.
460 */
461static void sessiond_cleanup_options(void)
462{
463 DBG("Cleaning up options");
464
e6142f2e 465 sessiond_config_fini(&config);
c9cb3e7d 466
7567352f 467 run_as_destroy_worker();
fac6795d
DG
468}
469
e065084a 470/*
d063d709 471 * Send data on a unix socket using the liblttsessiondcomm API.
e065084a 472 *
d063d709 473 * Return lttcomm error code.
e065084a
DG
474 */
475static int send_unix_sock(int sock, void *buf, size_t len)
476{
477 /* Check valid length */
c617c0c6 478 if (len == 0) {
e065084a
DG
479 return -1;
480 }
481
482 return lttcomm_send_unix_sock(sock, buf, len);
483}
484
5461b305 485/*
d063d709 486 * Free memory of a command context structure.
5461b305 487 */
a2fb29a5 488static void clean_command_ctx(struct command_ctx **cmd_ctx)
5461b305 489{
a2fb29a5
DG
490 DBG("Clean command context structure");
491 if (*cmd_ctx) {
492 if ((*cmd_ctx)->llm) {
493 free((*cmd_ctx)->llm);
5461b305 494 }
a2fb29a5
DG
495 if ((*cmd_ctx)->lsm) {
496 free((*cmd_ctx)->lsm);
5461b305 497 }
a2fb29a5
DG
498 free(*cmd_ctx);
499 *cmd_ctx = NULL;
5461b305
DG
500 }
501}
502
fac6795d 503/*
0fdd1e2c 504 * Notify UST applications using the shm mmap futex.
fac6795d 505 */
0fdd1e2c 506static int notify_ust_apps(int active)
fac6795d 507{
0fdd1e2c 508 char *wait_shm_mmap;
fac6795d 509
0fdd1e2c 510 DBG("Notifying applications of session daemon state: %d", active);
e07ae692 511
0fdd1e2c 512 /* See shm.c for this call implying mmap, shm and futex calls */
e6142f2e 513 wait_shm_mmap = shm_ust_get_mmap(config.wait_shm_path.value, is_root);
0fdd1e2c 514 if (wait_shm_mmap == NULL) {
fac6795d
DG
515 goto error;
516 }
517
0fdd1e2c
DG
518 /* Wake waiting process */
519 futex_wait_update((int32_t *) wait_shm_mmap, active);
520
521 /* Apps notified successfully */
522 return 0;
fac6795d
DG
523
524error:
0fdd1e2c 525 return -1;
fac6795d
DG
526}
527
e065084a 528/*
d063d709
DG
529 * Setup the outgoing data buffer for the response (llm) by allocating the
530 * right amount of memory and copying the original information from the lsm
531 * structure.
ca95a216 532 *
6e10c9b9 533 * Return 0 on success, negative value on error.
ca95a216 534 */
6e10c9b9
PP
535static int setup_lttng_msg(struct command_ctx *cmd_ctx,
536 const void *payload_buf, size_t payload_len,
537 const void *cmd_header_buf, size_t cmd_header_len)
ca95a216 538{
6e10c9b9
PP
539 int ret = 0;
540 const size_t header_len = sizeof(struct lttcomm_lttng_msg);
541 const size_t cmd_header_offset = header_len;
542 const size_t payload_offset = cmd_header_offset + cmd_header_len;
543 const size_t total_msg_size = header_len + cmd_header_len + payload_len;
ca95a216 544
6e10c9b9 545 cmd_ctx->llm = zmalloc(total_msg_size);
5461b305 546
5461b305 547 if (cmd_ctx->llm == NULL) {
76d7553f 548 PERROR("zmalloc");
5461b305 549 ret = -ENOMEM;
6e10c9b9 550 goto end;
ca95a216
DG
551 }
552
5461b305
DG
553 /* Copy common data */
554 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
9f19cc17 555 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
6e10c9b9
PP
556 cmd_ctx->llm->cmd_header_size = cmd_header_len;
557 cmd_ctx->llm->data_size = payload_len;
558 cmd_ctx->lttng_msg_size = total_msg_size;
5461b305 559
6e10c9b9 560 /* Copy command header */
b4e3ceb9
PP
561 if (cmd_header_len) {
562 memcpy(((uint8_t *) cmd_ctx->llm) + cmd_header_offset, cmd_header_buf,
563 cmd_header_len);
564 }
5461b305 565
6e10c9b9 566 /* Copy payload */
b4e3ceb9
PP
567 if (payload_len) {
568 memcpy(((uint8_t *) cmd_ctx->llm) + payload_offset, payload_buf,
569 payload_len);
570 }
ca95a216 571
6e10c9b9 572end:
ca95a216
DG
573 return ret;
574}
575
6e10c9b9
PP
576/*
577 * Version of setup_lttng_msg() without command header.
578 */
579static int setup_lttng_msg_no_cmd_header(struct command_ctx *cmd_ctx,
580 void *payload_buf, size_t payload_len)
581{
582 return setup_lttng_msg(cmd_ctx, payload_buf, payload_len, NULL, 0);
583}
7a485870 584/*
5eb91c98 585 * Update the kernel poll set of all channel fd available over all tracing
d063d709 586 * session. Add the wakeup pipe at the end of the set.
7a485870 587 */
5eb91c98 588static int update_kernel_poll(struct lttng_poll_event *events)
7a485870 589{
5eb91c98 590 int ret;
7a485870 591 struct ltt_kernel_channel *channel;
e32d7f27
JG
592 struct ltt_session *session;
593 const struct ltt_session_list *session_list = session_get_list();
7a485870 594
5eb91c98 595 DBG("Updating kernel poll set");
7a485870 596
54d01ffb 597 session_lock_list();
e32d7f27
JG
598 cds_list_for_each_entry(session, &session_list->head, list) {
599 if (!session_get(session)) {
600 continue;
601 }
54d01ffb 602 session_lock(session);
7a485870 603 if (session->kernel_session == NULL) {
54d01ffb 604 session_unlock(session);
e32d7f27 605 session_put(session);
7a485870
DG
606 continue;
607 }
7a485870 608
54d01ffb
DG
609 cds_list_for_each_entry(channel,
610 &session->kernel_session->channel_list.head, list) {
5eb91c98
DG
611 /* Add channel fd to the kernel poll set */
612 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
613 if (ret < 0) {
54d01ffb 614 session_unlock(session);
e32d7f27 615 session_put(session);
5eb91c98
DG
616 goto error;
617 }
618 DBG("Channel fd %d added to kernel set", channel->fd);
7a485870 619 }
54d01ffb 620 session_unlock(session);
7a485870 621 }
54d01ffb 622 session_unlock_list();
7a485870 623
5eb91c98 624 return 0;
7a485870
DG
625
626error:
54d01ffb 627 session_unlock_list();
7a485870
DG
628 return -1;
629}
630
631/*
54d01ffb 632 * Find the channel fd from 'fd' over all tracing session. When found, check
d063d709 633 * for new channel stream and send those stream fds to the kernel consumer.
7a485870 634 *
d063d709 635 * Useful for CPU hotplug feature.
7a485870 636 */
7f032058 637static int update_kernel_stream(int fd)
7a485870
DG
638{
639 int ret = 0;
640 struct ltt_session *session;
173af62f 641 struct ltt_kernel_session *ksess;
7a485870 642 struct ltt_kernel_channel *channel;
e32d7f27 643 const struct ltt_session_list *session_list = session_get_list();
7a485870
DG
644
645 DBG("Updating kernel streams for channel fd %d", fd);
646
54d01ffb 647 session_lock_list();
e32d7f27
JG
648 cds_list_for_each_entry(session, &session_list->head, list) {
649 if (!session_get(session)) {
650 continue;
651 }
54d01ffb 652 session_lock(session);
7a485870 653 if (session->kernel_session == NULL) {
54d01ffb 654 session_unlock(session);
e32d7f27 655 session_put(session);
7a485870
DG
656 continue;
657 }
173af62f 658 ksess = session->kernel_session;
d9800920 659
4a15001e
MD
660 cds_list_for_each_entry(channel,
661 &ksess->channel_list.head, list) {
662 struct lttng_ht_iter iter;
663 struct consumer_socket *socket;
d9800920 664
4a15001e
MD
665 if (channel->fd != fd) {
666 continue;
667 }
668 DBG("Channel found, updating kernel streams");
669 ret = kernel_open_channel_stream(channel);
670 if (ret < 0) {
671 goto error;
672 }
673 /* Update the stream global counter */
674 ksess->stream_count_global += ret;
675
676 /*
677 * Have we already sent fds to the consumer? If yes, it
678 * means that tracing is started so it is safe to send
679 * our updated stream fds.
680 */
681 if (ksess->consumer_fds_sent != 1
682 || ksess->consumer == NULL) {
683 ret = -1;
684 goto error;
685 }
686
687 rcu_read_lock();
688 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
689 &iter.iter, socket, node.node) {
690 pthread_mutex_lock(socket->lock);
1fc1b7c8 691 ret = kernel_consumer_send_channel_streams(socket,
4a15001e
MD
692 channel, ksess,
693 session->output_traces ? 1 : 0);
694 pthread_mutex_unlock(socket->lock);
695 if (ret < 0) {
e7fe706f 696 rcu_read_unlock();
4a15001e 697 goto error;
7a485870 698 }
7a485870 699 }
4a15001e 700 rcu_read_unlock();
7a485870 701 }
54d01ffb 702 session_unlock(session);
e32d7f27 703 session_put(session);
7a485870 704 }
54d01ffb 705 session_unlock_list();
b3c750d2 706 return ret;
7a485870 707
b3c750d2 708error:
54d01ffb 709 session_unlock(session);
e32d7f27 710 session_put(session);
54d01ffb 711 session_unlock_list();
7a485870
DG
712 return ret;
713}
714
487cf67c 715/*
ffe60014
DG
716 * For each tracing session, update newly registered apps. The session list
717 * lock MUST be acquired before calling this.
487cf67c
DG
718 */
719static void update_ust_app(int app_sock)
720{
721 struct ltt_session *sess, *stmp;
e32d7f27 722 const struct ltt_session_list *session_list = session_get_list();
487cf67c 723
fdadac08
DG
724 /* Consumer is in an ERROR state. Stop any application update. */
725 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
726 /* Stop the update process since the consumer is dead. */
727 return;
728 }
729
487cf67c 730 /* For all tracing session(s) */
e32d7f27 731 cds_list_for_each_entry_safe(sess, stmp, &session_list->head, list) {
a9ad0c8f
MD
732 struct ust_app *app;
733
e32d7f27
JG
734 if (!session_get(sess)) {
735 continue;
736 }
4ee14516 737 session_lock(sess);
a9ad0c8f
MD
738 if (!sess->ust_session) {
739 goto unlock_session;
740 }
741
742 rcu_read_lock();
743 assert(app_sock >= 0);
744 app = ust_app_find_by_sock(app_sock);
745 if (app == NULL) {
746 /*
747 * Application can be unregistered before so
748 * this is possible hence simply stopping the
749 * update.
750 */
751 DBG3("UST app update failed to find app sock %d",
752 app_sock);
753 goto unlock_rcu;
421cb601 754 }
a9ad0c8f
MD
755 ust_app_global_update(sess->ust_session, app);
756 unlock_rcu:
757 rcu_read_unlock();
758 unlock_session:
4ee14516 759 session_unlock(sess);
e32d7f27 760 session_put(sess);
487cf67c
DG
761 }
762}
763
7a485870 764/*
d063d709 765 * This thread manage event coming from the kernel.
7a485870 766 *
d063d709
DG
767 * Features supported in this thread:
768 * -) CPU Hotplug
7a485870
DG
769 */
770static void *thread_manage_kernel(void *data)
771{
139ac872 772 int ret, i, pollfd, update_poll_flag = 1, err = -1;
5eb91c98 773 uint32_t revents, nb_fd;
7a485870 774 char tmp;
5eb91c98 775 struct lttng_poll_event events;
7a485870 776
6993eeb3 777 DBG("[thread] Thread manage kernel started");
7a485870 778
6c71277b 779 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
927ca06a 780
d5d63bf1
DG
781 /*
782 * This first step of the while is to clean this structure which could free
6d737ce4 783 * non NULL pointers so initialize it before the loop.
d5d63bf1 784 */
6d737ce4 785 lttng_poll_init(&events);
d5d63bf1 786
e547b070 787 if (testpoint(sessiond_thread_manage_kernel)) {
6993eeb3
CB
788 goto error_testpoint;
789 }
8ac94142 790
840cb59c 791 health_code_update();
44a5e5eb 792
e547b070 793 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
d21b0d71 794 goto error_testpoint;
6993eeb3
CB
795 }
796
7a485870 797 while (1) {
840cb59c 798 health_code_update();
44a5e5eb 799
7a485870 800 if (update_poll_flag == 1) {
d21b0d71
DG
801 /* Clean events object. We are about to populate it again. */
802 lttng_poll_clean(&events);
803
d0b96690 804 ret = sessiond_set_thread_pollset(&events, 2);
d21b0d71
DG
805 if (ret < 0) {
806 goto error_poll_create;
807 }
808
809 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
810 if (ret < 0) {
811 goto error;
812 }
5f822d0a 813
d21b0d71 814 /* This will add the available kernel channel if any. */
5eb91c98
DG
815 ret = update_kernel_poll(&events);
816 if (ret < 0) {
7a485870
DG
817 goto error;
818 }
819 update_poll_flag = 0;
820 }
821
7fa2082e 822 DBG("Thread kernel polling");
7a485870
DG
823
824 /* Poll infinite value of time */
88f2b785 825 restart:
a78af745 826 health_poll_entry();
5eb91c98 827 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
828 DBG("Thread kernel return from poll on %d fds",
829 LTTNG_POLL_GETNB(&events));
a78af745 830 health_poll_exit();
7a485870 831 if (ret < 0) {
88f2b785
MD
832 /*
833 * Restart interrupted system call.
834 */
835 if (errno == EINTR) {
836 goto restart;
837 }
7a485870
DG
838 goto error;
839 } else if (ret == 0) {
840 /* Should not happen since timeout is infinite */
85611738
DG
841 ERR("Return value of poll is 0 with an infinite timeout.\n"
842 "This should not have happened! Continuing...");
7a485870
DG
843 continue;
844 }
845
0d9c5d77
DG
846 nb_fd = ret;
847
5eb91c98
DG
848 for (i = 0; i < nb_fd; i++) {
849 /* Fetch once the poll data */
850 revents = LTTNG_POLL_GETEV(&events, i);
851 pollfd = LTTNG_POLL_GETFD(&events, i);
7a485870 852
840cb59c 853 health_code_update();
44a5e5eb 854
fd20dac9
MD
855 if (!revents) {
856 /* No activity for this FD (poll implementation). */
857 continue;
858 }
859
5eb91c98 860 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 861 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 862 if (ret) {
139ac872
MD
863 err = 0;
864 goto exit;
5eb91c98 865 }
7a485870 866
5eb91c98 867 /* Check for data on kernel pipe */
03e43155
MD
868 if (revents & LPOLLIN) {
869 if (pollfd == kernel_poll_pipe[0]) {
870 (void) lttng_read(kernel_poll_pipe[0],
871 &tmp, 1);
872 /*
873 * Ret value is useless here, if this pipe gets any actions an
874 * update is required anyway.
875 */
876 update_poll_flag = 1;
877 continue;
878 } else {
879 /*
880 * New CPU detected by the kernel. Adding kernel stream to
881 * kernel session and updating the kernel consumer
882 */
7f032058 883 ret = update_kernel_stream(pollfd);
5eb91c98
DG
884 if (ret < 0) {
885 continue;
886 }
887 break;
7a485870 888 }
03e43155
MD
889 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
890 update_poll_flag = 1;
891 continue;
892 } else {
893 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
894 goto error;
7a485870
DG
895 }
896 }
897 }
898
139ac872 899exit:
7a485870 900error:
5eb91c98 901 lttng_poll_clean(&events);
76d7553f 902error_poll_create:
6993eeb3 903error_testpoint:
6620da75
DG
904 utils_close_pipe(kernel_poll_pipe);
905 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
139ac872 906 if (err) {
840cb59c 907 health_error();
139ac872 908 ERR("Health error occurred in %s", __func__);
6620da75
DG
909 WARN("Kernel thread died unexpectedly. "
910 "Kernel tracing can continue but CPU hotplug is disabled.");
139ac872 911 }
8782cc74 912 health_unregister(health_sessiond);
76d7553f 913 DBG("Kernel thread dying");
7a485870
DG
914 return NULL;
915}
916
a23ec3a7
DG
917/*
918 * Signal pthread condition of the consumer data that the thread.
919 */
920static void signal_consumer_condition(struct consumer_data *data, int state)
921{
922 pthread_mutex_lock(&data->cond_mutex);
923
924 /*
925 * The state is set before signaling. It can be any value, it's the waiter
926 * job to correctly interpret this condition variable associated to the
927 * consumer pthread_cond.
928 *
929 * A value of 0 means that the corresponding thread of the consumer data
930 * was not started. 1 indicates that the thread has started and is ready
931 * for action. A negative value means that there was an error during the
932 * thread bootstrap.
933 */
934 data->consumer_thread_is_ready = state;
935 (void) pthread_cond_signal(&data->cond);
936
937 pthread_mutex_unlock(&data->cond_mutex);
938}
939
1d4b027a 940/*
3bd1e081 941 * This thread manage the consumer error sent back to the session daemon.
1d4b027a 942 */
3bd1e081 943static void *thread_manage_consumer(void *data)
1d4b027a 944{
42fc1d0b 945 int sock = -1, i, ret, pollfd, err = -1, should_quit = 0;
5eb91c98 946 uint32_t revents, nb_fd;
1d4b027a 947 enum lttcomm_return_code code;
5eb91c98 948 struct lttng_poll_event events;
3bd1e081 949 struct consumer_data *consumer_data = data;
b3530820 950 struct consumer_socket *cmd_socket_wrapper = NULL;
1d4b027a 951
3bd1e081 952 DBG("[thread] Manage consumer started");
1d4b027a 953
34c1e15a
MD
954 rcu_register_thread();
955 rcu_thread_online();
956
6c71277b 957 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
927ca06a 958
855060f8 959 health_code_update();
9449cc75 960
5eb91c98 961 /*
331744e3
JD
962 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
963 * metadata_sock. Nothing more will be added to this poll set.
5eb91c98 964 */
331744e3 965 ret = sessiond_set_thread_pollset(&events, 3);
5eb91c98 966 if (ret < 0) {
76d7553f 967 goto error_poll;
5eb91c98 968 }
273ea72c 969
edb8b045
DG
970 /*
971 * The error socket here is already in a listening state which was done
972 * just before spawning this thread to avoid a race between the consumer
973 * daemon exec trying to connect and the listen() call.
974 */
3bd1e081 975 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
5eb91c98
DG
976 if (ret < 0) {
977 goto error;
978 }
979
840cb59c 980 health_code_update();
44a5e5eb 981
331744e3 982 /* Infinite blocking call, waiting for transmission */
88f2b785 983restart:
a78af745 984 health_poll_entry();
8ac94142 985
e547b070 986 if (testpoint(sessiond_thread_manage_consumer)) {
6993eeb3
CB
987 goto error;
988 }
8ac94142 989
5eb91c98 990 ret = lttng_poll_wait(&events, -1);
a78af745 991 health_poll_exit();
273ea72c 992 if (ret < 0) {
88f2b785
MD
993 /*
994 * Restart interrupted system call.
995 */
996 if (errno == EINTR) {
997 goto restart;
998 }
273ea72c
DG
999 goto error;
1000 }
1001
0d9c5d77
DG
1002 nb_fd = ret;
1003
5eb91c98
DG
1004 for (i = 0; i < nb_fd; i++) {
1005 /* Fetch once the poll data */
1006 revents = LTTNG_POLL_GETEV(&events, i);
1007 pollfd = LTTNG_POLL_GETFD(&events, i);
1008
840cb59c 1009 health_code_update();
44a5e5eb 1010
fd20dac9
MD
1011 if (!revents) {
1012 /* No activity for this FD (poll implementation). */
1013 continue;
1014 }
1015
5eb91c98 1016 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1017 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1018 if (ret) {
139ac872
MD
1019 err = 0;
1020 goto exit;
5eb91c98
DG
1021 }
1022
1023 /* Event on the registration socket */
3bd1e081 1024 if (pollfd == consumer_data->err_sock) {
03e43155
MD
1025 if (revents & LPOLLIN) {
1026 continue;
1027 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3bd1e081 1028 ERR("consumer err socket poll error");
5eb91c98 1029 goto error;
03e43155
MD
1030 } else {
1031 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1032 goto error;
5eb91c98
DG
1033 }
1034 }
273ea72c
DG
1035 }
1036
3bd1e081 1037 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1d4b027a
DG
1038 if (sock < 0) {
1039 goto error;
1040 }
1041
b662582b
DG
1042 /*
1043 * Set the CLOEXEC flag. Return code is useless because either way, the
1044 * show must go on.
1045 */
1046 (void) utils_set_fd_cloexec(sock);
1047
840cb59c 1048 health_code_update();
44a5e5eb 1049
3bd1e081 1050 DBG2("Receiving code from consumer err_sock");
ee0b0061 1051
712ea556 1052 /* Getting status code from kconsumerd */
54d01ffb
DG
1053 ret = lttcomm_recv_unix_sock(sock, &code,
1054 sizeof(enum lttcomm_return_code));
1d4b027a
DG
1055 if (ret <= 0) {
1056 goto error;
1057 }
1058
840cb59c 1059 health_code_update();
b3530820 1060 if (code != LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
3bd1e081 1061 ERR("consumer error when waiting for SOCK_READY : %s",
1d4b027a
DG
1062 lttcomm_get_readable_code(-code));
1063 goto error;
1064 }
1065
b3530820
JG
1066 /* Connect both command and metadata sockets. */
1067 consumer_data->cmd_sock =
1068 lttcomm_connect_unix_sock(
1069 consumer_data->cmd_unix_sock_path);
1070 consumer_data->metadata_fd =
1071 lttcomm_connect_unix_sock(
1072 consumer_data->cmd_unix_sock_path);
1073 if (consumer_data->cmd_sock < 0 || consumer_data->metadata_fd < 0) {
1074 PERROR("consumer connect cmd socket");
1075 /* On error, signal condition and quit. */
1076 signal_consumer_condition(consumer_data, -1);
1077 goto error;
1078 }
1079
1080 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
1081
1082 /* Create metadata socket lock. */
1083 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
1084 if (consumer_data->metadata_sock.lock == NULL) {
1085 PERROR("zmalloc pthread mutex");
1086 goto error;
1087 }
1088 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
1089
1090 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
1091 DBG("Consumer metadata socket ready (fd: %d)",
1092 consumer_data->metadata_fd);
1093
1094 /*
1095 * Remove the consumerd error sock since we've established a connection.
1096 */
3bd1e081 1097 ret = lttng_poll_del(&events, consumer_data->err_sock);
72079cae 1098 if (ret < 0) {
72079cae
DG
1099 goto error;
1100 }
1101
331744e3 1102 /* Add new accepted error socket. */
5eb91c98
DG
1103 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1104 if (ret < 0) {
72079cae 1105 goto error;
5eb91c98
DG
1106 }
1107
331744e3 1108 /* Add metadata socket that is successfully connected. */
4ce514c4 1109 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
331744e3
JD
1110 LPOLLIN | LPOLLRDHUP);
1111 if (ret < 0) {
1112 goto error;
1113 }
1114
840cb59c 1115 health_code_update();
44a5e5eb 1116
b3530820 1117 /*
62c43103 1118 * Transfer the write-end of the channel monitoring and rotate pipe
92816cc3 1119 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
b3530820
JG
1120 */
1121 cmd_socket_wrapper = consumer_allocate_socket(&consumer_data->cmd_sock);
1122 if (!cmd_socket_wrapper) {
1123 goto error;
1124 }
3e4dc117 1125 cmd_socket_wrapper->lock = &consumer_data->lock;
b3530820
JG
1126
1127 ret = consumer_send_channel_monitor_pipe(cmd_socket_wrapper,
1128 consumer_data->channel_monitor_pipe);
1129 if (ret) {
1130 goto error;
1131 }
62c43103 1132
b3530820
JG
1133 /* Discard the socket wrapper as it is no longer needed. */
1134 consumer_destroy_socket(cmd_socket_wrapper);
1135 cmd_socket_wrapper = NULL;
1136
1137 /* The thread is completely initialized, signal that it is ready. */
1138 signal_consumer_condition(consumer_data, 1);
1139
331744e3 1140 /* Infinite blocking call, waiting for transmission */
88f2b785 1141restart_poll:
331744e3 1142 while (1) {
42fc1d0b
DG
1143 health_code_update();
1144
1145 /* Exit the thread because the thread quit pipe has been triggered. */
1146 if (should_quit) {
1147 /* Not a health error. */
1148 err = 0;
1149 goto exit;
1150 }
1151
331744e3
JD
1152 health_poll_entry();
1153 ret = lttng_poll_wait(&events, -1);
1154 health_poll_exit();
1155 if (ret < 0) {
1156 /*
1157 * Restart interrupted system call.
1158 */
1159 if (errno == EINTR) {
1160 goto restart_poll;
1161 }
1162 goto error;
88f2b785 1163 }
72079cae 1164
331744e3 1165 nb_fd = ret;
0d9c5d77 1166
331744e3
JD
1167 for (i = 0; i < nb_fd; i++) {
1168 /* Fetch once the poll data */
1169 revents = LTTNG_POLL_GETEV(&events, i);
1170 pollfd = LTTNG_POLL_GETFD(&events, i);
5eb91c98 1171
331744e3 1172 health_code_update();
44a5e5eb 1173
fd20dac9
MD
1174 if (!revents) {
1175 /* No activity for this FD (poll implementation). */
1176 continue;
1177 }
1178
42fc1d0b
DG
1179 /*
1180 * Thread quit pipe has been triggered, flag that we should stop
1181 * but continue the current loop to handle potential data from
1182 * consumer.
1183 */
1184 should_quit = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1185
331744e3
JD
1186 if (pollfd == sock) {
1187 /* Event on the consumerd socket */
03e43155
MD
1188 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1189 && !(revents & LPOLLIN)) {
331744e3
JD
1190 ERR("consumer err socket second poll error");
1191 goto error;
1192 }
1193 health_code_update();
1194 /* Wait for any kconsumerd error */
1195 ret = lttcomm_recv_unix_sock(sock, &code,
1196 sizeof(enum lttcomm_return_code));
1197 if (ret <= 0) {
1198 ERR("consumer closed the command socket");
1199 goto error;
1200 }
1201
1202 ERR("consumer return code : %s",
1203 lttcomm_get_readable_code(-code));
1204
1205 goto exit;
4ce514c4 1206 } else if (pollfd == consumer_data->metadata_fd) {
03e43155
MD
1207 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1208 && !(revents & LPOLLIN)) {
1209 ERR("consumer err metadata socket second poll error");
1210 goto error;
1211 }
331744e3
JD
1212 /* UST metadata requests */
1213 ret = ust_consumer_metadata_request(
1214 &consumer_data->metadata_sock);
1215 if (ret < 0) {
1216 ERR("Handling metadata request");
1217 goto error;
1218 }
5eb91c98 1219 }
42fc1d0b 1220 /* No need for an else branch all FDs are tested prior. */
5eb91c98 1221 }
331744e3 1222 health_code_update();
5eb91c98
DG
1223 }
1224
139ac872 1225exit:
1d4b027a 1226error:
fdadac08
DG
1227 /*
1228 * We lock here because we are about to close the sockets and some other
92db7cdc
DG
1229 * thread might be using them so get exclusive access which will abort all
1230 * other consumer command by other threads.
fdadac08
DG
1231 */
1232 pthread_mutex_lock(&consumer_data->lock);
1233
5c827ce0
DG
1234 /* Immediately set the consumerd state to stopped */
1235 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1236 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1237 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1238 consumer_data->type == LTTNG_CONSUMER32_UST) {
1239 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1240 } else {
1241 /* Code flow error... */
1242 assert(0);
1243 }
1244
76d7553f
MD
1245 if (consumer_data->err_sock >= 0) {
1246 ret = close(consumer_data->err_sock);
1247 if (ret) {
1248 PERROR("close");
1249 }
a76cbd9f 1250 consumer_data->err_sock = -1;
76d7553f
MD
1251 }
1252 if (consumer_data->cmd_sock >= 0) {
1253 ret = close(consumer_data->cmd_sock);
1254 if (ret) {
1255 PERROR("close");
1256 }
a76cbd9f 1257 consumer_data->cmd_sock = -1;
76d7553f 1258 }
96544455
SS
1259 if (consumer_data->metadata_sock.fd_ptr &&
1260 *consumer_data->metadata_sock.fd_ptr >= 0) {
9363801e 1261 ret = close(*consumer_data->metadata_sock.fd_ptr);
331744e3
JD
1262 if (ret) {
1263 PERROR("close");
1264 }
1265 }
76d7553f
MD
1266 if (sock >= 0) {
1267 ret = close(sock);
1268 if (ret) {
1269 PERROR("close");
1270 }
1271 }
273ea72c 1272
3bd1e081
MD
1273 unlink(consumer_data->err_unix_sock_path);
1274 unlink(consumer_data->cmd_unix_sock_path);
fdadac08 1275 pthread_mutex_unlock(&consumer_data->lock);
92db7cdc 1276
fdadac08 1277 /* Cleanup metadata socket mutex. */
96544455
SS
1278 if (consumer_data->metadata_sock.lock) {
1279 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1280 free(consumer_data->metadata_sock.lock);
1281 }
5eb91c98 1282 lttng_poll_clean(&events);
b3530820
JG
1283
1284 if (cmd_socket_wrapper) {
1285 consumer_destroy_socket(cmd_socket_wrapper);
1286 }
76d7553f 1287error_poll:
139ac872 1288 if (err) {
840cb59c 1289 health_error();
139ac872
MD
1290 ERR("Health error occurred in %s", __func__);
1291 }
8782cc74 1292 health_unregister(health_sessiond);
76d7553f 1293 DBG("consumer thread cleanup completed");
0177d773 1294
34c1e15a
MD
1295 rcu_thread_offline();
1296 rcu_unregister_thread();
1297
5eb91c98 1298 return NULL;
099e26bd
DG
1299}
1300
099e26bd 1301/*
81f04d5f
JG
1302 * This thread receives application command sockets (FDs) on the
1303 * apps_cmd_pipe and waits (polls) on them until they are closed
1304 * or an error occurs.
1305 *
1306 * At that point, it flushes the data (tracing and metadata) associated
1307 * with this application and tears down ust app sessions and other
1308 * associated data structures through ust_app_unregister().
1309 *
1310 * Note that this thread never sends commands to the applications
1311 * through the command sockets; it merely listens for hang-ups
1312 * and errors on those sockets and cleans-up as they occur.
1d4b027a
DG
1313 */
1314static void *thread_manage_apps(void *data)
099e26bd 1315{
139ac872 1316 int i, ret, pollfd, err = -1;
6cd525e8 1317 ssize_t size_ret;
5eb91c98 1318 uint32_t revents, nb_fd;
5eb91c98 1319 struct lttng_poll_event events;
099e26bd
DG
1320
1321 DBG("[thread] Manage application started");
1322
f6a9efaa
DG
1323 rcu_register_thread();
1324 rcu_thread_online();
1325
6c71277b 1326 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
927ca06a 1327
e547b070 1328 if (testpoint(sessiond_thread_manage_apps)) {
6993eeb3
CB
1329 goto error_testpoint;
1330 }
1331
840cb59c 1332 health_code_update();
44a5e5eb 1333
d0b96690 1334 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 1335 if (ret < 0) {
76d7553f 1336 goto error_poll_create;
5eb91c98 1337 }
099e26bd 1338
5eb91c98
DG
1339 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1340 if (ret < 0) {
1341 goto error;
1342 }
099e26bd 1343
e547b070 1344 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
6993eeb3
CB
1345 goto error;
1346 }
8ac94142 1347
840cb59c 1348 health_code_update();
44a5e5eb 1349
5eb91c98 1350 while (1) {
7fa2082e 1351 DBG("Apps thread polling");
099e26bd
DG
1352
1353 /* Inifinite blocking call, waiting for transmission */
88f2b785 1354 restart:
a78af745 1355 health_poll_entry();
5eb91c98 1356 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
1357 DBG("Apps thread return from poll on %d fds",
1358 LTTNG_POLL_GETNB(&events));
a78af745 1359 health_poll_exit();
099e26bd 1360 if (ret < 0) {
88f2b785
MD
1361 /*
1362 * Restart interrupted system call.
1363 */
1364 if (errno == EINTR) {
1365 goto restart;
1366 }
099e26bd
DG
1367 goto error;
1368 }
1369
0d9c5d77
DG
1370 nb_fd = ret;
1371
5eb91c98
DG
1372 for (i = 0; i < nb_fd; i++) {
1373 /* Fetch once the poll data */
1374 revents = LTTNG_POLL_GETEV(&events, i);
1375 pollfd = LTTNG_POLL_GETFD(&events, i);
1376
840cb59c 1377 health_code_update();
44a5e5eb 1378
fd20dac9
MD
1379 if (!revents) {
1380 /* No activity for this FD (poll implementation). */
1381 continue;
1382 }
1383
5eb91c98 1384 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1385 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1386 if (ret) {
139ac872
MD
1387 err = 0;
1388 goto exit;
5eb91c98 1389 }
099e26bd 1390
5eb91c98
DG
1391 /* Inspect the apps cmd pipe */
1392 if (pollfd == apps_cmd_pipe[0]) {
03e43155 1393 if (revents & LPOLLIN) {
d0b96690
DG
1394 int sock;
1395
5eb91c98 1396 /* Empty pipe */
6cd525e8
MD
1397 size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
1398 if (size_ret < sizeof(sock)) {
76d7553f 1399 PERROR("read apps cmd pipe");
5eb91c98
DG
1400 goto error;
1401 }
099e26bd 1402
840cb59c 1403 health_code_update();
44a5e5eb 1404
ffe60014 1405 /*
03e43155
MD
1406 * Since this is a command socket (write then read),
1407 * we only monitor the error events of the socket.
ffe60014 1408 */
d0b96690
DG
1409 ret = lttng_poll_add(&events, sock,
1410 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1411 if (ret < 0) {
5eb91c98 1412 goto error;
e0c7ec2b 1413 }
acc7b41b 1414
d0b96690 1415 DBG("Apps with sock %d added to poll set", sock);
03e43155
MD
1416 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1417 ERR("Apps command pipe error");
1418 goto error;
1419 } else {
1420 ERR("Unknown poll events %u for sock %d", revents, pollfd);
1421 goto error;
0177d773 1422 }
5eb91c98
DG
1423 } else {
1424 /*
54d01ffb
DG
1425 * At this point, we know that a registered application made
1426 * the event at poll_wait.
5eb91c98
DG
1427 */
1428 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1429 /* Removing from the poll set */
1430 ret = lttng_poll_del(&events, pollfd);
1431 if (ret < 0) {
1432 goto error;
1433 }
099e26bd 1434
b9d9b220 1435 /* Socket closed on remote end. */
56fff090 1436 ust_app_unregister(pollfd);
03e43155
MD
1437 } else {
1438 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1439 goto error;
5eb91c98 1440 }
099e26bd 1441 }
44a5e5eb 1442
840cb59c 1443 health_code_update();
099e26bd 1444 }
099e26bd
DG
1445 }
1446
139ac872 1447exit:
099e26bd 1448error:
5eb91c98 1449 lttng_poll_clean(&events);
76d7553f 1450error_poll_create:
6993eeb3 1451error_testpoint:
6620da75
DG
1452 utils_close_pipe(apps_cmd_pipe);
1453 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1454
1455 /*
1456 * We don't clean the UST app hash table here since already registered
1457 * applications can still be controlled so let them be until the session
1458 * daemon dies or the applications stop.
1459 */
1460
139ac872 1461 if (err) {
840cb59c 1462 health_error();
139ac872
MD
1463 ERR("Health error occurred in %s", __func__);
1464 }
8782cc74 1465 health_unregister(health_sessiond);
76d7553f 1466 DBG("Application communication apps thread cleanup complete");
f6a9efaa
DG
1467 rcu_thread_offline();
1468 rcu_unregister_thread();
099e26bd
DG
1469 return NULL;
1470}
1471
d0b96690 1472/*
d88aee68
DG
1473 * Send a socket to a thread This is called from the dispatch UST registration
1474 * thread once all sockets are set for the application.
d0b96690 1475 *
b85dc84c
DG
1476 * The sock value can be invalid, we don't really care, the thread will handle
1477 * it and make the necessary cleanup if so.
1478 *
d0b96690
DG
1479 * On success, return 0 else a negative value being the errno message of the
1480 * write().
1481 */
d88aee68 1482static int send_socket_to_thread(int fd, int sock)
d0b96690 1483{
6cd525e8 1484 ssize_t ret;
d0b96690 1485
b85dc84c
DG
1486 /*
1487 * It's possible that the FD is set as invalid with -1 concurrently just
1488 * before calling this function being a shutdown state of the thread.
1489 */
1490 if (fd < 0) {
1491 ret = -EBADF;
1492 goto error;
1493 }
d0b96690 1494
6cd525e8
MD
1495 ret = lttng_write(fd, &sock, sizeof(sock));
1496 if (ret < sizeof(sock)) {
d88aee68 1497 PERROR("write apps pipe %d", fd);
d0b96690
DG
1498 if (ret < 0) {
1499 ret = -errno;
1500 }
1501 goto error;
1502 }
1503
1504 /* All good. Don't send back the write positive ret value. */
1505 ret = 0;
1506error:
6cd525e8 1507 return (int) ret;
d0b96690
DG
1508}
1509
f45e313d
DG
1510/*
1511 * Sanitize the wait queue of the dispatch registration thread meaning removing
1512 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1513 * notify socket is never received.
1514 */
1515static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1516{
1517 int ret, nb_fd = 0, i;
1518 unsigned int fd_added = 0;
1519 struct lttng_poll_event events;
1520 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1521
1522 assert(wait_queue);
1523
1524 lttng_poll_init(&events);
1525
1526 /* Just skip everything for an empty queue. */
1527 if (!wait_queue->count) {
1528 goto end;
1529 }
1530
1531 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1532 if (ret < 0) {
1533 goto error_create;
1534 }
1535
1536 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1537 &wait_queue->head, head) {
1538 assert(wait_node->app);
1539 ret = lttng_poll_add(&events, wait_node->app->sock,
1540 LPOLLHUP | LPOLLERR);
1541 if (ret < 0) {
1542 goto error;
1543 }
1544
1545 fd_added = 1;
1546 }
1547
1548 if (!fd_added) {
1549 goto end;
1550 }
1551
1552 /*
1553 * Poll but don't block so we can quickly identify the faulty events and
1554 * clean them afterwards from the wait queue.
1555 */
1556 ret = lttng_poll_wait(&events, 0);
1557 if (ret < 0) {
1558 goto error;
1559 }
1560 nb_fd = ret;
1561
1562 for (i = 0; i < nb_fd; i++) {
1563 /* Get faulty FD. */
1564 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1565 int pollfd = LTTNG_POLL_GETFD(&events, i);
1566
fd20dac9
MD
1567 if (!revents) {
1568 /* No activity for this FD (poll implementation). */
1569 continue;
1570 }
1571
f45e313d
DG
1572 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1573 &wait_queue->head, head) {
1574 if (pollfd == wait_node->app->sock &&
1575 (revents & (LPOLLHUP | LPOLLERR))) {
1576 cds_list_del(&wait_node->head);
1577 wait_queue->count--;
1578 ust_app_destroy(wait_node->app);
1579 free(wait_node);
48b40bcf
JG
1580 /*
1581 * Silence warning of use-after-free in
1582 * cds_list_for_each_entry_safe which uses
1583 * __typeof__(*wait_node).
1584 */
1585 wait_node = NULL;
f45e313d 1586 break;
03e43155
MD
1587 } else {
1588 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1589 goto error;
f45e313d
DG
1590 }
1591 }
1592 }
1593
1594 if (nb_fd > 0) {
1595 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1596 }
1597
1598end:
1599 lttng_poll_clean(&events);
1600 return;
1601
1602error:
1603 lttng_poll_clean(&events);
1604error_create:
1605 ERR("Unable to sanitize wait queue");
1606 return;
1607}
1608
099e26bd
DG
1609/*
1610 * Dispatch request from the registration threads to the application
1611 * communication thread.
1612 */
1613static void *thread_dispatch_ust_registration(void *data)
1614{
12e2b881 1615 int ret, err = -1;
8bdee6e2 1616 struct cds_wfcq_node *node;
099e26bd 1617 struct ust_command *ust_cmd = NULL;
f45e313d
DG
1618 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1619 struct ust_reg_wait_queue wait_queue = {
1620 .count = 0,
1621 };
d0b96690 1622
967e3668
MD
1623 rcu_register_thread();
1624
6c71277b 1625 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
12e2b881 1626
9ad42ec1
MD
1627 if (testpoint(sessiond_thread_app_reg_dispatch)) {
1628 goto error_testpoint;
1629 }
1630
12e2b881
MD
1631 health_code_update();
1632
f45e313d 1633 CDS_INIT_LIST_HEAD(&wait_queue.head);
099e26bd
DG
1634
1635 DBG("[thread] Dispatch UST command started");
1636
0ed3b1a8 1637 for (;;) {
12e2b881
MD
1638 health_code_update();
1639
099e26bd
DG
1640 /* Atomically prepare the queue futex */
1641 futex_nto1_prepare(&ust_cmd_queue.futex);
1642
0ed3b1a8
MD
1643 if (CMM_LOAD_SHARED(dispatch_thread_exit)) {
1644 break;
1645 }
1646
099e26bd 1647 do {
d0b96690 1648 struct ust_app *app = NULL;
7972aab2 1649 ust_cmd = NULL;
d0b96690 1650
f45e313d
DG
1651 /*
1652 * Make sure we don't have node(s) that have hung up before receiving
1653 * the notify socket. This is to clean the list in order to avoid
1654 * memory leaks from notify socket that are never seen.
1655 */
1656 sanitize_wait_queue(&wait_queue);
1657
12e2b881 1658 health_code_update();
099e26bd 1659 /* Dequeue command for registration */
8bdee6e2 1660 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
099e26bd 1661 if (node == NULL) {
00a17c97 1662 DBG("Woken up but nothing in the UST command queue");
099e26bd
DG
1663 /* Continue thread execution */
1664 break;
1665 }
1666
1667 ust_cmd = caa_container_of(node, struct ust_command, node);
1668
2f50c8a3
DG
1669 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1670 " gid:%d sock:%d name:%s (version %d.%d)",
1671 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1672 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1673 ust_cmd->sock, ust_cmd->reg_msg.name,
1674 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
d0b96690
DG
1675
1676 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1677 wait_node = zmalloc(sizeof(*wait_node));
1678 if (!wait_node) {
1679 PERROR("zmalloc wait_node dispatch");
020d7f60
DG
1680 ret = close(ust_cmd->sock);
1681 if (ret < 0) {
1682 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1683 }
51dec90d 1684 lttng_fd_put(LTTNG_FD_APPS, 1);
7972aab2 1685 free(ust_cmd);
d0b96690
DG
1686 goto error;
1687 }
1688 CDS_INIT_LIST_HEAD(&wait_node->head);
1689
1690 /* Create application object if socket is CMD. */
1691 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1692 ust_cmd->sock);
1693 if (!wait_node->app) {
1694 ret = close(ust_cmd->sock);
1695 if (ret < 0) {
1696 PERROR("close ust sock dispatch %d", ust_cmd->sock);
6620da75 1697 }
51dec90d 1698 lttng_fd_put(LTTNG_FD_APPS, 1);
d88aee68 1699 free(wait_node);
7972aab2 1700 free(ust_cmd);
d0b96690
DG
1701 continue;
1702 }
1703 /*
1704 * Add application to the wait queue so we can set the notify
1705 * socket before putting this object in the global ht.
1706 */
f45e313d
DG
1707 cds_list_add(&wait_node->head, &wait_queue.head);
1708 wait_queue.count++;
d0b96690 1709
7972aab2 1710 free(ust_cmd);
d0b96690
DG
1711 /*
1712 * We have to continue here since we don't have the notify
1713 * socket and the application MUST be added to the hash table
1714 * only at that moment.
1715 */
1716 continue;
1717 } else {
1718 /*
1719 * Look for the application in the local wait queue and set the
1720 * notify socket if found.
1721 */
d88aee68 1722 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
f45e313d 1723 &wait_queue.head, head) {
12e2b881 1724 health_code_update();
d0b96690
DG
1725 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1726 wait_node->app->notify_sock = ust_cmd->sock;
1727 cds_list_del(&wait_node->head);
f45e313d 1728 wait_queue.count--;
d0b96690
DG
1729 app = wait_node->app;
1730 free(wait_node);
1731 DBG3("UST app notify socket %d is set", ust_cmd->sock);
1732 break;
1733 }
1734 }
020d7f60
DG
1735
1736 /*
1737 * With no application at this stage the received socket is
1738 * basically useless so close it before we free the cmd data
1739 * structure for good.
1740 */
1741 if (!app) {
1742 ret = close(ust_cmd->sock);
1743 if (ret < 0) {
1744 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1745 }
51dec90d 1746 lttng_fd_put(LTTNG_FD_APPS, 1);
020d7f60 1747 }
7972aab2 1748 free(ust_cmd);
d0b96690
DG
1749 }
1750
1751 if (app) {
d0b96690
DG
1752 /*
1753 * @session_lock_list
1754 *
1755 * Lock the global session list so from the register up to the
1756 * registration done message, no thread can see the application
1757 * and change its state.
1758 */
1759 session_lock_list();
1760 rcu_read_lock();
d88aee68 1761
d0b96690
DG
1762 /*
1763 * Add application to the global hash table. This needs to be
1764 * done before the update to the UST registry can locate the
1765 * application.
1766 */
1767 ust_app_add(app);
d88aee68
DG
1768
1769 /* Set app version. This call will print an error if needed. */
1770 (void) ust_app_version(app);
1771
1772 /* Send notify socket through the notify pipe. */
1773 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
1774 app->notify_sock);
1775 if (ret < 0) {
1776 rcu_read_unlock();
1777 session_unlock_list();
b85dc84c
DG
1778 /*
1779 * No notify thread, stop the UST tracing. However, this is
1780 * not an internal error of the this thread thus setting
1781 * the health error code to a normal exit.
1782 */
1783 err = 0;
d88aee68 1784 goto error;
6620da75 1785 }
d88aee68 1786
d0b96690
DG
1787 /*
1788 * Update newly registered application with the tracing
1789 * registry info already enabled information.
1790 */
1791 update_ust_app(app->sock);
d88aee68
DG
1792
1793 /*
1794 * Don't care about return value. Let the manage apps threads
1795 * handle app unregistration upon socket close.
1796 */
fb45065e 1797 (void) ust_app_register_done(app);
d88aee68
DG
1798
1799 /*
1800 * Even if the application socket has been closed, send the app
1801 * to the thread and unregistration will take place at that
1802 * place.
1803 */
1804 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
d0b96690 1805 if (ret < 0) {
d88aee68
DG
1806 rcu_read_unlock();
1807 session_unlock_list();
b85dc84c
DG
1808 /*
1809 * No apps. thread, stop the UST tracing. However, this is
1810 * not an internal error of the this thread thus setting
1811 * the health error code to a normal exit.
1812 */
1813 err = 0;
d88aee68 1814 goto error;
d0b96690 1815 }
d88aee68 1816
d0b96690
DG
1817 rcu_read_unlock();
1818 session_unlock_list();
099e26bd 1819 }
099e26bd
DG
1820 } while (node != NULL);
1821
12e2b881 1822 health_poll_entry();
099e26bd
DG
1823 /* Futex wait on queue. Blocking call on futex() */
1824 futex_nto1_wait(&ust_cmd_queue.futex);
12e2b881 1825 health_poll_exit();
099e26bd 1826 }
12e2b881
MD
1827 /* Normal exit, no error */
1828 err = 0;
099e26bd
DG
1829
1830error:
d88aee68
DG
1831 /* Clean up wait queue. */
1832 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
f45e313d 1833 &wait_queue.head, head) {
d88aee68 1834 cds_list_del(&wait_node->head);
f45e313d 1835 wait_queue.count--;
d88aee68
DG
1836 free(wait_node);
1837 }
1838
772b8f4d
MD
1839 /* Empty command queue. */
1840 for (;;) {
1841 /* Dequeue command for registration */
1842 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
1843 if (node == NULL) {
1844 break;
1845 }
1846 ust_cmd = caa_container_of(node, struct ust_command, node);
1847 ret = close(ust_cmd->sock);
1848 if (ret < 0) {
1849 PERROR("close ust sock exit dispatch %d", ust_cmd->sock);
1850 }
1851 lttng_fd_put(LTTNG_FD_APPS, 1);
1852 free(ust_cmd);
1853 }
1854
9ad42ec1 1855error_testpoint:
099e26bd 1856 DBG("Dispatch thread dying");
12e2b881
MD
1857 if (err) {
1858 health_error();
1859 ERR("Health error occurred in %s", __func__);
1860 }
8782cc74 1861 health_unregister(health_sessiond);
967e3668 1862 rcu_unregister_thread();
099e26bd
DG
1863 return NULL;
1864}
1865
1866/*
1867 * This thread manage application registration.
1868 */
1869static void *thread_registration_apps(void *data)
1d4b027a 1870{
139ac872 1871 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98
DG
1872 uint32_t revents, nb_fd;
1873 struct lttng_poll_event events;
099e26bd
DG
1874 /*
1875 * Get allocated in this thread, enqueued to a global queue, dequeued and
1876 * freed in the manage apps thread.
1877 */
1878 struct ust_command *ust_cmd = NULL;
1d4b027a 1879
099e26bd 1880 DBG("[thread] Manage application registration started");
1d4b027a 1881
6c71277b 1882 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
927ca06a 1883
e547b070 1884 if (testpoint(sessiond_thread_registration_apps)) {
6993eeb3
CB
1885 goto error_testpoint;
1886 }
8ac94142 1887
1d4b027a
DG
1888 ret = lttcomm_listen_unix_sock(apps_sock);
1889 if (ret < 0) {
76d7553f 1890 goto error_listen;
1d4b027a
DG
1891 }
1892
5eb91c98
DG
1893 /*
1894 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1895 * more will be added to this poll set.
1896 */
d0b96690 1897 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 1898 if (ret < 0) {
76d7553f 1899 goto error_create_poll;
5eb91c98 1900 }
273ea72c 1901
5eb91c98
DG
1902 /* Add the application registration socket */
1903 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1904 if (ret < 0) {
76d7553f 1905 goto error_poll_add;
5eb91c98 1906 }
273ea72c 1907
1d4b027a 1908 /* Notify all applications to register */
0fdd1e2c
DG
1909 ret = notify_ust_apps(1);
1910 if (ret < 0) {
1911 ERR("Failed to notify applications or create the wait shared memory.\n"
54d01ffb
DG
1912 "Execution continues but there might be problem for already\n"
1913 "running applications that wishes to register.");
0fdd1e2c 1914 }
1d4b027a
DG
1915
1916 while (1) {
1917 DBG("Accepting application registration");
273ea72c
DG
1918
1919 /* Inifinite blocking call, waiting for transmission */
88f2b785 1920 restart:
a78af745 1921 health_poll_entry();
5eb91c98 1922 ret = lttng_poll_wait(&events, -1);
a78af745 1923 health_poll_exit();
273ea72c 1924 if (ret < 0) {
88f2b785
MD
1925 /*
1926 * Restart interrupted system call.
1927 */
1928 if (errno == EINTR) {
1929 goto restart;
1930 }
273ea72c
DG
1931 goto error;
1932 }
1933
0d9c5d77
DG
1934 nb_fd = ret;
1935
5eb91c98 1936 for (i = 0; i < nb_fd; i++) {
840cb59c 1937 health_code_update();
139ac872 1938
5eb91c98
DG
1939 /* Fetch once the poll data */
1940 revents = LTTNG_POLL_GETEV(&events, i);
1941 pollfd = LTTNG_POLL_GETFD(&events, i);
273ea72c 1942
fd20dac9
MD
1943 if (!revents) {
1944 /* No activity for this FD (poll implementation). */
1945 continue;
1946 }
1947
5eb91c98 1948 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1949 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1950 if (ret) {
139ac872
MD
1951 err = 0;
1952 goto exit;
90014c57 1953 }
1d4b027a 1954
5eb91c98
DG
1955 /* Event on the registration socket */
1956 if (pollfd == apps_sock) {
03e43155 1957 if (revents & LPOLLIN) {
5eb91c98
DG
1958 sock = lttcomm_accept_unix_sock(apps_sock);
1959 if (sock < 0) {
1960 goto error;
1961 }
099e26bd 1962
16c5c8fa
DG
1963 /*
1964 * Set socket timeout for both receiving and ending.
1965 * app_socket_timeout is in seconds, whereas
1966 * lttcomm_setsockopt_rcv_timeout and
1967 * lttcomm_setsockopt_snd_timeout expect msec as
1968 * parameter.
1969 */
e6142f2e 1970 if (config.app_socket_timeout >= 0) {
28ce0ff2 1971 (void) lttcomm_setsockopt_rcv_timeout(sock,
e6142f2e 1972 config.app_socket_timeout * 1000);
28ce0ff2 1973 (void) lttcomm_setsockopt_snd_timeout(sock,
e6142f2e 1974 config.app_socket_timeout * 1000);
28ce0ff2 1975 }
16c5c8fa 1976
b662582b
DG
1977 /*
1978 * Set the CLOEXEC flag. Return code is useless because
1979 * either way, the show must go on.
1980 */
1981 (void) utils_set_fd_cloexec(sock);
1982
5eb91c98 1983 /* Create UST registration command for enqueuing */
ba7f0ae5 1984 ust_cmd = zmalloc(sizeof(struct ust_command));
5eb91c98 1985 if (ust_cmd == NULL) {
76d7553f 1986 PERROR("ust command zmalloc");
41ed8e47
MD
1987 ret = close(sock);
1988 if (ret) {
1989 PERROR("close");
1990 }
5eb91c98
DG
1991 goto error;
1992 }
1d4b027a 1993
5eb91c98
DG
1994 /*
1995 * Using message-based transmissions to ensure we don't
1996 * have to deal with partially received messages.
1997 */
4063050c
MD
1998 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
1999 if (ret < 0) {
2000 ERR("Exhausted file descriptors allowed for applications.");
2001 free(ust_cmd);
2002 ret = close(sock);
2003 if (ret) {
2004 PERROR("close");
2005 }
2006 sock = -1;
2007 continue;
2008 }
d88aee68 2009
840cb59c 2010 health_code_update();
d0b96690
DG
2011 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
2012 if (ret < 0) {
5eb91c98 2013 free(ust_cmd);
d0b96690 2014 /* Close socket of the application. */
76d7553f
MD
2015 ret = close(sock);
2016 if (ret) {
2017 PERROR("close");
2018 }
4063050c 2019 lttng_fd_put(LTTNG_FD_APPS, 1);
76d7553f 2020 sock = -1;
5eb91c98
DG
2021 continue;
2022 }
840cb59c 2023 health_code_update();
099e26bd 2024
5eb91c98 2025 ust_cmd->sock = sock;
34a2494f 2026 sock = -1;
099e26bd 2027
5eb91c98
DG
2028 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2029 " gid:%d sock:%d name:%s (version %d.%d)",
2030 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
2031 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
2032 ust_cmd->sock, ust_cmd->reg_msg.name,
2033 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
54d01ffb 2034
5eb91c98
DG
2035 /*
2036 * Lock free enqueue the registration request. The red pill
54d01ffb 2037 * has been taken! This apps will be part of the *system*.
5eb91c98 2038 */
8bdee6e2 2039 cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node);
5eb91c98
DG
2040
2041 /*
2042 * Wake the registration queue futex. Implicit memory
8bdee6e2 2043 * barrier with the exchange in cds_wfcq_enqueue.
5eb91c98
DG
2044 */
2045 futex_nto1_wake(&ust_cmd_queue.futex);
03e43155
MD
2046 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2047 ERR("Register apps socket poll error");
2048 goto error;
2049 } else {
2050 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2051 goto error;
5eb91c98
DG
2052 }
2053 }
90014c57 2054 }
1d4b027a
DG
2055 }
2056
139ac872 2057exit:
1d4b027a 2058error:
0fdd1e2c
DG
2059 /* Notify that the registration thread is gone */
2060 notify_ust_apps(0);
2061
a4b35e07 2062 if (apps_sock >= 0) {
76d7553f
MD
2063 ret = close(apps_sock);
2064 if (ret) {
2065 PERROR("close");
2066 }
a4b35e07 2067 }
46c3f085 2068 if (sock >= 0) {
76d7553f
MD
2069 ret = close(sock);
2070 if (ret) {
2071 PERROR("close");
2072 }
4063050c 2073 lttng_fd_put(LTTNG_FD_APPS, 1);
a4b35e07 2074 }
e6142f2e 2075 unlink(config.apps_unix_sock_path.value);
0fdd1e2c 2076
76d7553f 2077error_poll_add:
5eb91c98 2078 lttng_poll_clean(&events);
76d7553f
MD
2079error_listen:
2080error_create_poll:
6993eeb3 2081error_testpoint:
76d7553f 2082 DBG("UST Registration thread cleanup complete");
9ad42ec1
MD
2083 if (err) {
2084 health_error();
2085 ERR("Health error occurred in %s", __func__);
2086 }
8782cc74 2087 health_unregister(health_sessiond);
5eb91c98 2088
1d4b027a
DG
2089 return NULL;
2090}
2091
8c0faa1d 2092/*
3bd1e081 2093 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
d063d709 2094 * exec or it will fails.
8c0faa1d 2095 */
3bd1e081 2096static int spawn_consumer_thread(struct consumer_data *consumer_data)
8c0faa1d 2097{
a23ec3a7 2098 int ret, clock_ret;
ee0b0061
DG
2099 struct timespec timeout;
2100
13a7bce3
JG
2101 /*
2102 * Make sure we set the readiness flag to 0 because we are NOT ready.
2103 * This access to consumer_thread_is_ready does not need to be
2104 * protected by consumer_data.cond_mutex (yet) since the consumer
2105 * management thread has not been started at this point.
2106 */
a23ec3a7 2107 consumer_data->consumer_thread_is_ready = 0;
8c0faa1d 2108
a23ec3a7
DG
2109 /* Setup pthread condition */
2110 ret = pthread_condattr_init(&consumer_data->condattr);
4a15001e 2111 if (ret) {
a23ec3a7
DG
2112 errno = ret;
2113 PERROR("pthread_condattr_init consumer data");
2114 goto error;
2115 }
2116
2117 /*
2118 * Set the monotonic clock in order to make sure we DO NOT jump in time
2119 * between the clock_gettime() call and the timedwait call. See bug #324
2120 * for a more details and how we noticed it.
2121 */
2122 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
4a15001e 2123 if (ret) {
a23ec3a7
DG
2124 errno = ret;
2125 PERROR("pthread_condattr_setclock consumer data");
ee0b0061
DG
2126 goto error;
2127 }
8c0faa1d 2128
a23ec3a7 2129 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
4a15001e 2130 if (ret) {
a23ec3a7
DG
2131 errno = ret;
2132 PERROR("pthread_cond_init consumer data");
2133 goto error;
2134 }
2135
1a1a34b4
MJ
2136 ret = pthread_create(&consumer_data->thread, default_pthread_attr(),
2137 thread_manage_consumer, consumer_data);
4a15001e
MD
2138 if (ret) {
2139 errno = ret;
3bd1e081 2140 PERROR("pthread_create consumer");
ee0b0061 2141 ret = -1;
8c0faa1d
DG
2142 goto error;
2143 }
2144
a23ec3a7
DG
2145 /* We are about to wait on a pthread condition */
2146 pthread_mutex_lock(&consumer_data->cond_mutex);
2147
ee0b0061 2148 /* Get time for sem_timedwait absolute timeout */
389fbf04 2149 clock_ret = lttng_clock_gettime(CLOCK_MONOTONIC, &timeout);
a23ec3a7
DG
2150 /*
2151 * Set the timeout for the condition timed wait even if the clock gettime
2152 * call fails since we might loop on that call and we want to avoid to
2153 * increment the timeout too many times.
2154 */
2155 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
2156
2157 /*
2158 * The following loop COULD be skipped in some conditions so this is why we
2159 * set ret to 0 in order to make sure at least one round of the loop is
2160 * done.
2161 */
2162 ret = 0;
2163
2164 /*
2165 * Loop until the condition is reached or when a timeout is reached. Note
2166 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2167 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2168 * possible. This loop does not take any chances and works with both of
2169 * them.
2170 */
2171 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
2172 if (clock_ret < 0) {
2173 PERROR("clock_gettime spawn consumer");
2174 /* Infinite wait for the consumerd thread to be ready */
2175 ret = pthread_cond_wait(&consumer_data->cond,
2176 &consumer_data->cond_mutex);
2177 } else {
2178 ret = pthread_cond_timedwait(&consumer_data->cond,
2179 &consumer_data->cond_mutex, &timeout);
2180 }
ee0b0061 2181 }
8c0faa1d 2182
a23ec3a7
DG
2183 /* Release the pthread condition */
2184 pthread_mutex_unlock(&consumer_data->cond_mutex);
2185
2186 if (ret != 0) {
2187 errno = ret;
2188 if (ret == ETIMEDOUT) {
4282f9a3
DG
2189 int pth_ret;
2190
ee0b0061
DG
2191 /*
2192 * Call has timed out so we kill the kconsumerd_thread and return
2193 * an error.
2194 */
a23ec3a7
DG
2195 ERR("Condition timed out. The consumer thread was never ready."
2196 " Killing it");
4282f9a3
DG
2197 pth_ret = pthread_cancel(consumer_data->thread);
2198 if (pth_ret < 0) {
3bd1e081 2199 PERROR("pthread_cancel consumer thread");
ee0b0061
DG
2200 }
2201 } else {
a23ec3a7 2202 PERROR("pthread_cond_wait failed consumer thread");
ee0b0061 2203 }
4282f9a3
DG
2204 /* Caller is expecting a negative value on failure. */
2205 ret = -1;
ee0b0061
DG
2206 goto error;
2207 }
2208
3bd1e081
MD
2209 pthread_mutex_lock(&consumer_data->pid_mutex);
2210 if (consumer_data->pid == 0) {
a23ec3a7 2211 ERR("Consumerd did not start");
3bd1e081 2212 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556
DG
2213 goto error;
2214 }
3bd1e081 2215 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556 2216
8c0faa1d
DG
2217 return 0;
2218
2219error:
2220 return ret;
2221}
2222
d9800920 2223/*
3bd1e081 2224 * Join consumer thread
d9800920 2225 */
3bd1e081 2226static int join_consumer_thread(struct consumer_data *consumer_data)
cf3af59e
MD
2227{
2228 void *status;
cf3af59e 2229
e8209f6b
DG
2230 /* Consumer pid must be a real one. */
2231 if (consumer_data->pid > 0) {
c617c0c6 2232 int ret;
3bd1e081 2233 ret = kill(consumer_data->pid, SIGTERM);
cf3af59e 2234 if (ret) {
4a15001e 2235 PERROR("Error killing consumer daemon");
cf3af59e
MD
2236 return ret;
2237 }
3bd1e081 2238 return pthread_join(consumer_data->thread, &status);
cf3af59e
MD
2239 } else {
2240 return 0;
2241 }
2242}
2243
8c0faa1d 2244/*
3bd1e081 2245 * Fork and exec a consumer daemon (consumerd).
8c0faa1d 2246 *
d063d709 2247 * Return pid if successful else -1.
8c0faa1d 2248 */
3bd1e081 2249static pid_t spawn_consumerd(struct consumer_data *consumer_data)
8c0faa1d
DG
2250{
2251 int ret;
2252 pid_t pid;
94c55f17 2253 const char *consumer_to_use;
53086306 2254 const char *verbosity;
94c55f17 2255 struct stat st;
8c0faa1d 2256
3bd1e081 2257 DBG("Spawning consumerd");
c49dc785 2258
8c0faa1d
DG
2259 pid = fork();
2260 if (pid == 0) {
2261 /*
3bd1e081 2262 * Exec consumerd.
8c0faa1d 2263 */
e6142f2e 2264 if (config.verbose_consumer) {
53086306 2265 verbosity = "--verbose";
4421f712 2266 } else if (lttng_opt_quiet) {
53086306 2267 verbosity = "--quiet";
4421f712
DG
2268 } else {
2269 verbosity = "";
53086306 2270 }
4421f712 2271
3bd1e081
MD
2272 switch (consumer_data->type) {
2273 case LTTNG_CONSUMER_KERNEL:
94c55f17 2274 /*
c7704d57
DG
2275 * Find out which consumerd to execute. We will first try the
2276 * 64-bit path, then the sessiond's installation directory, and
2277 * fallback on the 32-bit one,
94c55f17 2278 */
63a799e8 2279 DBG3("Looking for a kernel consumer at these locations:");
59ee5091 2280 DBG3(" 1) %s", config.consumerd64_bin_path.value ? : "NULL");
e6142f2e 2281 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, DEFAULT_CONSUMERD_FILE);
59ee5091 2282 DBG3(" 3) %s", config.consumerd32_bin_path.value ? : "NULL");
e6142f2e 2283 if (stat(config.consumerd64_bin_path.value, &st) == 0) {
63a799e8 2284 DBG3("Found location #1");
e6142f2e
JG
2285 consumer_to_use = config.consumerd64_bin_path.value;
2286 } else if (stat(INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE, &st) == 0) {
63a799e8 2287 DBG3("Found location #2");
e6142f2e 2288 consumer_to_use = INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE;
a1a9f823
JG
2289 } else if (config.consumerd32_bin_path.value &&
2290 stat(config.consumerd32_bin_path.value, &st) == 0) {
63a799e8 2291 DBG3("Found location #3");
e6142f2e 2292 consumer_to_use = config.consumerd32_bin_path.value;
94c55f17 2293 } else {
63a799e8 2294 DBG("Could not find any valid consumerd executable");
4282f9a3 2295 ret = -EINVAL;
3d678709 2296 goto error;
94c55f17
AM
2297 }
2298 DBG("Using kernel consumer at: %s", consumer_to_use);
6e2cc8d8 2299 (void) execl(consumer_to_use,
94c55f17
AM
2300 "lttng-consumerd", verbosity, "-k",
2301 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2302 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
e6142f2e 2303 "--group", config.tracing_group_name.value,
94c55f17 2304 NULL);
3bd1e081 2305 break;
7753dea8
MD
2306 case LTTNG_CONSUMER64_UST:
2307 {
46b23495 2308 if (config.consumerd64_lib_dir.value) {
8f4905da
MD
2309 char *tmp;
2310 size_t tmplen;
ddaec4a2 2311 char *tmpnew;
8f4905da 2312
e8fa9fb0 2313 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
8f4905da
MD
2314 if (!tmp) {
2315 tmp = "";
2316 }
d222983e 2317 tmplen = strlen(config.consumerd64_lib_dir.value) + 1 /* : */ + strlen(tmp);
8f4905da
MD
2318 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2319 if (!tmpnew) {
2320 ret = -ENOMEM;
2321 goto error;
2322 }
e6142f2e 2323 strcat(tmpnew, config.consumerd64_lib_dir.value);
8f4905da
MD
2324 if (tmp[0] != '\0') {
2325 strcat(tmpnew, ":");
2326 strcat(tmpnew, tmp);
2327 }
d222983e 2328 ret = setenv("LD_LIBRARY_PATH", tmpnew, 1);
ddaec4a2 2329 free(tmpnew);
8f4905da
MD
2330 if (ret) {
2331 ret = -errno;
2332 goto error;
2333 }
2334 }
e6142f2e
JG
2335 DBG("Using 64-bit UST consumer at: %s", config.consumerd64_bin_path.value);
2336 (void) execl(config.consumerd64_bin_path.value, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
2337 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2338 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
e6142f2e 2339 "--group", config.tracing_group_name.value,
7753dea8 2340 NULL);
3bd1e081 2341 break;
7753dea8
MD
2342 }
2343 case LTTNG_CONSUMER32_UST:
2344 {
46b23495 2345 if (config.consumerd32_lib_dir.value) {
8f4905da
MD
2346 char *tmp;
2347 size_t tmplen;
ddaec4a2 2348 char *tmpnew;
8f4905da 2349
e8fa9fb0 2350 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
8f4905da
MD
2351 if (!tmp) {
2352 tmp = "";
2353 }
d222983e 2354 tmplen = strlen(config.consumerd32_lib_dir.value) + 1 /* : */ + strlen(tmp);
8f4905da
MD
2355 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2356 if (!tmpnew) {
2357 ret = -ENOMEM;
2358 goto error;
2359 }
e6142f2e 2360 strcat(tmpnew, config.consumerd32_lib_dir.value);
8f4905da
MD
2361 if (tmp[0] != '\0') {
2362 strcat(tmpnew, ":");
2363 strcat(tmpnew, tmp);
2364 }
d222983e 2365 ret = setenv("LD_LIBRARY_PATH", tmpnew, 1);
ddaec4a2 2366 free(tmpnew);
8f4905da
MD
2367 if (ret) {
2368 ret = -errno;
2369 goto error;
2370 }
2371 }
e6142f2e
JG
2372 DBG("Using 32-bit UST consumer at: %s", config.consumerd32_bin_path.value);
2373 (void) execl(config.consumerd32_bin_path.value, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
2374 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2375 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
e6142f2e 2376 "--group", config.tracing_group_name.value,
7753dea8
MD
2377 NULL);
2378 break;
2379 }
3bd1e081 2380 default:
b60e7183 2381 ERR("unknown consumer type");
e9d6b496 2382 errno = 0;
3bd1e081 2383 }
8c0faa1d 2384 if (errno != 0) {
4282f9a3 2385 PERROR("Consumer execl()");
8c0faa1d 2386 }
4282f9a3 2387 /* Reaching this point, we got a failure on our execl(). */
8c0faa1d
DG
2388 exit(EXIT_FAILURE);
2389 } else if (pid > 0) {
2390 ret = pid;
8c0faa1d 2391 } else {
76d7553f 2392 PERROR("start consumer fork");
8c0faa1d 2393 ret = -errno;
8c0faa1d 2394 }
8f4905da 2395error:
8c0faa1d
DG
2396 return ret;
2397}
2398
693bd40b 2399/*
3bd1e081 2400 * Spawn the consumerd daemon and session daemon thread.
693bd40b 2401 */
3bd1e081 2402static int start_consumerd(struct consumer_data *consumer_data)
693bd40b 2403{
c617c0c6 2404 int ret;
edb8b045
DG
2405
2406 /*
2407 * Set the listen() state on the socket since there is a possible race
2408 * between the exec() of the consumer daemon and this call if place in the
2409 * consumer thread. See bug #366 for more details.
2410 */
2411 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
2412 if (ret < 0) {
2413 goto error;
2414 }
693bd40b 2415
3bd1e081
MD
2416 pthread_mutex_lock(&consumer_data->pid_mutex);
2417 if (consumer_data->pid != 0) {
2418 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785
DG
2419 goto end;
2420 }
693bd40b 2421
3bd1e081 2422 ret = spawn_consumerd(consumer_data);
c49dc785 2423 if (ret < 0) {
3bd1e081
MD
2424 ERR("Spawning consumerd failed");
2425 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785 2426 goto error;
693bd40b 2427 }
c49dc785 2428
3bd1e081
MD
2429 /* Setting up the consumer_data pid */
2430 consumer_data->pid = ret;
48842b30 2431 DBG2("Consumer pid %d", consumer_data->pid);
3bd1e081 2432 pthread_mutex_unlock(&consumer_data->pid_mutex);
693bd40b 2433
3bd1e081
MD
2434 DBG2("Spawning consumer control thread");
2435 ret = spawn_consumer_thread(consumer_data);
693bd40b 2436 if (ret < 0) {
3bd1e081 2437 ERR("Fatal error spawning consumer control thread");
693bd40b
DG
2438 goto error;
2439 }
2440
c49dc785 2441end:
693bd40b
DG
2442 return 0;
2443
2444error:
331744e3 2445 /* Cleanup already created sockets on error. */
edb8b045 2446 if (consumer_data->err_sock >= 0) {
c617c0c6
MD
2447 int err;
2448
edb8b045
DG
2449 err = close(consumer_data->err_sock);
2450 if (err < 0) {
2451 PERROR("close consumer data error socket");
2452 }
2453 }
693bd40b
DG
2454 return ret;
2455}
2456
b73401da 2457/*
096102bd 2458 * Setup necessary data for kernel tracer action.
b73401da 2459 */
096102bd 2460static int init_kernel_tracer(void)
b73401da
DG
2461{
2462 int ret;
b73401da 2463
096102bd
DG
2464 /* Modprobe lttng kernel modules */
2465 ret = modprobe_lttng_control();
b73401da 2466 if (ret < 0) {
b73401da
DG
2467 goto error;
2468 }
2469
096102bd
DG
2470 /* Open debugfs lttng */
2471 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2472 if (kernel_tracer_fd < 0) {
2473 DBG("Failed to open %s", module_proc_lttng);
2f77fc4b 2474 goto error_open;
54d01ffb
DG
2475 }
2476
2f77fc4b 2477 /* Validate kernel version */
88076e89
JD
2478 ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version,
2479 &kernel_tracer_abi_version);
2f77fc4b
DG
2480 if (ret < 0) {
2481 goto error_version;
b551a063 2482 }
54d01ffb 2483
2f77fc4b
DG
2484 ret = modprobe_lttng_data();
2485 if (ret < 0) {
2486 goto error_modules;
54d01ffb
DG
2487 }
2488
6e21424e
JR
2489 ret = kernel_supports_ring_buffer_snapshot_sample_positions(
2490 kernel_tracer_fd);
2491 if (ret < 0) {
2492 goto error_modules;
2493 }
2494
2495 if (ret < 1) {
2496 WARN("Kernel tracer does not support buffer monitoring. "
2497 "The monitoring timer of channels in the kernel domain "
2498 "will be set to 0 (disabled).");
2499 }
2500
2f77fc4b
DG
2501 DBG("Kernel tracer fd %d", kernel_tracer_fd);
2502 return 0;
2503
2504error_version:
2505 modprobe_remove_lttng_control();
2506 ret = close(kernel_tracer_fd);
2507 if (ret) {
2508 PERROR("close");
b551a063 2509 }
2f77fc4b 2510 kernel_tracer_fd = -1;
f73fabfd 2511 return LTTNG_ERR_KERN_VERSION;
b551a063 2512
2f77fc4b
DG
2513error_modules:
2514 ret = close(kernel_tracer_fd);
2515 if (ret) {
2516 PERROR("close");
b551a063 2517 }
54d01ffb 2518
2f77fc4b
DG
2519error_open:
2520 modprobe_remove_lttng_control();
54d01ffb
DG
2521
2522error:
2f77fc4b
DG
2523 WARN("No kernel tracer available");
2524 kernel_tracer_fd = -1;
2525 if (!is_root) {
f73fabfd 2526 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2f77fc4b 2527 } else {
f73fabfd 2528 return LTTNG_ERR_KERN_NA;
2f77fc4b 2529 }
54d01ffb
DG
2530}
2531
2f77fc4b 2532
54d01ffb 2533/*
2f77fc4b
DG
2534 * Copy consumer output from the tracing session to the domain session. The
2535 * function also applies the right modification on a per domain basis for the
2536 * trace files destination directory.
36b588ed
MD
2537 *
2538 * Should *NOT* be called with RCU read-side lock held.
54d01ffb 2539 */
2f77fc4b 2540static int copy_session_consumer(int domain, struct ltt_session *session)
54d01ffb
DG
2541{
2542 int ret;
2f77fc4b
DG
2543 const char *dir_name;
2544 struct consumer_output *consumer;
2545
2546 assert(session);
2547 assert(session->consumer);
54d01ffb 2548
b551a063
DG
2549 switch (domain) {
2550 case LTTNG_DOMAIN_KERNEL:
2f77fc4b 2551 DBG3("Copying tracing session consumer output in kernel session");
09a90bcd
DG
2552 /*
2553 * XXX: We should audit the session creation and what this function
2554 * does "extra" in order to avoid a destroy since this function is used
2555 * in the domain session creation (kernel and ust) only. Same for UST
2556 * domain.
2557 */
2558 if (session->kernel_session->consumer) {
6addfa37 2559 consumer_output_put(session->kernel_session->consumer);
09a90bcd 2560 }
2f77fc4b
DG
2561 session->kernel_session->consumer =
2562 consumer_copy_output(session->consumer);
2563 /* Ease our life a bit for the next part */
2564 consumer = session->kernel_session->consumer;
2565 dir_name = DEFAULT_KERNEL_TRACE_DIR;
b551a063 2566 break;
f20baf8e 2567 case LTTNG_DOMAIN_JUL:
5cdb6027 2568 case LTTNG_DOMAIN_LOG4J:
0e115563 2569 case LTTNG_DOMAIN_PYTHON:
b551a063 2570 case LTTNG_DOMAIN_UST:
2f77fc4b 2571 DBG3("Copying tracing session consumer output in UST session");
09a90bcd 2572 if (session->ust_session->consumer) {
6addfa37 2573 consumer_output_put(session->ust_session->consumer);
09a90bcd 2574 }
2f77fc4b
DG
2575 session->ust_session->consumer =
2576 consumer_copy_output(session->consumer);
2577 /* Ease our life a bit for the next part */
2578 consumer = session->ust_session->consumer;
2579 dir_name = DEFAULT_UST_TRACE_DIR;
b551a063
DG
2580 break;
2581 default:
f73fabfd 2582 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
54d01ffb
DG
2583 goto error;
2584 }
2585
2f77fc4b 2586 /* Append correct directory to subdir */
c30ce0b3
CB
2587 strncat(consumer->subdir, dir_name,
2588 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2f77fc4b
DG
2589 DBG3("Copy session consumer subdir %s", consumer->subdir);
2590
f73fabfd 2591 ret = LTTNG_OK;
54d01ffb
DG
2592
2593error:
2594 return ret;
2595}
2596
00e2e675 2597/*
2f77fc4b 2598 * Create an UST session and add it to the session ust list.
36b588ed
MD
2599 *
2600 * Should *NOT* be called with RCU read-side lock held.
00e2e675 2601 */
2f77fc4b
DG
2602static int create_ust_session(struct ltt_session *session,
2603 struct lttng_domain *domain)
00e2e675
DG
2604{
2605 int ret;
2f77fc4b 2606 struct ltt_ust_session *lus = NULL;
00e2e675 2607
a4b92340 2608 assert(session);
2f77fc4b
DG
2609 assert(domain);
2610 assert(session->consumer);
a4b92340 2611
2f77fc4b 2612 switch (domain->type) {
f20baf8e 2613 case LTTNG_DOMAIN_JUL:
5cdb6027 2614 case LTTNG_DOMAIN_LOG4J:
0e115563 2615 case LTTNG_DOMAIN_PYTHON:
2f77fc4b
DG
2616 case LTTNG_DOMAIN_UST:
2617 break;
2618 default:
2619 ERR("Unknown UST domain on create session %d", domain->type);
f73fabfd 2620 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
00e2e675
DG
2621 goto error;
2622 }
2623
2f77fc4b
DG
2624 DBG("Creating UST session");
2625
dec56f6c 2626 lus = trace_ust_create_session(session->id);
2f77fc4b 2627 if (lus == NULL) {
f73fabfd 2628 ret = LTTNG_ERR_UST_SESS_FAIL;
a4b92340
DG
2629 goto error;
2630 }
2631
2f77fc4b
DG
2632 lus->uid = session->uid;
2633 lus->gid = session->gid;
2bba9e53 2634 lus->output_traces = session->output_traces;
27babd3a 2635 lus->snapshot_mode = session->snapshot_mode;
ecc48a90 2636 lus->live_timer_interval = session->live_timer;
2f77fc4b 2637 session->ust_session = lus;
d7ba1388 2638 if (session->shm_path[0]) {
3d071855
MD
2639 strncpy(lus->root_shm_path, session->shm_path,
2640 sizeof(lus->root_shm_path));
2641 lus->root_shm_path[sizeof(lus->root_shm_path) - 1] = '\0';
d7ba1388
MD
2642 strncpy(lus->shm_path, session->shm_path,
2643 sizeof(lus->shm_path));
2644 lus->shm_path[sizeof(lus->shm_path) - 1] = '\0';
2645 strncat(lus->shm_path, "/ust",
2646 sizeof(lus->shm_path) - strlen(lus->shm_path) - 1);
2647 }
2f77fc4b
DG
2648 /* Copy session output to the newly created UST session */
2649 ret = copy_session_consumer(domain->type, session);
f73fabfd 2650 if (ret != LTTNG_OK) {
00e2e675
DG
2651 goto error;
2652 }
2653
f73fabfd 2654 return LTTNG_OK;
00e2e675
DG
2655
2656error:
2f77fc4b
DG
2657 free(lus);
2658 session->ust_session = NULL;
00e2e675
DG
2659 return ret;
2660}
2661
2662/*
2f77fc4b 2663 * Create a kernel tracer session then create the default channel.
00e2e675 2664 */
2f77fc4b 2665static int create_kernel_session(struct ltt_session *session)
00e2e675
DG
2666{
2667 int ret;
a4b92340 2668
2f77fc4b 2669 DBG("Creating kernel session");
00e2e675 2670
2f77fc4b
DG
2671 ret = kernel_create_session(session, kernel_tracer_fd);
2672 if (ret < 0) {
f73fabfd 2673 ret = LTTNG_ERR_KERN_SESS_FAIL;
00e2e675
DG
2674 goto error;
2675 }
2676
2f77fc4b
DG
2677 /* Code flow safety */
2678 assert(session->kernel_session);
2679
2680 /* Copy session output to the newly created Kernel session */
2681 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
f73fabfd 2682 if (ret != LTTNG_OK) {
a4b92340
DG
2683 goto error;
2684 }
2685
2f77fc4b
DG
2686 session->kernel_session->uid = session->uid;
2687 session->kernel_session->gid = session->gid;
2bba9e53 2688 session->kernel_session->output_traces = session->output_traces;
27babd3a 2689 session->kernel_session->snapshot_mode = session->snapshot_mode;
00e2e675 2690
f73fabfd 2691 return LTTNG_OK;
00e2e675 2692
2f77fc4b
DG
2693error:
2694 trace_kernel_destroy_session(session->kernel_session);
2695 session->kernel_session = NULL;
2696 return ret;
2697}
00e2e675 2698
2f77fc4b
DG
2699/*
2700 * Count number of session permitted by uid/gid.
2701 */
2702static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2703{
2704 unsigned int i = 0;
2705 struct ltt_session *session;
e32d7f27 2706 const struct ltt_session_list *session_list = session_get_list();
07424f16 2707
2f77fc4b
DG
2708 DBG("Counting number of available session for UID %d GID %d",
2709 uid, gid);
e32d7f27
JG
2710 cds_list_for_each_entry(session, &session_list->head, list) {
2711 if (!session_get(session)) {
2f77fc4b
DG
2712 continue;
2713 }
e32d7f27
JG
2714 session_lock(session);
2715 /* Only count the sessions the user can control. */
2716 if (session_access_ok(session, uid, gid) &&
2717 !session->destroyed) {
2718 i++;
2719 }
2720 session_unlock(session);
2721 session_put(session);
a4b92340 2722 }
2f77fc4b 2723 return i;
00e2e675
DG
2724}
2725
dcabc190
FD
2726static int receive_userspace_probe(struct command_ctx *cmd_ctx, int sock,
2727 int *sock_error, struct lttng_event *event)
2728{
2729 int fd, ret;
2730 struct lttng_userspace_probe_location *probe_location;
87597c2c 2731 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
dcabc190
FD
2732 struct lttng_dynamic_buffer probe_location_buffer;
2733 struct lttng_buffer_view buffer_view;
2734
2735 /*
2736 * Create a buffer to store the serialized version of the probe
2737 * location.
2738 */
2739 lttng_dynamic_buffer_init(&probe_location_buffer);
2740 ret = lttng_dynamic_buffer_set_size(&probe_location_buffer,
2741 cmd_ctx->lsm->u.enable.userspace_probe_location_len);
2742 if (ret) {
2743 ret = LTTNG_ERR_NOMEM;
2744 goto error;
2745 }
2746
2747 /*
2748 * Receive the probe location.
2749 */
2750 ret = lttcomm_recv_unix_sock(sock, probe_location_buffer.data,
2751 probe_location_buffer.size);
2752 if (ret <= 0) {
2753 DBG("Nothing recv() from client var len data... continuing");
2754 *sock_error = 1;
2755 lttng_dynamic_buffer_reset(&probe_location_buffer);
2756 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2757 goto error;
2758 }
2759
2760 buffer_view = lttng_buffer_view_from_dynamic_buffer(
2761 &probe_location_buffer, 0, probe_location_buffer.size);
2762
2763 /*
2764 * Extract the probe location from the serialized version.
2765 */
2766 ret = lttng_userspace_probe_location_create_from_buffer(
2767 &buffer_view, &probe_location);
2768 if (ret < 0) {
2769 WARN("Failed to create a userspace probe location from the received buffer");
2770 lttng_dynamic_buffer_reset( &probe_location_buffer);
2771 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2772 goto error;
2773 }
2774
2775 /*
2776 * Receive the file descriptor to the target binary from the client.
2777 */
2778 DBG("Receiving userspace probe target FD from client ...");
2779 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
2780 if (ret <= 0) {
2781 DBG("Nothing recv() from client userspace probe fd... continuing");
2782 *sock_error = 1;
2783 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2784 goto error;
2785 }
2786
2787 /*
2788 * Set the file descriptor received from the client through the unix
2789 * socket in the probe location.
2790 */
2791 lookup = lttng_userspace_probe_location_get_lookup_method(probe_location);
2792 if (!lookup) {
2793 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2794 goto error;
2795 }
2796
2797 /*
2798 * From the kernel tracer's perspective, all userspace probe event types
2799 * are all the same: a file and an offset.
2800 */
2801 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) {
2802 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
2803 ret = lttng_userspace_probe_location_function_set_binary_fd(
2804 probe_location, fd);
2805 break;
2806 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
2807 ret = lttng_userspace_probe_location_tracepoint_set_binary_fd(
2808 probe_location, fd);
2809 break;
2810 default:
2811 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2812 goto error;
2813 }
2814
2815 if (ret) {
2816 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2817 goto error;
2818 }
2819
2820 /* Attach the probe location to the event. */
2821 ret = lttng_event_set_userspace_probe_location(event, probe_location);
2822 if (ret) {
2823 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2824 goto error;
2825 }
2826
2827 lttng_dynamic_buffer_reset(&probe_location_buffer);
2828error:
2829 return ret;
2830}
2831
5c408ad8
JD
2832/*
2833 * Check if the current kernel tracer supports the session rotation feature.
2834 * Return 1 if it does, 0 otherwise.
2835 */
2836static int check_rotate_compatible(void)
2837{
2838 int ret = 1;
2839
2840 if (kernel_tracer_version.major != 2 || kernel_tracer_version.minor < 11) {
2841 DBG("Kernel tracer version is not compatible with the rotation feature");
2842 ret = 0;
2843 }
2844
2845 return ret;
2846}
2847
54d01ffb
DG
2848/*
2849 * Process the command requested by the lttng client within the command
2850 * context structure. This function make sure that the return structure (llm)
2851 * is set and ready for transmission before returning.
2852 *
2853 * Return any error encountered or 0 for success.
53a80697
MD
2854 *
2855 * "sock" is only used for special-case var. len data.
36b588ed
MD
2856 *
2857 * Should *NOT* be called with RCU read-side lock held.
54d01ffb 2858 */
53a80697
MD
2859static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2860 int *sock_error)
54d01ffb 2861{
f73fabfd 2862 int ret = LTTNG_OK;
44d3bd01 2863 int need_tracing_session = 1;
2e09ba09 2864 int need_domain;
54d01ffb
DG
2865
2866 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2867
3745d315
MD
2868 assert(!rcu_read_ongoing());
2869
53a80697
MD
2870 *sock_error = 0;
2871
2e09ba09
MD
2872 switch (cmd_ctx->lsm->cmd_type) {
2873 case LTTNG_CREATE_SESSION:
27babd3a 2874 case LTTNG_CREATE_SESSION_SNAPSHOT:
ecc48a90 2875 case LTTNG_CREATE_SESSION_LIVE:
2e09ba09
MD
2876 case LTTNG_DESTROY_SESSION:
2877 case LTTNG_LIST_SESSIONS:
2878 case LTTNG_LIST_DOMAINS:
2879 case LTTNG_START_TRACE:
2880 case LTTNG_STOP_TRACE:
6d805429 2881 case LTTNG_DATA_PENDING:
da3c9ec1
DG
2882 case LTTNG_SNAPSHOT_ADD_OUTPUT:
2883 case LTTNG_SNAPSHOT_DEL_OUTPUT:
2884 case LTTNG_SNAPSHOT_LIST_OUTPUT:
2885 case LTTNG_SNAPSHOT_RECORD:
fb198a11 2886 case LTTNG_SAVE_SESSION:
d7ba1388 2887 case LTTNG_SET_SESSION_SHM_PATH:
eded6438 2888 case LTTNG_REGENERATE_METADATA:
c2561365 2889 case LTTNG_REGENERATE_STATEDUMP:
b3530820
JG
2890 case LTTNG_REGISTER_TRIGGER:
2891 case LTTNG_UNREGISTER_TRIGGER:
5c408ad8 2892 case LTTNG_ROTATE_SESSION:
d68c9a04 2893 case LTTNG_ROTATION_GET_INFO:
259c2674 2894 case LTTNG_ROTATION_SET_SCHEDULE:
66ea93b1 2895 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
2e09ba09 2896 need_domain = 0;
3aace903 2897 break;
2e09ba09
MD
2898 default:
2899 need_domain = 1;
2900 }
2901
e6142f2e 2902 if (config.no_kernel && need_domain
2e09ba09 2903 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
531d29f9 2904 if (!is_root) {
f73fabfd 2905 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
531d29f9 2906 } else {
f73fabfd 2907 ret = LTTNG_ERR_KERN_NA;
531d29f9 2908 }
4fba7219
DG
2909 goto error;
2910 }
2911
8d3113b2
DG
2912 /* Deny register consumer if we already have a spawned consumer. */
2913 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2914 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2915 if (kconsumer_data.pid > 0) {
f73fabfd 2916 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
fa317f24 2917 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
8d3113b2
DG
2918 goto error;
2919 }
2920 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2921 }
2922
54d01ffb
DG
2923 /*
2924 * Check for command that don't needs to allocate a returned payload. We do
44d3bd01 2925 * this here so we don't have to make the call for no payload at each
54d01ffb
DG
2926 * command.
2927 */
2928 switch(cmd_ctx->lsm->cmd_type) {
2929 case LTTNG_LIST_SESSIONS:
2930 case LTTNG_LIST_TRACEPOINTS:
f37d259d 2931 case LTTNG_LIST_TRACEPOINT_FIELDS:
54d01ffb
DG
2932 case LTTNG_LIST_DOMAINS:
2933 case LTTNG_LIST_CHANNELS:
2934 case LTTNG_LIST_EVENTS:
834978fd 2935 case LTTNG_LIST_SYSCALLS:
a5dfbb9d 2936 case LTTNG_LIST_TRACKER_PIDS:
5cd0780d 2937 case LTTNG_DATA_PENDING:
5c408ad8 2938 case LTTNG_ROTATE_SESSION:
d68c9a04 2939 case LTTNG_ROTATION_GET_INFO:
66ea93b1 2940 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
54d01ffb
DG
2941 break;
2942 default:
2943 /* Setup lttng message with no payload */
6e10c9b9 2944 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0);
54d01ffb
DG
2945 if (ret < 0) {
2946 /* This label does not try to unlock the session */
2947 goto init_setup_error;
2948 }
2949 }
2950
2951 /* Commands that DO NOT need a session. */
2952 switch (cmd_ctx->lsm->cmd_type) {
54d01ffb 2953 case LTTNG_CREATE_SESSION:
27babd3a 2954 case LTTNG_CREATE_SESSION_SNAPSHOT:
ecc48a90 2955 case LTTNG_CREATE_SESSION_LIVE:
54d01ffb
DG
2956 case LTTNG_LIST_SESSIONS:
2957 case LTTNG_LIST_TRACEPOINTS:
834978fd 2958 case LTTNG_LIST_SYSCALLS:
f37d259d 2959 case LTTNG_LIST_TRACEPOINT_FIELDS:
fb198a11 2960 case LTTNG_SAVE_SESSION:
b3530820
JG
2961 case LTTNG_REGISTER_TRIGGER:
2962 case LTTNG_UNREGISTER_TRIGGER:
44d3bd01 2963 need_tracing_session = 0;
54d01ffb
DG
2964 break;
2965 default:
2966 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
256a5576
MD
2967 /*
2968 * We keep the session list lock across _all_ commands
2969 * for now, because the per-session lock does not
2970 * handle teardown properly.
2971 */
74babd95 2972 session_lock_list();
54d01ffb
DG
2973 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2974 if (cmd_ctx->session == NULL) {
bba2d65f 2975 ret = LTTNG_ERR_SESS_NOT_FOUND;
54d01ffb
DG
2976 goto error;
2977 } else {
2978 /* Acquire lock for the session */
2979 session_lock(cmd_ctx->session);
2980 }
2981 break;
2982 }
b389abbe 2983
5f3ecf22
DG
2984 /*
2985 * Commands that need a valid session but should NOT create one if none
2986 * exists. Instead of creating one and destroying it when the command is
2987 * handled, process that right before so we save some round trip in useless
2988 * code path.
2989 */
2990 switch (cmd_ctx->lsm->cmd_type) {
2991 case LTTNG_DISABLE_CHANNEL:
2992 case LTTNG_DISABLE_EVENT:
5f3ecf22
DG
2993 switch (cmd_ctx->lsm->domain.type) {
2994 case LTTNG_DOMAIN_KERNEL:
2995 if (!cmd_ctx->session->kernel_session) {
2996 ret = LTTNG_ERR_NO_CHANNEL;
2997 goto error;
2998 }
2999 break;
3000 case LTTNG_DOMAIN_JUL:
5cdb6027 3001 case LTTNG_DOMAIN_LOG4J:
0e115563 3002 case LTTNG_DOMAIN_PYTHON:
5f3ecf22
DG
3003 case LTTNG_DOMAIN_UST:
3004 if (!cmd_ctx->session->ust_session) {
3005 ret = LTTNG_ERR_NO_CHANNEL;
3006 goto error;
3007 }
3008 break;
3009 default:
3010 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
3011 goto error;
3012 }
3013 default:
3014 break;
3015 }
3016
2e09ba09
MD
3017 if (!need_domain) {
3018 goto skip_domain;
3019 }
a4b92340 3020
54d01ffb
DG
3021 /*
3022 * Check domain type for specific "pre-action".
3023 */
3024 switch (cmd_ctx->lsm->domain.type) {
3025 case LTTNG_DOMAIN_KERNEL:
d1f1c568 3026 if (!is_root) {
f73fabfd 3027 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
d1f1c568
DG
3028 goto error;
3029 }
3030
54d01ffb 3031 /* Kernel tracer check */
a4b35e07 3032 if (kernel_tracer_fd == -1) {
54d01ffb 3033 /* Basically, load kernel tracer modules */
096102bd
DG
3034 ret = init_kernel_tracer();
3035 if (ret != 0) {
54d01ffb
DG
3036 goto error;
3037 }
3038 }
5eb91c98 3039
5c827ce0
DG
3040 /* Consumer is in an ERROR state. Report back to client */
3041 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
f73fabfd 3042 ret = LTTNG_ERR_NO_KERNCONSUMERD;
5c827ce0
DG
3043 goto error;
3044 }
3045
54d01ffb 3046 /* Need a session for kernel command */
44d3bd01 3047 if (need_tracing_session) {
54d01ffb 3048 if (cmd_ctx->session->kernel_session == NULL) {
6df2e2c9 3049 ret = create_kernel_session(cmd_ctx->session);
5eb91c98 3050 if (ret < 0) {
f73fabfd 3051 ret = LTTNG_ERR_KERN_SESS_FAIL;
5eb91c98
DG
3052 goto error;
3053 }
b389abbe 3054 }
7d29a247 3055
54d01ffb 3056 /* Start the kernel consumer daemon */
3bd1e081
MD
3057 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3058 if (kconsumer_data.pid == 0 &&
785d2d0d 3059 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3bd1e081
MD
3060 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3061 ret = start_consumerd(&kconsumer_data);
7d29a247 3062 if (ret < 0) {
f73fabfd 3063 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
54d01ffb 3064 goto error;
950131af 3065 }
5c827ce0 3066 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3ff2ecac
MD
3067 } else {
3068 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
33a2b854 3069 }
173af62f 3070
a4b92340
DG
3071 /*
3072 * The consumer was just spawned so we need to add the socket to
3073 * the consumer output of the session if exist.
3074 */
3075 ret = consumer_create_socket(&kconsumer_data,
3076 cmd_ctx->session->kernel_session->consumer);
3077 if (ret < 0) {
3078 goto error;
173af62f 3079 }
0d0c377a 3080 }
5c827ce0 3081
54d01ffb 3082 break;
b9dfb167 3083 case LTTNG_DOMAIN_JUL:
5cdb6027 3084 case LTTNG_DOMAIN_LOG4J:
0e115563 3085 case LTTNG_DOMAIN_PYTHON:
2bdd86d4 3086 case LTTNG_DOMAIN_UST:
44d3bd01 3087 {
b51ec5b4
MD
3088 if (!ust_app_supported()) {
3089 ret = LTTNG_ERR_NO_UST;
3090 goto error;
3091 }
5c827ce0
DG
3092 /* Consumer is in an ERROR state. Report back to client */
3093 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
f73fabfd 3094 ret = LTTNG_ERR_NO_USTCONSUMERD;
5c827ce0
DG
3095 goto error;
3096 }
3097
44d3bd01 3098 if (need_tracing_session) {
a4b92340 3099 /* Create UST session if none exist. */
f6a9efaa 3100 if (cmd_ctx->session->ust_session == NULL) {
44d3bd01 3101 ret = create_ust_session(cmd_ctx->session,
6df2e2c9 3102 &cmd_ctx->lsm->domain);
f73fabfd 3103 if (ret != LTTNG_OK) {
44d3bd01
DG
3104 goto error;
3105 }
3106 }
00e2e675 3107
7753dea8
MD
3108 /* Start the UST consumer daemons */
3109 /* 64-bit */
3110 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
e6142f2e 3111 if (config.consumerd64_bin_path.value &&
7753dea8 3112 ustconsumer64_data.pid == 0 &&
785d2d0d 3113 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
7753dea8
MD
3114 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3115 ret = start_consumerd(&ustconsumer64_data);
2bdd86d4 3116 if (ret < 0) {
f73fabfd 3117 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
173af62f 3118 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2bdd86d4
MD
3119 goto error;
3120 }
48842b30 3121
173af62f 3122 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
5c827ce0 3123 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3ff2ecac 3124 } else {
7753dea8
MD
3125 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3126 }
173af62f
DG
3127
3128 /*
3129 * Setup socket for consumer 64 bit. No need for atomic access
3130 * since it was set above and can ONLY be set in this thread.
3131 */
a4b92340
DG
3132 ret = consumer_create_socket(&ustconsumer64_data,
3133 cmd_ctx->session->ust_session->consumer);
3134 if (ret < 0) {
3135 goto error;
173af62f
DG
3136 }
3137
7753dea8 3138 /* 32-bit */
385b881b 3139 pthread_mutex_lock(&ustconsumer32_data.pid_mutex);
e6142f2e 3140 if (config.consumerd32_bin_path.value &&
7753dea8 3141 ustconsumer32_data.pid == 0 &&
785d2d0d 3142 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
7753dea8
MD
3143 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3144 ret = start_consumerd(&ustconsumer32_data);
3145 if (ret < 0) {
f73fabfd 3146 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
173af62f 3147 uatomic_set(&ust_consumerd32_fd, -EINVAL);
7753dea8
MD
3148 goto error;
3149 }
5c827ce0 3150
173af62f 3151 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
5c827ce0 3152 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
7753dea8
MD
3153 } else {
3154 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2bdd86d4 3155 }
173af62f
DG
3156
3157 /*
92816cc3 3158 * Setup socket for consumer 32 bit. No need for atomic access
173af62f
DG
3159 * since it was set above and can ONLY be set in this thread.
3160 */
a4b92340
DG
3161 ret = consumer_create_socket(&ustconsumer32_data,
3162 cmd_ctx->session->ust_session->consumer);
3163 if (ret < 0) {
3164 goto error;
173af62f 3165 }
44d3bd01
DG
3166 }
3167 break;
48842b30 3168 }
54d01ffb 3169 default:
54d01ffb
DG
3170 break;
3171 }
2e09ba09 3172skip_domain:
33a2b854 3173
5c827ce0
DG
3174 /* Validate consumer daemon state when start/stop trace command */
3175 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
3176 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
3177 switch (cmd_ctx->lsm->domain.type) {
e7619875
JG
3178 case LTTNG_DOMAIN_NONE:
3179 break;
b9dfb167 3180 case LTTNG_DOMAIN_JUL:
5cdb6027 3181 case LTTNG_DOMAIN_LOG4J:
0e115563 3182 case LTTNG_DOMAIN_PYTHON:
5c827ce0
DG
3183 case LTTNG_DOMAIN_UST:
3184 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
f73fabfd 3185 ret = LTTNG_ERR_NO_USTCONSUMERD;
5c827ce0
DG
3186 goto error;
3187 }
3188 break;
3189 case LTTNG_DOMAIN_KERNEL:
3190 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
f73fabfd 3191 ret = LTTNG_ERR_NO_KERNCONSUMERD;
5c827ce0
DG
3192 goto error;
3193 }
3194 break;
825535cc
JG
3195 default:
3196 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
3197 goto error;
5c827ce0
DG
3198 }
3199 }
3200
8e0af1b4
MD
3201 /*
3202 * Check that the UID or GID match that of the tracing session.
3203 * The root user can interact with all sessions.
3204 */
3205 if (need_tracing_session) {
3206 if (!session_access_ok(cmd_ctx->session,
730389d9 3207 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
e32d7f27
JG
3208 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)) ||
3209 cmd_ctx->session->destroyed) {
f73fabfd 3210 ret = LTTNG_ERR_EPERM;
8e0af1b4
MD
3211 goto error;
3212 }
3213 }
3214
ffe60014
DG
3215 /*
3216 * Send relayd information to consumer as soon as we have a domain and a
3217 * session defined.
3218 */
3219 if (cmd_ctx->session && need_domain) {
3220 /*
3221 * Setup relayd if not done yet. If the relayd information was already
3222 * sent to the consumer, this call will gracefully return.
3223 */
3224 ret = cmd_setup_relayd(cmd_ctx->session);
3225 if (ret != LTTNG_OK) {
3226 goto error;
3227 }
3228 }
3229
54d01ffb
DG
3230 /* Process by command type */
3231 switch (cmd_ctx->lsm->cmd_type) {
3232 case LTTNG_ADD_CONTEXT:
3233 {
2001793c
JG
3234 /*
3235 * An LTTNG_ADD_CONTEXT command might have a supplementary
3236 * payload if the context being added is an application context.
3237 */
3238 if (cmd_ctx->lsm->u.context.ctx.ctx ==
3239 LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
3240 char *provider_name = NULL, *context_name = NULL;
3241 size_t provider_name_len =
3242 cmd_ctx->lsm->u.context.provider_name_len;
3243 size_t context_name_len =
3244 cmd_ctx->lsm->u.context.context_name_len;
3245
3246 if (provider_name_len == 0 || context_name_len == 0) {
3247 /*
3248 * Application provider and context names MUST
3249 * be provided.
3250 */
3251 ret = -LTTNG_ERR_INVALID;
3252 goto error;
3253 }
3254
3255 provider_name = zmalloc(provider_name_len + 1);
3256 if (!provider_name) {
3257 ret = -LTTNG_ERR_NOMEM;
3258 goto error;
3259 }
e0f4d107
JG
3260 cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name =
3261 provider_name;
2001793c
JG
3262
3263 context_name = zmalloc(context_name_len + 1);
3264 if (!context_name) {
3265 ret = -LTTNG_ERR_NOMEM;
3266 goto error_add_context;
3267 }
e0f4d107
JG
3268 cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name =
3269 context_name;
2001793c
JG
3270
3271 ret = lttcomm_recv_unix_sock(sock, provider_name,
3272 provider_name_len);
3273 if (ret < 0) {
3274 goto error_add_context;
3275 }
3276
3277 ret = lttcomm_recv_unix_sock(sock, context_name,
3278 context_name_len);
3279 if (ret < 0) {
3280 goto error_add_context;
3281 }
2001793c
JG
3282 }
3283
3284 /*
3285 * cmd_add_context assumes ownership of the provider and context
3286 * names.
3287 */
3288 ret = cmd_add_context(cmd_ctx->session,
3289 cmd_ctx->lsm->domain.type,
54d01ffb 3290 cmd_ctx->lsm->u.context.channel_name,
2001793c
JG
3291 &cmd_ctx->lsm->u.context.ctx,
3292 kernel_poll_pipe[1]);
3293
3294 cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name = NULL;
3295 cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name = NULL;
3296error_add_context:
3297 free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name);
3298 free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name);
3299 if (ret < 0) {
3300 goto error;
3301 }
54d01ffb
DG
3302 break;
3303 }
3304 case LTTNG_DISABLE_CHANNEL:
3305 {
3306 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3307 cmd_ctx->lsm->u.disable.channel_name);
3308 break;
3309 }
3310 case LTTNG_DISABLE_EVENT:
3311 {
47b7e2f9
JG
3312
3313 /*
3314 * FIXME: handle filter; for now we just receive the filter's
3315 * bytecode along with the filter expression which are sent by
3316 * liblttng-ctl and discard them.
3317 *
3318 * This fixes an issue where the client may block while sending
3319 * the filter payload and encounter an error because the session
3320 * daemon closes the socket without ever handling this data.
3321 */
3322 size_t count = cmd_ctx->lsm->u.disable.expression_len +
3323 cmd_ctx->lsm->u.disable.bytecode_len;
3324
3325 if (count) {
3326 char data[LTTNG_FILTER_MAX_LEN];
3327
3328 DBG("Discarding disable event command payload of size %zu", count);
3329 while (count) {
3330 ret = lttcomm_recv_unix_sock(sock, data,
3331 count > sizeof(data) ? sizeof(data) : count);
3332 if (ret < 0) {
3333 goto error;
3334 }
3335
3336 count -= (size_t) ret;
3337 }
3338 }
6e911cad 3339 /* FIXME: passing packed structure to non-packed pointer */
54d01ffb
DG
3340 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3341 cmd_ctx->lsm->u.disable.channel_name,
6e911cad 3342 &cmd_ctx->lsm->u.disable.event);
33a2b854
DG
3343 break;
3344 }
54d01ffb
DG
3345 case LTTNG_ENABLE_CHANNEL:
3346 {
b3530820
JG
3347 cmd_ctx->lsm->u.channel.chan.attr.extended.ptr =
3348 (struct lttng_channel_extended *) &cmd_ctx->lsm->u.channel.extended;
7972aab2 3349 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
b3530820
JG
3350 &cmd_ctx->lsm->u.channel.chan,
3351 kernel_poll_pipe[1]);
54d01ffb
DG
3352 break;
3353 }
ccf10263
MD
3354 case LTTNG_TRACK_PID:
3355 {
3356 ret = cmd_track_pid(cmd_ctx->session,
3357 cmd_ctx->lsm->domain.type,
3358 cmd_ctx->lsm->u.pid_tracker.pid);
3359 break;
3360 }
3361 case LTTNG_UNTRACK_PID:
3362 {
3363 ret = cmd_untrack_pid(cmd_ctx->session,
3364 cmd_ctx->lsm->domain.type,
3365 cmd_ctx->lsm->u.pid_tracker.pid);
3366 break;
3367 }
54d01ffb
DG
3368 case LTTNG_ENABLE_EVENT:
3369 {
dcabc190 3370 struct lttng_event *ev = NULL;
882ef835
JI
3371 struct lttng_event_exclusion *exclusion = NULL;
3372 struct lttng_filter_bytecode *bytecode = NULL;
6b453b5e 3373 char *filter_expression = NULL;
882ef835 3374
67676bd8
DG
3375 /* Handle exclusion events and receive it from the client. */
3376 if (cmd_ctx->lsm->u.enable.exclusion_count > 0) {
3377 size_t count = cmd_ctx->lsm->u.enable.exclusion_count;
3378
3379 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
3380 (count * LTTNG_SYMBOL_NAME_LEN));
3381 if (!exclusion) {
3382 ret = LTTNG_ERR_EXCLUSION_NOMEM;
3383 goto error;
882ef835 3384 }
882ef835 3385
67676bd8
DG
3386 DBG("Receiving var len exclusion event list from client ...");
3387 exclusion->count = count;
3388 ret = lttcomm_recv_unix_sock(sock, exclusion->names,
3389 count * LTTNG_SYMBOL_NAME_LEN);
3390 if (ret <= 0) {
3391 DBG("Nothing recv() from client var len data... continuing");
3392 *sock_error = 1;
3393 free(exclusion);
3394 ret = LTTNG_ERR_EXCLUSION_INVAL;
3395 goto error;
3396 }
3397 }
3398
6b453b5e
JG
3399 /* Get filter expression from client. */
3400 if (cmd_ctx->lsm->u.enable.expression_len > 0) {
3401 size_t expression_len =
3402 cmd_ctx->lsm->u.enable.expression_len;
3403
3404 if (expression_len > LTTNG_FILTER_MAX_LEN) {
3405 ret = LTTNG_ERR_FILTER_INVAL;
3406 free(exclusion);
3407 goto error;
3408 }
3409
3410 filter_expression = zmalloc(expression_len);
3411 if (!filter_expression) {
3412 free(exclusion);
3413 ret = LTTNG_ERR_FILTER_NOMEM;
3414 goto error;
3415 }
3416
3417 /* Receive var. len. data */
3418 DBG("Receiving var len filter's expression from client ...");
3419 ret = lttcomm_recv_unix_sock(sock, filter_expression,
3420 expression_len);
3421 if (ret <= 0) {
dcabc190 3422 DBG("Nothing recv() from client var len data... continuing");
6b453b5e
JG
3423 *sock_error = 1;
3424 free(filter_expression);
3425 free(exclusion);
3426 ret = LTTNG_ERR_FILTER_INVAL;
3427 goto error;
3428 }
3429 }
3430
67676bd8
DG
3431 /* Handle filter and get bytecode from client. */
3432 if (cmd_ctx->lsm->u.enable.bytecode_len > 0) {
3433 size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len;
3434
3435 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
3436 ret = LTTNG_ERR_FILTER_INVAL;
49d21f93 3437 free(filter_expression);
67676bd8
DG
3438 free(exclusion);
3439 goto error;
3440 }
3441
3442 bytecode = zmalloc(bytecode_len);
3443 if (!bytecode) {
49d21f93 3444 free(filter_expression);
67676bd8
DG
3445 free(exclusion);
3446 ret = LTTNG_ERR_FILTER_NOMEM;
3447 goto error;
882ef835
JI
3448 }
3449
67676bd8
DG
3450 /* Receive var. len. data */
3451 DBG("Receiving var len filter's bytecode from client ...");
3452 ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len);
3453 if (ret <= 0) {
dcabc190 3454 DBG("Nothing recv() from client var len data... continuing");
67676bd8 3455 *sock_error = 1;
49d21f93 3456 free(filter_expression);
67676bd8
DG
3457 free(bytecode);
3458 free(exclusion);
3459 ret = LTTNG_ERR_FILTER_INVAL;
3460 goto error;
3461 }
3462
3463 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
49d21f93 3464 free(filter_expression);
67676bd8
DG
3465 free(bytecode);
3466 free(exclusion);
3467 ret = LTTNG_ERR_FILTER_INVAL;
3468 goto error;
3469 }
882ef835 3470 }
67676bd8 3471
dcabc190
FD
3472 ev = lttng_event_copy(&cmd_ctx->lsm->u.enable.event);
3473 if (!ev) {
3474 DBG("Failed to copy event: %s",
3475 cmd_ctx->lsm->u.enable.event.name);
5ae0c80e
JG
3476 free(filter_expression);
3477 free(bytecode);
3478 free(exclusion);
dcabc190
FD
3479 ret = LTTNG_ERR_NOMEM;
3480 goto error;
3481 }
3482
3483
3484 if (cmd_ctx->lsm->u.enable.userspace_probe_location_len > 0) {
3485 /* Expect a userspace probe description. */
3486 ret = receive_userspace_probe(cmd_ctx, sock, sock_error, ev);
3487 if (ret) {
5ae0c80e
JG
3488 free(filter_expression);
3489 free(bytecode);
3490 free(exclusion);
dcabc190
FD
3491 lttng_event_destroy(ev);
3492 goto error;
3493 }
3494 }
3495
67676bd8
DG
3496 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
3497 cmd_ctx->lsm->u.enable.channel_name,
dcabc190 3498 ev,
6b453b5e 3499 filter_expression, bytecode, exclusion,
67676bd8 3500 kernel_poll_pipe[1]);
dcabc190 3501 lttng_event_destroy(ev);
54d01ffb
DG
3502 break;
3503 }
052da939 3504 case LTTNG_LIST_TRACEPOINTS:
2ef84c95 3505 {
9f19cc17 3506 struct lttng_event *events;
54d01ffb 3507 ssize_t nb_events;
052da939 3508
0845bbfa 3509 session_lock_list();
54d01ffb 3510 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
0845bbfa 3511 session_unlock_list();
54d01ffb 3512 if (nb_events < 0) {
f73fabfd 3513 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3514 ret = -nb_events;
3515 goto error;
2ef84c95
DG
3516 }
3517
3518 /*
3519 * Setup lttng message with payload size set to the event list size in
3520 * bytes and then copy list into the llm payload.
3521 */
6e10c9b9
PP
3522 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
3523 sizeof(struct lttng_event) * nb_events);
3524 free(events);
3525
2ef84c95
DG
3526 if (ret < 0) {
3527 goto setup_error;
3528 }
3529
f73fabfd 3530 ret = LTTNG_OK;
2ef84c95
DG
3531 break;
3532 }
f37d259d
MD
3533 case LTTNG_LIST_TRACEPOINT_FIELDS:
3534 {
3535 struct lttng_event_field *fields;
3536 ssize_t nb_fields;
3537
0845bbfa 3538 session_lock_list();
a4b92340
DG
3539 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
3540 &fields);
0845bbfa 3541 session_unlock_list();
f37d259d 3542 if (nb_fields < 0) {
f73fabfd 3543 /* Return value is a negative lttng_error_code. */
f37d259d
MD
3544 ret = -nb_fields;
3545 goto error;
3546 }
3547
3548 /*
3549 * Setup lttng message with payload size set to the event list size in
3550 * bytes and then copy list into the llm payload.
3551 */
6e10c9b9 3552 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, fields,
a4b92340 3553 sizeof(struct lttng_event_field) * nb_fields);
6e10c9b9
PP
3554 free(fields);
3555
f37d259d 3556 if (ret < 0) {
f37d259d
MD
3557 goto setup_error;
3558 }
3559
f73fabfd 3560 ret = LTTNG_OK;
f37d259d
MD
3561 break;
3562 }
834978fd
DG
3563 case LTTNG_LIST_SYSCALLS:
3564 {
3565 struct lttng_event *events;
3566 ssize_t nb_events;
3567
3568 nb_events = cmd_list_syscalls(&events);
3569 if (nb_events < 0) {
3570 /* Return value is a negative lttng_error_code. */
3571 ret = -nb_events;
3572 goto error;
3573 }
3574
3575 /*
3576 * Setup lttng message with payload size set to the event list size in
3577 * bytes and then copy list into the llm payload.
3578 */
6e10c9b9
PP
3579 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
3580 sizeof(struct lttng_event) * nb_events);
3581 free(events);
3582
834978fd 3583 if (ret < 0) {
834978fd
DG
3584 goto setup_error;
3585 }
3586
834978fd
DG
3587 ret = LTTNG_OK;
3588 break;
3589 }
a5dfbb9d
MD
3590 case LTTNG_LIST_TRACKER_PIDS:
3591 {
3592 int32_t *pids = NULL;
3593 ssize_t nr_pids;
3594
3595 nr_pids = cmd_list_tracker_pids(cmd_ctx->session,
3596 cmd_ctx->lsm->domain.type, &pids);
3597 if (nr_pids < 0) {
3598 /* Return value is a negative lttng_error_code. */
3599 ret = -nr_pids;
3600 goto error;
3601 }
3602
3603 /*
3604 * Setup lttng message with payload size set to the event list size in
3605 * bytes and then copy list into the llm payload.
3606 */
6e10c9b9
PP
3607 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, pids,
3608 sizeof(int32_t) * nr_pids);
3609 free(pids);
3610
a5dfbb9d 3611 if (ret < 0) {
a5dfbb9d
MD
3612 goto setup_error;
3613 }
3614
a5dfbb9d
MD
3615 ret = LTTNG_OK;
3616 break;
3617 }
00e2e675
DG
3618 case LTTNG_SET_CONSUMER_URI:
3619 {
a4b92340
DG
3620 size_t nb_uri, len;
3621 struct lttng_uri *uris;
3622
3623 nb_uri = cmd_ctx->lsm->u.uri.size;
3624 len = nb_uri * sizeof(struct lttng_uri);
3625
3626 if (nb_uri == 0) {
f73fabfd 3627 ret = LTTNG_ERR_INVALID;
a4b92340
DG
3628 goto error;
3629 }
3630
3631 uris = zmalloc(len);
3632 if (uris == NULL) {
f73fabfd 3633 ret = LTTNG_ERR_FATAL;
a4b92340
DG
3634 goto error;
3635 }
3636
3637 /* Receive variable len data */
77c7c900 3638 DBG("Receiving %zu URI(s) from client ...", nb_uri);
a4b92340
DG
3639 ret = lttcomm_recv_unix_sock(sock, uris, len);
3640 if (ret <= 0) {
3641 DBG("No URIs received from client... continuing");
3642 *sock_error = 1;
f73fabfd 3643 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3644 free(uris);
a4b92340
DG
3645 goto error;
3646 }
3647
bda32d56
JG
3648 ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris);
3649 free(uris);
f73fabfd 3650 if (ret != LTTNG_OK) {
a4b92340
DG
3651 goto error;
3652 }
3653
b1d41407 3654
00e2e675
DG
3655 break;
3656 }
f3ed775e 3657 case LTTNG_START_TRACE:
8c0faa1d 3658 {
259c2674
JD
3659 /*
3660 * On the first start, if we have a kernel session and we have
3661 * enabled time or size-based rotations, we have to make sure
3662 * the kernel tracer supports it.
3663 */
3664 if (!cmd_ctx->session->has_been_started && \
3665 cmd_ctx->session->kernel_session && \
3666 (cmd_ctx->session->rotate_timer_period || \
3667 cmd_ctx->session->rotate_size) && \
3668 !check_rotate_compatible()) {
3669 DBG("Kernel tracer version is not compatible with the rotation feature");
3670 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
3671 goto error;
3672 }
54d01ffb 3673 ret = cmd_start_trace(cmd_ctx->session);
8c0faa1d
DG
3674 break;
3675 }
f3ed775e 3676 case LTTNG_STOP_TRACE:
8c0faa1d 3677 {
54d01ffb 3678 ret = cmd_stop_trace(cmd_ctx->session);
8c0faa1d
DG
3679 break;
3680 }
5e16da05
MD
3681 case LTTNG_CREATE_SESSION:
3682 {
a4b92340
DG
3683 size_t nb_uri, len;
3684 struct lttng_uri *uris = NULL;
3685
3686 nb_uri = cmd_ctx->lsm->u.uri.size;
3687 len = nb_uri * sizeof(struct lttng_uri);
3688
3689 if (nb_uri > 0) {
3690 uris = zmalloc(len);
3691 if (uris == NULL) {
f73fabfd 3692 ret = LTTNG_ERR_FATAL;
a4b92340
DG
3693 goto error;
3694 }
3695
3696 /* Receive variable len data */
77c7c900 3697 DBG("Waiting for %zu URIs from client ...", nb_uri);
a4b92340
DG
3698 ret = lttcomm_recv_unix_sock(sock, uris, len);
3699 if (ret <= 0) {
3700 DBG("No URIs received from client... continuing");
3701 *sock_error = 1;
f73fabfd 3702 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3703 free(uris);
a4b92340
DG
3704 goto error;
3705 }
3706
3707 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3708 DBG("Creating session with ONE network URI is a bad call");
f73fabfd 3709 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3710 free(uris);
a4b92340
DG
3711 goto error;
3712 }
3713 }
3714
3715 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
ecc48a90 3716 &cmd_ctx->creds, 0);
a4b92340 3717
b1d41407
DG
3718 free(uris);
3719
00e2e675
DG
3720 break;
3721 }
5e16da05
MD
3722 case LTTNG_DESTROY_SESSION:
3723 {
e32d7f27 3724 ret = cmd_destroy_session(cmd_ctx->session,
90936dcf 3725 notification_thread_handle);
5461b305 3726 break;
5e16da05 3727 }
9f19cc17 3728 case LTTNG_LIST_DOMAINS:
5e16da05 3729 {
54d01ffb 3730 ssize_t nb_dom;
fa64dfb4 3731 struct lttng_domain *domains = NULL;
5461b305 3732
54d01ffb
DG
3733 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
3734 if (nb_dom < 0) {
f73fabfd 3735 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3736 ret = -nb_dom;
3737 goto error;
ce3d728c 3738 }
520ff687 3739
6e10c9b9
PP
3740 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, domains,
3741 nb_dom * sizeof(struct lttng_domain));
3742 free(domains);
3743
5461b305
DG
3744 if (ret < 0) {
3745 goto setup_error;
520ff687 3746 }
57167058 3747
f73fabfd 3748 ret = LTTNG_OK;
5e16da05
MD
3749 break;
3750 }
9f19cc17 3751 case LTTNG_LIST_CHANNELS:
5e16da05 3752 {
53e367f9 3753 ssize_t payload_size;
ade7ce52 3754 struct lttng_channel *channels = NULL;
54d01ffb 3755
53e367f9 3756 payload_size = cmd_list_channels(cmd_ctx->lsm->domain.type,
b551a063 3757 cmd_ctx->session, &channels);
53e367f9 3758 if (payload_size < 0) {
f73fabfd 3759 /* Return value is a negative lttng_error_code. */
53e367f9 3760 ret = -payload_size;
54d01ffb 3761 goto error;
5461b305 3762 }
ca95a216 3763
6e10c9b9 3764 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, channels,
53e367f9 3765 payload_size);
6e10c9b9
PP
3766 free(channels);
3767
5461b305
DG
3768 if (ret < 0) {
3769 goto setup_error;
3770 }
9f19cc17 3771
f73fabfd 3772 ret = LTTNG_OK;
5461b305 3773 break;
5e16da05 3774 }
9f19cc17 3775 case LTTNG_LIST_EVENTS:
5e16da05 3776 {
b551a063 3777 ssize_t nb_event;
684d34d2 3778 struct lttng_event *events = NULL;
b4e3ceb9
PP
3779 struct lttcomm_event_command_header cmd_header;
3780 size_t total_size;
3781
b2d66287 3782 memset(&cmd_header, 0, sizeof(cmd_header));
b4e3ceb9
PP
3783 /* Extended infos are included at the end of events */
3784 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type,
3785 cmd_ctx->session, cmd_ctx->lsm->u.list.channel_name,
3786 &events, &total_size);
9f19cc17 3787
54d01ffb 3788 if (nb_event < 0) {
f73fabfd 3789 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3790 ret = -nb_event;
3791 goto error;
5461b305 3792 }
ca95a216 3793
b4e3ceb9
PP
3794 cmd_header.nb_events = nb_event;
3795 ret = setup_lttng_msg(cmd_ctx, events, total_size,
3796 &cmd_header, sizeof(cmd_header));
6e10c9b9
PP
3797 free(events);
3798
5461b305
DG
3799 if (ret < 0) {
3800 goto setup_error;
3801 }
9f19cc17 3802
f73fabfd 3803 ret = LTTNG_OK;
5461b305 3804 break;
5e16da05
MD
3805 }
3806 case LTTNG_LIST_SESSIONS:
3807 {
8e0af1b4 3808 unsigned int nr_sessions;
6e10c9b9
PP
3809 void *sessions_payload;
3810 size_t payload_len;
5461b305 3811
8e0af1b4 3812 session_lock_list();
730389d9
DG
3813 nr_sessions = lttng_sessions_count(
3814 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3815 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
6e10c9b9
PP
3816 payload_len = sizeof(struct lttng_session) * nr_sessions;
3817 sessions_payload = zmalloc(payload_len);
d32fb093 3818
6e10c9b9 3819 if (!sessions_payload) {
54d01ffb 3820 session_unlock_list();
6e10c9b9 3821 ret = -ENOMEM;
5461b305 3822 goto setup_error;
e065084a 3823 }
5e16da05 3824
6e10c9b9 3825 cmd_list_lttng_sessions(sessions_payload,
730389d9
DG
3826 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3827 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
54d01ffb 3828 session_unlock_list();
5e16da05 3829
6e10c9b9
PP
3830 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload,
3831 payload_len);
3832 free(sessions_payload);
3833
3834 if (ret < 0) {
3835 goto setup_error;
3836 }
3837
f73fabfd 3838 ret = LTTNG_OK;
5e16da05
MD
3839 break;
3840 }
d9800920
DG
3841 case LTTNG_REGISTER_CONSUMER:
3842 {
2f77fc4b
DG
3843 struct consumer_data *cdata;
3844
3845 switch (cmd_ctx->lsm->domain.type) {
3846 case LTTNG_DOMAIN_KERNEL:
3847 cdata = &kconsumer_data;
3848 break;
3849 default:
f73fabfd 3850 ret = LTTNG_ERR_UND;
2f77fc4b
DG
3851 goto error;
3852 }
3853
54d01ffb 3854 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2f77fc4b 3855 cmd_ctx->lsm->u.reg.path, cdata);
d9800920
DG
3856 break;
3857 }
6d805429 3858 case LTTNG_DATA_PENDING:
806e2684 3859 {
f6151c55 3860 int pending_ret;
6e10c9b9 3861 uint8_t pending_ret_byte;
f6151c55
JG
3862
3863 pending_ret = cmd_data_pending(cmd_ctx->session);
6e10c9b9 3864
f6151c55
JG
3865 /*
3866 * FIXME
3867 *
3868 * This function may returns 0 or 1 to indicate whether or not
3869 * there is data pending. In case of error, it should return an
3870 * LTTNG_ERR code. However, some code paths may still return
3871 * a nondescript error code, which we handle by returning an
3872 * "unknown" error.
3873 */
3874 if (pending_ret == 0 || pending_ret == 1) {
6e10c9b9
PP
3875 /*
3876 * ret will be set to LTTNG_OK at the end of
3877 * this function.
3878 */
f6151c55
JG
3879 } else if (pending_ret < 0) {
3880 ret = LTTNG_ERR_UNK;
3881 goto setup_error;
3882 } else {
3883 ret = pending_ret;
3884 goto setup_error;
3885 }
3886
6e10c9b9
PP
3887 pending_ret_byte = (uint8_t) pending_ret;
3888
3889 /* 1 byte to return whether or not data is pending */
3890 ret = setup_lttng_msg_no_cmd_header(cmd_ctx,
3891 &pending_ret_byte, 1);
3892
3893 if (ret < 0) {
3894 goto setup_error;
3895 }
3896
3897 ret = LTTNG_OK;
806e2684
DG
3898 break;
3899 }
da3c9ec1
DG
3900 case LTTNG_SNAPSHOT_ADD_OUTPUT:
3901 {
6dc3064a
DG
3902 struct lttcomm_lttng_output_id reply;
3903
3904 ret = cmd_snapshot_add_output(cmd_ctx->session,
3905 &cmd_ctx->lsm->u.snapshot_output.output, &reply.id);
3906 if (ret != LTTNG_OK) {
3907 goto error;
3908 }
3909
6e10c9b9
PP
3910 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &reply,
3911 sizeof(reply));
6dc3064a
DG
3912 if (ret < 0) {
3913 goto setup_error;
3914 }
3915
3916 /* Copy output list into message payload */
6dc3064a 3917 ret = LTTNG_OK;
da3c9ec1
DG
3918 break;
3919 }
3920 case LTTNG_SNAPSHOT_DEL_OUTPUT:
3921 {
6dc3064a
DG
3922 ret = cmd_snapshot_del_output(cmd_ctx->session,
3923 &cmd_ctx->lsm->u.snapshot_output.output);
da3c9ec1
DG
3924 break;
3925 }
3926 case LTTNG_SNAPSHOT_LIST_OUTPUT:
3927 {
6dc3064a
DG
3928 ssize_t nb_output;
3929 struct lttng_snapshot_output *outputs = NULL;
3930
3931 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
3932 if (nb_output < 0) {
3933 ret = -nb_output;
3934 goto error;
3935 }
3936
6e10c9b9
PP
3937 assert((nb_output > 0 && outputs) || nb_output == 0);
3938 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, outputs,
6dc3064a 3939 nb_output * sizeof(struct lttng_snapshot_output));
6e10c9b9
PP
3940 free(outputs);
3941
6dc3064a 3942 if (ret < 0) {
6dc3064a
DG
3943 goto setup_error;
3944 }
3945
6dc3064a 3946 ret = LTTNG_OK;
da3c9ec1
DG
3947 break;
3948 }
3949 case LTTNG_SNAPSHOT_RECORD:
3950 {
6dc3064a
DG
3951 ret = cmd_snapshot_record(cmd_ctx->session,
3952 &cmd_ctx->lsm->u.snapshot_record.output,
3953 cmd_ctx->lsm->u.snapshot_record.wait);
da3c9ec1
DG
3954 break;
3955 }
27babd3a
DG
3956 case LTTNG_CREATE_SESSION_SNAPSHOT:
3957 {
3958 size_t nb_uri, len;
3959 struct lttng_uri *uris = NULL;
3960
3961 nb_uri = cmd_ctx->lsm->u.uri.size;
3962 len = nb_uri * sizeof(struct lttng_uri);
3963
3964 if (nb_uri > 0) {
3965 uris = zmalloc(len);
3966 if (uris == NULL) {
3967 ret = LTTNG_ERR_FATAL;
3968 goto error;
3969 }
3970
3971 /* Receive variable len data */
3972 DBG("Waiting for %zu URIs from client ...", nb_uri);
3973 ret = lttcomm_recv_unix_sock(sock, uris, len);
3974 if (ret <= 0) {
3975 DBG("No URIs received from client... continuing");
3976 *sock_error = 1;
3977 ret = LTTNG_ERR_SESSION_FAIL;
3978 free(uris);
3979 goto error;
3980 }
3981
3982 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3983 DBG("Creating session with ONE network URI is a bad call");
3984 ret = LTTNG_ERR_SESSION_FAIL;
3985 free(uris);
3986 goto error;
3987 }
3988 }
3989
3990 ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris,
3991 nb_uri, &cmd_ctx->creds);
3992 free(uris);
3993 break;
3994 }
ecc48a90
JD
3995 case LTTNG_CREATE_SESSION_LIVE:
3996 {
3997 size_t nb_uri, len;
3998 struct lttng_uri *uris = NULL;
3999
4000 nb_uri = cmd_ctx->lsm->u.uri.size;
4001 len = nb_uri * sizeof(struct lttng_uri);
4002
4003 if (nb_uri > 0) {
4004 uris = zmalloc(len);
4005 if (uris == NULL) {
4006 ret = LTTNG_ERR_FATAL;
4007 goto error;
4008 }
4009
4010 /* Receive variable len data */
4011 DBG("Waiting for %zu URIs from client ...", nb_uri);
4012 ret = lttcomm_recv_unix_sock(sock, uris, len);
4013 if (ret <= 0) {
4014 DBG("No URIs received from client... continuing");
4015 *sock_error = 1;
4016 ret = LTTNG_ERR_SESSION_FAIL;
4017 free(uris);
4018 goto error;
4019 }
4020
4021 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
4022 DBG("Creating session with ONE network URI is a bad call");
4023 ret = LTTNG_ERR_SESSION_FAIL;
4024 free(uris);
4025 goto error;
4026 }
4027 }
4028
4029 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris,
4030 nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval);
4031 free(uris);
4032 break;
4033 }
fb198a11
JG
4034 case LTTNG_SAVE_SESSION:
4035 {
4036 ret = cmd_save_sessions(&cmd_ctx->lsm->u.save_session.attr,
4037 &cmd_ctx->creds);
4038 break;
4039 }
d7ba1388
MD
4040 case LTTNG_SET_SESSION_SHM_PATH:
4041 {
4042 ret = cmd_set_session_shm_path(cmd_ctx->session,
4043 cmd_ctx->lsm->u.set_shm_path.shm_path);
4044 break;
4045 }
eded6438 4046 case LTTNG_REGENERATE_METADATA:
93ec662e 4047 {
eded6438 4048 ret = cmd_regenerate_metadata(cmd_ctx->session);
93ec662e
JD
4049 break;
4050 }
c2561365
JD
4051 case LTTNG_REGENERATE_STATEDUMP:
4052 {
4053 ret = cmd_regenerate_statedump(cmd_ctx->session);
4054 break;
4055 }
b3530820
JG
4056 case LTTNG_REGISTER_TRIGGER:
4057 {
4058 ret = cmd_register_trigger(cmd_ctx, sock,
4059 notification_thread_handle);
4060 break;
4061 }
4062 case LTTNG_UNREGISTER_TRIGGER:
4063 {
4064 ret = cmd_unregister_trigger(cmd_ctx, sock,
4065 notification_thread_handle);
4066 break;
4067 }
5c408ad8
JD
4068 case LTTNG_ROTATE_SESSION:
4069 {
4070 struct lttng_rotate_session_return rotate_return;
4071
4072 DBG("Client rotate session \"%s\"", cmd_ctx->session->name);
4073
d68c9a04 4074 memset(&rotate_return, 0, sizeof(rotate_return));
5c408ad8
JD
4075 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
4076 DBG("Kernel tracer version is not compatible with the rotation feature");
4077 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
4078 goto error;
4079 }
4080
4081 ret = cmd_rotate_session(cmd_ctx->session, &rotate_return);
4082 if (ret < 0) {
4083 ret = -ret;
4084 goto error;
4085 }
4086
4087 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &rotate_return,
4088 sizeof(rotate_return));
4089 if (ret < 0) {
4090 ret = -ret;
4091 goto error;
4092 }
4093
4094 ret = LTTNG_OK;
4095 break;
4096 }
d68c9a04 4097 case LTTNG_ROTATION_GET_INFO:
5c408ad8 4098 {
d68c9a04 4099 struct lttng_rotation_get_info_return get_info_return;
5c408ad8 4100
d68c9a04
JD
4101 memset(&get_info_return, 0, sizeof(get_info_return));
4102 ret = cmd_rotate_get_info(cmd_ctx->session, &get_info_return,
4103 cmd_ctx->lsm->u.get_rotation_info.rotation_id);
5c408ad8
JD
4104 if (ret < 0) {
4105 ret = -ret;
4106 goto error;
4107 }
4108
d68c9a04
JD
4109 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &get_info_return,
4110 sizeof(get_info_return));
5c408ad8
JD
4111 if (ret < 0) {
4112 ret = -ret;
4113 goto error;
4114 }
4115
4116 ret = LTTNG_OK;
4117 break;
4118 }
259c2674
JD
4119 case LTTNG_ROTATION_SET_SCHEDULE:
4120 {
66ea93b1
JG
4121 bool set_schedule;
4122 enum lttng_rotation_schedule_type schedule_type;
4123 uint64_t value;
4124
259c2674
JD
4125 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
4126 DBG("Kernel tracer version does not support session rotations");
4127 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
4128 goto error;
4129 }
4130
66ea93b1
JG
4131 set_schedule = cmd_ctx->lsm->u.rotation_set_schedule.set == 1;
4132 schedule_type = (enum lttng_rotation_schedule_type) cmd_ctx->lsm->u.rotation_set_schedule.type;
4133 value = cmd_ctx->lsm->u.rotation_set_schedule.value;
4134
259c2674 4135 ret = cmd_rotation_set_schedule(cmd_ctx->session,
66ea93b1
JG
4136 set_schedule,
4137 schedule_type,
4138 value,
90936dcf 4139 notification_thread_handle);
a2026546 4140 if (ret != LTTNG_OK) {
259c2674
JD
4141 goto error;
4142 }
4143
259c2674
JD
4144 break;
4145 }
66ea93b1 4146 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
329f3443 4147 {
66ea93b1
JG
4148 struct lttng_session_list_schedules_return schedules = {
4149 .periodic.set = !!cmd_ctx->session->rotate_timer_period,
4150 .periodic.value = cmd_ctx->session->rotate_timer_period,
4151 .size.set = !!cmd_ctx->session->rotate_size,
4152 .size.value = cmd_ctx->session->rotate_size,
4153 };
4154
4155 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &schedules,
4156 sizeof(schedules));
329f3443
JD
4157 if (ret < 0) {
4158 ret = -ret;
4159 goto error;
4160 }
4161
4162 ret = LTTNG_OK;
4163 break;
4164 }
5e16da05 4165 default:
f73fabfd 4166 ret = LTTNG_ERR_UND;
5461b305 4167 break;
fac6795d
DG
4168 }
4169
5461b305 4170error:
5461b305
DG
4171 if (cmd_ctx->llm == NULL) {
4172 DBG("Missing llm structure. Allocating one.");
6e10c9b9 4173 if (setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0) < 0) {
5461b305
DG
4174 goto setup_error;
4175 }
4176 }
54d01ffb 4177 /* Set return code */
5461b305 4178 cmd_ctx->llm->ret_code = ret;
5461b305 4179setup_error:
b5541356 4180 if (cmd_ctx->session) {
54d01ffb 4181 session_unlock(cmd_ctx->session);
e32d7f27 4182 session_put(cmd_ctx->session);
b5541356 4183 }
256a5576
MD
4184 if (need_tracing_session) {
4185 session_unlock_list();
4186 }
54d01ffb 4187init_setup_error:
3745d315 4188 assert(!rcu_read_ongoing());
8028d920 4189 return ret;
fac6795d
DG
4190}
4191
1d4b027a 4192/*
d063d709
DG
4193 * This thread manage all clients request using the unix client socket for
4194 * communication.
1d4b027a
DG
4195 */
4196static void *thread_manage_clients(void *data)
4197{
139ac872 4198 int sock = -1, ret, i, pollfd, err = -1;
53a80697 4199 int sock_error;
5eb91c98 4200 uint32_t revents, nb_fd;
273ea72c 4201 struct command_ctx *cmd_ctx = NULL;
5eb91c98 4202 struct lttng_poll_event events;
1d4b027a
DG
4203
4204 DBG("[thread] Manage client started");
4205
f6a9efaa
DG
4206 rcu_register_thread();
4207
6c71277b 4208 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
927ca06a 4209
840cb59c 4210 health_code_update();
44a5e5eb 4211
1d4b027a
DG
4212 ret = lttcomm_listen_unix_sock(client_sock);
4213 if (ret < 0) {
35df2755 4214 goto error_listen;
1d4b027a
DG
4215 }
4216
5eb91c98
DG
4217 /*
4218 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4219 * more will be added to this poll set.
4220 */
d0b96690 4221 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 4222 if (ret < 0) {
35df2755 4223 goto error_create_poll;
5eb91c98 4224 }
273ea72c 4225
5eb91c98
DG
4226 /* Add the application registration socket */
4227 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
4228 if (ret < 0) {
4229 goto error;
4230 }
273ea72c 4231
ef367a93
JG
4232 ret = sem_post(&load_info->message_thread_ready);
4233 if (ret) {
4234 PERROR("sem_post message_thread_ready");
4235 goto error;
4236 }
0bb7724a 4237
f28f9e44
JG
4238 /*
4239 * Wait until all support threads are initialized before accepting
4240 * commands.
4241 */
4242 while (uatomic_read(&lttng_sessiond_ready) != 0) {
f28f9e44
JG
4243 /*
4244 * If a support thread failed to launch, it may signal that
4245 * we must exit and the sessiond would never be marked as
4246 * "ready".
4247 *
4248 * The timeout is set to 1ms, which serves as a way to
4249 * pace down this check.
4250 */
a7333da7
JG
4251 ret = sessiond_wait_for_quit_pipe(1000);
4252 if (ret > 0) {
f28f9e44
JG
4253 goto exit;
4254 }
4255 }
8b0d4636
JG
4256 /*
4257 * This barrier is paired with the one in sessiond_notify_ready() to
4258 * ensure that loads accessing data initialized by the other threads,
4259 * on which this thread was waiting, are not performed before this point.
4260 *
4261 * Note that this could be a 'read' memory barrier, but a full barrier
4262 * is used in case the code changes. The performance implications of
4263 * this choice are minimal since this is a slow path.
4264 */
4265 cmm_smp_mb();
f28f9e44 4266
72453c8c
MD
4267 /* This testpoint is after we signal readiness to the parent. */
4268 if (testpoint(sessiond_thread_manage_clients)) {
4269 goto error;
4270 }
4271
e547b070 4272 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
6993eeb3
CB
4273 goto error;
4274 }
8ac94142 4275
840cb59c 4276 health_code_update();
44a5e5eb 4277
99d688f2
JG
4278 /* Set state as running. */
4279 sessiond_set_client_thread_state(true);
4280
1d4b027a 4281 while (1) {
a503e1ef
JG
4282 const struct cmd_completion_handler *cmd_completion_handler;
4283
1d4b027a 4284 DBG("Accepting client command ...");
273ea72c
DG
4285
4286 /* Inifinite blocking call, waiting for transmission */
88f2b785 4287 restart:
a78af745 4288 health_poll_entry();
5eb91c98 4289 ret = lttng_poll_wait(&events, -1);
a78af745 4290 health_poll_exit();
273ea72c 4291 if (ret < 0) {
88f2b785
MD
4292 /*
4293 * Restart interrupted system call.
4294 */
4295 if (errno == EINTR) {
4296 goto restart;
4297 }
273ea72c
DG
4298 goto error;
4299 }
4300
0d9c5d77
DG
4301 nb_fd = ret;
4302
5eb91c98
DG
4303 for (i = 0; i < nb_fd; i++) {
4304 /* Fetch once the poll data */
4305 revents = LTTNG_POLL_GETEV(&events, i);
4306 pollfd = LTTNG_POLL_GETFD(&events, i);
4307
840cb59c 4308 health_code_update();
44a5e5eb 4309
fd20dac9
MD
4310 if (!revents) {
4311 /* No activity for this FD (poll implementation). */
4312 continue;
4313 }
4314
5eb91c98 4315 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 4316 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 4317 if (ret) {
139ac872
MD
4318 err = 0;
4319 goto exit;
5eb91c98
DG
4320 }
4321
4322 /* Event on the registration socket */
4323 if (pollfd == client_sock) {
03e43155
MD
4324 if (revents & LPOLLIN) {
4325 continue;
4326 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
5eb91c98
DG
4327 ERR("Client socket poll error");
4328 goto error;
03e43155
MD
4329 } else {
4330 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
4331 goto error;
5eb91c98
DG
4332 }
4333 }
4334 }
4335
4336 DBG("Wait for client response");
4337
840cb59c 4338 health_code_update();
44a5e5eb 4339
5eb91c98
DG
4340 sock = lttcomm_accept_unix_sock(client_sock);
4341 if (sock < 0) {
273ea72c 4342 goto error;
273ea72c
DG
4343 }
4344
b662582b
DG
4345 /*
4346 * Set the CLOEXEC flag. Return code is useless because either way, the
4347 * show must go on.
4348 */
4349 (void) utils_set_fd_cloexec(sock);
4350
be040666
DG
4351 /* Set socket option for credentials retrieval */
4352 ret = lttcomm_setsockopt_creds_unix_sock(sock);
4353 if (ret < 0) {
4354 goto error;
4355 }
4356
5eb91c98 4357 /* Allocate context command to process the client request */
ba7f0ae5 4358 cmd_ctx = zmalloc(sizeof(struct command_ctx));
5eb91c98 4359 if (cmd_ctx == NULL) {
76d7553f 4360 PERROR("zmalloc cmd_ctx");
1d4b027a 4361 goto error;
5eb91c98 4362 }
1d4b027a 4363
5eb91c98 4364 /* Allocate data buffer for reception */
ba7f0ae5 4365 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
5eb91c98 4366 if (cmd_ctx->lsm == NULL) {
76d7553f 4367 PERROR("zmalloc cmd_ctx->lsm");
5eb91c98
DG
4368 goto error;
4369 }
1d4b027a 4370
5eb91c98
DG
4371 cmd_ctx->llm = NULL;
4372 cmd_ctx->session = NULL;
1d4b027a 4373
840cb59c 4374 health_code_update();
44a5e5eb 4375
5eb91c98
DG
4376 /*
4377 * Data is received from the lttng client. The struct
4378 * lttcomm_session_msg (lsm) contains the command and data request of
4379 * the client.
4380 */
4381 DBG("Receiving data from client ...");
be040666
DG
4382 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
4383 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
5eb91c98
DG
4384 if (ret <= 0) {
4385 DBG("Nothing recv() from client... continuing");
76d7553f
MD
4386 ret = close(sock);
4387 if (ret) {
4388 PERROR("close");
4389 }
4390 sock = -1;
a2c0da86 4391 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
4392 continue;
4393 }
1d4b027a 4394
840cb59c 4395 health_code_update();
44a5e5eb 4396
5eb91c98
DG
4397 // TODO: Validate cmd_ctx including sanity check for
4398 // security purpose.
f7776ea7 4399
f6a9efaa 4400 rcu_thread_online();
5eb91c98
DG
4401 /*
4402 * This function dispatch the work to the kernel or userspace tracer
4403 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4404 * informations for the client. The command context struct contains
4405 * everything this function may needs.
4406 */
53a80697 4407 ret = process_client_msg(cmd_ctx, sock, &sock_error);
f6a9efaa 4408 rcu_thread_offline();
5eb91c98 4409 if (ret < 0) {
36134aa1
DG
4410 ret = close(sock);
4411 if (ret) {
4412 PERROR("close");
53a80697 4413 }
36134aa1 4414 sock = -1;
bbd973c2 4415 /*
5eb91c98 4416 * TODO: Inform client somehow of the fatal error. At
ba7f0ae5 4417 * this point, ret < 0 means that a zmalloc failed
53a80697
MD
4418 * (ENOMEM). Error detected but still accept
4419 * command, unless a socket error has been
4420 * detected.
bbd973c2 4421 */
bbd973c2 4422 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
4423 continue;
4424 }
d6e4fca4 4425
a503e1ef
JG
4426 cmd_completion_handler = cmd_pop_completion_handler();
4427 if (cmd_completion_handler) {
4428 enum lttng_error_code completion_code;
4429
4430 completion_code = cmd_completion_handler->run(
4431 cmd_completion_handler->data);
4432 if (completion_code != LTTNG_OK) {
4433 clean_command_ctx(&cmd_ctx);
4434 continue;
4435 }
4436 }
4437
840cb59c 4438 health_code_update();
44a5e5eb 4439
a1ac8b97 4440 DBG("Sending response (size: %d, retcode: %s (%d))",
54d01ffb 4441 cmd_ctx->lttng_msg_size,
a1ac8b97
JG
4442 lttng_strerror(-cmd_ctx->llm->ret_code),
4443 cmd_ctx->llm->ret_code);
54d01ffb 4444 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
5eb91c98
DG
4445 if (ret < 0) {
4446 ERR("Failed to send data back to client");
bbd973c2 4447 }
1d4b027a 4448
5eb91c98 4449 /* End of transmission */
76d7553f
MD
4450 ret = close(sock);
4451 if (ret) {
4452 PERROR("close");
4453 }
4454 sock = -1;
be040666
DG
4455
4456 clean_command_ctx(&cmd_ctx);
44a5e5eb 4457
840cb59c 4458 health_code_update();
273ea72c
DG
4459 }
4460
139ac872 4461exit:
5eb91c98 4462error:
35df2755
CB
4463 if (sock >= 0) {
4464 ret = close(sock);
4465 if (ret) {
4466 PERROR("close");
4467 }
139ac872 4468 }
44a5e5eb 4469
35df2755
CB
4470 lttng_poll_clean(&events);
4471 clean_command_ctx(&cmd_ctx);
4472
4473error_listen:
4474error_create_poll:
e6142f2e 4475 unlink(config.client_unix_sock_path.value);
76d7553f
MD
4476 if (client_sock >= 0) {
4477 ret = close(client_sock);
4478 if (ret) {
4479 PERROR("close");
4480 }
a4b35e07 4481 }
35df2755
CB
4482
4483 if (err) {
840cb59c 4484 health_error();
35df2755 4485 ERR("Health error occurred in %s", __func__);
a4b35e07 4486 }
273ea72c 4487
8782cc74 4488 health_unregister(health_sessiond);
35df2755
CB
4489
4490 DBG("Client thread dying");
f6a9efaa
DG
4491
4492 rcu_unregister_thread();
4a15001e
MD
4493
4494 /*
4495 * Since we are creating the consumer threads, we own them, so we need
4496 * to join them before our thread exits.
4497 */
4498 ret = join_consumer_thread(&kconsumer_data);
4499 if (ret) {
4500 errno = ret;
4501 PERROR("join_consumer");
4502 }
4503
4504 ret = join_consumer_thread(&ustconsumer32_data);
4505 if (ret) {
4506 errno = ret;
4507 PERROR("join_consumer ust32");
4508 }
4509
4510 ret = join_consumer_thread(&ustconsumer64_data);
4511 if (ret) {
4512 errno = ret;
4513 PERROR("join_consumer ust64");
4514 }
99d688f2
JG
4515
4516 /* Set state as non-running. */
4517 sessiond_set_client_thread_state(false);
1d4b027a
DG
4518 return NULL;
4519}
4520
c0232ea5
JG
4521static int string_match(const char *str1, const char *str2)
4522{
4523 return (str1 && str2) && !strcmp(str1, str2);
4524}
4525
fac6795d 4526/*
26296c48
JG
4527 * Take an option from the getopt output and set it in the right variable to be
4528 * used later.
4529 *
4530 * Return 0 on success else a negative value.
fac6795d 4531 */
26296c48 4532static int set_option(int opt, const char *arg, const char *optname)
fac6795d 4533{
26296c48 4534 int ret = 0;
fac6795d 4535
c0232ea5 4536 if (string_match(optname, "client-sock") || opt == 'c') {
66b2ce8e
JG
4537 if (!arg || *arg == '\0') {
4538 ret = -EINVAL;
4539 goto end;
4540 }
e8fa9fb0
MD
4541 if (lttng_is_setuid_setgid()) {
4542 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4543 "-c, --client-sock");
4544 } else {
e6142f2e
JG
4545 config_string_set(&config.client_unix_sock_path,
4546 strdup(arg));
4547 if (!config.client_unix_sock_path.value) {
4548 ret = -ENOMEM;
4549 PERROR("strdup");
4550 }
e8fa9fb0 4551 }
c0232ea5 4552 } else if (string_match(optname, "apps-sock") || opt == 'a') {
66b2ce8e
JG
4553 if (!arg || *arg == '\0') {
4554 ret = -EINVAL;
4555 goto end;
4556 }
e8fa9fb0
MD
4557 if (lttng_is_setuid_setgid()) {
4558 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4559 "-a, --apps-sock");
4560 } else {
e6142f2e
JG
4561 config_string_set(&config.apps_unix_sock_path,
4562 strdup(arg));
4563 if (!config.apps_unix_sock_path.value) {
4564 ret = -ENOMEM;
4565 PERROR("strdup");
4566 }
e8fa9fb0 4567 }
c0232ea5 4568 } else if (string_match(optname, "daemonize") || opt == 'd') {
e6142f2e 4569 config.daemonize = true;
c0232ea5 4570 } else if (string_match(optname, "background") || opt == 'b') {
e6142f2e 4571 config.background = true;
c0232ea5 4572 } else if (string_match(optname, "group") || opt == 'g') {
66b2ce8e
JG
4573 if (!arg || *arg == '\0') {
4574 ret = -EINVAL;
4575 goto end;
4576 }
e8fa9fb0
MD
4577 if (lttng_is_setuid_setgid()) {
4578 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4579 "-g, --group");
4580 } else {
e6142f2e
JG
4581 config_string_set(&config.tracing_group_name,
4582 strdup(arg));
4583 if (!config.tracing_group_name.value) {
e8fa9fb0 4584 ret = -ENOMEM;
e6142f2e 4585 PERROR("strdup");
e8fa9fb0 4586 }
db322c4d 4587 }
c0232ea5 4588 } else if (string_match(optname, "help") || opt == 'h') {
4fc83d94 4589 ret = utils_show_help(8, "lttng-sessiond", help_msg);
8190767e 4590 if (ret) {
4fc83d94 4591 ERR("Cannot show --help for `lttng-sessiond`");
8190767e
PP
4592 perror("exec");
4593 }
4594 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
c0232ea5 4595 } else if (string_match(optname, "version") || opt == 'V') {
26296c48
JG
4596 fprintf(stdout, "%s\n", VERSION);
4597 exit(EXIT_SUCCESS);
c0232ea5 4598 } else if (string_match(optname, "sig-parent") || opt == 'S') {
e6142f2e 4599 config.sig_parent = true;
c0232ea5 4600 } else if (string_match(optname, "kconsumerd-err-sock")) {
66b2ce8e
JG
4601 if (!arg || *arg == '\0') {
4602 ret = -EINVAL;
4603 goto end;
4604 }
e8fa9fb0
MD
4605 if (lttng_is_setuid_setgid()) {
4606 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4607 "--kconsumerd-err-sock");
4608 } else {
e6142f2e
JG
4609 config_string_set(&config.kconsumerd_err_unix_sock_path,
4610 strdup(arg));
4611 if (!config.kconsumerd_err_unix_sock_path.value) {
4612 ret = -ENOMEM;
4613 PERROR("strdup");
4614 }
e8fa9fb0 4615 }
c0232ea5 4616 } else if (string_match(optname, "kconsumerd-cmd-sock")) {
66b2ce8e
JG
4617 if (!arg || *arg == '\0') {
4618 ret = -EINVAL;
4619 goto end;
4620 }
e8fa9fb0
MD
4621 if (lttng_is_setuid_setgid()) {
4622 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4623 "--kconsumerd-cmd-sock");
4624 } else {
e6142f2e
JG
4625 config_string_set(&config.kconsumerd_cmd_unix_sock_path,
4626 strdup(arg));
4627 if (!config.kconsumerd_cmd_unix_sock_path.value) {
4628 ret = -ENOMEM;
4629 PERROR("strdup");
4630 }
e8fa9fb0 4631 }
c0232ea5 4632 } else if (string_match(optname, "ustconsumerd64-err-sock")) {
66b2ce8e
JG
4633 if (!arg || *arg == '\0') {
4634 ret = -EINVAL;
4635 goto end;
4636 }
e8fa9fb0
MD
4637 if (lttng_is_setuid_setgid()) {
4638 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4639 "--ustconsumerd64-err-sock");
4640 } else {
e6142f2e
JG
4641 config_string_set(&config.consumerd64_err_unix_sock_path,
4642 strdup(arg));
4643 if (!config.consumerd64_err_unix_sock_path.value) {
4644 ret = -ENOMEM;
4645 PERROR("strdup");
4646 }
e8fa9fb0 4647 }
c0232ea5 4648 } else if (string_match(optname, "ustconsumerd64-cmd-sock")) {
66b2ce8e
JG
4649 if (!arg || *arg == '\0') {
4650 ret = -EINVAL;
4651 goto end;
4652 }
e8fa9fb0
MD
4653 if (lttng_is_setuid_setgid()) {
4654 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4655 "--ustconsumerd64-cmd-sock");
4656 } else {
e6142f2e
JG
4657 config_string_set(&config.consumerd64_cmd_unix_sock_path,
4658 strdup(arg));
4659 if (!config.consumerd64_cmd_unix_sock_path.value) {
4660 ret = -ENOMEM;
4661 PERROR("strdup");
4662 }
e8fa9fb0 4663 }
c0232ea5 4664 } else if (string_match(optname, "ustconsumerd32-err-sock")) {
66b2ce8e
JG
4665 if (!arg || *arg == '\0') {
4666 ret = -EINVAL;
4667 goto end;
4668 }
e8fa9fb0
MD
4669 if (lttng_is_setuid_setgid()) {
4670 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4671 "--ustconsumerd32-err-sock");
4672 } else {
e6142f2e
JG
4673 config_string_set(&config.consumerd32_err_unix_sock_path,
4674 strdup(arg));
4675 if (!config.consumerd32_err_unix_sock_path.value) {
4676 ret = -ENOMEM;
4677 PERROR("strdup");
4678 }
e8fa9fb0 4679 }
c0232ea5 4680 } else if (string_match(optname, "ustconsumerd32-cmd-sock")) {
66b2ce8e
JG
4681 if (!arg || *arg == '\0') {
4682 ret = -EINVAL;
4683 goto end;
4684 }
e8fa9fb0
MD
4685 if (lttng_is_setuid_setgid()) {
4686 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4687 "--ustconsumerd32-cmd-sock");
4688 } else {
e6142f2e
JG
4689 config_string_set(&config.consumerd32_cmd_unix_sock_path,
4690 strdup(arg));
4691 if (!config.consumerd32_cmd_unix_sock_path.value) {
4692 ret = -ENOMEM;
4693 PERROR("strdup");
4694 }
e8fa9fb0 4695 }
c0232ea5 4696 } else if (string_match(optname, "no-kernel")) {
e6142f2e 4697 config.no_kernel = true;
c0232ea5 4698 } else if (string_match(optname, "quiet") || opt == 'q') {
6d9a9c65 4699 config.quiet = true;
c0232ea5 4700 } else if (string_match(optname, "verbose") || opt == 'v') {
26296c48
JG
4701 /* Verbose level can increase using multiple -v */
4702 if (arg) {
13755a18 4703 /* Value obtained from config file */
e6142f2e 4704 config.verbose = config_parse_value(arg);
26296c48 4705 } else {
13755a18 4706 /* -v used on command line */
e6142f2e 4707 config.verbose++;
26296c48 4708 }
13755a18 4709 /* Clamp value to [0, 3] */
e6142f2e
JG
4710 config.verbose = config.verbose < 0 ? 0 :
4711 (config.verbose <= 3 ? config.verbose : 3);
c0232ea5 4712 } else if (string_match(optname, "verbose-consumer")) {
26296c48 4713 if (arg) {
e6142f2e 4714 config.verbose_consumer = config_parse_value(arg);
26296c48 4715 } else {
e6142f2e 4716 config.verbose_consumer++;
26296c48 4717 }
c0232ea5 4718 } else if (string_match(optname, "consumerd32-path")) {
66b2ce8e
JG
4719 if (!arg || *arg == '\0') {
4720 ret = -EINVAL;
4721 goto end;
4722 }
e8fa9fb0
MD
4723 if (lttng_is_setuid_setgid()) {
4724 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4725 "--consumerd32-path");
4726 } else {
e6142f2e
JG
4727 config_string_set(&config.consumerd32_bin_path,
4728 strdup(arg));
4729 if (!config.consumerd32_bin_path.value) {
e8fa9fb0
MD
4730 PERROR("strdup");
4731 ret = -ENOMEM;
4732 }
db322c4d 4733 }
c0232ea5 4734 } else if (string_match(optname, "consumerd32-libdir")) {
66b2ce8e
JG
4735 if (!arg || *arg == '\0') {
4736 ret = -EINVAL;
4737 goto end;
4738 }
e8fa9fb0
MD
4739 if (lttng_is_setuid_setgid()) {
4740 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4741 "--consumerd32-libdir");
4742 } else {
e6142f2e
JG
4743 config_string_set(&config.consumerd32_lib_dir,
4744 strdup(arg));
4745 if (!config.consumerd32_lib_dir.value) {
e8fa9fb0
MD
4746 PERROR("strdup");
4747 ret = -ENOMEM;
4748 }
db322c4d 4749 }
c0232ea5 4750 } else if (string_match(optname, "consumerd64-path")) {
66b2ce8e
JG
4751 if (!arg || *arg == '\0') {
4752 ret = -EINVAL;
4753 goto end;
4754 }
e8fa9fb0
MD
4755 if (lttng_is_setuid_setgid()) {
4756 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4757 "--consumerd64-path");
4758 } else {
e6142f2e
JG
4759 config_string_set(&config.consumerd64_bin_path,
4760 strdup(arg));
4761 if (!config.consumerd64_bin_path.value) {
e8fa9fb0
MD
4762 PERROR("strdup");
4763 ret = -ENOMEM;
4764 }
db322c4d 4765 }
c0232ea5 4766 } else if (string_match(optname, "consumerd64-libdir")) {
66b2ce8e
JG
4767 if (!arg || *arg == '\0') {
4768 ret = -EINVAL;
4769 goto end;
4770 }
e8fa9fb0
MD
4771 if (lttng_is_setuid_setgid()) {
4772 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4773 "--consumerd64-libdir");
4774 } else {
e6142f2e
JG
4775 config_string_set(&config.consumerd64_lib_dir,
4776 strdup(arg));
4777 if (!config.consumerd64_lib_dir.value) {
e8fa9fb0
MD
4778 PERROR("strdup");
4779 ret = -ENOMEM;
4780 }
db322c4d 4781 }
c0232ea5 4782 } else if (string_match(optname, "pidfile") || opt == 'p') {
66b2ce8e
JG
4783 if (!arg || *arg == '\0') {
4784 ret = -EINVAL;
4785 goto end;
4786 }
e8fa9fb0
MD
4787 if (lttng_is_setuid_setgid()) {
4788 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4789 "-p, --pidfile");
4790 } else {
e6142f2e
JG
4791 config_string_set(&config.pid_file_path, strdup(arg));
4792 if (!config.pid_file_path.value) {
e8fa9fb0
MD
4793 PERROR("strdup");
4794 ret = -ENOMEM;
4795 }
db322c4d 4796 }
c0232ea5 4797 } else if (string_match(optname, "agent-tcp-port")) {
66b2ce8e
JG
4798 if (!arg || *arg == '\0') {
4799 ret = -EINVAL;
4800 goto end;
4801 }
e8fa9fb0
MD
4802 if (lttng_is_setuid_setgid()) {
4803 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4804 "--agent-tcp-port");
4805 } else {
4806 unsigned long v;
4d076222 4807
e8fa9fb0
MD
4808 errno = 0;
4809 v = strtoul(arg, NULL, 0);
4810 if (errno != 0 || !isdigit(arg[0])) {
4811 ERR("Wrong value in --agent-tcp-port parameter: %s", arg);
4812 return -1;
4813 }
4814 if (v == 0 || v >= 65535) {
4815 ERR("Port overflow in --agent-tcp-port parameter: %s", arg);
4816 return -1;
4817 }
2288467f
JG
4818 config.agent_tcp_port.begin = config.agent_tcp_port.end = (int) v;
4819 DBG3("Agent TCP port set to non default: %i", (int) v);
26296c48 4820 }
c0232ea5 4821 } else if (string_match(optname, "load") || opt == 'l') {
66b2ce8e
JG
4822 if (!arg || *arg == '\0') {
4823 ret = -EINVAL;
4824 goto end;
4825 }
e8fa9fb0
MD
4826 if (lttng_is_setuid_setgid()) {
4827 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4828 "-l, --load");
4829 } else {
e6142f2e
JG
4830 config_string_set(&config.load_session_path, strdup(arg));
4831 if (!config.load_session_path.value) {
e8fa9fb0
MD
4832 PERROR("strdup");
4833 ret = -ENOMEM;
4834 }
ef367a93 4835 }
c0232ea5 4836 } else if (string_match(optname, "kmod-probes")) {
66b2ce8e
JG
4837 if (!arg || *arg == '\0') {
4838 ret = -EINVAL;
4839 goto end;
4840 }
e8fa9fb0
MD
4841 if (lttng_is_setuid_setgid()) {
4842 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4843 "--kmod-probes");
4844 } else {
e6142f2e
JG
4845 config_string_set(&config.kmod_probes_list, strdup(arg));
4846 if (!config.kmod_probes_list.value) {
e8fa9fb0
MD
4847 PERROR("strdup");
4848 ret = -ENOMEM;
4849 }
c9d42407 4850 }
c0232ea5 4851 } else if (string_match(optname, "extra-kmod-probes")) {
66b2ce8e
JG
4852 if (!arg || *arg == '\0') {
4853 ret = -EINVAL;
4854 goto end;
4855 }
e8fa9fb0
MD
4856 if (lttng_is_setuid_setgid()) {
4857 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4858 "--extra-kmod-probes");
4859 } else {
e6142f2e
JG
4860 config_string_set(&config.kmod_extra_probes_list,
4861 strdup(arg));
4862 if (!config.kmod_extra_probes_list.value) {
e8fa9fb0
MD
4863 PERROR("strdup");
4864 ret = -ENOMEM;
4865 }
db322c4d 4866 }
c0232ea5
JG
4867 } else if (string_match(optname, "config") || opt == 'f') {
4868 /* This is handled in set_options() thus silent skip. */
4869 goto end;
4870 } else {
26296c48
JG
4871 /* Unknown option or other error.
4872 * Error is printed by getopt, just return */
4873 ret = -1;
4874 }
4875
f3dd7ce7 4876end:
c5d350b2
JG
4877 if (ret == -EINVAL) {
4878 const char *opt_name = "unknown";
4879 int i;
4880
4881 for (i = 0; i < sizeof(long_options) / sizeof(struct option);
4882 i++) {
4883 if (opt == long_options[i].val) {
4884 opt_name = long_options[i].name;
4885 break;
4886 }
4887 }
4888
4889 WARN("Invalid argument provided for option \"%s\", using default value.",
4890 opt_name);
4891 }
f3dd7ce7 4892
26296c48
JG
4893 return ret;
4894}
4895
4896/*
4897 * config_entry_handler_cb used to handle options read from a config file.
f40ef1d5 4898 * See config_entry_handler_cb comment in common/config/session-config.h for the
26296c48
JG
4899 * return value conventions.
4900 */
4901static int config_entry_handler(const struct config_entry *entry, void *unused)
4902{
4903 int ret = 0, i;
4904
4905 if (!entry || !entry->name || !entry->value) {
4906 ret = -EINVAL;
4907 goto end;
4908 }
4909
4910 /* Check if the option is to be ignored */
4911 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
4912 if (!strcmp(entry->name, config_ignore_options[i])) {
4913 goto end;
4914 }
4915 }
4916
4917 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1;
4918 i++) {
4919
4920 /* Ignore if not fully matched. */
4921 if (strcmp(entry->name, long_options[i].name)) {
4922 continue;
4923 }
4924
4925 /*
4926 * If the option takes no argument on the command line, we have to
4927 * check if the value is "true". We support non-zero numeric values,
4928 * true, on and yes.
4929 */
4930 if (!long_options[i].has_arg) {
4931 ret = config_parse_value(entry->value);
4932 if (ret <= 0) {
4933 if (ret) {
4934 WARN("Invalid configuration value \"%s\" for option %s",
4935 entry->value, entry->name);
4936 }
4937 /* False, skip boolean config option. */
4938 goto end;
4d076222 4939 }
26296c48
JG
4940 }
4941
4942 ret = set_option(long_options[i].val, entry->value, entry->name);
4943 goto end;
4944 }
4945
4946 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
4947
4948end:
4949 return ret;
4950}
4951
4952/*
4953 * daemon configuration loading and argument parsing
4954 */
4955static int set_options(int argc, char **argv)
4956{
4957 int ret = 0, c = 0, option_index = 0;
4958 int orig_optopt = optopt, orig_optind = optind;
4959 char *optstring;
4960 const char *config_path = NULL;
4961
4962 optstring = utils_generate_optstring(long_options,
4963 sizeof(long_options) / sizeof(struct option));
4964 if (!optstring) {
4965 ret = -ENOMEM;
4966 goto end;
4967 }
4968
4969 /* Check for the --config option */
4970 while ((c = getopt_long(argc, argv, optstring, long_options,
4971 &option_index)) != -1) {
4972 if (c == '?') {
4973 ret = -EINVAL;
4974 goto end;
4975 } else if (c != 'f') {
4976 /* if not equal to --config option. */
4977 continue;
4978 }
4979
e8fa9fb0
MD
4980 if (lttng_is_setuid_setgid()) {
4981 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4982 "-f, --config");
4983 } else {
4984 config_path = utils_expand_path(optarg);
4985 if (!config_path) {
4986 ERR("Failed to resolve path: %s", optarg);
4987 }
26296c48
JG
4988 }
4989 }
4990
4991 ret = config_get_section_entries(config_path, config_section_name,
4992 config_entry_handler, NULL);
4993 if (ret) {
4994 if (ret > 0) {
4995 ERR("Invalid configuration option at line %i", ret);
4996 ret = -1;
4997 }
4998 goto end;
4999 }
5000
5001 /* Reset getopt's global state */
5002 optopt = orig_optopt;
5003 optind = orig_optind;
5004 while (1) {
c0232ea5
JG
5005 option_index = -1;
5006 /*
5007 * getopt_long() will not set option_index if it encounters a
5008 * short option.
5009 */
5010 c = getopt_long(argc, argv, optstring, long_options,
5011 &option_index);
26296c48 5012 if (c == -1) {
4d076222
DG
5013 break;
5014 }
26296c48 5015
c0232ea5
JG
5016 /*
5017 * Pass NULL as the long option name if popt left the index
5018 * unset.
5019 */
5020 ret = set_option(c, optarg,
5021 option_index < 0 ? NULL :
5022 long_options[option_index].name);
26296c48
JG
5023 if (ret < 0) {
5024 break;
fac6795d
DG
5025 }
5026 }
5027
26296c48
JG
5028end:
5029 free(optstring);
5030 return ret;
fac6795d
DG
5031}
5032
5033/*
d063d709 5034 * Creates the two needed socket by the daemon.
d6f42150
DG
5035 * apps_sock - The communication socket for all UST apps.
5036 * client_sock - The communication of the cli tool (lttng).
fac6795d 5037 */
cf3af59e 5038static int init_daemon_socket(void)
fac6795d
DG
5039{
5040 int ret = 0;
5041 mode_t old_umask;
5042
5043 old_umask = umask(0);
5044
5045 /* Create client tool unix socket */
e6142f2e 5046 client_sock = lttcomm_create_unix_sock(config.client_unix_sock_path.value);
d6f42150 5047 if (client_sock < 0) {
e6142f2e 5048 ERR("Create unix sock failed: %s", config.client_unix_sock_path.value);
fac6795d
DG
5049 ret = -1;
5050 goto end;
5051 }
5052
b662582b
DG
5053 /* Set the cloexec flag */
5054 ret = utils_set_fd_cloexec(client_sock);
5055 if (ret < 0) {
5056 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5057 "Continuing but note that the consumer daemon will have a "
5058 "reference to this socket on exec()", client_sock);
5059 }
5060
fac6795d 5061 /* File permission MUST be 660 */
e6142f2e 5062 ret = chmod(config.client_unix_sock_path.value, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
fac6795d 5063 if (ret < 0) {
e6142f2e 5064 ERR("Set file permissions failed: %s", config.client_unix_sock_path.value);
76d7553f 5065 PERROR("chmod");
fac6795d
DG
5066 goto end;
5067 }
5068
5069 /* Create the application unix socket */
e6142f2e 5070 apps_sock = lttcomm_create_unix_sock(config.apps_unix_sock_path.value);
d6f42150 5071 if (apps_sock < 0) {
e6142f2e 5072 ERR("Create unix sock failed: %s", config.apps_unix_sock_path.value);
fac6795d
DG
5073 ret = -1;
5074 goto end;
5075 }
5076
b662582b
DG
5077 /* Set the cloexec flag */
5078 ret = utils_set_fd_cloexec(apps_sock);
5079 if (ret < 0) {
5080 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5081 "Continuing but note that the consumer daemon will have a "
5082 "reference to this socket on exec()", apps_sock);
5083 }
5084
d6f42150 5085 /* File permission MUST be 666 */
e6142f2e 5086 ret = chmod(config.apps_unix_sock_path.value,
54d01ffb 5087 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fac6795d 5088 if (ret < 0) {
e6142f2e 5089 ERR("Set file permissions failed: %s", config.apps_unix_sock_path.value);
76d7553f 5090 PERROR("chmod");
fac6795d
DG
5091 goto end;
5092 }
5093
b662582b
DG
5094 DBG3("Session daemon client socket %d and application socket %d created",
5095 client_sock, apps_sock);
5096
fac6795d
DG
5097end:
5098 umask(old_umask);
5099 return ret;
5100}
5101
f472090a
JG
5102/*
5103 * Create lockfile using the rundir and return its fd.
5104 */
5105static int create_lockfile(void)
5106{
5107 return utils_create_lock_file(config.lock_file_path.value);
5108}
5109
fac6795d 5110/*
54d01ffb
DG
5111 * Check if the global socket is available, and if a daemon is answering at the
5112 * other side. If yes, error is returned.
f472090a
JG
5113 *
5114 * Also attempts to create and hold the lock file.
fac6795d 5115 */
cf3af59e 5116static int check_existing_daemon(void)
fac6795d 5117{
f472090a
JG
5118 int ret = 0;
5119
7d8234d9 5120 /* Is there anybody out there ? */
099e26bd 5121 if (lttng_session_daemon_alive()) {
f472090a
JG
5122 ret = -EEXIST;
5123 goto end;
099e26bd 5124 }
b09c7c76 5125
f472090a
JG
5126 lockfile_fd = create_lockfile();
5127 if (lockfile_fd < 0) {
5128 ret = -EEXIST;
5129 goto end;
5130 }
5131end:
5132 return ret;
5133}
5134
5135static void sessiond_cleanup_lock_file(void)
5136{
5137 int ret;
5138
5139 /*
5140 * Cleanup lock file by deleting it and finaly closing it which will
5141 * release the file system lock.
5142 */
5143 if (lockfile_fd >= 0) {
5144 ret = remove(config.lock_file_path.value);
5145 if (ret < 0) {
5146 PERROR("remove lock file");
5147 }
5148 ret = close(lockfile_fd);
5149 if (ret < 0) {
5150 PERROR("close lock file");
5151 }
5152 }
fac6795d
DG
5153}
5154
fac6795d 5155/*
d063d709 5156 * Set the tracing group gid onto the client socket.
5e16da05 5157 *
d063d709 5158 * Race window between mkdir and chown is OK because we are going from more
d1613cf5 5159 * permissive (root.root) to less permissive (root.tracing).
fac6795d 5160 */
be040666 5161static int set_permissions(char *rundir)
fac6795d
DG
5162{
5163 int ret;
996b65c8 5164 gid_t gid;
fac6795d 5165
e6142f2e 5166 gid = utils_get_group_id(config.tracing_group_name.value);
fac6795d 5167
d6f42150 5168 /* Set lttng run dir */
be040666 5169 ret = chown(rundir, 0, gid);
d6f42150 5170 if (ret < 0) {
be040666 5171 ERR("Unable to set group on %s", rundir);
76d7553f 5172 PERROR("chown");
d6f42150
DG
5173 }
5174
6c71277b
MD
5175 /*
5176 * Ensure all applications and tracing group can search the run
5177 * dir. Allow everyone to read the directory, since it does not
5178 * buy us anything to hide its content.
5179 */
5180 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
d1613cf5
JN
5181 if (ret < 0) {
5182 ERR("Unable to set permissions on %s", rundir);
76d7553f 5183 PERROR("chmod");
d1613cf5
JN
5184 }
5185
d6f42150 5186 /* lttng client socket path */
e6142f2e 5187 ret = chown(config.client_unix_sock_path.value, 0, gid);
fac6795d 5188 if (ret < 0) {
e6142f2e 5189 ERR("Unable to set group on %s", config.client_unix_sock_path.value);
76d7553f 5190 PERROR("chown");
d6f42150
DG
5191 }
5192
3bd1e081 5193 /* kconsumer error socket path */
6c71277b 5194 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
d6f42150 5195 if (ret < 0) {
3bd1e081 5196 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
76d7553f 5197 PERROR("chown");
3bd1e081
MD
5198 }
5199
7753dea8 5200 /* 64-bit ustconsumer error socket path */
6c71277b 5201 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
7753dea8
MD
5202 if (ret < 0) {
5203 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
76d7553f 5204 PERROR("chown");
7753dea8
MD
5205 }
5206
5207 /* 32-bit ustconsumer compat32 error socket path */
6c71277b 5208 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
3bd1e081 5209 if (ret < 0) {
7753dea8 5210 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
76d7553f 5211 PERROR("chown");
fac6795d
DG
5212 }
5213
d6f42150 5214 DBG("All permissions are set");
e07ae692 5215
fac6795d
DG
5216 return ret;
5217}
5218
d6f42150 5219/*
d063d709 5220 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150 5221 */
e6142f2e 5222static int create_lttng_rundir(void)
d6f42150
DG
5223{
5224 int ret;
5225
e6142f2e 5226 DBG3("Creating LTTng run directory: %s", config.rundir.value);
67e40797 5227
e6142f2e 5228 ret = mkdir(config.rundir.value, S_IRWXU);
d6f42150 5229 if (ret < 0) {
b1f11e69 5230 if (errno != EEXIST) {
e6142f2e 5231 ERR("Unable to create %s", config.rundir.value);
b1f11e69
DG
5232 goto error;
5233 } else {
5234 ret = 0;
5235 }
d6f42150
DG
5236 }
5237
5238error:
5239 return ret;
5240}
5241
5242/*
e6142f2e 5243 * Setup sockets and directory needed by the consumerds' communication with the
d063d709 5244 * session daemon.
d6f42150 5245 */
e6142f2e 5246static int set_consumer_sockets(struct consumer_data *consumer_data)
d6f42150
DG
5247{
5248 int ret;
e6142f2e 5249 char *path = NULL;
d6f42150 5250
6c71277b 5251 switch (consumer_data->type) {
7753dea8 5252 case LTTNG_CONSUMER_KERNEL:
e6142f2e 5253 path = config.kconsumerd_path.value;
7753dea8
MD
5254 break;
5255 case LTTNG_CONSUMER64_UST:
e6142f2e 5256 path = config.consumerd64_path.value;
7753dea8
MD
5257 break;
5258 case LTTNG_CONSUMER32_UST:
e6142f2e 5259 path = config.consumerd32_path.value;
7753dea8
MD
5260 break;
5261 default:
5262 ERR("Consumer type unknown");
5263 ret = -EINVAL;
5264 goto error;
d6f42150 5265 }
e6142f2e 5266 assert(path);
d6f42150 5267
67e40797
DG
5268 DBG2("Creating consumer directory: %s", path);
5269
6c71277b 5270 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
e7e1ef17
JR
5271 if (ret < 0 && errno != EEXIST) {
5272 PERROR("mkdir");
5273 ERR("Failed to create %s", path);
5274 goto error;
d6f42150 5275 }
6c71277b 5276 if (is_root) {
e6142f2e 5277 ret = chown(path, 0, utils_get_group_id(config.tracing_group_name.value));
6c71277b
MD
5278 if (ret < 0) {
5279 ERR("Unable to set group on %s", path);
5280 PERROR("chown");
5281 goto error;
5282 }
5283 }
d6f42150 5284
e6142f2e 5285 /* Create the consumerd error unix socket */
3bd1e081
MD
5286 consumer_data->err_sock =
5287 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
5288 if (consumer_data->err_sock < 0) {
5289 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
d6f42150
DG
5290 ret = -1;
5291 goto error;
5292 }
5293
a24f05ab
MD
5294 /*
5295 * Set the CLOEXEC flag. Return code is useless because either way, the
5296 * show must go on.
5297 */
5298 ret = utils_set_fd_cloexec(consumer_data->err_sock);
5299 if (ret < 0) {
5300 PERROR("utils_set_fd_cloexec");
5301 /* continue anyway */
5302 }
5303
d6f42150 5304 /* File permission MUST be 660 */
3bd1e081 5305 ret = chmod(consumer_data->err_unix_sock_path,
54d01ffb 5306 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
d6f42150 5307 if (ret < 0) {
3bd1e081 5308 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
67e40797 5309 PERROR("chmod");
d6f42150
DG
5310 goto error;
5311 }
5312
5313error:
5314 return ret;
5315}
5316
fac6795d 5317/*
d063d709 5318 * Signal handler for the daemon
cf3af59e 5319 *
54d01ffb
DG
5320 * Simply stop all worker threads, leaving main() return gracefully after
5321 * joining all threads and calling cleanup().
fac6795d
DG
5322 */
5323static void sighandler(int sig)
5324{
5325 switch (sig) {
cf3af59e 5326 case SIGINT:
af87c45a 5327 DBG("SIGINT caught");
cf3af59e
MD
5328 stop_threads();
5329 break;
5330 case SIGTERM:
af87c45a 5331 DBG("SIGTERM caught");
cf3af59e
MD
5332 stop_threads();
5333 break;
0bb7724a
DG
5334 case SIGUSR1:
5335 CMM_STORE_SHARED(recv_child_signal, 1);
5336 break;
cf3af59e
MD
5337 default:
5338 break;
fac6795d 5339 }
fac6795d
DG
5340}
5341
5342/*
d063d709 5343 * Setup signal handler for :
1d4b027a 5344 * SIGINT, SIGTERM, SIGPIPE
fac6795d 5345 */
1d4b027a 5346static int set_signal_handler(void)
fac6795d 5347{
1d4b027a
DG
5348 int ret = 0;
5349 struct sigaction sa;
5350 sigset_t sigset;
fac6795d 5351
1d4b027a 5352 if ((ret = sigemptyset(&sigset)) < 0) {
76d7553f 5353 PERROR("sigemptyset");
1d4b027a
DG
5354 return ret;
5355 }
d6f42150 5356
1d4b027a
DG
5357 sa.sa_mask = sigset;
5358 sa.sa_flags = 0;
0072e5e2
MD
5359
5360 sa.sa_handler = sighandler;
1d4b027a 5361 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
76d7553f 5362 PERROR("sigaction");
1d4b027a 5363 return ret;
d6f42150
DG
5364 }
5365
1d4b027a 5366 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
76d7553f 5367 PERROR("sigaction");
1d4b027a 5368 return ret;
d6f42150 5369 }
aaf26714 5370
0072e5e2 5371 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
76d7553f 5372 PERROR("sigaction");
1d4b027a 5373 return ret;
8c0faa1d
DG
5374 }
5375
0072e5e2
MD
5376 sa.sa_handler = SIG_IGN;
5377 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
0bb7724a
DG
5378 PERROR("sigaction");
5379 return ret;
5380 }
5381
5382 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1d4b027a
DG
5383
5384 return ret;
fac6795d
DG
5385}
5386
f3ed775e 5387/*
d063d709 5388 * Set open files limit to unlimited. This daemon can open a large number of
514bb9f0 5389 * file descriptors in order to consume multiple kernel traces.
f3ed775e
DG
5390 */
5391static void set_ulimit(void)
5392{
5393 int ret;
5394 struct rlimit lim;
5395
514bb9f0 5396 /* The kernel does not allow an infinite limit for open files */
f3ed775e
DG
5397 lim.rlim_cur = 65535;
5398 lim.rlim_max = 65535;
5399
5400 ret = setrlimit(RLIMIT_NOFILE, &lim);
5401 if (ret < 0) {
76d7553f 5402 PERROR("failed to set open files limit");
f3ed775e
DG
5403 }
5404}
5405
4a15001e 5406static int write_pidfile(void)
35f90c40 5407{
e6142f2e 5408 return utils_create_pid_file(getpid(), config.pid_file_path.value);
35f90c40
DG
5409}
5410
e6142f2e
JG
5411static int set_clock_plugin_env(void)
5412{
5413 int ret = 0;
5414 char *env_value = NULL;
cd9290dd 5415
e6142f2e
JG
5416 if (!config.lttng_ust_clock_plugin.value) {
5417 goto end;
5418 }
5419
5420 ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s",
5421 config.lttng_ust_clock_plugin.value);
cd9290dd 5422 if (ret < 0) {
e6142f2e
JG
5423 PERROR("asprintf");
5424 goto end;
cd9290dd
DG
5425 }
5426
e6142f2e
JG
5427 ret = putenv(env_value);
5428 if (ret) {
5429 free(env_value);
5430 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5431 goto end;
5432 }
cd9290dd 5433
e6142f2e
JG
5434 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5435 config.lttng_ust_clock_plugin.value);
5436end:
4a15001e 5437 return ret;
ef367a93
JG
5438}
5439
99d688f2
JG
5440static void destroy_all_sessions_and_wait(void)
5441{
5442 struct ltt_session *session, *tmp;
5443 struct ltt_session_list *session_list;
5444
5445 session_list = session_get_list();
5446 DBG("Initiating destruction of all sessions");
5447
5448 if (!session_list) {
5449 return;
5450 }
5451
5452 /*
5453 * Ensure that the client thread is no longer accepting new commands,
5454 * which could cause new sessions to be created.
5455 */
5456 sessiond_wait_client_thread_stopped();
5457
5458 session_lock_list();
5459 /* Initiate the destruction of all sessions. */
5460 cds_list_for_each_entry_safe(session, tmp,
5461 &session_list->head, list) {
5462 if (!session_get(session)) {
5463 continue;
5464 }
5465
5466 session_lock(session);
5467 if (session->destroyed) {
5468 goto unlock_session;
5469 }
5470 (void) cmd_destroy_session(session,
5471 notification_thread_handle);
5472 unlock_session:
5473 session_unlock(session);
5474 session_put(session);
5475 }
5476 session_unlock_list();
5477
5478 /* Wait for the destruction of all sessions to complete. */
5479 DBG("Waiting for the destruction of all sessions to complete");
5480 session_list_wait_empty();
5481 DBG("Destruction of all sessions completed");
5482}
5483
fac6795d
DG
5484/*
5485 * main
5486 */
5487int main(int argc, char **argv)
5488{
4a15001e 5489 int ret = 0, retval = 0;
fac6795d 5490 void *status;
e6142f2e 5491 const char *env_app_timeout;
b3530820
JG
5492 struct lttng_pipe *ust32_channel_monitor_pipe = NULL,
5493 *ust64_channel_monitor_pipe = NULL,
5494 *kernel_channel_monitor_pipe = NULL;
99611076 5495 bool timer_thread_launched = false;
a3707772 5496 struct lttng_thread *ht_cleanup_thread = NULL;
d086f507 5497 struct timer_thread_parameters timer_thread_ctx;
64d9b072
JG
5498 /* Rotation thread handle. */
5499 struct rotation_thread_handle *rotation_thread_handle = NULL;
d086f507
JD
5500 /* Queue of rotation jobs populated by the sessiond-timer. */
5501 struct rotation_thread_timer_queue *rotation_timer_queue = NULL;
fac6795d 5502
335a95b7
MD
5503 init_kernel_workarounds();
5504
f6a9efaa
DG
5505 rcu_register_thread();
5506
4a15001e
MD
5507 if (set_signal_handler()) {
5508 retval = -1;
5509 goto exit_set_signal_handler;
0bb7724a
DG
5510 }
5511
92816cc3 5512 if (timer_signal_init()) {
d086f507
JD
5513 retval = -1;
5514 goto exit_set_signal_handler;
5515 }
5516
12744796
DG
5517 page_size = sysconf(_SC_PAGESIZE);
5518 if (page_size < 0) {
5519 PERROR("sysconf _SC_PAGESIZE");
5520 page_size = LONG_MAX;
5521 WARN("Fallback page size to %ld", page_size);
5522 }
5523
e6142f2e
JG
5524 ret = sessiond_config_init(&config);
5525 if (ret) {
5526 retval = -1;
5527 goto exit_set_signal_handler;
5528 }
5529
2788b494
JR
5530 /*
5531 * Init config from environment variables.
5532 * Command line option override env configuration per-doc. Do env first.
5533 */
5534 sessiond_config_apply_env_config(&config);
5535
4a15001e
MD
5536 /*
5537 * Parse arguments and load the daemon configuration file.
5538 *
5539 * We have an exit_options exit path to free memory reserved by
5540 * set_options. This is needed because the rest of sessiond_cleanup()
5541 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5542 * depends on set_options.
5543 */
fac6795d 5544 progname = argv[0];
4a15001e
MD
5545 if (set_options(argc, argv)) {
5546 retval = -1;
5547 goto exit_options;
fac6795d
DG
5548 }
5549
e6142f2e
JG
5550 /*
5551 * Resolve all paths received as arguments, configuration option, or
5552 * through environment variable as absolute paths. This is necessary
5553 * since daemonizing causes the sessiond's current working directory
5554 * to '/'.
5555 */
5556 ret = sessiond_config_resolve_paths(&config);
5557 if (ret) {
5558 goto exit_options;
5559 }
5560
5561 /* Apply config. */
5562 lttng_opt_verbose = config.verbose;
5563 lttng_opt_quiet = config.quiet;
5564 kconsumer_data.err_unix_sock_path =
5565 config.kconsumerd_err_unix_sock_path.value;
5566 kconsumer_data.cmd_unix_sock_path =
5567 config.kconsumerd_cmd_unix_sock_path.value;
5568 ustconsumer32_data.err_unix_sock_path =
5569 config.consumerd32_err_unix_sock_path.value;
5570 ustconsumer32_data.cmd_unix_sock_path =
5571 config.consumerd32_cmd_unix_sock_path.value;
5572 ustconsumer64_data.err_unix_sock_path =
5573 config.consumerd64_err_unix_sock_path.value;
5574 ustconsumer64_data.cmd_unix_sock_path =
5575 config.consumerd64_cmd_unix_sock_path.value;
5576 set_clock_plugin_env();
5577
5578 sessiond_config_log(&config);
5579
f472090a
JG
5580 if (create_lttng_rundir()) {
5581 retval = -1;
5582 goto exit_options;
5583 }
5584
5585 /* Abort launch if a session daemon is already running. */
5586 if (check_existing_daemon()) {
5587 ERR("A session daemon is already running.");
5588 retval = -1;
5589 goto exit_options;
5590 }
5591
fac6795d 5592 /* Daemonize */
e6142f2e 5593 if (config.daemonize || config.background) {
ceed52b5
MD
5594 int i;
5595
72dd7491 5596 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
e6142f2e 5597 !config.background);
53094c05 5598 if (ret < 0) {
4a15001e
MD
5599 retval = -1;
5600 goto exit_options;
53094c05 5601 }
0bb7724a 5602
ceed52b5 5603 /*
0bb7724a 5604 * We are in the child. Make sure all other file descriptors are
4a15001e 5605 * closed, in case we are called with more opened file
f472090a 5606 * descriptors than the standard ones and the lock file.
ceed52b5
MD
5607 */
5608 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
f472090a
JG
5609 if (i == lockfile_fd) {
5610 continue;
5611 }
ceed52b5
MD
5612 (void) close(i);
5613 }
5614 }
5615
7567352f
MD
5616 if (run_as_create_worker(argv[0]) < 0) {
5617 goto exit_create_run_as_worker_cleanup;
5618 }
5619
4a15001e
MD
5620 /*
5621 * Starting from here, we can create threads. This needs to be after
5622 * lttng_daemonize due to RCU.
5623 */
5624
5625 /*
5626 * Initialize the health check subsystem. This call should set the
5627 * appropriate time values.
5628 */
5629 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
5630 if (!health_sessiond) {
5631 PERROR("health_app_create error");
5632 retval = -1;
5633 goto exit_health_sessiond_cleanup;
5634 }
5635
4a15001e 5636 /* Create thread to clean up RCU hash tables */
a3707772
JG
5637 ht_cleanup_thread = launch_ht_cleanup_thread();
5638 if (!ht_cleanup_thread) {
4a15001e
MD
5639 retval = -1;
5640 goto exit_ht_cleanup;
5641 }
5642
ceed52b5 5643 /* Create thread quit pipe */
a7333da7 5644 if (sessiond_init_thread_quit_pipe()) {
4a15001e
MD
5645 retval = -1;
5646 goto exit_init_data;
fac6795d
DG
5647 }
5648
5649 /* Check if daemon is UID = 0 */
5650 is_root = !getuid();
fac6795d 5651 if (is_root) {
67e40797 5652 /* Create global run dir with root access */
67e40797 5653
b3530820
JG
5654 kernel_channel_monitor_pipe = lttng_pipe_open(0);
5655 if (!kernel_channel_monitor_pipe) {
5656 ERR("Failed to create kernel consumer channel monitor pipe");
5657 retval = -1;
5658 goto exit_init_data;
5659 }
5660 kconsumer_data.channel_monitor_pipe =
5661 lttng_pipe_release_writefd(
5662 kernel_channel_monitor_pipe);
5663 if (kconsumer_data.channel_monitor_pipe < 0) {
5664 retval = -1;
5665 goto exit_init_data;
5666 }
fac6795d
DG
5667 }
5668
5c827ce0
DG
5669 /* Set consumer initial state */
5670 kernel_consumerd_state = CONSUMER_STOPPED;
5671 ust_consumerd_state = CONSUMER_STOPPED;
5672
b3530820
JG
5673 ust32_channel_monitor_pipe = lttng_pipe_open(0);
5674 if (!ust32_channel_monitor_pipe) {
5675 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5676 retval = -1;
5677 goto exit_init_data;
5678 }
5679 ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd(
5680 ust32_channel_monitor_pipe);
5681 if (ustconsumer32_data.channel_monitor_pipe < 0) {
5682 retval = -1;
5683 goto exit_init_data;
5684 }
62c43103 5685
d086f507 5686 /*
92816cc3
JG
5687 * The rotation_thread_timer_queue structure is shared between the
5688 * sessiond timer thread and the rotation thread. The main thread keeps
5689 * its ownership and destroys it when both threads have been joined.
d086f507 5690 */
92816cc3 5691 rotation_timer_queue = rotation_thread_timer_queue_create();
d086f507
JD
5692 if (!rotation_timer_queue) {
5693 retval = -1;
5694 goto exit_init_data;
5695 }
92816cc3 5696 timer_thread_ctx.rotation_thread_job_queue = rotation_timer_queue;
67e40797 5697
b3530820
JG
5698 ust64_channel_monitor_pipe = lttng_pipe_open(0);
5699 if (!ust64_channel_monitor_pipe) {
5700 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
5701 retval = -1;
5702 goto exit_init_data;
5703 }
5704 ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd(
5705 ust64_channel_monitor_pipe);
5706 if (ustconsumer64_data.channel_monitor_pipe < 0) {
5707 retval = -1;
5708 goto exit_init_data;
5709 }
847177cd 5710
1427f9b2
DG
5711 /*
5712 * Init UST app hash table. Alloc hash table before this point since
5713 * cleanup() can get called after that point.
5714 */
4a15001e 5715 if (ust_app_ht_alloc()) {
ddbeb0f6 5716 ERR("Failed to allocate UST app hash table");
4a15001e
MD
5717 retval = -1;
5718 goto exit_init_data;
5719 }
1427f9b2 5720
6a4e4039
JG
5721 /*
5722 * Initialize agent app hash table. We allocate the hash table here
5723 * since cleanup() can get called after this point.
5724 */
5725 if (agent_app_ht_alloc()) {
5726 ERR("Failed to allocate Agent app hash table");
4a15001e
MD
5727 retval = -1;
5728 goto exit_init_data;
f20baf8e
DG
5729 }
5730
a88df331
DG
5731 /*
5732 * These actions must be executed as root. We do that *after* setting up
5733 * the sockets path because we MUST make the check for another daemon using
5734 * those paths *before* trying to set the kernel consumer sockets and init
5735 * kernel tracer.
5736 */
5737 if (is_root) {
e6142f2e 5738 if (set_consumer_sockets(&kconsumer_data)) {
4a15001e
MD
5739 retval = -1;
5740 goto exit_init_data;
7753dea8
MD
5741 }
5742
a88df331 5743 /* Setup kernel tracer */
e6142f2e 5744 if (!config.no_kernel) {
4fba7219 5745 init_kernel_tracer();
89588270
DG
5746 if (kernel_tracer_fd >= 0) {
5747 ret = syscall_init_table();
5748 if (ret < 0) {
4a15001e
MD
5749 ERR("Unable to populate syscall table. "
5750 "Syscall tracing won't work "
5751 "for this session daemon.");
89588270 5752 }
834978fd 5753 }
4fba7219 5754 }
a88df331
DG
5755
5756 /* Set ulimit for open files */
5757 set_ulimit();
fac6795d 5758 }
4063050c
MD
5759 /* init lttng_fd tracking must be done after set_ulimit. */
5760 lttng_fd_init();
fac6795d 5761
e6142f2e 5762 if (set_consumer_sockets(&ustconsumer64_data)) {
4a15001e
MD
5763 retval = -1;
5764 goto exit_init_data;
67e40797
DG
5765 }
5766
e6142f2e 5767 if (set_consumer_sockets(&ustconsumer32_data)) {
4a15001e
MD
5768 retval = -1;
5769 goto exit_init_data;
67e40797
DG
5770 }
5771
d6f42150 5772 /* Setup the needed unix socket */
4a15001e
MD
5773 if (init_daemon_socket()) {
5774 retval = -1;
5775 goto exit_init_data;
fac6795d
DG
5776 }
5777
5778 /* Set credentials to socket */
e6142f2e 5779 if (is_root && set_permissions(config.rundir.value)) {
4a15001e
MD
5780 retval = -1;
5781 goto exit_init_data;
fac6795d
DG
5782 }
5783
5b8719f5 5784 /* Get parent pid if -S, --sig-parent is specified. */
e6142f2e 5785 if (config.sig_parent) {
5b8719f5
DG
5786 ppid = getppid();
5787 }
5788
7a485870 5789 /* Setup the kernel pipe for waking up the kernel thread */
e6142f2e 5790 if (is_root && !config.no_kernel) {
4a15001e
MD
5791 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
5792 retval = -1;
5793 goto exit_init_data;
6620da75 5794 }
7a485870
DG
5795 }
5796
099e26bd 5797 /* Setup the thread apps communication pipe. */
4a15001e
MD
5798 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
5799 retval = -1;
5800 goto exit_init_data;
099e26bd
DG
5801 }
5802
d0b96690 5803 /* Setup the thread apps notify communication pipe. */
4a15001e
MD
5804 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
5805 retval = -1;
5806 goto exit_init_data;
d0b96690
DG
5807 }
5808
7972aab2
DG
5809 /* Initialize global buffer per UID and PID registry. */
5810 buffer_reg_init_uid_registry();
5811 buffer_reg_init_pid_registry();
5812
099e26bd 5813 /* Init UST command queue. */
8bdee6e2 5814 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
099e26bd 5815
2f77fc4b 5816 cmd_init();
00e2e675 5817
ae9e45b3
DG
5818 /* Check for the application socket timeout env variable. */
5819 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
5820 if (env_app_timeout) {
e6142f2e 5821 config.app_socket_timeout = atoi(env_app_timeout);
ae9e45b3 5822 } else {
e6142f2e 5823 config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
ae9e45b3
DG
5824 }
5825
4a15001e
MD
5826 ret = write_pidfile();
5827 if (ret) {
5828 ERR("Error in write_pidfile");
5829 retval = -1;
5830 goto exit_init_data;
5831 }
35f90c40 5832
554831e7
MD
5833 /* Initialize communication library */
5834 lttcomm_init();
4a15001e 5835 /* Initialize TCP timeout values */
d831c249 5836 lttcomm_inet_init();
554831e7 5837
ef367a93 5838 if (load_session_init_data(&load_info) < 0) {
4a15001e
MD
5839 retval = -1;
5840 goto exit_init_data;
ef367a93 5841 }
e6142f2e 5842 load_info->path = config.load_session_path.value;
ef367a93 5843
12b4fb37 5844 /* Create health-check thread. */
173fca4f 5845 if (!launch_health_management_thread()) {
4a15001e 5846 retval = -1;
44a5e5eb
DG
5847 goto exit_health;
5848 }
5849
b3530820
JG
5850 /* notification_thread_data acquires the pipes' read side. */
5851 notification_thread_handle = notification_thread_handle_create(
5852 ust32_channel_monitor_pipe,
5853 ust64_channel_monitor_pipe,
c8a9de5a 5854 kernel_channel_monitor_pipe);
b3530820
JG
5855 if (!notification_thread_handle) {
5856 retval = -1;
5857 ERR("Failed to create notification thread shared data");
b3530820
JG
5858 goto exit_notification;
5859 }
5860
5861 /* Create notification thread. */
c8a9de5a 5862 if (!launch_notification_thread(notification_thread_handle)) {
b3530820 5863 retval = -1;
b3530820 5864 goto exit_notification;
c8a9de5a 5865
b3530820
JG
5866 }
5867
d086f507
JD
5868 /* Create timer thread. */
5869 ret = pthread_create(&timer_thread, default_pthread_attr(),
92816cc3 5870 timer_thread_func, &timer_thread_ctx);
d086f507
JD
5871 if (ret) {
5872 errno = ret;
5873 PERROR("pthread_create timer");
5874 retval = -1;
5875 stop_threads();
5876 goto exit_notification;
5877 }
99611076 5878 timer_thread_launched = true;
d086f507 5879
db66e574
JD
5880 /* rotation_thread_data acquires the pipes' read side. */
5881 rotation_thread_handle = rotation_thread_handle_create(
90936dcf 5882 rotation_timer_queue,
c8a9de5a 5883 notification_thread_handle);
db66e574
JD
5884 if (!rotation_thread_handle) {
5885 retval = -1;
5886 ERR("Failed to create rotation thread shared data");
5887 stop_threads();
5888 goto exit_rotation;
5889 }
db66e574
JD
5890
5891 /* Create rotation thread. */
64d9b072 5892 if (!launch_rotation_thread(rotation_thread_handle)) {
db66e574 5893 retval = -1;
db66e574
JD
5894 goto exit_rotation;
5895 }
5896
cf3af59e 5897 /* Create thread to manage the client socket */
1a1a34b4 5898 ret = pthread_create(&client_thread, default_pthread_attr(),
099e26bd 5899 thread_manage_clients, (void *) NULL);
4a15001e
MD
5900 if (ret) {
5901 errno = ret;
76d7553f 5902 PERROR("pthread_create clients");
4a15001e 5903 retval = -1;
b3530820 5904 stop_threads();
cf3af59e
MD
5905 goto exit_client;
5906 }
fac6795d 5907
099e26bd 5908 /* Create thread to dispatch registration */
1a1a34b4 5909 ret = pthread_create(&dispatch_thread, default_pthread_attr(),
099e26bd 5910 thread_dispatch_ust_registration, (void *) NULL);
4a15001e
MD
5911 if (ret) {
5912 errno = ret;
76d7553f 5913 PERROR("pthread_create dispatch");
4a15001e 5914 retval = -1;
b3530820 5915 stop_threads();
099e26bd
DG
5916 goto exit_dispatch;
5917 }
5918
5919 /* Create thread to manage application registration. */
1a1a34b4 5920 ret = pthread_create(&reg_apps_thread, default_pthread_attr(),
099e26bd 5921 thread_registration_apps, (void *) NULL);
4a15001e
MD
5922 if (ret) {
5923 errno = ret;
76d7553f 5924 PERROR("pthread_create registration");
4a15001e 5925 retval = -1;
b3530820 5926 stop_threads();
099e26bd
DG
5927 goto exit_reg_apps;
5928 }
5929
cf3af59e 5930 /* Create thread to manage application socket */
1a1a34b4 5931 ret = pthread_create(&apps_thread, default_pthread_attr(),
54d01ffb 5932 thread_manage_apps, (void *) NULL);
4a15001e
MD
5933 if (ret) {
5934 errno = ret;
d0b96690 5935 PERROR("pthread_create apps");
4a15001e 5936 retval = -1;
b3530820 5937 stop_threads();
d0b96690
DG
5938 goto exit_apps;
5939 }
5940
5941 /* Create thread to manage application notify socket */
1a1a34b4 5942 ret = pthread_create(&apps_notify_thread, default_pthread_attr(),
d0b96690 5943 ust_thread_manage_notify, (void *) NULL);
4a15001e
MD
5944 if (ret) {
5945 errno = ret;
f5c32ef1 5946 PERROR("pthread_create notify");
4a15001e 5947 retval = -1;
b3530820 5948 stop_threads();
9563b0ad 5949 goto exit_apps_notify;
cf3af59e 5950 }
fac6795d 5951
022d91ba 5952 /* Create agent registration thread. */
1a1a34b4 5953 ret = pthread_create(&agent_reg_thread, default_pthread_attr(),
022d91ba 5954 agent_thread_manage_registration, (void *) NULL);
4a15001e
MD
5955 if (ret) {
5956 errno = ret;
022d91ba 5957 PERROR("pthread_create agent");
4a15001e 5958 retval = -1;
b3530820 5959 stop_threads();
022d91ba 5960 goto exit_agent_reg;
4d076222
DG
5961 }
5962
6620da75 5963 /* Don't start this thread if kernel tracing is not requested nor root */
e6142f2e 5964 if (is_root && !config.no_kernel) {
6620da75 5965 /* Create kernel thread to manage kernel event */
1a1a34b4 5966 ret = pthread_create(&kernel_thread, default_pthread_attr(),
6620da75 5967 thread_manage_kernel, (void *) NULL);
4a15001e
MD
5968 if (ret) {
5969 errno = ret;
6620da75 5970 PERROR("pthread_create kernel");
4a15001e 5971 retval = -1;
b3530820 5972 stop_threads();
6620da75
DG
5973 goto exit_kernel;
5974 }
ef367a93 5975 }
7a485870 5976
4a15001e 5977 /* Create session loading thread. */
1a1a34b4
MJ
5978 ret = pthread_create(&load_session_thread, default_pthread_attr(),
5979 thread_load_session, load_info);
4a15001e
MD
5980 if (ret) {
5981 errno = ret;
5982 PERROR("pthread_create load_session_thread");
5983 retval = -1;
b3530820 5984 stop_threads();
4a15001e
MD
5985 goto exit_load_session;
5986 }
5987
5988 /*
5989 * This is where we start awaiting program completion (e.g. through
5990 * signal that asks threads to teardown).
5991 */
5992
5993 ret = pthread_join(load_session_thread, &status);
5994 if (ret) {
5995 errno = ret;
5996 PERROR("pthread_join load_session_thread");
5997 retval = -1;
5998 }
99d688f2
JG
5999
6000 /* Initiate teardown once activity occurs on the quit pipe. */
6001 sessiond_wait_for_quit_pipe(-1U);
6002 destroy_all_sessions_and_wait();
4a15001e 6003exit_load_session:
ef367a93 6004
e6142f2e 6005 if (is_root && !config.no_kernel) {
6620da75 6006 ret = pthread_join(kernel_thread, &status);
4a15001e
MD
6007 if (ret) {
6008 errno = ret;
6620da75 6009 PERROR("pthread_join");
4a15001e 6010 retval = -1;
6620da75 6011 }
fac6795d 6012 }
cf3af59e 6013exit_kernel:
4a15001e 6014
022d91ba 6015 ret = pthread_join(agent_reg_thread, &status);
4a15001e
MD
6016 if (ret) {
6017 errno = ret;
022d91ba 6018 PERROR("pthread_join agent");
4a15001e 6019 retval = -1;
4d076222 6020 }
022d91ba 6021exit_agent_reg:
4a15001e 6022
9563b0ad 6023 ret = pthread_join(apps_notify_thread, &status);
4a15001e
MD
6024 if (ret) {
6025 errno = ret;
9563b0ad 6026 PERROR("pthread_join apps notify");
4a15001e 6027 retval = -1;
9563b0ad 6028 }
9563b0ad 6029exit_apps_notify:
4a15001e 6030
cf3af59e 6031 ret = pthread_join(apps_thread, &status);
4a15001e
MD
6032 if (ret) {
6033 errno = ret;
9563b0ad 6034 PERROR("pthread_join apps");
4a15001e 6035 retval = -1;
cf3af59e 6036 }
cf3af59e 6037exit_apps:
4a15001e 6038
099e26bd 6039 ret = pthread_join(reg_apps_thread, &status);
4a15001e
MD
6040 if (ret) {
6041 errno = ret;
76d7553f 6042 PERROR("pthread_join");
4a15001e 6043 retval = -1;
099e26bd 6044 }
099e26bd 6045exit_reg_apps:
4a15001e 6046
772b8f4d
MD
6047 /*
6048 * Join dispatch thread after joining reg_apps_thread to ensure
6049 * we don't leak applications in the queue.
6050 */
099e26bd 6051 ret = pthread_join(dispatch_thread, &status);
4a15001e
MD
6052 if (ret) {
6053 errno = ret;
76d7553f 6054 PERROR("pthread_join");
4a15001e 6055 retval = -1;
099e26bd 6056 }
099e26bd 6057exit_dispatch:
4a15001e 6058
cf3af59e 6059 ret = pthread_join(client_thread, &status);
4a15001e
MD
6060 if (ret) {
6061 errno = ret;
76d7553f 6062 PERROR("pthread_join");
4a15001e 6063 retval = -1;
cf3af59e 6064 }
a88df331 6065
b3530820 6066exit_client:
db66e574 6067exit_rotation:
b3530820 6068exit_notification:
a3707772 6069 lttng_thread_list_shutdown_orphans();
b3530820 6070exit_health:
4a15001e 6071exit_init_data:
4d62fbf8
MD
6072 /*
6073 * Wait for all pending call_rcu work to complete before tearing
6074 * down data structures. call_rcu worker may be trying to
6075 * perform lookups in those structures.
6076 */
6077 rcu_barrier();
4a15001e
MD
6078 /*
6079 * sessiond_cleanup() is called when no other thread is running, except
6080 * the ht_cleanup thread, which is needed to destroy the hash tables.
6081 */
6082 rcu_thread_online();
6083 sessiond_cleanup();
06f525de 6084
c4d5de9d
JG
6085 /*
6086 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6087 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6088 * the queue is empty before shutting down the clean-up thread.
6089 */
6090 rcu_barrier();
6091
99611076 6092 if (timer_thread_launched) {
92816cc3 6093 timer_exit();
d086f507
JD
6094 ret = pthread_join(timer_thread, &status);
6095 if (ret) {
6096 errno = ret;
6097 PERROR("pthread_join timer thread");
6098 retval = -1;
6099 }
6100 }
6101
a3707772
JG
6102 if (ht_cleanup_thread) {
6103 lttng_thread_shutdown(ht_cleanup_thread);
6104 lttng_thread_put(ht_cleanup_thread);
6105 }
6106
64d9b072
JG
6107 rcu_thread_offline();
6108 rcu_unregister_thread();
6109
6110 if (rotation_thread_handle) {
6111 rotation_thread_handle_destroy(rotation_thread_handle);
6112 }
6113
d086f507
JD
6114 /*
6115 * After the rotation and timer thread have quit, we can safely destroy
6116 * the rotation_timer_queue.
6117 */
92816cc3 6118 rotation_thread_timer_queue_destroy(rotation_timer_queue);
c8a9de5a
JG
6119 /*
6120 * The teardown of the notification system is performed after the
6121 * session daemon's teardown in order to allow it to be notified
6122 * of the active session and channels at the moment of the teardown.
6123 */
6124 if (notification_thread_handle) {
6125 notification_thread_handle_destroy(notification_thread_handle);
6126 }
b3530820
JG
6127 lttng_pipe_destroy(ust32_channel_monitor_pipe);
6128 lttng_pipe_destroy(ust64_channel_monitor_pipe);
6129 lttng_pipe_destroy(kernel_channel_monitor_pipe);
0b2dc8df 6130exit_ht_cleanup:
4a15001e 6131
4a15001e
MD
6132 health_app_destroy(health_sessiond);
6133exit_health_sessiond_cleanup:
7567352f 6134exit_create_run_as_worker_cleanup:
4a15001e
MD
6135
6136exit_options:
f472090a 6137 sessiond_cleanup_lock_file();
4a15001e
MD
6138 sessiond_cleanup_options();
6139
6140exit_set_signal_handler:
6141 if (!retval) {
cf3af59e 6142 exit(EXIT_SUCCESS);
4a15001e
MD
6143 } else {
6144 exit(EXIT_FAILURE);
67e40797 6145 }
fac6795d 6146}
This page took 0.584703 seconds and 5 git commands to generate.