Create all trace directories and files with client user credentials
[lttng-tools.git] / lttng-sessiond / main.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
8 *
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.
13 *
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.
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>
25 #include <semaphore.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/mman.h>
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #include <urcu/futex.h>
38 #include <unistd.h>
39 #include <config.h>
40
41 #include <lttng-consumerd.h>
42 #include <lttng-sessiond-comm.h>
43 #include <lttng/lttng-consumer.h>
44
45 #include <lttngerr.h>
46
47 #include "channel.h"
48 #include "compat/poll.h"
49 #include "context.h"
50 #include "event.h"
51 #include "futex.h"
52 #include "hashtable.h"
53 #include "kernel.h"
54 #include "lttng-sessiond.h"
55 #include "shm.h"
56 #include "ust-app.h"
57 #include "ust-ctl.h"
58 #include "utils.h"
59
60 #define CONSUMERD_FILE "lttng-consumerd"
61
62 struct consumer_data {
63 enum lttng_consumer_type type;
64
65 pthread_t thread; /* Worker thread interacting with the consumer */
66 sem_t sem;
67
68 /* Mutex to control consumerd pid assignation */
69 pthread_mutex_t pid_mutex;
70 pid_t pid;
71
72 int err_sock;
73 int cmd_sock;
74
75 /* consumer error and command Unix socket path */
76 char err_unix_sock_path[PATH_MAX];
77 char cmd_unix_sock_path[PATH_MAX];
78 };
79
80 /* Const values */
81 const char default_home_dir[] = DEFAULT_HOME_DIR;
82 const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP;
83 const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
84 const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
85
86 /* Variables */
87 int opt_verbose; /* Not static for lttngerr.h */
88 int opt_verbose_consumer; /* Not static for lttngerr.h */
89 int opt_quiet; /* Not static for lttngerr.h */
90
91 const char *progname;
92 const char *opt_tracing_group;
93 static int opt_sig_parent;
94 static int opt_daemon;
95 static int opt_no_kernel;
96 static int is_root; /* Set to 1 if the daemon is running as root */
97 static pid_t ppid; /* Parent PID for --sig-parent option */
98 static char *rundir;
99
100 /* Consumer daemon specific control data */
101 static struct consumer_data kconsumer_data = {
102 .type = LTTNG_CONSUMER_KERNEL,
103 .err_unix_sock_path = KCONSUMERD_ERR_SOCK_PATH,
104 .cmd_unix_sock_path = KCONSUMERD_CMD_SOCK_PATH,
105 };
106 static struct consumer_data ustconsumer64_data = {
107 .type = LTTNG_CONSUMER64_UST,
108 .err_unix_sock_path = USTCONSUMERD64_ERR_SOCK_PATH,
109 .cmd_unix_sock_path = USTCONSUMERD64_CMD_SOCK_PATH,
110 };
111 static struct consumer_data ustconsumer32_data = {
112 .type = LTTNG_CONSUMER32_UST,
113 .err_unix_sock_path = USTCONSUMERD32_ERR_SOCK_PATH,
114 .cmd_unix_sock_path = USTCONSUMERD32_CMD_SOCK_PATH,
115 };
116
117 static int dispatch_thread_exit;
118
119 /* Global application Unix socket path */
120 static char apps_unix_sock_path[PATH_MAX];
121 /* Global client Unix socket path */
122 static char client_unix_sock_path[PATH_MAX];
123 /* global wait shm path for UST */
124 static char wait_shm_path[PATH_MAX];
125
126 /* Sockets and FDs */
127 static int client_sock;
128 static int apps_sock;
129 static int kernel_tracer_fd;
130 static int kernel_poll_pipe[2];
131
132 /*
133 * Quit pipe for all threads. This permits a single cancellation point
134 * for all threads when receiving an event on the pipe.
135 */
136 static int thread_quit_pipe[2];
137
138 /*
139 * This pipe is used to inform the thread managing application communication
140 * that a command is queued and ready to be processed.
141 */
142 static int apps_cmd_pipe[2];
143
144 /* Pthread, Mutexes and Semaphores */
145 static pthread_t apps_thread;
146 static pthread_t reg_apps_thread;
147 static pthread_t client_thread;
148 static pthread_t kernel_thread;
149 static pthread_t dispatch_thread;
150
151
152 /*
153 * UST registration command queue. This queue is tied with a futex and uses a N
154 * wakers / 1 waiter implemented and detailed in futex.c/.h
155 *
156 * The thread_manage_apps and thread_dispatch_ust_registration interact with
157 * this queue and the wait/wake scheme.
158 */
159 static struct ust_cmd_queue ust_cmd_queue;
160
161 /*
162 * Pointer initialized before thread creation.
163 *
164 * This points to the tracing session list containing the session count and a
165 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
166 * MUST NOT be taken if you call a public function in session.c.
167 *
168 * The lock is nested inside the structure: session_list_ptr->lock. Please use
169 * session_lock_list and session_unlock_list for lock acquisition.
170 */
171 static struct ltt_session_list *session_list_ptr;
172
173 int ust_consumerd64_fd = -1;
174 int ust_consumerd32_fd = -1;
175
176 static const char *consumerd32_bin =
177 __stringify(CONFIG_CONSUMERD32_BIN);
178 static const char *consumerd64_bin =
179 __stringify(CONFIG_CONSUMERD64_BIN);
180 static const char *consumerd32_libdir =
181 __stringify(CONFIG_CONSUMERD32_LIBDIR);
182 static const char *consumerd64_libdir =
183 __stringify(CONFIG_CONSUMERD64_LIBDIR);
184
185 static
186 void setup_consumerd_path(void)
187 {
188 const char *bin, *libdir;
189
190 /*
191 * Allow INSTALL_BIN_PATH to be used as a target path for the
192 * native architecture size consumer if CONFIG_CONSUMER*_PATH
193 * has not been defined.
194 */
195 #if (CAA_BITS_PER_LONG == 32)
196 if (!consumerd32_bin[0]) {
197 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
198 }
199 if (!consumerd32_libdir[0]) {
200 consumerd32_libdir = INSTALL_LIB_PATH;
201 }
202 #elif (CAA_BITS_PER_LONG == 64)
203 if (!consumerd64_bin[0]) {
204 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
205 }
206 if (!consumerd64_libdir[0]) {
207 consumerd64_libdir = INSTALL_LIB_PATH;
208 }
209 #else
210 #error "Unknown bitness"
211 #endif
212
213 /*
214 * runtime env. var. overrides the build default.
215 */
216 bin = getenv("LTTNG_CONSUMERD32_BIN");
217 if (bin) {
218 consumerd32_bin = bin;
219 }
220 bin = getenv("LTTNG_CONSUMERD64_BIN");
221 if (bin) {
222 consumerd64_bin = bin;
223 }
224 libdir = getenv("LTTNG_TOOLS_CONSUMERD32_LIBDIR");
225 if (libdir) {
226 consumerd32_libdir = libdir;
227 }
228 libdir = getenv("LTTNG_TOOLS_CONSUMERD64_LIBDIR");
229 if (libdir) {
230 consumerd64_libdir = libdir;
231 }
232 }
233
234 /*
235 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
236 */
237 static int create_thread_poll_set(struct lttng_poll_event *events,
238 unsigned int size)
239 {
240 int ret;
241
242 if (events == NULL || size == 0) {
243 ret = -1;
244 goto error;
245 }
246
247 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
248 if (ret < 0) {
249 goto error;
250 }
251
252 /* Add quit pipe */
253 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN);
254 if (ret < 0) {
255 goto error;
256 }
257
258 return 0;
259
260 error:
261 return ret;
262 }
263
264 /*
265 * Check if the thread quit pipe was triggered.
266 *
267 * Return 1 if it was triggered else 0;
268 */
269 static int check_thread_quit_pipe(int fd, uint32_t events)
270 {
271 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
272 return 1;
273 }
274
275 return 0;
276 }
277
278 /*
279 * Remove modules in reverse load order.
280 */
281 static int modprobe_remove_kernel_modules(void)
282 {
283 int ret = 0, i;
284 char modprobe[256];
285
286 for (i = ARRAY_SIZE(kernel_modules_list) - 1; i >= 0; i--) {
287 ret = snprintf(modprobe, sizeof(modprobe),
288 "/sbin/modprobe -r -q %s",
289 kernel_modules_list[i].name);
290 if (ret < 0) {
291 perror("snprintf modprobe -r");
292 goto error;
293 }
294 modprobe[sizeof(modprobe) - 1] = '\0';
295 ret = system(modprobe);
296 if (ret == -1) {
297 ERR("Unable to launch modprobe -r for module %s",
298 kernel_modules_list[i].name);
299 } else if (kernel_modules_list[i].required
300 && WEXITSTATUS(ret) != 0) {
301 ERR("Unable to remove module %s",
302 kernel_modules_list[i].name);
303 } else {
304 DBG("Modprobe removal successful %s",
305 kernel_modules_list[i].name);
306 }
307 }
308
309 error:
310 return ret;
311 }
312
313 /*
314 * Return group ID of the tracing group or -1 if not found.
315 */
316 static gid_t allowed_group(void)
317 {
318 struct group *grp;
319
320 if (opt_tracing_group) {
321 grp = getgrnam(opt_tracing_group);
322 } else {
323 grp = getgrnam(default_tracing_group);
324 }
325 if (!grp) {
326 return -1;
327 } else {
328 return grp->gr_gid;
329 }
330 }
331
332 /*
333 * Init thread quit pipe.
334 *
335 * Return -1 on error or 0 if all pipes are created.
336 */
337 static int init_thread_quit_pipe(void)
338 {
339 int ret;
340
341 ret = pipe2(thread_quit_pipe, O_CLOEXEC);
342 if (ret < 0) {
343 perror("thread quit pipe");
344 goto error;
345 }
346
347 error:
348 return ret;
349 }
350
351 /*
352 * Complete teardown of a kernel session. This free all data structure related
353 * to a kernel session and update counter.
354 */
355 static void teardown_kernel_session(struct ltt_session *session)
356 {
357 if (!session->kernel_session) {
358 DBG3("No kernel session when tearingdown session");
359 return;
360 }
361
362 DBG("Tearing down kernel session");
363
364 /*
365 * If a custom kernel consumer was registered, close the socket before
366 * tearing down the complete kernel session structure
367 */
368 if (session->kernel_session->consumer_fd != kconsumer_data.cmd_sock) {
369 lttcomm_close_unix_sock(session->kernel_session->consumer_fd);
370 }
371
372 trace_kernel_destroy_session(session->kernel_session);
373 }
374
375 /*
376 * Complete teardown of all UST sessions. This will free everything on his path
377 * and destroy the core essence of all ust sessions :)
378 */
379 static void teardown_ust_session(struct ltt_session *session)
380 {
381 int ret;
382
383 if (!session->ust_session) {
384 DBG3("No UST session when tearingdown session");
385 return;
386 }
387
388 DBG("Tearing down UST session(s)");
389
390 ret = ust_app_destroy_trace_all(session->ust_session);
391 if (ret) {
392 ERR("Error in ust_app_destroy_trace_all");
393 }
394
395 trace_ust_destroy_session(session->ust_session);
396 }
397
398 /*
399 * Stop all threads by closing the thread quit pipe.
400 */
401 static void stop_threads(void)
402 {
403 int ret;
404
405 /* Stopping all threads */
406 DBG("Terminating all threads");
407 ret = notify_thread_pipe(thread_quit_pipe[1]);
408 if (ret < 0) {
409 ERR("write error on thread quit pipe");
410 }
411
412 /* Dispatch thread */
413 dispatch_thread_exit = 1;
414 futex_nto1_wake(&ust_cmd_queue.futex);
415 }
416
417 /*
418 * Cleanup the daemon
419 */
420 static void cleanup(void)
421 {
422 int ret;
423 char *cmd;
424 struct ltt_session *sess, *stmp;
425
426 DBG("Cleaning up");
427
428 DBG("Removing %s directory", rundir);
429 ret = asprintf(&cmd, "rm -rf %s", rundir);
430 if (ret < 0) {
431 ERR("asprintf failed. Something is really wrong!");
432 }
433
434 /* Remove lttng run directory */
435 ret = system(cmd);
436 if (ret < 0) {
437 ERR("Unable to clean %s", rundir);
438 }
439 free(cmd);
440
441 DBG("Cleaning up all session");
442
443 /* Destroy session list mutex */
444 if (session_list_ptr != NULL) {
445 pthread_mutex_destroy(&session_list_ptr->lock);
446
447 /* Cleanup ALL session */
448 cds_list_for_each_entry_safe(sess, stmp,
449 &session_list_ptr->head, list) {
450 teardown_kernel_session(sess);
451 teardown_ust_session(sess);
452 free(sess);
453 }
454 }
455
456 DBG("Closing all UST sockets");
457 ust_app_clean_list();
458
459 pthread_mutex_destroy(&kconsumer_data.pid_mutex);
460
461 if (is_root && !opt_no_kernel) {
462 DBG2("Closing kernel fd");
463 close(kernel_tracer_fd);
464 DBG("Unloading kernel modules");
465 modprobe_remove_kernel_modules();
466 }
467
468 close(thread_quit_pipe[0]);
469 close(thread_quit_pipe[1]);
470
471 /* <fun> */
472 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
473 "Matthew, BEET driven development works!%c[%dm",
474 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
475 /* </fun> */
476 }
477
478 /*
479 * Send data on a unix socket using the liblttsessiondcomm API.
480 *
481 * Return lttcomm error code.
482 */
483 static int send_unix_sock(int sock, void *buf, size_t len)
484 {
485 /* Check valid length */
486 if (len <= 0) {
487 return -1;
488 }
489
490 return lttcomm_send_unix_sock(sock, buf, len);
491 }
492
493 /*
494 * Free memory of a command context structure.
495 */
496 static void clean_command_ctx(struct command_ctx **cmd_ctx)
497 {
498 DBG("Clean command context structure");
499 if (*cmd_ctx) {
500 if ((*cmd_ctx)->llm) {
501 free((*cmd_ctx)->llm);
502 }
503 if ((*cmd_ctx)->lsm) {
504 free((*cmd_ctx)->lsm);
505 }
506 free(*cmd_ctx);
507 *cmd_ctx = NULL;
508 }
509 }
510
511 /*
512 * Send all stream fds of kernel channel to the consumer.
513 */
514 static int send_kconsumer_channel_streams(struct consumer_data *consumer_data,
515 int sock, struct ltt_kernel_channel *channel,
516 uid_t uid, gid_t gid)
517 {
518 int ret;
519 struct ltt_kernel_stream *stream;
520 struct lttcomm_consumer_msg lkm;
521
522 DBG("Sending streams of channel %s to kernel consumer",
523 channel->channel->name);
524
525 /* Send channel */
526 lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
527 lkm.u.channel.channel_key = channel->fd;
528 lkm.u.channel.max_sb_size = channel->channel->attr.subbuf_size;
529 lkm.u.channel.mmap_len = 0; /* for kernel */
530 DBG("Sending channel %d to consumer", lkm.u.channel.channel_key);
531 ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
532 if (ret < 0) {
533 perror("send consumer channel");
534 goto error;
535 }
536
537 /* Send streams */
538 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
539 if (!stream->fd) {
540 continue;
541 }
542 lkm.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
543 lkm.u.stream.channel_key = channel->fd;
544 lkm.u.stream.stream_key = stream->fd;
545 lkm.u.stream.state = stream->state;
546 lkm.u.stream.output = channel->channel->attr.output;
547 lkm.u.stream.mmap_len = 0; /* for kernel */
548 lkm.u.stream.uid = uid;
549 lkm.u.stream.gid = gid;
550 strncpy(lkm.u.stream.path_name, stream->pathname, PATH_MAX - 1);
551 lkm.u.stream.path_name[PATH_MAX - 1] = '\0';
552 DBG("Sending stream %d to consumer", lkm.u.stream.stream_key);
553 ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
554 if (ret < 0) {
555 perror("send consumer stream");
556 goto error;
557 }
558 ret = lttcomm_send_fds_unix_sock(sock, &stream->fd, 1);
559 if (ret < 0) {
560 perror("send consumer stream ancillary data");
561 goto error;
562 }
563 }
564
565 DBG("consumer channel streams sent");
566
567 return 0;
568
569 error:
570 return ret;
571 }
572
573 /*
574 * Send all stream fds of the kernel session to the consumer.
575 */
576 static int send_kconsumer_session_streams(struct consumer_data *consumer_data,
577 struct ltt_kernel_session *session)
578 {
579 int ret;
580 struct ltt_kernel_channel *chan;
581 struct lttcomm_consumer_msg lkm;
582 int sock = session->consumer_fd;
583
584 DBG("Sending metadata stream fd");
585
586 /* Extra protection. It's NOT supposed to be set to 0 at this point */
587 if (session->consumer_fd == 0) {
588 session->consumer_fd = consumer_data->cmd_sock;
589 }
590
591 if (session->metadata_stream_fd != 0) {
592 /* Send metadata channel fd */
593 lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
594 lkm.u.channel.channel_key = session->metadata->fd;
595 lkm.u.channel.max_sb_size = session->metadata->conf->attr.subbuf_size;
596 lkm.u.channel.mmap_len = 0; /* for kernel */
597 DBG("Sending metadata channel %d to consumer", lkm.u.stream.stream_key);
598 ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
599 if (ret < 0) {
600 perror("send consumer channel");
601 goto error;
602 }
603
604 /* Send metadata stream fd */
605 lkm.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
606 lkm.u.stream.channel_key = session->metadata->fd;
607 lkm.u.stream.stream_key = session->metadata_stream_fd;
608 lkm.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
609 lkm.u.stream.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
610 lkm.u.stream.mmap_len = 0; /* for kernel */
611 lkm.u.stream.uid = session->uid;
612 lkm.u.stream.gid = session->gid;
613 strncpy(lkm.u.stream.path_name, session->metadata->pathname, PATH_MAX - 1);
614 lkm.u.stream.path_name[PATH_MAX - 1] = '\0';
615 DBG("Sending metadata stream %d to consumer", lkm.u.stream.stream_key);
616 ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm));
617 if (ret < 0) {
618 perror("send consumer stream");
619 goto error;
620 }
621 ret = lttcomm_send_fds_unix_sock(sock, &session->metadata_stream_fd, 1);
622 if (ret < 0) {
623 perror("send consumer stream");
624 goto error;
625 }
626 }
627
628 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
629 ret = send_kconsumer_channel_streams(consumer_data, sock, chan,
630 session->uid, session->gid);
631 if (ret < 0) {
632 goto error;
633 }
634 }
635
636 DBG("consumer fds (metadata and channel streams) sent");
637
638 return 0;
639
640 error:
641 return ret;
642 }
643
644 /*
645 * Notify UST applications using the shm mmap futex.
646 */
647 static int notify_ust_apps(int active)
648 {
649 char *wait_shm_mmap;
650
651 DBG("Notifying applications of session daemon state: %d", active);
652
653 /* See shm.c for this call implying mmap, shm and futex calls */
654 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
655 if (wait_shm_mmap == NULL) {
656 goto error;
657 }
658
659 /* Wake waiting process */
660 futex_wait_update((int32_t *) wait_shm_mmap, active);
661
662 /* Apps notified successfully */
663 return 0;
664
665 error:
666 return -1;
667 }
668
669 /*
670 * Setup the outgoing data buffer for the response (llm) by allocating the
671 * right amount of memory and copying the original information from the lsm
672 * structure.
673 *
674 * Return total size of the buffer pointed by buf.
675 */
676 static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
677 {
678 int ret, buf_size;
679
680 buf_size = size;
681
682 cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
683 if (cmd_ctx->llm == NULL) {
684 perror("zmalloc");
685 ret = -ENOMEM;
686 goto error;
687 }
688
689 /* Copy common data */
690 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
691 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
692
693 cmd_ctx->llm->data_size = size;
694 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
695
696 return buf_size;
697
698 error:
699 return ret;
700 }
701
702 /*
703 * Update the kernel poll set of all channel fd available over all tracing
704 * session. Add the wakeup pipe at the end of the set.
705 */
706 static int update_kernel_poll(struct lttng_poll_event *events)
707 {
708 int ret;
709 struct ltt_session *session;
710 struct ltt_kernel_channel *channel;
711
712 DBG("Updating kernel poll set");
713
714 session_lock_list();
715 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
716 session_lock(session);
717 if (session->kernel_session == NULL) {
718 session_unlock(session);
719 continue;
720 }
721
722 cds_list_for_each_entry(channel,
723 &session->kernel_session->channel_list.head, list) {
724 /* Add channel fd to the kernel poll set */
725 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
726 if (ret < 0) {
727 session_unlock(session);
728 goto error;
729 }
730 DBG("Channel fd %d added to kernel set", channel->fd);
731 }
732 session_unlock(session);
733 }
734 session_unlock_list();
735
736 return 0;
737
738 error:
739 session_unlock_list();
740 return -1;
741 }
742
743 /*
744 * Find the channel fd from 'fd' over all tracing session. When found, check
745 * for new channel stream and send those stream fds to the kernel consumer.
746 *
747 * Useful for CPU hotplug feature.
748 */
749 static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
750 {
751 int ret = 0;
752 struct ltt_session *session;
753 struct ltt_kernel_channel *channel;
754
755 DBG("Updating kernel streams for channel fd %d", fd);
756
757 session_lock_list();
758 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
759 session_lock(session);
760 if (session->kernel_session == NULL) {
761 session_unlock(session);
762 continue;
763 }
764
765 /* This is not suppose to be 0 but this is an extra security check */
766 if (session->kernel_session->consumer_fd == 0) {
767 session->kernel_session->consumer_fd = consumer_data->cmd_sock;
768 }
769
770 cds_list_for_each_entry(channel,
771 &session->kernel_session->channel_list.head, list) {
772 if (channel->fd == fd) {
773 DBG("Channel found, updating kernel streams");
774 ret = kernel_open_channel_stream(channel);
775 if (ret < 0) {
776 goto error;
777 }
778
779 /*
780 * Have we already sent fds to the consumer? If yes, it means
781 * that tracing is started so it is safe to send our updated
782 * stream fds.
783 */
784 if (session->kernel_session->consumer_fds_sent == 1) {
785 ret = send_kconsumer_channel_streams(consumer_data,
786 session->kernel_session->consumer_fd, channel,
787 session->uid, session->gid);
788 if (ret < 0) {
789 goto error;
790 }
791 }
792 goto error;
793 }
794 }
795 session_unlock(session);
796 }
797 session_unlock_list();
798 return ret;
799
800 error:
801 session_unlock(session);
802 session_unlock_list();
803 return ret;
804 }
805
806 /*
807 * For each tracing session, update newly registered apps.
808 */
809 static void update_ust_app(int app_sock)
810 {
811 struct ltt_session *sess, *stmp;
812
813 /* For all tracing session(s) */
814 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
815 if (sess->ust_session) {
816 ust_app_global_update(sess->ust_session, app_sock);
817 }
818 }
819 }
820
821 /*
822 * This thread manage event coming from the kernel.
823 *
824 * Features supported in this thread:
825 * -) CPU Hotplug
826 */
827 static void *thread_manage_kernel(void *data)
828 {
829 int ret, i, pollfd, update_poll_flag = 1;
830 uint32_t revents, nb_fd;
831 char tmp;
832 struct lttng_poll_event events;
833
834 DBG("Thread manage kernel started");
835
836 ret = create_thread_poll_set(&events, 2);
837 if (ret < 0) {
838 goto error;
839 }
840
841 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
842 if (ret < 0) {
843 goto error;
844 }
845
846 while (1) {
847 if (update_poll_flag == 1) {
848 /*
849 * Reset number of fd in the poll set. Always 2 since there is the thread
850 * quit pipe and the kernel pipe.
851 */
852 events.nb_fd = 2;
853
854 ret = update_kernel_poll(&events);
855 if (ret < 0) {
856 goto error;
857 }
858 update_poll_flag = 0;
859 }
860
861 nb_fd = LTTNG_POLL_GETNB(&events);
862
863 DBG("Thread kernel polling on %d fds", nb_fd);
864
865 /* Zeroed the poll events */
866 lttng_poll_reset(&events);
867
868 /* Poll infinite value of time */
869 ret = lttng_poll_wait(&events, -1);
870 if (ret < 0) {
871 goto error;
872 } else if (ret == 0) {
873 /* Should not happen since timeout is infinite */
874 ERR("Return value of poll is 0 with an infinite timeout.\n"
875 "This should not have happened! Continuing...");
876 continue;
877 }
878
879 for (i = 0; i < nb_fd; i++) {
880 /* Fetch once the poll data */
881 revents = LTTNG_POLL_GETEV(&events, i);
882 pollfd = LTTNG_POLL_GETFD(&events, i);
883
884 /* Thread quit pipe has been closed. Killing thread. */
885 ret = check_thread_quit_pipe(pollfd, revents);
886 if (ret) {
887 goto error;
888 }
889
890 /* Check for data on kernel pipe */
891 if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
892 ret = read(kernel_poll_pipe[0], &tmp, 1);
893 update_poll_flag = 1;
894 continue;
895 } else {
896 /*
897 * New CPU detected by the kernel. Adding kernel stream to
898 * kernel session and updating the kernel consumer
899 */
900 if (revents & LPOLLIN) {
901 ret = update_kernel_stream(&kconsumer_data, pollfd);
902 if (ret < 0) {
903 continue;
904 }
905 break;
906 /*
907 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
908 * and unregister kernel stream at this point.
909 */
910 }
911 }
912 }
913 }
914
915 error:
916 DBG("Kernel thread dying");
917 close(kernel_poll_pipe[0]);
918 close(kernel_poll_pipe[1]);
919
920 lttng_poll_clean(&events);
921
922 return NULL;
923 }
924
925 /*
926 * This thread manage the consumer error sent back to the session daemon.
927 */
928 static void *thread_manage_consumer(void *data)
929 {
930 int sock = 0, i, ret, pollfd;
931 uint32_t revents, nb_fd;
932 enum lttcomm_return_code code;
933 struct lttng_poll_event events;
934 struct consumer_data *consumer_data = data;
935
936 DBG("[thread] Manage consumer started");
937
938 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
939 if (ret < 0) {
940 goto error;
941 }
942
943 /*
944 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
945 * Nothing more will be added to this poll set.
946 */
947 ret = create_thread_poll_set(&events, 2);
948 if (ret < 0) {
949 goto error;
950 }
951
952 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
953 if (ret < 0) {
954 goto error;
955 }
956
957 nb_fd = LTTNG_POLL_GETNB(&events);
958
959 /* Inifinite blocking call, waiting for transmission */
960 ret = lttng_poll_wait(&events, -1);
961 if (ret < 0) {
962 goto error;
963 }
964
965 for (i = 0; i < nb_fd; i++) {
966 /* Fetch once the poll data */
967 revents = LTTNG_POLL_GETEV(&events, i);
968 pollfd = LTTNG_POLL_GETFD(&events, i);
969
970 /* Thread quit pipe has been closed. Killing thread. */
971 ret = check_thread_quit_pipe(pollfd, revents);
972 if (ret) {
973 goto error;
974 }
975
976 /* Event on the registration socket */
977 if (pollfd == consumer_data->err_sock) {
978 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
979 ERR("consumer err socket poll error");
980 goto error;
981 }
982 }
983 }
984
985 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
986 if (sock < 0) {
987 goto error;
988 }
989
990 DBG2("Receiving code from consumer err_sock");
991
992 /* Getting status code from kconsumerd */
993 ret = lttcomm_recv_unix_sock(sock, &code,
994 sizeof(enum lttcomm_return_code));
995 if (ret <= 0) {
996 goto error;
997 }
998
999 if (code == CONSUMERD_COMMAND_SOCK_READY) {
1000 consumer_data->cmd_sock =
1001 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
1002 if (consumer_data->cmd_sock < 0) {
1003 sem_post(&consumer_data->sem);
1004 PERROR("consumer connect");
1005 goto error;
1006 }
1007 /* Signal condition to tell that the kconsumerd is ready */
1008 sem_post(&consumer_data->sem);
1009 DBG("consumer command socket ready");
1010 } else {
1011 ERR("consumer error when waiting for SOCK_READY : %s",
1012 lttcomm_get_readable_code(-code));
1013 goto error;
1014 }
1015
1016 /* Remove the kconsumerd error sock since we've established a connexion */
1017 ret = lttng_poll_del(&events, consumer_data->err_sock);
1018 if (ret < 0) {
1019 goto error;
1020 }
1021
1022 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1023 if (ret < 0) {
1024 goto error;
1025 }
1026
1027 /* Update number of fd */
1028 nb_fd = LTTNG_POLL_GETNB(&events);
1029
1030 /* Inifinite blocking call, waiting for transmission */
1031 ret = lttng_poll_wait(&events, -1);
1032 if (ret < 0) {
1033 goto error;
1034 }
1035
1036 for (i = 0; i < nb_fd; i++) {
1037 /* Fetch once the poll data */
1038 revents = LTTNG_POLL_GETEV(&events, i);
1039 pollfd = LTTNG_POLL_GETFD(&events, i);
1040
1041 /* Thread quit pipe has been closed. Killing thread. */
1042 ret = check_thread_quit_pipe(pollfd, revents);
1043 if (ret) {
1044 goto error;
1045 }
1046
1047 /* Event on the kconsumerd socket */
1048 if (pollfd == sock) {
1049 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1050 ERR("consumer err socket second poll error");
1051 goto error;
1052 }
1053 }
1054 }
1055
1056 /* Wait for any kconsumerd error */
1057 ret = lttcomm_recv_unix_sock(sock, &code,
1058 sizeof(enum lttcomm_return_code));
1059 if (ret <= 0) {
1060 ERR("consumer closed the command socket");
1061 goto error;
1062 }
1063
1064 ERR("consumer return code : %s", lttcomm_get_readable_code(-code));
1065
1066 error:
1067 DBG("consumer thread dying");
1068 close(consumer_data->err_sock);
1069 close(consumer_data->cmd_sock);
1070 close(sock);
1071
1072 unlink(consumer_data->err_unix_sock_path);
1073 unlink(consumer_data->cmd_unix_sock_path);
1074 consumer_data->pid = 0;
1075
1076 lttng_poll_clean(&events);
1077
1078 return NULL;
1079 }
1080
1081 /*
1082 * This thread manage application communication.
1083 */
1084 static void *thread_manage_apps(void *data)
1085 {
1086 int i, ret, pollfd;
1087 uint32_t revents, nb_fd;
1088 struct ust_command ust_cmd;
1089 struct lttng_poll_event events;
1090
1091 DBG("[thread] Manage application started");
1092
1093 rcu_register_thread();
1094 rcu_thread_online();
1095
1096 ret = create_thread_poll_set(&events, 2);
1097 if (ret < 0) {
1098 goto error;
1099 }
1100
1101 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1102 if (ret < 0) {
1103 goto error;
1104 }
1105
1106 while (1) {
1107 /* Zeroed the events structure */
1108 lttng_poll_reset(&events);
1109
1110 nb_fd = LTTNG_POLL_GETNB(&events);
1111
1112 DBG("Apps thread polling on %d fds", nb_fd);
1113
1114 /* Inifinite blocking call, waiting for transmission */
1115 ret = lttng_poll_wait(&events, -1);
1116 if (ret < 0) {
1117 goto error;
1118 }
1119
1120 for (i = 0; i < nb_fd; i++) {
1121 /* Fetch once the poll data */
1122 revents = LTTNG_POLL_GETEV(&events, i);
1123 pollfd = LTTNG_POLL_GETFD(&events, i);
1124
1125 /* Thread quit pipe has been closed. Killing thread. */
1126 ret = check_thread_quit_pipe(pollfd, revents);
1127 if (ret) {
1128 goto error;
1129 }
1130
1131 /* Inspect the apps cmd pipe */
1132 if (pollfd == apps_cmd_pipe[0]) {
1133 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1134 ERR("Apps command pipe error");
1135 goto error;
1136 } else if (revents & LPOLLIN) {
1137 /* Empty pipe */
1138 ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd));
1139 if (ret < 0 || ret < sizeof(ust_cmd)) {
1140 perror("read apps cmd pipe");
1141 goto error;
1142 }
1143
1144 /* Register applicaton to the session daemon */
1145 ret = ust_app_register(&ust_cmd.reg_msg,
1146 ust_cmd.sock);
1147 if (ret == -ENOMEM) {
1148 goto error;
1149 } else if (ret < 0) {
1150 break;
1151 }
1152
1153 /*
1154 * Add channel(s) and event(s) to newly registered apps
1155 * from lttng global UST domain.
1156 */
1157 update_ust_app(ust_cmd.sock);
1158
1159 ret = ustctl_register_done(ust_cmd.sock);
1160 if (ret < 0) {
1161 /*
1162 * If the registration is not possible, we simply
1163 * unregister the apps and continue
1164 */
1165 ust_app_unregister(ust_cmd.sock);
1166 } else {
1167 /*
1168 * We just need here to monitor the close of the UST
1169 * socket and poll set monitor those by default.
1170 */
1171 ret = lttng_poll_add(&events, ust_cmd.sock, 0);
1172 if (ret < 0) {
1173 goto error;
1174 }
1175
1176 DBG("Apps with sock %d added to poll set",
1177 ust_cmd.sock);
1178 }
1179
1180 break;
1181 }
1182 } else {
1183 /*
1184 * At this point, we know that a registered application made
1185 * the event at poll_wait.
1186 */
1187 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1188 /* Removing from the poll set */
1189 ret = lttng_poll_del(&events, pollfd);
1190 if (ret < 0) {
1191 goto error;
1192 }
1193
1194 /* Socket closed on remote end. */
1195 ust_app_unregister(pollfd);
1196 break;
1197 }
1198 }
1199 }
1200 }
1201
1202 error:
1203 DBG("Application communication apps dying");
1204 close(apps_cmd_pipe[0]);
1205 close(apps_cmd_pipe[1]);
1206
1207 lttng_poll_clean(&events);
1208
1209 rcu_thread_offline();
1210 rcu_unregister_thread();
1211 return NULL;
1212 }
1213
1214 /*
1215 * Dispatch request from the registration threads to the application
1216 * communication thread.
1217 */
1218 static void *thread_dispatch_ust_registration(void *data)
1219 {
1220 int ret;
1221 struct cds_wfq_node *node;
1222 struct ust_command *ust_cmd = NULL;
1223
1224 DBG("[thread] Dispatch UST command started");
1225
1226 while (!dispatch_thread_exit) {
1227 /* Atomically prepare the queue futex */
1228 futex_nto1_prepare(&ust_cmd_queue.futex);
1229
1230 do {
1231 /* Dequeue command for registration */
1232 node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
1233 if (node == NULL) {
1234 DBG("Woken up but nothing in the UST command queue");
1235 /* Continue thread execution */
1236 break;
1237 }
1238
1239 ust_cmd = caa_container_of(node, struct ust_command, node);
1240
1241 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1242 " gid:%d sock:%d name:%s (version %d.%d)",
1243 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1244 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1245 ust_cmd->sock, ust_cmd->reg_msg.name,
1246 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1247 /*
1248 * Inform apps thread of the new application registration. This
1249 * call is blocking so we can be assured that the data will be read
1250 * at some point in time or wait to the end of the world :)
1251 */
1252 ret = write(apps_cmd_pipe[1], ust_cmd,
1253 sizeof(struct ust_command));
1254 if (ret < 0) {
1255 perror("write apps cmd pipe");
1256 if (errno == EBADF) {
1257 /*
1258 * We can't inform the application thread to process
1259 * registration. We will exit or else application
1260 * registration will not occur and tracing will never
1261 * start.
1262 */
1263 goto error;
1264 }
1265 }
1266 free(ust_cmd);
1267 } while (node != NULL);
1268
1269 /* Futex wait on queue. Blocking call on futex() */
1270 futex_nto1_wait(&ust_cmd_queue.futex);
1271 }
1272
1273 error:
1274 DBG("Dispatch thread dying");
1275 return NULL;
1276 }
1277
1278 /*
1279 * This thread manage application registration.
1280 */
1281 static void *thread_registration_apps(void *data)
1282 {
1283 int sock = 0, i, ret, pollfd;
1284 uint32_t revents, nb_fd;
1285 struct lttng_poll_event events;
1286 /*
1287 * Get allocated in this thread, enqueued to a global queue, dequeued and
1288 * freed in the manage apps thread.
1289 */
1290 struct ust_command *ust_cmd = NULL;
1291
1292 DBG("[thread] Manage application registration started");
1293
1294 ret = lttcomm_listen_unix_sock(apps_sock);
1295 if (ret < 0) {
1296 goto error;
1297 }
1298
1299 /*
1300 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1301 * more will be added to this poll set.
1302 */
1303 ret = create_thread_poll_set(&events, 2);
1304 if (ret < 0) {
1305 goto error;
1306 }
1307
1308 /* Add the application registration socket */
1309 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1310 if (ret < 0) {
1311 goto error;
1312 }
1313
1314 /* Notify all applications to register */
1315 ret = notify_ust_apps(1);
1316 if (ret < 0) {
1317 ERR("Failed to notify applications or create the wait shared memory.\n"
1318 "Execution continues but there might be problem for already\n"
1319 "running applications that wishes to register.");
1320 }
1321
1322 while (1) {
1323 DBG("Accepting application registration");
1324
1325 nb_fd = LTTNG_POLL_GETNB(&events);
1326
1327 /* Inifinite blocking call, waiting for transmission */
1328 ret = lttng_poll_wait(&events, -1);
1329 if (ret < 0) {
1330 goto error;
1331 }
1332
1333 for (i = 0; i < nb_fd; i++) {
1334 /* Fetch once the poll data */
1335 revents = LTTNG_POLL_GETEV(&events, i);
1336 pollfd = LTTNG_POLL_GETFD(&events, i);
1337
1338 /* Thread quit pipe has been closed. Killing thread. */
1339 ret = check_thread_quit_pipe(pollfd, revents);
1340 if (ret) {
1341 goto error;
1342 }
1343
1344 /* Event on the registration socket */
1345 if (pollfd == apps_sock) {
1346 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1347 ERR("Register apps socket poll error");
1348 goto error;
1349 } else if (revents & LPOLLIN) {
1350 sock = lttcomm_accept_unix_sock(apps_sock);
1351 if (sock < 0) {
1352 goto error;
1353 }
1354
1355 /* Create UST registration command for enqueuing */
1356 ust_cmd = zmalloc(sizeof(struct ust_command));
1357 if (ust_cmd == NULL) {
1358 perror("ust command zmalloc");
1359 goto error;
1360 }
1361
1362 /*
1363 * Using message-based transmissions to ensure we don't
1364 * have to deal with partially received messages.
1365 */
1366 ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg,
1367 sizeof(struct ust_register_msg));
1368 if (ret < 0 || ret < sizeof(struct ust_register_msg)) {
1369 if (ret < 0) {
1370 perror("lttcomm_recv_unix_sock register apps");
1371 } else {
1372 ERR("Wrong size received on apps register");
1373 }
1374 free(ust_cmd);
1375 close(sock);
1376 continue;
1377 }
1378
1379 ust_cmd->sock = sock;
1380
1381 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1382 " gid:%d sock:%d name:%s (version %d.%d)",
1383 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1384 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1385 ust_cmd->sock, ust_cmd->reg_msg.name,
1386 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1387
1388 /*
1389 * Lock free enqueue the registration request. The red pill
1390 * has been taken! This apps will be part of the *system*.
1391 */
1392 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
1393
1394 /*
1395 * Wake the registration queue futex. Implicit memory
1396 * barrier with the exchange in cds_wfq_enqueue.
1397 */
1398 futex_nto1_wake(&ust_cmd_queue.futex);
1399 }
1400 }
1401 }
1402 }
1403
1404 error:
1405 DBG("UST Registration thread dying");
1406
1407 /* Notify that the registration thread is gone */
1408 notify_ust_apps(0);
1409
1410 close(apps_sock);
1411 close(sock);
1412 unlink(apps_unix_sock_path);
1413
1414 lttng_poll_clean(&events);
1415
1416 return NULL;
1417 }
1418
1419 /*
1420 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1421 * exec or it will fails.
1422 */
1423 static int spawn_consumer_thread(struct consumer_data *consumer_data)
1424 {
1425 int ret;
1426 struct timespec timeout;
1427
1428 timeout.tv_sec = DEFAULT_SEM_WAIT_TIMEOUT;
1429 timeout.tv_nsec = 0;
1430
1431 /* Setup semaphore */
1432 ret = sem_init(&consumer_data->sem, 0, 0);
1433 if (ret < 0) {
1434 PERROR("sem_init consumer semaphore");
1435 goto error;
1436 }
1437
1438 ret = pthread_create(&consumer_data->thread, NULL,
1439 thread_manage_consumer, consumer_data);
1440 if (ret != 0) {
1441 PERROR("pthread_create consumer");
1442 ret = -1;
1443 goto error;
1444 }
1445
1446 /* Get time for sem_timedwait absolute timeout */
1447 ret = clock_gettime(CLOCK_REALTIME, &timeout);
1448 if (ret < 0) {
1449 PERROR("clock_gettime spawn consumer");
1450 /* Infinite wait for the kconsumerd thread to be ready */
1451 ret = sem_wait(&consumer_data->sem);
1452 } else {
1453 /* Normal timeout if the gettime was successful */
1454 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
1455 ret = sem_timedwait(&consumer_data->sem, &timeout);
1456 }
1457
1458 if (ret < 0) {
1459 if (errno == ETIMEDOUT) {
1460 /*
1461 * Call has timed out so we kill the kconsumerd_thread and return
1462 * an error.
1463 */
1464 ERR("The consumer thread was never ready. Killing it");
1465 ret = pthread_cancel(consumer_data->thread);
1466 if (ret < 0) {
1467 PERROR("pthread_cancel consumer thread");
1468 }
1469 } else {
1470 PERROR("semaphore wait failed consumer thread");
1471 }
1472 goto error;
1473 }
1474
1475 pthread_mutex_lock(&consumer_data->pid_mutex);
1476 if (consumer_data->pid == 0) {
1477 ERR("Kconsumerd did not start");
1478 pthread_mutex_unlock(&consumer_data->pid_mutex);
1479 goto error;
1480 }
1481 pthread_mutex_unlock(&consumer_data->pid_mutex);
1482
1483 return 0;
1484
1485 error:
1486 return ret;
1487 }
1488
1489 /*
1490 * Join consumer thread
1491 */
1492 static int join_consumer_thread(struct consumer_data *consumer_data)
1493 {
1494 void *status;
1495 int ret;
1496
1497 if (consumer_data->pid != 0) {
1498 ret = kill(consumer_data->pid, SIGTERM);
1499 if (ret) {
1500 ERR("Error killing consumer daemon");
1501 return ret;
1502 }
1503 return pthread_join(consumer_data->thread, &status);
1504 } else {
1505 return 0;
1506 }
1507 }
1508
1509 /*
1510 * Fork and exec a consumer daemon (consumerd).
1511 *
1512 * Return pid if successful else -1.
1513 */
1514 static pid_t spawn_consumerd(struct consumer_data *consumer_data)
1515 {
1516 int ret;
1517 pid_t pid;
1518 const char *consumer_to_use;
1519 const char *verbosity;
1520 struct stat st;
1521
1522 DBG("Spawning consumerd");
1523
1524 pid = fork();
1525 if (pid == 0) {
1526 /*
1527 * Exec consumerd.
1528 */
1529 if (opt_verbose > 1 || opt_verbose_consumer) {
1530 verbosity = "--verbose";
1531 } else {
1532 verbosity = "--quiet";
1533 }
1534 switch (consumer_data->type) {
1535 case LTTNG_CONSUMER_KERNEL:
1536 /*
1537 * Find out which consumerd to execute. We will
1538 * first try the 64-bit path, then the
1539 * sessiond's installation directory, and
1540 * fallback on the 32-bit one,
1541 */
1542 if (stat(consumerd64_bin, &st) == 0) {
1543 consumer_to_use = consumerd64_bin;
1544 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
1545 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
1546 } else if (stat(consumerd32_bin, &st) == 0) {
1547 consumer_to_use = consumerd32_bin;
1548 } else {
1549 break;
1550 }
1551 DBG("Using kernel consumer at: %s", consumer_to_use);
1552 execl(consumer_to_use,
1553 "lttng-consumerd", verbosity, "-k",
1554 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1555 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1556 NULL);
1557 break;
1558 case LTTNG_CONSUMER64_UST:
1559 {
1560 char *tmpnew = NULL;
1561
1562 if (consumerd64_libdir[0] != '\0') {
1563 char *tmp;
1564 size_t tmplen;
1565
1566 tmp = getenv("LD_LIBRARY_PATH");
1567 if (!tmp) {
1568 tmp = "";
1569 }
1570 tmplen = strlen("LD_LIBRARY_PATH=")
1571 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
1572 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1573 if (!tmpnew) {
1574 ret = -ENOMEM;
1575 goto error;
1576 }
1577 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1578 strcat(tmpnew, consumerd64_libdir);
1579 if (tmp[0] != '\0') {
1580 strcat(tmpnew, ":");
1581 strcat(tmpnew, tmp);
1582 }
1583 ret = putenv(tmpnew);
1584 if (ret) {
1585 ret = -errno;
1586 goto error;
1587 }
1588 }
1589 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
1590 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
1591 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1592 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1593 NULL);
1594 if (consumerd64_libdir[0] != '\0') {
1595 free(tmpnew);
1596 }
1597 if (ret) {
1598 goto error;
1599 }
1600 break;
1601 }
1602 case LTTNG_CONSUMER32_UST:
1603 {
1604 char *tmpnew = NULL;
1605
1606 if (consumerd32_libdir[0] != '\0') {
1607 char *tmp;
1608 size_t tmplen;
1609
1610 tmp = getenv("LD_LIBRARY_PATH");
1611 if (!tmp) {
1612 tmp = "";
1613 }
1614 tmplen = strlen("LD_LIBRARY_PATH=")
1615 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
1616 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1617 if (!tmpnew) {
1618 ret = -ENOMEM;
1619 goto error;
1620 }
1621 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1622 strcat(tmpnew, consumerd32_libdir);
1623 if (tmp[0] != '\0') {
1624 strcat(tmpnew, ":");
1625 strcat(tmpnew, tmp);
1626 }
1627 ret = putenv(tmpnew);
1628 if (ret) {
1629 ret = -errno;
1630 goto error;
1631 }
1632 }
1633 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
1634 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
1635 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1636 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1637 NULL);
1638 if (consumerd32_libdir[0] != '\0') {
1639 free(tmpnew);
1640 }
1641 if (ret) {
1642 goto error;
1643 }
1644 break;
1645 }
1646 default:
1647 perror("unknown consumer type");
1648 exit(EXIT_FAILURE);
1649 }
1650 if (errno != 0) {
1651 perror("kernel start consumer exec");
1652 }
1653 exit(EXIT_FAILURE);
1654 } else if (pid > 0) {
1655 ret = pid;
1656 } else {
1657 perror("start consumer fork");
1658 ret = -errno;
1659 }
1660 error:
1661 return ret;
1662 }
1663
1664 /*
1665 * Spawn the consumerd daemon and session daemon thread.
1666 */
1667 static int start_consumerd(struct consumer_data *consumer_data)
1668 {
1669 int ret;
1670
1671 pthread_mutex_lock(&consumer_data->pid_mutex);
1672 if (consumer_data->pid != 0) {
1673 pthread_mutex_unlock(&consumer_data->pid_mutex);
1674 goto end;
1675 }
1676
1677 ret = spawn_consumerd(consumer_data);
1678 if (ret < 0) {
1679 ERR("Spawning consumerd failed");
1680 pthread_mutex_unlock(&consumer_data->pid_mutex);
1681 goto error;
1682 }
1683
1684 /* Setting up the consumer_data pid */
1685 consumer_data->pid = ret;
1686 DBG2("Consumer pid %d", consumer_data->pid);
1687 pthread_mutex_unlock(&consumer_data->pid_mutex);
1688
1689 DBG2("Spawning consumer control thread");
1690 ret = spawn_consumer_thread(consumer_data);
1691 if (ret < 0) {
1692 ERR("Fatal error spawning consumer control thread");
1693 goto error;
1694 }
1695
1696 end:
1697 return 0;
1698
1699 error:
1700 return ret;
1701 }
1702
1703 /*
1704 * modprobe_kernel_modules
1705 */
1706 static int modprobe_kernel_modules(void)
1707 {
1708 int ret = 0, i;
1709 char modprobe[256];
1710
1711 for (i = 0; i < ARRAY_SIZE(kernel_modules_list); i++) {
1712 ret = snprintf(modprobe, sizeof(modprobe),
1713 "/sbin/modprobe %s%s",
1714 kernel_modules_list[i].required ? "" : "-q ",
1715 kernel_modules_list[i].name);
1716 if (ret < 0) {
1717 perror("snprintf modprobe");
1718 goto error;
1719 }
1720 modprobe[sizeof(modprobe) - 1] = '\0';
1721 ret = system(modprobe);
1722 if (ret == -1) {
1723 ERR("Unable to launch modprobe for module %s",
1724 kernel_modules_list[i].name);
1725 } else if (kernel_modules_list[i].required
1726 && WEXITSTATUS(ret) != 0) {
1727 ERR("Unable to load module %s",
1728 kernel_modules_list[i].name);
1729 } else {
1730 DBG("Modprobe successfully %s",
1731 kernel_modules_list[i].name);
1732 }
1733 }
1734
1735 error:
1736 return ret;
1737 }
1738
1739 /*
1740 * mount_debugfs
1741 */
1742 static int mount_debugfs(char *path)
1743 {
1744 int ret;
1745 char *type = "debugfs";
1746
1747 ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid());
1748 if (ret < 0) {
1749 PERROR("Cannot create debugfs path");
1750 goto error;
1751 }
1752
1753 ret = mount(type, path, type, 0, NULL);
1754 if (ret < 0) {
1755 PERROR("Cannot mount debugfs");
1756 goto error;
1757 }
1758
1759 DBG("Mounted debugfs successfully at %s", path);
1760
1761 error:
1762 return ret;
1763 }
1764
1765 /*
1766 * Setup necessary data for kernel tracer action.
1767 */
1768 static void init_kernel_tracer(void)
1769 {
1770 int ret;
1771 char *proc_mounts = "/proc/mounts";
1772 char line[256];
1773 char *debugfs_path = NULL, *lttng_path = NULL;
1774 FILE *fp;
1775
1776 /* Detect debugfs */
1777 fp = fopen(proc_mounts, "r");
1778 if (fp == NULL) {
1779 ERR("Unable to probe %s", proc_mounts);
1780 goto error;
1781 }
1782
1783 while (fgets(line, sizeof(line), fp) != NULL) {
1784 if (strstr(line, "debugfs") != NULL) {
1785 /* Remove first string */
1786 strtok(line, " ");
1787 /* Dup string here so we can reuse line later on */
1788 debugfs_path = strdup(strtok(NULL, " "));
1789 DBG("Got debugfs path : %s", debugfs_path);
1790 break;
1791 }
1792 }
1793
1794 fclose(fp);
1795
1796 /* Mount debugfs if needded */
1797 if (debugfs_path == NULL) {
1798 ret = asprintf(&debugfs_path, "/mnt/debugfs");
1799 if (ret < 0) {
1800 perror("asprintf debugfs path");
1801 goto error;
1802 }
1803 ret = mount_debugfs(debugfs_path);
1804 if (ret < 0) {
1805 perror("Cannot mount debugfs");
1806 goto error;
1807 }
1808 }
1809
1810 /* Modprobe lttng kernel modules */
1811 ret = modprobe_kernel_modules();
1812 if (ret < 0) {
1813 goto error;
1814 }
1815
1816 /* Setup lttng kernel path */
1817 ret = asprintf(&lttng_path, "%s/lttng", debugfs_path);
1818 if (ret < 0) {
1819 perror("asprintf lttng path");
1820 goto error;
1821 }
1822
1823 /* Open debugfs lttng */
1824 kernel_tracer_fd = open(lttng_path, O_RDWR);
1825 if (kernel_tracer_fd < 0) {
1826 DBG("Failed to open %s", lttng_path);
1827 goto error;
1828 }
1829
1830 free(lttng_path);
1831 free(debugfs_path);
1832 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1833 return;
1834
1835 error:
1836 if (lttng_path) {
1837 free(lttng_path);
1838 }
1839 if (debugfs_path) {
1840 free(debugfs_path);
1841 }
1842 WARN("No kernel tracer available");
1843 kernel_tracer_fd = 0;
1844 return;
1845 }
1846
1847 /*
1848 * Init tracing by creating trace directory and sending fds kernel consumer.
1849 */
1850 static int init_kernel_tracing(struct ltt_kernel_session *session)
1851 {
1852 int ret = 0;
1853
1854 if (session->consumer_fds_sent == 0) {
1855 /*
1856 * Assign default kernel consumer socket if no consumer assigned to the
1857 * kernel session. At this point, it's NOT suppose to be 0 but this is
1858 * an extra security check.
1859 */
1860 if (session->consumer_fd == 0) {
1861 session->consumer_fd = kconsumer_data.cmd_sock;
1862 }
1863
1864 ret = send_kconsumer_session_streams(&kconsumer_data, session);
1865 if (ret < 0) {
1866 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1867 goto error;
1868 }
1869
1870 session->consumer_fds_sent = 1;
1871 }
1872
1873 error:
1874 return ret;
1875 }
1876
1877 /*
1878 * Create an UST session and add it to the session ust list.
1879 */
1880 static int create_ust_session(struct ltt_session *session,
1881 struct lttng_domain *domain)
1882 {
1883 struct ltt_ust_session *lus = NULL;
1884 int ret;
1885
1886 switch (domain->type) {
1887 case LTTNG_DOMAIN_UST:
1888 break;
1889 default:
1890 ret = LTTCOMM_UNKNOWN_DOMAIN;
1891 goto error;
1892 }
1893
1894 DBG("Creating UST session");
1895
1896 lus = trace_ust_create_session(session->path, session->id, domain);
1897 if (lus == NULL) {
1898 ret = LTTCOMM_UST_SESS_FAIL;
1899 goto error;
1900 }
1901
1902 ret = mkdir_recursive(lus->pathname, S_IRWXU | S_IRWXG,
1903 session->uid, session->gid);
1904 if (ret < 0) {
1905 if (ret != -EEXIST) {
1906 ERR("Trace directory creation error");
1907 ret = LTTCOMM_UST_SESS_FAIL;
1908 goto error;
1909 }
1910 }
1911
1912 /* The domain type dictate different actions on session creation */
1913 switch (domain->type) {
1914 case LTTNG_DOMAIN_UST:
1915 /* No ustctl for the global UST domain */
1916 break;
1917 default:
1918 ERR("Unknown UST domain on create session %d", domain->type);
1919 goto error;
1920 }
1921 lus->uid = session->uid;
1922 lus->gid = session->gid;
1923 session->ust_session = lus;
1924
1925 return LTTCOMM_OK;
1926
1927 error:
1928 free(lus);
1929 return ret;
1930 }
1931
1932 /*
1933 * Create a kernel tracer session then create the default channel.
1934 */
1935 static int create_kernel_session(struct ltt_session *session)
1936 {
1937 int ret;
1938
1939 DBG("Creating kernel session");
1940
1941 ret = kernel_create_session(session, kernel_tracer_fd);
1942 if (ret < 0) {
1943 ret = LTTCOMM_KERN_SESS_FAIL;
1944 goto error;
1945 }
1946
1947 /* Set kernel consumer socket fd */
1948 if (kconsumer_data.cmd_sock) {
1949 session->kernel_session->consumer_fd = kconsumer_data.cmd_sock;
1950 }
1951
1952 ret = mkdir_recursive(session->kernel_session->trace_path,
1953 S_IRWXU | S_IRWXG, session->uid, session->gid);
1954 if (ret < 0) {
1955 if (ret != -EEXIST) {
1956 ERR("Trace directory creation error");
1957 goto error;
1958 }
1959 }
1960 session->kernel_session->uid = session->uid;
1961 session->kernel_session->gid = session->gid;
1962
1963 error:
1964 return ret;
1965 }
1966
1967 /*
1968 * Using the session list, filled a lttng_session array to send back to the
1969 * client for session listing.
1970 *
1971 * The session list lock MUST be acquired before calling this function. Use
1972 * session_lock_list() and session_unlock_list().
1973 */
1974 static void list_lttng_sessions(struct lttng_session *sessions)
1975 {
1976 int i = 0;
1977 struct ltt_session *session;
1978
1979 DBG("Getting all available session");
1980 /*
1981 * Iterate over session list and append data after the control struct in
1982 * the buffer.
1983 */
1984 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
1985 strncpy(sessions[i].path, session->path, PATH_MAX);
1986 sessions[i].path[PATH_MAX - 1] = '\0';
1987 strncpy(sessions[i].name, session->name, NAME_MAX);
1988 sessions[i].name[NAME_MAX - 1] = '\0';
1989 sessions[i].enabled = session->enabled;
1990 i++;
1991 }
1992 }
1993
1994 /*
1995 * Fill lttng_channel array of all channels.
1996 */
1997 static void list_lttng_channels(int domain, struct ltt_session *session,
1998 struct lttng_channel *channels)
1999 {
2000 int i = 0;
2001 struct ltt_kernel_channel *kchan;
2002
2003 DBG("Listing channels for session %s", session->name);
2004
2005 switch (domain) {
2006 case LTTNG_DOMAIN_KERNEL:
2007 /* Kernel channels */
2008 if (session->kernel_session != NULL) {
2009 cds_list_for_each_entry(kchan,
2010 &session->kernel_session->channel_list.head, list) {
2011 /* Copy lttng_channel struct to array */
2012 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
2013 channels[i].enabled = kchan->enabled;
2014 i++;
2015 }
2016 }
2017 break;
2018 case LTTNG_DOMAIN_UST:
2019 {
2020 struct cds_lfht_iter iter;
2021 struct ltt_ust_channel *uchan;
2022
2023 cds_lfht_for_each_entry(session->ust_session->domain_global.channels,
2024 &iter, uchan, node) {
2025 strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN);
2026 channels[i].attr.overwrite = uchan->attr.overwrite;
2027 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
2028 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
2029 channels[i].attr.switch_timer_interval =
2030 uchan->attr.switch_timer_interval;
2031 channels[i].attr.read_timer_interval =
2032 uchan->attr.read_timer_interval;
2033 channels[i].enabled = uchan->enabled;
2034 switch (uchan->attr.output) {
2035 case LTTNG_UST_MMAP:
2036 default:
2037 channels[i].attr.output = LTTNG_EVENT_MMAP;
2038 break;
2039 }
2040 i++;
2041 }
2042 break;
2043 }
2044 default:
2045 break;
2046 }
2047 }
2048
2049 /*
2050 * Create a list of ust global domain events.
2051 */
2052 static int list_lttng_ust_global_events(char *channel_name,
2053 struct ltt_ust_domain_global *ust_global, struct lttng_event **events)
2054 {
2055 int i = 0, ret = 0;
2056 unsigned int nb_event = 0;
2057 struct cds_lfht_iter iter;
2058 struct cds_lfht_node *node;
2059 struct ltt_ust_channel *uchan;
2060 struct ltt_ust_event *uevent;
2061 struct lttng_event *tmp;
2062
2063 DBG("Listing UST global events for channel %s", channel_name);
2064
2065 rcu_read_lock();
2066
2067 node = hashtable_lookup(ust_global->channels, (void *) channel_name,
2068 strlen(channel_name), &iter);
2069 if (node == NULL) {
2070 ret = -LTTCOMM_UST_CHAN_NOT_FOUND;
2071 goto error;
2072 }
2073
2074 uchan = caa_container_of(node, struct ltt_ust_channel, node);
2075
2076 nb_event += hashtable_get_count(uchan->events);
2077
2078 if (nb_event == 0) {
2079 ret = nb_event;
2080 goto error;
2081 }
2082
2083 DBG3("Listing UST global %d events", nb_event);
2084
2085 tmp = zmalloc(nb_event * sizeof(struct lttng_event));
2086 if (tmp == NULL) {
2087 ret = -LTTCOMM_FATAL;
2088 goto error;
2089 }
2090
2091 cds_lfht_for_each_entry(uchan->events, &iter, uevent, node) {
2092 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
2093 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2094 tmp[i].enabled = uevent->enabled;
2095 switch (uevent->attr.instrumentation) {
2096 case LTTNG_UST_TRACEPOINT:
2097 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
2098 break;
2099 case LTTNG_UST_PROBE:
2100 tmp[i].type = LTTNG_EVENT_PROBE;
2101 break;
2102 case LTTNG_UST_FUNCTION:
2103 tmp[i].type = LTTNG_EVENT_FUNCTION;
2104 break;
2105 case LTTNG_UST_TRACEPOINT_LOGLEVEL:
2106 tmp[i].type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL;
2107 break;
2108 }
2109 i++;
2110 }
2111
2112 ret = nb_event;
2113 *events = tmp;
2114
2115 error:
2116 rcu_read_unlock();
2117 return ret;
2118 }
2119
2120 /*
2121 * Fill lttng_event array of all kernel events in the channel.
2122 */
2123 static int list_lttng_kernel_events(char *channel_name,
2124 struct ltt_kernel_session *kernel_session, struct lttng_event **events)
2125 {
2126 int i = 0, ret;
2127 unsigned int nb_event;
2128 struct ltt_kernel_event *event;
2129 struct ltt_kernel_channel *kchan;
2130
2131 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
2132 if (kchan == NULL) {
2133 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2134 goto error;
2135 }
2136
2137 nb_event = kchan->event_count;
2138
2139 DBG("Listing events for channel %s", kchan->channel->name);
2140
2141 if (nb_event == 0) {
2142 ret = nb_event;
2143 goto error;
2144 }
2145
2146 *events = zmalloc(nb_event * sizeof(struct lttng_event));
2147 if (*events == NULL) {
2148 ret = LTTCOMM_FATAL;
2149 goto error;
2150 }
2151
2152 /* Kernel channels */
2153 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
2154 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
2155 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2156 (*events)[i].enabled = event->enabled;
2157 switch (event->event->instrumentation) {
2158 case LTTNG_KERNEL_TRACEPOINT:
2159 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
2160 break;
2161 case LTTNG_KERNEL_KPROBE:
2162 case LTTNG_KERNEL_KRETPROBE:
2163 (*events)[i].type = LTTNG_EVENT_PROBE;
2164 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
2165 sizeof(struct lttng_kernel_kprobe));
2166 break;
2167 case LTTNG_KERNEL_FUNCTION:
2168 (*events)[i].type = LTTNG_EVENT_FUNCTION;
2169 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
2170 sizeof(struct lttng_kernel_function));
2171 break;
2172 case LTTNG_KERNEL_NOOP:
2173 (*events)[i].type = LTTNG_EVENT_NOOP;
2174 break;
2175 case LTTNG_KERNEL_SYSCALL:
2176 (*events)[i].type = LTTNG_EVENT_SYSCALL;
2177 break;
2178 case LTTNG_KERNEL_ALL:
2179 assert(0);
2180 break;
2181 }
2182 i++;
2183 }
2184
2185 return nb_event;
2186
2187 error:
2188 return ret;
2189 }
2190
2191 /*
2192 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
2193 */
2194 static int cmd_disable_channel(struct ltt_session *session,
2195 int domain, char *channel_name)
2196 {
2197 int ret;
2198 struct ltt_ust_session *usess;
2199
2200 usess = session->ust_session;
2201
2202 switch (domain) {
2203 case LTTNG_DOMAIN_KERNEL:
2204 {
2205 ret = channel_kernel_disable(session->kernel_session,
2206 channel_name);
2207 if (ret != LTTCOMM_OK) {
2208 goto error;
2209 }
2210
2211 kernel_wait_quiescent(kernel_tracer_fd);
2212 break;
2213 }
2214 case LTTNG_DOMAIN_UST:
2215 {
2216 struct ltt_ust_channel *uchan;
2217 struct cds_lfht *chan_ht;
2218
2219 chan_ht = usess->domain_global.channels;
2220
2221 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
2222 if (uchan == NULL) {
2223 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2224 goto error;
2225 }
2226
2227 ret = channel_ust_disable(usess, domain, uchan);
2228 if (ret != LTTCOMM_OK) {
2229 goto error;
2230 }
2231 break;
2232 }
2233 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2234 case LTTNG_DOMAIN_UST_EXEC_NAME:
2235 case LTTNG_DOMAIN_UST_PID:
2236 ret = LTTCOMM_NOT_IMPLEMENTED;
2237 goto error;
2238 default:
2239 ret = LTTCOMM_UNKNOWN_DOMAIN;
2240 goto error;
2241 }
2242
2243 ret = LTTCOMM_OK;
2244
2245 error:
2246 return ret;
2247 }
2248
2249 /*
2250 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
2251 */
2252 static int cmd_enable_channel(struct ltt_session *session,
2253 int domain, struct lttng_channel *attr)
2254 {
2255 int ret;
2256 struct ltt_ust_session *usess = session->ust_session;
2257 struct cds_lfht *chan_ht;
2258
2259 DBG("Enabling channel %s for session %s", attr->name, session->name);
2260
2261 switch (domain) {
2262 case LTTNG_DOMAIN_KERNEL:
2263 {
2264 struct ltt_kernel_channel *kchan;
2265
2266 kchan = trace_kernel_get_channel_by_name(attr->name,
2267 session->kernel_session);
2268 if (kchan == NULL) {
2269 ret = channel_kernel_create(session->kernel_session,
2270 attr, kernel_poll_pipe[1]);
2271 } else {
2272 ret = channel_kernel_enable(session->kernel_session, kchan);
2273 }
2274
2275 if (ret != LTTCOMM_OK) {
2276 goto error;
2277 }
2278
2279 kernel_wait_quiescent(kernel_tracer_fd);
2280 break;
2281 }
2282 case LTTNG_DOMAIN_UST:
2283 {
2284 struct ltt_ust_channel *uchan;
2285
2286 chan_ht = usess->domain_global.channels;
2287
2288 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
2289 if (uchan == NULL) {
2290 ret = channel_ust_create(usess, domain, attr);
2291 } else {
2292 ret = channel_ust_enable(usess, domain, uchan);
2293 }
2294 break;
2295 }
2296 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2297 case LTTNG_DOMAIN_UST_EXEC_NAME:
2298 case LTTNG_DOMAIN_UST_PID:
2299 ret = LTTCOMM_NOT_IMPLEMENTED;
2300 goto error;
2301 default:
2302 ret = LTTCOMM_UNKNOWN_DOMAIN;
2303 goto error;
2304 }
2305
2306 error:
2307 return ret;
2308 }
2309
2310 /*
2311 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2312 */
2313 static int cmd_disable_event(struct ltt_session *session, int domain,
2314 char *channel_name, char *event_name)
2315 {
2316 int ret;
2317
2318 switch (domain) {
2319 case LTTNG_DOMAIN_KERNEL:
2320 {
2321 struct ltt_kernel_channel *kchan;
2322 struct ltt_kernel_session *ksess;
2323
2324 ksess = session->kernel_session;
2325
2326 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
2327 if (kchan == NULL) {
2328 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2329 goto error;
2330 }
2331
2332 ret = event_kernel_disable_tracepoint(ksess, kchan, event_name);
2333 if (ret != LTTCOMM_OK) {
2334 goto error;
2335 }
2336
2337 kernel_wait_quiescent(kernel_tracer_fd);
2338 break;
2339 }
2340 case LTTNG_DOMAIN_UST:
2341 {
2342 struct ltt_ust_channel *uchan;
2343 struct ltt_ust_session *usess;
2344
2345 usess = session->ust_session;
2346
2347 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2348 channel_name);
2349 if (uchan == NULL) {
2350 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2351 goto error;
2352 }
2353
2354 ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name);
2355 if (ret != LTTCOMM_OK) {
2356 goto error;
2357 }
2358
2359 DBG3("Disable UST event %s in channel %s completed", event_name,
2360 channel_name);
2361 break;
2362 }
2363 case LTTNG_DOMAIN_UST_EXEC_NAME:
2364 case LTTNG_DOMAIN_UST_PID:
2365 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2366 default:
2367 ret = LTTCOMM_NOT_IMPLEMENTED;
2368 goto error;
2369 }
2370
2371 ret = LTTCOMM_OK;
2372
2373 error:
2374 return ret;
2375 }
2376
2377 /*
2378 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
2379 */
2380 static int cmd_disable_event_all(struct ltt_session *session, int domain,
2381 char *channel_name)
2382 {
2383 int ret;
2384
2385 switch (domain) {
2386 case LTTNG_DOMAIN_KERNEL:
2387 {
2388 struct ltt_kernel_session *ksess;
2389 struct ltt_kernel_channel *kchan;
2390
2391 ksess = session->kernel_session;
2392
2393 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
2394 if (kchan == NULL) {
2395 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2396 goto error;
2397 }
2398
2399 ret = event_kernel_disable_all(ksess, kchan);
2400 if (ret != LTTCOMM_OK) {
2401 goto error;
2402 }
2403
2404 kernel_wait_quiescent(kernel_tracer_fd);
2405 break;
2406 }
2407 case LTTNG_DOMAIN_UST:
2408 {
2409 struct ltt_ust_session *usess;
2410 struct ltt_ust_channel *uchan;
2411
2412 usess = session->ust_session;
2413
2414 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2415 channel_name);
2416 if (uchan == NULL) {
2417 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2418 goto error;
2419 }
2420
2421 ret = event_ust_disable_all_tracepoints(usess, domain, uchan);
2422 if (ret != 0) {
2423 goto error;
2424 }
2425
2426 DBG3("Disable all UST events in channel %s completed", channel_name);
2427
2428 break;
2429 }
2430 case LTTNG_DOMAIN_UST_EXEC_NAME:
2431 case LTTNG_DOMAIN_UST_PID:
2432 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2433 default:
2434 ret = LTTCOMM_NOT_IMPLEMENTED;
2435 goto error;
2436 }
2437
2438 ret = LTTCOMM_OK;
2439
2440 error:
2441 return ret;
2442 }
2443
2444 /*
2445 * Command LTTNG_ADD_CONTEXT processed by the client thread.
2446 */
2447 static int cmd_add_context(struct ltt_session *session, int domain,
2448 char *channel_name, char *event_name, struct lttng_event_context *ctx)
2449 {
2450 int ret;
2451
2452 switch (domain) {
2453 case LTTNG_DOMAIN_KERNEL:
2454 /* Add kernel context to kernel tracer */
2455 ret = context_kernel_add(session->kernel_session, ctx,
2456 event_name, channel_name);
2457 if (ret != LTTCOMM_OK) {
2458 goto error;
2459 }
2460 break;
2461 case LTTNG_DOMAIN_UST:
2462 {
2463 struct ltt_ust_session *usess = session->ust_session;
2464
2465 ret = context_ust_add(usess, domain, ctx, event_name, channel_name);
2466 if (ret != LTTCOMM_OK) {
2467 goto error;
2468 }
2469 break;
2470 }
2471 case LTTNG_DOMAIN_UST_EXEC_NAME:
2472 case LTTNG_DOMAIN_UST_PID:
2473 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2474 default:
2475 ret = LTTCOMM_NOT_IMPLEMENTED;
2476 goto error;
2477 }
2478
2479 ret = LTTCOMM_OK;
2480
2481 error:
2482 return ret;
2483 }
2484
2485 /*
2486 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2487 *
2488 * TODO: currently, both events and loglevels are kept within the same
2489 * namespace for UST global registry/app registery, so if an event
2490 * happen to have the same name as the loglevel (very unlikely though),
2491 * and an attempt is made to enable/disable both in the same session,
2492 * the first to be created will be the only one allowed to exist.
2493 */
2494 static int cmd_enable_event(struct ltt_session *session, int domain,
2495 char *channel_name, struct lttng_event *event)
2496 {
2497 int ret;
2498 struct lttng_channel *attr;
2499 struct ltt_ust_session *usess = session->ust_session;
2500
2501 switch (domain) {
2502 case LTTNG_DOMAIN_KERNEL:
2503 {
2504 struct ltt_kernel_channel *kchan;
2505
2506 kchan = trace_kernel_get_channel_by_name(channel_name,
2507 session->kernel_session);
2508 if (kchan == NULL) {
2509 attr = channel_new_default_attr(domain);
2510 if (attr == NULL) {
2511 ret = LTTCOMM_FATAL;
2512 goto error;
2513 }
2514 snprintf(attr->name, NAME_MAX, "%s", channel_name);
2515
2516 /* This call will notify the kernel thread */
2517 ret = channel_kernel_create(session->kernel_session,
2518 attr, kernel_poll_pipe[1]);
2519 if (ret != LTTCOMM_OK) {
2520 free(attr);
2521 goto error;
2522 }
2523 free(attr);
2524 }
2525
2526 /* Get the newly created kernel channel pointer */
2527 kchan = trace_kernel_get_channel_by_name(channel_name,
2528 session->kernel_session);
2529 if (kchan == NULL) {
2530 /* This sould not happen... */
2531 ret = LTTCOMM_FATAL;
2532 goto error;
2533 }
2534
2535 ret = event_kernel_enable_tracepoint(session->kernel_session, kchan,
2536 event);
2537 if (ret != LTTCOMM_OK) {
2538 goto error;
2539 }
2540
2541 kernel_wait_quiescent(kernel_tracer_fd);
2542 break;
2543 }
2544 case LTTNG_DOMAIN_UST:
2545 {
2546 struct lttng_channel *attr;
2547 struct ltt_ust_channel *uchan;
2548
2549 /* Get channel from global UST domain */
2550 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2551 channel_name);
2552 if (uchan == NULL) {
2553 /* Create default channel */
2554 attr = channel_new_default_attr(domain);
2555 if (attr == NULL) {
2556 ret = LTTCOMM_FATAL;
2557 goto error;
2558 }
2559 snprintf(attr->name, NAME_MAX, "%s", channel_name);
2560 attr->name[NAME_MAX - 1] = '\0';
2561
2562 ret = channel_ust_create(usess, domain, attr);
2563 if (ret != LTTCOMM_OK) {
2564 free(attr);
2565 goto error;
2566 }
2567 free(attr);
2568
2569 /* Get the newly created channel reference back */
2570 uchan = trace_ust_find_channel_by_name(
2571 usess->domain_global.channels, channel_name);
2572 if (uchan == NULL) {
2573 /* Something is really wrong */
2574 ret = LTTCOMM_FATAL;
2575 goto error;
2576 }
2577 }
2578
2579 /* At this point, the session and channel exist on the tracer */
2580
2581 ret = event_ust_enable_tracepoint(usess, domain, uchan, event);
2582 if (ret != LTTCOMM_OK) {
2583 goto error;
2584 }
2585 break;
2586 }
2587 case LTTNG_DOMAIN_UST_EXEC_NAME:
2588 case LTTNG_DOMAIN_UST_PID:
2589 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2590 default:
2591 ret = LTTCOMM_NOT_IMPLEMENTED;
2592 goto error;
2593 }
2594
2595 ret = LTTCOMM_OK;
2596
2597 error:
2598 return ret;
2599 }
2600
2601 /*
2602 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
2603 */
2604 static int cmd_enable_event_all(struct ltt_session *session, int domain,
2605 char *channel_name, int event_type)
2606 {
2607 int ret;
2608 struct ltt_kernel_channel *kchan;
2609
2610 switch (domain) {
2611 case LTTNG_DOMAIN_KERNEL:
2612 kchan = trace_kernel_get_channel_by_name(channel_name,
2613 session->kernel_session);
2614 if (kchan == NULL) {
2615 /* This call will notify the kernel thread */
2616 ret = channel_kernel_create(session->kernel_session, NULL,
2617 kernel_poll_pipe[1]);
2618 if (ret != LTTCOMM_OK) {
2619 goto error;
2620 }
2621
2622 /* Get the newly created kernel channel pointer */
2623 kchan = trace_kernel_get_channel_by_name(channel_name,
2624 session->kernel_session);
2625 if (kchan == NULL) {
2626 /* This sould not happen... */
2627 ret = LTTCOMM_FATAL;
2628 goto error;
2629 }
2630
2631 }
2632
2633 switch (event_type) {
2634 case LTTNG_EVENT_SYSCALL:
2635 ret = event_kernel_enable_all_syscalls(session->kernel_session,
2636 kchan, kernel_tracer_fd);
2637 break;
2638 case LTTNG_EVENT_TRACEPOINT:
2639 /*
2640 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
2641 * events already registered to the channel.
2642 */
2643 ret = event_kernel_enable_all_tracepoints(session->kernel_session,
2644 kchan, kernel_tracer_fd);
2645 break;
2646 case LTTNG_EVENT_ALL:
2647 /* Enable syscalls and tracepoints */
2648 ret = event_kernel_enable_all(session->kernel_session,
2649 kchan, kernel_tracer_fd);
2650 break;
2651 default:
2652 ret = LTTCOMM_KERN_ENABLE_FAIL;
2653 goto error;
2654 }
2655
2656 /* Manage return value */
2657 if (ret != LTTCOMM_OK) {
2658 goto error;
2659 }
2660
2661 kernel_wait_quiescent(kernel_tracer_fd);
2662 break;
2663 case LTTNG_DOMAIN_UST:
2664 {
2665 struct lttng_channel *attr;
2666 struct ltt_ust_channel *uchan;
2667 struct ltt_ust_session *usess = session->ust_session;
2668
2669 /* Get channel from global UST domain */
2670 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2671 channel_name);
2672 if (uchan == NULL) {
2673 /* Create default channel */
2674 attr = channel_new_default_attr(domain);
2675 if (attr == NULL) {
2676 ret = LTTCOMM_FATAL;
2677 goto error;
2678 }
2679 snprintf(attr->name, NAME_MAX, "%s", channel_name);
2680 attr->name[NAME_MAX - 1] = '\0';
2681
2682 /* Use the internal command enable channel */
2683 ret = channel_ust_create(usess, domain, attr);
2684 if (ret != LTTCOMM_OK) {
2685 free(attr);
2686 goto error;
2687 }
2688 free(attr);
2689
2690 /* Get the newly created channel reference back */
2691 uchan = trace_ust_find_channel_by_name(
2692 usess->domain_global.channels, channel_name);
2693 if (uchan == NULL) {
2694 /* Something is really wrong */
2695 ret = LTTCOMM_FATAL;
2696 goto error;
2697 }
2698 }
2699
2700 /* At this point, the session and channel exist on the tracer */
2701
2702 switch (event_type) {
2703 case LTTNG_EVENT_ALL:
2704 case LTTNG_EVENT_TRACEPOINT:
2705 ret = event_ust_enable_all_tracepoints(usess, domain, uchan);
2706 if (ret != LTTCOMM_OK) {
2707 goto error;
2708 }
2709 break;
2710 default:
2711 ret = LTTCOMM_UST_ENABLE_FAIL;
2712 goto error;
2713 }
2714
2715 /* Manage return value */
2716 if (ret != LTTCOMM_OK) {
2717 goto error;
2718 }
2719
2720 break;
2721 }
2722 case LTTNG_DOMAIN_UST_EXEC_NAME:
2723 case LTTNG_DOMAIN_UST_PID:
2724 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2725 default:
2726 ret = LTTCOMM_NOT_IMPLEMENTED;
2727 goto error;
2728 }
2729
2730 ret = LTTCOMM_OK;
2731
2732 error:
2733 return ret;
2734 }
2735
2736 /*
2737 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2738 */
2739 static ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events)
2740 {
2741 int ret;
2742 ssize_t nb_events = 0;
2743
2744 switch (domain) {
2745 case LTTNG_DOMAIN_KERNEL:
2746 nb_events = kernel_list_events(kernel_tracer_fd, events);
2747 if (nb_events < 0) {
2748 ret = LTTCOMM_KERN_LIST_FAIL;
2749 goto error;
2750 }
2751 break;
2752 case LTTNG_DOMAIN_UST:
2753 nb_events = ust_app_list_events(events);
2754 if (nb_events < 0) {
2755 ret = LTTCOMM_UST_LIST_FAIL;
2756 goto error;
2757 }
2758 break;
2759 default:
2760 ret = LTTCOMM_NOT_IMPLEMENTED;
2761 goto error;
2762 }
2763
2764 return nb_events;
2765
2766 error:
2767 /* Return negative value to differentiate return code */
2768 return -ret;
2769 }
2770
2771 /*
2772 * Command LTTNG_START_TRACE processed by the client thread.
2773 */
2774 static int cmd_start_trace(struct ltt_session *session)
2775 {
2776 int ret;
2777 struct ltt_kernel_session *ksession;
2778 struct ltt_ust_session *usess;
2779
2780 /* Short cut */
2781 ksession = session->kernel_session;
2782 usess = session->ust_session;
2783
2784 if (session->enabled) {
2785 ret = LTTCOMM_UST_START_FAIL;
2786 goto error;
2787 }
2788
2789 session->enabled = 1;
2790
2791 /* Kernel tracing */
2792 if (ksession != NULL) {
2793 struct ltt_kernel_channel *kchan;
2794
2795 /* Open kernel metadata */
2796 if (ksession->metadata == NULL) {
2797 ret = kernel_open_metadata(ksession, ksession->trace_path);
2798 if (ret < 0) {
2799 ret = LTTCOMM_KERN_META_FAIL;
2800 goto error;
2801 }
2802 }
2803
2804 /* Open kernel metadata stream */
2805 if (ksession->metadata_stream_fd == 0) {
2806 ret = kernel_open_metadata_stream(ksession);
2807 if (ret < 0) {
2808 ERR("Kernel create metadata stream failed");
2809 ret = LTTCOMM_KERN_STREAM_FAIL;
2810 goto error;
2811 }
2812 }
2813
2814 /* For each channel */
2815 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
2816 if (kchan->stream_count == 0) {
2817 ret = kernel_open_channel_stream(kchan);
2818 if (ret < 0) {
2819 ret = LTTCOMM_KERN_STREAM_FAIL;
2820 goto error;
2821 }
2822 /* Update the stream global counter */
2823 ksession->stream_count_global += ret;
2824 }
2825 }
2826
2827 /* Setup kernel consumer socket and send fds to it */
2828 ret = init_kernel_tracing(ksession);
2829 if (ret < 0) {
2830 ret = LTTCOMM_KERN_START_FAIL;
2831 goto error;
2832 }
2833
2834 /* This start the kernel tracing */
2835 ret = kernel_start_session(ksession);
2836 if (ret < 0) {
2837 ret = LTTCOMM_KERN_START_FAIL;
2838 goto error;
2839 }
2840
2841 /* Quiescent wait after starting trace */
2842 kernel_wait_quiescent(kernel_tracer_fd);
2843 }
2844
2845 /* Flag session that trace should start automatically */
2846 if (usess) {
2847 usess->start_trace = 1;
2848
2849 ret = ust_app_start_trace_all(usess);
2850 if (ret < 0) {
2851 ret = LTTCOMM_UST_START_FAIL;
2852 goto error;
2853 }
2854 }
2855
2856 ret = LTTCOMM_OK;
2857
2858 error:
2859 return ret;
2860 }
2861
2862 /*
2863 * Command LTTNG_STOP_TRACE processed by the client thread.
2864 */
2865 static int cmd_stop_trace(struct ltt_session *session)
2866 {
2867 int ret;
2868 struct ltt_kernel_channel *kchan;
2869 struct ltt_kernel_session *ksession;
2870 struct ltt_ust_session *usess;
2871
2872 /* Short cut */
2873 ksession = session->kernel_session;
2874 usess = session->ust_session;
2875
2876 if (!session->enabled) {
2877 ret = LTTCOMM_UST_START_FAIL;
2878 goto error;
2879 }
2880
2881 session->enabled = 0;
2882
2883 /* Kernel tracer */
2884 if (ksession != NULL) {
2885 DBG("Stop kernel tracing");
2886
2887 /* Flush all buffers before stopping */
2888 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
2889 if (ret < 0) {
2890 ERR("Kernel metadata flush failed");
2891 }
2892
2893 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
2894 ret = kernel_flush_buffer(kchan);
2895 if (ret < 0) {
2896 ERR("Kernel flush buffer error");
2897 }
2898 }
2899
2900 ret = kernel_stop_session(ksession);
2901 if (ret < 0) {
2902 ret = LTTCOMM_KERN_STOP_FAIL;
2903 goto error;
2904 }
2905
2906 kernel_wait_quiescent(kernel_tracer_fd);
2907 }
2908
2909 if (usess) {
2910 usess->start_trace = 0;
2911
2912 ret = ust_app_stop_trace_all(usess);
2913 if (ret < 0) {
2914 ret = LTTCOMM_UST_START_FAIL;
2915 goto error;
2916 }
2917 }
2918
2919 ret = LTTCOMM_OK;
2920
2921 error:
2922 return ret;
2923 }
2924
2925 /*
2926 * Command LTTNG_CREATE_SESSION processed by the client thread.
2927 */
2928 static int cmd_create_session(char *name, char *path, struct ucred *creds)
2929 {
2930 int ret;
2931
2932 ret = session_create(name, path, creds->uid, creds->gid);
2933 if (ret != LTTCOMM_OK) {
2934 goto error;
2935 }
2936
2937 ret = LTTCOMM_OK;
2938
2939 error:
2940 return ret;
2941 }
2942
2943 /*
2944 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2945 */
2946 static int cmd_destroy_session(struct ltt_session *session, char *name)
2947 {
2948 int ret;
2949
2950 /* Clean kernel session teardown */
2951 teardown_kernel_session(session);
2952 /* UST session teardown */
2953 teardown_ust_session(session);
2954
2955 /*
2956 * Must notify the kernel thread here to update it's poll setin order
2957 * to remove the channel(s)' fd just destroyed.
2958 */
2959 ret = notify_thread_pipe(kernel_poll_pipe[1]);
2960 if (ret < 0) {
2961 perror("write kernel poll pipe");
2962 }
2963
2964 ret = session_destroy(session);
2965
2966 return ret;
2967 }
2968
2969 /*
2970 * Command LTTNG_CALIBRATE processed by the client thread.
2971 */
2972 static int cmd_calibrate(int domain, struct lttng_calibrate *calibrate)
2973 {
2974 int ret;
2975
2976 switch (domain) {
2977 case LTTNG_DOMAIN_KERNEL:
2978 {
2979 struct lttng_kernel_calibrate kcalibrate;
2980
2981 kcalibrate.type = calibrate->type;
2982 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2983 if (ret < 0) {
2984 ret = LTTCOMM_KERN_ENABLE_FAIL;
2985 goto error;
2986 }
2987 break;
2988 }
2989 default:
2990 /* TODO: Userspace tracing */
2991 ret = LTTCOMM_NOT_IMPLEMENTED;
2992 goto error;
2993 }
2994
2995 ret = LTTCOMM_OK;
2996
2997 error:
2998 return ret;
2999 }
3000
3001 /*
3002 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3003 */
3004 static int cmd_register_consumer(struct ltt_session *session, int domain,
3005 char *sock_path)
3006 {
3007 int ret, sock;
3008
3009 switch (domain) {
3010 case LTTNG_DOMAIN_KERNEL:
3011 /* Can't register a consumer if there is already one */
3012 if (session->kernel_session->consumer_fds_sent != 0) {
3013 ret = LTTCOMM_KERN_CONSUMER_FAIL;
3014 goto error;
3015 }
3016
3017 sock = lttcomm_connect_unix_sock(sock_path);
3018 if (sock < 0) {
3019 ret = LTTCOMM_CONNECT_FAIL;
3020 goto error;
3021 }
3022
3023 session->kernel_session->consumer_fd = sock;
3024 break;
3025 default:
3026 /* TODO: Userspace tracing */
3027 ret = LTTCOMM_NOT_IMPLEMENTED;
3028 goto error;
3029 }
3030
3031 ret = LTTCOMM_OK;
3032
3033 error:
3034 return ret;
3035 }
3036
3037 /*
3038 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3039 */
3040 static ssize_t cmd_list_domains(struct ltt_session *session,
3041 struct lttng_domain **domains)
3042 {
3043 int ret, index = 0;
3044 ssize_t nb_dom = 0;
3045
3046 if (session->kernel_session != NULL) {
3047 DBG3("Listing domains found kernel domain");
3048 nb_dom++;
3049 }
3050
3051 if (session->ust_session != NULL) {
3052 DBG3("Listing domains found UST global domain");
3053 nb_dom++;
3054 }
3055
3056 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
3057 if (*domains == NULL) {
3058 ret = -LTTCOMM_FATAL;
3059 goto error;
3060 }
3061
3062 if (session->kernel_session != NULL) {
3063 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
3064 index++;
3065 }
3066
3067 if (session->ust_session != NULL) {
3068 (*domains)[index].type = LTTNG_DOMAIN_UST;
3069 index++;
3070 }
3071
3072 return nb_dom;
3073
3074 error:
3075 return ret;
3076 }
3077
3078 /*
3079 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3080 */
3081 static ssize_t cmd_list_channels(int domain, struct ltt_session *session,
3082 struct lttng_channel **channels)
3083 {
3084 int ret;
3085 ssize_t nb_chan = 0;
3086
3087 switch (domain) {
3088 case LTTNG_DOMAIN_KERNEL:
3089 if (session->kernel_session != NULL) {
3090 nb_chan = session->kernel_session->channel_count;
3091 }
3092 DBG3("Number of kernel channels %zd", nb_chan);
3093 break;
3094 case LTTNG_DOMAIN_UST:
3095 if (session->ust_session != NULL) {
3096 nb_chan = hashtable_get_count(
3097 session->ust_session->domain_global.channels);
3098 }
3099 DBG3("Number of UST global channels %zd", nb_chan);
3100 break;
3101 default:
3102 *channels = NULL;
3103 ret = -LTTCOMM_NOT_IMPLEMENTED;
3104 goto error;
3105 }
3106
3107 if (nb_chan > 0) {
3108 *channels = zmalloc(nb_chan * sizeof(struct lttng_channel));
3109 if (*channels == NULL) {
3110 ret = -LTTCOMM_FATAL;
3111 goto error;
3112 }
3113
3114 list_lttng_channels(domain, session, *channels);
3115 } else {
3116 *channels = NULL;
3117 }
3118
3119 return nb_chan;
3120
3121 error:
3122 return ret;
3123 }
3124
3125 /*
3126 * Command LTTNG_LIST_EVENTS processed by the client thread.
3127 */
3128 static ssize_t cmd_list_events(int domain, struct ltt_session *session,
3129 char *channel_name, struct lttng_event **events)
3130 {
3131 int ret = 0;
3132 ssize_t nb_event = 0;
3133
3134 switch (domain) {
3135 case LTTNG_DOMAIN_KERNEL:
3136 if (session->kernel_session != NULL) {
3137 nb_event = list_lttng_kernel_events(channel_name,
3138 session->kernel_session, events);
3139 }
3140 break;
3141 case LTTNG_DOMAIN_UST:
3142 {
3143 if (session->ust_session != NULL) {
3144 nb_event = list_lttng_ust_global_events(channel_name,
3145 &session->ust_session->domain_global, events);
3146 }
3147 break;
3148 }
3149 default:
3150 ret = -LTTCOMM_NOT_IMPLEMENTED;
3151 goto error;
3152 }
3153
3154 ret = nb_event;
3155
3156 error:
3157 return ret;
3158 }
3159
3160 /*
3161 * Process the command requested by the lttng client within the command
3162 * context structure. This function make sure that the return structure (llm)
3163 * is set and ready for transmission before returning.
3164 *
3165 * Return any error encountered or 0 for success.
3166 */
3167 static int process_client_msg(struct command_ctx *cmd_ctx)
3168 {
3169 int ret = LTTCOMM_OK;
3170 int need_tracing_session = 1;
3171
3172 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
3173
3174 if (opt_no_kernel && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
3175 ret = LTTCOMM_KERN_NA;
3176 goto error;
3177 }
3178
3179 /*
3180 * Check for command that don't needs to allocate a returned payload. We do
3181 * this here so we don't have to make the call for no payload at each
3182 * command.
3183 */
3184 switch(cmd_ctx->lsm->cmd_type) {
3185 case LTTNG_LIST_SESSIONS:
3186 case LTTNG_LIST_TRACEPOINTS:
3187 case LTTNG_LIST_DOMAINS:
3188 case LTTNG_LIST_CHANNELS:
3189 case LTTNG_LIST_EVENTS:
3190 break;
3191 default:
3192 /* Setup lttng message with no payload */
3193 ret = setup_lttng_msg(cmd_ctx, 0);
3194 if (ret < 0) {
3195 /* This label does not try to unlock the session */
3196 goto init_setup_error;
3197 }
3198 }
3199
3200 /* Commands that DO NOT need a session. */
3201 switch (cmd_ctx->lsm->cmd_type) {
3202 case LTTNG_CALIBRATE:
3203 case LTTNG_CREATE_SESSION:
3204 case LTTNG_LIST_SESSIONS:
3205 case LTTNG_LIST_TRACEPOINTS:
3206 need_tracing_session = 0;
3207 break;
3208 default:
3209 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
3210 session_lock_list();
3211 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
3212 session_unlock_list();
3213 if (cmd_ctx->session == NULL) {
3214 if (cmd_ctx->lsm->session.name != NULL) {
3215 ret = LTTCOMM_SESS_NOT_FOUND;
3216 } else {
3217 /* If no session name specified */
3218 ret = LTTCOMM_SELECT_SESS;
3219 }
3220 goto error;
3221 } else {
3222 /* Acquire lock for the session */
3223 session_lock(cmd_ctx->session);
3224 }
3225 break;
3226 }
3227
3228 /*
3229 * Check domain type for specific "pre-action".
3230 */
3231 switch (cmd_ctx->lsm->domain.type) {
3232 case LTTNG_DOMAIN_KERNEL:
3233 /* Kernel tracer check */
3234 if (kernel_tracer_fd == 0) {
3235 /* Basically, load kernel tracer modules */
3236 init_kernel_tracer();
3237 if (kernel_tracer_fd == 0) {
3238 ret = LTTCOMM_KERN_NA;
3239 goto error;
3240 }
3241 }
3242
3243 /* Need a session for kernel command */
3244 if (need_tracing_session) {
3245 if (cmd_ctx->session->kernel_session == NULL) {
3246 ret = create_kernel_session(cmd_ctx->session);
3247 if (ret < 0) {
3248 ret = LTTCOMM_KERN_SESS_FAIL;
3249 goto error;
3250 }
3251 }
3252
3253 /* Start the kernel consumer daemon */
3254 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3255 if (kconsumer_data.pid == 0 &&
3256 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3257 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3258 ret = start_consumerd(&kconsumer_data);
3259 if (ret < 0) {
3260 ret = LTTCOMM_KERN_CONSUMER_FAIL;
3261 goto error;
3262 }
3263 } else {
3264 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3265 }
3266 }
3267 break;
3268 case LTTNG_DOMAIN_UST:
3269 {
3270 if (need_tracing_session) {
3271 if (cmd_ctx->session->ust_session == NULL) {
3272 ret = create_ust_session(cmd_ctx->session,
3273 &cmd_ctx->lsm->domain);
3274 if (ret != LTTCOMM_OK) {
3275 goto error;
3276 }
3277 }
3278 /* Start the UST consumer daemons */
3279 /* 64-bit */
3280 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
3281 if (consumerd64_bin[0] != '\0' &&
3282 ustconsumer64_data.pid == 0 &&
3283 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3284 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3285 ret = start_consumerd(&ustconsumer64_data);
3286 if (ret < 0) {
3287 ret = LTTCOMM_UST_CONSUMER64_FAIL;
3288 ust_consumerd64_fd = -EINVAL;
3289 goto error;
3290 }
3291
3292 ust_consumerd64_fd = ustconsumer64_data.cmd_sock;
3293 } else {
3294 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3295 }
3296 /* 32-bit */
3297 if (consumerd32_bin[0] != '\0' &&
3298 ustconsumer32_data.pid == 0 &&
3299 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3300 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3301 ret = start_consumerd(&ustconsumer32_data);
3302 if (ret < 0) {
3303 ret = LTTCOMM_UST_CONSUMER32_FAIL;
3304 ust_consumerd32_fd = -EINVAL;
3305 goto error;
3306 }
3307 ust_consumerd32_fd = ustconsumer32_data.cmd_sock;
3308 } else {
3309 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3310 }
3311 }
3312 break;
3313 }
3314 default:
3315 break;
3316 }
3317
3318 /* Process by command type */
3319 switch (cmd_ctx->lsm->cmd_type) {
3320 case LTTNG_ADD_CONTEXT:
3321 {
3322 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3323 cmd_ctx->lsm->u.context.channel_name,
3324 cmd_ctx->lsm->u.context.event_name,
3325 &cmd_ctx->lsm->u.context.ctx);
3326 break;
3327 }
3328 case LTTNG_DISABLE_CHANNEL:
3329 {
3330 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3331 cmd_ctx->lsm->u.disable.channel_name);
3332 break;