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