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