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