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