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