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