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 "kernel-ctl.h"
60 #include "ltt-sessiond.h"
67 struct consumer_data
{
68 enum lttng_consumer_type type
;
70 pthread_t thread
; /* Worker thread interacting with the consumer */
73 /* Mutex to control consumerd pid assignation */
74 pthread_mutex_t pid_mutex
;
80 /* consumer error and command Unix socket path */
81 char err_unix_sock_path
[PATH_MAX
];
82 char cmd_unix_sock_path
[PATH_MAX
];
86 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
87 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
88 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
89 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
92 int opt_verbose
; /* Not static for lttngerr.h */
93 int opt_verbose_consumer
; /* Not static for lttngerr.h */
94 int opt_quiet
; /* Not static for lttngerr.h */
97 const char *opt_tracing_group
;
98 static int opt_sig_parent
;
99 static int opt_daemon
;
100 static int is_root
; /* Set to 1 if the daemon is running as root */
101 static pid_t ppid
; /* Parent PID for --sig-parent option */
103 /* Consumer daemon specific control data */
104 static struct consumer_data kconsumer_data
= {
105 .type
= LTTNG_CONSUMER_KERNEL
,
107 static struct consumer_data ustconsumer_data
= {
108 .type
= LTTNG_CONSUMER_UST
,
111 static int dispatch_thread_exit
;
113 /* Global application Unix socket path */
114 static char apps_unix_sock_path
[PATH_MAX
];
115 /* Global client Unix socket path */
116 static char client_unix_sock_path
[PATH_MAX
];
117 /* global wait shm path for UST */
118 static char wait_shm_path
[PATH_MAX
];
120 /* Sockets and FDs */
121 static int client_sock
;
122 static int apps_sock
;
123 static int kernel_tracer_fd
;
124 static int kernel_poll_pipe
[2];
127 * Quit pipe for all threads. This permits a single cancellation point
128 * for all threads when receiving an event on the pipe.
130 static int thread_quit_pipe
[2];
133 * This pipe is used to inform the thread managing application communication
134 * that a command is queued and ready to be processed.
136 static int apps_cmd_pipe
[2];
138 /* Pthread, Mutexes and Semaphores */
139 static pthread_t apps_thread
;
140 static pthread_t reg_apps_thread
;
141 static pthread_t client_thread
;
142 static pthread_t kernel_thread
;
143 static pthread_t dispatch_thread
;
147 * UST registration command queue. This queue is tied with a futex and uses a N
148 * wakers / 1 waiter implemented and detailed in futex.c/.h
150 * The thread_manage_apps and thread_dispatch_ust_registration interact with
151 * this queue and the wait/wake scheme.
153 static struct ust_cmd_queue ust_cmd_queue
;
156 * Pointer initialized before thread creation.
158 * This points to the tracing session list containing the session count and a
159 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
160 * MUST NOT be taken if you call a public function in session.c.
162 * The lock is nested inside the structure: session_list_ptr->lock. Please use
163 * session_lock_list and session_unlock_list for lock acquisition.
165 static struct ltt_session_list
*session_list_ptr
;
168 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
170 static int create_thread_poll_set(struct lttng_poll_event
*events
,
175 if (events
== NULL
|| size
== 0) {
180 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
186 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
198 * Check if the thread quit pipe was triggered.
200 * Return 1 if it was triggered else 0;
202 static int check_thread_quit_pipe(int fd
, uint32_t events
)
204 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
212 * Remove modules in reverse load order.
214 static int modprobe_remove_kernel_modules(void)
219 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
220 ret
= snprintf(modprobe
, sizeof(modprobe
),
221 "/sbin/modprobe --remove --quiet %s",
222 kernel_modules_list
[i
].name
);
224 perror("snprintf modprobe --remove");
227 modprobe
[sizeof(modprobe
) - 1] = '\0';
228 ret
= system(modprobe
);
230 ERR("Unable to launch modprobe --remove for module %s",
231 kernel_modules_list
[i
].name
);
232 } else if (kernel_modules_list
[i
].required
233 && WEXITSTATUS(ret
) != 0) {
234 ERR("Unable to remove module %s",
235 kernel_modules_list
[i
].name
);
237 DBG("Modprobe removal successful %s",
238 kernel_modules_list
[i
].name
);
247 * Return group ID of the tracing group or -1 if not found.
249 static gid_t
allowed_group(void)
253 if (opt_tracing_group
) {
254 grp
= getgrnam(opt_tracing_group
);
256 grp
= getgrnam(default_tracing_group
);
266 * Init thread quit pipe.
268 * Return -1 on error or 0 if all pipes are created.
270 static int init_thread_quit_pipe(void)
274 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
276 perror("thread quit pipe");
285 * Complete teardown of a kernel session. This free all data structure related
286 * to a kernel session and update counter.
288 static void teardown_kernel_session(struct ltt_session
*session
)
290 if (session
->kernel_session
!= NULL
) {
291 DBG("Tearing down kernel session");
294 * If a custom kernel consumer was registered, close the socket before
295 * tearing down the complete kernel session structure
297 if (session
->kernel_session
->consumer_fd
!= kconsumer_data
.cmd_sock
) {
298 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
301 trace_kernel_destroy_session(session
->kernel_session
);
302 /* Extra precaution */
303 session
->kernel_session
= NULL
;
308 * Stop all threads by closing the thread quit pipe.
310 static void stop_threads(void)
314 /* Stopping all threads */
315 DBG("Terminating all threads");
316 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
318 ERR("write error on thread quit pipe");
321 /* Dispatch thread */
322 dispatch_thread_exit
= 1;
323 futex_nto1_wake(&ust_cmd_queue
.futex
);
329 static void cleanup(void)
333 struct ltt_session
*sess
, *stmp
;
338 MSG("%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
339 "Matthew, BEET driven development works!%c[%dm",
340 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
344 DBG("Removing %s directory", LTTNG_RUNDIR
);
345 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
347 ERR("asprintf failed. Something is really wrong!");
350 /* Remove lttng run directory */
353 ERR("Unable to clean " LTTNG_RUNDIR
);
357 DBG("Cleaning up all session");
359 /* Destroy session list mutex */
360 if (session_list_ptr
!= NULL
) {
361 pthread_mutex_destroy(&session_list_ptr
->lock
);
363 /* Cleanup ALL session */
364 cds_list_for_each_entry_safe(sess
, stmp
,
365 &session_list_ptr
->head
, list
) {
366 teardown_kernel_session(sess
);
367 // TODO complete session cleanup (including UST)
371 DBG("Closing all UST sockets");
372 ust_app_clean_list();
374 pthread_mutex_destroy(&kconsumer_data
.pid_mutex
);
376 DBG("Closing kernel fd");
377 close(kernel_tracer_fd
);
380 DBG("Unloading kernel modules");
381 modprobe_remove_kernel_modules();
384 close(thread_quit_pipe
[0]);
385 close(thread_quit_pipe
[1]);
389 * Send data on a unix socket using the liblttsessiondcomm API.
391 * Return lttcomm error code.
393 static int send_unix_sock(int sock
, void *buf
, size_t len
)
395 /* Check valid length */
400 return lttcomm_send_unix_sock(sock
, buf
, len
);
404 * Free memory of a command context structure.
406 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
408 DBG("Clean command context structure");
410 if ((*cmd_ctx
)->llm
) {
411 free((*cmd_ctx
)->llm
);
413 if ((*cmd_ctx
)->lsm
) {
414 free((*cmd_ctx
)->lsm
);
422 * Send all stream fds of kernel channel to the consumer.
424 static int send_kconsumer_channel_streams(struct consumer_data
*consumer_data
,
425 int sock
, struct ltt_kernel_channel
*channel
)
428 struct ltt_kernel_stream
*stream
;
429 struct lttcomm_consumer_msg lkm
;
431 DBG("Sending streams of channel %s to kernel consumer",
432 channel
->channel
->name
);
435 lkm
.cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
436 lkm
.u
.channel
.channel_key
= channel
->fd
;
437 lkm
.u
.channel
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
438 lkm
.u
.channel
.mmap_len
= 0; /* for kernel */
439 DBG("Sending channel %d to consumer", lkm
.u
.channel
.channel_key
);
440 ret
= lttcomm_send_unix_sock(sock
, &lkm
, sizeof(lkm
));
442 perror("send consumer channel");
447 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
451 lkm
.cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
452 lkm
.u
.stream
.channel_key
= channel
->fd
;
453 lkm
.u
.stream
.stream_key
= stream
->fd
;
454 lkm
.u
.stream
.state
= stream
->state
;
455 lkm
.u
.stream
.output
= channel
->channel
->attr
.output
;
456 lkm
.u
.stream
.mmap_len
= 0; /* for kernel */
457 strncpy(lkm
.u
.stream
.path_name
, stream
->pathname
, PATH_MAX
- 1);
458 lkm
.u
.stream
.path_name
[PATH_MAX
- 1] = '\0';
459 DBG("Sending stream %d to consumer", lkm
.u
.stream
.stream_key
);
460 ret
= lttcomm_send_unix_sock(sock
, &lkm
, sizeof(lkm
));
462 perror("send consumer stream");
465 ret
= lttcomm_send_fds_unix_sock(sock
, &stream
->fd
, 1);
467 perror("send consumer stream ancillary data");
472 DBG("consumer channel streams sent");
481 * Send all stream fds of UST channel to the consumer.
483 static int send_ustconsumer_channel_streams(struct consumer_data
*consumer_data
,
484 int sock
, struct ltt_ust_channel
*channel
)
487 struct ltt_ust_stream
*stream
;
488 struct lttcomm_consumer_msg lum
;
490 DBG("Sending streams of channel %s to UST consumer",
494 lum
.cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
496 * We need to keep shm_fd open to make sure this key stays
497 * unique within the session daemon.
499 lum
.u
.channel
.channel_key
= channel
->obj
->shm_fd
;
500 lum
.u
.channel
.max_sb_size
= channel
->attr
.subbuf_size
;
501 lum
.u
.channel
.mmap_len
= channel
->obj
->memory_map_size
;
502 DBG("Sending channel %d to consumer", lum
.u
.channel
.channel_key
);
503 ret
= lttcomm_send_unix_sock(sock
, &lum
, sizeof(lum
));
505 perror("send consumer channel");
508 fds
[0] = channel
->obj
->shm_fd
;
509 fds
[1] = channel
->obj
->wait_fd
;
510 ret
= lttcomm_send_fds_unix_sock(sock
, fds
, 2);
512 perror("send consumer channel ancillary data");
517 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
520 if (!stream
->obj
->shm_fd
) {
523 lum
.cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
524 lum
.u
.stream
.channel_key
= channel
->obj
->shm_fd
;
525 lum
.u
.stream
.stream_key
= stream
->obj
->shm_fd
;
526 lum
.u
.stream
.state
= LTTNG_CONSUMER_ACTIVE_STREAM
;
527 lum
.u
.stream
.output
= channel
->attr
.output
;
528 lum
.u
.stream
.mmap_len
= stream
->obj
->memory_map_size
;
529 strncpy(lum
.u
.stream
.path_name
, stream
->pathname
, PATH_MAX
- 1);
530 lum
.u
.stream
.path_name
[PATH_MAX
- 1] = '\0';
531 DBG("Sending stream %d to consumer", lum
.u
.stream
.stream_key
);
532 ret
= lttcomm_send_unix_sock(sock
, &lum
, sizeof(lum
));
534 perror("send consumer stream");
537 fds
[0] = stream
->obj
->shm_fd
;
538 fds
[1] = stream
->obj
->wait_fd
;
539 ret
= lttcomm_send_fds_unix_sock(sock
, fds
, 2);
541 perror("send consumer stream ancillary data");
546 DBG("consumer channel streams sent");
555 * Send all stream fds of the kernel session to the consumer.
557 static int send_kconsumer_session_streams(struct consumer_data
*consumer_data
,
558 struct ltt_kernel_session
*session
)
561 struct ltt_kernel_channel
*chan
;
562 struct lttcomm_consumer_msg lkm
;
563 int sock
= session
->consumer_fd
;
565 DBG("Sending metadata stream fd");
567 /* Extra protection. It's NOT supposed to be set to 0 at this point */
568 if (session
->consumer_fd
== 0) {
569 session
->consumer_fd
= consumer_data
->cmd_sock
;
572 if (session
->metadata_stream_fd
!= 0) {
573 /* Send metadata channel fd */
574 lkm
.cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
575 lkm
.u
.channel
.channel_key
= session
->metadata
->fd
;
576 lkm
.u
.channel
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
577 lkm
.u
.channel
.mmap_len
= 0; /* for kernel */
578 DBG("Sending metadata channel %d to consumer", lkm
.u
.stream
.stream_key
);
579 ret
= lttcomm_send_unix_sock(sock
, &lkm
, sizeof(lkm
));
581 perror("send consumer channel");
585 /* Send metadata stream fd */
586 lkm
.cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
587 lkm
.u
.stream
.channel_key
= session
->metadata
->fd
;
588 lkm
.u
.stream
.stream_key
= session
->metadata_stream_fd
;
589 lkm
.u
.stream
.state
= LTTNG_CONSUMER_ACTIVE_STREAM
;
590 lkm
.u
.stream
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
591 lkm
.u
.stream
.mmap_len
= 0; /* for kernel */
592 strncpy(lkm
.u
.stream
.path_name
, session
->metadata
->pathname
, PATH_MAX
- 1);
593 lkm
.u
.stream
.path_name
[PATH_MAX
- 1] = '\0';
594 DBG("Sending metadata stream %d to consumer", lkm
.u
.stream
.stream_key
);
595 ret
= lttcomm_send_unix_sock(sock
, &lkm
, sizeof(lkm
));
597 perror("send consumer stream");
600 ret
= lttcomm_send_fds_unix_sock(sock
, &session
->metadata_stream_fd
, 1);
602 perror("send consumer stream");
607 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
608 ret
= send_kconsumer_channel_streams(consumer_data
, sock
, chan
);
614 DBG("consumer fds (metadata and channel streams) sent");
623 * Send all stream fds of the UST session to the consumer.
625 static int send_ustconsumer_session_streams(struct consumer_data
*consumer_data
,
626 struct ltt_ust_session
*session
)
629 struct ltt_ust_channel
*chan
;
630 struct lttcomm_consumer_msg lum
;
631 int sock
= session
->consumer_fd
;
633 DBG("Sending metadata stream fd");
635 /* Extra protection. It's NOT supposed to be set to 0 at this point */
636 if (session
->consumer_fd
== 0) {
637 session
->consumer_fd
= consumer_data
->cmd_sock
;
640 if (session
->metadata
->obj
->shm_fd
!= 0) {
643 /* Send metadata channel fd */
644 lum
.cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
645 lum
.u
.channel
.channel_key
= session
->metadata
->obj
->shm_fd
;
646 lum
.u
.channel
.max_sb_size
= session
->metadata
->attr
.subbuf_size
;
647 lum
.u
.channel
.mmap_len
= 0; /* for kernel */
648 DBG("Sending metadata channel %d to consumer", lum
.u
.stream
.stream_key
);
649 ret
= lttcomm_send_unix_sock(sock
, &lum
, sizeof(lum
));
651 perror("send consumer channel");
654 fds
[0] = session
->metadata
->obj
->shm_fd
;
655 fds
[1] = session
->metadata
->obj
->wait_fd
;
656 ret
= lttcomm_send_fds_unix_sock(sock
, fds
, 2);
658 perror("send consumer metadata channel");
662 /* Send metadata stream fd */
663 lum
.cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
664 lum
.u
.stream
.channel_key
= session
->metadata
->obj
->shm_fd
;
665 lum
.u
.stream
.stream_key
= session
->metadata
->stream_obj
->shm_fd
;
666 lum
.u
.stream
.state
= LTTNG_CONSUMER_ACTIVE_STREAM
;
667 lum
.u
.stream
.output
= DEFAULT_UST_CHANNEL_OUTPUT
;
668 lum
.u
.stream
.mmap_len
= session
->metadata
->stream_obj
->memory_map_size
;
669 strncpy(lum
.u
.stream
.path_name
, session
->metadata
->pathname
, PATH_MAX
- 1);
670 lum
.u
.stream
.path_name
[PATH_MAX
- 1] = '\0';
671 DBG("Sending metadata stream %d to consumer", lum
.u
.stream
.stream_key
);
672 ret
= lttcomm_send_unix_sock(sock
, &lum
, sizeof(lum
));
674 perror("send consumer metadata stream");
677 fds
[0] = session
->metadata
->stream_obj
->shm_fd
;
678 fds
[1] = session
->metadata
->stream_obj
->wait_fd
;
679 ret
= lttcomm_send_fds_unix_sock(sock
, fds
, 2);
681 perror("send consumer stream");
686 cds_list_for_each_entry(chan
, &session
->channels
.head
, list
) {
687 ret
= send_ustconsumer_channel_streams(consumer_data
, sock
, chan
);
693 DBG("consumer fds (metadata and channel streams) sent");
702 * Notify UST applications using the shm mmap futex.
704 static int notify_ust_apps(int active
)
708 DBG("Notifying applications of session daemon state: %d", active
);
710 /* See shm.c for this call implying mmap, shm and futex calls */
711 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
712 if (wait_shm_mmap
== NULL
) {
716 /* Wake waiting process */
717 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
719 /* Apps notified successfully */
727 * Setup the outgoing data buffer for the response (llm) by allocating the
728 * right amount of memory and copying the original information from the lsm
731 * Return total size of the buffer pointed by buf.
733 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
739 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
740 if (cmd_ctx
->llm
== NULL
) {
746 /* Copy common data */
747 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
748 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
750 cmd_ctx
->llm
->data_size
= size
;
751 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
760 * Update the kernel poll set of all channel fd available over all tracing
761 * session. Add the wakeup pipe at the end of the set.
763 static int update_kernel_poll(struct lttng_poll_event
*events
)
766 struct ltt_session
*session
;
767 struct ltt_kernel_channel
*channel
;
769 DBG("Updating kernel poll set");
772 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
773 session_lock(session
);
774 if (session
->kernel_session
== NULL
) {
775 session_unlock(session
);
779 cds_list_for_each_entry(channel
,
780 &session
->kernel_session
->channel_list
.head
, list
) {
781 /* Add channel fd to the kernel poll set */
782 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
784 session_unlock(session
);
787 DBG("Channel fd %d added to kernel set", channel
->fd
);
789 session_unlock(session
);
791 session_unlock_list();
796 session_unlock_list();
801 * Find the channel fd from 'fd' over all tracing session. When found, check
802 * for new channel stream and send those stream fds to the kernel consumer.
804 * Useful for CPU hotplug feature.
806 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
809 struct ltt_session
*session
;
810 struct ltt_kernel_channel
*channel
;
812 DBG("Updating kernel streams for channel fd %d", fd
);
815 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
816 session_lock(session
);
817 if (session
->kernel_session
== NULL
) {
818 session_unlock(session
);
822 /* This is not suppose to be 0 but this is an extra security check */
823 if (session
->kernel_session
->consumer_fd
== 0) {
824 session
->kernel_session
->consumer_fd
= consumer_data
->cmd_sock
;
827 cds_list_for_each_entry(channel
,
828 &session
->kernel_session
->channel_list
.head
, list
) {
829 if (channel
->fd
== fd
) {
830 DBG("Channel found, updating kernel streams");
831 ret
= kernel_open_channel_stream(channel
);
837 * Have we already sent fds to the consumer? If yes, it means
838 * that tracing is started so it is safe to send our updated
841 if (session
->kernel_session
->consumer_fds_sent
== 1) {
842 ret
= send_kconsumer_channel_streams(consumer_data
,
843 session
->kernel_session
->consumer_fd
, channel
);
851 session_unlock(session
);
853 session_unlock_list();
857 session_unlock(session
);
858 session_unlock_list();
863 * This thread manage event coming from the kernel.
865 * Features supported in this thread:
868 static void *thread_manage_kernel(void *data
)
870 int ret
, i
, pollfd
, update_poll_flag
= 1;
871 uint32_t revents
, nb_fd
;
873 struct lttng_poll_event events
;
875 DBG("Thread manage kernel started");
877 ret
= create_thread_poll_set(&events
, 2);
882 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
888 if (update_poll_flag
== 1) {
890 * Reset number of fd in the poll set. Always 2 since there is the thread
891 * quit pipe and the kernel pipe.
895 ret
= update_kernel_poll(&events
);
899 update_poll_flag
= 0;
902 nb_fd
= LTTNG_POLL_GETNB(&events
);
904 DBG("Thread kernel polling on %d fds", nb_fd
);
906 /* Zeroed the poll events */
907 lttng_poll_reset(&events
);
909 /* Poll infinite value of time */
910 ret
= lttng_poll_wait(&events
, -1);
913 } else if (ret
== 0) {
914 /* Should not happen since timeout is infinite */
915 ERR("Return value of poll is 0 with an infinite timeout.\n"
916 "This should not have happened! Continuing...");
920 for (i
= 0; i
< nb_fd
; i
++) {
921 /* Fetch once the poll data */
922 revents
= LTTNG_POLL_GETEV(&events
, i
);
923 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
925 /* Thread quit pipe has been closed. Killing thread. */
926 ret
= check_thread_quit_pipe(pollfd
, revents
);
931 /* Check for data on kernel pipe */
932 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
933 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
934 update_poll_flag
= 1;
938 * New CPU detected by the kernel. Adding kernel stream to
939 * kernel session and updating the kernel consumer
941 if (revents
& LPOLLIN
) {
942 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
948 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
949 * and unregister kernel stream at this point.
957 DBG("Kernel thread dying");
958 close(kernel_poll_pipe
[0]);
959 close(kernel_poll_pipe
[1]);
961 lttng_poll_clean(&events
);
967 * This thread manage the consumer error sent back to the session daemon.
969 static void *thread_manage_consumer(void *data
)
971 int sock
= 0, i
, ret
, pollfd
;
972 uint32_t revents
, nb_fd
;
973 enum lttcomm_return_code code
;
974 struct lttng_poll_event events
;
975 struct consumer_data
*consumer_data
= data
;
977 DBG("[thread] Manage consumer started");
979 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
985 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
986 * Nothing more will be added to this poll set.
988 ret
= create_thread_poll_set(&events
, 2);
993 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
998 nb_fd
= LTTNG_POLL_GETNB(&events
);
1000 /* Inifinite blocking call, waiting for transmission */
1001 ret
= lttng_poll_wait(&events
, -1);
1006 for (i
= 0; i
< nb_fd
; i
++) {
1007 /* Fetch once the poll data */
1008 revents
= LTTNG_POLL_GETEV(&events
, i
);
1009 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1011 /* Thread quit pipe has been closed. Killing thread. */
1012 ret
= check_thread_quit_pipe(pollfd
, revents
);
1017 /* Event on the registration socket */
1018 if (pollfd
== consumer_data
->err_sock
) {
1019 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1020 ERR("consumer err socket poll error");
1026 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1031 DBG2("Receiving code from consumer err_sock");
1033 /* Getting status code from kconsumerd */
1034 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1035 sizeof(enum lttcomm_return_code
));
1040 if (code
== CONSUMERD_COMMAND_SOCK_READY
) {
1041 consumer_data
->cmd_sock
=
1042 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1043 if (consumer_data
->cmd_sock
< 0) {
1044 sem_post(&consumer_data
->sem
);
1045 perror("consumer connect");
1048 /* Signal condition to tell that the kconsumerd is ready */
1049 sem_post(&consumer_data
->sem
);
1050 DBG("consumer command socket ready");
1052 ERR("consumer error when waiting for SOCK_READY : %s",
1053 lttcomm_get_readable_code(-code
));
1057 /* Remove the kconsumerd error sock since we've established a connexion */
1058 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1063 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1068 /* Update number of fd */
1069 nb_fd
= LTTNG_POLL_GETNB(&events
);
1071 /* Inifinite blocking call, waiting for transmission */
1072 ret
= lttng_poll_wait(&events
, -1);
1077 for (i
= 0; i
< nb_fd
; i
++) {
1078 /* Fetch once the poll data */
1079 revents
= LTTNG_POLL_GETEV(&events
, i
);
1080 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1082 /* Thread quit pipe has been closed. Killing thread. */
1083 ret
= check_thread_quit_pipe(pollfd
, revents
);
1088 /* Event on the kconsumerd socket */
1089 if (pollfd
== sock
) {
1090 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1091 ERR("consumer err socket second poll error");
1097 /* Wait for any kconsumerd error */
1098 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1099 sizeof(enum lttcomm_return_code
));
1101 ERR("consumer closed the command socket");
1105 ERR("consumer return code : %s", lttcomm_get_readable_code(-code
));
1108 DBG("consumer thread dying");
1109 close(consumer_data
->err_sock
);
1110 close(consumer_data
->cmd_sock
);
1113 unlink(consumer_data
->err_unix_sock_path
);
1114 unlink(consumer_data
->cmd_unix_sock_path
);
1115 consumer_data
->pid
= 0;
1117 lttng_poll_clean(&events
);
1123 * This thread manage application communication.
1125 static void *thread_manage_apps(void *data
)
1128 uint32_t revents
, nb_fd
;
1129 struct ust_command ust_cmd
;
1130 struct lttng_poll_event events
;
1132 DBG("[thread] Manage application started");
1134 ret
= create_thread_poll_set(&events
, 2);
1139 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1145 /* Zeroed the events structure */
1146 lttng_poll_reset(&events
);
1148 nb_fd
= LTTNG_POLL_GETNB(&events
);
1150 DBG("Apps thread polling on %d fds", nb_fd
);
1152 /* Inifinite blocking call, waiting for transmission */
1153 ret
= lttng_poll_wait(&events
, -1);
1158 for (i
= 0; i
< nb_fd
; i
++) {
1159 /* Fetch once the poll data */
1160 revents
= LTTNG_POLL_GETEV(&events
, i
);
1161 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1163 /* Thread quit pipe has been closed. Killing thread. */
1164 ret
= check_thread_quit_pipe(pollfd
, revents
);
1169 /* Inspect the apps cmd pipe */
1170 if (pollfd
== apps_cmd_pipe
[0]) {
1171 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1172 ERR("Apps command pipe error");
1174 } else if (revents
& LPOLLIN
) {
1176 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
1177 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
1178 perror("read apps cmd pipe");
1182 /* Register applicaton to the session daemon */
1183 ret
= ust_app_register(&ust_cmd
.reg_msg
,
1186 /* Only critical ENOMEM error can be returned here */
1190 ret
= ustctl_register_done(ust_cmd
.sock
);
1193 * If the registration is not possible, we simply
1194 * unregister the apps and continue
1196 ust_app_unregister(ust_cmd
.sock
);
1199 * We just need here to monitor the close of the UST
1200 * socket and poll set monitor those by default.
1202 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, 0);
1207 DBG("Apps with sock %d added to poll set",
1214 * At this point, we know that a registered application made
1215 * the event at poll_wait.
1217 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1218 /* Removing from the poll set */
1219 ret
= lttng_poll_del(&events
, pollfd
);
1225 ust_app_unregister(pollfd
);
1233 DBG("Application communication apps dying");
1234 close(apps_cmd_pipe
[0]);
1235 close(apps_cmd_pipe
[1]);
1237 lttng_poll_clean(&events
);
1243 * Dispatch request from the registration threads to the application
1244 * communication thread.
1246 static void *thread_dispatch_ust_registration(void *data
)
1249 struct cds_wfq_node
*node
;
1250 struct ust_command
*ust_cmd
= NULL
;
1252 DBG("[thread] Dispatch UST command started");
1254 while (!dispatch_thread_exit
) {
1255 /* Atomically prepare the queue futex */
1256 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1259 /* Dequeue command for registration */
1260 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1262 DBG("Woken up but nothing in the UST command queue");
1263 /* Continue thread execution */
1267 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1269 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1270 " gid:%d sock:%d name:%s (version %d.%d)",
1271 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1272 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1273 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1274 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1276 * Inform apps thread of the new application registration. This
1277 * call is blocking so we can be assured that the data will be read
1278 * at some point in time or wait to the end of the world :)
1280 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1281 sizeof(struct ust_command
));
1283 perror("write apps cmd pipe");
1284 if (errno
== EBADF
) {
1286 * We can't inform the application thread to process
1287 * registration. We will exit or else application
1288 * registration will not occur and tracing will never
1295 } while (node
!= NULL
);
1297 /* Futex wait on queue. Blocking call on futex() */
1298 futex_nto1_wait(&ust_cmd_queue
.futex
);
1302 DBG("Dispatch thread dying");
1307 * This thread manage application registration.
1309 static void *thread_registration_apps(void *data
)
1311 int sock
= 0, i
, ret
, pollfd
;
1312 uint32_t revents
, nb_fd
;
1313 struct lttng_poll_event events
;
1315 * Get allocated in this thread, enqueued to a global queue, dequeued and
1316 * freed in the manage apps thread.
1318 struct ust_command
*ust_cmd
= NULL
;
1320 DBG("[thread] Manage application registration started");
1322 ret
= lttcomm_listen_unix_sock(apps_sock
);
1328 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1329 * more will be added to this poll set.
1331 ret
= create_thread_poll_set(&events
, 2);
1336 /* Add the application registration socket */
1337 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1342 /* Notify all applications to register */
1343 ret
= notify_ust_apps(1);
1345 ERR("Failed to notify applications or create the wait shared memory.\n"
1346 "Execution continues but there might be problem for already\n"
1347 "running applications that wishes to register.");
1351 DBG("Accepting application registration");
1353 nb_fd
= LTTNG_POLL_GETNB(&events
);
1355 /* Inifinite blocking call, waiting for transmission */
1356 ret
= lttng_poll_wait(&events
, -1);
1361 for (i
= 0; i
< nb_fd
; i
++) {
1362 /* Fetch once the poll data */
1363 revents
= LTTNG_POLL_GETEV(&events
, i
);
1364 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1366 /* Thread quit pipe has been closed. Killing thread. */
1367 ret
= check_thread_quit_pipe(pollfd
, revents
);
1372 /* Event on the registration socket */
1373 if (pollfd
== apps_sock
) {
1374 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1375 ERR("Register apps socket poll error");
1377 } else if (revents
& LPOLLIN
) {
1378 sock
= lttcomm_accept_unix_sock(apps_sock
);
1383 /* Create UST registration command for enqueuing */
1384 ust_cmd
= malloc(sizeof(struct ust_command
));
1385 if (ust_cmd
== NULL
) {
1386 perror("ust command malloc");
1391 * Using message-based transmissions to ensure we don't
1392 * have to deal with partially received messages.
1394 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1395 sizeof(struct ust_register_msg
));
1396 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1398 perror("lttcomm_recv_unix_sock register apps");
1400 ERR("Wrong size received on apps register");
1407 ust_cmd
->sock
= sock
;
1409 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1410 " gid:%d sock:%d name:%s (version %d.%d)",
1411 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1412 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1413 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1414 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1417 * Lock free enqueue the registration request. The red pill
1418 * has been taken! This apps will be part of the *system*.
1420 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1423 * Wake the registration queue futex. Implicit memory
1424 * barrier with the exchange in cds_wfq_enqueue.
1426 futex_nto1_wake(&ust_cmd_queue
.futex
);
1433 DBG("UST Registration thread dying");
1435 /* Notify that the registration thread is gone */
1440 unlink(apps_unix_sock_path
);
1442 lttng_poll_clean(&events
);
1448 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1449 * exec or it will fails.
1451 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1454 struct timespec timeout
;
1456 timeout
.tv_sec
= DEFAULT_SEM_WAIT_TIMEOUT
;
1457 timeout
.tv_nsec
= 0;
1459 /* Setup semaphore */
1460 ret
= sem_init(&consumer_data
->sem
, 0, 0);
1462 PERROR("sem_init consumer semaphore");
1466 ret
= pthread_create(&consumer_data
->thread
, NULL
,
1467 thread_manage_consumer
, consumer_data
);
1469 PERROR("pthread_create consumer");
1474 /* Get time for sem_timedwait absolute timeout */
1475 ret
= clock_gettime(CLOCK_REALTIME
, &timeout
);
1477 PERROR("clock_gettime spawn consumer");
1478 /* Infinite wait for the kconsumerd thread to be ready */
1479 ret
= sem_wait(&consumer_data
->sem
);
1481 /* Normal timeout if the gettime was successful */
1482 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1483 ret
= sem_timedwait(&consumer_data
->sem
, &timeout
);
1487 if (errno
== ETIMEDOUT
) {
1489 * Call has timed out so we kill the kconsumerd_thread and return
1492 ERR("The consumer thread was never ready. Killing it");
1493 ret
= pthread_cancel(consumer_data
->thread
);
1495 PERROR("pthread_cancel consumer thread");
1498 PERROR("semaphore wait failed consumer thread");
1503 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1504 if (consumer_data
->pid
== 0) {
1505 ERR("Kconsumerd did not start");
1506 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1509 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1518 * Join consumer thread
1520 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1525 if (consumer_data
->pid
!= 0) {
1526 ret
= kill(consumer_data
->pid
, SIGTERM
);
1528 ERR("Error killing consumer daemon");
1531 return pthread_join(consumer_data
->thread
, &status
);
1538 * Fork and exec a consumer daemon (consumerd).
1540 * Return pid if successful else -1.
1542 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1546 const char *verbosity
;
1548 DBG("Spawning consumerd");
1555 if (opt_verbose
> 1 || opt_verbose_consumer
) {
1556 verbosity
= "--verbose";
1558 verbosity
= "--quiet";
1560 switch (consumer_data
->type
) {
1561 case LTTNG_CONSUMER_KERNEL
:
1562 execl(INSTALL_BIN_PATH
"/lttng-consumerd",
1563 "lttng-consumerd", verbosity
, "-k", NULL
);
1565 case LTTNG_CONSUMER_UST
:
1566 execl(INSTALL_BIN_PATH
"/lttng-consumerd",
1567 "lttng-consumerd", verbosity
, "-u", NULL
);
1570 perror("unknown consumer type");
1574 perror("kernel start consumer exec");
1577 } else if (pid
> 0) {
1580 perror("start consumer fork");
1587 * Spawn the consumerd daemon and session daemon thread.
1589 static int start_consumerd(struct consumer_data
*consumer_data
)
1593 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1594 if (consumer_data
->pid
!= 0) {
1595 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1599 ret
= spawn_consumerd(consumer_data
);
1601 ERR("Spawning consumerd failed");
1602 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1606 /* Setting up the consumer_data pid */
1607 consumer_data
->pid
= ret
;
1608 DBG2("consumer pid %d", consumer_data
->pid
);
1609 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1611 DBG2("Spawning consumer control thread");
1612 ret
= spawn_consumer_thread(consumer_data
);
1614 ERR("Fatal error spawning consumer control thread");
1626 * modprobe_kernel_modules
1628 static int modprobe_kernel_modules(void)
1633 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
1634 ret
= snprintf(modprobe
, sizeof(modprobe
),
1635 "/sbin/modprobe %s%s",
1636 kernel_modules_list
[i
].required
? "" : "--quiet ",
1637 kernel_modules_list
[i
].name
);
1639 perror("snprintf modprobe");
1642 modprobe
[sizeof(modprobe
) - 1] = '\0';
1643 ret
= system(modprobe
);
1645 ERR("Unable to launch modprobe for module %s",
1646 kernel_modules_list
[i
].name
);
1647 } else if (kernel_modules_list
[i
].required
1648 && WEXITSTATUS(ret
) != 0) {
1649 ERR("Unable to load module %s",
1650 kernel_modules_list
[i
].name
);
1652 DBG("Modprobe successfully %s",
1653 kernel_modules_list
[i
].name
);
1664 static int mount_debugfs(char *path
)
1667 char *type
= "debugfs";
1669 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1671 PERROR("Cannot create debugfs path");
1675 ret
= mount(type
, path
, type
, 0, NULL
);
1677 PERROR("Cannot mount debugfs");
1681 DBG("Mounted debugfs successfully at %s", path
);
1688 * Setup necessary data for kernel tracer action.
1690 static void init_kernel_tracer(void)
1693 char *proc_mounts
= "/proc/mounts";
1695 char *debugfs_path
= NULL
, *lttng_path
= NULL
;
1698 /* Detect debugfs */
1699 fp
= fopen(proc_mounts
, "r");
1701 ERR("Unable to probe %s", proc_mounts
);
1705 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1706 if (strstr(line
, "debugfs") != NULL
) {
1707 /* Remove first string */
1709 /* Dup string here so we can reuse line later on */
1710 debugfs_path
= strdup(strtok(NULL
, " "));
1711 DBG("Got debugfs path : %s", debugfs_path
);
1718 /* Mount debugfs if needded */
1719 if (debugfs_path
== NULL
) {
1720 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1722 perror("asprintf debugfs path");
1725 ret
= mount_debugfs(debugfs_path
);
1727 perror("Cannot mount debugfs");
1732 /* Modprobe lttng kernel modules */
1733 ret
= modprobe_kernel_modules();
1738 /* Setup lttng kernel path */
1739 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1741 perror("asprintf lttng path");
1745 /* Open debugfs lttng */
1746 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1747 if (kernel_tracer_fd
< 0) {
1748 DBG("Failed to open %s", lttng_path
);
1754 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1764 WARN("No kernel tracer available");
1765 kernel_tracer_fd
= 0;
1770 * Init tracing by creating trace directory and sending fds kernel consumer.
1772 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
1776 if (session
->consumer_fds_sent
== 0) {
1778 * Assign default kernel consumer socket if no consumer assigned to the
1779 * kernel session. At this point, it's NOT suppose to be 0 but this is
1780 * an extra security check.
1782 if (session
->consumer_fd
== 0) {
1783 session
->consumer_fd
= kconsumer_data
.cmd_sock
;
1786 ret
= send_kconsumer_session_streams(&kconsumer_data
, session
);
1788 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1792 session
->consumer_fds_sent
= 1;
1800 * Init tracing by creating trace directory and sending fds ust consumer.
1802 static int init_ust_tracing(struct ltt_ust_session
*session
)
1806 if (session
->consumer_fds_sent
== 0) {
1808 * Assign default ust consumer socket if no consumer assigned to the
1809 * ust session. At this point, it's NOT suppose to be 0 but this is
1810 * an extra security check.
1812 if (session
->consumer_fd
== 0) {
1813 session
->consumer_fd
= ustconsumer_data
.cmd_sock
;
1816 ret
= send_ustconsumer_session_streams(&ustconsumer_data
, session
);
1818 ret
= LTTCOMM_UST_CONSUMER_FAIL
;
1822 session
->consumer_fds_sent
= 1;
1830 * Create an UST session and add it to the session ust list.
1832 static int create_ust_session(struct ltt_session
*session
,
1833 struct lttng_domain
*domain
)
1836 struct ltt_ust_session
*lus
= NULL
;
1837 struct ust_app
*app
;
1839 switch (domain
->type
) {
1840 case LTTNG_DOMAIN_UST_PID
:
1841 app
= ust_app_get_by_pid(domain
->attr
.pid
);
1843 ret
= LTTCOMM_APP_NOT_FOUND
;
1848 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
1852 DBG("Creating UST session");
1854 lus
= trace_ust_create_session(session
->path
, domain
->attr
.pid
, domain
);
1856 ret
= LTTCOMM_UST_SESS_FAIL
;
1860 ret
= mkdir_recursive(lus
->path
, S_IRWXU
| S_IRWXG
,
1861 geteuid(), allowed_group());
1863 if (ret
!= -EEXIST
) {
1864 ERR("Trace directory creation error");
1865 ret
= LTTCOMM_UST_SESS_FAIL
;
1870 /* Create session on the UST tracer */
1871 ret
= ustctl_create_session(app
->sock
);
1873 ret
= LTTCOMM_UST_SESS_FAIL
;
1877 lus
->sock
= app
->sock
;
1879 cds_list_add(&lus
->list
, &session
->ust_session_list
.head
);
1880 session
->ust_session_list
.count
++;
1890 * Create a kernel tracer session then create the default channel.
1892 static int create_kernel_session(struct ltt_session
*session
)
1896 DBG("Creating kernel session");
1898 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1900 ret
= LTTCOMM_KERN_SESS_FAIL
;
1904 /* Set kernel consumer socket fd */
1905 if (kconsumer_data
.cmd_sock
) {
1906 session
->kernel_session
->consumer_fd
= kconsumer_data
.cmd_sock
;
1909 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1910 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1912 if (ret
!= -EEXIST
) {
1913 ERR("Trace directory creation error");
1923 * Using the session list, filled a lttng_session array to send back to the
1924 * client for session listing.
1926 * The session list lock MUST be acquired before calling this function. Use
1927 * session_lock_list() and session_unlock_list().
1929 static void list_lttng_sessions(struct lttng_session
*sessions
)
1932 struct ltt_session
*session
;
1934 DBG("Getting all available session");
1936 * Iterate over session list and append data after the control struct in
1939 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1940 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1941 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1942 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1943 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1949 * Fill lttng_channel array of all channels.
1951 static void list_lttng_channels(struct ltt_session
*session
,
1952 struct lttng_channel
*channels
)
1955 struct ltt_kernel_channel
*kchan
;
1957 DBG("Listing channels for session %s", session
->name
);
1959 /* Kernel channels */
1960 if (session
->kernel_session
!= NULL
) {
1961 cds_list_for_each_entry(kchan
,
1962 &session
->kernel_session
->channel_list
.head
, list
) {
1963 /* Copy lttng_channel struct to array */
1964 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1965 channels
[i
].enabled
= kchan
->enabled
;
1970 /* TODO: Missing UST listing */
1974 * Fill lttng_event array of all events in the channel.
1976 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1977 struct lttng_event
*events
)
1980 * TODO: This is ONLY kernel. Need UST support.
1983 struct ltt_kernel_event
*event
;
1985 DBG("Listing events for channel %s", kchan
->channel
->name
);
1987 /* Kernel channels */
1988 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1989 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1990 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1991 events
[i
].enabled
= event
->enabled
;
1992 switch (event
->event
->instrumentation
) {
1993 case LTTNG_KERNEL_TRACEPOINT
:
1994 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1996 case LTTNG_KERNEL_KPROBE
:
1997 case LTTNG_KERNEL_KRETPROBE
:
1998 events
[i
].type
= LTTNG_EVENT_PROBE
;
1999 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
2000 sizeof(struct lttng_kernel_kprobe
));
2002 case LTTNG_KERNEL_FUNCTION
:
2003 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
2004 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
2005 sizeof(struct lttng_kernel_function
));
2007 case LTTNG_KERNEL_NOOP
:
2008 events
[i
].type
= LTTNG_EVENT_NOOP
;
2010 case LTTNG_KERNEL_SYSCALL
:
2011 events
[i
].type
= LTTNG_EVENT_SYSCALL
;
2013 case LTTNG_KERNEL_ALL
:
2022 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
2024 static int cmd_disable_channel(struct ltt_session
*session
,
2025 int domain
, char *channel_name
)
2030 case LTTNG_DOMAIN_KERNEL
:
2031 ret
= channel_kernel_disable(session
->kernel_session
,
2033 if (ret
!= LTTCOMM_OK
) {
2037 kernel_wait_quiescent(kernel_tracer_fd
);
2039 case LTTNG_DOMAIN_UST_PID
:
2042 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2053 * Copy channel from attributes and set it in the application channel list.
2055 static int copy_ust_channel_to_app(struct ltt_ust_session
*usess
,
2056 struct lttng_channel
*attr
, struct ust_app
*app
)
2059 struct ltt_ust_channel
*uchan
, *new_chan
;
2061 uchan
= trace_ust_get_channel_by_name(attr
->name
, usess
);
2062 if (uchan
== NULL
) {
2063 ret
= LTTCOMM_FATAL
;
2067 new_chan
= trace_ust_create_channel(attr
, usess
->path
);
2068 if (new_chan
== NULL
) {
2069 PERROR("malloc ltt_ust_channel");
2070 ret
= LTTCOMM_FATAL
;
2074 ret
= channel_ust_copy(new_chan
, uchan
);
2076 ret
= LTTCOMM_FATAL
;
2080 /* Add channel to the ust app channel list */
2081 cds_list_add(&new_chan
->list
, &app
->channels
.head
);
2082 app
->channels
.count
++;
2089 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
2091 static int cmd_enable_channel(struct ltt_session
*session
,
2092 struct lttng_domain
*domain
, struct lttng_channel
*attr
)
2096 switch (domain
->type
) {
2097 case LTTNG_DOMAIN_KERNEL
:
2099 struct ltt_kernel_channel
*kchan
;
2101 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
2102 session
->kernel_session
);
2103 if (kchan
== NULL
) {
2104 ret
= channel_kernel_create(session
->kernel_session
,
2105 attr
, kernel_poll_pipe
[1]);
2107 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
2110 if (ret
!= LTTCOMM_OK
) {
2114 kernel_wait_quiescent(kernel_tracer_fd
);
2117 case LTTNG_DOMAIN_UST_PID
:
2120 struct ltt_ust_channel
*uchan
;
2121 struct ltt_ust_session
*usess
;
2122 struct ust_app
*app
;
2124 usess
= trace_ust_get_session_by_pid(&session
->ust_session_list
,
2126 if (usess
== NULL
) {
2127 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2131 app
= ust_app_get_by_pid(domain
->attr
.pid
);
2133 ret
= LTTCOMM_APP_NOT_FOUND
;
2138 uchan
= trace_ust_get_channel_by_name(attr
->name
, usess
);
2139 if (uchan
== NULL
) {
2140 ret
= channel_ust_create(usess
, attr
, sock
);
2142 ret
= channel_ust_enable(usess
, uchan
, sock
);
2145 if (ret
!= LTTCOMM_OK
) {
2149 ret
= copy_ust_channel_to_app(usess
, attr
, app
);
2150 if (ret
!= LTTCOMM_OK
) {
2154 DBG("UST channel %s created for app sock %d with pid %d",
2155 attr
->name
, app
->sock
, domain
->attr
.pid
);
2159 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2170 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2172 static int cmd_disable_event(struct ltt_session
*session
, int domain
,
2173 char *channel_name
, char *event_name
)
2178 case LTTNG_DOMAIN_KERNEL
:
2180 struct ltt_kernel_channel
*kchan
;
2182 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2183 session
->kernel_session
);
2184 if (kchan
== NULL
) {
2185 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2189 ret
= event_kernel_disable_tracepoint(session
->kernel_session
, kchan
, event_name
);
2190 if (ret
!= LTTCOMM_OK
) {
2194 kernel_wait_quiescent(kernel_tracer_fd
);
2197 case LTTNG_DOMAIN_UST
:
2199 struct ltt_ust_session
*ustsession
;
2201 cds_list_for_each_entry(ustsession
, &session
->ust_session_list
.head
, list
) {
2202 struct ltt_ust_channel
*ustchan
;
2204 ustchan
= trace_ust_get_channel_by_name(channel_name
,
2206 if (ustchan
== NULL
) {
2207 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2210 ret
= event_ust_disable_tracepoint(ustsession
, ustchan
, event_name
);
2211 if (ret
!= LTTCOMM_OK
) {
2215 ustctl_wait_quiescent(ustsession
->sock
);
2219 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2220 case LTTNG_DOMAIN_UST_PID
:
2221 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2223 /* TODO: Other UST domains */
2224 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2235 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
2237 static int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
2241 struct ltt_kernel_channel
*kchan
;
2244 case LTTNG_DOMAIN_KERNEL
:
2245 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2246 session
->kernel_session
);
2247 if (kchan
== NULL
) {
2248 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2252 ret
= event_kernel_disable_all(session
->kernel_session
, kchan
);
2253 if (ret
!= LTTCOMM_OK
) {
2257 kernel_wait_quiescent(kernel_tracer_fd
);
2260 /* TODO: Userspace tracing */
2261 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2272 * Command LTTNG_ADD_CONTEXT processed by the client thread.
2274 static int cmd_add_context(struct ltt_session
*session
, int domain
,
2275 char *channel_name
, char *event_name
, struct lttng_event_context
*ctx
)
2280 case LTTNG_DOMAIN_KERNEL
:
2281 /* Add kernel context to kernel tracer */
2282 ret
= context_kernel_add(session
->kernel_session
, ctx
,
2283 event_name
, channel_name
);
2284 if (ret
!= LTTCOMM_OK
) {
2288 case LTTNG_DOMAIN_UST
:
2290 struct ltt_ust_session
*ustsession
;
2292 cds_list_for_each_entry(ustsession
, &session
->ust_session_list
.head
, list
) {
2293 /* Add UST context to UST tracer */
2294 ret
= context_ust_add(ustsession
, ctx
,
2295 event_name
, channel_name
);
2296 if (ret
!= LTTCOMM_OK
) {
2303 /* TODO: UST other domains */
2304 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2315 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2317 static int cmd_enable_event(struct ltt_session
*session
, int domain
,
2318 char *channel_name
, struct lttng_event
*event
)
2321 struct lttng_channel
*attr
;
2324 case LTTNG_DOMAIN_KERNEL
:
2326 struct ltt_kernel_channel
*kchan
;
2328 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2329 session
->kernel_session
);
2330 if (kchan
== NULL
) {
2331 attr
= channel_new_default_attr(domain
);
2333 ret
= LTTCOMM_FATAL
;
2336 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2338 /* This call will notify the kernel thread */
2339 ret
= channel_kernel_create(session
->kernel_session
,
2340 attr
, kernel_poll_pipe
[1]);
2341 if (ret
!= LTTCOMM_OK
) {
2346 /* Get the newly created kernel channel pointer */
2347 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2348 session
->kernel_session
);
2349 if (kchan
== NULL
) {
2350 /* This sould not happen... */
2351 ret
= LTTCOMM_FATAL
;
2355 ret
= event_kernel_enable_tracepoint(session
->kernel_session
, kchan
, event
);
2356 if (ret
!= LTTCOMM_OK
) {
2360 kernel_wait_quiescent(kernel_tracer_fd
);
2363 case LTTNG_DOMAIN_UST
:
2365 struct ltt_ust_session
*ustsession
;
2367 cds_list_for_each_entry(ustsession
, &session
->ust_session_list
.head
, list
) {
2368 struct ltt_ust_channel
*ustchan
;
2370 ustchan
= trace_ust_get_channel_by_name(channel_name
,
2372 if (ustchan
== NULL
) {
2373 attr
= channel_new_default_attr(domain
);
2375 ret
= LTTCOMM_FATAL
;
2378 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2380 ret
= channel_ust_create(ustsession
,
2381 attr
, ustsession
->sock
);
2382 if (ret
!= LTTCOMM_OK
) {
2387 /* Get the newly created ust channel pointer */
2388 ustchan
= trace_ust_get_channel_by_name(channel_name
,
2390 if (ustchan
== NULL
) {
2391 /* This sould not happen... */
2392 ret
= LTTCOMM_FATAL
;
2396 ret
= event_ust_enable_tracepoint(ustsession
, ustchan
, event
);
2397 if (ret
!= LTTCOMM_OK
) {
2401 ustctl_wait_quiescent(ustsession
->sock
);
2405 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2406 case LTTNG_DOMAIN_UST_PID
:
2407 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2409 /* TODO: UST other domains */
2410 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2421 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
2423 static int cmd_enable_event_all(struct ltt_session
*session
, int domain
,
2424 char *channel_name
, int event_type
)
2427 struct ltt_kernel_channel
*kchan
;
2430 case LTTNG_DOMAIN_KERNEL
:
2431 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2432 session
->kernel_session
);
2433 if (kchan
== NULL
) {
2434 /* This call will notify the kernel thread */
2435 ret
= channel_kernel_create(session
->kernel_session
, NULL
,
2436 kernel_poll_pipe
[1]);
2437 if (ret
!= LTTCOMM_OK
) {
2442 /* Get the newly created kernel channel pointer */
2443 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2444 session
->kernel_session
);
2445 if (kchan
== NULL
) {
2446 /* This sould not happen... */
2447 ret
= LTTCOMM_FATAL
;
2451 switch (event_type
) {
2452 case LTTNG_KERNEL_SYSCALL
:
2453 ret
= event_kernel_enable_all_syscalls(session
->kernel_session
,
2454 kchan
, kernel_tracer_fd
);
2456 case LTTNG_KERNEL_TRACEPOINT
:
2458 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
2459 * events already registered to the channel.
2461 ret
= event_kernel_enable_all_tracepoints(session
->kernel_session
,
2462 kchan
, kernel_tracer_fd
);
2464 case LTTNG_KERNEL_ALL
:
2465 /* Enable syscalls and tracepoints */
2466 ret
= event_kernel_enable_all(session
->kernel_session
,
2467 kchan
, kernel_tracer_fd
);
2470 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2473 if (ret
!= LTTCOMM_OK
) {
2477 kernel_wait_quiescent(kernel_tracer_fd
);
2480 /* TODO: Userspace tracing */
2481 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2492 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2494 static ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
2497 ssize_t nb_events
= 0;
2500 case LTTNG_DOMAIN_KERNEL
:
2501 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2502 if (nb_events
< 0) {
2503 ret
= LTTCOMM_KERN_LIST_FAIL
;
2508 /* TODO: Userspace listing */
2509 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2516 /* Return negative value to differentiate return code */
2521 * Command LTTNG_START_TRACE processed by the client thread.
2523 static int cmd_start_trace(struct ltt_session
*session
)
2526 struct ltt_kernel_session
*ksession
;
2527 struct ltt_ust_session
*ustsession
;
2530 ksession
= session
->kernel_session
;
2532 /* Kernel tracing */
2533 if (ksession
!= NULL
) {
2534 struct ltt_kernel_channel
*kchan
;
2536 /* Open kernel metadata */
2537 if (ksession
->metadata
== NULL
) {
2538 ret
= kernel_open_metadata(ksession
, ksession
->trace_path
);
2540 ret
= LTTCOMM_KERN_META_FAIL
;
2545 /* Open kernel metadata stream */
2546 if (ksession
->metadata_stream_fd
== 0) {
2547 ret
= kernel_open_metadata_stream(ksession
);
2549 ERR("Kernel create metadata stream failed");
2550 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2555 /* For each channel */
2556 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2557 if (kchan
->stream_count
== 0) {
2558 ret
= kernel_open_channel_stream(kchan
);
2560 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2563 /* Update the stream global counter */
2564 ksession
->stream_count_global
+= ret
;
2568 /* Setup kernel consumer socket and send fds to it */
2569 ret
= init_kernel_tracing(ksession
);
2571 ret
= LTTCOMM_KERN_START_FAIL
;
2575 /* This start the kernel tracing */
2576 ret
= kernel_start_session(ksession
);
2578 ret
= LTTCOMM_KERN_START_FAIL
;
2582 /* Quiescent wait after starting trace */
2583 kernel_wait_quiescent(kernel_tracer_fd
);
2586 /* Start all UST traces */
2587 cds_list_for_each_entry(ustsession
, &session
->ust_session_list
.head
, list
) {
2588 struct ltt_ust_channel
*ustchan
;
2590 /* Open kernel metadata */
2591 if (ustsession
->metadata
== NULL
) {
2592 struct lttng_ust_channel_attr ustattr
;
2594 /* Allocate UST metadata */
2595 ustsession
->metadata
= trace_ust_create_metadata(ustsession
->path
);
2596 if (ustsession
->metadata
== NULL
) {
2597 ret
= LTTCOMM_UST_META_FAIL
;
2601 ustattr
.overwrite
= ustsession
->metadata
->attr
.overwrite
;
2602 ustattr
.subbuf_size
= ustsession
->metadata
->attr
.subbuf_size
;
2603 ustattr
.num_subbuf
= ustsession
->metadata
->attr
.num_subbuf
;
2604 ustattr
.switch_timer_interval
= ustsession
->metadata
->attr
.switch_timer_interval
;
2605 ustattr
.read_timer_interval
= ustsession
->metadata
->attr
.read_timer_interval
;
2606 ustattr
.output
= ustsession
->metadata
->attr
.output
;
2608 /* UST tracer metadata creation */
2609 ret
= ustctl_open_metadata(ustsession
->sock
,
2610 ustsession
->handle
, &ustattr
,
2611 &ustsession
->metadata
->obj
);
2613 ret
= LTTCOMM_UST_META_FAIL
;
2618 /* Open UST metadata stream */
2619 if (ustsession
->metadata
->stream_obj
== NULL
) {
2620 ret
= ustctl_create_stream(ustsession
->sock
,
2621 ustsession
->metadata
->obj
,
2622 &ustsession
->metadata
->stream_obj
);
2624 ERR("UST create metadata stream failed");
2625 ret
= LTTCOMM_UST_STREAM_FAIL
;
2628 ret
= asprintf(&ustsession
->metadata
->pathname
, "%s/%s",
2629 ustsession
->path
, "metadata");
2631 perror("asprintf UST create stream");
2636 /* For each channel */
2637 cds_list_for_each_entry(ustchan
, &ustsession
->channels
.head
, list
) {
2638 if (ustchan
->stream_count
== 0) {
2639 struct ltt_ust_stream
*ustream
;
2641 ustream
= zmalloc(sizeof(*ustream
));
2643 ret
= LTTCOMM_UST_STREAM_FAIL
;
2646 ret
= ustctl_create_stream(ustsession
->sock
,
2647 ustchan
->obj
, &ustream
->obj
);
2649 ret
= LTTCOMM_UST_STREAM_FAIL
;
2652 ret
= asprintf(&ustream
->pathname
, "%s/%s_%d",
2653 ustchan
->trace_path
, ustchan
->name
,
2654 ustchan
->stream_count
);
2656 perror("asprintf UST create stream");
2659 cds_list_add(&ustream
->list
, &ustchan
->stream_list
.head
);
2660 ustchan
->stream_count
++;
2664 /* Setup UST consumer socket and send fds to it */
2665 ret
= init_ust_tracing(ustsession
);
2667 ret
= LTTCOMM_UST_START_FAIL
;
2671 /* This start the UST tracing */
2672 ret
= ustctl_start_session(ustsession
->sock
, ustsession
->handle
);
2674 ret
= LTTCOMM_UST_START_FAIL
;
2678 /* Quiescent wait after starting trace */
2679 ustctl_wait_quiescent(ustsession
->sock
);
2689 * Command LTTNG_STOP_TRACE processed by the client thread.
2691 static int cmd_stop_trace(struct ltt_session
*session
)
2694 struct ltt_kernel_channel
*kchan
;
2695 struct ltt_kernel_session
*ksession
;
2696 struct ltt_ust_session
*ustsession
;
2697 struct ltt_ust_channel
*ustchan
;
2700 ksession
= session
->kernel_session
;
2703 if (ksession
!= NULL
) {
2704 DBG("Stop kernel tracing");
2706 /* Flush all buffers before stopping */
2707 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2709 ERR("Kernel metadata flush failed");
2712 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2713 ret
= kernel_flush_buffer(kchan
);
2715 ERR("Kernel flush buffer error");
2719 ret
= kernel_stop_session(ksession
);
2721 ret
= LTTCOMM_KERN_STOP_FAIL
;
2725 kernel_wait_quiescent(kernel_tracer_fd
);
2728 /* Stop each UST session */
2729 DBG("Stop UST tracing");
2730 cds_list_for_each_entry(ustsession
, &session
->ust_session_list
.head
, list
) {
2731 /* Flush all buffers before stopping */
2732 ret
= ustctl_flush_buffer(ustsession
->sock
, ustsession
->metadata
->obj
);
2734 ERR("UST metadata flush failed");
2737 cds_list_for_each_entry(ustchan
, &ustsession
->channels
.head
, list
) {
2738 ret
= ustctl_flush_buffer(ustsession
->sock
, ustchan
->obj
);
2740 ERR("UST flush buffer error");
2744 ret
= ustctl_stop_session(ustsession
->sock
, ustsession
->handle
);
2746 ret
= LTTCOMM_KERN_STOP_FAIL
;
2750 ustctl_wait_quiescent(ustsession
->sock
);
2760 * Command LTTNG_CREATE_SESSION processed by the client thread.
2762 static int cmd_create_session(char *name
, char *path
)
2766 ret
= session_create(name
, path
);
2767 if (ret
!= LTTCOMM_OK
) {
2778 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2780 static int cmd_destroy_session(struct ltt_session
*session
, char *name
)
2784 /* Clean kernel session teardown */
2785 teardown_kernel_session(session
);
2788 * Must notify the kernel thread here to update it's poll setin order
2789 * to remove the channel(s)' fd just destroyed.
2791 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
2793 perror("write kernel poll pipe");
2796 ret
= session_destroy(session
);
2802 * Command LTTNG_CALIBRATE processed by the client thread.
2804 static int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2809 case LTTNG_DOMAIN_KERNEL
:
2811 struct lttng_kernel_calibrate kcalibrate
;
2813 kcalibrate
.type
= calibrate
->type
;
2814 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2816 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2822 /* TODO: Userspace tracing */
2823 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2834 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2836 static int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2842 case LTTNG_DOMAIN_KERNEL
:
2843 /* Can't register a consumer if there is already one */
2844 if (session
->kernel_session
->consumer_fd
!= 0) {
2845 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
2849 sock
= lttcomm_connect_unix_sock(sock_path
);
2851 ret
= LTTCOMM_CONNECT_FAIL
;
2855 session
->kernel_session
->consumer_fd
= sock
;
2858 /* TODO: Userspace tracing */
2859 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2870 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2872 static ssize_t
cmd_list_domains(struct ltt_session
*session
,
2873 struct lttng_domain
**domains
)
2878 if (session
->kernel_session
!= NULL
) {
2882 nb_dom
+= session
->ust_session_list
.count
;
2884 *domains
= malloc(nb_dom
* sizeof(struct lttng_domain
));
2885 if (*domains
== NULL
) {
2886 ret
= -LTTCOMM_FATAL
;
2890 (*domains
)[0].type
= LTTNG_DOMAIN_KERNEL
;
2892 /* TODO: User-space tracer domain support */
2901 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2903 static ssize_t
cmd_list_channels(struct ltt_session
*session
,
2904 struct lttng_channel
**channels
)
2907 ssize_t nb_chan
= 0;
2909 if (session
->kernel_session
!= NULL
) {
2910 nb_chan
+= session
->kernel_session
->channel_count
;
2913 *channels
= malloc(nb_chan
* sizeof(struct lttng_channel
));
2914 if (*channels
== NULL
) {
2915 ret
= -LTTCOMM_FATAL
;
2919 list_lttng_channels(session
, *channels
);
2921 /* TODO UST support */
2930 * Command LTTNG_LIST_EVENTS processed by the client thread.
2932 static ssize_t
cmd_list_events(struct ltt_session
*session
,
2933 char *channel_name
, struct lttng_event
**events
)
2936 ssize_t nb_event
= 0;
2937 struct ltt_kernel_channel
*kchan
= NULL
;
2939 if (session
->kernel_session
!= NULL
) {
2940 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2941 session
->kernel_session
);
2942 if (kchan
== NULL
) {
2943 ret
= -LTTCOMM_KERN_CHAN_NOT_FOUND
;
2946 nb_event
+= kchan
->event_count
;
2949 *events
= malloc(nb_event
* sizeof(struct lttng_event
));
2950 if (*events
== NULL
) {
2951 ret
= -LTTCOMM_FATAL
;
2955 list_lttng_events(kchan
, *events
);
2957 /* TODO: User-space tracer support */
2966 * Process the command requested by the lttng client within the command
2967 * context structure. This function make sure that the return structure (llm)
2968 * is set and ready for transmission before returning.
2970 * Return any error encountered or 0 for success.
2972 static int process_client_msg(struct command_ctx
*cmd_ctx
)
2974 int ret
= LTTCOMM_OK
;
2975 int need_tracing_session
= 1;
2977 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2980 * Check for command that don't needs to allocate a returned payload. We do
2981 * this here so we don't have to make the call for no payload at each
2984 switch(cmd_ctx
->lsm
->cmd_type
) {
2985 case LTTNG_LIST_SESSIONS
:
2986 case LTTNG_LIST_TRACEPOINTS
:
2987 case LTTNG_LIST_DOMAINS
:
2988 case LTTNG_LIST_CHANNELS
:
2989 case LTTNG_LIST_EVENTS
:
2992 /* Setup lttng message with no payload */
2993 ret
= setup_lttng_msg(cmd_ctx
, 0);
2995 /* This label does not try to unlock the session */
2996 goto init_setup_error
;
3000 /* Commands that DO NOT need a session. */
3001 switch (cmd_ctx
->lsm
->cmd_type
) {
3002 case LTTNG_CALIBRATE
:
3003 case LTTNG_CREATE_SESSION
:
3004 case LTTNG_LIST_SESSIONS
:
3005 case LTTNG_LIST_TRACEPOINTS
:
3006 need_tracing_session
= 0;
3009 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3010 session_lock_list();
3011 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3012 session_unlock_list();
3013 if (cmd_ctx
->session
== NULL
) {
3014 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
3015 ret
= LTTCOMM_SESS_NOT_FOUND
;
3017 /* If no session name specified */
3018 ret
= LTTCOMM_SELECT_SESS
;
3022 /* Acquire lock for the session */
3023 session_lock(cmd_ctx
->session
);
3029 * Check domain type for specific "pre-action".
3031 switch (cmd_ctx
->lsm
->domain
.type
) {
3032 case LTTNG_DOMAIN_KERNEL
:
3033 /* Kernel tracer check */
3034 if (kernel_tracer_fd
== 0) {
3035 /* Basically, load kernel tracer modules */
3036 init_kernel_tracer();
3037 if (kernel_tracer_fd
== 0) {
3038 ret
= LTTCOMM_KERN_NA
;
3043 /* Need a session for kernel command */
3044 if (need_tracing_session
) {
3045 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3046 ret
= create_kernel_session(cmd_ctx
->session
);
3048 ret
= LTTCOMM_KERN_SESS_FAIL
;
3053 /* Start the kernel consumer daemon */
3054 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3055 if (kconsumer_data
.pid
== 0 &&
3056 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3057 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3058 ret
= start_consumerd(&kconsumer_data
);
3060 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
3064 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3067 case LTTNG_DOMAIN_UST
:
3068 case LTTNG_DOMAIN_UST_EXEC_NAME
:
3069 case LTTNG_DOMAIN_UST_PID
:
3070 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
3072 struct ltt_ust_session
*usess
;
3074 if (need_tracing_session
) {
3075 usess
= trace_ust_get_session_by_pid(
3076 &cmd_ctx
->session
->ust_session_list
,
3077 cmd_ctx
->lsm
->domain
.attr
.pid
);
3078 if (usess
== NULL
) {
3079 ret
= create_ust_session(cmd_ctx
->session
,
3080 &cmd_ctx
->lsm
->domain
);
3081 if (ret
!= LTTCOMM_OK
) {
3085 /* Start the kernel consumer daemon */
3086 pthread_mutex_lock(&ustconsumer_data
.pid_mutex
);
3087 if (ustconsumer_data
.pid
== 0 &&
3088 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3089 pthread_mutex_unlock(&ustconsumer_data
.pid_mutex
);
3090 ret
= start_consumerd(&ustconsumer_data
);
3092 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
3096 pthread_mutex_unlock(&ustconsumer_data
.pid_mutex
);
3104 /* Process by command type */
3105 switch (cmd_ctx
->lsm
->cmd_type
) {
3106 case LTTNG_ADD_CONTEXT
:
3108 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3109 cmd_ctx
->lsm
->u
.context
.channel_name
,
3110 cmd_ctx
->lsm
->u
.context
.event_name
,
3111 &cmd_ctx
->lsm
->u
.context
.ctx
);
3114 case LTTNG_DISABLE_CHANNEL
:
3116 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3117 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3120 case LTTNG_DISABLE_EVENT
:
3122 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3123 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3124 cmd_ctx
->lsm
->u
.disable
.name
);
3128 case LTTNG_DISABLE_ALL_EVENT
:
3130 DBG("Disabling all events");
3132 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3133 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3136 case LTTNG_ENABLE_CHANNEL
:
3138 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3139 &cmd_ctx
->lsm
->u
.channel
.chan
);
3142 case LTTNG_ENABLE_EVENT
:
3144 ret
= cmd_enable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3145 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3146 &cmd_ctx
->lsm
->u
.enable
.event
);
3149 case LTTNG_ENABLE_ALL_EVENT
:
3151 DBG("Enabling all events");
3153 ret
= cmd_enable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3154 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3155 cmd_ctx
->lsm
->u
.enable
.event
.type
);
3158 case LTTNG_LIST_TRACEPOINTS
:
3160 struct lttng_event
*events
;
3163 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3164 if (nb_events
< 0) {
3170 * Setup lttng message with payload size set to the event list size in
3171 * bytes and then copy list into the llm payload.
3173 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3179 /* Copy event list into message payload */
3180 memcpy(cmd_ctx
->llm
->payload
, events
,
3181 sizeof(struct lttng_event
) * nb_events
);
3188 case LTTNG_START_TRACE
:
3190 ret
= cmd_start_trace(cmd_ctx
->session
);
3193 case LTTNG_STOP_TRACE
:
3195 ret
= cmd_stop_trace(cmd_ctx
->session
);
3198 case LTTNG_CREATE_SESSION
:
3200 ret
= cmd_create_session(cmd_ctx
->lsm
->session
.name
,
3201 cmd_ctx
->lsm
->session
.path
);
3204 case LTTNG_DESTROY_SESSION
:
3206 ret
= cmd_destroy_session(cmd_ctx
->session
,
3207 cmd_ctx
->lsm
->session
.name
);
3210 case LTTNG_LIST_DOMAINS
:
3213 struct lttng_domain
*domains
;
3215 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3221 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3226 /* Copy event list into message payload */
3227 memcpy(cmd_ctx
->llm
->payload
, domains
,
3228 nb_dom
* sizeof(struct lttng_domain
));
3235 case LTTNG_LIST_CHANNELS
:
3238 struct lttng_channel
*channels
;
3240 nb_chan
= cmd_list_channels(cmd_ctx
->session
, &channels
);
3246 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3251 /* Copy event list into message payload */
3252 memcpy(cmd_ctx
->llm
->payload
, channels
,
3253 nb_chan
* sizeof(struct lttng_channel
));
3260 case LTTNG_LIST_EVENTS
:
3263 struct lttng_event
*events
= NULL
;
3265 nb_event
= cmd_list_events(cmd_ctx
->session
,
3266 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3272 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3277 /* Copy event list into message payload */
3278 memcpy(cmd_ctx
->llm
->payload
, events
,
3279 nb_event
* sizeof(struct lttng_event
));
3286 case LTTNG_LIST_SESSIONS
:
3288 session_lock_list();
3290 if (session_list_ptr
->count
== 0) {
3291 ret
= LTTCOMM_NO_SESSION
;
3292 session_unlock_list();
3296 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
3297 session_list_ptr
->count
);
3299 session_unlock_list();
3303 /* Filled the session array */
3304 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
3306 session_unlock_list();
3311 case LTTNG_CALIBRATE
:
3313 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3314 &cmd_ctx
->lsm
->u
.calibrate
);
3317 case LTTNG_REGISTER_CONSUMER
:
3319 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3320 cmd_ctx
->lsm
->u
.reg
.path
);
3329 if (cmd_ctx
->llm
== NULL
) {
3330 DBG("Missing llm structure. Allocating one.");
3331 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3335 /* Set return code */
3336 cmd_ctx
->llm
->ret_code
= ret
;
3338 if (cmd_ctx
->session
) {
3339 session_unlock(cmd_ctx
->session
);
3346 * This thread manage all clients request using the unix client socket for
3349 static void *thread_manage_clients(void *data
)
3351 int sock
= 0, ret
, i
, pollfd
;
3352 uint32_t revents
, nb_fd
;
3353 struct command_ctx
*cmd_ctx
= NULL
;
3354 struct lttng_poll_event events
;
3356 DBG("[thread] Manage client started");
3358 ret
= lttcomm_listen_unix_sock(client_sock
);
3364 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3365 * more will be added to this poll set.
3367 ret
= create_thread_poll_set(&events
, 2);
3372 /* Add the application registration socket */
3373 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3379 * Notify parent pid that we are ready to accept command for client side.
3381 if (opt_sig_parent
) {
3382 kill(ppid
, SIGCHLD
);
3386 DBG("Accepting client command ...");
3388 nb_fd
= LTTNG_POLL_GETNB(&events
);
3390 /* Inifinite blocking call, waiting for transmission */
3391 ret
= lttng_poll_wait(&events
, -1);
3396 for (i
= 0; i
< nb_fd
; i
++) {
3397 /* Fetch once the poll data */
3398 revents
= LTTNG_POLL_GETEV(&events
, i
);
3399 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3401 /* Thread quit pipe has been closed. Killing thread. */
3402 ret
= check_thread_quit_pipe(pollfd
, revents
);
3407 /* Event on the registration socket */
3408 if (pollfd
== client_sock
) {
3409 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3410 ERR("Client socket poll error");
3416 DBG("Wait for client response");
3418 sock
= lttcomm_accept_unix_sock(client_sock
);
3423 /* Allocate context command to process the client request */
3424 cmd_ctx
= malloc(sizeof(struct command_ctx
));
3425 if (cmd_ctx
== NULL
) {
3426 perror("malloc cmd_ctx");
3430 /* Allocate data buffer for reception */
3431 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
3432 if (cmd_ctx
->lsm
== NULL
) {
3433 perror("malloc cmd_ctx->lsm");
3437 cmd_ctx
->llm
= NULL
;
3438 cmd_ctx
->session
= NULL
;
3441 * Data is received from the lttng client. The struct
3442 * lttcomm_session_msg (lsm) contains the command and data request of
3445 DBG("Receiving data from client ...");
3446 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
,
3447 sizeof(struct lttcomm_session_msg
));
3449 DBG("Nothing recv() from client... continuing");
3455 // TODO: Validate cmd_ctx including sanity check for
3456 // security purpose.
3459 * This function dispatch the work to the kernel or userspace tracer
3460 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3461 * informations for the client. The command context struct contains
3462 * everything this function may needs.
3464 ret
= process_client_msg(cmd_ctx
);
3467 * TODO: Inform client somehow of the fatal error. At
3468 * this point, ret < 0 means that a malloc failed
3469 * (ENOMEM). Error detected but still accept command.
3471 clean_command_ctx(&cmd_ctx
);
3475 DBG("Sending response (size: %d, retcode: %s)",
3476 cmd_ctx
->lttng_msg_size
,
3477 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3478 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3480 ERR("Failed to send data back to client");
3483 clean_command_ctx(&cmd_ctx
);
3485 /* End of transmission */
3490 DBG("Client thread dying");
3491 unlink(client_unix_sock_path
);
3495 lttng_poll_clean(&events
);
3496 clean_command_ctx(&cmd_ctx
);
3502 * usage function on stderr
3504 static void usage(void)
3506 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
3507 fprintf(stderr
, " -h, --help Display this usage.\n");
3508 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
3509 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3510 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3511 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3512 fprintf(stderr
, " --ustconsumerd-err-sock PATH Specify path for the UST consumer error socket\n");
3513 fprintf(stderr
, " --ustconsumerd-cmd-sock PATH Specify path for the UST consumer command socket\n");
3514 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
3515 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3516 fprintf(stderr
, " -V, --version Show version number.\n");
3517 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3518 fprintf(stderr
, " -q, --quiet No output at all.\n");
3519 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3520 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3524 * daemon argument parsing
3526 static int parse_args(int argc
, char **argv
)
3530 static struct option long_options
[] = {
3531 { "client-sock", 1, 0, 'c' },
3532 { "apps-sock", 1, 0, 'a' },
3533 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3534 { "kconsumerd-err-sock", 1, 0, 'E' },
3535 { "ustconsumerd-cmd-sock", 1, 0, 'D' },
3536 { "ustconsumerd-err-sock", 1, 0, 'F' },
3537 { "daemonize", 0, 0, 'd' },
3538 { "sig-parent", 0, 0, 'S' },
3539 { "help", 0, 0, 'h' },
3540 { "group", 1, 0, 'g' },
3541 { "version", 0, 0, 'V' },
3542 { "quiet", 0, 0, 'q' },
3543 { "verbose", 0, 0, 'v' },
3544 { "verbose-consumer", 0, 0, 'Z' },
3549 int option_index
= 0;
3550 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:C:E:D:F:Z",
3551 long_options
, &option_index
);
3558 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
3560 fprintf(stderr
, " with arg %s\n", optarg
);
3564 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3567 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3573 opt_tracing_group
= strdup(optarg
);
3579 fprintf(stdout
, "%s\n", VERSION
);
3585 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3588 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3591 snprintf(ustconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3594 snprintf(ustconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3600 /* Verbose level can increase using multiple -v */
3604 opt_verbose_consumer
+= 1;
3607 /* Unknown option or other error.
3608 * Error is printed by getopt, just return */
3617 * Creates the two needed socket by the daemon.
3618 * apps_sock - The communication socket for all UST apps.
3619 * client_sock - The communication of the cli tool (lttng).
3621 static int init_daemon_socket(void)
3626 old_umask
= umask(0);
3628 /* Create client tool unix socket */
3629 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
3630 if (client_sock
< 0) {
3631 ERR("Create unix sock failed: %s", client_unix_sock_path
);
3636 /* File permission MUST be 660 */
3637 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3639 ERR("Set file permissions failed: %s", client_unix_sock_path
);
3644 /* Create the application unix socket */
3645 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
3646 if (apps_sock
< 0) {
3647 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
3652 /* File permission MUST be 666 */
3653 ret
= chmod(apps_unix_sock_path
,
3654 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
3656 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
3667 * Check if the global socket is available, and if a daemon is answering at the
3668 * other side. If yes, error is returned.
3670 static int check_existing_daemon(void)
3672 if (access(client_unix_sock_path
, F_OK
) < 0 &&
3673 access(apps_unix_sock_path
, F_OK
) < 0) {
3677 /* Is there anybody out there ? */
3678 if (lttng_session_daemon_alive()) {
3686 * Set the tracing group gid onto the client socket.
3688 * Race window between mkdir and chown is OK because we are going from more
3689 * permissive (root.root) to les permissive (root.tracing).
3691 static int set_permissions(void)
3696 gid
= allowed_group();
3699 WARN("No tracing group detected");
3702 ERR("Missing tracing group. Aborting execution.");
3708 /* Set lttng run dir */
3709 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
3711 ERR("Unable to set group on " LTTNG_RUNDIR
);
3715 /* lttng client socket path */
3716 ret
= chown(client_unix_sock_path
, 0, gid
);
3718 ERR("Unable to set group on %s", client_unix_sock_path
);
3722 /* kconsumer error socket path */
3723 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
3725 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
3729 /* ustconsumer error socket path */
3730 ret
= chown(ustconsumer_data
.err_unix_sock_path
, 0, gid
);
3732 ERR("Unable to set group on %s", ustconsumer_data
.err_unix_sock_path
);
3736 DBG("All permissions are set");
3743 * Create the pipe used to wake up the kernel thread.
3745 static int create_kernel_poll_pipe(void)
3747 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
3751 * Create the application command pipe to wake thread_manage_apps.
3753 static int create_apps_cmd_pipe(void)
3755 return pipe2(apps_cmd_pipe
, O_CLOEXEC
);
3759 * Create the lttng run directory needed for all global sockets and pipe.
3761 static int create_lttng_rundir(void)
3765 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
3767 if (errno
!= EEXIST
) {
3768 ERR("Unable to create " LTTNG_RUNDIR
);
3780 * Setup sockets and directory needed by the kconsumerd communication with the
3783 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
3786 const char *path
= consumer_data
->type
== LTTNG_CONSUMER_KERNEL
?
3787 KCONSUMERD_PATH
: USTCONSUMERD_PATH
;
3789 if (strlen(consumer_data
->err_unix_sock_path
) == 0) {
3790 snprintf(consumer_data
->err_unix_sock_path
, PATH_MAX
,
3791 consumer_data
->type
== LTTNG_CONSUMER_KERNEL
?
3792 KCONSUMERD_ERR_SOCK_PATH
:
3793 USTCONSUMERD_ERR_SOCK_PATH
);
3796 if (strlen(consumer_data
->cmd_unix_sock_path
) == 0) {
3797 snprintf(consumer_data
->cmd_unix_sock_path
, PATH_MAX
,
3798 consumer_data
->type
== LTTNG_CONSUMER_KERNEL
?
3799 KCONSUMERD_CMD_SOCK_PATH
:
3800 USTCONSUMERD_CMD_SOCK_PATH
);
3803 ret
= mkdir(path
, S_IRWXU
| S_IRWXG
);
3805 if (errno
!= EEXIST
) {
3806 ERR("Failed to create %s", path
);
3812 /* Create the kconsumerd error unix socket */
3813 consumer_data
->err_sock
=
3814 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
3815 if (consumer_data
->err_sock
< 0) {
3816 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
3821 /* File permission MUST be 660 */
3822 ret
= chmod(consumer_data
->err_unix_sock_path
,
3823 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3825 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
3835 * Signal handler for the daemon
3837 * Simply stop all worker threads, leaving main() return gracefully after
3838 * joining all threads and calling cleanup().
3840 static void sighandler(int sig
)
3844 DBG("SIGPIPE catched");
3847 DBG("SIGINT catched");
3851 DBG("SIGTERM catched");
3860 * Setup signal handler for :
3861 * SIGINT, SIGTERM, SIGPIPE
3863 static int set_signal_handler(void)
3866 struct sigaction sa
;
3869 if ((ret
= sigemptyset(&sigset
)) < 0) {
3870 perror("sigemptyset");
3874 sa
.sa_handler
= sighandler
;
3875 sa
.sa_mask
= sigset
;
3877 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3878 perror("sigaction");
3882 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3883 perror("sigaction");
3887 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3888 perror("sigaction");
3892 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3898 * Set open files limit to unlimited. This daemon can open a large number of
3899 * file descriptors in order to consumer multiple kernel traces.
3901 static void set_ulimit(void)
3906 /* The kernel does not allowed an infinite limit for open files */
3907 lim
.rlim_cur
= 65535;
3908 lim
.rlim_max
= 65535;
3910 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3912 perror("failed to set open files limit");
3919 int main(int argc
, char **argv
)
3923 const char *home_path
;
3925 /* Create thread quit pipe */
3926 if ((ret
= init_thread_quit_pipe()) < 0) {
3930 /* Parse arguments */
3932 if ((ret
= parse_args(argc
, argv
) < 0)) {
3945 /* Check if daemon is UID = 0 */
3946 is_root
= !getuid();
3949 ret
= create_lttng_rundir();
3954 if (strlen(apps_unix_sock_path
) == 0) {
3955 snprintf(apps_unix_sock_path
, PATH_MAX
,
3956 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3959 if (strlen(client_unix_sock_path
) == 0) {
3960 snprintf(client_unix_sock_path
, PATH_MAX
,
3961 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3964 /* Set global SHM for ust */
3965 if (strlen(wait_shm_path
) == 0) {
3966 snprintf(wait_shm_path
, PATH_MAX
,
3967 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
3970 home_path
= get_home_dir();
3971 if (home_path
== NULL
) {
3972 /* TODO: Add --socket PATH option */
3973 ERR("Can't get HOME directory for sockets creation.");
3978 if (strlen(apps_unix_sock_path
) == 0) {
3979 snprintf(apps_unix_sock_path
, PATH_MAX
,
3980 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3983 /* Set the cli tool unix socket path */
3984 if (strlen(client_unix_sock_path
) == 0) {
3985 snprintf(client_unix_sock_path
, PATH_MAX
,
3986 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3989 /* Set global SHM for ust */
3990 if (strlen(wait_shm_path
) == 0) {
3991 snprintf(wait_shm_path
, PATH_MAX
,
3992 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
3996 DBG("Client socket path %s", client_unix_sock_path
);
3997 DBG("Application socket path %s", apps_unix_sock_path
);
4000 * See if daemon already exist.
4002 if ((ret
= check_existing_daemon()) < 0) {
4003 ERR("Already running daemon.\n");
4005 * We do not goto exit because we must not cleanup()
4006 * because a daemon is already running.
4011 /* After this point, we can safely call cleanup() with "goto exit" */
4014 * These actions must be executed as root. We do that *after* setting up
4015 * the sockets path because we MUST make the check for another daemon using
4016 * those paths *before* trying to set the kernel consumer sockets and init
4020 ret
= set_consumer_sockets(&kconsumer_data
);
4024 ret
= set_consumer_sockets(&ustconsumer_data
);
4028 /* Setup kernel tracer */
4029 init_kernel_tracer();
4031 /* Set ulimit for open files */
4035 if ((ret
= set_signal_handler()) < 0) {
4039 /* Setup the needed unix socket */
4040 if ((ret
= init_daemon_socket()) < 0) {
4044 /* Set credentials to socket */
4045 if (is_root
&& ((ret
= set_permissions()) < 0)) {
4049 /* Get parent pid if -S, --sig-parent is specified. */
4050 if (opt_sig_parent
) {
4054 /* Setup the kernel pipe for waking up the kernel thread */
4055 if ((ret
= create_kernel_poll_pipe()) < 0) {
4059 /* Setup the thread apps communication pipe. */
4060 if ((ret
= create_apps_cmd_pipe()) < 0) {
4064 /* Init UST command queue. */
4065 cds_wfq_init(&ust_cmd_queue
.queue
);
4068 * Get session list pointer. This pointer MUST NOT be free(). This list is
4069 * statically declared in session.c
4071 session_list_ptr
= session_get_list();
4073 /* Set up max poll set size */
4074 lttng_poll_set_max_size();
4076 /* Create thread to manage the client socket */
4077 ret
= pthread_create(&client_thread
, NULL
,
4078 thread_manage_clients
, (void *) NULL
);
4080 perror("pthread_create clients");
4084 /* Create thread to dispatch registration */
4085 ret
= pthread_create(&dispatch_thread
, NULL
,
4086 thread_dispatch_ust_registration
, (void *) NULL
);
4088 perror("pthread_create dispatch");
4092 /* Create thread to manage application registration. */
4093 ret
= pthread_create(®_apps_thread
, NULL
,
4094 thread_registration_apps
, (void *) NULL
);
4096 perror("pthread_create registration");
4100 /* Create thread to manage application socket */
4101 ret
= pthread_create(&apps_thread
, NULL
,
4102 thread_manage_apps
, (void *) NULL
);
4104 perror("pthread_create apps");
4108 /* Create kernel thread to manage kernel event */
4109 ret
= pthread_create(&kernel_thread
, NULL
,
4110 thread_manage_kernel
, (void *) NULL
);
4112 perror("pthread_create kernel");
4116 ret
= pthread_join(kernel_thread
, &status
);
4118 perror("pthread_join");
4119 goto error
; /* join error, exit without cleanup */
4123 ret
= pthread_join(apps_thread
, &status
);
4125 perror("pthread_join");
4126 goto error
; /* join error, exit without cleanup */
4130 ret
= pthread_join(reg_apps_thread
, &status
);
4132 perror("pthread_join");
4133 goto error
; /* join error, exit without cleanup */
4137 ret
= pthread_join(dispatch_thread
, &status
);
4139 perror("pthread_join");
4140 goto error
; /* join error, exit without cleanup */
4144 ret
= pthread_join(client_thread
, &status
);
4146 perror("pthread_join");
4147 goto error
; /* join error, exit without cleanup */
4150 ret
= join_consumer_thread(&kconsumer_data
);
4152 perror("join_consumer");
4153 goto error
; /* join error, exit without cleanup */
4159 * cleanup() is called when no other thread is running.