Fix: JUL to enable user and root tracepoints
[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>
fac6795d 4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
91d76f53 8 *
d14d33bf
AM
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
91d76f53 13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
fac6795d
DG
17 */
18
19#define _GNU_SOURCE
fac6795d
DG
20#include <getopt.h>
21#include <grp.h>
22#include <limits.h>
0bb7724a 23#include <paths.h>
fac6795d
DG
24#include <pthread.h>
25#include <signal.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
331744e3 29#include <inttypes.h>
0fdd1e2c 30#include <sys/mman.h>
b73401da 31#include <sys/mount.h>
1e307fab 32#include <sys/resource.h>
fac6795d
DG
33#include <sys/socket.h>
34#include <sys/stat.h>
35#include <sys/types.h>
0fdd1e2c 36#include <sys/wait.h>
5c827ce0 37#include <urcu/uatomic.h>
fac6795d 38#include <unistd.h>
3bd1e081 39#include <config.h>
fac6795d 40
990570ed 41#include <common/common.h>
d27c42b8 42#include <common/compat/socket.h>
db758600
DG
43#include <common/defaults.h>
44#include <common/kernel-consumer/kernel-consumer.h>
50c8f484 45#include <common/futex.h>
00e2e675 46#include <common/relayd/relayd.h>
81b86775 47#include <common/utils.h>
449935a1 48#include <common/daemonize.h>
fac6795d 49
10a8a223 50#include "lttng-sessiond.h"
7972aab2 51#include "buffer-registry.h"
54d01ffb 52#include "channel.h"
2f77fc4b 53#include "cmd.h"
00e2e675 54#include "consumer.h"
099e26bd 55#include "context.h"
54d01ffb 56#include "event.h"
4771f025 57#include "kernel.h"
f1e16794 58#include "kernel-consumer.h"
096102bd 59#include "modprobe.h"
0fdd1e2c 60#include "shm.h"
1e307fab 61#include "ust-ctl.h"
00e2e675 62#include "ust-consumer.h"
8e68d1c8 63#include "utils.h"
4063050c 64#include "fd-limit.h"
8782cc74 65#include "health-sessiond.h"
8ac94142 66#include "testpoint.h"
d0b96690 67#include "ust-thread.h"
4d076222 68#include "jul-thread.h"
fac6795d 69
ebaeda94
MD
70#define CONSUMERD_FILE "lttng-consumerd"
71
fac6795d 72const char *progname;
6c71277b 73static const char *tracing_group_name = DEFAULT_TRACING_GROUP;
35f90c40 74static const char *opt_pidfile;
5b8719f5 75static int opt_sig_parent;
97e19046 76static int opt_verbose_consumer;
929de64e 77static int opt_daemon, opt_background;
4fba7219 78static int opt_no_kernel;
1d4b027a 79static pid_t ppid; /* Parent PID for --sig-parent option */
0bb7724a 80static pid_t child_ppid; /* Internal parent PID use with daemonize. */
67e40797 81static char *rundir;
3bd1e081 82
0bb7724a
DG
83/* Set to 1 when a SIGUSR1 signal is received. */
84static int recv_child_signal;
85
a23ec3a7
DG
86/*
87 * Consumer daemon specific control data. Every value not initialized here is
88 * set to 0 by the static definition.
89 */
3bd1e081
MD
90static struct consumer_data kconsumer_data = {
91 .type = LTTNG_CONSUMER_KERNEL,
60922cb0
DG
92 .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
93 .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
03550b58
MD
94 .err_sock = -1,
95 .cmd_sock = -1,
173af62f
DG
96 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
97 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
98 .cond = PTHREAD_COND_INITIALIZER,
99 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
3bd1e081 100};
7753dea8
MD
101static struct consumer_data ustconsumer64_data = {
102 .type = LTTNG_CONSUMER64_UST,
60922cb0
DG
103 .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
104 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
03550b58
MD
105 .err_sock = -1,
106 .cmd_sock = -1,
173af62f
DG
107 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
108 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
109 .cond = PTHREAD_COND_INITIALIZER,
110 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
7753dea8
MD
111};
112static struct consumer_data ustconsumer32_data = {
113 .type = LTTNG_CONSUMER32_UST,
60922cb0
DG
114 .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
115 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
03550b58
MD
116 .err_sock = -1,
117 .cmd_sock = -1,
173af62f
DG
118 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
119 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
120 .cond = PTHREAD_COND_INITIALIZER,
121 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
3bd1e081
MD
122};
123
26c9d55e 124/* Shared between threads */
099e26bd 125static int dispatch_thread_exit;
fac6795d 126
54d01ffb
DG
127/* Global application Unix socket path */
128static char apps_unix_sock_path[PATH_MAX];
129/* Global client Unix socket path */
130static char client_unix_sock_path[PATH_MAX];
54d01ffb
DG
131/* global wait shm path for UST */
132static char wait_shm_path[PATH_MAX];
44a5e5eb
DG
133/* Global health check unix path */
134static char health_unix_sock_path[PATH_MAX];
fac6795d 135
1d4b027a 136/* Sockets and FDs */
a4b35e07
MD
137static int client_sock = -1;
138static int apps_sock = -1;
2f77fc4b 139int kernel_tracer_fd = -1;
76d7553f 140static int kernel_poll_pipe[2] = { -1, -1 };
1d4b027a 141
273ea72c
DG
142/*
143 * Quit pipe for all threads. This permits a single cancellation point
144 * for all threads when receiving an event on the pipe.
145 */
76d7553f 146static int thread_quit_pipe[2] = { -1, -1 };
273ea72c 147
099e26bd
DG
148/*
149 * This pipe is used to inform the thread managing application communication
150 * that a command is queued and ready to be processed.
151 */
76d7553f 152static int apps_cmd_pipe[2] = { -1, -1 };
099e26bd 153
d0b96690
DG
154int apps_cmd_notify_pipe[2] = { -1, -1 };
155
1d4b027a 156/* Pthread, Mutexes and Semaphores */
1d4b027a 157static pthread_t apps_thread;
d0b96690 158static pthread_t apps_notify_thread;
099e26bd 159static pthread_t reg_apps_thread;
1d4b027a 160static pthread_t client_thread;
7a485870 161static pthread_t kernel_thread;
099e26bd 162static pthread_t dispatch_thread;
44a5e5eb 163static pthread_t health_thread;
0b2dc8df 164static pthread_t ht_cleanup_thread;
4d076222 165static pthread_t jul_reg_thread;
5eb91c98 166
099e26bd
DG
167/*
168 * UST registration command queue. This queue is tied with a futex and uses a N
169 * wakers / 1 waiter implemented and detailed in futex.c/.h
170 *
171 * The thread_manage_apps and thread_dispatch_ust_registration interact with
172 * this queue and the wait/wake scheme.
173 */
174static struct ust_cmd_queue ust_cmd_queue;
175
b5541356
DG
176/*
177 * Pointer initialized before thread creation.
178 *
179 * This points to the tracing session list containing the session count and a
180 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
181 * MUST NOT be taken if you call a public function in session.c.
04ea676f 182 *
d063d709 183 * The lock is nested inside the structure: session_list_ptr->lock. Please use
54d01ffb 184 * session_lock_list and session_unlock_list for lock acquisition.
b5541356
DG
185 */
186static struct ltt_session_list *session_list_ptr;
187
7753dea8
MD
188int ust_consumerd64_fd = -1;
189int ust_consumerd32_fd = -1;
190
fb6f1fa2
YB
191static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN;
192static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN;
193static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR;
194static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR;
fb09408a 195
2f77fc4b
DG
196static const char *module_proc_lttng = "/proc/lttng";
197
5c827ce0
DG
198/*
199 * Consumer daemon state which is changed when spawning it, killing it or in
200 * case of a fatal error.
201 */
202enum consumerd_state {
203 CONSUMER_STARTED = 1,
204 CONSUMER_STOPPED = 2,
205 CONSUMER_ERROR = 3,
206};
207
208/*
209 * This consumer daemon state is used to validate if a client command will be
210 * able to reach the consumer. If not, the client is informed. For instance,
211 * doing a "lttng start" when the consumer state is set to ERROR will return an
212 * error to the client.
213 *
214 * The following example shows a possible race condition of this scheme:
215 *
216 * consumer thread error happens
217 * client cmd arrives
218 * client cmd checks state -> still OK
219 * consumer thread exit, sets error
220 * client cmd try to talk to consumer
221 * ...
222 *
223 * However, since the consumer is a different daemon, we have no way of making
224 * sure the command will reach it safely even with this state flag. This is why
225 * we consider that up to the state validation during command processing, the
226 * command is safe. After that, we can not guarantee the correctness of the
227 * client request vis-a-vis the consumer.
228 */
229static enum consumerd_state ust_consumerd_state;
230static enum consumerd_state kernel_consumerd_state;
231
ae9e45b3
DG
232/*
233 * Socket timeout for receiving and sending in seconds.
234 */
235static int app_socket_timeout;
236
12744796
DG
237/* Set in main() with the current page size. */
238long page_size;
239
8782cc74
MD
240/* Application health monitoring */
241struct health_app *health_sessiond;
242
4d076222
DG
243/* JUL TCP port for registration. Used by the JUL thread. */
244unsigned int jul_tcp_port = DEFAULT_JUL_TCP_PORT;
245
59cec096
DG
246/* Am I root or not. */
247int is_root; /* Set to 1 if the daemon is running as root */
248
56045742
MD
249/*
250 * Whether sessiond is ready for commands/health check requests.
251 * NR_LTTNG_SESSIOND_READY must match the number of calls to
252 * lttng_sessiond_notify_ready().
253 */
254#define NR_LTTNG_SESSIOND_READY 2
255int lttng_sessiond_ready = NR_LTTNG_SESSIOND_READY;
256
257/* Notify parents that we are ready for cmd and health check */
258static
259void lttng_sessiond_notify_ready(void)
260{
261 if (uatomic_sub_return(&lttng_sessiond_ready, 1) == 0) {
262 /*
263 * Notify parent pid that we are ready to accept command
264 * for client side. This ppid is the one from the
265 * external process that spawned us.
266 */
267 if (opt_sig_parent) {
268 kill(ppid, SIGUSR1);
269 }
270
271 /*
272 * Notify the parent of the fork() process that we are
273 * ready.
274 */
929de64e 275 if (opt_daemon || opt_background) {
56045742
MD
276 kill(child_ppid, SIGUSR1);
277 }
278 }
279}
280
fb09408a 281static
7753dea8 282void setup_consumerd_path(void)
fb09408a 283{
fc7a59ce 284 const char *bin, *libdir;
fb09408a 285
7753dea8
MD
286 /*
287 * Allow INSTALL_BIN_PATH to be used as a target path for the
ebaeda94
MD
288 * native architecture size consumer if CONFIG_CONSUMER*_PATH
289 * has not been defined.
7753dea8 290 */
ebaeda94 291#if (CAA_BITS_PER_LONG == 32)
fc7a59ce
AM
292 if (!consumerd32_bin[0]) {
293 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
ebaeda94
MD
294 }
295 if (!consumerd32_libdir[0]) {
296 consumerd32_libdir = INSTALL_LIB_PATH;
297 }
298#elif (CAA_BITS_PER_LONG == 64)
fc7a59ce
AM
299 if (!consumerd64_bin[0]) {
300 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
7753dea8 301 }
ebaeda94
MD
302 if (!consumerd64_libdir[0]) {
303 consumerd64_libdir = INSTALL_LIB_PATH;
7753dea8
MD
304 }
305#else
306#error "Unknown bitness"
307#endif
308
fb09408a
MD
309 /*
310 * runtime env. var. overrides the build default.
311 */
fc7a59ce
AM
312 bin = getenv("LTTNG_CONSUMERD32_BIN");
313 if (bin) {
314 consumerd32_bin = bin;
7753dea8 315 }
fc7a59ce
AM
316 bin = getenv("LTTNG_CONSUMERD64_BIN");
317 if (bin) {
318 consumerd64_bin = bin;
ebaeda94 319 }
72f579ee 320 libdir = getenv("LTTNG_CONSUMERD32_LIBDIR");
ebaeda94
MD
321 if (libdir) {
322 consumerd32_libdir = libdir;
323 }
72f579ee 324 libdir = getenv("LTTNG_CONSUMERD64_LIBDIR");
ebaeda94
MD
325 if (libdir) {
326 consumerd64_libdir = libdir;
fb09408a
MD
327 }
328}
329
5eb91c98
DG
330/*
331 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
332 */
d0b96690 333int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
5eb91c98
DG
334{
335 int ret;
336
d0b96690 337 assert(events);
5eb91c98
DG
338
339 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
340 if (ret < 0) {
341 goto error;
342 }
343
344 /* Add quit pipe */
d0b96690 345 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
5eb91c98
DG
346 if (ret < 0) {
347 goto error;
348 }
349
350 return 0;
351
352error:
353 return ret;
354}
355
356/*
357 * Check if the thread quit pipe was triggered.
358 *
359 * Return 1 if it was triggered else 0;
360 */
d0b96690 361int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
5eb91c98
DG
362{
363 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
364 return 1;
365 }
366
367 return 0;
368}
369
273ea72c 370/*
5eb91c98 371 * Init thread quit pipe.
273ea72c
DG
372 *
373 * Return -1 on error or 0 if all pipes are created.
374 */
375static int init_thread_quit_pipe(void)
376{
730389d9 377 int ret, i;
273ea72c 378
730389d9 379 ret = pipe(thread_quit_pipe);
273ea72c 380 if (ret < 0) {
730389d9 381 PERROR("thread quit pipe");
273ea72c
DG
382 goto error;
383 }
384
730389d9
DG
385 for (i = 0; i < 2; i++) {
386 ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC);
387 if (ret < 0) {
388 PERROR("fcntl");
389 goto error;
390 }
391 }
392
273ea72c
DG
393error:
394 return ret;
395}
396
099e26bd
DG
397/*
398 * Stop all threads by closing the thread quit pipe.
399 */
cf3af59e
MD
400static void stop_threads(void)
401{
5eb91c98
DG
402 int ret;
403
cf3af59e
MD
404 /* Stopping all threads */
405 DBG("Terminating all threads");
54d01ffb 406 ret = notify_thread_pipe(thread_quit_pipe[1]);
5eb91c98
DG
407 if (ret < 0) {
408 ERR("write error on thread quit pipe");
409 }
410
099e26bd 411 /* Dispatch thread */
26c9d55e 412 CMM_STORE_SHARED(dispatch_thread_exit, 1);
099e26bd 413 futex_nto1_wake(&ust_cmd_queue.futex);
cf3af59e
MD
414}
415
e975f9f8
DG
416/*
417 * Close every consumer sockets.
418 */
419static void close_consumer_sockets(void)
420{
421 int ret;
422
423 if (kconsumer_data.err_sock >= 0) {
424 ret = close(kconsumer_data.err_sock);
425 if (ret < 0) {
426 PERROR("kernel consumer err_sock close");
427 }
428 }
429 if (ustconsumer32_data.err_sock >= 0) {
430 ret = close(ustconsumer32_data.err_sock);
431 if (ret < 0) {
a76cbd9f 432 PERROR("UST consumerd32 err_sock close");
e975f9f8
DG
433 }
434 }
435 if (ustconsumer64_data.err_sock >= 0) {
436 ret = close(ustconsumer64_data.err_sock);
437 if (ret < 0) {
a76cbd9f 438 PERROR("UST consumerd64 err_sock close");
e975f9f8
DG
439 }
440 }
441 if (kconsumer_data.cmd_sock >= 0) {
442 ret = close(kconsumer_data.cmd_sock);
443 if (ret < 0) {
444 PERROR("kernel consumer cmd_sock close");
445 }
446 }
447 if (ustconsumer32_data.cmd_sock >= 0) {
448 ret = close(ustconsumer32_data.cmd_sock);
449 if (ret < 0) {
a76cbd9f 450 PERROR("UST consumerd32 cmd_sock close");
e975f9f8
DG
451 }
452 }
453 if (ustconsumer64_data.cmd_sock >= 0) {
454 ret = close(ustconsumer64_data.cmd_sock);
455 if (ret < 0) {
a76cbd9f 456 PERROR("UST consumerd64 cmd_sock close");
e975f9f8
DG
457 }
458 }
459}
460
fac6795d 461/*
d063d709 462 * Cleanup the daemon
fac6795d 463 */
cf3af59e 464static void cleanup(void)
fac6795d 465{
ef599319 466 int ret;
af9737e9 467 struct ltt_session *sess, *stmp;
8c6c56c2 468 char path[PATH_MAX];
fac6795d 469
1d4b027a 470 DBG("Cleaning up");
e07ae692 471
4e449f3f
MD
472 /*
473 * Close the thread quit pipe. It has already done its job,
474 * since we are now called.
475 */
2f77fc4b
DG
476 utils_close_pipe(thread_quit_pipe);
477
35f90c40
DG
478 /*
479 * If opt_pidfile is undefined, the default file will be wiped when
480 * removing the rundir.
481 */
482 if (opt_pidfile) {
483 ret = remove(opt_pidfile);
484 if (ret < 0) {
485 PERROR("remove pidfile %s", opt_pidfile);
486 }
487 }
488
8c6c56c2
MD
489 DBG("Removing sessiond and consumerd content of directory %s", rundir);
490
491 /* sessiond */
492 snprintf(path, PATH_MAX,
493 "%s/%s",
494 rundir, DEFAULT_LTTNG_SESSIOND_PIDFILE);
495 DBG("Removing %s", path);
496 (void) unlink(path);
497
cd9290dd
DG
498 snprintf(path, PATH_MAX, "%s/%s", rundir,
499 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE);
500 DBG("Removing %s", path);
501 (void) unlink(path);
502
8c6c56c2
MD
503 /* kconsumerd */
504 snprintf(path, PATH_MAX,
505 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
506 rundir);
507 DBG("Removing %s", path);
508 (void) unlink(path);
509
510 snprintf(path, PATH_MAX,
511 DEFAULT_KCONSUMERD_PATH,
512 rundir);
513 DBG("Removing directory %s", path);
514 (void) rmdir(path);
515
516 /* ust consumerd 32 */
517 snprintf(path, PATH_MAX,
518 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
519 rundir);
520 DBG("Removing %s", path);
521 (void) unlink(path);
522
523 snprintf(path, PATH_MAX,
524 DEFAULT_USTCONSUMERD32_PATH,
525 rundir);
526 DBG("Removing directory %s", path);
527 (void) rmdir(path);
528
529 /* ust consumerd 64 */
530 snprintf(path, PATH_MAX,
531 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
532 rundir);
533 DBG("Removing %s", path);
534 (void) unlink(path);
535
536 snprintf(path, PATH_MAX,
537 DEFAULT_USTCONSUMERD64_PATH,
538 rundir);
539 DBG("Removing directory %s", path);
540 (void) rmdir(path);
5461b305 541
dcbcae3e
MD
542 /*
543 * We do NOT rmdir rundir because there are other processes
544 * using it, for instance lttng-relayd, which can start in
545 * parallel with this teardown.
546 */
547
bd69add2 548 free(rundir);
5461b305 549
99bab54f 550 DBG("Cleaning up all sessions");
fac6795d 551
b5541356 552 /* Destroy session list mutex */
273ea72c
DG
553 if (session_list_ptr != NULL) {
554 pthread_mutex_destroy(&session_list_ptr->lock);
555
556 /* Cleanup ALL session */
54d01ffb
DG
557 cds_list_for_each_entry_safe(sess, stmp,
558 &session_list_ptr->head, list) {
2f77fc4b 559 cmd_destroy_session(sess, kernel_poll_pipe[1]);
273ea72c
DG
560 }
561 }
562
099e26bd 563 DBG("Closing all UST sockets");
56fff090 564 ust_app_clean_list();
7972aab2 565 buffer_reg_destroy_registries();
099e26bd 566
4fba7219
DG
567 if (is_root && !opt_no_kernel) {
568 DBG2("Closing kernel fd");
a4b35e07 569 if (kernel_tracer_fd >= 0) {
76d7553f
MD
570 ret = close(kernel_tracer_fd);
571 if (ret) {
572 PERROR("close");
573 }
a4b35e07 574 }
2f50c8a3 575 DBG("Unloading kernel modules");
096102bd 576 modprobe_remove_lttng_all();
2f50c8a3 577 }
2f77fc4b 578
e975f9f8
DG
579 close_consumer_sockets();
580
421cb601 581 /* <fun> */
f56a39af 582 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
421cb601
DG
583 "Matthew, BEET driven development works!%c[%dm",
584 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
585 /* </fun> */
fac6795d
DG
586}
587
e065084a 588/*
d063d709 589 * Send data on a unix socket using the liblttsessiondcomm API.
e065084a 590 *
d063d709 591 * Return lttcomm error code.
e065084a
DG
592 */
593static int send_unix_sock(int sock, void *buf, size_t len)
594{
595 /* Check valid length */
c617c0c6 596 if (len == 0) {
e065084a
DG
597 return -1;
598 }
599
600 return lttcomm_send_unix_sock(sock, buf, len);
601}
602
5461b305 603/*
d063d709 604 * Free memory of a command context structure.
5461b305 605 */
a2fb29a5 606static void clean_command_ctx(struct command_ctx **cmd_ctx)
5461b305 607{
a2fb29a5
DG
608 DBG("Clean command context structure");
609 if (*cmd_ctx) {
610 if ((*cmd_ctx)->llm) {
611 free((*cmd_ctx)->llm);
5461b305 612 }
a2fb29a5
DG
613 if ((*cmd_ctx)->lsm) {
614 free((*cmd_ctx)->lsm);
5461b305 615 }
a2fb29a5
DG
616 free(*cmd_ctx);
617 *cmd_ctx = NULL;
5461b305
DG
618 }
619}
620
fac6795d 621/*
0fdd1e2c 622 * Notify UST applications using the shm mmap futex.
fac6795d 623 */
0fdd1e2c 624static int notify_ust_apps(int active)
fac6795d 625{
0fdd1e2c 626 char *wait_shm_mmap;
fac6795d 627
0fdd1e2c 628 DBG("Notifying applications of session daemon state: %d", active);
e07ae692 629
0fdd1e2c
DG
630 /* See shm.c for this call implying mmap, shm and futex calls */
631 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
632 if (wait_shm_mmap == NULL) {
fac6795d
DG
633 goto error;
634 }
635
0fdd1e2c
DG
636 /* Wake waiting process */
637 futex_wait_update((int32_t *) wait_shm_mmap, active);
638
639 /* Apps notified successfully */
640 return 0;
fac6795d
DG
641
642error:
0fdd1e2c 643 return -1;
fac6795d
DG
644}
645
e065084a 646/*
d063d709
DG
647 * Setup the outgoing data buffer for the response (llm) by allocating the
648 * right amount of memory and copying the original information from the lsm
649 * structure.
ca95a216 650 *
d063d709 651 * Return total size of the buffer pointed by buf.
ca95a216 652 */
5461b305 653static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
ca95a216 654{
f3ed775e 655 int ret, buf_size;
ca95a216 656
f3ed775e 657 buf_size = size;
5461b305 658
ba7f0ae5 659 cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
5461b305 660 if (cmd_ctx->llm == NULL) {
76d7553f 661 PERROR("zmalloc");
5461b305 662 ret = -ENOMEM;
ca95a216
DG
663 goto error;
664 }
665
5461b305
DG
666 /* Copy common data */
667 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
9f19cc17 668 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
5461b305 669
5461b305
DG
670 cmd_ctx->llm->data_size = size;
671 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
672
ca95a216
DG
673 return buf_size;
674
675error:
676 return ret;
677}
678
7a485870 679/*
5eb91c98 680 * Update the kernel poll set of all channel fd available over all tracing
d063d709 681 * session. Add the wakeup pipe at the end of the set.
7a485870 682 */
5eb91c98 683static int update_kernel_poll(struct lttng_poll_event *events)
7a485870 684{
5eb91c98 685 int ret;
7a485870
DG
686 struct ltt_session *session;
687 struct ltt_kernel_channel *channel;
688
5eb91c98 689 DBG("Updating kernel poll set");
7a485870 690
54d01ffb 691 session_lock_list();
b5541356 692 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 693 session_lock(session);
7a485870 694 if (session->kernel_session == NULL) {
54d01ffb 695 session_unlock(session);
7a485870
DG
696 continue;
697 }
7a485870 698
54d01ffb
DG
699 cds_list_for_each_entry(channel,
700 &session->kernel_session->channel_list.head, list) {
5eb91c98
DG
701 /* Add channel fd to the kernel poll set */
702 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
703 if (ret < 0) {
54d01ffb 704 session_unlock(session);
5eb91c98
DG
705 goto error;
706 }
707 DBG("Channel fd %d added to kernel set", channel->fd);
7a485870 708 }
54d01ffb 709 session_unlock(session);
7a485870 710 }
54d01ffb 711 session_unlock_list();
7a485870 712
5eb91c98 713 return 0;
7a485870
DG
714
715error:
54d01ffb 716 session_unlock_list();
7a485870
DG
717 return -1;
718}
719
720/*
54d01ffb 721 * Find the channel fd from 'fd' over all tracing session. When found, check
d063d709 722 * for new channel stream and send those stream fds to the kernel consumer.
7a485870 723 *
d063d709 724 * Useful for CPU hotplug feature.
7a485870 725 */
2bdd86d4 726static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
7a485870
DG
727{
728 int ret = 0;
729 struct ltt_session *session;
173af62f 730 struct ltt_kernel_session *ksess;
7a485870
DG
731 struct ltt_kernel_channel *channel;
732
733 DBG("Updating kernel streams for channel fd %d", fd);
734
54d01ffb 735 session_lock_list();
b5541356 736 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 737 session_lock(session);
7a485870 738 if (session->kernel_session == NULL) {
54d01ffb 739 session_unlock(session);
7a485870
DG
740 continue;
741 }
173af62f 742 ksess = session->kernel_session;
d9800920 743
173af62f 744 cds_list_for_each_entry(channel, &ksess->channel_list.head, list) {
7a485870
DG
745 if (channel->fd == fd) {
746 DBG("Channel found, updating kernel streams");
747 ret = kernel_open_channel_stream(channel);
748 if (ret < 0) {
b3c750d2 749 goto error;
7a485870 750 }
5c786ded
JD
751 /* Update the stream global counter */
752 ksess->stream_count_global += ret;
d9800920 753
7a485870 754 /*
5eb91c98
DG
755 * Have we already sent fds to the consumer? If yes, it means
756 * that tracing is started so it is safe to send our updated
757 * stream fds.
7a485870 758 */
173af62f
DG
759 if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) {
760 struct lttng_ht_iter iter;
761 struct consumer_socket *socket;
762
e7fe706f 763 rcu_read_lock();
173af62f
DG
764 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
765 &iter.iter, socket, node.node) {
173af62f 766 pthread_mutex_lock(socket->lock);
f50f23d9 767 ret = kernel_consumer_send_channel_stream(socket,
2bba9e53
DG
768 channel, ksess,
769 session->output_traces ? 1 : 0);
173af62f
DG
770 pthread_mutex_unlock(socket->lock);
771 if (ret < 0) {
e7fe706f 772 rcu_read_unlock();
173af62f
DG
773 goto error;
774 }
7a485870 775 }
e7fe706f 776 rcu_read_unlock();
7a485870 777 }
b3c750d2 778 goto error;
7a485870
DG
779 }
780 }
54d01ffb 781 session_unlock(session);
7a485870 782 }
54d01ffb 783 session_unlock_list();
b3c750d2 784 return ret;
7a485870 785
b3c750d2 786error:
54d01ffb
DG
787 session_unlock(session);
788 session_unlock_list();
7a485870
DG
789 return ret;
790}
791
487cf67c 792/*
ffe60014
DG
793 * For each tracing session, update newly registered apps. The session list
794 * lock MUST be acquired before calling this.
487cf67c
DG
795 */
796static void update_ust_app(int app_sock)
797{
798 struct ltt_session *sess, *stmp;
799
fdadac08
DG
800 /* Consumer is in an ERROR state. Stop any application update. */
801 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
802 /* Stop the update process since the consumer is dead. */
803 return;
804 }
805
487cf67c
DG
806 /* For all tracing session(s) */
807 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
4ee14516 808 session_lock(sess);
421cb601
DG
809 if (sess->ust_session) {
810 ust_app_global_update(sess->ust_session, app_sock);
811 }
4ee14516 812 session_unlock(sess);
487cf67c
DG
813 }
814}
815
7a485870 816/*
d063d709 817 * This thread manage event coming from the kernel.
7a485870 818 *
d063d709
DG
819 * Features supported in this thread:
820 * -) CPU Hotplug
7a485870
DG
821 */
822static void *thread_manage_kernel(void *data)
823{
139ac872 824 int ret, i, pollfd, update_poll_flag = 1, err = -1;
5eb91c98 825 uint32_t revents, nb_fd;
7a485870 826 char tmp;
5eb91c98 827 struct lttng_poll_event events;
7a485870 828
6993eeb3 829 DBG("[thread] Thread manage kernel started");
7a485870 830
6c71277b 831 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
927ca06a 832
d5d63bf1
DG
833 /*
834 * This first step of the while is to clean this structure which could free
6d737ce4 835 * non NULL pointers so initialize it before the loop.
d5d63bf1 836 */
6d737ce4 837 lttng_poll_init(&events);
d5d63bf1 838
71ddb4ed 839 if (testpoint(sessiond_thread_manage_kernel)) {
6993eeb3
CB
840 goto error_testpoint;
841 }
8ac94142 842
840cb59c 843 health_code_update();
44a5e5eb 844
71ddb4ed 845 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
d21b0d71 846 goto error_testpoint;
6993eeb3
CB
847 }
848
7a485870 849 while (1) {
840cb59c 850 health_code_update();
44a5e5eb 851
7a485870 852 if (update_poll_flag == 1) {
d21b0d71
DG
853 /* Clean events object. We are about to populate it again. */
854 lttng_poll_clean(&events);
855
d0b96690 856 ret = sessiond_set_thread_pollset(&events, 2);
d21b0d71
DG
857 if (ret < 0) {
858 goto error_poll_create;
859 }
860
861 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
862 if (ret < 0) {
863 goto error;
864 }
5f822d0a 865
d21b0d71 866 /* This will add the available kernel channel if any. */
5eb91c98
DG
867 ret = update_kernel_poll(&events);
868 if (ret < 0) {
7a485870
DG
869 goto error;
870 }
871 update_poll_flag = 0;
872 }
873
d21b0d71 874 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events));
7a485870
DG
875
876 /* Poll infinite value of time */
88f2b785 877 restart:
a78af745 878 health_poll_entry();
5eb91c98 879 ret = lttng_poll_wait(&events, -1);
a78af745 880 health_poll_exit();
7a485870 881 if (ret < 0) {
88f2b785
MD
882 /*
883 * Restart interrupted system call.
884 */
885 if (errno == EINTR) {
886 goto restart;
887 }
7a485870
DG
888 goto error;
889 } else if (ret == 0) {
890 /* Should not happen since timeout is infinite */
85611738
DG
891 ERR("Return value of poll is 0 with an infinite timeout.\n"
892 "This should not have happened! Continuing...");
7a485870
DG
893 continue;
894 }
895
0d9c5d77
DG
896 nb_fd = ret;
897
5eb91c98
DG
898 for (i = 0; i < nb_fd; i++) {
899 /* Fetch once the poll data */
900 revents = LTTNG_POLL_GETEV(&events, i);
901 pollfd = LTTNG_POLL_GETFD(&events, i);
7a485870 902
840cb59c 903 health_code_update();
44a5e5eb 904
5eb91c98 905 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 906 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 907 if (ret) {
139ac872
MD
908 err = 0;
909 goto exit;
5eb91c98 910 }
7a485870 911
5eb91c98
DG
912 /* Check for data on kernel pipe */
913 if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
6cd525e8
MD
914 (void) lttng_read(kernel_poll_pipe[0],
915 &tmp, 1);
f921c78f
DG
916 /*
917 * Ret value is useless here, if this pipe gets any actions an
918 * update is required anyway.
919 */
5eb91c98
DG
920 update_poll_flag = 1;
921 continue;
922 } else {
923 /*
924 * New CPU detected by the kernel. Adding kernel stream to
925 * kernel session and updating the kernel consumer
926 */
927 if (revents & LPOLLIN) {
2bdd86d4 928 ret = update_kernel_stream(&kconsumer_data, pollfd);
5eb91c98
DG
929 if (ret < 0) {
930 continue;
931 }
932 break;
933 /*
934 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
935 * and unregister kernel stream at this point.
936 */
7a485870 937 }
7a485870
DG
938 }
939 }
940 }
941
139ac872 942exit:
7a485870 943error:
5eb91c98 944 lttng_poll_clean(&events);
76d7553f 945error_poll_create:
6993eeb3 946error_testpoint:
6620da75
DG
947 utils_close_pipe(kernel_poll_pipe);
948 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
139ac872 949 if (err) {
840cb59c 950 health_error();
139ac872 951 ERR("Health error occurred in %s", __func__);
6620da75
DG
952 WARN("Kernel thread died unexpectedly. "
953 "Kernel tracing can continue but CPU hotplug is disabled.");
139ac872 954 }
8782cc74 955 health_unregister(health_sessiond);
76d7553f 956 DBG("Kernel thread dying");
7a485870
DG
957 return NULL;
958}
959
a23ec3a7
DG
960/*
961 * Signal pthread condition of the consumer data that the thread.
962 */
963static void signal_consumer_condition(struct consumer_data *data, int state)
964{
965 pthread_mutex_lock(&data->cond_mutex);
966
967 /*
968 * The state is set before signaling. It can be any value, it's the waiter
969 * job to correctly interpret this condition variable associated to the
970 * consumer pthread_cond.
971 *
972 * A value of 0 means that the corresponding thread of the consumer data
973 * was not started. 1 indicates that the thread has started and is ready
974 * for action. A negative value means that there was an error during the
975 * thread bootstrap.
976 */
977 data->consumer_thread_is_ready = state;
978 (void) pthread_cond_signal(&data->cond);
979
980 pthread_mutex_unlock(&data->cond_mutex);
981}
982
1d4b027a 983/*
3bd1e081 984 * This thread manage the consumer error sent back to the session daemon.
1d4b027a 985 */
3bd1e081 986static void *thread_manage_consumer(void *data)
1d4b027a 987{
139ac872 988 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98 989 uint32_t revents, nb_fd;
1d4b027a 990 enum lttcomm_return_code code;
5eb91c98 991 struct lttng_poll_event events;
3bd1e081 992 struct consumer_data *consumer_data = data;
1d4b027a 993
3bd1e081 994 DBG("[thread] Manage consumer started");
1d4b027a 995
6c71277b 996 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
927ca06a 997
855060f8 998 health_code_update();
9449cc75 999
5eb91c98 1000 /*
331744e3
JD
1001 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1002 * metadata_sock. Nothing more will be added to this poll set.
5eb91c98 1003 */
331744e3 1004 ret = sessiond_set_thread_pollset(&events, 3);
5eb91c98 1005 if (ret < 0) {
76d7553f 1006 goto error_poll;
5eb91c98 1007 }
273ea72c 1008
edb8b045
DG
1009 /*
1010 * The error socket here is already in a listening state which was done
1011 * just before spawning this thread to avoid a race between the consumer
1012 * daemon exec trying to connect and the listen() call.
1013 */
3bd1e081 1014 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
5eb91c98
DG
1015 if (ret < 0) {
1016 goto error;
1017 }
1018
840cb59c 1019 health_code_update();
44a5e5eb 1020
331744e3 1021 /* Infinite blocking call, waiting for transmission */
88f2b785 1022restart:
a78af745 1023 health_poll_entry();
8ac94142 1024
71ddb4ed 1025 if (testpoint(sessiond_thread_manage_consumer)) {
6993eeb3
CB
1026 goto error;
1027 }
8ac94142 1028
5eb91c98 1029 ret = lttng_poll_wait(&events, -1);
a78af745 1030 health_poll_exit();
273ea72c 1031 if (ret < 0) {
88f2b785
MD
1032 /*
1033 * Restart interrupted system call.
1034 */
1035 if (errno == EINTR) {
1036 goto restart;
1037 }
273ea72c
DG
1038 goto error;
1039 }
1040
0d9c5d77
DG
1041 nb_fd = ret;
1042
5eb91c98
DG
1043 for (i = 0; i < nb_fd; i++) {
1044 /* Fetch once the poll data */
1045 revents = LTTNG_POLL_GETEV(&events, i);
1046 pollfd = LTTNG_POLL_GETFD(&events, i);
1047
840cb59c 1048 health_code_update();
44a5e5eb 1049
5eb91c98 1050 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1051 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1052 if (ret) {
139ac872
MD
1053 err = 0;
1054 goto exit;
5eb91c98
DG
1055 }
1056
1057 /* Event on the registration socket */
3bd1e081 1058 if (pollfd == consumer_data->err_sock) {
5eb91c98 1059 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3bd1e081 1060 ERR("consumer err socket poll error");
5eb91c98
DG
1061 goto error;
1062 }
1063 }
273ea72c
DG
1064 }
1065
3bd1e081 1066 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1d4b027a
DG
1067 if (sock < 0) {
1068 goto error;
1069 }
1070
b662582b
DG
1071 /*
1072 * Set the CLOEXEC flag. Return code is useless because either way, the
1073 * show must go on.
1074 */
1075 (void) utils_set_fd_cloexec(sock);
1076
840cb59c 1077 health_code_update();
44a5e5eb 1078
3bd1e081 1079 DBG2("Receiving code from consumer err_sock");
ee0b0061 1080
712ea556 1081 /* Getting status code from kconsumerd */
54d01ffb
DG
1082 ret = lttcomm_recv_unix_sock(sock, &code,
1083 sizeof(enum lttcomm_return_code));
1d4b027a
DG
1084 if (ret <= 0) {
1085 goto error;
1086 }
1087
840cb59c 1088 health_code_update();
f73fabfd 1089 if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
331744e3 1090 /* Connect both socket, command and metadata. */
3bd1e081
MD
1091 consumer_data->cmd_sock =
1092 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
4ce514c4 1093 consumer_data->metadata_fd =
331744e3 1094 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
92db7cdc
DG
1095 if (consumer_data->cmd_sock < 0
1096 || consumer_data->metadata_fd < 0) {
331744e3 1097 PERROR("consumer connect cmd socket");
a23ec3a7
DG
1098 /* On error, signal condition and quit. */
1099 signal_consumer_condition(consumer_data, -1);
1d4b027a
DG
1100 goto error;
1101 }
9363801e 1102 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
331744e3
JD
1103 /* Create metadata socket lock. */
1104 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
1105 if (consumer_data->metadata_sock.lock == NULL) {
1106 PERROR("zmalloc pthread mutex");
1107 ret = -1;
1108 goto error;
1109 }
1110 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
1111
a23ec3a7 1112 signal_consumer_condition(consumer_data, 1);
331744e3
JD
1113 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
1114 DBG("Consumer metadata socket ready (fd: %d)",
4ce514c4 1115 consumer_data->metadata_fd);
1d4b027a 1116 } else {
3bd1e081 1117 ERR("consumer error when waiting for SOCK_READY : %s",
1d4b027a
DG
1118 lttcomm_get_readable_code(-code));
1119 goto error;
1120 }
1121
331744e3 1122 /* Remove the consumerd error sock since we've established a connexion */
3bd1e081 1123 ret = lttng_poll_del(&events, consumer_data->err_sock);
72079cae 1124 if (ret < 0) {
72079cae
DG
1125 goto error;
1126 }
1127
331744e3 1128 /* Add new accepted error socket. */
5eb91c98
DG
1129 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1130 if (ret < 0) {
72079cae 1131 goto error;
5eb91c98
DG
1132 }
1133
331744e3 1134 /* Add metadata socket that is successfully connected. */
4ce514c4 1135 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
331744e3
JD
1136 LPOLLIN | LPOLLRDHUP);
1137 if (ret < 0) {
1138 goto error;
1139 }
1140
840cb59c 1141 health_code_update();
44a5e5eb 1142
331744e3 1143 /* Infinite blocking call, waiting for transmission */
88f2b785 1144restart_poll:
331744e3
JD
1145 while (1) {
1146 health_poll_entry();
1147 ret = lttng_poll_wait(&events, -1);
1148 health_poll_exit();
1149 if (ret < 0) {
1150 /*
1151 * Restart interrupted system call.
1152 */
1153 if (errno == EINTR) {
1154 goto restart_poll;
1155 }
1156 goto error;
88f2b785 1157 }
72079cae 1158
331744e3 1159 nb_fd = ret;
0d9c5d77 1160
331744e3
JD
1161 for (i = 0; i < nb_fd; i++) {
1162 /* Fetch once the poll data */
1163 revents = LTTNG_POLL_GETEV(&events, i);
1164 pollfd = LTTNG_POLL_GETFD(&events, i);
5eb91c98 1165
331744e3 1166 health_code_update();
44a5e5eb 1167
331744e3
JD
1168 /* Thread quit pipe has been closed. Killing thread. */
1169 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1170 if (ret) {
1171 err = 0;
1172 goto exit;
1173 }
5eb91c98 1174
331744e3
JD
1175 if (pollfd == sock) {
1176 /* Event on the consumerd socket */
1177 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1178 ERR("consumer err socket second poll error");
1179 goto error;
1180 }
1181 health_code_update();
1182 /* Wait for any kconsumerd error */
1183 ret = lttcomm_recv_unix_sock(sock, &code,
1184 sizeof(enum lttcomm_return_code));
1185 if (ret <= 0) {
1186 ERR("consumer closed the command socket");
1187 goto error;
1188 }
1189
1190 ERR("consumer return code : %s",
1191 lttcomm_get_readable_code(-code));
1192
1193 goto exit;
4ce514c4 1194 } else if (pollfd == consumer_data->metadata_fd) {
331744e3
JD
1195 /* UST metadata requests */
1196 ret = ust_consumer_metadata_request(
1197 &consumer_data->metadata_sock);
1198 if (ret < 0) {
1199 ERR("Handling metadata request");
1200 goto error;
1201 }
1202 break;
1203 } else {
1204 ERR("Unknown pollfd");
5eb91c98
DG
1205 goto error;
1206 }
1207 }
331744e3 1208 health_code_update();
5eb91c98
DG
1209 }
1210
139ac872 1211exit:
1d4b027a 1212error:
fdadac08
DG
1213 /*
1214 * We lock here because we are about to close the sockets and some other
92db7cdc
DG
1215 * thread might be using them so get exclusive access which will abort all
1216 * other consumer command by other threads.
fdadac08
DG
1217 */
1218 pthread_mutex_lock(&consumer_data->lock);
1219
5c827ce0
DG
1220 /* Immediately set the consumerd state to stopped */
1221 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1222 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1223 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1224 consumer_data->type == LTTNG_CONSUMER32_UST) {
1225 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1226 } else {
1227 /* Code flow error... */
1228 assert(0);
1229 }
1230
76d7553f
MD
1231 if (consumer_data->err_sock >= 0) {
1232 ret = close(consumer_data->err_sock);
1233 if (ret) {
1234 PERROR("close");
1235 }
a76cbd9f 1236 consumer_data->err_sock = -1;
76d7553f
MD
1237 }
1238 if (consumer_data->cmd_sock >= 0) {
1239 ret = close(consumer_data->cmd_sock);
1240 if (ret) {
1241 PERROR("close");
1242 }
a76cbd9f 1243 consumer_data->cmd_sock = -1;
76d7553f 1244 }
24a54a9b
SS
1245 if (consumer_data->metadata_sock.fd_ptr &&
1246 *consumer_data->metadata_sock.fd_ptr >= 0) {
9363801e 1247 ret = close(*consumer_data->metadata_sock.fd_ptr);
331744e3
JD
1248 if (ret) {
1249 PERROR("close");
1250 }
1251 }
76d7553f
MD
1252 if (sock >= 0) {
1253 ret = close(sock);
1254 if (ret) {
1255 PERROR("close");
1256 }
1257 }
273ea72c 1258
3bd1e081
MD
1259 unlink(consumer_data->err_unix_sock_path);
1260 unlink(consumer_data->cmd_unix_sock_path);
1261 consumer_data->pid = 0;
fdadac08 1262 pthread_mutex_unlock(&consumer_data->lock);
92db7cdc 1263
fdadac08 1264 /* Cleanup metadata socket mutex. */
24a54a9b
SS
1265 if (consumer_data->metadata_sock.lock) {
1266 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1267 free(consumer_data->metadata_sock.lock);
1268 }
5eb91c98 1269 lttng_poll_clean(&events);
76d7553f 1270error_poll:
139ac872 1271 if (err) {
840cb59c 1272 health_error();
139ac872
MD
1273 ERR("Health error occurred in %s", __func__);
1274 }
8782cc74 1275 health_unregister(health_sessiond);
76d7553f 1276 DBG("consumer thread cleanup completed");
0177d773 1277
5eb91c98 1278 return NULL;
099e26bd
DG
1279}
1280
099e26bd
DG
1281/*
1282 * This thread manage application communication.
1d4b027a
DG
1283 */
1284static void *thread_manage_apps(void *data)
099e26bd 1285{
139ac872 1286 int i, ret, pollfd, err = -1;
6cd525e8 1287 ssize_t size_ret;
5eb91c98 1288 uint32_t revents, nb_fd;
5eb91c98 1289 struct lttng_poll_event events;
099e26bd
DG
1290
1291 DBG("[thread] Manage application started");
1292
f6a9efaa
DG
1293 rcu_register_thread();
1294 rcu_thread_online();
1295
6c71277b 1296 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
927ca06a 1297
71ddb4ed 1298 if (testpoint(sessiond_thread_manage_apps)) {
6993eeb3
CB
1299 goto error_testpoint;
1300 }
1301
840cb59c 1302 health_code_update();
44a5e5eb 1303
d0b96690 1304 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 1305 if (ret < 0) {
76d7553f 1306 goto error_poll_create;
5eb91c98 1307 }
099e26bd 1308
5eb91c98
DG
1309 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1310 if (ret < 0) {
1311 goto error;
1312 }
099e26bd 1313
71ddb4ed 1314 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
6993eeb3
CB
1315 goto error;
1316 }
8ac94142 1317
840cb59c 1318 health_code_update();
44a5e5eb 1319
5eb91c98 1320 while (1) {
d21b0d71 1321 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events));
099e26bd
DG
1322
1323 /* Inifinite blocking call, waiting for transmission */
88f2b785 1324 restart:
a78af745 1325 health_poll_entry();
5eb91c98 1326 ret = lttng_poll_wait(&events, -1);
a78af745 1327 health_poll_exit();
099e26bd 1328 if (ret < 0) {
88f2b785
MD
1329 /*
1330 * Restart interrupted system call.
1331 */
1332 if (errno == EINTR) {
1333 goto restart;
1334 }
099e26bd
DG
1335 goto error;
1336 }
1337
0d9c5d77
DG
1338 nb_fd = ret;
1339
5eb91c98
DG
1340 for (i = 0; i < nb_fd; i++) {
1341 /* Fetch once the poll data */
1342 revents = LTTNG_POLL_GETEV(&events, i);
1343 pollfd = LTTNG_POLL_GETFD(&events, i);
1344
840cb59c 1345 health_code_update();
44a5e5eb 1346
5eb91c98 1347 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1348 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1349 if (ret) {
139ac872
MD
1350 err = 0;
1351 goto exit;
5eb91c98 1352 }
099e26bd 1353
5eb91c98
DG
1354 /* Inspect the apps cmd pipe */
1355 if (pollfd == apps_cmd_pipe[0]) {
1356 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1357 ERR("Apps command pipe error");
0177d773 1358 goto error;
5eb91c98 1359 } else if (revents & LPOLLIN) {
d0b96690
DG
1360 int sock;
1361
5eb91c98 1362 /* Empty pipe */
6cd525e8
MD
1363 size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
1364 if (size_ret < sizeof(sock)) {
76d7553f 1365 PERROR("read apps cmd pipe");
5eb91c98
DG
1366 goto error;
1367 }
099e26bd 1368
840cb59c 1369 health_code_update();
44a5e5eb 1370
ffe60014 1371 /*
d0b96690
DG
1372 * We only monitor the error events of the socket. This
1373 * thread does not handle any incoming data from UST
1374 * (POLLIN).
ffe60014 1375 */
d0b96690
DG
1376 ret = lttng_poll_add(&events, sock,
1377 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1378 if (ret < 0) {
5eb91c98 1379 goto error;
e0c7ec2b 1380 }
acc7b41b 1381
d0b96690 1382 DBG("Apps with sock %d added to poll set", sock);
0177d773 1383 }
5eb91c98
DG
1384 } else {
1385 /*
54d01ffb
DG
1386 * At this point, we know that a registered application made
1387 * the event at poll_wait.
5eb91c98
DG
1388 */
1389 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1390 /* Removing from the poll set */
1391 ret = lttng_poll_del(&events, pollfd);
1392 if (ret < 0) {
1393 goto error;
1394 }
099e26bd 1395
b9d9b220 1396 /* Socket closed on remote end. */
56fff090 1397 ust_app_unregister(pollfd);
5eb91c98 1398 }
099e26bd 1399 }
44a5e5eb 1400
840cb59c 1401 health_code_update();
099e26bd 1402 }
099e26bd
DG
1403 }
1404
139ac872 1405exit:
099e26bd 1406error:
5eb91c98 1407 lttng_poll_clean(&events);
76d7553f 1408error_poll_create:
6993eeb3 1409error_testpoint:
6620da75
DG
1410 utils_close_pipe(apps_cmd_pipe);
1411 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1412
1413 /*
1414 * We don't clean the UST app hash table here since already registered
1415 * applications can still be controlled so let them be until the session
1416 * daemon dies or the applications stop.
1417 */
1418
139ac872 1419 if (err) {
840cb59c 1420 health_error();
139ac872
MD
1421 ERR("Health error occurred in %s", __func__);
1422 }
8782cc74 1423 health_unregister(health_sessiond);
76d7553f 1424 DBG("Application communication apps thread cleanup complete");
f6a9efaa
DG
1425 rcu_thread_offline();
1426 rcu_unregister_thread();
099e26bd
DG
1427 return NULL;
1428}
1429
d0b96690 1430/*
d88aee68
DG
1431 * Send a socket to a thread This is called from the dispatch UST registration
1432 * thread once all sockets are set for the application.
d0b96690 1433 *
b85dc84c
DG
1434 * The sock value can be invalid, we don't really care, the thread will handle
1435 * it and make the necessary cleanup if so.
1436 *
d0b96690
DG
1437 * On success, return 0 else a negative value being the errno message of the
1438 * write().
1439 */
d88aee68 1440static int send_socket_to_thread(int fd, int sock)
d0b96690 1441{
6cd525e8 1442 ssize_t ret;
d0b96690 1443
b85dc84c
DG
1444 /*
1445 * It's possible that the FD is set as invalid with -1 concurrently just
1446 * before calling this function being a shutdown state of the thread.
1447 */
1448 if (fd < 0) {
1449 ret = -EBADF;
1450 goto error;
1451 }
d0b96690 1452
6cd525e8
MD
1453 ret = lttng_write(fd, &sock, sizeof(sock));
1454 if (ret < sizeof(sock)) {
d88aee68 1455 PERROR("write apps pipe %d", fd);
d0b96690
DG
1456 if (ret < 0) {
1457 ret = -errno;
1458 }
1459 goto error;
1460 }
1461
1462 /* All good. Don't send back the write positive ret value. */
1463 ret = 0;
1464error:
6cd525e8 1465 return (int) ret;
d0b96690
DG
1466}
1467
f45e313d
DG
1468/*
1469 * Sanitize the wait queue of the dispatch registration thread meaning removing
1470 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1471 * notify socket is never received.
1472 */
1473static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1474{
1475 int ret, nb_fd = 0, i;
1476 unsigned int fd_added = 0;
1477 struct lttng_poll_event events;
1478 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1479
1480 assert(wait_queue);
1481
1482 lttng_poll_init(&events);
1483
1484 /* Just skip everything for an empty queue. */
1485 if (!wait_queue->count) {
1486 goto end;
1487 }
1488
1489 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1490 if (ret < 0) {
1491 goto error_create;
1492 }
1493
1494 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1495 &wait_queue->head, head) {
1496 assert(wait_node->app);
1497 ret = lttng_poll_add(&events, wait_node->app->sock,
1498 LPOLLHUP | LPOLLERR);
1499 if (ret < 0) {
1500 goto error;
1501 }
1502
1503 fd_added = 1;
1504 }
1505
1506 if (!fd_added) {
1507 goto end;
1508 }
1509
1510 /*
1511 * Poll but don't block so we can quickly identify the faulty events and
1512 * clean them afterwards from the wait queue.
1513 */
1514 ret = lttng_poll_wait(&events, 0);
1515 if (ret < 0) {
1516 goto error;
1517 }
1518 nb_fd = ret;
1519
1520 for (i = 0; i < nb_fd; i++) {
1521 /* Get faulty FD. */
1522 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1523 int pollfd = LTTNG_POLL_GETFD(&events, i);
1524
1525 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1526 &wait_queue->head, head) {
1527 if (pollfd == wait_node->app->sock &&
1528 (revents & (LPOLLHUP | LPOLLERR))) {
1529 cds_list_del(&wait_node->head);
1530 wait_queue->count--;
1531 ust_app_destroy(wait_node->app);
1532 free(wait_node);
1533 break;
1534 }
1535 }
1536 }
1537
1538 if (nb_fd > 0) {
1539 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1540 }
1541
1542end:
1543 lttng_poll_clean(&events);
1544 return;
1545
1546error:
1547 lttng_poll_clean(&events);
1548error_create:
1549 ERR("Unable to sanitize wait queue");
1550 return;
1551}
1552
099e26bd
DG
1553/*
1554 * Dispatch request from the registration threads to the application
1555 * communication thread.
1556 */
1557static void *thread_dispatch_ust_registration(void *data)
1558{
12e2b881 1559 int ret, err = -1;
099e26bd
DG
1560 struct cds_wfq_node *node;
1561 struct ust_command *ust_cmd = NULL;
f45e313d
DG
1562 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1563 struct ust_reg_wait_queue wait_queue = {
1564 .count = 0,
1565 };
d0b96690 1566
6c71277b 1567 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
12e2b881 1568
a09bf708
MD
1569 if (testpoint(sessiond_thread_app_reg_dispatch)) {
1570 goto error_testpoint;
1571 }
1572
12e2b881
MD
1573 health_code_update();
1574
f45e313d 1575 CDS_INIT_LIST_HEAD(&wait_queue.head);
099e26bd
DG
1576
1577 DBG("[thread] Dispatch UST command started");
1578
26c9d55e 1579 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
12e2b881
MD
1580 health_code_update();
1581
099e26bd
DG
1582 /* Atomically prepare the queue futex */
1583 futex_nto1_prepare(&ust_cmd_queue.futex);
1584
1585 do {
d0b96690 1586 struct ust_app *app = NULL;
7972aab2 1587 ust_cmd = NULL;
d0b96690 1588
f45e313d
DG
1589 /*
1590 * Make sure we don't have node(s) that have hung up before receiving
1591 * the notify socket. This is to clean the list in order to avoid
1592 * memory leaks from notify socket that are never seen.
1593 */
1594 sanitize_wait_queue(&wait_queue);
1595
12e2b881 1596 health_code_update();
099e26bd
DG
1597 /* Dequeue command for registration */
1598 node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
1599 if (node == NULL) {
00a17c97 1600 DBG("Woken up but nothing in the UST command queue");
099e26bd
DG
1601 /* Continue thread execution */
1602 break;
1603 }
1604
1605 ust_cmd = caa_container_of(node, struct ust_command, node);
1606
2f50c8a3
DG
1607 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1608 " gid:%d sock:%d name:%s (version %d.%d)",
1609 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1610 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1611 ust_cmd->sock, ust_cmd->reg_msg.name,
1612 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
d0b96690
DG
1613
1614 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1615 wait_node = zmalloc(sizeof(*wait_node));
1616 if (!wait_node) {
1617 PERROR("zmalloc wait_node dispatch");
020d7f60
DG
1618 ret = close(ust_cmd->sock);
1619 if (ret < 0) {
1620 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1621 }
51dec90d 1622 lttng_fd_put(LTTNG_FD_APPS, 1);
7972aab2 1623 free(ust_cmd);
d0b96690
DG
1624 goto error;
1625 }
1626 CDS_INIT_LIST_HEAD(&wait_node->head);
1627
1628 /* Create application object if socket is CMD. */
1629 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1630 ust_cmd->sock);
1631 if (!wait_node->app) {
1632 ret = close(ust_cmd->sock);
1633 if (ret < 0) {
1634 PERROR("close ust sock dispatch %d", ust_cmd->sock);
6620da75 1635 }
51dec90d 1636 lttng_fd_put(LTTNG_FD_APPS, 1);
d88aee68 1637 free(wait_node);
7972aab2 1638 free(ust_cmd);
d0b96690
DG
1639 continue;
1640 }
1641 /*
1642 * Add application to the wait queue so we can set the notify
1643 * socket before putting this object in the global ht.
1644 */
f45e313d
DG
1645 cds_list_add(&wait_node->head, &wait_queue.head);
1646 wait_queue.count++;
d0b96690 1647
7972aab2 1648 free(ust_cmd);
d0b96690
DG
1649 /*
1650 * We have to continue here since we don't have the notify
1651 * socket and the application MUST be added to the hash table
1652 * only at that moment.
1653 */
1654 continue;
1655 } else {
1656 /*
1657 * Look for the application in the local wait queue and set the
1658 * notify socket if found.
1659 */
d88aee68 1660 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
f45e313d 1661 &wait_queue.head, head) {
12e2b881 1662 health_code_update();
d0b96690
DG
1663 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1664 wait_node->app->notify_sock = ust_cmd->sock;
1665 cds_list_del(&wait_node->head);
f45e313d 1666 wait_queue.count--;
d0b96690
DG
1667 app = wait_node->app;
1668 free(wait_node);
1669 DBG3("UST app notify socket %d is set", ust_cmd->sock);
1670 break;
1671 }
1672 }
020d7f60
DG
1673
1674 /*
1675 * With no application at this stage the received socket is
1676 * basically useless so close it before we free the cmd data
1677 * structure for good.
1678 */
1679 if (!app) {
1680 ret = close(ust_cmd->sock);
1681 if (ret < 0) {
1682 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1683 }
51dec90d 1684 lttng_fd_put(LTTNG_FD_APPS, 1);
020d7f60 1685 }
7972aab2 1686 free(ust_cmd);
d0b96690
DG
1687 }
1688
1689 if (app) {
d0b96690
DG
1690 /*
1691 * @session_lock_list
1692 *
1693 * Lock the global session list so from the register up to the
1694 * registration done message, no thread can see the application
1695 * and change its state.
1696 */
1697 session_lock_list();
1698 rcu_read_lock();
d88aee68 1699
d0b96690
DG
1700 /*
1701 * Add application to the global hash table. This needs to be
1702 * done before the update to the UST registry can locate the
1703 * application.
1704 */
1705 ust_app_add(app);
d88aee68
DG
1706
1707 /* Set app version. This call will print an error if needed. */
1708 (void) ust_app_version(app);
1709
1710 /* Send notify socket through the notify pipe. */
1711 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
1712 app->notify_sock);
1713 if (ret < 0) {
1714 rcu_read_unlock();
1715 session_unlock_list();
b85dc84c
DG
1716 /*
1717 * No notify thread, stop the UST tracing. However, this is
1718 * not an internal error of the this thread thus setting
1719 * the health error code to a normal exit.
1720 */
1721 err = 0;
d88aee68 1722 goto error;
6620da75 1723 }
d88aee68 1724
d0b96690
DG
1725 /*
1726 * Update newly registered application with the tracing
1727 * registry info already enabled information.
1728 */
1729 update_ust_app(app->sock);
d88aee68
DG
1730
1731 /*
1732 * Don't care about return value. Let the manage apps threads
1733 * handle app unregistration upon socket close.
1734 */
1735 (void) ust_app_register_done(app->sock);
1736
1737 /*
1738 * Even if the application socket has been closed, send the app
1739 * to the thread and unregistration will take place at that
1740 * place.
1741 */
1742 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
d0b96690 1743 if (ret < 0) {
d88aee68
DG
1744 rcu_read_unlock();
1745 session_unlock_list();
b85dc84c
DG
1746 /*
1747 * No apps. thread, stop the UST tracing. However, this is
1748 * not an internal error of the this thread thus setting
1749 * the health error code to a normal exit.
1750 */
1751 err = 0;
d88aee68 1752 goto error;
d0b96690 1753 }
d88aee68 1754
d0b96690
DG
1755 rcu_read_unlock();
1756 session_unlock_list();
099e26bd 1757 }
099e26bd
DG
1758 } while (node != NULL);
1759
12e2b881 1760 health_poll_entry();
099e26bd
DG
1761 /* Futex wait on queue. Blocking call on futex() */
1762 futex_nto1_wait(&ust_cmd_queue.futex);
12e2b881 1763 health_poll_exit();
099e26bd 1764 }
12e2b881
MD
1765 /* Normal exit, no error */
1766 err = 0;
099e26bd
DG
1767
1768error:
d88aee68
DG
1769 /* Clean up wait queue. */
1770 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
f45e313d 1771 &wait_queue.head, head) {
d88aee68 1772 cds_list_del(&wait_node->head);
f45e313d 1773 wait_queue.count--;
d88aee68
DG
1774 free(wait_node);
1775 }
1776
a09bf708 1777error_testpoint:
099e26bd 1778 DBG("Dispatch thread dying");
12e2b881
MD
1779 if (err) {
1780 health_error();
1781 ERR("Health error occurred in %s", __func__);
1782 }
8782cc74 1783 health_unregister(health_sessiond);
099e26bd
DG
1784 return NULL;
1785}
1786
1787/*
1788 * This thread manage application registration.
1789 */
1790static void *thread_registration_apps(void *data)
1d4b027a 1791{
139ac872 1792 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98
DG
1793 uint32_t revents, nb_fd;
1794 struct lttng_poll_event events;
099e26bd
DG
1795 /*
1796 * Get allocated in this thread, enqueued to a global queue, dequeued and
1797 * freed in the manage apps thread.
1798 */
1799 struct ust_command *ust_cmd = NULL;
1d4b027a 1800
099e26bd 1801 DBG("[thread] Manage application registration started");
1d4b027a 1802
6c71277b 1803 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
927ca06a 1804
71ddb4ed 1805 if (testpoint(sessiond_thread_registration_apps)) {
6993eeb3
CB
1806 goto error_testpoint;
1807 }
8ac94142 1808
1d4b027a
DG
1809 ret = lttcomm_listen_unix_sock(apps_sock);
1810 if (ret < 0) {
76d7553f 1811 goto error_listen;
1d4b027a
DG
1812 }
1813
5eb91c98
DG
1814 /*
1815 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1816 * more will be added to this poll set.
1817 */
d0b96690 1818 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 1819 if (ret < 0) {
76d7553f 1820 goto error_create_poll;
5eb91c98 1821 }
273ea72c 1822
5eb91c98
DG
1823 /* Add the application registration socket */
1824 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1825 if (ret < 0) {
76d7553f 1826 goto error_poll_add;
5eb91c98 1827 }
273ea72c 1828
1d4b027a 1829 /* Notify all applications to register */
0fdd1e2c
DG
1830 ret = notify_ust_apps(1);
1831 if (ret < 0) {
1832 ERR("Failed to notify applications or create the wait shared memory.\n"
54d01ffb
DG
1833 "Execution continues but there might be problem for already\n"
1834 "running applications that wishes to register.");
0fdd1e2c 1835 }
1d4b027a
DG
1836
1837 while (1) {
1838 DBG("Accepting application registration");
273ea72c
DG
1839
1840 /* Inifinite blocking call, waiting for transmission */
88f2b785 1841 restart:
a78af745 1842 health_poll_entry();
5eb91c98 1843 ret = lttng_poll_wait(&events, -1);
a78af745 1844 health_poll_exit();
273ea72c 1845 if (ret < 0) {
88f2b785
MD
1846 /*
1847 * Restart interrupted system call.
1848 */
1849 if (errno == EINTR) {
1850 goto restart;
1851 }
273ea72c
DG
1852 goto error;
1853 }
1854
0d9c5d77
DG
1855 nb_fd = ret;
1856
5eb91c98 1857 for (i = 0; i < nb_fd; i++) {
840cb59c 1858 health_code_update();
139ac872 1859
5eb91c98
DG
1860 /* Fetch once the poll data */
1861 revents = LTTNG_POLL_GETEV(&events, i);
1862 pollfd = LTTNG_POLL_GETFD(&events, i);
273ea72c 1863
5eb91c98 1864 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1865 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1866 if (ret) {
139ac872
MD
1867 err = 0;
1868 goto exit;
90014c57 1869 }
1d4b027a 1870
5eb91c98
DG
1871 /* Event on the registration socket */
1872 if (pollfd == apps_sock) {
1873 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1874 ERR("Register apps socket poll error");
1875 goto error;
1876 } else if (revents & LPOLLIN) {
1877 sock = lttcomm_accept_unix_sock(apps_sock);
1878 if (sock < 0) {
1879 goto error;
1880 }
099e26bd 1881
16c5c8fa
DG
1882 /*
1883 * Set socket timeout for both receiving and ending.
1884 * app_socket_timeout is in seconds, whereas
1885 * lttcomm_setsockopt_rcv_timeout and
1886 * lttcomm_setsockopt_snd_timeout expect msec as
1887 * parameter.
1888 */
1889 (void) lttcomm_setsockopt_rcv_timeout(sock,
1890 app_socket_timeout * 1000);
1891 (void) lttcomm_setsockopt_snd_timeout(sock,
1892 app_socket_timeout * 1000);
1893
b662582b
DG
1894 /*
1895 * Set the CLOEXEC flag. Return code is useless because
1896 * either way, the show must go on.
1897 */
1898 (void) utils_set_fd_cloexec(sock);
1899
5eb91c98 1900 /* Create UST registration command for enqueuing */
ba7f0ae5 1901 ust_cmd = zmalloc(sizeof(struct ust_command));
5eb91c98 1902 if (ust_cmd == NULL) {
76d7553f 1903 PERROR("ust command zmalloc");
5eb91c98
DG
1904 goto error;
1905 }
1d4b027a 1906
5eb91c98
DG
1907 /*
1908 * Using message-based transmissions to ensure we don't
1909 * have to deal with partially received messages.
1910 */
4063050c
MD
1911 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
1912 if (ret < 0) {
1913 ERR("Exhausted file descriptors allowed for applications.");
1914 free(ust_cmd);
1915 ret = close(sock);
1916 if (ret) {
1917 PERROR("close");
1918 }
1919 sock = -1;
1920 continue;
1921 }
d88aee68 1922
840cb59c 1923 health_code_update();
d0b96690
DG
1924 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
1925 if (ret < 0) {
5eb91c98 1926 free(ust_cmd);
d0b96690 1927 /* Close socket of the application. */
76d7553f
MD
1928 ret = close(sock);
1929 if (ret) {
1930 PERROR("close");
1931 }
4063050c 1932 lttng_fd_put(LTTNG_FD_APPS, 1);
76d7553f 1933 sock = -1;
5eb91c98
DG
1934 continue;
1935 }
840cb59c 1936 health_code_update();
099e26bd 1937
5eb91c98 1938 ust_cmd->sock = sock;
34a2494f 1939 sock = -1;
099e26bd 1940
5eb91c98
DG
1941 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1942 " gid:%d sock:%d name:%s (version %d.%d)",
1943 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1944 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1945 ust_cmd->sock, ust_cmd->reg_msg.name,
1946 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
54d01ffb 1947
5eb91c98
DG
1948 /*
1949 * Lock free enqueue the registration request. The red pill
54d01ffb 1950 * has been taken! This apps will be part of the *system*.
5eb91c98
DG
1951 */
1952 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
1953
1954 /*
1955 * Wake the registration queue futex. Implicit memory
1956 * barrier with the exchange in cds_wfq_enqueue.
1957 */
1958 futex_nto1_wake(&ust_cmd_queue.futex);
1959 }
1960 }
90014c57 1961 }
1d4b027a
DG
1962 }
1963
139ac872 1964exit:
1d4b027a 1965error:
0fdd1e2c
DG
1966 /* Notify that the registration thread is gone */
1967 notify_ust_apps(0);
1968
a4b35e07 1969 if (apps_sock >= 0) {
76d7553f
MD
1970 ret = close(apps_sock);
1971 if (ret) {
1972 PERROR("close");
1973 }
a4b35e07 1974 }
46c3f085 1975 if (sock >= 0) {
76d7553f
MD
1976 ret = close(sock);
1977 if (ret) {
1978 PERROR("close");
1979 }
4063050c 1980 lttng_fd_put(LTTNG_FD_APPS, 1);
a4b35e07 1981 }
273ea72c 1982 unlink(apps_unix_sock_path);
0fdd1e2c 1983
76d7553f 1984error_poll_add:
5eb91c98 1985 lttng_poll_clean(&events);
76d7553f
MD
1986error_listen:
1987error_create_poll:
6993eeb3 1988error_testpoint:
76d7553f 1989 DBG("UST Registration thread cleanup complete");
a09bf708
MD
1990 if (err) {
1991 health_error();
1992 ERR("Health error occurred in %s", __func__);
1993 }
8782cc74 1994 health_unregister(health_sessiond);
5eb91c98 1995
1d4b027a
DG
1996 return NULL;
1997}
1998
8c0faa1d 1999/*
3bd1e081 2000 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
d063d709 2001 * exec or it will fails.
8c0faa1d 2002 */
3bd1e081 2003static int spawn_consumer_thread(struct consumer_data *consumer_data)
8c0faa1d 2004{
a23ec3a7 2005 int ret, clock_ret;
ee0b0061
DG
2006 struct timespec timeout;
2007
a23ec3a7
DG
2008 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2009 consumer_data->consumer_thread_is_ready = 0;
8c0faa1d 2010
a23ec3a7
DG
2011 /* Setup pthread condition */
2012 ret = pthread_condattr_init(&consumer_data->condattr);
2013 if (ret != 0) {
2014 errno = ret;
2015 PERROR("pthread_condattr_init consumer data");
2016 goto error;
2017 }
2018
2019 /*
2020 * Set the monotonic clock in order to make sure we DO NOT jump in time
2021 * between the clock_gettime() call and the timedwait call. See bug #324
2022 * for a more details and how we noticed it.
2023 */
2024 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
2025 if (ret != 0) {
2026 errno = ret;
2027 PERROR("pthread_condattr_setclock consumer data");
ee0b0061
DG
2028 goto error;
2029 }
8c0faa1d 2030
a23ec3a7
DG
2031 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
2032 if (ret != 0) {
2033 errno = ret;
2034 PERROR("pthread_cond_init consumer data");
2035 goto error;
2036 }
2037
2038 ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer,
2039 consumer_data);
8c0faa1d 2040 if (ret != 0) {
3bd1e081 2041 PERROR("pthread_create consumer");
ee0b0061 2042 ret = -1;
8c0faa1d
DG
2043 goto error;
2044 }
2045
a23ec3a7
DG
2046 /* We are about to wait on a pthread condition */
2047 pthread_mutex_lock(&consumer_data->cond_mutex);
2048
ee0b0061 2049 /* Get time for sem_timedwait absolute timeout */
a23ec3a7
DG
2050 clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2051 /*
2052 * Set the timeout for the condition timed wait even if the clock gettime
2053 * call fails since we might loop on that call and we want to avoid to
2054 * increment the timeout too many times.
2055 */
2056 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
2057
2058 /*
2059 * The following loop COULD be skipped in some conditions so this is why we
2060 * set ret to 0 in order to make sure at least one round of the loop is
2061 * done.
2062 */
2063 ret = 0;
2064
2065 /*
2066 * Loop until the condition is reached or when a timeout is reached. Note
2067 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2068 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2069 * possible. This loop does not take any chances and works with both of
2070 * them.
2071 */
2072 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
2073 if (clock_ret < 0) {
2074 PERROR("clock_gettime spawn consumer");
2075 /* Infinite wait for the consumerd thread to be ready */
2076 ret = pthread_cond_wait(&consumer_data->cond,
2077 &consumer_data->cond_mutex);
2078 } else {
2079 ret = pthread_cond_timedwait(&consumer_data->cond,
2080 &consumer_data->cond_mutex, &timeout);
2081 }
ee0b0061 2082 }
8c0faa1d 2083
a23ec3a7
DG
2084 /* Release the pthread condition */
2085 pthread_mutex_unlock(&consumer_data->cond_mutex);
2086
2087 if (ret != 0) {
2088 errno = ret;
2089 if (ret == ETIMEDOUT) {
6565552a
DG
2090 int pth_ret;
2091
ee0b0061
DG
2092 /*
2093 * Call has timed out so we kill the kconsumerd_thread and return
2094 * an error.
2095 */
a23ec3a7
DG
2096 ERR("Condition timed out. The consumer thread was never ready."
2097 " Killing it");
6565552a
DG
2098 pth_ret = pthread_cancel(consumer_data->thread);
2099 if (pth_ret < 0) {
3bd1e081 2100 PERROR("pthread_cancel consumer thread");
ee0b0061
DG
2101 }
2102 } else {
a23ec3a7 2103 PERROR("pthread_cond_wait failed consumer thread");
ee0b0061 2104 }
6565552a
DG
2105 /* Caller is expecting a negative value on failure. */
2106 ret = -1;
ee0b0061
DG
2107 goto error;
2108 }
2109
3bd1e081
MD
2110 pthread_mutex_lock(&consumer_data->pid_mutex);
2111 if (consumer_data->pid == 0) {
a23ec3a7 2112 ERR("Consumerd did not start");
3bd1e081 2113 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556
DG
2114 goto error;
2115 }
3bd1e081 2116 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556 2117
8c0faa1d
DG
2118 return 0;
2119
2120error:
2121 return ret;
2122}
2123
d9800920 2124/*
3bd1e081 2125 * Join consumer thread
d9800920 2126 */
3bd1e081 2127static int join_consumer_thread(struct consumer_data *consumer_data)
cf3af59e
MD
2128{
2129 void *status;
cf3af59e 2130
e8209f6b
DG
2131 /* Consumer pid must be a real one. */
2132 if (consumer_data->pid > 0) {
c617c0c6 2133 int ret;
3bd1e081 2134 ret = kill(consumer_data->pid, SIGTERM);
cf3af59e 2135 if (ret) {
3bd1e081 2136 ERR("Error killing consumer daemon");
cf3af59e
MD
2137 return ret;
2138 }
3bd1e081 2139 return pthread_join(consumer_data->thread, &status);
cf3af59e
MD
2140 } else {
2141 return 0;
2142 }
2143}
2144
8c0faa1d 2145/*
3bd1e081 2146 * Fork and exec a consumer daemon (consumerd).
8c0faa1d 2147 *
d063d709 2148 * Return pid if successful else -1.
8c0faa1d 2149 */
3bd1e081 2150static pid_t spawn_consumerd(struct consumer_data *consumer_data)
8c0faa1d
DG
2151{
2152 int ret;
2153 pid_t pid;
94c55f17 2154 const char *consumer_to_use;
53086306 2155 const char *verbosity;
94c55f17 2156 struct stat st;
8c0faa1d 2157
3bd1e081 2158 DBG("Spawning consumerd");
c49dc785 2159
8c0faa1d
DG
2160 pid = fork();
2161 if (pid == 0) {
2162 /*
3bd1e081 2163 * Exec consumerd.
8c0faa1d 2164 */
daee5345 2165 if (opt_verbose_consumer) {
53086306
DG
2166 verbosity = "--verbose";
2167 } else {
2168 verbosity = "--quiet";
2169 }
3bd1e081
MD
2170 switch (consumer_data->type) {
2171 case LTTNG_CONSUMER_KERNEL:
94c55f17 2172 /*
c7704d57
DG
2173 * Find out which consumerd to execute. We will first try the
2174 * 64-bit path, then the sessiond's installation directory, and
2175 * fallback on the 32-bit one,
94c55f17 2176 */
63a799e8
AM
2177 DBG3("Looking for a kernel consumer at these locations:");
2178 DBG3(" 1) %s", consumerd64_bin);
2179 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
2180 DBG3(" 3) %s", consumerd32_bin);
94c55f17 2181 if (stat(consumerd64_bin, &st) == 0) {
63a799e8 2182 DBG3("Found location #1");
94c55f17 2183 consumer_to_use = consumerd64_bin;
94c55f17 2184 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
63a799e8 2185 DBG3("Found location #2");
94c55f17 2186 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
eb1e0bd4 2187 } else if (stat(consumerd32_bin, &st) == 0) {
63a799e8 2188 DBG3("Found location #3");
eb1e0bd4 2189 consumer_to_use = consumerd32_bin;
94c55f17 2190 } else {
63a799e8 2191 DBG("Could not find any valid consumerd executable");
6565552a 2192 ret = -EINVAL;
94c55f17
AM
2193 break;
2194 }
2195 DBG("Using kernel consumer at: %s", consumer_to_use);
6565552a 2196 ret = execl(consumer_to_use,
94c55f17
AM
2197 "lttng-consumerd", verbosity, "-k",
2198 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2199 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
6c71277b 2200 "--group", tracing_group_name,
94c55f17 2201 NULL);
3bd1e081 2202 break;
7753dea8
MD
2203 case LTTNG_CONSUMER64_UST:
2204 {
b1e0b6b6 2205 char *tmpnew = NULL;
8f4905da
MD
2206
2207 if (consumerd64_libdir[0] != '\0') {
2208 char *tmp;
2209 size_t tmplen;
2210
2211 tmp = getenv("LD_LIBRARY_PATH");
2212 if (!tmp) {
2213 tmp = "";
2214 }
2215 tmplen = strlen("LD_LIBRARY_PATH=")
2216 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
2217 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2218 if (!tmpnew) {
2219 ret = -ENOMEM;
2220 goto error;
2221 }
2222 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2223 strcat(tmpnew, consumerd64_libdir);
2224 if (tmp[0] != '\0') {
2225 strcat(tmpnew, ":");
2226 strcat(tmpnew, tmp);
2227 }
2228 ret = putenv(tmpnew);
2229 if (ret) {
2230 ret = -errno;
c6f76da9 2231 free(tmpnew);
8f4905da
MD
2232 goto error;
2233 }
2234 }
94c55f17 2235 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
a5a6aff3 2236 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
2237 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2238 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
6c71277b 2239 "--group", tracing_group_name,
7753dea8 2240 NULL);
8f4905da
MD
2241 if (consumerd64_libdir[0] != '\0') {
2242 free(tmpnew);
2243 }
3bd1e081 2244 break;
7753dea8
MD
2245 }
2246 case LTTNG_CONSUMER32_UST:
2247 {
937dde8e 2248 char *tmpnew = NULL;
8f4905da
MD
2249
2250 if (consumerd32_libdir[0] != '\0') {
2251 char *tmp;
2252 size_t tmplen;
2253
2254 tmp = getenv("LD_LIBRARY_PATH");
2255 if (!tmp) {
2256 tmp = "";
2257 }
2258 tmplen = strlen("LD_LIBRARY_PATH=")
2259 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
2260 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2261 if (!tmpnew) {
2262 ret = -ENOMEM;
2263 goto error;
2264 }
2265 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2266 strcat(tmpnew, consumerd32_libdir);
2267 if (tmp[0] != '\0') {
2268 strcat(tmpnew, ":");
2269 strcat(tmpnew, tmp);
2270 }
2271 ret = putenv(tmpnew);
2272 if (ret) {
2273 ret = -errno;
c6f76da9 2274 free(tmpnew);
8f4905da
MD
2275 goto error;
2276 }
2277 }
94c55f17 2278 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
a5a6aff3 2279 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
2280 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2281 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
6c71277b 2282 "--group", tracing_group_name,
7753dea8 2283 NULL);
8f4905da
MD
2284 if (consumerd32_libdir[0] != '\0') {
2285 free(tmpnew);
2286 }
7753dea8
MD
2287 break;
2288 }
3bd1e081 2289 default:
76d7553f 2290 PERROR("unknown consumer type");
3bd1e081
MD
2291 exit(EXIT_FAILURE);
2292 }
8c0faa1d 2293 if (errno != 0) {
6565552a 2294 PERROR("Consumer execl()");
8c0faa1d 2295 }
6565552a 2296 /* Reaching this point, we got a failure on our execl(). */
8c0faa1d
DG
2297 exit(EXIT_FAILURE);
2298 } else if (pid > 0) {
2299 ret = pid;
8c0faa1d 2300 } else {
76d7553f 2301 PERROR("start consumer fork");
8c0faa1d 2302 ret = -errno;
8c0faa1d 2303 }
8f4905da 2304error:
8c0faa1d
DG
2305 return ret;
2306}
2307
693bd40b 2308/*
3bd1e081 2309 * Spawn the consumerd daemon and session daemon thread.
693bd40b 2310 */
3bd1e081 2311static int start_consumerd(struct consumer_data *consumer_data)
693bd40b 2312{
c617c0c6 2313 int ret;
edb8b045
DG
2314
2315 /*
2316 * Set the listen() state on the socket since there is a possible race
2317 * between the exec() of the consumer daemon and this call if place in the
2318 * consumer thread. See bug #366 for more details.
2319 */
2320 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
2321 if (ret < 0) {
2322 goto error;
2323 }
693bd40b 2324
3bd1e081
MD
2325 pthread_mutex_lock(&consumer_data->pid_mutex);
2326 if (consumer_data->pid != 0) {
2327 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785
DG
2328 goto end;
2329 }
693bd40b 2330
3bd1e081 2331 ret = spawn_consumerd(consumer_data);
c49dc785 2332 if (ret < 0) {
3bd1e081
MD
2333 ERR("Spawning consumerd failed");
2334 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785 2335 goto error;
693bd40b 2336 }
c49dc785 2337
3bd1e081
MD
2338 /* Setting up the consumer_data pid */
2339 consumer_data->pid = ret;
48842b30 2340 DBG2("Consumer pid %d", consumer_data->pid);
3bd1e081 2341 pthread_mutex_unlock(&consumer_data->pid_mutex);
693bd40b 2342
3bd1e081
MD
2343 DBG2("Spawning consumer control thread");
2344 ret = spawn_consumer_thread(consumer_data);
693bd40b 2345 if (ret < 0) {
3bd1e081 2346 ERR("Fatal error spawning consumer control thread");
693bd40b
DG
2347 goto error;
2348 }
2349
c49dc785 2350end:
693bd40b
DG
2351 return 0;
2352
2353error:
331744e3 2354 /* Cleanup already created sockets on error. */
edb8b045 2355 if (consumer_data->err_sock >= 0) {
c617c0c6
MD
2356 int err;
2357
edb8b045
DG
2358 err = close(consumer_data->err_sock);
2359 if (err < 0) {
2360 PERROR("close consumer data error socket");
2361 }
2362 }
693bd40b
DG
2363 return ret;
2364}
2365
b73401da 2366/*
096102bd 2367 * Setup necessary data for kernel tracer action.
b73401da 2368 */
096102bd 2369static int init_kernel_tracer(void)
b73401da
DG
2370{
2371 int ret;
b73401da 2372
096102bd
DG
2373 /* Modprobe lttng kernel modules */
2374 ret = modprobe_lttng_control();
b73401da 2375 if (ret < 0) {
b73401da
DG
2376 goto error;
2377 }
2378
096102bd
DG
2379 /* Open debugfs lttng */
2380 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2381 if (kernel_tracer_fd < 0) {
2382 DBG("Failed to open %s", module_proc_lttng);
2f77fc4b
DG
2383 ret = -1;
2384 goto error_open;
54d01ffb
DG
2385 }
2386
2f77fc4b
DG
2387 /* Validate kernel version */
2388 ret = kernel_validate_version(kernel_tracer_fd);
2389 if (ret < 0) {
2390 goto error_version;
b551a063 2391 }
54d01ffb 2392
2f77fc4b
DG
2393 ret = modprobe_lttng_data();
2394 if (ret < 0) {
2395 goto error_modules;
54d01ffb
DG
2396 }
2397
2f77fc4b
DG
2398 DBG("Kernel tracer fd %d", kernel_tracer_fd);
2399 return 0;
2400
2401error_version:
2402 modprobe_remove_lttng_control();
2403 ret = close(kernel_tracer_fd);
2404 if (ret) {
2405 PERROR("close");
b551a063 2406 }
2f77fc4b 2407 kernel_tracer_fd = -1;
f73fabfd 2408 return LTTNG_ERR_KERN_VERSION;
b551a063 2409
2f77fc4b
DG
2410error_modules:
2411 ret = close(kernel_tracer_fd);
2412 if (ret) {
2413 PERROR("close");
b551a063 2414 }
54d01ffb 2415
2f77fc4b
DG
2416error_open:
2417 modprobe_remove_lttng_control();
54d01ffb
DG
2418
2419error:
2f77fc4b
DG
2420 WARN("No kernel tracer available");
2421 kernel_tracer_fd = -1;
2422 if (!is_root) {
f73fabfd 2423 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2f77fc4b 2424 } else {
f73fabfd 2425 return LTTNG_ERR_KERN_NA;
2f77fc4b 2426 }
54d01ffb
DG
2427}
2428
2f77fc4b 2429
54d01ffb 2430/*
2f77fc4b
DG
2431 * Copy consumer output from the tracing session to the domain session. The
2432 * function also applies the right modification on a per domain basis for the
2433 * trace files destination directory.
36b588ed
MD
2434 *
2435 * Should *NOT* be called with RCU read-side lock held.
54d01ffb 2436 */
2f77fc4b 2437static int copy_session_consumer(int domain, struct ltt_session *session)
54d01ffb
DG
2438{
2439 int ret;
2f77fc4b
DG
2440 const char *dir_name;
2441 struct consumer_output *consumer;
2442
2443 assert(session);
2444 assert(session->consumer);
54d01ffb 2445
b551a063
DG
2446 switch (domain) {
2447 case LTTNG_DOMAIN_KERNEL:
2f77fc4b 2448 DBG3("Copying tracing session consumer output in kernel session");
09a90bcd
DG
2449 /*
2450 * XXX: We should audit the session creation and what this function
2451 * does "extra" in order to avoid a destroy since this function is used
2452 * in the domain session creation (kernel and ust) only. Same for UST
2453 * domain.
2454 */
2455 if (session->kernel_session->consumer) {
2456 consumer_destroy_output(session->kernel_session->consumer);
2457 }
2f77fc4b
DG
2458 session->kernel_session->consumer =
2459 consumer_copy_output(session->consumer);
2460 /* Ease our life a bit for the next part */
2461 consumer = session->kernel_session->consumer;
2462 dir_name = DEFAULT_KERNEL_TRACE_DIR;
b551a063 2463 break;
f20baf8e 2464 case LTTNG_DOMAIN_JUL:
b551a063 2465 case LTTNG_DOMAIN_UST:
2f77fc4b 2466 DBG3("Copying tracing session consumer output in UST session");
09a90bcd
DG
2467 if (session->ust_session->consumer) {
2468 consumer_destroy_output(session->ust_session->consumer);
2469 }
2f77fc4b
DG
2470 session->ust_session->consumer =
2471 consumer_copy_output(session->consumer);
2472 /* Ease our life a bit for the next part */
2473 consumer = session->ust_session->consumer;
2474 dir_name = DEFAULT_UST_TRACE_DIR;
b551a063
DG
2475 break;
2476 default:
f73fabfd 2477 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
54d01ffb
DG
2478 goto error;
2479 }
2480
2f77fc4b 2481 /* Append correct directory to subdir */
c30ce0b3
CB
2482 strncat(consumer->subdir, dir_name,
2483 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2f77fc4b
DG
2484 DBG3("Copy session consumer subdir %s", consumer->subdir);
2485
f73fabfd 2486 ret = LTTNG_OK;
54d01ffb
DG
2487
2488error:
2489 return ret;
2490}
2491
00e2e675 2492/*
2f77fc4b 2493 * Create an UST session and add it to the session ust list.
36b588ed
MD
2494 *
2495 * Should *NOT* be called with RCU read-side lock held.
00e2e675 2496 */
2f77fc4b
DG
2497static int create_ust_session(struct ltt_session *session,
2498 struct lttng_domain *domain)
00e2e675
DG
2499{
2500 int ret;
2f77fc4b 2501 struct ltt_ust_session *lus = NULL;
00e2e675 2502
a4b92340 2503 assert(session);
2f77fc4b
DG
2504 assert(domain);
2505 assert(session->consumer);
a4b92340 2506
2f77fc4b 2507 switch (domain->type) {
f20baf8e 2508 case LTTNG_DOMAIN_JUL:
2f77fc4b
DG
2509 case LTTNG_DOMAIN_UST:
2510 break;
2511 default:
2512 ERR("Unknown UST domain on create session %d", domain->type);
f73fabfd 2513 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
00e2e675
DG
2514 goto error;
2515 }
2516
2f77fc4b
DG
2517 DBG("Creating UST session");
2518
dec56f6c 2519 lus = trace_ust_create_session(session->id);
2f77fc4b 2520 if (lus == NULL) {
f73fabfd 2521 ret = LTTNG_ERR_UST_SESS_FAIL;
a4b92340
DG
2522 goto error;
2523 }
2524
2f77fc4b
DG
2525 lus->uid = session->uid;
2526 lus->gid = session->gid;
2bba9e53 2527 lus->output_traces = session->output_traces;
27babd3a 2528 lus->snapshot_mode = session->snapshot_mode;
ecc48a90 2529 lus->live_timer_interval = session->live_timer;
2f77fc4b 2530 session->ust_session = lus;
00e2e675 2531
2f77fc4b
DG
2532 /* Copy session output to the newly created UST session */
2533 ret = copy_session_consumer(domain->type, session);
f73fabfd 2534 if (ret != LTTNG_OK) {
00e2e675
DG
2535 goto error;
2536 }
2537
f73fabfd 2538 return LTTNG_OK;
00e2e675
DG
2539
2540error:
2f77fc4b
DG
2541 free(lus);
2542 session->ust_session = NULL;
00e2e675
DG
2543 return ret;
2544}
2545
2546/*
2f77fc4b 2547 * Create a kernel tracer session then create the default channel.
00e2e675 2548 */
2f77fc4b 2549static int create_kernel_session(struct ltt_session *session)
00e2e675
DG
2550{
2551 int ret;
a4b92340 2552
2f77fc4b 2553 DBG("Creating kernel session");
00e2e675 2554
2f77fc4b
DG
2555 ret = kernel_create_session(session, kernel_tracer_fd);
2556 if (ret < 0) {
f73fabfd 2557 ret = LTTNG_ERR_KERN_SESS_FAIL;
00e2e675
DG
2558 goto error;
2559 }
2560
2f77fc4b
DG
2561 /* Code flow safety */
2562 assert(session->kernel_session);
2563
2564 /* Copy session output to the newly created Kernel session */
2565 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
f73fabfd 2566 if (ret != LTTNG_OK) {
a4b92340
DG
2567 goto error;
2568 }
2569
2f77fc4b
DG
2570 /* Create directory(ies) on local filesystem. */
2571 if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
2572 strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
2573 ret = run_as_mkdir_recursive(
2574 session->kernel_session->consumer->dst.trace_path,
2575 S_IRWXU | S_IRWXG, session->uid, session->gid);
2576 if (ret < 0) {
2577 if (ret != -EEXIST) {
2578 ERR("Trace directory creation error");
00e2e675
DG
2579 goto error;
2580 }
00e2e675 2581 }
2f77fc4b 2582 }
00e2e675 2583
2f77fc4b
DG
2584 session->kernel_session->uid = session->uid;
2585 session->kernel_session->gid = session->gid;
2bba9e53 2586 session->kernel_session->output_traces = session->output_traces;
27babd3a 2587 session->kernel_session->snapshot_mode = session->snapshot_mode;
00e2e675 2588
f73fabfd 2589 return LTTNG_OK;
00e2e675 2590
2f77fc4b
DG
2591error:
2592 trace_kernel_destroy_session(session->kernel_session);
2593 session->kernel_session = NULL;
2594 return ret;
2595}
00e2e675 2596
2f77fc4b
DG
2597/*
2598 * Count number of session permitted by uid/gid.
2599 */
2600static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2601{
2602 unsigned int i = 0;
2603 struct ltt_session *session;
07424f16 2604
2f77fc4b
DG
2605 DBG("Counting number of available session for UID %d GID %d",
2606 uid, gid);
2607 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
00e2e675 2608 /*
2f77fc4b 2609 * Only list the sessions the user can control.
00e2e675 2610 */
2f77fc4b
DG
2611 if (!session_access_ok(session, uid, gid)) {
2612 continue;
2613 }
2614 i++;
a4b92340 2615 }
2f77fc4b 2616 return i;
00e2e675
DG
2617}
2618
54d01ffb
DG
2619/*
2620 * Process the command requested by the lttng client within the command
2621 * context structure. This function make sure that the return structure (llm)
2622 * is set and ready for transmission before returning.
2623 *
2624 * Return any error encountered or 0 for success.
53a80697
MD
2625 *
2626 * "sock" is only used for special-case var. len data.
36b588ed
MD
2627 *
2628 * Should *NOT* be called with RCU read-side lock held.
54d01ffb 2629 */
53a80697
MD
2630static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2631 int *sock_error)
54d01ffb 2632{
f73fabfd 2633 int ret = LTTNG_OK;
44d3bd01 2634 int need_tracing_session = 1;
2e09ba09 2635 int need_domain;
54d01ffb
DG
2636
2637 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2638
53a80697
MD
2639 *sock_error = 0;
2640
2e09ba09
MD
2641 switch (cmd_ctx->lsm->cmd_type) {
2642 case LTTNG_CREATE_SESSION:
27babd3a 2643 case LTTNG_CREATE_SESSION_SNAPSHOT:
ecc48a90 2644 case LTTNG_CREATE_SESSION_LIVE:
2e09ba09
MD
2645 case LTTNG_DESTROY_SESSION:
2646 case LTTNG_LIST_SESSIONS:
2647 case LTTNG_LIST_DOMAINS:
2648 case LTTNG_START_TRACE:
2649 case LTTNG_STOP_TRACE:
6d805429 2650 case LTTNG_DATA_PENDING:
da3c9ec1
DG
2651 case LTTNG_SNAPSHOT_ADD_OUTPUT:
2652 case LTTNG_SNAPSHOT_DEL_OUTPUT:
2653 case LTTNG_SNAPSHOT_LIST_OUTPUT:
2654 case LTTNG_SNAPSHOT_RECORD:
2e09ba09 2655 need_domain = 0;
3aace903 2656 break;
2e09ba09
MD
2657 default:
2658 need_domain = 1;
2659 }
2660
2661 if (opt_no_kernel && need_domain
2662 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
531d29f9 2663 if (!is_root) {
f73fabfd 2664 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
531d29f9 2665 } else {
f73fabfd 2666 ret = LTTNG_ERR_KERN_NA;
531d29f9 2667 }
4fba7219
DG
2668 goto error;
2669 }
2670
8d3113b2
DG
2671 /* Deny register consumer if we already have a spawned consumer. */
2672 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2673 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2674 if (kconsumer_data.pid > 0) {
f73fabfd 2675 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
fa317f24 2676 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
8d3113b2
DG
2677 goto error;
2678 }
2679 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2680 }
2681
54d01ffb
DG
2682 /*
2683 * Check for command that don't needs to allocate a returned payload. We do
44d3bd01 2684 * this here so we don't have to make the call for no payload at each
54d01ffb
DG
2685 * command.
2686 */
2687 switch(cmd_ctx->lsm->cmd_type) {
2688 case LTTNG_LIST_SESSIONS:
2689 case LTTNG_LIST_TRACEPOINTS:
f37d259d 2690 case LTTNG_LIST_TRACEPOINT_FIELDS:
54d01ffb
DG
2691 case LTTNG_LIST_DOMAINS:
2692 case LTTNG_LIST_CHANNELS:
2693 case LTTNG_LIST_EVENTS:
2694 break;
2695 default:
2696 /* Setup lttng message with no payload */
2697 ret = setup_lttng_msg(cmd_ctx, 0);
2698 if (ret < 0) {
2699 /* This label does not try to unlock the session */
2700 goto init_setup_error;
2701 }
2702 }
2703
2704 /* Commands that DO NOT need a session. */
2705 switch (cmd_ctx->lsm->cmd_type) {
54d01ffb 2706 case LTTNG_CREATE_SESSION:
27babd3a 2707 case LTTNG_CREATE_SESSION_SNAPSHOT:
ecc48a90 2708 case LTTNG_CREATE_SESSION_LIVE:
2e09ba09 2709 case LTTNG_CALIBRATE:
54d01ffb
DG
2710 case LTTNG_LIST_SESSIONS:
2711 case LTTNG_LIST_TRACEPOINTS:
f37d259d 2712 case LTTNG_LIST_TRACEPOINT_FIELDS:
44d3bd01 2713 need_tracing_session = 0;
54d01ffb
DG
2714 break;
2715 default:
2716 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
256a5576
MD
2717 /*
2718 * We keep the session list lock across _all_ commands
2719 * for now, because the per-session lock does not
2720 * handle teardown properly.
2721 */
74babd95 2722 session_lock_list();
54d01ffb
DG
2723 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2724 if (cmd_ctx->session == NULL) {
bba2d65f 2725 ret = LTTNG_ERR_SESS_NOT_FOUND;
54d01ffb
DG
2726 goto error;
2727 } else {
2728 /* Acquire lock for the session */
2729 session_lock(cmd_ctx->session);
2730 }
2731 break;
2732 }
b389abbe 2733
2e09ba09
MD
2734 if (!need_domain) {
2735 goto skip_domain;
2736 }
a4b92340 2737
54d01ffb
DG
2738 /*
2739 * Check domain type for specific "pre-action".
2740 */
2741 switch (cmd_ctx->lsm->domain.type) {
2742 case LTTNG_DOMAIN_KERNEL:
d1f1c568 2743 if (!is_root) {
f73fabfd 2744 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
d1f1c568
DG
2745 goto error;
2746 }
2747
54d01ffb 2748 /* Kernel tracer check */
a4b35e07 2749 if (kernel_tracer_fd == -1) {
54d01ffb 2750 /* Basically, load kernel tracer modules */
096102bd
DG
2751 ret = init_kernel_tracer();
2752 if (ret != 0) {
54d01ffb
DG
2753 goto error;
2754 }
2755 }
5eb91c98 2756
5c827ce0
DG
2757 /* Consumer is in an ERROR state. Report back to client */
2758 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
f73fabfd 2759 ret = LTTNG_ERR_NO_KERNCONSUMERD;
5c827ce0
DG
2760 goto error;
2761 }
2762
54d01ffb 2763 /* Need a session for kernel command */
44d3bd01 2764 if (need_tracing_session) {
54d01ffb 2765 if (cmd_ctx->session->kernel_session == NULL) {
6df2e2c9 2766 ret = create_kernel_session(cmd_ctx->session);
5eb91c98 2767 if (ret < 0) {
f73fabfd 2768 ret = LTTNG_ERR_KERN_SESS_FAIL;
5eb91c98
DG
2769 goto error;
2770 }
b389abbe 2771 }
7d29a247 2772
54d01ffb 2773 /* Start the kernel consumer daemon */
3bd1e081
MD
2774 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2775 if (kconsumer_data.pid == 0 &&
785d2d0d 2776 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3bd1e081
MD
2777 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2778 ret = start_consumerd(&kconsumer_data);
7d29a247 2779 if (ret < 0) {
f73fabfd 2780 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
54d01ffb 2781 goto error;
950131af 2782 }
5c827ce0 2783 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3ff2ecac
MD
2784 } else {
2785 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
33a2b854 2786 }
173af62f 2787
a4b92340
DG
2788 /*
2789 * The consumer was just spawned so we need to add the socket to
2790 * the consumer output of the session if exist.
2791 */
2792 ret = consumer_create_socket(&kconsumer_data,
2793 cmd_ctx->session->kernel_session->consumer);
2794 if (ret < 0) {
2795 goto error;
173af62f 2796 }
0d0c377a 2797 }
5c827ce0 2798
54d01ffb 2799 break;
b9dfb167 2800 case LTTNG_DOMAIN_JUL:
2bdd86d4 2801 case LTTNG_DOMAIN_UST:
44d3bd01 2802 {
b51ec5b4
MD
2803 if (!ust_app_supported()) {
2804 ret = LTTNG_ERR_NO_UST;
2805 goto error;
2806 }
5c827ce0
DG
2807 /* Consumer is in an ERROR state. Report back to client */
2808 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
f73fabfd 2809 ret = LTTNG_ERR_NO_USTCONSUMERD;
5c827ce0
DG
2810 goto error;
2811 }
2812
44d3bd01 2813 if (need_tracing_session) {
a4b92340 2814 /* Create UST session if none exist. */
f6a9efaa 2815 if (cmd_ctx->session->ust_session == NULL) {
44d3bd01 2816 ret = create_ust_session(cmd_ctx->session,
6df2e2c9 2817 &cmd_ctx->lsm->domain);
f73fabfd 2818 if (ret != LTTNG_OK) {
44d3bd01
DG
2819 goto error;
2820 }
2821 }
00e2e675 2822
7753dea8
MD
2823 /* Start the UST consumer daemons */
2824 /* 64-bit */
2825 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
fc7a59ce 2826 if (consumerd64_bin[0] != '\0' &&
7753dea8 2827 ustconsumer64_data.pid == 0 &&
785d2d0d 2828 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
7753dea8
MD
2829 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2830 ret = start_consumerd(&ustconsumer64_data);
2bdd86d4 2831 if (ret < 0) {
f73fabfd 2832 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
173af62f 2833 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2bdd86d4
MD
2834 goto error;
2835 }
48842b30 2836
173af62f 2837 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
5c827ce0 2838 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3ff2ecac 2839 } else {
7753dea8
MD
2840 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2841 }
173af62f
DG
2842
2843 /*
2844 * Setup socket for consumer 64 bit. No need for atomic access
2845 * since it was set above and can ONLY be set in this thread.
2846 */
a4b92340
DG
2847 ret = consumer_create_socket(&ustconsumer64_data,
2848 cmd_ctx->session->ust_session->consumer);
2849 if (ret < 0) {
2850 goto error;
173af62f
DG
2851 }
2852
7753dea8 2853 /* 32-bit */
fc7a59ce 2854 if (consumerd32_bin[0] != '\0' &&
7753dea8 2855 ustconsumer32_data.pid == 0 &&
785d2d0d 2856 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
7753dea8
MD
2857 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2858 ret = start_consumerd(&ustconsumer32_data);
2859 if (ret < 0) {
f73fabfd 2860 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
173af62f 2861 uatomic_set(&ust_consumerd32_fd, -EINVAL);
7753dea8
MD
2862 goto error;
2863 }
5c827ce0 2864
173af62f 2865 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
5c827ce0 2866 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
7753dea8
MD
2867 } else {
2868 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2bdd86d4 2869 }
173af62f
DG
2870
2871 /*
2872 * Setup socket for consumer 64 bit. No need for atomic access
2873 * since it was set above and can ONLY be set in this thread.
2874 */
a4b92340
DG
2875 ret = consumer_create_socket(&ustconsumer32_data,
2876 cmd_ctx->session->ust_session->consumer);
2877 if (ret < 0) {
2878 goto error;
173af62f 2879 }
44d3bd01
DG
2880 }
2881 break;
48842b30 2882 }
54d01ffb 2883 default:
54d01ffb
DG
2884 break;
2885 }
2e09ba09 2886skip_domain:
33a2b854 2887
5c827ce0
DG
2888 /* Validate consumer daemon state when start/stop trace command */
2889 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
2890 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
2891 switch (cmd_ctx->lsm->domain.type) {
b9dfb167 2892 case LTTNG_DOMAIN_JUL:
5c827ce0
DG
2893 case LTTNG_DOMAIN_UST:
2894 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
f73fabfd 2895 ret = LTTNG_ERR_NO_USTCONSUMERD;
5c827ce0
DG
2896 goto error;
2897 }
2898 break;
2899 case LTTNG_DOMAIN_KERNEL:
2900 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
f73fabfd 2901 ret = LTTNG_ERR_NO_KERNCONSUMERD;
5c827ce0
DG
2902 goto error;
2903 }
2904 break;
2905 }
2906 }
2907
8e0af1b4
MD
2908 /*
2909 * Check that the UID or GID match that of the tracing session.
2910 * The root user can interact with all sessions.
2911 */
2912 if (need_tracing_session) {
2913 if (!session_access_ok(cmd_ctx->session,
730389d9
DG
2914 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2915 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
f73fabfd 2916 ret = LTTNG_ERR_EPERM;
8e0af1b4
MD
2917 goto error;
2918 }
2919 }
2920
ffe60014
DG
2921 /*
2922 * Send relayd information to consumer as soon as we have a domain and a
2923 * session defined.
2924 */
2925 if (cmd_ctx->session && need_domain) {
2926 /*
2927 * Setup relayd if not done yet. If the relayd information was already
2928 * sent to the consumer, this call will gracefully return.
2929 */
2930 ret = cmd_setup_relayd(cmd_ctx->session);
2931 if (ret != LTTNG_OK) {
2932 goto error;
2933 }
2934 }
2935
54d01ffb
DG
2936 /* Process by command type */
2937 switch (cmd_ctx->lsm->cmd_type) {
2938 case LTTNG_ADD_CONTEXT:
2939 {
2940 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2941 cmd_ctx->lsm->u.context.channel_name,
979e618e 2942 &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]);
54d01ffb
DG
2943 break;
2944 }
2945 case LTTNG_DISABLE_CHANNEL:
2946 {
2947 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2948 cmd_ctx->lsm->u.disable.channel_name);
2949 break;
2950 }
2951 case LTTNG_DISABLE_EVENT:
2952 {
2953 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2954 cmd_ctx->lsm->u.disable.channel_name,
2955 cmd_ctx->lsm->u.disable.name);
33a2b854
DG
2956 break;
2957 }
54d01ffb
DG
2958 case LTTNG_DISABLE_ALL_EVENT:
2959 {
2bdd86d4 2960 DBG("Disabling all events");
54d01ffb
DG
2961
2962 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2963 cmd_ctx->lsm->u.disable.channel_name);
2964 break;
2965 }
2966 case LTTNG_ENABLE_CHANNEL:
2967 {
7972aab2 2968 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
2f77fc4b 2969 &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
54d01ffb
DG
2970 break;
2971 }
2972 case LTTNG_ENABLE_EVENT:
2973 {
882ef835
JI
2974 struct lttng_event_exclusion *exclusion = NULL;
2975 struct lttng_filter_bytecode *bytecode = NULL;
2976
67676bd8
DG
2977 /* Handle exclusion events and receive it from the client. */
2978 if (cmd_ctx->lsm->u.enable.exclusion_count > 0) {
2979 size_t count = cmd_ctx->lsm->u.enable.exclusion_count;
2980
2981 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
2982 (count * LTTNG_SYMBOL_NAME_LEN));
2983 if (!exclusion) {
2984 ret = LTTNG_ERR_EXCLUSION_NOMEM;
2985 goto error;
882ef835 2986 }
882ef835 2987
67676bd8
DG
2988 DBG("Receiving var len exclusion event list from client ...");
2989 exclusion->count = count;
2990 ret = lttcomm_recv_unix_sock(sock, exclusion->names,
2991 count * LTTNG_SYMBOL_NAME_LEN);
2992 if (ret <= 0) {
2993 DBG("Nothing recv() from client var len data... continuing");
2994 *sock_error = 1;
2995 free(exclusion);
2996 ret = LTTNG_ERR_EXCLUSION_INVAL;
2997 goto error;
2998 }
2999 }
3000
3001 /* Handle filter and get bytecode from client. */
3002 if (cmd_ctx->lsm->u.enable.bytecode_len > 0) {
3003 size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len;
3004
3005 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
3006 ret = LTTNG_ERR_FILTER_INVAL;
3007 free(exclusion);
3008 goto error;
3009 }
3010
3011 bytecode = zmalloc(bytecode_len);
3012 if (!bytecode) {
3013 free(exclusion);
3014 ret = LTTNG_ERR_FILTER_NOMEM;
3015 goto error;
882ef835
JI
3016 }
3017
67676bd8
DG
3018 /* Receive var. len. data */
3019 DBG("Receiving var len filter's bytecode from client ...");
3020 ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len);
3021 if (ret <= 0) {
3022 DBG("Nothing recv() from client car len data... continuing");
3023 *sock_error = 1;
3024 free(bytecode);
3025 free(exclusion);
3026 ret = LTTNG_ERR_FILTER_INVAL;
3027 goto error;
3028 }
3029
3030 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
3031 free(bytecode);
3032 free(exclusion);
3033 ret = LTTNG_ERR_FILTER_INVAL;
3034 goto error;
3035 }
882ef835 3036 }
67676bd8
DG
3037
3038 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
3039 cmd_ctx->lsm->u.enable.channel_name,
3040 &cmd_ctx->lsm->u.enable.event, bytecode, exclusion,
3041 kernel_poll_pipe[1]);
54d01ffb
DG
3042 break;
3043 }
3044 case LTTNG_ENABLE_ALL_EVENT:
3045 {
2bdd86d4 3046 DBG("Enabling all events");
54d01ffb 3047
7972aab2 3048 ret = cmd_enable_event_all(cmd_ctx->session, &cmd_ctx->lsm->domain,
8c9ae521 3049 cmd_ctx->lsm->u.enable.channel_name,
025faf73 3050 cmd_ctx->lsm->u.enable.event.type, NULL, kernel_poll_pipe[1]);
54d01ffb
DG
3051 break;
3052 }
052da939 3053 case LTTNG_LIST_TRACEPOINTS:
2ef84c95 3054 {
9f19cc17 3055 struct lttng_event *events;
54d01ffb 3056 ssize_t nb_events;
052da939 3057
54d01ffb
DG
3058 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
3059 if (nb_events < 0) {
f73fabfd 3060 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3061 ret = -nb_events;
3062 goto error;
2ef84c95
DG
3063 }
3064
3065 /*
3066 * Setup lttng message with payload size set to the event list size in
3067 * bytes and then copy list into the llm payload.
3068 */
052da939 3069 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2ef84c95 3070 if (ret < 0) {
052da939 3071 free(events);
2ef84c95
DG
3072 goto setup_error;
3073 }
3074
3075 /* Copy event list into message payload */
9f19cc17 3076 memcpy(cmd_ctx->llm->payload, events,
052da939 3077 sizeof(struct lttng_event) * nb_events);
2ef84c95 3078
9f19cc17 3079 free(events);
2ef84c95 3080
f73fabfd 3081 ret = LTTNG_OK;
2ef84c95
DG
3082 break;
3083 }
f37d259d
MD
3084 case LTTNG_LIST_TRACEPOINT_FIELDS:
3085 {
3086 struct lttng_event_field *fields;
3087 ssize_t nb_fields;
3088
a4b92340
DG
3089 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
3090 &fields);
f37d259d 3091 if (nb_fields < 0) {
f73fabfd 3092 /* Return value is a negative lttng_error_code. */
f37d259d
MD
3093 ret = -nb_fields;
3094 goto error;
3095 }
3096
3097 /*
3098 * Setup lttng message with payload size set to the event list size in
3099 * bytes and then copy list into the llm payload.
3100 */
a4b92340
DG
3101 ret = setup_lttng_msg(cmd_ctx,
3102 sizeof(struct lttng_event_field) * nb_fields);
f37d259d
MD
3103 if (ret < 0) {
3104 free(fields);
3105 goto setup_error;
3106 }
3107
3108 /* Copy event list into message payload */
3109 memcpy(cmd_ctx->llm->payload, fields,
3110 sizeof(struct lttng_event_field) * nb_fields);
3111
3112 free(fields);
3113
f73fabfd 3114 ret = LTTNG_OK;
f37d259d
MD
3115 break;
3116 }
00e2e675
DG
3117 case LTTNG_SET_CONSUMER_URI:
3118 {
a4b92340
DG
3119 size_t nb_uri, len;
3120 struct lttng_uri *uris;
3121
3122 nb_uri = cmd_ctx->lsm->u.uri.size;
3123 len = nb_uri * sizeof(struct lttng_uri);
3124
3125 if (nb_uri == 0) {
f73fabfd 3126 ret = LTTNG_ERR_INVALID;
a4b92340
DG
3127 goto error;
3128 }
3129
3130 uris = zmalloc(len);
3131 if (uris == NULL) {
f73fabfd 3132 ret = LTTNG_ERR_FATAL;
a4b92340
DG
3133 goto error;
3134 }
3135
3136 /* Receive variable len data */
77c7c900 3137 DBG("Receiving %zu URI(s) from client ...", nb_uri);
a4b92340
DG
3138 ret = lttcomm_recv_unix_sock(sock, uris, len);
3139 if (ret <= 0) {
3140 DBG("No URIs received from client... continuing");
3141 *sock_error = 1;
f73fabfd 3142 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3143 free(uris);
a4b92340
DG
3144 goto error;
3145 }
3146
00e2e675 3147 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
a4b92340 3148 nb_uri, uris);
f73fabfd 3149 if (ret != LTTNG_OK) {
b1d41407 3150 free(uris);
a4b92340
DG
3151 goto error;
3152 }
3153
3154 /*
3155 * XXX: 0 means that this URI should be applied on the session. Should
3156 * be a DOMAIN enuam.
3157 */
3158 if (cmd_ctx->lsm->domain.type == 0) {
3159 /* Add the URI for the UST session if a consumer is present. */
3160 if (cmd_ctx->session->ust_session &&
3161 cmd_ctx->session->ust_session->consumer) {
3162 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session,
3163 nb_uri, uris);
3164 } else if (cmd_ctx->session->kernel_session &&
3165 cmd_ctx->session->kernel_session->consumer) {
3166 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL,
3167 cmd_ctx->session, nb_uri, uris);
3168 }
3169 }
3170
b1d41407
DG
3171 free(uris);
3172
00e2e675
DG
3173 break;
3174 }
f3ed775e 3175 case LTTNG_START_TRACE:
8c0faa1d 3176 {
54d01ffb 3177 ret = cmd_start_trace(cmd_ctx->session);
8c0faa1d
DG
3178 break;
3179 }
f3ed775e 3180 case LTTNG_STOP_TRACE:
8c0faa1d 3181 {
54d01ffb 3182 ret = cmd_stop_trace(cmd_ctx->session);
8c0faa1d
DG
3183 break;
3184 }
5e16da05
MD
3185 case LTTNG_CREATE_SESSION:
3186 {
a4b92340
DG
3187 size_t nb_uri, len;
3188 struct lttng_uri *uris = NULL;
3189
3190 nb_uri = cmd_ctx->lsm->u.uri.size;
3191 len = nb_uri * sizeof(struct lttng_uri);
3192
3193 if (nb_uri > 0) {
3194 uris = zmalloc(len);
3195 if (uris == NULL) {
f73fabfd 3196 ret = LTTNG_ERR_FATAL;
a4b92340
DG
3197 goto error;
3198 }
3199
3200 /* Receive variable len data */
77c7c900 3201 DBG("Waiting for %zu URIs from client ...", nb_uri);
a4b92340
DG
3202 ret = lttcomm_recv_unix_sock(sock, uris, len);
3203 if (ret <= 0) {
3204 DBG("No URIs received from client... continuing");
3205 *sock_error = 1;
f73fabfd 3206 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3207 free(uris);
a4b92340
DG
3208 goto error;
3209 }
3210
3211 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3212 DBG("Creating session with ONE network URI is a bad call");
f73fabfd 3213 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3214 free(uris);
a4b92340
DG
3215 goto error;
3216 }
3217 }
3218
3219 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
ecc48a90 3220 &cmd_ctx->creds, 0);
a4b92340 3221
b1d41407
DG
3222 free(uris);
3223
00e2e675
DG
3224 break;
3225 }
5e16da05
MD
3226 case LTTNG_DESTROY_SESSION:
3227 {
2f77fc4b 3228 ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]);
a4b92340
DG
3229
3230 /* Set session to NULL so we do not unlock it after free. */
256a5576 3231 cmd_ctx->session = NULL;
5461b305 3232 break;
5e16da05 3233 }
9f19cc17 3234 case LTTNG_LIST_DOMAINS:
5e16da05 3235 {
54d01ffb
DG
3236 ssize_t nb_dom;
3237 struct lttng_domain *domains;
5461b305 3238
54d01ffb
DG
3239 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
3240 if (nb_dom < 0) {
f73fabfd 3241 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3242 ret = -nb_dom;
3243 goto error;
ce3d728c 3244 }
520ff687 3245
54d01ffb 3246 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
5461b305 3247 if (ret < 0) {
ae9c20bc 3248 free(domains);
5461b305 3249 goto setup_error;
520ff687 3250 }
57167058 3251
54d01ffb
DG
3252 /* Copy event list into message payload */
3253 memcpy(cmd_ctx->llm->payload, domains,
3254 nb_dom * sizeof(struct lttng_domain));
3255
3256 free(domains);
5e16da05 3257
f73fabfd 3258 ret = LTTNG_OK;
5e16da05
MD
3259 break;
3260 }
9f19cc17 3261 case LTTNG_LIST_CHANNELS:
5e16da05 3262 {
6775595e 3263 int nb_chan;
54d01ffb
DG
3264 struct lttng_channel *channels;
3265
b551a063
DG
3266 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
3267 cmd_ctx->session, &channels);
54d01ffb 3268 if (nb_chan < 0) {
f73fabfd 3269 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3270 ret = -nb_chan;
3271 goto error;
5461b305 3272 }
ca95a216 3273
54d01ffb 3274 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
5461b305 3275 if (ret < 0) {
ae9c20bc 3276 free(channels);
5461b305
DG
3277 goto setup_error;
3278 }
9f19cc17 3279
54d01ffb
DG
3280 /* Copy event list into message payload */
3281 memcpy(cmd_ctx->llm->payload, channels,
3282 nb_chan * sizeof(struct lttng_channel));
3283
3284 free(channels);
9f19cc17 3285
f73fabfd 3286 ret = LTTNG_OK;
5461b305 3287 break;
5e16da05 3288 }
9f19cc17 3289 case LTTNG_LIST_EVENTS:
5e16da05 3290 {
b551a063 3291 ssize_t nb_event;
684d34d2 3292 struct lttng_event *events = NULL;
9f19cc17 3293
b551a063 3294 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
54d01ffb
DG
3295 cmd_ctx->lsm->u.list.channel_name, &events);
3296 if (nb_event < 0) {
f73fabfd 3297 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3298 ret = -nb_event;
3299 goto error;
5461b305 3300 }
ca95a216 3301
54d01ffb 3302 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
5461b305 3303 if (ret < 0) {
ae9c20bc 3304 free(events);
5461b305
DG
3305 goto setup_error;
3306 }
9f19cc17 3307
54d01ffb
DG
3308 /* Copy event list into message payload */
3309 memcpy(cmd_ctx->llm->payload, events,
3310 nb_event * sizeof(struct lttng_event));
9f19cc17 3311
54d01ffb 3312 free(events);
9f19cc17 3313
f73fabfd 3314 ret = LTTNG_OK;
5461b305 3315 break;
5e16da05
MD
3316 }
3317 case LTTNG_LIST_SESSIONS:
3318 {
8e0af1b4 3319 unsigned int nr_sessions;
5461b305 3320
8e0af1b4 3321 session_lock_list();
730389d9
DG
3322 nr_sessions = lttng_sessions_count(
3323 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3324 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
d32fb093 3325
8e0af1b4 3326 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
5461b305 3327 if (ret < 0) {
54d01ffb 3328 session_unlock_list();
5461b305 3329 goto setup_error;
e065084a 3330 }
5e16da05 3331
6c9cc2ab 3332 /* Filled the session array */
2f77fc4b 3333 cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
730389d9
DG
3334 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3335 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
6c9cc2ab 3336
54d01ffb 3337 session_unlock_list();
5e16da05 3338
f73fabfd 3339 ret = LTTNG_OK;
5e16da05
MD
3340 break;
3341 }
d0254c7c
MD
3342 case LTTNG_CALIBRATE:
3343 {
54d01ffb
DG
3344 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
3345 &cmd_ctx->lsm->u.calibrate);
d0254c7c
MD
3346 break;
3347 }
d9800920
DG
3348 case LTTNG_REGISTER_CONSUMER:
3349 {
2f77fc4b
DG
3350 struct consumer_data *cdata;
3351
3352 switch (cmd_ctx->lsm->domain.type) {
3353 case LTTNG_DOMAIN_KERNEL:
3354 cdata = &kconsumer_data;
3355 break;
3356 default:
f73fabfd 3357 ret = LTTNG_ERR_UND;
2f77fc4b
DG
3358 goto error;
3359 }
3360
54d01ffb 3361 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2f77fc4b 3362 cmd_ctx->lsm->u.reg.path, cdata);
d9800920
DG
3363 break;
3364 }
6d805429 3365 case LTTNG_DATA_PENDING:
806e2684 3366 {
6d805429 3367 ret = cmd_data_pending(cmd_ctx->session);
806e2684
DG
3368 break;
3369 }
da3c9ec1
DG
3370 case LTTNG_SNAPSHOT_ADD_OUTPUT:
3371 {
6dc3064a
DG
3372 struct lttcomm_lttng_output_id reply;
3373
3374 ret = cmd_snapshot_add_output(cmd_ctx->session,
3375 &cmd_ctx->lsm->u.snapshot_output.output, &reply.id);
3376 if (ret != LTTNG_OK) {
3377 goto error;
3378 }
3379
3380 ret = setup_lttng_msg(cmd_ctx, sizeof(reply));
3381 if (ret < 0) {
3382 goto setup_error;
3383 }
3384
3385 /* Copy output list into message payload */
3386 memcpy(cmd_ctx->llm->payload, &reply, sizeof(reply));
3387 ret = LTTNG_OK;
da3c9ec1
DG
3388 break;
3389 }
3390 case LTTNG_SNAPSHOT_DEL_OUTPUT:
3391 {
6dc3064a
DG
3392 ret = cmd_snapshot_del_output(cmd_ctx->session,
3393 &cmd_ctx->lsm->u.snapshot_output.output);
da3c9ec1
DG
3394 break;
3395 }
3396 case LTTNG_SNAPSHOT_LIST_OUTPUT:
3397 {
6dc3064a
DG
3398 ssize_t nb_output;
3399 struct lttng_snapshot_output *outputs = NULL;
3400
3401 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
3402 if (nb_output < 0) {
3403 ret = -nb_output;
3404 goto error;
3405 }
3406
3407 ret = setup_lttng_msg(cmd_ctx,
3408 nb_output * sizeof(struct lttng_snapshot_output));
3409 if (ret < 0) {
3410 free(outputs);
3411 goto setup_error;
3412 }
3413
3414 if (outputs) {
3415 /* Copy output list into message payload */
3416 memcpy(cmd_ctx->llm->payload, outputs,
3417 nb_output * sizeof(struct lttng_snapshot_output));
3418 free(outputs);
3419 }
3420
3421 ret = LTTNG_OK;
da3c9ec1
DG
3422 break;
3423 }
3424 case LTTNG_SNAPSHOT_RECORD:
3425 {
6dc3064a
DG
3426 ret = cmd_snapshot_record(cmd_ctx->session,
3427 &cmd_ctx->lsm->u.snapshot_record.output,
3428 cmd_ctx->lsm->u.snapshot_record.wait);
da3c9ec1
DG
3429 break;
3430 }
27babd3a
DG
3431 case LTTNG_CREATE_SESSION_SNAPSHOT:
3432 {
3433 size_t nb_uri, len;
3434 struct lttng_uri *uris = NULL;
3435
3436 nb_uri = cmd_ctx->lsm->u.uri.size;
3437 len = nb_uri * sizeof(struct lttng_uri);
3438
3439 if (nb_uri > 0) {
3440 uris = zmalloc(len);
3441 if (uris == NULL) {
3442 ret = LTTNG_ERR_FATAL;
3443 goto error;
3444 }
3445
3446 /* Receive variable len data */
3447 DBG("Waiting for %zu URIs from client ...", nb_uri);
3448 ret = lttcomm_recv_unix_sock(sock, uris, len);
3449 if (ret <= 0) {
3450 DBG("No URIs received from client... continuing");
3451 *sock_error = 1;
3452 ret = LTTNG_ERR_SESSION_FAIL;
3453 free(uris);
3454 goto error;
3455 }
3456
3457 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3458 DBG("Creating session with ONE network URI is a bad call");
3459 ret = LTTNG_ERR_SESSION_FAIL;
3460 free(uris);
3461 goto error;
3462 }
3463 }
3464
3465 ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris,
3466 nb_uri, &cmd_ctx->creds);
3467 free(uris);
3468 break;
3469 }
ecc48a90
JD
3470 case LTTNG_CREATE_SESSION_LIVE:
3471 {
3472 size_t nb_uri, len;
3473 struct lttng_uri *uris = NULL;
3474
3475 nb_uri = cmd_ctx->lsm->u.uri.size;
3476 len = nb_uri * sizeof(struct lttng_uri);
3477
3478 if (nb_uri > 0) {
3479 uris = zmalloc(len);
3480 if (uris == NULL) {
3481 ret = LTTNG_ERR_FATAL;
3482 goto error;
3483 }
3484
3485 /* Receive variable len data */
3486 DBG("Waiting for %zu URIs from client ...", nb_uri);
3487 ret = lttcomm_recv_unix_sock(sock, uris, len);
3488 if (ret <= 0) {
3489 DBG("No URIs received from client... continuing");
3490 *sock_error = 1;
3491 ret = LTTNG_ERR_SESSION_FAIL;
3492 free(uris);
3493 goto error;
3494 }
3495
3496 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3497 DBG("Creating session with ONE network URI is a bad call");
3498 ret = LTTNG_ERR_SESSION_FAIL;
3499 free(uris);
3500 goto error;
3501 }
3502 }
3503
3504 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris,
3505 nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval);
3506 free(uris);
3507 break;
3508 }
5e16da05 3509 default:
f73fabfd 3510 ret = LTTNG_ERR_UND;
5461b305 3511 break;
fac6795d
DG
3512 }
3513
5461b305 3514error:
5461b305
DG
3515 if (cmd_ctx->llm == NULL) {
3516 DBG("Missing llm structure. Allocating one.");
894be886 3517 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
5461b305
DG
3518 goto setup_error;
3519 }
3520 }
54d01ffb 3521 /* Set return code */
5461b305 3522 cmd_ctx->llm->ret_code = ret;
5461b305 3523setup_error:
b5541356 3524 if (cmd_ctx->session) {
54d01ffb 3525 session_unlock(cmd_ctx->session);
b5541356 3526 }
256a5576
MD
3527 if (need_tracing_session) {
3528 session_unlock_list();
3529 }
54d01ffb 3530init_setup_error:
8028d920 3531 return ret;
fac6795d
DG
3532}
3533
44a5e5eb
DG
3534/*
3535 * Thread managing health check socket.
3536 */
3537static void *thread_manage_health(void *data)
3538{
eb4a2943 3539 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
44a5e5eb
DG
3540 uint32_t revents, nb_fd;
3541 struct lttng_poll_event events;
0c89d795
MD
3542 struct health_comm_msg msg;
3543 struct health_comm_reply reply;
44a5e5eb
DG
3544
3545 DBG("[thread] Manage health check started");
3546
3547 rcu_register_thread();
3548
6d737ce4
DG
3549 /* We might hit an error path before this is created. */
3550 lttng_poll_init(&events);
3cc04881 3551
44a5e5eb
DG
3552 /* Create unix socket */
3553 sock = lttcomm_create_unix_sock(health_unix_sock_path);
3554 if (sock < 0) {
3555 ERR("Unable to create health check Unix socket");
3556 ret = -1;
3557 goto error;
3558 }
3559
6c71277b
MD
3560 if (is_root) {
3561 /* lttng health client socket path permissions */
3562 ret = chown(health_unix_sock_path, 0,
3563 utils_get_group_id(tracing_group_name));
3564 if (ret < 0) {
3565 ERR("Unable to set group on %s", health_unix_sock_path);
3566 PERROR("chown");
3567 ret = -1;
3568 goto error;
3569 }
3570
3571 ret = chmod(health_unix_sock_path,
3572 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3573 if (ret < 0) {
3574 ERR("Unable to set permissions on %s", health_unix_sock_path);
3575 PERROR("chmod");
3576 ret = -1;
3577 goto error;
3578 }
3579 }
3580
b662582b
DG
3581 /*
3582 * Set the CLOEXEC flag. Return code is useless because either way, the
3583 * show must go on.
3584 */
3585 (void) utils_set_fd_cloexec(sock);
3586
44a5e5eb
DG
3587 ret = lttcomm_listen_unix_sock(sock);
3588 if (ret < 0) {
3589 goto error;
3590 }
3591
3592 /*
3593 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3594 * more will be added to this poll set.
3595 */
d0b96690 3596 ret = sessiond_set_thread_pollset(&events, 2);
44a5e5eb
DG
3597 if (ret < 0) {
3598 goto error;
3599 }
3600
3601 /* Add the application registration socket */
3602 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
3603 if (ret < 0) {
3604 goto error;
3605 }
3606
56045742
MD
3607 lttng_sessiond_notify_ready();
3608
44a5e5eb
DG
3609 while (1) {
3610 DBG("Health check ready");
3611
44a5e5eb
DG
3612 /* Inifinite blocking call, waiting for transmission */
3613restart:
3614 ret = lttng_poll_wait(&events, -1);
3615 if (ret < 0) {
3616 /*
3617 * Restart interrupted system call.
3618 */
3619 if (errno == EINTR) {
3620 goto restart;
3621 }
3622 goto error;
3623 }
3624
0d9c5d77
DG
3625 nb_fd = ret;
3626
44a5e5eb
DG
3627 for (i = 0; i < nb_fd; i++) {
3628 /* Fetch once the poll data */
3629 revents = LTTNG_POLL_GETEV(&events, i);
3630 pollfd = LTTNG_POLL_GETFD(&events, i);
3631
3632 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 3633 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
44a5e5eb 3634 if (ret) {
139ac872
MD
3635 err = 0;
3636 goto exit;
44a5e5eb
DG
3637 }
3638
3639 /* Event on the registration socket */
3640 if (pollfd == sock) {
3641 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3642 ERR("Health socket poll error");
3643 goto error;
3644 }
3645 }
3646 }
3647
3648 new_sock = lttcomm_accept_unix_sock(sock);
3649 if (new_sock < 0) {
3650 goto error;
3651 }
3652
b662582b
DG
3653 /*
3654 * Set the CLOEXEC flag. Return code is useless because either way, the
3655 * show must go on.
3656 */
3657 (void) utils_set_fd_cloexec(new_sock);
3658
44a5e5eb
DG
3659 DBG("Receiving data from client for health...");
3660 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
3661 if (ret <= 0) {
3662 DBG("Nothing recv() from client... continuing");
3663 ret = close(new_sock);
3664 if (ret) {
3665 PERROR("close");
3666 }
3667 new_sock = -1;
3668 continue;
3669 }
3670
3671 rcu_thread_online();
3672
239f3aec 3673 memset(&reply, 0, sizeof(reply));
6c71277b
MD
3674 for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) {
3675 /*
3676 * health_check_state returns 0 if health is
3677 * bad.
3678 */
3679 if (!health_check_state(health_sessiond, i)) {
3680 reply.ret_code |= 1ULL << i;
3681 }
44a5e5eb
DG
3682 }
3683
6c71277b 3684 DBG2("Health check return value %" PRIx64, reply.ret_code);
44a5e5eb
DG
3685
3686 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
3687 if (ret < 0) {
3688 ERR("Failed to send health data back to client");
3689 }
3690
3691 /* End of transmission */
3692 ret = close(new_sock);
3693 if (ret) {
3694 PERROR("close");
3695 }
3696 new_sock = -1;
3697 }
3698
139ac872 3699exit:
44a5e5eb 3700error:
139ac872
MD
3701 if (err) {
3702 ERR("Health error occurred in %s", __func__);
3703 }
44a5e5eb
DG
3704 DBG("Health check thread dying");
3705 unlink(health_unix_sock_path);
3706 if (sock >= 0) {
3707 ret = close(sock);
3708 if (ret) {
3709 PERROR("close");
3710 }
3711 }
44a5e5eb
DG
3712
3713 lttng_poll_clean(&events);
3714
3715 rcu_unregister_thread();
3716 return NULL;
3717}
3718
1d4b027a 3719/*
d063d709
DG
3720 * This thread manage all clients request using the unix client socket for
3721 * communication.
1d4b027a
DG
3722 */
3723static void *thread_manage_clients(void *data)
3724{
139ac872 3725 int sock = -1, ret, i, pollfd, err = -1;
53a80697 3726 int sock_error;
5eb91c98 3727 uint32_t revents, nb_fd;
273ea72c 3728 struct command_ctx *cmd_ctx = NULL;
5eb91c98 3729 struct lttng_poll_event events;
1d4b027a
DG
3730
3731 DBG("[thread] Manage client started");
3732
f6a9efaa
DG
3733 rcu_register_thread();
3734
6c71277b 3735 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
927ca06a 3736
840cb59c 3737 health_code_update();
44a5e5eb 3738
1d4b027a
DG
3739 ret = lttcomm_listen_unix_sock(client_sock);
3740 if (ret < 0) {
35df2755 3741 goto error_listen;
1d4b027a
DG
3742 }
3743
5eb91c98
DG
3744 /*
3745 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3746 * more will be added to this poll set.
3747 */
d0b96690 3748 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 3749 if (ret < 0) {
35df2755 3750 goto error_create_poll;
5eb91c98 3751 }
273ea72c 3752
5eb91c98
DG
3753 /* Add the application registration socket */
3754 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
3755 if (ret < 0) {
3756 goto error;
3757 }
273ea72c 3758
56045742 3759 lttng_sessiond_notify_ready();
0bb7724a 3760
62c131b7
MD
3761 /* This testpoint is after we signal readiness to the parent. */
3762 if (testpoint(sessiond_thread_manage_clients)) {
3763 goto error;
3764 }
3765
71ddb4ed 3766 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
6993eeb3
CB
3767 goto error;
3768 }
8ac94142 3769
840cb59c 3770 health_code_update();
44a5e5eb 3771
1d4b027a 3772 while (1) {
1d4b027a 3773 DBG("Accepting client command ...");
273ea72c
DG
3774
3775 /* Inifinite blocking call, waiting for transmission */
88f2b785 3776 restart:
a78af745 3777 health_poll_entry();
5eb91c98 3778 ret = lttng_poll_wait(&events, -1);
a78af745 3779 health_poll_exit();
273ea72c 3780 if (ret < 0) {
88f2b785
MD
3781 /*
3782 * Restart interrupted system call.
3783 */
3784 if (errno == EINTR) {
3785 goto restart;
3786 }
273ea72c
DG
3787 goto error;
3788 }
3789
0d9c5d77
DG
3790 nb_fd = ret;
3791
5eb91c98
DG
3792 for (i = 0; i < nb_fd; i++) {
3793 /* Fetch once the poll data */
3794 revents = LTTNG_POLL_GETEV(&events, i);
3795 pollfd = LTTNG_POLL_GETFD(&events, i);
3796
840cb59c 3797 health_code_update();
44a5e5eb 3798
5eb91c98 3799 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 3800 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 3801 if (ret) {
139ac872
MD
3802 err = 0;
3803 goto exit;
5eb91c98
DG
3804 }
3805
3806 /* Event on the registration socket */
3807 if (pollfd == client_sock) {
3808 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3809 ERR("Client socket poll error");
3810 goto error;
3811 }
3812 }
3813 }
3814
3815 DBG("Wait for client response");
3816
840cb59c 3817 health_code_update();
44a5e5eb 3818
5eb91c98
DG
3819 sock = lttcomm_accept_unix_sock(client_sock);
3820 if (sock < 0) {
273ea72c 3821 goto error;
273ea72c
DG
3822 }
3823
b662582b
DG
3824 /*
3825 * Set the CLOEXEC flag. Return code is useless because either way, the
3826 * show must go on.
3827 */
3828 (void) utils_set_fd_cloexec(sock);
3829
be040666
DG
3830 /* Set socket option for credentials retrieval */
3831 ret = lttcomm_setsockopt_creds_unix_sock(sock);
3832 if (ret < 0) {
3833 goto error;
3834 }
3835
5eb91c98 3836 /* Allocate context command to process the client request */
ba7f0ae5 3837 cmd_ctx = zmalloc(sizeof(struct command_ctx));
5eb91c98 3838 if (cmd_ctx == NULL) {
76d7553f 3839 PERROR("zmalloc cmd_ctx");
1d4b027a 3840 goto error;
5eb91c98 3841 }
1d4b027a 3842
5eb91c98 3843 /* Allocate data buffer for reception */
ba7f0ae5 3844 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
5eb91c98 3845 if (cmd_ctx->lsm == NULL) {
76d7553f 3846 PERROR("zmalloc cmd_ctx->lsm");
5eb91c98
DG
3847 goto error;
3848 }
1d4b027a 3849
5eb91c98
DG
3850 cmd_ctx->llm = NULL;
3851 cmd_ctx->session = NULL;
1d4b027a 3852
840cb59c 3853 health_code_update();
44a5e5eb 3854
5eb91c98
DG
3855 /*
3856 * Data is received from the lttng client. The struct
3857 * lttcomm_session_msg (lsm) contains the command and data request of
3858 * the client.
3859 */
3860 DBG("Receiving data from client ...");
be040666
DG
3861 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
3862 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
5eb91c98
DG
3863 if (ret <= 0) {
3864 DBG("Nothing recv() from client... continuing");
76d7553f
MD
3865 ret = close(sock);
3866 if (ret) {
3867 PERROR("close");
3868 }
3869 sock = -1;
a2c0da86 3870 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
3871 continue;
3872 }
1d4b027a 3873
840cb59c 3874 health_code_update();
44a5e5eb 3875
5eb91c98
DG
3876 // TODO: Validate cmd_ctx including sanity check for
3877 // security purpose.
f7776ea7 3878
f6a9efaa 3879 rcu_thread_online();
5eb91c98
DG
3880 /*
3881 * This function dispatch the work to the kernel or userspace tracer
3882 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3883 * informations for the client. The command context struct contains
3884 * everything this function may needs.
3885 */
53a80697 3886 ret = process_client_msg(cmd_ctx, sock, &sock_error);
f6a9efaa 3887 rcu_thread_offline();
5eb91c98 3888 if (ret < 0) {
36134aa1
DG
3889 ret = close(sock);
3890 if (ret) {
3891 PERROR("close");
53a80697 3892 }
36134aa1 3893 sock = -1;
bbd973c2 3894 /*
5eb91c98 3895 * TODO: Inform client somehow of the fatal error. At
ba7f0ae5 3896 * this point, ret < 0 means that a zmalloc failed
53a80697
MD
3897 * (ENOMEM). Error detected but still accept
3898 * command, unless a socket error has been
3899 * detected.
bbd973c2 3900 */
bbd973c2 3901 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
3902 continue;
3903 }
d6e4fca4 3904
840cb59c 3905 health_code_update();
44a5e5eb 3906
54d01ffb
DG
3907 DBG("Sending response (size: %d, retcode: %s)",
3908 cmd_ctx->lttng_msg_size,
9a745bc7 3909 lttng_strerror(-cmd_ctx->llm->ret_code));
54d01ffb 3910 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
5eb91c98
DG
3911 if (ret < 0) {
3912 ERR("Failed to send data back to client");
bbd973c2 3913 }
1d4b027a 3914
5eb91c98 3915 /* End of transmission */
76d7553f
MD
3916 ret = close(sock);
3917 if (ret) {
3918 PERROR("close");
3919 }
3920 sock = -1;
be040666
DG
3921
3922 clean_command_ctx(&cmd_ctx);
44a5e5eb 3923
840cb59c 3924 health_code_update();
273ea72c
DG
3925 }
3926
139ac872 3927exit:
5eb91c98 3928error:
35df2755
CB
3929 if (sock >= 0) {
3930 ret = close(sock);
3931 if (ret) {
3932 PERROR("close");
3933 }
139ac872 3934 }
44a5e5eb 3935
35df2755
CB
3936 lttng_poll_clean(&events);
3937 clean_command_ctx(&cmd_ctx);
3938
3939error_listen:
3940error_create_poll:
273ea72c 3941 unlink(client_unix_sock_path);
76d7553f
MD
3942 if (client_sock >= 0) {
3943 ret = close(client_sock);
3944 if (ret) {
3945 PERROR("close");
3946 }
a4b35e07 3947 }
35df2755
CB
3948
3949 if (err) {
840cb59c 3950 health_error();
35df2755 3951 ERR("Health error occurred in %s", __func__);
a4b35e07 3952 }
273ea72c 3953
8782cc74 3954 health_unregister(health_sessiond);
35df2755
CB
3955
3956 DBG("Client thread dying");
f6a9efaa
DG
3957
3958 rcu_unregister_thread();
1d4b027a
DG
3959 return NULL;
3960}
3961
3962
fac6795d
DG
3963/*
3964 * usage function on stderr
3965 */
3966static void usage(void)
3967{
b716ce68 3968 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
d6f42150
DG
3969 fprintf(stderr, " -h, --help Display this usage.\n");
3970 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
3971 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3972 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3973 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
7753dea8
MD
3974 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3975 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3976 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3977 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
ebaeda94
MD
3978 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3979 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3980 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3981 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
d6f42150 3982 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
929de64e 3983 fprintf(stderr, " -b, --background Start as a daemon, keeping console open.\n");
d6f42150
DG
3984 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3985 fprintf(stderr, " -V, --version Show version number.\n");
7fe6d2c5 3986 fprintf(stderr, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
d6f42150
DG
3987 fprintf(stderr, " -q, --quiet No output at all.\n");
3988 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
35f90c40 3989 fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
3bd1e081 3990 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4fba7219 3991 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
4d076222 3992 fprintf(stderr, " --jul-tcp-port JUL application registration TCP port\n");
fac6795d
DG
3993}
3994
3995/*
3996 * daemon argument parsing
3997 */
3998static int parse_args(int argc, char **argv)
3999{
4000 int c;
4001
4002 static struct option long_options[] = {
4003 { "client-sock", 1, 0, 'c' },
4004 { "apps-sock", 1, 0, 'a' },
3bd1e081
MD
4005 { "kconsumerd-cmd-sock", 1, 0, 'C' },
4006 { "kconsumerd-err-sock", 1, 0, 'E' },
7753dea8
MD
4007 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
4008 { "ustconsumerd32-err-sock", 1, 0, 'H' },
ebaeda94
MD
4009 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
4010 { "ustconsumerd64-err-sock", 1, 0, 'F' },
4011 { "consumerd32-path", 1, 0, 'u' },
4012 { "consumerd32-libdir", 1, 0, 'U' },
4013 { "consumerd64-path", 1, 0, 't' },
4014 { "consumerd64-libdir", 1, 0, 'T' },
fac6795d 4015 { "daemonize", 0, 0, 'd' },
5b8719f5 4016 { "sig-parent", 0, 0, 'S' },
fac6795d
DG
4017 { "help", 0, 0, 'h' },
4018 { "group", 1, 0, 'g' },
4019 { "version", 0, 0, 'V' },
75462a81 4020 { "quiet", 0, 0, 'q' },
3f9947db 4021 { "verbose", 0, 0, 'v' },
3bd1e081 4022 { "verbose-consumer", 0, 0, 'Z' },
4fba7219 4023 { "no-kernel", 0, 0, 'N' },
35f90c40 4024 { "pidfile", 1, 0, 'p' },
4d076222 4025 { "jul-tcp-port", 1, 0, 'J' },
06b55dbb 4026 { "background", 0, 0, 'b' },
fac6795d
DG
4027 { NULL, 0, 0, 0 }
4028 };
4029
4030 while (1) {
4031 int option_index = 0;
06b55dbb 4032 c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:J:b",
54d01ffb 4033 long_options, &option_index);
fac6795d
DG
4034 if (c == -1) {
4035 break;
4036 }
4037
4038 switch (c) {
4039 case 0:
4040 fprintf(stderr, "option %s", long_options[option_index].name);
4041 if (optarg) {
4042 fprintf(stderr, " with arg %s\n", optarg);
4043 }
4044 break;
b716ce68 4045 case 'c':
fac6795d
DG
4046 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
4047 break;
4048 case 'a':
4049 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
4050 break;
4051 case 'd':
4052 opt_daemon = 1;
4053 break;
06b55dbb
DG
4054 case 'b':
4055 opt_background = 1;
4056 break;
fac6795d 4057 case 'g':
6c71277b 4058 tracing_group_name = optarg;
fac6795d
DG
4059 break;
4060 case 'h':
4061 usage();
4062 exit(EXIT_FAILURE);
4063 case 'V':
4064 fprintf(stdout, "%s\n", VERSION);
4065 exit(EXIT_SUCCESS);
5b8719f5
DG
4066 case 'S':
4067 opt_sig_parent = 1;
4068 break;
d6f42150 4069 case 'E':
3bd1e081 4070 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
d6f42150
DG
4071 break;
4072 case 'C':
3bd1e081
MD
4073 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
4074 break;
4075 case 'F':
7753dea8 4076 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3bd1e081
MD
4077 break;
4078 case 'D':
7753dea8
MD
4079 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
4080 break;
4081 case 'H':
4082 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
4083 break;
4084 case 'G':
4085 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
d6f42150 4086 break;
4fba7219
DG
4087 case 'N':
4088 opt_no_kernel = 1;
4089 break;
75462a81 4090 case 'q':
97e19046 4091 lttng_opt_quiet = 1;
75462a81 4092 break;
3f9947db 4093 case 'v':
53086306 4094 /* Verbose level can increase using multiple -v */
97e19046 4095 lttng_opt_verbose += 1;
3f9947db 4096 break;
31f73cc9 4097 case 'Z':
3bd1e081 4098 opt_verbose_consumer += 1;
31f73cc9 4099 break;
fb09408a 4100 case 'u':
fc7a59ce 4101 consumerd32_bin= optarg;
ebaeda94
MD
4102 break;
4103 case 'U':
4104 consumerd32_libdir = optarg;
7753dea8
MD
4105 break;
4106 case 't':
fc7a59ce 4107 consumerd64_bin = optarg;
ebaeda94
MD
4108 break;
4109 case 'T':
4110 consumerd64_libdir = optarg;
fb09408a 4111 break;
35f90c40
DG
4112 case 'p':
4113 opt_pidfile = optarg;
4114 break;
4d076222
DG
4115 case 'J': /* JUL TCP port. */
4116 {
4117 unsigned long v;
4118
4119 errno = 0;
4120 v = strtoul(optarg, NULL, 0);
4121 if (errno != 0 || !isdigit(optarg[0])) {
4122 ERR("Wrong value in --jul-tcp-port parameter: %s", optarg);
4123 return -1;
4124 }
4125 if (v == 0 || v >= 65535) {
4126 ERR("Port overflow in --jul-tcp-port parameter: %s", optarg);
4127 return -1;
4128 }
4129 jul_tcp_port = (uint32_t) v;
4130 DBG3("JUL TCP port set to non default: %u", jul_tcp_port);
4131 break;
4132 }
fac6795d
DG
4133 default:
4134 /* Unknown option or other error.
4135 * Error is printed by getopt, just return */
4136 return -1;
4137 }
4138 }
4139
4140 return 0;
4141}
4142
4143/*
d063d709 4144 * Creates the two needed socket by the daemon.
d6f42150
DG
4145 * apps_sock - The communication socket for all UST apps.
4146 * client_sock - The communication of the cli tool (lttng).
fac6795d 4147 */
cf3af59e 4148static int init_daemon_socket(void)
fac6795d
DG
4149{
4150 int ret = 0;
4151 mode_t old_umask;
4152
4153 old_umask = umask(0);
4154
4155 /* Create client tool unix socket */
d6f42150
DG
4156 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
4157 if (client_sock < 0) {
4158 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
4159 ret = -1;
4160 goto end;
4161 }
4162
b662582b
DG
4163 /* Set the cloexec flag */
4164 ret = utils_set_fd_cloexec(client_sock);
4165 if (ret < 0) {
4166 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4167 "Continuing but note that the consumer daemon will have a "
4168 "reference to this socket on exec()", client_sock);
4169 }
4170
fac6795d
DG
4171 /* File permission MUST be 660 */
4172 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
4173 if (ret < 0) {
d6f42150 4174 ERR("Set file permissions failed: %s", client_unix_sock_path);
76d7553f 4175 PERROR("chmod");
fac6795d
DG
4176 goto end;
4177 }
4178
4179 /* Create the application unix socket */
d6f42150
DG
4180 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
4181 if (apps_sock < 0) {
4182 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
4183 ret = -1;
4184 goto end;
4185 }
4186
b662582b
DG
4187 /* Set the cloexec flag */
4188 ret = utils_set_fd_cloexec(apps_sock);
4189 if (ret < 0) {
4190 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4191 "Continuing but note that the consumer daemon will have a "
4192 "reference to this socket on exec()", apps_sock);
4193 }
4194
d6f42150 4195 /* File permission MUST be 666 */
54d01ffb
DG
4196 ret = chmod(apps_unix_sock_path,
4197 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fac6795d 4198 if (ret < 0) {
d6f42150 4199 ERR("Set file permissions failed: %s", apps_unix_sock_path);
76d7553f 4200 PERROR("chmod");
fac6795d
DG
4201 goto end;
4202 }
4203
b662582b
DG
4204 DBG3("Session daemon client socket %d and application socket %d created",
4205 client_sock, apps_sock);
4206
fac6795d
DG
4207end:
4208 umask(old_umask);
4209 return ret;
4210}
4211
4212/*
54d01ffb
DG
4213 * Check if the global socket is available, and if a daemon is answering at the
4214 * other side. If yes, error is returned.
fac6795d 4215 */
cf3af59e 4216static int check_existing_daemon(void)
fac6795d 4217{
7d8234d9 4218 /* Is there anybody out there ? */
099e26bd 4219 if (lttng_session_daemon_alive()) {
7d8234d9 4220 return -EEXIST;
099e26bd 4221 }
b09c7c76
DG
4222
4223 return 0;
fac6795d
DG
4224}
4225
fac6795d 4226/*
d063d709 4227 * Set the tracing group gid onto the client socket.
5e16da05 4228 *
d063d709 4229 * Race window between mkdir and chown is OK because we are going from more
d1613cf5 4230 * permissive (root.root) to less permissive (root.tracing).
fac6795d 4231 */
be040666 4232static int set_permissions(char *rundir)
fac6795d
DG
4233{
4234 int ret;
996b65c8 4235 gid_t gid;
fac6795d 4236
6c71277b 4237 gid = utils_get_group_id(tracing_group_name);
fac6795d 4238
d6f42150 4239 /* Set lttng run dir */
be040666 4240 ret = chown(rundir, 0, gid);
d6f42150 4241 if (ret < 0) {
be040666 4242 ERR("Unable to set group on %s", rundir);
76d7553f 4243 PERROR("chown");
d6f42150
DG
4244 }
4245
6c71277b
MD
4246 /*
4247 * Ensure all applications and tracing group can search the run
4248 * dir. Allow everyone to read the directory, since it does not
4249 * buy us anything to hide its content.
4250 */
4251 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
d1613cf5
JN
4252 if (ret < 0) {
4253 ERR("Unable to set permissions on %s", rundir);
76d7553f 4254 PERROR("chmod");
d1613cf5
JN
4255 }
4256
d6f42150 4257 /* lttng client socket path */
996b65c8 4258 ret = chown(client_unix_sock_path, 0, gid);
fac6795d 4259 if (ret < 0) {
d6f42150 4260 ERR("Unable to set group on %s", client_unix_sock_path);
76d7553f 4261 PERROR("chown");
d6f42150
DG
4262 }
4263
3bd1e081 4264 /* kconsumer error socket path */
6c71277b 4265 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
d6f42150 4266 if (ret < 0) {
3bd1e081 4267 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
76d7553f 4268 PERROR("chown");
3bd1e081
MD
4269 }
4270
7753dea8 4271 /* 64-bit ustconsumer error socket path */
6c71277b 4272 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
7753dea8
MD
4273 if (ret < 0) {
4274 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
76d7553f 4275 PERROR("chown");
7753dea8
MD
4276 }
4277
4278 /* 32-bit ustconsumer compat32 error socket path */
6c71277b 4279 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
3bd1e081 4280 if (ret < 0) {
7753dea8 4281 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
76d7553f 4282 PERROR("chown");
fac6795d
DG
4283 }
4284
d6f42150 4285 DBG("All permissions are set");
e07ae692 4286
fac6795d
DG
4287 return ret;
4288}
4289
d6f42150 4290/*
d063d709 4291 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150 4292 */
67e40797 4293static int create_lttng_rundir(const char *rundir)
d6f42150
DG
4294{
4295 int ret;
4296
67e40797
DG
4297 DBG3("Creating LTTng run directory: %s", rundir);
4298
d1613cf5 4299 ret = mkdir(rundir, S_IRWXU);
d6f42150 4300 if (ret < 0) {
b1f11e69 4301 if (errno != EEXIST) {
67e40797 4302 ERR("Unable to create %s", rundir);
b1f11e69
DG
4303 goto error;
4304 } else {
4305 ret = 0;
4306 }
d6f42150
DG
4307 }
4308
4309error:
4310 return ret;
4311}
4312
4313/*
d063d709
DG
4314 * Setup sockets and directory needed by the kconsumerd communication with the
4315 * session daemon.
d6f42150 4316 */
67e40797
DG
4317static int set_consumer_sockets(struct consumer_data *consumer_data,
4318 const char *rundir)
d6f42150
DG
4319{
4320 int ret;
67e40797 4321 char path[PATH_MAX];
d6f42150 4322
6c71277b 4323 switch (consumer_data->type) {
7753dea8 4324 case LTTNG_CONSUMER_KERNEL:
60922cb0 4325 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
7753dea8
MD
4326 break;
4327 case LTTNG_CONSUMER64_UST:
60922cb0 4328 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
7753dea8
MD
4329 break;
4330 case LTTNG_CONSUMER32_UST:
60922cb0 4331 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
7753dea8
MD
4332 break;
4333 default:
4334 ERR("Consumer type unknown");
4335 ret = -EINVAL;
4336 goto error;
d6f42150
DG
4337 }
4338
67e40797
DG
4339 DBG2("Creating consumer directory: %s", path);
4340
6c71277b 4341 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
d6f42150 4342 if (ret < 0) {
6beb2242 4343 if (errno != EEXIST) {
f11e84c2 4344 PERROR("mkdir");
3bd1e081 4345 ERR("Failed to create %s", path);
6beb2242
DG
4346 goto error;
4347 }
f11e84c2 4348 ret = -1;
d6f42150 4349 }
6c71277b
MD
4350 if (is_root) {
4351 ret = chown(path, 0, utils_get_group_id(tracing_group_name));
4352 if (ret < 0) {
4353 ERR("Unable to set group on %s", path);
4354 PERROR("chown");
4355 goto error;
4356 }
4357 }
d6f42150
DG
4358
4359 /* Create the kconsumerd error unix socket */
3bd1e081
MD
4360 consumer_data->err_sock =
4361 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
4362 if (consumer_data->err_sock < 0) {
4363 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
d6f42150
DG
4364 ret = -1;
4365 goto error;
4366 }
4367
a24f05ab
MD
4368 /*
4369 * Set the CLOEXEC flag. Return code is useless because either way, the
4370 * show must go on.
4371 */
4372 ret = utils_set_fd_cloexec(consumer_data->err_sock);
4373 if (ret < 0) {
4374 PERROR("utils_set_fd_cloexec");
4375 /* continue anyway */
4376 }
4377
d6f42150 4378 /* File permission MUST be 660 */
3bd1e081 4379 ret = chmod(consumer_data->err_unix_sock_path,
54d01ffb 4380 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
d6f42150 4381 if (ret < 0) {
3bd1e081 4382 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
67e40797 4383 PERROR("chmod");
d6f42150
DG
4384 goto error;
4385 }
4386
4387error:
4388 return ret;
4389}
4390
fac6795d 4391/*
d063d709 4392 * Signal handler for the daemon
cf3af59e 4393 *
54d01ffb
DG
4394 * Simply stop all worker threads, leaving main() return gracefully after
4395 * joining all threads and calling cleanup().
fac6795d
DG
4396 */
4397static void sighandler(int sig)
4398{
4399 switch (sig) {
cf3af59e 4400 case SIGPIPE:
af87c45a 4401 DBG("SIGPIPE caught");
cf3af59e
MD
4402 return;
4403 case SIGINT:
af87c45a 4404 DBG("SIGINT caught");
cf3af59e
MD
4405 stop_threads();
4406 break;
4407 case SIGTERM:
af87c45a 4408 DBG("SIGTERM caught");
cf3af59e
MD
4409 stop_threads();
4410 break;
0bb7724a
DG
4411 case SIGUSR1:
4412 CMM_STORE_SHARED(recv_child_signal, 1);
4413 break;
cf3af59e
MD
4414 default:
4415 break;
fac6795d 4416 }
fac6795d
DG
4417}
4418
4419/*
d063d709 4420 * Setup signal handler for :
1d4b027a 4421 * SIGINT, SIGTERM, SIGPIPE
fac6795d 4422 */
1d4b027a 4423static int set_signal_handler(void)
fac6795d 4424{
1d4b027a
DG
4425 int ret = 0;
4426 struct sigaction sa;
4427 sigset_t sigset;
fac6795d 4428
1d4b027a 4429 if ((ret = sigemptyset(&sigset)) < 0) {
76d7553f 4430 PERROR("sigemptyset");
1d4b027a
DG
4431 return ret;
4432 }
d6f42150 4433
1d4b027a
DG
4434 sa.sa_handler = sighandler;
4435 sa.sa_mask = sigset;
4436 sa.sa_flags = 0;
4437 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
76d7553f 4438 PERROR("sigaction");
1d4b027a 4439 return ret;
d6f42150
DG
4440 }
4441
1d4b027a 4442 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
76d7553f 4443 PERROR("sigaction");
1d4b027a 4444 return ret;
d6f42150 4445 }
aaf26714 4446
1d4b027a 4447 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
76d7553f 4448 PERROR("sigaction");
1d4b027a 4449 return ret;
8c0faa1d
DG
4450 }
4451
0bb7724a
DG
4452 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
4453 PERROR("sigaction");
4454 return ret;
4455 }
4456
4457 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1d4b027a
DG
4458
4459 return ret;
fac6795d
DG
4460}
4461
f3ed775e 4462/*
d063d709
DG
4463 * Set open files limit to unlimited. This daemon can open a large number of
4464 * file descriptors in order to consumer multiple kernel traces.
f3ed775e
DG
4465 */
4466static void set_ulimit(void)
4467{
4468 int ret;
4469 struct rlimit lim;
4470
a88df331 4471 /* The kernel does not allowed an infinite limit for open files */
f3ed775e
DG
4472 lim.rlim_cur = 65535;
4473 lim.rlim_max = 65535;
4474
4475 ret = setrlimit(RLIMIT_NOFILE, &lim);
4476 if (ret < 0) {
76d7553f 4477 PERROR("failed to set open files limit");
f3ed775e
DG
4478 }
4479}
4480
35f90c40
DG
4481/*
4482 * Write pidfile using the rundir and opt_pidfile.
4483 */
4484static void write_pidfile(void)
4485{
4486 int ret;
4487 char pidfile_path[PATH_MAX];
4488
4489 assert(rundir);
4490
4491 if (opt_pidfile) {
4492 strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path));
4493 } else {
4494 /* Build pidfile path from rundir and opt_pidfile. */
4495 ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/"
4496 DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir);
4497 if (ret < 0) {
4498 PERROR("snprintf pidfile path");
4499 goto error;
4500 }
4501 }
4502
4503 /*
4504 * Create pid file in rundir. Return value is of no importance. The
4505 * execution will continue even though we are not able to write the file.
4506 */
4507 (void) utils_create_pid_file(getpid(), pidfile_path);
4508
4509error:
4510 return;
4511}
4512
cd9290dd
DG
4513/*
4514 * Write JUL TCP port using the rundir.
4515 */
4516static void write_julport(void)
4517{
4518 int ret;
4519 char path[PATH_MAX];
4520
4521 assert(rundir);
4522
4523 ret = snprintf(path, sizeof(path), "%s/"
4524 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE, rundir);
4525 if (ret < 0) {
4526 PERROR("snprintf julport path");
4527 goto error;
4528 }
4529
4530 /*
4531 * Create TCP JUL port file in rundir. Return value is of no importance.
4532 * The execution will continue even though we are not able to write the
4533 * file.
4534 */
4535 (void) utils_create_pid_file(jul_tcp_port, path);
4536
4537error:
4538 return;
4539}
4540
fac6795d
DG
4541/*
4542 * main
4543 */
4544int main(int argc, char **argv)
4545{
fac6795d
DG
4546 int ret = 0;
4547 void *status;
ae9e45b3 4548 const char *home_path, *env_app_timeout;
fac6795d 4549
335a95b7
MD
4550 init_kernel_workarounds();
4551
f6a9efaa
DG
4552 rcu_register_thread();
4553
0bb7724a
DG
4554 if ((ret = set_signal_handler()) < 0) {
4555 goto error;
4556 }
4557
7753dea8 4558 setup_consumerd_path();
fb09408a 4559
12744796
DG
4560 page_size = sysconf(_SC_PAGESIZE);
4561 if (page_size < 0) {
4562 PERROR("sysconf _SC_PAGESIZE");
4563 page_size = LONG_MAX;
4564 WARN("Fallback page size to %ld", page_size);
4565 }
4566
fac6795d
DG
4567 /* Parse arguments */
4568 progname = argv[0];
c617c0c6 4569 if ((ret = parse_args(argc, argv)) < 0) {
cf3af59e 4570 goto error;
fac6795d
DG
4571 }
4572
4573 /* Daemonize */
929de64e 4574 if (opt_daemon || opt_background) {
ceed52b5
MD
4575 int i;
4576
929de64e
MD
4577 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
4578 !opt_background);
53094c05 4579 if (ret < 0) {
cf3af59e 4580 goto error;
53094c05 4581 }
0bb7724a 4582
ceed52b5 4583 /*
0bb7724a
DG
4584 * We are in the child. Make sure all other file descriptors are
4585 * closed, in case we are called with more opened file descriptors than
4586 * the standard ones.
ceed52b5
MD
4587 */
4588 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
4589 (void) close(i);
4590 }
4591 }
4592
4593 /* Create thread quit pipe */
4594 if ((ret = init_thread_quit_pipe()) < 0) {
4595 goto error;
fac6795d
DG
4596 }
4597
4598 /* Check if daemon is UID = 0 */
4599 is_root = !getuid();
4600
fac6795d 4601 if (is_root) {
990570ed 4602 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
67e40797
DG
4603
4604 /* Create global run dir with root access */
4605 ret = create_lttng_rundir(rundir);
d6f42150 4606 if (ret < 0) {
cf3af59e 4607 goto error;
d6f42150
DG
4608 }
4609
fac6795d 4610 if (strlen(apps_unix_sock_path) == 0) {
d6f42150
DG
4611 snprintf(apps_unix_sock_path, PATH_MAX,
4612 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
fac6795d
DG
4613 }
4614
4615 if (strlen(client_unix_sock_path) == 0) {
d6f42150
DG
4616 snprintf(client_unix_sock_path, PATH_MAX,
4617 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
4618 }
0fdd1e2c
DG
4619
4620 /* Set global SHM for ust */
4621 if (strlen(wait_shm_path) == 0) {
4622 snprintf(wait_shm_path, PATH_MAX,
4623 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
4624 }
67e40797 4625
44a5e5eb
DG
4626 if (strlen(health_unix_sock_path) == 0) {
4627 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4628 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
4629 }
4630
67e40797
DG
4631 /* Setup kernel consumerd path */
4632 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
60922cb0 4633 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
67e40797 4634 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 4635 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
67e40797
DG
4636
4637 DBG2("Kernel consumer err path: %s",
4638 kconsumer_data.err_unix_sock_path);
4639 DBG2("Kernel consumer cmd path: %s",
4640 kconsumer_data.cmd_unix_sock_path);
fac6795d 4641 } else {
feb0f3e5 4642 home_path = utils_get_home_dir();
b082db07 4643 if (home_path == NULL) {
273ea72c
DG
4644 /* TODO: Add --socket PATH option */
4645 ERR("Can't get HOME directory for sockets creation.");
cf3af59e
MD
4646 ret = -EPERM;
4647 goto error;
b082db07
DG
4648 }
4649
67e40797
DG
4650 /*
4651 * Create rundir from home path. This will create something like
4652 * $HOME/.lttng
4653 */
990570ed 4654 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
67e40797
DG
4655 if (ret < 0) {
4656 ret = -ENOMEM;
4657 goto error;
4658 }
4659
4660 ret = create_lttng_rundir(rundir);
4661 if (ret < 0) {
4662 goto error;
4663 }
4664
fac6795d 4665 if (strlen(apps_unix_sock_path) == 0) {
d6f42150 4666 snprintf(apps_unix_sock_path, PATH_MAX,
b082db07 4667 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
fac6795d
DG
4668 }
4669
4670 /* Set the cli tool unix socket path */
4671 if (strlen(client_unix_sock_path) == 0) {
d6f42150 4672 snprintf(client_unix_sock_path, PATH_MAX,
b082db07 4673 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
fac6795d 4674 }
0fdd1e2c
DG
4675
4676 /* Set global SHM for ust */
4677 if (strlen(wait_shm_path) == 0) {
4678 snprintf(wait_shm_path, PATH_MAX,
d0b96690 4679 DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid());
0fdd1e2c 4680 }
44a5e5eb
DG
4681
4682 /* Set health check Unix path */
4683 if (strlen(health_unix_sock_path) == 0) {
4684 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4685 DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
4686 }
fac6795d
DG
4687 }
4688
5c827ce0
DG
4689 /* Set consumer initial state */
4690 kernel_consumerd_state = CONSUMER_STOPPED;
4691 ust_consumerd_state = CONSUMER_STOPPED;
4692
847177cd
DG
4693 DBG("Client socket path %s", client_unix_sock_path);
4694 DBG("Application socket path %s", apps_unix_sock_path);
d0b96690 4695 DBG("Application wait path %s", wait_shm_path);
67e40797
DG
4696 DBG("LTTng run directory path: %s", rundir);
4697
4698 /* 32 bits consumerd path setup */
4699 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
60922cb0 4700 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
67e40797 4701 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 4702 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
67e40797
DG
4703
4704 DBG2("UST consumer 32 bits err path: %s",
4705 ustconsumer32_data.err_unix_sock_path);
4706 DBG2("UST consumer 32 bits cmd path: %s",
4707 ustconsumer32_data.cmd_unix_sock_path);
4708
4709 /* 64 bits consumerd path setup */
4710 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
60922cb0 4711 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
67e40797 4712 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 4713 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
67e40797
DG
4714
4715 DBG2("UST consumer 64 bits err path: %s",
4716 ustconsumer64_data.err_unix_sock_path);
4717 DBG2("UST consumer 64 bits cmd path: %s",
4718 ustconsumer64_data.cmd_unix_sock_path);
847177cd 4719
273ea72c 4720 /*
7d8234d9 4721 * See if daemon already exist.
fac6795d 4722 */
7d8234d9 4723 if ((ret = check_existing_daemon()) < 0) {
75462a81 4724 ERR("Already running daemon.\n");
273ea72c 4725 /*
cf3af59e
MD
4726 * We do not goto exit because we must not cleanup()
4727 * because a daemon is already running.
ab118b20 4728 */
cf3af59e 4729 goto error;
a88df331
DG
4730 }
4731
1427f9b2
DG
4732 /*
4733 * Init UST app hash table. Alloc hash table before this point since
4734 * cleanup() can get called after that point.
4735 */
4736 ust_app_ht_alloc();
4737
f20baf8e
DG
4738 /* Initialize JUL domain subsystem. */
4739 if ((ret = jul_init()) < 0) {
4740 /* ENOMEM at this point. */
4741 goto error;
4742 }
4743
54d01ffb 4744 /* After this point, we can safely call cleanup() with "goto exit" */
a88df331
DG
4745
4746 /*
4747 * These actions must be executed as root. We do that *after* setting up
4748 * the sockets path because we MUST make the check for another daemon using
4749 * those paths *before* trying to set the kernel consumer sockets and init
4750 * kernel tracer.
4751 */
4752 if (is_root) {
67e40797 4753 ret = set_consumer_sockets(&kconsumer_data, rundir);
7753dea8
MD
4754 if (ret < 0) {
4755 goto exit;
4756 }
4757
a88df331 4758 /* Setup kernel tracer */
4fba7219
DG
4759 if (!opt_no_kernel) {
4760 init_kernel_tracer();
4761 }
a88df331
DG
4762
4763 /* Set ulimit for open files */
4764 set_ulimit();
fac6795d 4765 }
4063050c
MD
4766 /* init lttng_fd tracking must be done after set_ulimit. */
4767 lttng_fd_init();
fac6795d 4768
67e40797
DG
4769 ret = set_consumer_sockets(&ustconsumer64_data, rundir);
4770 if (ret < 0) {
4771 goto exit;
4772 }
4773
4774 ret = set_consumer_sockets(&ustconsumer32_data, rundir);
4775 if (ret < 0) {
4776 goto exit;
4777 }
4778
d6f42150 4779 /* Setup the needed unix socket */
cf3af59e
MD
4780 if ((ret = init_daemon_socket()) < 0) {
4781 goto exit;
fac6795d
DG
4782 }
4783
4784 /* Set credentials to socket */
be040666 4785 if (is_root && ((ret = set_permissions(rundir)) < 0)) {
cf3af59e 4786 goto exit;
fac6795d
DG
4787 }
4788
5b8719f5
DG
4789 /* Get parent pid if -S, --sig-parent is specified. */
4790 if (opt_sig_parent) {
4791 ppid = getppid();
4792 }
4793
7a485870 4794 /* Setup the kernel pipe for waking up the kernel thread */
6620da75
DG
4795 if (is_root && !opt_no_kernel) {
4796 if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) {
4797 goto exit;
4798 }
7a485870
DG
4799 }
4800
0b2dc8df
MD
4801 /* Setup the thread ht_cleanup communication pipe. */
4802 if (utils_create_pipe_cloexec(ht_cleanup_pipe) < 0) {
4803 goto exit;
4804 }
4805
099e26bd 4806 /* Setup the thread apps communication pipe. */
ef599319 4807 if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) {
099e26bd
DG
4808 goto exit;
4809 }
4810
d0b96690
DG
4811 /* Setup the thread apps notify communication pipe. */
4812 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe) < 0) {
4813 goto exit;
4814 }
4815
7972aab2
DG
4816 /* Initialize global buffer per UID and PID registry. */
4817 buffer_reg_init_uid_registry();
4818 buffer_reg_init_pid_registry();
4819
099e26bd
DG
4820 /* Init UST command queue. */
4821 cds_wfq_init(&ust_cmd_queue.queue);
4822
273ea72c 4823 /*
54d01ffb
DG
4824 * Get session list pointer. This pointer MUST NOT be free(). This list is
4825 * statically declared in session.c
273ea72c 4826 */
54d01ffb 4827 session_list_ptr = session_get_list();
b5541356 4828
5eb91c98
DG
4829 /* Set up max poll set size */
4830 lttng_poll_set_max_size();
4831
2f77fc4b 4832 cmd_init();
00e2e675 4833
ae9e45b3
DG
4834 /* Check for the application socket timeout env variable. */
4835 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
4836 if (env_app_timeout) {
4837 app_socket_timeout = atoi(env_app_timeout);
4838 } else {
4839 app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
4840 }
4841
35f90c40 4842 write_pidfile();
cd9290dd 4843 write_julport();
35f90c40 4844
554831e7
MD
4845 /* Initialize communication library */
4846 lttcomm_init();
d831c249
DG
4847 /* This is to get the TCP timeout value. */
4848 lttcomm_inet_init();
554831e7 4849
67e05644
DG
4850 /*
4851 * Initialize the health check subsystem. This call should set the
4852 * appropriate time values.
4853 */
6c71277b 4854 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
8782cc74
MD
4855 if (!health_sessiond) {
4856 PERROR("health_app_create error");
4857 goto exit_health_sessiond_cleanup;
4858 }
67e05644 4859
d5b9fd2f 4860 /* Create thread to clean up RCU hash tables */
0b2dc8df
MD
4861 ret = pthread_create(&ht_cleanup_thread, NULL,
4862 thread_ht_cleanup, (void *) NULL);
4863 if (ret != 0) {
4864 PERROR("pthread_create ht_cleanup");
4865 goto exit_ht_cleanup;
4866 }
4867
d5b9fd2f 4868 /* Create health-check thread */
44a5e5eb
DG
4869 ret = pthread_create(&health_thread, NULL,
4870 thread_manage_health, (void *) NULL);
4871 if (ret != 0) {
4872 PERROR("pthread_create health");
4873 goto exit_health;
4874 }
4875
cf3af59e 4876 /* Create thread to manage the client socket */
099e26bd
DG
4877 ret = pthread_create(&client_thread, NULL,
4878 thread_manage_clients, (void *) NULL);
cf3af59e 4879 if (ret != 0) {
76d7553f 4880 PERROR("pthread_create clients");
cf3af59e
MD
4881 goto exit_client;
4882 }
fac6795d 4883
099e26bd
DG
4884 /* Create thread to dispatch registration */
4885 ret = pthread_create(&dispatch_thread, NULL,
4886 thread_dispatch_ust_registration, (void *) NULL);
4887 if (ret != 0) {
76d7553f 4888 PERROR("pthread_create dispatch");
099e26bd
DG
4889 goto exit_dispatch;
4890 }
4891
4892 /* Create thread to manage application registration. */
4893 ret = pthread_create(&reg_apps_thread, NULL,
4894 thread_registration_apps, (void *) NULL);
4895 if (ret != 0) {
76d7553f 4896 PERROR("pthread_create registration");
099e26bd
DG
4897 goto exit_reg_apps;
4898 }
4899
cf3af59e 4900 /* Create thread to manage application socket */
54d01ffb
DG
4901 ret = pthread_create(&apps_thread, NULL,
4902 thread_manage_apps, (void *) NULL);
cf3af59e 4903 if (ret != 0) {
d0b96690
DG
4904 PERROR("pthread_create apps");
4905 goto exit_apps;
4906 }
4907
4908 /* Create thread to manage application notify socket */
4909 ret = pthread_create(&apps_notify_thread, NULL,
4910 ust_thread_manage_notify, (void *) NULL);
4911 if (ret != 0) {
76d7553f 4912 PERROR("pthread_create apps");
9563b0ad 4913 goto exit_apps_notify;
cf3af59e 4914 }
fac6795d 4915
4d076222
DG
4916 /* Create JUL registration thread. */
4917 ret = pthread_create(&jul_reg_thread, NULL,
4918 jul_thread_manage_registration, (void *) NULL);
4919 if (ret != 0) {
4920 PERROR("pthread_create apps");
4921 goto exit_jul_reg;
4922 }
4923
6620da75
DG
4924 /* Don't start this thread if kernel tracing is not requested nor root */
4925 if (is_root && !opt_no_kernel) {
4926 /* Create kernel thread to manage kernel event */
4927 ret = pthread_create(&kernel_thread, NULL,
4928 thread_manage_kernel, (void *) NULL);
4929 if (ret != 0) {
4930 PERROR("pthread_create kernel");
4931 goto exit_kernel;
4932 }
7a485870 4933
6620da75
DG
4934 ret = pthread_join(kernel_thread, &status);
4935 if (ret != 0) {
4936 PERROR("pthread_join");
4937 goto error; /* join error, exit without cleanup */
4938 }
fac6795d
DG
4939 }
4940
cf3af59e 4941exit_kernel:
4d076222
DG
4942 ret = pthread_join(jul_reg_thread, &status);
4943 if (ret != 0) {
4944 PERROR("pthread_join JUL");
4945 goto error; /* join error, exit without cleanup */
4946 }
4947
4948exit_jul_reg:
9563b0ad
DG
4949 ret = pthread_join(apps_notify_thread, &status);
4950 if (ret != 0) {
4951 PERROR("pthread_join apps notify");
4952 goto error; /* join error, exit without cleanup */
4953 }
4954
4955exit_apps_notify:
cf3af59e
MD
4956 ret = pthread_join(apps_thread, &status);
4957 if (ret != 0) {
9563b0ad 4958 PERROR("pthread_join apps");
cf3af59e
MD
4959 goto error; /* join error, exit without cleanup */
4960 }
fac6795d 4961
4ccd8c8f 4962
cf3af59e 4963exit_apps:
099e26bd
DG
4964 ret = pthread_join(reg_apps_thread, &status);
4965 if (ret != 0) {
76d7553f 4966 PERROR("pthread_join");
099e26bd
DG
4967 goto error; /* join error, exit without cleanup */
4968 }
4969
4970exit_reg_apps:
4971 ret = pthread_join(dispatch_thread, &status);
4972 if (ret != 0) {
76d7553f 4973 PERROR("pthread_join");
099e26bd
DG
4974 goto error; /* join error, exit without cleanup */
4975 }
4976
4977exit_dispatch:
cf3af59e
MD
4978 ret = pthread_join(client_thread, &status);
4979 if (ret != 0) {
76d7553f 4980 PERROR("pthread_join");
cf3af59e
MD
4981 goto error; /* join error, exit without cleanup */
4982 }
4983
3bd1e081 4984 ret = join_consumer_thread(&kconsumer_data);
cf3af59e 4985 if (ret != 0) {
76d7553f 4986 PERROR("join_consumer");
cf3af59e
MD
4987 goto error; /* join error, exit without cleanup */
4988 }
a88df331 4989
06f525de
DG
4990 ret = join_consumer_thread(&ustconsumer32_data);
4991 if (ret != 0) {
4992 PERROR("join_consumer ust32");
4993 goto error; /* join error, exit without cleanup */
4994 }
4995
4996 ret = join_consumer_thread(&ustconsumer64_data);
4997 if (ret != 0) {
4998 PERROR("join_consumer ust64");
4999 goto error; /* join error, exit without cleanup */
5000 }
5001
cf3af59e 5002exit_client:
06f525de
DG
5003 ret = pthread_join(health_thread, &status);
5004 if (ret != 0) {
5005 PERROR("pthread_join health thread");
5006 goto error; /* join error, exit without cleanup */
5007 }
5008
44a5e5eb 5009exit_health:
0b2dc8df
MD
5010 ret = pthread_join(ht_cleanup_thread, &status);
5011 if (ret != 0) {
5012 PERROR("pthread_join ht cleanup thread");
5013 goto error; /* join error, exit without cleanup */
5014 }
5015exit_ht_cleanup:
8782cc74
MD
5016 health_app_destroy(health_sessiond);
5017exit_health_sessiond_cleanup:
a88df331 5018exit:
cf3af59e
MD
5019 /*
5020 * cleanup() is called when no other thread is running.
5021 */
f6a9efaa 5022 rcu_thread_online();
cf3af59e 5023 cleanup();
f6a9efaa
DG
5024 rcu_thread_offline();
5025 rcu_unregister_thread();
67e40797 5026 if (!ret) {
cf3af59e 5027 exit(EXIT_SUCCESS);
67e40797 5028 }
cf3af59e 5029error:
5e16da05 5030 exit(EXIT_FAILURE);
fac6795d 5031}
This page took 0.441801 seconds and 4 git commands to generate.