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