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