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