2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
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.
25 #include <semaphore.h>
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <urcu/futex.h>
41 #include <lttng-consumerd.h>
42 #include <lttng-sessiond-comm.h>
43 #include <lttng/lttng-consumer.h>
45 #ifdef CONFIG_LTTNG_TOOLS_HAVE_UST
46 #include <ust/lttng-ust-ctl.h>
48 #include "lttng-ust-ctl.h"
55 #include "compat/poll.h"
59 #include "hashtable.h"
60 #include "kernel-ctl.h"
61 #include "ltt-sessiond.h"
68 struct consumer_data
{
69 enum lttng_consumer_type type
;
71 pthread_t thread
; /* Worker thread interacting with the consumer */
74 /* Mutex to control consumerd pid assignation */
75 pthread_mutex_t pid_mutex
;
81 /* consumer error and command Unix socket path */
82 char err_unix_sock_path
[PATH_MAX
];
83 char cmd_unix_sock_path
[PATH_MAX
];
87 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
88 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
89 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
90 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
93 int opt_verbose
; /* Not static for lttngerr.h */
94 int opt_verbose_consumer
; /* Not static for lttngerr.h */
95 int opt_quiet
; /* Not static for lttngerr.h */
98 const char *opt_tracing_group
;
99 static int opt_sig_parent
;
100 static int opt_daemon
;
101 static int is_root
; /* Set to 1 if the daemon is running as root */
102 static pid_t ppid
; /* Parent PID for --sig-parent option */
104 /* Consumer daemon specific control data */
105 static struct consumer_data kconsumer_data
= {
106 .type
= LTTNG_CONSUMER_KERNEL
,
108 static struct consumer_data ustconsumer_data
= {
109 .type
= LTTNG_CONSUMER_UST
,
112 static int dispatch_thread_exit
;
114 /* Global application Unix socket path */
115 static char apps_unix_sock_path
[PATH_MAX
];
116 /* Global client Unix socket path */
117 static char client_unix_sock_path
[PATH_MAX
];
118 /* global wait shm path for UST */
119 static char wait_shm_path
[PATH_MAX
];
121 /* Sockets and FDs */
122 static int client_sock
;
123 static int apps_sock
;
124 static int kernel_tracer_fd
;
125 static int kernel_poll_pipe
[2];
128 * Quit pipe for all threads. This permits a single cancellation point
129 * for all threads when receiving an event on the pipe.
131 static int thread_quit_pipe
[2];
134 * This pipe is used to inform the thread managing application communication
135 * that a command is queued and ready to be processed.
137 static int apps_cmd_pipe
[2];
139 /* Pthread, Mutexes and Semaphores */
140 static pthread_t apps_thread
;
141 static pthread_t reg_apps_thread
;
142 static pthread_t client_thread
;
143 static pthread_t kernel_thread
;
144 static pthread_t dispatch_thread
;
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
151 * The thread_manage_apps and thread_dispatch_ust_registration interact with
152 * this queue and the wait/wake scheme.
154 static struct ust_cmd_queue ust_cmd_queue
;
157 * Pointer initialized before thread creation.
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.
163 * The lock is nested inside the structure: session_list_ptr->lock. Please use
164 * session_lock_list and session_unlock_list for lock acquisition.
166 static struct ltt_session_list
*session_list_ptr
;
169 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
171 static int create_thread_poll_set(struct lttng_poll_event
*events
,
176 if (events
== NULL
|| size
== 0) {
181 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
187 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
199 * Check if the thread quit pipe was triggered.
201 * Return 1 if it was triggered else 0;
203 static int check_thread_quit_pipe(int fd
, uint32_t events
)
205 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
213 * Remove modules in reverse load order.
215 static int modprobe_remove_kernel_modules(void)
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
);
225 perror("snprintf modprobe --remove");
228 modprobe
[sizeof(modprobe
) - 1] = '\0';
229 ret
= system(modprobe
);
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
);
238 DBG("Modprobe removal successful %s",
239 kernel_modules_list
[i
].name
);
248 * Return group ID of the tracing group or -1 if not found.
250 static gid_t
allowed_group(void)
254 if (opt_tracing_group
) {
255 grp
= getgrnam(opt_tracing_group
);
257 grp
= getgrnam(default_tracing_group
);
267 * Init thread quit pipe.
269 * Return -1 on error or 0 if all pipes are created.
271 static int init_thread_quit_pipe(void)
275 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
277 perror("thread quit pipe");
286 * Complete teardown of a kernel session. This free all data structure related
287 * to a kernel session and update counter.
289 static void teardown_kernel_session(struct ltt_session
*session
)
291 if (session
->kernel_session
!= NULL
) {
292 DBG("Tearing down kernel session");
295 * If a custom kernel consumer was registered, close the socket before
296 * tearing down the complete kernel session structure
298 if (session
->kernel_session
->consumer_fd
!= kconsumer_data
.cmd_sock
) {
299 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
302 trace_kernel_destroy_session(session
->kernel_session
);
303 /* Extra precaution */
304 session
->kernel_session
= NULL
;
309 * Complete teardown of all UST sessions. This will free everything on his path
310 * and destroy the core essence of all ust sessions :)
312 static void teardown_ust_session(struct ltt_session
*session
)
314 DBG("Tearing down UST session(s)");
316 trace_ust_destroy_session(session
->ust_session
);
320 * Stop all threads by closing the thread quit pipe.
322 static void stop_threads(void)
326 /* Stopping all threads */
327 DBG("Terminating all threads");
328 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
330 ERR("write error on thread quit pipe");
333 /* Dispatch thread */
334 dispatch_thread_exit
= 1;
335 futex_nto1_wake(&ust_cmd_queue
.futex
);
341 static void cleanup(void)
345 struct ltt_session
*sess
, *stmp
;
350 MSG("%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
351 "Matthew, BEET driven development works!%c[%dm",
352 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
356 DBG("Removing %s directory", LTTNG_RUNDIR
);
357 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
359 ERR("asprintf failed. Something is really wrong!");
362 /* Remove lttng run directory */
365 ERR("Unable to clean " LTTNG_RUNDIR
);
369 DBG("Cleaning up all session");
371 /* Destroy session list mutex */
372 if (session_list_ptr
!= NULL
) {
373 pthread_mutex_destroy(&session_list_ptr
->lock
);
375 /* Cleanup ALL session */
376 cds_list_for_each_entry_safe(sess
, stmp
,
377 &session_list_ptr
->head
, list
) {
378 teardown_kernel_session(sess
);
379 teardown_ust_session(sess
);
384 DBG("Closing all UST sockets");
385 ust_app_clean_list();
387 pthread_mutex_destroy(&kconsumer_data
.pid_mutex
);
389 DBG("Closing kernel fd");
390 close(kernel_tracer_fd
);
393 DBG("Unloading kernel modules");
394 modprobe_remove_kernel_modules();
397 close(thread_quit_pipe
[0]);
398 close(thread_quit_pipe
[1]);
402 * Send data on a unix socket using the liblttsessiondcomm API.
404 * Return lttcomm error code.
406 static int send_unix_sock(int sock
, void *buf
, size_t len
)
408 /* Check valid length */
413 return lttcomm_send_unix_sock(sock
, buf
, len
);
417 * Free memory of a command context structure.
419 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
421 DBG("Clean command context structure");
423 if ((*cmd_ctx
)->llm
) {
424 free((*cmd_ctx
)->llm
);
426 if ((*cmd_ctx
)->lsm
) {
427 free((*cmd_ctx
)->lsm
);
435 * Send all stream fds of kernel channel to the consumer.
437 static int send_kconsumer_channel_streams(struct consumer_data
*consumer_data
,
438 int sock
, struct ltt_kernel_channel
*channel
)
441 struct ltt_kernel_stream
*stream
;
442 struct lttcomm_consumer_msg lkm
;
444 DBG("Sending streams of channel %s to kernel consumer",
445 channel
->channel
->name
);
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 */
452 DBG("Sending channel %d to consumer", lkm
.u
.channel
.channel_key
);
453 ret
= lttcomm_send_unix_sock(sock
, &lkm
, sizeof(lkm
));
455 perror("send consumer channel");
460 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
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
));
475 perror("send consumer stream");
478 ret
= lttcomm_send_fds_unix_sock(sock
, &stream
->fd
, 1);
480 perror("send consumer stream ancillary data");
485 DBG("consumer channel streams sent");
494 * Send all stream fds of UST channel to the consumer.
496 static int send_ustconsumer_channel_streams(struct consumer_data
*consumer_data
,
497 int sock
, struct ltt_ust_channel
*channel
)
500 struct ltt_ust_stream
*stream
;
501 struct lttcomm_consumer_msg lum
;
503 DBG("Sending streams of channel %s to UST consumer",
507 lum
.cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
509 * We need to keep shm_fd open to make sure this key stays
510 * unique within the session daemon.
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
));
518 perror("send consumer channel");
521 fds
[0] = channel
->obj
->shm_fd
;
522 fds
[1] = channel
->obj
->wait_fd
;
523 ret
= lttcomm_send_fds_unix_sock(sock
, fds
, 2);
525 perror("send consumer channel ancillary data");
530 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
533 if (!stream
->obj
->shm_fd
) {
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
));
547 perror("send consumer stream");
550 fds
[0] = stream
->obj
->shm_fd
;
551 fds
[1] = stream
->obj
->wait_fd
;
552 ret
= lttcomm_send_fds_unix_sock(sock
, fds
, 2);
554 perror("send consumer stream ancillary data");
559 DBG("consumer channel streams sent");
568 * Send all stream fds of the kernel session to the consumer.
570 static int send_kconsumer_session_streams(struct consumer_data
*consumer_data
,
571 struct ltt_kernel_session
*session
)
574 struct ltt_kernel_channel
*chan
;
575 struct lttcomm_consumer_msg lkm
;
576 int sock
= session
->consumer_fd
;
578 DBG("Sending metadata stream fd");
580 /* Extra protection. It's NOT supposed to be set to 0 at this point */
581 if (session
->consumer_fd
== 0) {
582 session
->consumer_fd
= consumer_data
->cmd_sock
;
585 if (session
->metadata_stream_fd
!= 0) {
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
));
594 perror("send consumer channel");
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
));
610 perror("send consumer stream");
613 ret
= lttcomm_send_fds_unix_sock(sock
, &session
->metadata_stream_fd
, 1);
615 perror("send consumer stream");
620 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
621 ret
= send_kconsumer_channel_streams(consumer_data
, sock
, chan
);
627 DBG("consumer fds (metadata and channel streams) sent");
636 * Send all stream fds of the UST session to the consumer.
638 static int send_ustconsumer_session_streams(struct consumer_data
*consumer_data
,
639 struct ltt_ust_session
*session
)
642 struct ltt_ust_channel
*chan
;
643 struct lttcomm_consumer_msg lum
;
644 int sock
= session
->consumer_fd
;
646 DBG("Sending metadata stream fd");
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
;
653 if (session
->metadata
->obj
->shm_fd
!= 0) {
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
));
664 perror("send consumer channel");
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);
671 perror("send consumer metadata channel");
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
));
687 perror("send consumer metadata stream");
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);
694 perror("send consumer stream");
699 cds_list_for_each_entry(chan
, &session
->channels
.head
, list
) {
700 ret
= send_ustconsumer_channel_streams(consumer_data
, sock
, chan
);
706 DBG("consumer fds (metadata and channel streams) sent");
715 * Notify UST applications using the shm mmap futex.
717 static int notify_ust_apps(int active
)
721 DBG("Notifying applications of session daemon state: %d", active
);
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
) {
729 /* Wake waiting process */
730 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
732 /* Apps notified successfully */
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
744 * Return total size of the buffer pointed by buf.
746 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
752 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
753 if (cmd_ctx
->llm
== NULL
) {
759 /* Copy common data */
760 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
761 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
763 cmd_ctx
->llm
->data_size
= size
;
764 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
773 * Update the kernel poll set of all channel fd available over all tracing
774 * session. Add the wakeup pipe at the end of the set.
776 static int update_kernel_poll(struct lttng_poll_event
*events
)
779 struct ltt_session
*session
;
780 struct ltt_kernel_channel
*channel
;
782 DBG("Updating kernel poll set");
785 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
786 session_lock(session
);
787 if (session
->kernel_session
== NULL
) {
788 session_unlock(session
);
792 cds_list_for_each_entry(channel
,
793 &session
->kernel_session
->channel_list
.head
, list
) {
794 /* Add channel fd to the kernel poll set */
795 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
797 session_unlock(session
);
800 DBG("Channel fd %d added to kernel set", channel
->fd
);
802 session_unlock(session
);
804 session_unlock_list();
809 session_unlock_list();
814 * Find the channel fd from 'fd' over all tracing session. When found, check
815 * for new channel stream and send those stream fds to the kernel consumer.
817 * Useful for CPU hotplug feature.
819 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
822 struct ltt_session
*session
;
823 struct ltt_kernel_channel
*channel
;
825 DBG("Updating kernel streams for channel fd %d", fd
);
828 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
829 session_lock(session
);
830 if (session
->kernel_session
== NULL
) {
831 session_unlock(session
);
835 /* This is not suppose to be 0 but this is an extra security check */
836 if (session
->kernel_session
->consumer_fd
== 0) {
837 session
->kernel_session
->consumer_fd
= consumer_data
->cmd_sock
;
840 cds_list_for_each_entry(channel
,
841 &session
->kernel_session
->channel_list
.head
, list
) {
842 if (channel
->fd
== fd
) {
843 DBG("Channel found, updating kernel streams");
844 ret
= kernel_open_channel_stream(channel
);
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
854 if (session
->kernel_session
->consumer_fds_sent
== 1) {
855 ret
= send_kconsumer_channel_streams(consumer_data
,
856 session
->kernel_session
->consumer_fd
, channel
);
864 session_unlock(session
);
866 session_unlock_list();
870 session_unlock(session
);
871 session_unlock_list();
876 * This thread manage event coming from the kernel.
878 * Features supported in this thread:
881 static void *thread_manage_kernel(void *data
)
883 int ret
, i
, pollfd
, update_poll_flag
= 1;
884 uint32_t revents
, nb_fd
;
886 struct lttng_poll_event events
;
888 DBG("Thread manage kernel started");
890 ret
= create_thread_poll_set(&events
, 2);
895 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
901 if (update_poll_flag
== 1) {
903 * Reset number of fd in the poll set. Always 2 since there is the thread
904 * quit pipe and the kernel pipe.
908 ret
= update_kernel_poll(&events
);
912 update_poll_flag
= 0;
915 nb_fd
= LTTNG_POLL_GETNB(&events
);
917 DBG("Thread kernel polling on %d fds", nb_fd
);
919 /* Zeroed the poll events */
920 lttng_poll_reset(&events
);
922 /* Poll infinite value of time */
923 ret
= lttng_poll_wait(&events
, -1);
926 } else if (ret
== 0) {
927 /* Should not happen since timeout is infinite */
928 ERR("Return value of poll is 0 with an infinite timeout.\n"
929 "This should not have happened! Continuing...");
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
);
938 /* Thread quit pipe has been closed. Killing thread. */
939 ret
= check_thread_quit_pipe(pollfd
, revents
);
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;
951 * New CPU detected by the kernel. Adding kernel stream to
952 * kernel session and updating the kernel consumer
954 if (revents
& LPOLLIN
) {
955 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
961 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
962 * and unregister kernel stream at this point.
970 DBG("Kernel thread dying");
971 close(kernel_poll_pipe
[0]);
972 close(kernel_poll_pipe
[1]);
974 lttng_poll_clean(&events
);
980 * This thread manage the consumer error sent back to the session daemon.
982 static void *thread_manage_consumer(void *data
)
984 int sock
= 0, i
, ret
, pollfd
;
985 uint32_t revents
, nb_fd
;
986 enum lttcomm_return_code code
;
987 struct lttng_poll_event events
;
988 struct consumer_data
*consumer_data
= data
;
990 DBG("[thread] Manage consumer started");
992 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
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.
1001 ret
= create_thread_poll_set(&events
, 2);
1006 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1011 nb_fd
= LTTNG_POLL_GETNB(&events
);
1013 /* Inifinite blocking call, waiting for transmission */
1014 ret
= lttng_poll_wait(&events
, -1);
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
);
1024 /* Thread quit pipe has been closed. Killing thread. */
1025 ret
= check_thread_quit_pipe(pollfd
, revents
);
1030 /* Event on the registration socket */
1031 if (pollfd
== consumer_data
->err_sock
) {
1032 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1033 ERR("consumer err socket poll error");
1039 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1044 DBG2("Receiving code from consumer err_sock");
1046 /* Getting status code from kconsumerd */
1047 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1048 sizeof(enum lttcomm_return_code
));
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");
1061 /* Signal condition to tell that the kconsumerd is ready */
1062 sem_post(&consumer_data
->sem
);
1063 DBG("consumer command socket ready");
1065 ERR("consumer error when waiting for SOCK_READY : %s",
1066 lttcomm_get_readable_code(-code
));
1070 /* Remove the kconsumerd error sock since we've established a connexion */
1071 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1076 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1081 /* Update number of fd */
1082 nb_fd
= LTTNG_POLL_GETNB(&events
);
1084 /* Inifinite blocking call, waiting for transmission */
1085 ret
= lttng_poll_wait(&events
, -1);
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
);
1095 /* Thread quit pipe has been closed. Killing thread. */
1096 ret
= check_thread_quit_pipe(pollfd
, revents
);
1101 /* Event on the kconsumerd socket */
1102 if (pollfd
== sock
) {
1103 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1104 ERR("consumer err socket second poll error");
1110 /* Wait for any kconsumerd error */
1111 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1112 sizeof(enum lttcomm_return_code
));
1114 ERR("consumer closed the command socket");
1118 ERR("consumer return code : %s", lttcomm_get_readable_code(-code
));
1121 DBG("consumer thread dying");
1122 close(consumer_data
->err_sock
);
1123 close(consumer_data
->cmd_sock
);
1126 unlink(consumer_data
->err_unix_sock_path
);
1127 unlink(consumer_data
->cmd_unix_sock_path
);
1128 consumer_data
->pid
= 0;
1130 lttng_poll_clean(&events
);
1136 * This thread manage application communication.
1138 static void *thread_manage_apps(void *data
)
1141 uint32_t revents
, nb_fd
;
1142 struct ust_command ust_cmd
;
1143 struct lttng_poll_event events
;
1145 DBG("[thread] Manage application started");
1147 rcu_register_thread();
1148 rcu_thread_online();
1150 ret
= create_thread_poll_set(&events
, 2);
1155 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1161 /* Zeroed the events structure */
1162 lttng_poll_reset(&events
);
1164 nb_fd
= LTTNG_POLL_GETNB(&events
);
1166 DBG("Apps thread polling on %d fds", nb_fd
);
1168 /* Inifinite blocking call, waiting for transmission */
1169 ret
= lttng_poll_wait(&events
, -1);
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
);
1179 /* Thread quit pipe has been closed. Killing thread. */
1180 ret
= check_thread_quit_pipe(pollfd
, revents
);
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");
1190 } else if (revents
& LPOLLIN
) {
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");
1198 /* Register applicaton to the session daemon */
1199 ret
= ust_app_register(&ust_cmd
.reg_msg
,
1202 /* Only critical ENOMEM error can be returned here */
1206 ret
= ustctl_register_done(ust_cmd
.sock
);
1209 * If the registration is not possible, we simply
1210 * unregister the apps and continue
1212 ust_app_unregister(ust_cmd
.sock
);
1215 * We just need here to monitor the close of the UST
1216 * socket and poll set monitor those by default.
1218 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, 0);
1223 DBG("Apps with sock %d added to poll set",
1230 * At this point, we know that a registered application made
1231 * the event at poll_wait.
1233 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1234 /* Removing from the poll set */
1235 ret
= lttng_poll_del(&events
, pollfd
);
1241 ust_app_unregister(pollfd
);
1249 DBG("Application communication apps dying");
1250 close(apps_cmd_pipe
[0]);
1251 close(apps_cmd_pipe
[1]);
1253 lttng_poll_clean(&events
);
1255 rcu_thread_offline();
1256 rcu_unregister_thread();
1261 * Dispatch request from the registration threads to the application
1262 * communication thread.
1264 static void *thread_dispatch_ust_registration(void *data
)
1267 struct cds_wfq_node
*node
;
1268 struct ust_command
*ust_cmd
= NULL
;
1270 DBG("[thread] Dispatch UST command started");
1272 while (!dispatch_thread_exit
) {
1273 /* Atomically prepare the queue futex */
1274 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1277 /* Dequeue command for registration */
1278 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1280 DBG("Woken up but nothing in the UST command queue");
1281 /* Continue thread execution */
1285 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
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
);
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 :)
1298 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1299 sizeof(struct ust_command
));
1301 perror("write apps cmd pipe");
1302 if (errno
== EBADF
) {
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
1313 } while (node
!= NULL
);
1315 /* Futex wait on queue. Blocking call on futex() */
1316 futex_nto1_wait(&ust_cmd_queue
.futex
);
1320 DBG("Dispatch thread dying");
1325 * This thread manage application registration.
1327 static void *thread_registration_apps(void *data
)
1329 int sock
= 0, i
, ret
, pollfd
;
1330 uint32_t revents
, nb_fd
;
1331 struct lttng_poll_event events
;
1333 * Get allocated in this thread, enqueued to a global queue, dequeued and
1334 * freed in the manage apps thread.
1336 struct ust_command
*ust_cmd
= NULL
;
1338 DBG("[thread] Manage application registration started");
1340 ret
= lttcomm_listen_unix_sock(apps_sock
);
1346 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1347 * more will be added to this poll set.
1349 ret
= create_thread_poll_set(&events
, 2);
1354 /* Add the application registration socket */
1355 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1360 /* Notify all applications to register */
1361 ret
= notify_ust_apps(1);
1363 ERR("Failed to notify applications or create the wait shared memory.\n"
1364 "Execution continues but there might be problem for already\n"
1365 "running applications that wishes to register.");
1369 DBG("Accepting application registration");
1371 nb_fd
= LTTNG_POLL_GETNB(&events
);
1373 /* Inifinite blocking call, waiting for transmission */
1374 ret
= lttng_poll_wait(&events
, -1);
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
);
1384 /* Thread quit pipe has been closed. Killing thread. */
1385 ret
= check_thread_quit_pipe(pollfd
, revents
);
1390 /* Event on the registration socket */
1391 if (pollfd
== apps_sock
) {
1392 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1393 ERR("Register apps socket poll error");
1395 } else if (revents
& LPOLLIN
) {
1396 sock
= lttcomm_accept_unix_sock(apps_sock
);
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");
1409 * Using message-based transmissions to ensure we don't
1410 * have to deal with partially received messages.
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
)) {
1416 perror("lttcomm_recv_unix_sock register apps");
1418 ERR("Wrong size received on apps register");
1425 ust_cmd
->sock
= sock
;
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
);
1435 * Lock free enqueue the registration request. The red pill
1436 * has been taken! This apps will be part of the *system*.
1438 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1441 * Wake the registration queue futex. Implicit memory
1442 * barrier with the exchange in cds_wfq_enqueue.
1444 futex_nto1_wake(&ust_cmd_queue
.futex
);
1451 DBG("UST Registration thread dying");
1453 /* Notify that the registration thread is gone */
1458 unlink(apps_unix_sock_path
);
1460 lttng_poll_clean(&events
);
1466 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1467 * exec or it will fails.
1469 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1472 struct timespec timeout
;
1474 timeout
.tv_sec
= DEFAULT_SEM_WAIT_TIMEOUT
;
1475 timeout
.tv_nsec
= 0;
1477 /* Setup semaphore */
1478 ret
= sem_init(&consumer_data
->sem
, 0, 0);
1480 PERROR("sem_init consumer semaphore");
1484 ret
= pthread_create(&consumer_data
->thread
, NULL
,
1485 thread_manage_consumer
, consumer_data
);
1487 PERROR("pthread_create consumer");
1492 /* Get time for sem_timedwait absolute timeout */
1493 ret
= clock_gettime(CLOCK_REALTIME
, &timeout
);
1495 PERROR("clock_gettime spawn consumer");
1496 /* Infinite wait for the kconsumerd thread to be ready */
1497 ret
= sem_wait(&consumer_data
->sem
);
1499 /* Normal timeout if the gettime was successful */
1500 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1501 ret
= sem_timedwait(&consumer_data
->sem
, &timeout
);
1505 if (errno
== ETIMEDOUT
) {
1507 * Call has timed out so we kill the kconsumerd_thread and return
1510 ERR("The consumer thread was never ready. Killing it");
1511 ret
= pthread_cancel(consumer_data
->thread
);
1513 PERROR("pthread_cancel consumer thread");
1516 PERROR("semaphore wait failed consumer thread");
1521 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1522 if (consumer_data
->pid
== 0) {
1523 ERR("Kconsumerd did not start");
1524 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1527 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1536 * Join consumer thread
1538 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1543 if (consumer_data
->pid
!= 0) {
1544 ret
= kill(consumer_data
->pid
, SIGTERM
);
1546 ERR("Error killing consumer daemon");
1549 return pthread_join(consumer_data
->thread
, &status
);
1556 * Fork and exec a consumer daemon (consumerd).
1558 * Return pid if successful else -1.
1560 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1564 const char *verbosity
;
1566 DBG("Spawning consumerd");
1573 if (opt_verbose
> 1 || opt_verbose_consumer
) {
1574 verbosity
= "--verbose";
1576 verbosity
= "--quiet";
1578 switch (consumer_data
->type
) {
1579 case LTTNG_CONSUMER_KERNEL
:
1580 execl(INSTALL_BIN_PATH
"/lttng-consumerd",
1581 "lttng-consumerd", verbosity
, "-k", NULL
);
1583 case LTTNG_CONSUMER_UST
:
1584 execl(INSTALL_BIN_PATH
"/lttng-consumerd",
1585 "lttng-consumerd", verbosity
, "-u", NULL
);
1588 perror("unknown consumer type");
1592 perror("kernel start consumer exec");
1595 } else if (pid
> 0) {
1598 perror("start consumer fork");
1605 * Spawn the consumerd daemon and session daemon thread.
1607 static int start_consumerd(struct consumer_data
*consumer_data
)
1611 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1612 if (consumer_data
->pid
!= 0) {
1613 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1617 ret
= spawn_consumerd(consumer_data
);
1619 ERR("Spawning consumerd failed");
1620 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
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
);
1629 DBG2("Spawning consumer control thread");
1630 ret
= spawn_consumer_thread(consumer_data
);
1632 ERR("Fatal error spawning consumer control thread");
1644 * modprobe_kernel_modules
1646 static int modprobe_kernel_modules(void)
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
);
1657 perror("snprintf modprobe");
1660 modprobe
[sizeof(modprobe
) - 1] = '\0';
1661 ret
= system(modprobe
);
1663 ERR("Unable to launch modprobe for module %s",
1664 kernel_modules_list
[i
].name
);
1665 } else if (kernel_modules_list
[i
].required
1666 && WEXITSTATUS(ret
) != 0) {
1667 ERR("Unable to load module %s",
1668 kernel_modules_list
[i
].name
);
1670 DBG("Modprobe successfully %s",
1671 kernel_modules_list
[i
].name
);
1682 static int mount_debugfs(char *path
)
1685 char *type
= "debugfs";
1687 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1689 PERROR("Cannot create debugfs path");
1693 ret
= mount(type
, path
, type
, 0, NULL
);
1695 PERROR("Cannot mount debugfs");
1699 DBG("Mounted debugfs successfully at %s", path
);
1706 * Setup necessary data for kernel tracer action.
1708 static void init_kernel_tracer(void)
1711 char *proc_mounts
= "/proc/mounts";
1713 char *debugfs_path
= NULL
, *lttng_path
= NULL
;
1716 /* Detect debugfs */
1717 fp
= fopen(proc_mounts
, "r");
1719 ERR("Unable to probe %s", proc_mounts
);
1723 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1724 if (strstr(line
, "debugfs") != NULL
) {
1725 /* Remove first string */
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
);
1736 /* Mount debugfs if needded */
1737 if (debugfs_path
== NULL
) {
1738 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1740 perror("asprintf debugfs path");
1743 ret
= mount_debugfs(debugfs_path
);
1745 perror("Cannot mount debugfs");
1750 /* Modprobe lttng kernel modules */
1751 ret
= modprobe_kernel_modules();
1756 /* Setup lttng kernel path */
1757 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1759 perror("asprintf lttng path");
1763 /* Open debugfs lttng */
1764 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1765 if (kernel_tracer_fd
< 0) {
1766 DBG("Failed to open %s", lttng_path
);
1772 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1782 WARN("No kernel tracer available");
1783 kernel_tracer_fd
= 0;
1788 * Init tracing by creating trace directory and sending fds kernel consumer.
1790 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
1794 if (session
->consumer_fds_sent
== 0) {
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.
1800 if (session
->consumer_fd
== 0) {
1801 session
->consumer_fd
= kconsumer_data
.cmd_sock
;
1804 ret
= send_kconsumer_session_streams(&kconsumer_data
, session
);
1806 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1810 session
->consumer_fds_sent
= 1;
1818 * Init tracing by creating trace directory and sending fds ust consumer.
1820 static int init_ust_tracing(struct ltt_ust_session
*session
)
1824 if (session
->consumer_fds_sent
== 0) {
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.
1830 if (session
->consumer_fd
== 0) {
1831 session
->consumer_fd
= ustconsumer_data
.cmd_sock
;
1834 ret
= send_ustconsumer_session_streams(&ustconsumer_data
, session
);
1836 ret
= LTTCOMM_UST_CONSUMER_FAIL
;
1840 session
->consumer_fds_sent
= 1;
1848 * Create an UST session and add it to the session ust list.
1850 static int create_ust_session(struct ltt_session
*session
,
1851 struct lttng_domain
*domain
)
1854 struct ltt_ust_session
*lus
= NULL
;
1855 struct ust_app
*app
;
1857 switch (domain
->type
) {
1858 case LTTNG_DOMAIN_UST_PID
:
1859 app
= ust_app_find_by_pid(domain
->attr
.pid
);
1861 ret
= LTTCOMM_APP_NOT_FOUND
;
1866 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
1870 DBG("Creating UST session");
1872 lus
= trace_ust_create_session(session
->path
, domain
->attr
.pid
, domain
);
1874 ret
= LTTCOMM_UST_SESS_FAIL
;
1878 ret
= mkdir_recursive(lus
->path
, S_IRWXU
| S_IRWXG
,
1879 geteuid(), allowed_group());
1881 if (ret
!= -EEXIST
) {
1882 ERR("Trace directory creation error");
1883 ret
= LTTCOMM_UST_SESS_FAIL
;
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
);
1893 ret
= LTTCOMM_APP_NOT_FOUND
;
1896 /* Create session on the UST tracer */
1897 ret
= ustctl_create_session(app
->key
.sock
, lus
);
1899 ret
= LTTCOMM_UST_SESS_FAIL
;
1905 case LTTNG_DOMAIN_UST
:
1906 /* Create session on the UST tracer */
1907 ret
= ustctl_create_session(app
->key
.sock
, lus
);
1909 ret
= LTTCOMM_UST_SESS_FAIL
;
1913 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1919 lus
->sock
= app
->sock
;
1921 session
->ust_session
= lus
;
1922 printf("%p\n", session
->ust_session
);
1932 * Create a kernel tracer session then create the default channel.
1934 static int create_kernel_session(struct ltt_session
*session
)
1938 DBG("Creating kernel session");
1940 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1942 ret
= LTTCOMM_KERN_SESS_FAIL
;
1946 /* Set kernel consumer socket fd */
1947 if (kconsumer_data
.cmd_sock
) {
1948 session
->kernel_session
->consumer_fd
= kconsumer_data
.cmd_sock
;
1951 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1952 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1954 if (ret
!= -EEXIST
) {
1955 ERR("Trace directory creation error");
1965 * Using the session list, filled a lttng_session array to send back to the
1966 * client for session listing.
1968 * The session list lock MUST be acquired before calling this function. Use
1969 * session_lock_list() and session_unlock_list().
1971 static void list_lttng_sessions(struct lttng_session
*sessions
)
1974 struct ltt_session
*session
;
1976 DBG("Getting all available session");
1978 * Iterate over session list and append data after the control struct in
1981 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1982 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1983 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1984 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1985 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1991 * Fill lttng_channel array of all channels.
1993 static void list_lttng_channels(struct ltt_session
*session
,
1994 struct lttng_channel
*channels
)
1997 struct ltt_kernel_channel
*kchan
;
1999 DBG("Listing channels for session %s", session
->name
);
2001 /* Kernel channels */
2002 if (session
->kernel_session
!= NULL
) {
2003 cds_list_for_each_entry(kchan
,
2004 &session
->kernel_session
->channel_list
.head
, list
) {
2005 /* Copy lttng_channel struct to array */
2006 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
2007 channels
[i
].enabled
= kchan
->enabled
;
2012 /* TODO: Missing UST listing */
2016 * Fill lttng_event array of all events in the channel.
2018 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
2019 struct lttng_event
*events
)
2022 * TODO: This is ONLY kernel. Need UST support.
2025 struct ltt_kernel_event
*event
;
2027 DBG("Listing events for channel %s", kchan
->channel
->name
);
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
);
2032 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
2033 events
[i
].enabled
= event
->enabled
;
2034 switch (event
->event
->instrumentation
) {
2035 case LTTNG_KERNEL_TRACEPOINT
:
2036 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
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
));
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
));
2049 case LTTNG_KERNEL_NOOP
:
2050 events
[i
].type
= LTTNG_EVENT_NOOP
;
2052 case LTTNG_KERNEL_SYSCALL
:
2053 events
[i
].type
= LTTNG_EVENT_SYSCALL
;
2055 case LTTNG_KERNEL_ALL
:
2064 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
2066 static int cmd_disable_channel(struct ltt_session
*session
,
2067 int domain
, char *channel_name
)
2072 case LTTNG_DOMAIN_KERNEL
:
2073 ret
= channel_kernel_disable(session
->kernel_session
,
2075 if (ret
!= LTTCOMM_OK
) {
2079 kernel_wait_quiescent(kernel_tracer_fd
);
2081 case LTTNG_DOMAIN_UST_PID
:
2084 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2095 * Copy channel from attributes and set it in the application channel list.
2098 static int copy_ust_channel_to_app(struct ltt_ust_session *usess,
2099 struct lttng_channel *attr, struct ust_app *app)
2102 struct ltt_ust_channel *uchan, *new_chan;
2104 uchan = trace_ust_get_channel_by_key(usess->channels, attr->name);
2105 if (uchan == NULL) {
2106 ret = LTTCOMM_FATAL;
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;
2117 ret = channel_ust_copy(new_chan, uchan);
2119 ret = LTTCOMM_FATAL;
2129 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
2131 static int cmd_enable_channel(struct ltt_session
*session
,
2132 struct lttng_domain
*domain
, struct lttng_channel
*attr
)
2135 struct ltt_ust_session
*usess
= session
->ust_session
;
2137 DBG("Enabling channel %s for session %s", session
->name
, attr
->name
);
2139 switch (domain
->type
) {
2140 case LTTNG_DOMAIN_KERNEL
:
2142 struct ltt_kernel_channel
*kchan
;
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]);
2150 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
2153 if (ret
!= LTTCOMM_OK
) {
2157 kernel_wait_quiescent(kernel_tracer_fd
);
2160 case LTTNG_DOMAIN_UST
:
2162 struct ltt_ust_channel
*uchan
;
2164 DBG2("Enabling channel for LTTNG_DOMAIN_UST domain");
2166 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2168 if (uchan
== NULL
) {
2169 uchan
= trace_ust_create_channel(attr
, usess
->path
);
2170 if (uchan
== NULL
) {
2171 ret
= LTTCOMM_UST_CHAN_FAIL
;
2175 hashtable_add_unique(usess
->domain_global
.channels
, &uchan
->node
);
2178 ret
= LTTCOMM_UST_CHAN_EXIST
;
2182 /* TODO: Iterate over trace apps to enable that channel */
2186 case LTTNG_DOMAIN_UST_PID
:
2190 struct ltt_ust_channel *uchan;
2191 struct ltt_ust_session *usess;
2192 struct ust_app *app;
2194 usess = trace_ust_get_session_by_pid(&session->ust_session_list,
2196 if (usess == NULL) {
2197 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2201 app = ust_app_get_by_pid(domain->attr.pid);
2203 ret = LTTCOMM_APP_NOT_FOUND;
2208 uchan = trace_ust_get_channel_by_name(attr->name, usess);
2209 if (uchan == NULL) {
2210 ret = channel_ust_create(usess, attr, sock);
2212 ret = channel_ust_enable(usess, uchan, sock);
2215 if (ret != LTTCOMM_OK) {
2219 ret = copy_ust_channel_to_app(usess, attr, app);
2220 if (ret != LTTCOMM_OK) {
2224 DBG("UST channel %s created for app sock %d with pid %d",
2225 attr->name, app->sock, domain->attr.pid);
2227 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2231 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2242 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2244 static int cmd_disable_event(struct ltt_session
*session
, int domain
,
2245 char *channel_name
, char *event_name
)
2250 case LTTNG_DOMAIN_KERNEL
:
2252 struct ltt_kernel_channel
*kchan
;
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
;
2261 ret
= event_kernel_disable_tracepoint(session
->kernel_session
, kchan
, event_name
);
2262 if (ret
!= LTTCOMM_OK
) {
2266 kernel_wait_quiescent(kernel_tracer_fd
);
2269 case LTTNG_DOMAIN_UST
:
2271 struct ltt_ust_session
*ustsession
;
2273 cds_list_for_each_entry(ustsession
, &session
->ust_session_list
.head
, list
) {
2274 struct ltt_ust_channel
*ustchan
;
2276 ustchan
= trace_ust_get_channel_by_name(channel_name
,
2278 if (ustchan
== NULL
) {
2279 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2282 ret
= event_ust_disable_tracepoint(ustsession
, ustchan
, event_name
);
2283 if (ret
!= LTTCOMM_OK
) {
2287 ustctl_wait_quiescent(ustsession
->sock
);
2291 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2292 case LTTNG_DOMAIN_UST_PID
:
2293 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2295 /* TODO: Other UST domains */
2296 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2307 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
2309 static int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
2313 struct ltt_kernel_channel
*kchan
;
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
;
2324 ret
= event_kernel_disable_all(session
->kernel_session
, kchan
);
2325 if (ret
!= LTTCOMM_OK
) {
2329 kernel_wait_quiescent(kernel_tracer_fd
);
2332 /* TODO: Userspace tracing */
2333 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2344 * Command LTTNG_ADD_CONTEXT processed by the client thread.
2346 static int cmd_add_context(struct ltt_session
*session
, int domain
,
2347 char *channel_name
, char *event_name
, struct lttng_event_context
*ctx
)
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
) {
2360 case LTTNG_DOMAIN_UST
:
2362 struct ltt_ust_session
*ustsession
;
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
) {
2375 /* TODO: UST other domains */
2376 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2387 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2389 static int cmd_enable_event(struct ltt_session
*session
, int domain
,
2390 char *channel_name
, struct lttng_event
*event
)
2393 struct lttng_channel
*attr
;
2396 case LTTNG_DOMAIN_KERNEL
:
2398 struct ltt_kernel_channel
*kchan
;
2400 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2401 session
->kernel_session
);
2402 if (kchan
== NULL
) {
2403 attr
= channel_new_default_attr(domain
);
2405 ret
= LTTCOMM_FATAL
;
2408 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2410 /* This call will notify the kernel thread */
2411 ret
= channel_kernel_create(session
->kernel_session
,
2412 attr
, kernel_poll_pipe
[1]);
2413 if (ret
!= LTTCOMM_OK
) {
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
;
2427 ret
= event_kernel_enable_tracepoint(session
->kernel_session
, kchan
, event
);
2428 if (ret
!= LTTCOMM_OK
) {
2432 kernel_wait_quiescent(kernel_tracer_fd
);
2435 case LTTNG_DOMAIN_UST
:
2437 struct ltt_ust_session
*ustsession
;
2439 cds_list_for_each_entry(ustsession
, &session
->ust_session_list
.head
, list
) {
2440 struct ltt_ust_channel
*ustchan
;
2442 ustchan
= trace_ust_get_channel_by_name(channel_name
,
2444 if (ustchan
== NULL
) {
2445 attr
= channel_new_default_attr(domain
);
2447 ret
= LTTCOMM_FATAL
;
2450 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2452 ret
= channel_ust_create(ustsession
,
2453 attr
, ustsession
->sock
);
2454 if (ret
!= LTTCOMM_OK
) {
2459 /* Get the newly created ust channel pointer */
2460 ustchan
= trace_ust_get_channel_by_name(channel_name
,
2462 if (ustchan
== NULL
) {
2463 /* This sould not happen... */
2464 ret
= LTTCOMM_FATAL
;
2468 ret
= event_ust_enable_tracepoint(ustsession
, ustchan
, event
);
2469 if (ret
!= LTTCOMM_OK
) {
2473 ustctl_wait_quiescent(ustsession
->sock
);
2477 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2478 case LTTNG_DOMAIN_UST_PID
:
2479 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2481 /* TODO: UST other domains */
2482 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2493 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
2495 static int cmd_enable_event_all(struct ltt_session
*session
, int domain
,
2496 char *channel_name
, int event_type
)
2499 struct ltt_kernel_channel
*kchan
;
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 */
2507 ret
= channel_kernel_create(session
->kernel_session
, NULL
,
2508 kernel_poll_pipe
[1]);
2509 if (ret
!= LTTCOMM_OK
) {
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
;
2523 switch (event_type
) {
2524 case LTTNG_KERNEL_SYSCALL
:
2525 ret
= event_kernel_enable_all_syscalls(session
->kernel_session
,
2526 kchan
, kernel_tracer_fd
);
2528 case LTTNG_KERNEL_TRACEPOINT
:
2530 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
2531 * events already registered to the channel.
2533 ret
= event_kernel_enable_all_tracepoints(session
->kernel_session
,
2534 kchan
, kernel_tracer_fd
);
2536 case LTTNG_KERNEL_ALL
:
2537 /* Enable syscalls and tracepoints */
2538 ret
= event_kernel_enable_all(session
->kernel_session
,
2539 kchan
, kernel_tracer_fd
);
2542 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2545 if (ret
!= LTTCOMM_OK
) {
2549 kernel_wait_quiescent(kernel_tracer_fd
);
2552 /* TODO: Userspace tracing */
2553 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2564 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2566 static ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
2569 ssize_t nb_events
= 0;
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
;
2580 /* TODO: Userspace listing */
2581 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2588 /* Return negative value to differentiate return code */
2593 * Command LTTNG_START_TRACE processed by the client thread.
2595 static int cmd_start_trace(struct ltt_session
*session
)
2598 struct ltt_kernel_session
*ksession
;
2599 struct ltt_ust_session
*ustsession
;
2602 ksession
= session
->kernel_session
;
2604 /* Kernel tracing */
2605 if (ksession
!= NULL
) {
2606 struct ltt_kernel_channel
*kchan
;
2608 /* Open kernel metadata */
2609 if (ksession
->metadata
== NULL
) {
2610 ret
= kernel_open_metadata(ksession
, ksession
->trace_path
);
2612 ret
= LTTCOMM_KERN_META_FAIL
;
2617 /* Open kernel metadata stream */
2618 if (ksession
->metadata_stream_fd
== 0) {
2619 ret
= kernel_open_metadata_stream(ksession
);
2621 ERR("Kernel create metadata stream failed");
2622 ret
= LTTCOMM_KERN_STREAM_FAIL
;
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
);
2632 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2635 /* Update the stream global counter */
2636 ksession
->stream_count_global
+= ret
;
2640 /* Setup kernel consumer socket and send fds to it */
2641 ret
= init_kernel_tracing(ksession
);
2643 ret
= LTTCOMM_KERN_START_FAIL
;
2647 /* This start the kernel tracing */
2648 ret
= kernel_start_session(ksession
);
2650 ret
= LTTCOMM_KERN_START_FAIL
;
2654 /* Quiescent wait after starting trace */
2655 kernel_wait_quiescent(kernel_tracer_fd
);
2658 /* Start all UST traces */
2659 cds_list_for_each_entry(ustsession
, &session
->ust_session_list
.head
, list
) {
2660 struct ltt_ust_channel
*ustchan
;
2662 /* Open kernel metadata */
2663 if (ustsession
->metadata
== NULL
) {
2664 struct lttng_ust_channel_attr ustattr
;
2666 /* Allocate UST metadata */
2667 ustsession
->metadata
= trace_ust_create_metadata(ustsession
->path
);
2668 if (ustsession
->metadata
== NULL
) {
2669 ret
= LTTCOMM_UST_META_FAIL
;
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
;
2680 /* UST tracer metadata creation */
2681 ret
= ustctl_open_metadata(ustsession
->sock
,
2682 ustsession
->handle
, &ustattr
,
2683 &ustsession
->metadata
->obj
);
2685 ret
= LTTCOMM_UST_META_FAIL
;
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
);
2696 ERR("UST create metadata stream failed");
2697 ret
= LTTCOMM_UST_STREAM_FAIL
;
2700 ret
= asprintf(&ustsession
->metadata
->pathname
, "%s/%s",
2701 ustsession
->path
, "metadata");
2703 perror("asprintf UST create stream");
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
;
2713 ustream
= zmalloc(sizeof(*ustream
));
2715 ret
= LTTCOMM_UST_STREAM_FAIL
;
2718 ret
= ustctl_create_stream(ustsession
->sock
,
2719 ustchan
->obj
, &ustream
->obj
);
2721 ret
= LTTCOMM_UST_STREAM_FAIL
;
2724 ret
= asprintf(&ustream
->pathname
, "%s/%s_%d",
2725 ustchan
->trace_path
, ustchan
->name
,
2726 ustchan
->stream_count
);
2728 perror("asprintf UST create stream");
2731 cds_list_add(&ustream
->list
, &ustchan
->stream_list
.head
);
2732 ustchan
->stream_count
++;
2736 /* Setup UST consumer socket and send fds to it */
2737 ret
= init_ust_tracing(ustsession
);
2739 ret
= LTTCOMM_UST_START_FAIL
;
2743 /* This start the UST tracing */
2744 ret
= ustctl_start_session(ustsession
->sock
, ustsession
->handle
);
2746 ret
= LTTCOMM_UST_START_FAIL
;
2750 /* Quiescent wait after starting trace */
2751 ustctl_wait_quiescent(ustsession
->sock
);
2761 * Command LTTNG_STOP_TRACE processed by the client thread.
2763 static int cmd_stop_trace(struct ltt_session
*session
)
2766 struct ltt_kernel_channel
*kchan
;
2767 struct ltt_kernel_session
*ksession
;
2768 struct ltt_ust_session
*ustsession
;
2769 struct ltt_ust_channel
*ustchan
;
2772 ksession
= session
->kernel_session
;
2775 if (ksession
!= NULL
) {
2776 DBG("Stop kernel tracing");
2778 /* Flush all buffers before stopping */
2779 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2781 ERR("Kernel metadata flush failed");
2784 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2785 ret
= kernel_flush_buffer(kchan
);
2787 ERR("Kernel flush buffer error");
2791 ret
= kernel_stop_session(ksession
);
2793 ret
= LTTCOMM_KERN_STOP_FAIL
;
2797 kernel_wait_quiescent(kernel_tracer_fd
);
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
);
2806 ERR("UST metadata flush failed");
2809 cds_list_for_each_entry(ustchan
, &ustsession
->channels
.head
, list
) {
2810 ret
= ustctl_flush_buffer(ustsession
->sock
, ustchan
->obj
);
2812 ERR("UST flush buffer error");
2816 ret
= ustctl_stop_session(ustsession
->sock
, ustsession
->handle
);
2818 ret
= LTTCOMM_KERN_STOP_FAIL
;
2822 ustctl_wait_quiescent(ustsession
->sock
);
2832 * Command LTTNG_CREATE_SESSION processed by the client thread.
2834 static int cmd_create_session(char *name
, char *path
)
2838 ret
= session_create(name
, path
);
2839 if (ret
!= LTTCOMM_OK
) {
2850 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2852 static int cmd_destroy_session(struct ltt_session
*session
, char *name
)
2856 /* Clean kernel session teardown */
2857 teardown_kernel_session(session
);
2860 * Must notify the kernel thread here to update it's poll setin order
2861 * to remove the channel(s)' fd just destroyed.
2863 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
2865 perror("write kernel poll pipe");
2868 ret
= session_destroy(session
);
2874 * Command LTTNG_CALIBRATE processed by the client thread.
2876 static int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2881 case LTTNG_DOMAIN_KERNEL
:
2883 struct lttng_kernel_calibrate kcalibrate
;
2885 kcalibrate
.type
= calibrate
->type
;
2886 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2888 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2894 /* TODO: Userspace tracing */
2895 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2906 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2908 static int cmd_register_consumer(struct ltt_session
*session
, int 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
;
2921 sock
= lttcomm_connect_unix_sock(sock_path
);
2923 ret
= LTTCOMM_CONNECT_FAIL
;
2927 session
->kernel_session
->consumer_fd
= sock
;
2930 /* TODO: Userspace tracing */
2931 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2942 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2944 static ssize_t
cmd_list_domains(struct ltt_session
*session
,
2945 struct lttng_domain
**domains
)
2950 if (session
->kernel_session
!= NULL
) {
2954 /* TODO: User-space tracer domain support */
2956 *domains
= malloc(nb_dom
* sizeof(struct lttng_domain
));
2957 if (*domains
== NULL
) {
2958 ret
= -LTTCOMM_FATAL
;
2962 (*domains
)[0].type
= LTTNG_DOMAIN_KERNEL
;
2971 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2973 static ssize_t
cmd_list_channels(struct ltt_session
*session
,
2974 struct lttng_channel
**channels
)
2977 ssize_t nb_chan
= 0;
2979 if (session
->kernel_session
!= NULL
) {
2980 nb_chan
+= session
->kernel_session
->channel_count
;
2983 *channels
= malloc(nb_chan
* sizeof(struct lttng_channel
));
2984 if (*channels
== NULL
) {
2985 ret
= -LTTCOMM_FATAL
;
2989 list_lttng_channels(session
, *channels
);
2991 /* TODO UST support */
3000 * Command LTTNG_LIST_EVENTS processed by the client thread.
3002 static ssize_t
cmd_list_events(struct ltt_session
*session
,
3003 char *channel_name
, struct lttng_event
**events
)
3006 ssize_t nb_event
= 0;
3007 struct ltt_kernel_channel
*kchan
= NULL
;
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
;
3016 nb_event
+= kchan
->event_count
;
3019 *events
= malloc(nb_event
* sizeof(struct lttng_event
));
3020 if (*events
== NULL
) {
3021 ret
= -LTTCOMM_FATAL
;
3025 list_lttng_events(kchan
, *events
);
3027 /* TODO: User-space tracer support */
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.
3040 * Return any error encountered or 0 for success.
3042 static int process_client_msg(struct command_ctx
*cmd_ctx
)
3044 int ret
= LTTCOMM_OK
;
3045 int need_tracing_session
= 1;
3047 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
3050 * Check for command that don't needs to allocate a returned payload. We do
3051 * this here so we don't have to make the call for no payload at each
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
:
3062 /* Setup lttng message with no payload */
3063 ret
= setup_lttng_msg(cmd_ctx
, 0);
3065 /* This label does not try to unlock the session */
3066 goto init_setup_error
;
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
:
3076 need_tracing_session
= 0;
3079 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3080 session_lock_list();
3081 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3082 session_unlock_list();
3083 if (cmd_ctx
->session
== NULL
) {
3084 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
3085 ret
= LTTCOMM_SESS_NOT_FOUND
;
3087 /* If no session name specified */
3088 ret
= LTTCOMM_SELECT_SESS
;
3092 /* Acquire lock for the session */
3093 session_lock(cmd_ctx
->session
);
3099 * Check domain type for specific "pre-action".
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
;
3113 /* Need a session for kernel command */
3114 if (need_tracing_session
) {
3115 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3116 ret
= create_kernel_session(cmd_ctx
->session
);
3118 ret
= LTTCOMM_KERN_SESS_FAIL
;
3123 /* Start the kernel consumer daemon */
3124 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3125 if (kconsumer_data
.pid
== 0 &&
3126 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3127 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3128 ret
= start_consumerd(&kconsumer_data
);
3130 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
3134 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3137 case LTTNG_DOMAIN_UST
:
3138 case LTTNG_DOMAIN_UST_EXEC_NAME
:
3139 case LTTNG_DOMAIN_UST_PID
:
3140 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
3142 struct ltt_ust_session
*usess
;
3143 if (need_tracing_session
) {
3144 if (cmd_ctx
->session
->ust_session
== NULL
) {
3145 ret
= create_ust_session(cmd_ctx
->session
,
3146 &cmd_ctx
->lsm
->domain
);
3147 if (ret
!= LTTCOMM_OK
) {
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
);
3158 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
3162 pthread_mutex_unlock(&ustconsumer_data
.pid_mutex
);
3169 /* Process by command type */
3170 switch (cmd_ctx
->lsm
->cmd_type
) {
3171 case LTTNG_ADD_CONTEXT
:
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
);
3179 case LTTNG_DISABLE_CHANNEL
:
3181 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3182 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3185 case LTTNG_DISABLE_EVENT
:
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
);
3193 case LTTNG_DISABLE_ALL_EVENT
:
3195 DBG("Disabling all events");
3197 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3198 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3201 case LTTNG_ENABLE_CHANNEL
:
3203 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3204 &cmd_ctx
->lsm
->u
.channel
.chan
);
3207 case LTTNG_ENABLE_EVENT
:
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
);
3214 case LTTNG_ENABLE_ALL_EVENT
:
3216 DBG("Enabling all events");
3218 ret
= cmd_enable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3219 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3220 cmd_ctx
->lsm
->u
.enable
.event
.type
);
3223 case LTTNG_LIST_TRACEPOINTS
:
3225 struct lttng_event
*events
;
3228 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3229 if (nb_events
< 0) {
3235 * Setup lttng message with payload size set to the event list size in
3236 * bytes and then copy list into the llm payload.
3238 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3244 /* Copy event list into message payload */
3245 memcpy(cmd_ctx
->llm
->payload
, events
,
3246 sizeof(struct lttng_event
) * nb_events
);
3253 case LTTNG_START_TRACE
:
3255 ret
= cmd_start_trace(cmd_ctx
->session
);
3258 case LTTNG_STOP_TRACE
:
3260 ret
= cmd_stop_trace(cmd_ctx
->session
);
3263 case LTTNG_CREATE_SESSION
:
3265 ret
= cmd_create_session(cmd_ctx
->lsm
->session
.name
,
3266 cmd_ctx
->lsm
->session
.path
);
3269 case LTTNG_DESTROY_SESSION
:
3271 ret
= cmd_destroy_session(cmd_ctx
->session
,
3272 cmd_ctx
->lsm
->session
.name
);
3275 case LTTNG_LIST_DOMAINS
:
3278 struct lttng_domain
*domains
;
3280 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3286 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3291 /* Copy event list into message payload */
3292 memcpy(cmd_ctx
->llm
->payload
, domains
,
3293 nb_dom
* sizeof(struct lttng_domain
));
3300 case LTTNG_LIST_CHANNELS
:
3303 struct lttng_channel
*channels
;
3305 nb_chan
= cmd_list_channels(cmd_ctx
->session
, &channels
);
3311 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3316 /* Copy event list into message payload */
3317 memcpy(cmd_ctx
->llm
->payload
, channels
,
3318 nb_chan
* sizeof(struct lttng_channel
));
3325 case LTTNG_LIST_EVENTS
:
3328 struct lttng_event
*events
= NULL
;
3330 nb_event
= cmd_list_events(cmd_ctx
->session
,
3331 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3337 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3342 /* Copy event list into message payload */
3343 memcpy(cmd_ctx
->llm
->payload
, events
,
3344 nb_event
* sizeof(struct lttng_event
));
3351 case LTTNG_LIST_SESSIONS
:
3353 session_lock_list();
3355 if (session_list_ptr
->count
== 0) {
3356 ret
= LTTCOMM_NO_SESSION
;
3357 session_unlock_list();
3361 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
3362 session_list_ptr
->count
);
3364 session_unlock_list();
3368 /* Filled the session array */
3369 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
3371 session_unlock_list();
3376 case LTTNG_CALIBRATE
:
3378 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3379 &cmd_ctx
->lsm
->u
.calibrate
);
3382 case LTTNG_REGISTER_CONSUMER
:
3384 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3385 cmd_ctx
->lsm
->u
.reg
.path
);
3394 if (cmd_ctx
->llm
== NULL
) {
3395 DBG("Missing llm structure. Allocating one.");
3396 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3400 /* Set return code */
3401 cmd_ctx
->llm
->ret_code
= ret
;
3403 if (cmd_ctx
->session
) {
3404 session_unlock(cmd_ctx
->session
);
3411 * This thread manage all clients request using the unix client socket for
3414 static void *thread_manage_clients(void *data
)
3416 int sock
= 0, ret
, i
, pollfd
;
3417 uint32_t revents
, nb_fd
;
3418 struct command_ctx
*cmd_ctx
= NULL
;
3419 struct lttng_poll_event events
;
3421 DBG("[thread] Manage client started");
3423 rcu_register_thread();
3425 ret
= lttcomm_listen_unix_sock(client_sock
);
3431 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3432 * more will be added to this poll set.
3434 ret
= create_thread_poll_set(&events
, 2);
3439 /* Add the application registration socket */
3440 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3446 * Notify parent pid that we are ready to accept command for client side.
3448 if (opt_sig_parent
) {
3449 kill(ppid
, SIGCHLD
);
3453 DBG("Accepting client command ...");
3455 nb_fd
= LTTNG_POLL_GETNB(&events
);
3457 /* Inifinite blocking call, waiting for transmission */
3458 ret
= lttng_poll_wait(&events
, -1);
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
);
3468 /* Thread quit pipe has been closed. Killing thread. */
3469 ret
= check_thread_quit_pipe(pollfd
, revents
);
3474 /* Event on the registration socket */
3475 if (pollfd
== client_sock
) {
3476 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3477 ERR("Client socket poll error");
3483 DBG("Wait for client response");
3485 sock
= lttcomm_accept_unix_sock(client_sock
);
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");
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");
3504 cmd_ctx
->llm
= NULL
;
3505 cmd_ctx
->session
= NULL
;
3508 * Data is received from the lttng client. The struct
3509 * lttcomm_session_msg (lsm) contains the command and data request of
3512 DBG("Receiving data from client ...");
3513 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
,
3514 sizeof(struct lttcomm_session_msg
));
3516 DBG("Nothing recv() from client... continuing");
3522 // TODO: Validate cmd_ctx including sanity check for
3523 // security purpose.
3525 rcu_thread_online();
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.
3532 ret
= process_client_msg(cmd_ctx
);
3533 rcu_thread_offline();
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.
3540 clean_command_ctx(&cmd_ctx
);
3544 DBG("Sending response (size: %d, retcode: %s)",
3545 cmd_ctx
->lttng_msg_size
,
3546 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3547 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3549 ERR("Failed to send data back to client");
3552 clean_command_ctx(&cmd_ctx
);
3554 /* End of transmission */
3559 DBG("Client thread dying");
3560 unlink(client_unix_sock_path
);
3564 lttng_poll_clean(&events
);
3565 clean_command_ctx(&cmd_ctx
);
3567 rcu_unregister_thread();
3573 * usage function on stderr
3575 static void usage(void)
3577 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
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");
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");
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");
3591 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3595 * daemon argument parsing
3597 static int parse_args(int argc
, char **argv
)
3601 static struct option long_options
[] = {
3602 { "client-sock", 1, 0, 'c' },
3603 { "apps-sock", 1, 0, 'a' },
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' },
3608 { "daemonize", 0, 0, 'd' },
3609 { "sig-parent", 0, 0, 'S' },
3610 { "help", 0, 0, 'h' },
3611 { "group", 1, 0, 'g' },
3612 { "version", 0, 0, 'V' },
3613 { "quiet", 0, 0, 'q' },
3614 { "verbose", 0, 0, 'v' },
3615 { "verbose-consumer", 0, 0, 'Z' },
3620 int option_index
= 0;
3621 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:C:E:D:F:Z",
3622 long_options
, &option_index
);
3629 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
3631 fprintf(stderr
, " with arg %s\n", optarg
);
3635 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3638 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3644 opt_tracing_group
= strdup(optarg
);
3650 fprintf(stdout
, "%s\n", VERSION
);
3656 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3659 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3662 snprintf(ustconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3665 snprintf(ustconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3671 /* Verbose level can increase using multiple -v */
3675 opt_verbose_consumer
+= 1;
3678 /* Unknown option or other error.
3679 * Error is printed by getopt, just return */
3688 * Creates the two needed socket by the daemon.
3689 * apps_sock - The communication socket for all UST apps.
3690 * client_sock - The communication of the cli tool (lttng).
3692 static int init_daemon_socket(void)
3697 old_umask
= umask(0);
3699 /* Create client tool unix socket */
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
);
3707 /* File permission MUST be 660 */
3708 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3710 ERR("Set file permissions failed: %s", client_unix_sock_path
);
3715 /* Create the application unix socket */
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
);
3723 /* File permission MUST be 666 */
3724 ret
= chmod(apps_unix_sock_path
,
3725 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
3727 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
3738 * Check if the global socket is available, and if a daemon is answering at the
3739 * other side. If yes, error is returned.
3741 static int check_existing_daemon(void)
3743 if (access(client_unix_sock_path
, F_OK
) < 0 &&
3744 access(apps_unix_sock_path
, F_OK
) < 0) {
3748 /* Is there anybody out there ? */
3749 if (lttng_session_daemon_alive()) {
3757 * Set the tracing group gid onto the client socket.
3759 * Race window between mkdir and chown is OK because we are going from more
3760 * permissive (root.root) to les permissive (root.tracing).
3762 static int set_permissions(void)
3767 gid
= allowed_group();
3770 WARN("No tracing group detected");
3773 ERR("Missing tracing group. Aborting execution.");
3779 /* Set lttng run dir */
3780 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
3782 ERR("Unable to set group on " LTTNG_RUNDIR
);
3786 /* lttng client socket path */
3787 ret
= chown(client_unix_sock_path
, 0, gid
);
3789 ERR("Unable to set group on %s", client_unix_sock_path
);
3793 /* kconsumer error socket path */
3794 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
3796 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
3800 /* ustconsumer error socket path */
3801 ret
= chown(ustconsumer_data
.err_unix_sock_path
, 0, gid
);
3803 ERR("Unable to set group on %s", ustconsumer_data
.err_unix_sock_path
);
3807 DBG("All permissions are set");
3814 * Create the pipe used to wake up the kernel thread.
3816 static int create_kernel_poll_pipe(void)
3818 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
3822 * Create the application command pipe to wake thread_manage_apps.
3824 static int create_apps_cmd_pipe(void)
3826 return pipe2(apps_cmd_pipe
, O_CLOEXEC
);
3830 * Create the lttng run directory needed for all global sockets and pipe.
3832 static int create_lttng_rundir(void)
3836 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
3838 if (errno
!= EEXIST
) {
3839 ERR("Unable to create " LTTNG_RUNDIR
);
3851 * Setup sockets and directory needed by the kconsumerd communication with the
3854 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
3857 const char *path
= consumer_data
->type
== LTTNG_CONSUMER_KERNEL
?
3858 KCONSUMERD_PATH
: USTCONSUMERD_PATH
;
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
);
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
);
3874 ret
= mkdir(path
, S_IRWXU
| S_IRWXG
);
3876 if (errno
!= EEXIST
) {
3877 ERR("Failed to create %s", path
);
3883 /* Create the kconsumerd error unix socket */
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
);
3892 /* File permission MUST be 660 */
3893 ret
= chmod(consumer_data
->err_unix_sock_path
,
3894 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3896 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
3906 * Signal handler for the daemon
3908 * Simply stop all worker threads, leaving main() return gracefully after
3909 * joining all threads and calling cleanup().
3911 static void sighandler(int sig
)
3915 DBG("SIGPIPE catched");
3918 DBG("SIGINT catched");
3922 DBG("SIGTERM catched");
3931 * Setup signal handler for :
3932 * SIGINT, SIGTERM, SIGPIPE
3934 static int set_signal_handler(void)
3937 struct sigaction sa
;
3940 if ((ret
= sigemptyset(&sigset
)) < 0) {
3941 perror("sigemptyset");
3945 sa
.sa_handler
= sighandler
;
3946 sa
.sa_mask
= sigset
;
3948 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3949 perror("sigaction");
3953 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3954 perror("sigaction");
3958 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3959 perror("sigaction");
3963 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
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.
3972 static void set_ulimit(void)
3977 /* The kernel does not allowed an infinite limit for open files */
3978 lim
.rlim_cur
= 65535;
3979 lim
.rlim_max
= 65535;
3981 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3983 perror("failed to set open files limit");
3990 int main(int argc
, char **argv
)
3994 const char *home_path
;
3996 rcu_register_thread();
3998 /* Create thread quit pipe */
3999 if ((ret
= init_thread_quit_pipe()) < 0) {
4003 /* Parse arguments */
4005 if ((ret
= parse_args(argc
, argv
) < 0)) {
4018 /* Check if daemon is UID = 0 */
4019 is_root
= !getuid();
4022 ret
= create_lttng_rundir();
4027 if (strlen(apps_unix_sock_path
) == 0) {
4028 snprintf(apps_unix_sock_path
, PATH_MAX
,
4029 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
4032 if (strlen(client_unix_sock_path
) == 0) {
4033 snprintf(client_unix_sock_path
, PATH_MAX
,
4034 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
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
);
4043 home_path
= get_home_dir();
4044 if (home_path
== NULL
) {
4045 /* TODO: Add --socket PATH option */
4046 ERR("Can't get HOME directory for sockets creation.");
4051 if (strlen(apps_unix_sock_path
) == 0) {
4052 snprintf(apps_unix_sock_path
, PATH_MAX
,
4053 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
4056 /* Set the cli tool unix socket path */
4057 if (strlen(client_unix_sock_path
) == 0) {
4058 snprintf(client_unix_sock_path
, PATH_MAX
,
4059 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
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());
4069 DBG("Client socket path %s", client_unix_sock_path
);
4070 DBG("Application socket path %s", apps_unix_sock_path
);
4073 * See if daemon already exist.
4075 if ((ret
= check_existing_daemon()) < 0) {
4076 ERR("Already running daemon.\n");
4078 * We do not goto exit because we must not cleanup()
4079 * because a daemon is already running.
4084 /* After this point, we can safely call cleanup() with "goto exit" */
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
4093 ret
= set_consumer_sockets(&kconsumer_data
);
4097 ret
= set_consumer_sockets(&ustconsumer_data
);
4101 /* Setup kernel tracer */
4102 init_kernel_tracer();
4104 /* Set ulimit for open files */
4108 if ((ret
= set_signal_handler()) < 0) {
4112 /* Setup the needed unix socket */
4113 if ((ret
= init_daemon_socket()) < 0) {
4117 /* Set credentials to socket */
4118 if (is_root
&& ((ret
= set_permissions()) < 0)) {
4122 /* Get parent pid if -S, --sig-parent is specified. */
4123 if (opt_sig_parent
) {
4127 /* Setup the kernel pipe for waking up the kernel thread */
4128 if ((ret
= create_kernel_poll_pipe()) < 0) {
4132 /* Setup the thread apps communication pipe. */
4133 if ((ret
= create_apps_cmd_pipe()) < 0) {
4137 /* Init UST command queue. */
4138 cds_wfq_init(&ust_cmd_queue
.queue
);
4140 /* Init UST app hash table */
4144 * Get session list pointer. This pointer MUST NOT be free(). This list is
4145 * statically declared in session.c
4147 session_list_ptr
= session_get_list();
4149 /* Set up max poll set size */
4150 lttng_poll_set_max_size();
4152 /* Create thread to manage the client socket */
4153 ret
= pthread_create(&client_thread
, NULL
,
4154 thread_manage_clients
, (void *) NULL
);
4156 perror("pthread_create clients");
4160 /* Create thread to dispatch registration */
4161 ret
= pthread_create(&dispatch_thread
, NULL
,
4162 thread_dispatch_ust_registration
, (void *) NULL
);
4164 perror("pthread_create dispatch");
4168 /* Create thread to manage application registration. */
4169 ret
= pthread_create(®_apps_thread
, NULL
,
4170 thread_registration_apps
, (void *) NULL
);
4172 perror("pthread_create registration");
4176 /* Create thread to manage application socket */
4177 ret
= pthread_create(&apps_thread
, NULL
,
4178 thread_manage_apps
, (void *) NULL
);
4180 perror("pthread_create apps");
4184 /* Create kernel thread to manage kernel event */
4185 ret
= pthread_create(&kernel_thread
, NULL
,
4186 thread_manage_kernel
, (void *) NULL
);
4188 perror("pthread_create kernel");
4192 ret
= pthread_join(kernel_thread
, &status
);
4194 perror("pthread_join");
4195 goto error
; /* join error, exit without cleanup */
4199 ret
= pthread_join(apps_thread
, &status
);
4201 perror("pthread_join");
4202 goto error
; /* join error, exit without cleanup */
4206 ret
= pthread_join(reg_apps_thread
, &status
);
4208 perror("pthread_join");
4209 goto error
; /* join error, exit without cleanup */
4213 ret
= pthread_join(dispatch_thread
, &status
);
4215 perror("pthread_join");
4216 goto error
; /* join error, exit without cleanup */
4220 ret
= pthread_join(client_thread
, &status
);
4222 perror("pthread_join");
4223 goto error
; /* join error, exit without cleanup */
4226 ret
= join_consumer_thread(&kconsumer_data
);
4228 perror("join_consumer");
4229 goto error
; /* join error, exit without cleanup */
4235 * cleanup() is called when no other thread is running.
4237 rcu_thread_online();
4239 rcu_thread_offline();
4240 rcu_unregister_thread();