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