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