1bb2c8c1acf2bfa43d61fb30fe0d5362bc064546
[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 DBG("[thread] Manage application registration started");
1110
1111 ret = lttcomm_listen_unix_sock(apps_sock);
1112 if (ret < 0) {
1113 goto error;
1114 }
1115
1116 /* First fd is always the quit pipe */
1117 pollfd[0].fd = thread_quit_pipe[0];
1118
1119 /* Apps socket */
1120 pollfd[1].fd = apps_sock;
1121 pollfd[1].events = POLLIN;
1122
1123 /* Notify all applications to register */
1124 ret = notify_ust_apps(1);
1125 if (ret < 0) {
1126 ERR("Failed to notify applications or create the wait shared memory.\n"
1127 "Execution continues but there might be problem for already running\n"
1128 "applications that wishes to register.");
1129 }
1130
1131 while (1) {
1132 DBG("Accepting application registration");
1133
1134 tracepoint(sessiond_th_apps_poll);
1135
1136 /* Inifinite blocking call, waiting for transmission */
1137 ret = poll(pollfd, 2, -1);
1138 if (ret < 0) {
1139 perror("poll register apps thread");
1140 goto error;
1141 }
1142
1143 /* Thread quit pipe has been closed. Killing thread. */
1144 if (pollfd[0].revents == POLLNVAL) {
1145 goto error;
1146 } else if (pollfd[1].revents == POLLERR) {
1147 ERR("Register apps socket poll error");
1148 goto error;
1149 }
1150
1151 sock = lttcomm_accept_unix_sock(apps_sock);
1152 if (sock < 0) {
1153 goto error;
1154 }
1155
1156 /* Create UST registration command for enqueuing */
1157 ust_cmd = malloc(sizeof(struct ust_command));
1158 if (ust_cmd == NULL) {
1159 perror("ust command malloc");
1160 goto error;
1161 }
1162
1163 /*
1164 * Using message-based transmissions to ensure we don't have to deal
1165 * with partially received messages.
1166 */
1167 ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg,
1168 sizeof(struct ust_register_msg));
1169 if (ret < 0 || ret < sizeof(struct ust_register_msg)) {
1170 if (ret < 0) {
1171 perror("lttcomm_recv_unix_sock register apps");
1172 } else {
1173 ERR("Wrong size received on apps register");
1174 }
1175 free(ust_cmd);
1176 close(sock);
1177 continue;
1178 }
1179
1180 ust_cmd->sock = sock;
1181
1182 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1183 " gid:%d sock:%d name:%s (version %d.%d)",
1184 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1185 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1186 ust_cmd->sock, ust_cmd->reg_msg.name,
1187 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1188 /*
1189 * Lock free enqueue the registration request.
1190 * The red pill has been taken! This apps will be part of the *system*
1191 */
1192 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
1193
1194 /*
1195 * Wake the registration queue futex.
1196 * Implicit memory barrier with the exchange in cds_wfq_enqueue.
1197 */
1198 futex_nto1_wake(&ust_cmd_queue.futex);
1199 }
1200
1201 error:
1202 DBG("UST Registration thread dying");
1203
1204 /* Notify that the registration thread is gone */
1205 notify_ust_apps(0);
1206
1207 close(apps_sock);
1208 close(sock);
1209
1210 unlink(apps_unix_sock_path);
1211
1212 return NULL;
1213 }
1214
1215 /*
1216 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
1217 * exec or it will fails.
1218 */
1219 static int spawn_kconsumerd_thread(void)
1220 {
1221 int ret;
1222
1223 /* Setup semaphore */
1224 sem_init(&kconsumerd_sem, 0, 0);
1225
1226 ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL);
1227 if (ret != 0) {
1228 perror("pthread_create kconsumerd");
1229 goto error;
1230 }
1231
1232 /* Wait for the kconsumerd thread to be ready */
1233 sem_wait(&kconsumerd_sem);
1234
1235 if (kconsumerd_pid == 0) {
1236 ERR("Kconsumerd did not start");
1237 goto error;
1238 }
1239
1240 return 0;
1241
1242 error:
1243 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1244 return ret;
1245 }
1246
1247 /*
1248 * Join kernel consumer thread
1249 */
1250 static int join_kconsumerd_thread(void)
1251 {
1252 void *status;
1253 int ret;
1254
1255 if (kconsumerd_pid != 0) {
1256 ret = kill(kconsumerd_pid, SIGTERM);
1257 if (ret) {
1258 ERR("Error killing kconsumerd");
1259 return ret;
1260 }
1261 return pthread_join(kconsumerd_thread, &status);
1262 } else {
1263 return 0;
1264 }
1265 }
1266
1267 /*
1268 * Fork and exec a kernel consumer daemon (kconsumerd).
1269 *
1270 * Return pid if successful else -1.
1271 */
1272 static pid_t spawn_kconsumerd(void)
1273 {
1274 int ret;
1275 pid_t pid;
1276 const char *verbosity;
1277
1278 DBG("Spawning kconsumerd");
1279
1280 pid = fork();
1281 if (pid == 0) {
1282 /*
1283 * Exec kconsumerd.
1284 */
1285 if (opt_verbose > 1 || opt_verbose_kconsumerd) {
1286 verbosity = "--verbose";
1287 } else {
1288 verbosity = "--quiet";
1289 }
1290 execl(INSTALL_BIN_PATH "/ltt-kconsumerd", "ltt-kconsumerd", verbosity, NULL);
1291 if (errno != 0) {
1292 perror("kernel start consumer exec");
1293 }
1294 exit(EXIT_FAILURE);
1295 } else if (pid > 0) {
1296 ret = pid;
1297 goto error;
1298 } else {
1299 perror("kernel start consumer fork");
1300 ret = -errno;
1301 goto error;
1302 }
1303
1304 error:
1305 return ret;
1306 }
1307
1308 /*
1309 * Spawn the kconsumerd daemon and session daemon thread.
1310 */
1311 static int start_kconsumerd(void)
1312 {
1313 int ret;
1314
1315 pthread_mutex_lock(&kconsumerd_pid_mutex);
1316 if (kconsumerd_pid != 0) {
1317 pthread_mutex_unlock(&kconsumerd_pid_mutex);
1318 goto end;
1319 }
1320
1321 ret = spawn_kconsumerd();
1322 if (ret < 0) {
1323 ERR("Spawning kconsumerd failed");
1324 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1325 pthread_mutex_unlock(&kconsumerd_pid_mutex);
1326 goto error;
1327 }
1328
1329 /* Setting up the global kconsumerd_pid */
1330 kconsumerd_pid = ret;
1331 pthread_mutex_unlock(&kconsumerd_pid_mutex);
1332
1333 DBG("Kconsumerd pid %d", ret);
1334
1335 DBG("Spawning kconsumerd thread");
1336 ret = spawn_kconsumerd_thread();
1337 if (ret < 0) {
1338 ERR("Fatal error spawning kconsumerd thread");
1339 goto error;
1340 }
1341
1342 end:
1343 return 0;
1344
1345 error:
1346 return ret;
1347 }
1348
1349 /*
1350 * modprobe_kernel_modules
1351 */
1352 static int modprobe_kernel_modules(void)
1353 {
1354 int ret = 0, i;
1355 char modprobe[256];
1356
1357 for (i = 0; i < ARRAY_SIZE(kernel_modules_list); i++) {
1358 ret = snprintf(modprobe, sizeof(modprobe),
1359 "/sbin/modprobe %s%s",
1360 kernel_modules_list[i].required ? "" : "--quiet ",
1361 kernel_modules_list[i].name);
1362 if (ret < 0) {
1363 perror("snprintf modprobe");
1364 goto error;
1365 }
1366 modprobe[sizeof(modprobe) - 1] = '\0';
1367 ret = system(modprobe);
1368 if (ret == -1) {
1369 ERR("Unable to launch modprobe for module %s",
1370 kernel_modules_list[i].name);
1371 } else if (kernel_modules_list[i].required
1372 && WEXITSTATUS(ret) != 0) {
1373 ERR("Unable to load module %s",
1374 kernel_modules_list[i].name);
1375 } else {
1376 DBG("Modprobe successfully %s",
1377 kernel_modules_list[i].name);
1378 }
1379 }
1380
1381 error:
1382 return ret;
1383 }
1384
1385 /*
1386 * mount_debugfs
1387 */
1388 static int mount_debugfs(char *path)
1389 {
1390 int ret;
1391 char *type = "debugfs";
1392
1393 ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid());
1394 if (ret < 0) {
1395 goto error;
1396 }
1397
1398 ret = mount(type, path, type, 0, NULL);
1399 if (ret < 0) {
1400 perror("mount debugfs");
1401 goto error;
1402 }
1403
1404 DBG("Mounted debugfs successfully at %s", path);
1405
1406 error:
1407 return ret;
1408 }
1409
1410 /*
1411 * Setup necessary data for kernel tracer action.
1412 */
1413 static void init_kernel_tracer(void)
1414 {
1415 int ret;
1416 char *proc_mounts = "/proc/mounts";
1417 char line[256];
1418 char *debugfs_path = NULL, *lttng_path;
1419 FILE *fp;
1420
1421 /* Detect debugfs */
1422 fp = fopen(proc_mounts, "r");
1423 if (fp == NULL) {
1424 ERR("Unable to probe %s", proc_mounts);
1425 goto error;
1426 }
1427
1428 while (fgets(line, sizeof(line), fp) != NULL) {
1429 if (strstr(line, "debugfs") != NULL) {
1430 /* Remove first string */
1431 strtok(line, " ");
1432 /* Dup string here so we can reuse line later on */
1433 debugfs_path = strdup(strtok(NULL, " "));
1434 DBG("Got debugfs path : %s", debugfs_path);
1435 break;
1436 }
1437 }
1438
1439 fclose(fp);
1440
1441 /* Mount debugfs if needded */
1442 if (debugfs_path == NULL) {
1443 ret = asprintf(&debugfs_path, "/mnt/debugfs");
1444 if (ret < 0) {
1445 perror("asprintf debugfs path");
1446 goto error;
1447 }
1448 ret = mount_debugfs(debugfs_path);
1449 if (ret < 0) {
1450 goto error;
1451 }
1452 }
1453
1454 /* Modprobe lttng kernel modules */
1455 ret = modprobe_kernel_modules();
1456 if (ret < 0) {
1457 goto error;
1458 }
1459
1460 /* Setup lttng kernel path */
1461 ret = asprintf(&lttng_path, "%s/lttng", debugfs_path);
1462 if (ret < 0) {
1463 perror("asprintf lttng path");
1464 goto error;
1465 }
1466
1467 /* Open debugfs lttng */
1468 kernel_tracer_fd = open(lttng_path, O_RDWR);
1469 if (kernel_tracer_fd < 0) {
1470 DBG("Failed to open %s", lttng_path);
1471 goto error;
1472 }
1473
1474 free(lttng_path);
1475 free(debugfs_path);
1476 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1477 return;
1478
1479 error:
1480 if (lttng_path) {
1481 free(lttng_path);
1482 }
1483 if (debugfs_path) {
1484 free(debugfs_path);
1485 }
1486 WARN("No kernel tracer available");
1487 kernel_tracer_fd = 0;
1488 return;
1489 }
1490
1491 /*
1492 * Start tracing by creating trace directory and sending FDs to the kernel
1493 * consumer.
1494 */
1495 static int start_kernel_trace(struct ltt_kernel_session *session)
1496 {
1497 int ret = 0;
1498
1499 if (session->kconsumer_fds_sent == 0) {
1500 /*
1501 * Assign default kernel consumer if no consumer assigned to the kernel
1502 * session. At this point, it's NOT suppose to be 0 but this is an extra
1503 * security check.
1504 */
1505 if (session->consumer_fd == 0) {
1506 session->consumer_fd = kconsumerd_cmd_sock;
1507 }
1508
1509 ret = send_kconsumerd_fds(session);
1510 if (ret < 0) {
1511 ERR("Send kconsumerd fds failed");
1512 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1513 goto error;
1514 }
1515
1516 session->kconsumer_fds_sent = 1;
1517 }
1518
1519 error:
1520 return ret;
1521 }
1522
1523 /*
1524 * Notify kernel thread to update it's pollfd.
1525 */
1526 static int notify_kernel_pollfd(void)
1527 {
1528 int ret;
1529
1530 /* Inform kernel thread of the new kernel channel */
1531 ret = write(kernel_poll_pipe[1], "!", 1);
1532 if (ret < 0) {
1533 perror("write kernel poll pipe");
1534 }
1535
1536 return ret;
1537 }
1538
1539 /*
1540 * Allocate a channel structure and fill it.
1541 */
1542 static struct lttng_channel *init_default_channel(enum lttng_domain_type domain_type,
1543 char *name)
1544 {
1545 struct lttng_channel *chan;
1546
1547 chan = malloc(sizeof(struct lttng_channel));
1548 if (chan == NULL) {
1549 perror("init channel malloc");
1550 goto error;
1551 }
1552
1553 if (snprintf(chan->name, NAME_MAX, "%s", name) < 0) {
1554 perror("snprintf channel name");
1555 goto error;
1556 }
1557
1558 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
1559 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1560 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1561
1562 switch (domain_type) {
1563 case LTTNG_DOMAIN_KERNEL:
1564 chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
1565 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1566 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1567 break;
1568 /* TODO: add UST */
1569 default:
1570 goto error; /* Not implemented */
1571 }
1572
1573 return chan;
1574
1575 error:
1576 free(chan);
1577 return NULL;
1578 }
1579
1580 /*
1581 * Create an UST session and add it to the session ust list.
1582 */
1583 static int create_ust_session(pid_t pid, struct ltt_session *session)
1584 {
1585 int ret = -1;
1586 struct ltt_ust_session *lus;
1587
1588 DBG("Creating UST session");
1589
1590 lus = trace_ust_create_session(session->path, pid);
1591 if (lus == NULL) {
1592 goto error;
1593 }
1594
1595 ret = mkdir_recursive(lus->path, S_IRWXU | S_IRWXG,
1596 geteuid(), allowed_group());
1597 if (ret < 0) {
1598 if (ret != -EEXIST) {
1599 ERR("Trace directory creation error");
1600 goto error;
1601 }
1602 }
1603
1604 /* Create session on the UST tracer */
1605 ret = ustctl_create_session(lus);
1606 if (ret < 0) {
1607 goto error;
1608 }
1609
1610 return 0;
1611
1612 error:
1613 free(lus);
1614 return ret;
1615 }
1616
1617 /*
1618 * Create a kernel tracer session then create the default channel.
1619 */
1620 static int create_kernel_session(struct ltt_session *session)
1621 {
1622 int ret;
1623
1624 DBG("Creating kernel session");
1625
1626 ret = kernel_create_session(session, kernel_tracer_fd);
1627 if (ret < 0) {
1628 ret = LTTCOMM_KERN_SESS_FAIL;
1629 goto error;
1630 }
1631
1632 /* Set kernel consumer socket fd */
1633 if (kconsumerd_cmd_sock) {
1634 session->kernel_session->consumer_fd = kconsumerd_cmd_sock;
1635 }
1636
1637 ret = mkdir_recursive(session->kernel_session->trace_path,
1638 S_IRWXU | S_IRWXG, geteuid(), allowed_group());
1639 if (ret < 0) {
1640 if (ret != -EEXIST) {
1641 ERR("Trace directory creation error");
1642 goto error;
1643 }
1644 }
1645
1646 error:
1647 return ret;
1648 }
1649
1650 /*
1651 * Using the session list, filled a lttng_session array to send back to the
1652 * client for session listing.
1653 *
1654 * The session list lock MUST be acquired before calling this function. Use
1655 * lock_session_list() and unlock_session_list().
1656 */
1657 static void list_lttng_sessions(struct lttng_session *sessions)
1658 {
1659 int i = 0;
1660 struct ltt_session *session;
1661
1662 DBG("Getting all available session");
1663 /*
1664 * Iterate over session list and append data after the control struct in
1665 * the buffer.
1666 */
1667 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
1668 strncpy(sessions[i].path, session->path, PATH_MAX);
1669 sessions[i].path[PATH_MAX - 1] = '\0';
1670 strncpy(sessions[i].name, session->name, NAME_MAX);
1671 sessions[i].name[NAME_MAX - 1] = '\0';
1672 i++;
1673 }
1674 }
1675
1676 /*
1677 * Fill lttng_channel array of all channels.
1678 */
1679 static void list_lttng_channels(struct ltt_session *session,
1680 struct lttng_channel *channels)
1681 {
1682 int i = 0;
1683 struct ltt_kernel_channel *kchan;
1684
1685 DBG("Listing channels for session %s", session->name);
1686
1687 /* Kernel channels */
1688 if (session->kernel_session != NULL) {
1689 cds_list_for_each_entry(kchan, &session->kernel_session->channel_list.head, list) {
1690 /* Copy lttng_channel struct to array */
1691 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
1692 channels[i].enabled = kchan->enabled;
1693 i++;
1694 }
1695 }
1696
1697 /* TODO: Missing UST listing */
1698 }
1699
1700 /*
1701 * Fill lttng_event array of all events in the channel.
1702 */
1703 static void list_lttng_events(struct ltt_kernel_channel *kchan,
1704 struct lttng_event *events)
1705 {
1706 /*
1707 * TODO: This is ONLY kernel. Need UST support.
1708 */
1709 int i = 0;
1710 struct ltt_kernel_event *event;
1711
1712 DBG("Listing events for channel %s", kchan->channel->name);
1713
1714 /* Kernel channels */
1715 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
1716 strncpy(events[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
1717 events[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1718 events[i].enabled = event->enabled;
1719 switch (event->event->instrumentation) {
1720 case LTTNG_KERNEL_TRACEPOINT:
1721 events[i].type = LTTNG_EVENT_TRACEPOINT;
1722 break;
1723 case LTTNG_KERNEL_KPROBE:
1724 case LTTNG_KERNEL_KRETPROBE:
1725 events[i].type = LTTNG_EVENT_PROBE;
1726 memcpy(&events[i].attr.probe, &event->event->u.kprobe,
1727 sizeof(struct lttng_kernel_kprobe));
1728 break;
1729 case LTTNG_KERNEL_FUNCTION:
1730 events[i].type = LTTNG_EVENT_FUNCTION;
1731 memcpy(&events[i].attr.ftrace, &event->event->u.ftrace,
1732 sizeof(struct lttng_kernel_function));
1733 break;
1734 }
1735 i++;
1736 }
1737 }
1738
1739 /*
1740 * Process the command requested by the lttng client within the command
1741 * context structure. This function make sure that the return structure (llm)
1742 * is set and ready for transmission before returning.
1743 *
1744 * Return any error encountered or 0 for success.
1745 */
1746 static int process_client_msg(struct command_ctx *cmd_ctx)
1747 {
1748 int ret = LTTCOMM_OK;
1749
1750 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
1751
1752 /*
1753 * Commands that DO NOT need a session.
1754 */
1755 switch (cmd_ctx->lsm->cmd_type) {
1756 case LTTNG_CREATE_SESSION:
1757 case LTTNG_LIST_SESSIONS:
1758 case LTTNG_LIST_TRACEPOINTS:
1759 case LTTNG_CALIBRATE:
1760 break;
1761 default:
1762 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
1763 cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session.name);
1764 if (cmd_ctx->session == NULL) {
1765 /* If session name not found */
1766 if (cmd_ctx->lsm->session.name != NULL) {
1767 ret = LTTCOMM_SESS_NOT_FOUND;
1768 } else { /* If no session name specified */
1769 ret = LTTCOMM_SELECT_SESS;
1770 }
1771 goto error;
1772 } else {
1773 /* Acquire lock for the session */
1774 lock_session(cmd_ctx->session);
1775 }
1776 break;
1777 }
1778
1779 /*
1780 * Check domain type for specific "pre-action".
1781 */
1782 switch (cmd_ctx->lsm->domain.type) {
1783 case LTTNG_DOMAIN_KERNEL:
1784 /* Kernel tracer check */
1785 if (kernel_tracer_fd == 0) {
1786 init_kernel_tracer();
1787 if (kernel_tracer_fd == 0) {
1788 ret = LTTCOMM_KERN_NA;
1789 goto error;
1790 }
1791 }
1792 /* Need a session for kernel command */
1793 switch (cmd_ctx->lsm->cmd_type) {
1794 case LTTNG_CALIBRATE:
1795 case LTTNG_CREATE_SESSION:
1796 case LTTNG_LIST_SESSIONS:
1797 case LTTNG_LIST_TRACEPOINTS:
1798 break;
1799 default:
1800 if (cmd_ctx->session->kernel_session == NULL) {
1801 ret = create_kernel_session(cmd_ctx->session);
1802 if (ret < 0) {
1803 ret = LTTCOMM_KERN_SESS_FAIL;
1804 goto error;
1805 }
1806 /* Start the kernel consumer daemon */
1807 if (kconsumerd_pid == 0 &&
1808 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
1809 ret = start_kconsumerd();
1810 if (ret < 0) {
1811 goto error;
1812 }
1813 }
1814 }
1815 }
1816 break;
1817 case LTTNG_DOMAIN_UST_PID:
1818 break;
1819 default:
1820 break;
1821 }
1822
1823 /* Process by command type */
1824 switch (cmd_ctx->lsm->cmd_type) {
1825 case LTTNG_ADD_CONTEXT:
1826 {
1827 struct lttng_kernel_context kctx;
1828
1829 /* Setup lttng message with no payload */
1830 ret = setup_lttng_msg(cmd_ctx, 0);
1831 if (ret < 0) {
1832 goto setup_error;
1833 }
1834
1835 switch (cmd_ctx->lsm->domain.type) {
1836 case LTTNG_DOMAIN_KERNEL:
1837 /* Create Kernel context */
1838 kctx.ctx = cmd_ctx->lsm->u.context.ctx.ctx;
1839 kctx.u.perf_counter.type = cmd_ctx->lsm->u.context.ctx.u.perf_counter.type;
1840 kctx.u.perf_counter.config = cmd_ctx->lsm->u.context.ctx.u.perf_counter.config;
1841 strncpy(kctx.u.perf_counter.name,
1842 cmd_ctx->lsm->u.context.ctx.u.perf_counter.name,
1843 LTTNG_SYMBOL_NAME_LEN);
1844 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1845
1846 /* Add kernel context to kernel tracer. See context.c */
1847 ret = add_kernel_context(cmd_ctx->session->kernel_session, &kctx,
1848 cmd_ctx->lsm->u.context.event_name,
1849 cmd_ctx->lsm->u.context.channel_name);
1850 if (ret != LTTCOMM_OK) {
1851 goto error;
1852 }
1853 break;
1854 default:
1855 /* TODO: Userspace tracing */
1856 ret = LTTCOMM_NOT_IMPLEMENTED;
1857 goto error;
1858 }
1859
1860 ret = LTTCOMM_OK;
1861 break;
1862 }
1863 case LTTNG_DISABLE_CHANNEL:
1864 {
1865 struct ltt_kernel_channel *kchan;
1866
1867 /* Setup lttng message with no payload */
1868 ret = setup_lttng_msg(cmd_ctx, 0);
1869 if (ret < 0) {
1870 goto setup_error;
1871 }
1872
1873 switch (cmd_ctx->lsm->domain.type) {
1874 case LTTNG_DOMAIN_KERNEL:
1875 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1876 cmd_ctx->session->kernel_session);
1877 if (kchan == NULL) {
1878 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1879 goto error;
1880 } else if (kchan->enabled == 1) {
1881 ret = kernel_disable_channel(kchan);
1882 if (ret < 0) {
1883 if (ret != EEXIST) {
1884 ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL;
1885 }
1886 goto error;
1887 }
1888 }
1889 kernel_wait_quiescent(kernel_tracer_fd);
1890 break;
1891 default:
1892 /* TODO: Userspace tracing */
1893 ret = LTTCOMM_NOT_IMPLEMENTED;
1894 goto error;
1895 }
1896
1897 ret = LTTCOMM_OK;
1898 break;
1899 }
1900 case LTTNG_DISABLE_EVENT:
1901 {
1902 struct ltt_kernel_channel *kchan;
1903 struct ltt_kernel_event *kevent;
1904
1905 /* Setup lttng message with no payload */
1906 ret = setup_lttng_msg(cmd_ctx, 0);
1907 if (ret < 0) {
1908 goto setup_error;
1909 }
1910
1911 switch (cmd_ctx->lsm->domain.type) {
1912 case LTTNG_DOMAIN_KERNEL:
1913 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1914 cmd_ctx->session->kernel_session);
1915 if (kchan == NULL) {
1916 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1917 goto error;
1918 }
1919
1920 kevent = trace_kernel_get_event_by_name(cmd_ctx->lsm->u.disable.name, kchan);
1921 if (kevent != NULL) {
1922 DBG("Disabling kernel event %s for channel %s.", kevent->event->name,
1923 kchan->channel->name);
1924 ret = kernel_disable_event(kevent);
1925 if (ret < 0) {
1926 ret = LTTCOMM_KERN_ENABLE_FAIL;
1927 goto error;
1928 }
1929 }
1930
1931 kernel_wait_quiescent(kernel_tracer_fd);
1932 break;
1933 default:
1934 /* TODO: Userspace tracing */
1935 ret = LTTCOMM_NOT_IMPLEMENTED;
1936 goto error;
1937 }
1938
1939 ret = LTTCOMM_OK;
1940 break;
1941 }
1942 case LTTNG_DISABLE_ALL_EVENT:
1943 {
1944 struct ltt_kernel_channel *kchan;
1945 struct ltt_kernel_event *kevent;
1946
1947 /* Setup lttng message with no payload */
1948 ret = setup_lttng_msg(cmd_ctx, 0);
1949 if (ret < 0) {
1950 goto setup_error;
1951 }
1952
1953 switch (cmd_ctx->lsm->domain.type) {
1954 case LTTNG_DOMAIN_KERNEL:
1955 DBG("Disabling all enabled kernel events");
1956 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1957 cmd_ctx->session->kernel_session);
1958 if (kchan == NULL) {
1959 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1960 goto error;
1961 }
1962
1963 /* For each event in the kernel session */
1964 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
1965 DBG("Disabling kernel event %s for channel %s.",
1966 kevent->event->name, kchan->channel->name);
1967 ret = kernel_disable_event(kevent);
1968 if (ret < 0) {
1969 continue;
1970 }
1971 }
1972
1973 /* Quiescent wait after event disable */
1974 kernel_wait_quiescent(kernel_tracer_fd);
1975 break;
1976 default:
1977 /* TODO: Userspace tracing */
1978 ret = LTTCOMM_NOT_IMPLEMENTED;
1979 goto error;
1980 }
1981
1982 ret = LTTCOMM_OK;
1983 break;
1984 }
1985 case LTTNG_ENABLE_CHANNEL:
1986 {
1987 struct ltt_kernel_channel *kchan;
1988
1989 /* Setup lttng message with no payload */
1990 ret = setup_lttng_msg(cmd_ctx, 0);
1991 if (ret < 0) {
1992 goto setup_error;
1993 }
1994
1995 switch (cmd_ctx->lsm->domain.type) {
1996 case LTTNG_DOMAIN_KERNEL:
1997 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1998 cmd_ctx->session->kernel_session);
1999 if (kchan == NULL) {
2000 /* Channel not found, creating it */
2001 DBG("Creating kernel channel");
2002
2003 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
2004 &cmd_ctx->lsm->u.channel.chan,
2005 cmd_ctx->session->kernel_session->trace_path);
2006 if (ret < 0) {
2007 ret = LTTCOMM_KERN_CHAN_FAIL;
2008 goto error;
2009 }
2010
2011 /* Notify kernel thread that there is a new channel */
2012 ret = notify_kernel_pollfd();
2013 if (ret < 0) {
2014 ret = LTTCOMM_FATAL;
2015 goto error;
2016 }
2017 } else if (kchan->enabled == 0) {
2018 ret = kernel_enable_channel(kchan);
2019 if (ret < 0) {
2020 if (ret != EEXIST) {
2021 ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL;
2022 }
2023 goto error;
2024 }
2025 }
2026
2027 kernel_wait_quiescent(kernel_tracer_fd);
2028 break;
2029 case LTTNG_DOMAIN_UST_PID:
2030
2031 break;
2032 default:
2033 ret = LTTCOMM_NOT_IMPLEMENTED;
2034 goto error;
2035 }
2036
2037 ret = LTTCOMM_OK;
2038 break;
2039 }
2040 case LTTNG_ENABLE_EVENT:
2041 {
2042 char *channel_name;
2043 struct ltt_kernel_channel *kchan;
2044 struct ltt_kernel_event *kevent;
2045 struct lttng_channel *chan;
2046
2047 /* Setup lttng message with no payload */
2048 ret = setup_lttng_msg(cmd_ctx, 0);
2049 if (ret < 0) {
2050 goto setup_error;
2051 }
2052
2053 channel_name = cmd_ctx->lsm->u.enable.channel_name;
2054
2055 switch (cmd_ctx->lsm->domain.type) {
2056 case LTTNG_DOMAIN_KERNEL:
2057 kchan = trace_kernel_get_channel_by_name(channel_name,
2058 cmd_ctx->session->kernel_session);
2059 if (kchan == NULL) {
2060 DBG("Channel not found. Creating channel %s", channel_name);
2061
2062 chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
2063 if (chan == NULL) {
2064 ret = LTTCOMM_FATAL;
2065 goto error;
2066 }
2067
2068 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
2069 chan, cmd_ctx->session->kernel_session->trace_path);
2070 if (ret < 0) {
2071 ret = LTTCOMM_KERN_CHAN_FAIL;
2072 goto error;
2073 }
2074 kchan = trace_kernel_get_channel_by_name(channel_name,
2075 cmd_ctx->session->kernel_session);
2076 if (kchan == NULL) {
2077 ERR("Channel %s not found after creation. Internal error, giving up.",
2078 channel_name);
2079 ret = LTTCOMM_FATAL;
2080 goto error;
2081 }
2082 }
2083
2084 kevent = trace_kernel_get_event_by_name(cmd_ctx->lsm->u.enable.event.name, kchan);
2085 if (kevent == NULL) {
2086 DBG("Creating kernel event %s for channel %s.",
2087 cmd_ctx->lsm->u.enable.event.name, channel_name);
2088 ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, kchan);
2089 } else {
2090 DBG("Enabling kernel event %s for channel %s.",
2091 kevent->event->name, channel_name);
2092 ret = kernel_enable_event(kevent);
2093 if (ret == -EEXIST) {
2094 ret = LTTCOMM_KERN_EVENT_EXIST;
2095 goto error;
2096 }
2097 }
2098
2099 if (ret < 0) {
2100 ret = LTTCOMM_KERN_ENABLE_FAIL;
2101 goto error;
2102 }
2103
2104 kernel_wait_quiescent(kernel_tracer_fd);
2105 break;
2106 default:
2107 /* TODO: Userspace tracing */
2108 ret = LTTCOMM_NOT_IMPLEMENTED;
2109 goto error;
2110 }
2111 ret = LTTCOMM_OK;
2112 break;
2113 }
2114 case LTTNG_ENABLE_ALL_EVENT:
2115 {
2116 int size, i;
2117 char *channel_name;
2118 struct ltt_kernel_channel *kchan;
2119 struct ltt_kernel_event *kevent;
2120 struct lttng_event *event_list;
2121 struct lttng_channel *chan;
2122
2123 /* Setup lttng message with no payload */
2124 ret = setup_lttng_msg(cmd_ctx, 0);
2125 if (ret < 0) {
2126 goto setup_error;
2127 }
2128
2129 DBG("Enabling all kernel event");
2130
2131 channel_name = cmd_ctx->lsm->u.enable.channel_name;
2132
2133 switch (cmd_ctx->lsm->domain.type) {
2134 case LTTNG_DOMAIN_KERNEL:
2135 kchan = trace_kernel_get_channel_by_name(channel_name,
2136 cmd_ctx->session->kernel_session);
2137 if (kchan == NULL) {
2138 DBG("Channel not found. Creating channel %s", channel_name);
2139
2140 chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
2141 if (chan == NULL) {
2142 ret = LTTCOMM_FATAL;
2143 goto error;
2144 }
2145
2146 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
2147 chan, cmd_ctx->session->kernel_session->trace_path);
2148 if (ret < 0) {
2149 ret = LTTCOMM_KERN_CHAN_FAIL;
2150 goto error;
2151 }
2152 kchan = trace_kernel_get_channel_by_name(channel_name,
2153 cmd_ctx->session->kernel_session);
2154 if (kchan == NULL) {
2155 ERR("Channel %s not found after creation. Internal error, giving up.",
2156 channel_name);
2157 ret = LTTCOMM_FATAL;
2158 goto error;
2159 }
2160 }
2161
2162 /* For each event in the kernel session */
2163 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
2164 DBG("Enabling kernel event %s for channel %s.",
2165 kevent->event->name, channel_name);
2166 ret = kernel_enable_event(kevent);
2167 if (ret < 0) {
2168 continue;
2169 }
2170 }
2171
2172 size = kernel_list_events(kernel_tracer_fd, &event_list);
2173 if (size < 0) {
2174 ret = LTTCOMM_KERN_LIST_FAIL;
2175 goto error;
2176 }
2177
2178 for (i = 0; i < size; i++) {
2179 kevent = trace_kernel_get_event_by_name(event_list[i].name, kchan);
2180 if (kevent == NULL) {
2181 /* Default event type for enable all */
2182 event_list[i].type = LTTNG_EVENT_TRACEPOINT;
2183 /* Enable each single tracepoint event */
2184 ret = kernel_create_event(&event_list[i], kchan);
2185 if (ret < 0) {
2186 /* Ignore error here and continue */
2187 }
2188 }
2189 }
2190
2191 free(event_list);
2192
2193 /* Quiescent wait after event enable */
2194 kernel_wait_quiescent(kernel_tracer_fd);
2195 break;
2196 default:
2197 /* TODO: Userspace tracing */
2198 ret = LTTCOMM_NOT_IMPLEMENTED;
2199 goto error;
2200 }
2201
2202 ret = LTTCOMM_OK;
2203 break;
2204 }
2205 case LTTNG_LIST_TRACEPOINTS:
2206 {
2207 struct lttng_event *events;
2208 ssize_t nb_events = 0;
2209
2210 switch (cmd_ctx->lsm->domain.type) {
2211 case LTTNG_DOMAIN_KERNEL:
2212 DBG("Listing kernel events");
2213 nb_events = kernel_list_events(kernel_tracer_fd, &events);
2214 if (nb_events < 0) {
2215 ret = LTTCOMM_KERN_LIST_FAIL;
2216 goto error;
2217 }
2218 break;
2219 default:
2220 /* TODO: Userspace listing */
2221 ret = LTTCOMM_NOT_IMPLEMENTED;
2222 break;
2223 }
2224
2225 /*
2226 * Setup lttng message with payload size set to the event list size in
2227 * bytes and then copy list into the llm payload.
2228 */
2229 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2230 if (ret < 0) {
2231 free(events);
2232 goto setup_error;
2233 }
2234
2235 /* Copy event list into message payload */
2236 memcpy(cmd_ctx->llm->payload, events,
2237 sizeof(struct lttng_event) * nb_events);
2238
2239 free(events);
2240
2241 ret = LTTCOMM_OK;
2242 break;
2243 }
2244 case LTTNG_START_TRACE:
2245 {
2246 struct ltt_kernel_channel *chan;
2247
2248 /* Setup lttng message with no payload */
2249 ret = setup_lttng_msg(cmd_ctx, 0);
2250 if (ret < 0) {
2251 goto setup_error;
2252 }
2253
2254 /* Kernel tracing */
2255 if (cmd_ctx->session->kernel_session != NULL) {
2256 if (cmd_ctx->session->kernel_session->metadata == NULL) {
2257 DBG("Open kernel metadata");
2258 ret = kernel_open_metadata(cmd_ctx->session->kernel_session,
2259 cmd_ctx->session->kernel_session->trace_path);
2260 if (ret < 0) {
2261 ret = LTTCOMM_KERN_META_FAIL;
2262 goto error;
2263 }
2264 }
2265
2266 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
2267 DBG("Opening kernel metadata stream");
2268 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
2269 ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session);
2270 if (ret < 0) {
2271 ERR("Kernel create metadata stream failed");
2272 ret = LTTCOMM_KERN_STREAM_FAIL;
2273 goto error;
2274 }
2275 }
2276 }
2277
2278 /* For each channel */
2279 cds_list_for_each_entry(chan,
2280 &cmd_ctx->session->kernel_session->channel_list.head, list) {
2281 if (chan->stream_count == 0) {
2282 ret = kernel_open_channel_stream(chan);
2283 if (ret < 0) {
2284 ERR("Kernel create channel stream failed");
2285 ret = LTTCOMM_KERN_STREAM_FAIL;
2286 goto error;
2287 }
2288 /* Update the stream global counter */
2289 cmd_ctx->session->kernel_session->stream_count_global += ret;
2290 }
2291 }
2292
2293 ret = start_kernel_trace(cmd_ctx->session->kernel_session);
2294 if (ret < 0) {
2295 ret = LTTCOMM_KERN_START_FAIL;
2296 goto error;
2297 }
2298
2299 DBG("Start kernel tracing");
2300 ret = kernel_start_session(cmd_ctx->session->kernel_session);
2301 if (ret < 0) {
2302 ERR("Kernel start session failed");
2303 ret = LTTCOMM_KERN_START_FAIL;
2304 goto error;
2305 }
2306
2307 /* Quiescent wait after starting trace */
2308 kernel_wait_quiescent(kernel_tracer_fd);
2309 }
2310
2311 /* TODO: Start all UST traces */
2312
2313 ret = LTTCOMM_OK;
2314 break;
2315 }
2316 case LTTNG_STOP_TRACE:
2317 {
2318 struct ltt_kernel_channel *chan;
2319 /* Setup lttng message with no payload */
2320 ret = setup_lttng_msg(cmd_ctx, 0);
2321 if (ret < 0) {
2322 goto setup_error;
2323 }
2324
2325 /* Kernel tracer */
2326 if (cmd_ctx->session->kernel_session != NULL) {
2327 DBG("Stop kernel tracing");
2328
2329 ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd);
2330 if (ret < 0) {
2331 ERR("Kernel metadata flush failed");
2332 }
2333
2334 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
2335 ret = kernel_flush_buffer(chan);
2336 if (ret < 0) {
2337 ERR("Kernel flush buffer error");
2338 }
2339 }
2340
2341 ret = kernel_stop_session(cmd_ctx->session->kernel_session);
2342 if (ret < 0) {
2343 ERR("Kernel stop session failed");
2344 ret = LTTCOMM_KERN_STOP_FAIL;
2345 goto error;
2346 }
2347
2348 /* Quiescent wait after stopping trace */
2349 kernel_wait_quiescent(kernel_tracer_fd);
2350 }
2351
2352 /* TODO : User-space tracer */
2353
2354 ret = LTTCOMM_OK;
2355 break;
2356 }
2357 case LTTNG_CREATE_SESSION:
2358 {
2359 /* Setup lttng message with no payload */
2360 ret = setup_lttng_msg(cmd_ctx, 0);
2361 if (ret < 0) {
2362 goto setup_error;
2363 }
2364
2365 tracepoint(create_session_start);
2366 ret = create_session(cmd_ctx->lsm->session.name, cmd_ctx->lsm->session.path);
2367 tracepoint(create_session_end);
2368 if (ret < 0) {
2369 if (ret == -EEXIST) {
2370 ret = LTTCOMM_EXIST_SESS;
2371 } else {
2372 ret = LTTCOMM_FATAL;
2373 }
2374 goto error;
2375 }
2376
2377 ret = LTTCOMM_OK;
2378 break;
2379 }
2380 case LTTNG_DESTROY_SESSION:
2381 {
2382 /* Setup lttng message with no payload */
2383 ret = setup_lttng_msg(cmd_ctx, 0);
2384 if (ret < 0) {
2385 goto setup_error;
2386 }
2387
2388 /* Clean kernel session teardown */
2389 teardown_kernel_session(cmd_ctx->session);
2390
2391 tracepoint(destroy_session_start);
2392 ret = destroy_session(cmd_ctx->lsm->session.name);
2393 tracepoint(destroy_session_end);
2394 if (ret < 0) {
2395 ret = LTTCOMM_FATAL;
2396 goto error;
2397 }
2398
2399 /*
2400 * Must notify the kernel thread here to update it's pollfd in order to
2401 * remove the channel(s)' fd just destroyed.
2402 */
2403 ret = notify_kernel_pollfd();
2404 if (ret < 0) {
2405 ret = LTTCOMM_FATAL;
2406 goto error;
2407 }
2408
2409 ret = LTTCOMM_OK;
2410 break;
2411 }
2412 case LTTNG_LIST_DOMAINS:
2413 {
2414 size_t nb_dom = 0;
2415
2416 if (cmd_ctx->session->kernel_session != NULL) {
2417 nb_dom++;
2418 }
2419
2420 nb_dom += cmd_ctx->session->ust_session_list.count;
2421
2422 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_domain) * nb_dom);
2423 if (ret < 0) {
2424 goto setup_error;
2425 }
2426
2427 ((struct lttng_domain *)(cmd_ctx->llm->payload))[0].type =
2428 LTTNG_DOMAIN_KERNEL;
2429
2430 /* TODO: User-space tracer domain support */
2431 ret = LTTCOMM_OK;
2432 break;
2433 }
2434 case LTTNG_LIST_CHANNELS:
2435 {
2436 /*
2437 * TODO: Only kernel channels are listed here. UST listing
2438 * is needed on lttng-ust 2.0 release.
2439 */
2440 size_t nb_chan = 0;
2441 if (cmd_ctx->session->kernel_session != NULL) {
2442 nb_chan += cmd_ctx->session->kernel_session->channel_count;
2443 }
2444
2445 ret = setup_lttng_msg(cmd_ctx,
2446 sizeof(struct lttng_channel) * nb_chan);
2447 if (ret < 0) {
2448 goto setup_error;
2449 }
2450
2451 list_lttng_channels(cmd_ctx->session,
2452 (struct lttng_channel *)(cmd_ctx->llm->payload));
2453
2454 ret = LTTCOMM_OK;
2455 break;
2456 }
2457 case LTTNG_LIST_EVENTS:
2458 {
2459 /*
2460 * TODO: Only kernel events are listed here. UST listing
2461 * is needed on lttng-ust 2.0 release.
2462 */
2463 size_t nb_event = 0;
2464 struct ltt_kernel_channel *kchan = NULL;
2465
2466 if (cmd_ctx->session->kernel_session != NULL) {
2467 kchan = trace_kernel_get_channel_by_name(cmd_ctx->lsm->u.list.channel_name,
2468 cmd_ctx->session->kernel_session);
2469 if (kchan == NULL) {
2470 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2471 goto error;
2472 }
2473 nb_event += kchan->event_count;
2474 }
2475
2476 ret = setup_lttng_msg(cmd_ctx,
2477 sizeof(struct lttng_event) * nb_event);
2478 if (ret < 0) {
2479 goto setup_error;
2480 }
2481
2482 DBG("Listing events (%zu events)", nb_event);
2483
2484 list_lttng_events(kchan,
2485 (struct lttng_event *)(cmd_ctx->llm->payload));
2486
2487 ret = LTTCOMM_OK;
2488 break;
2489 }
2490 case LTTNG_LIST_SESSIONS:
2491 {
2492 lock_session_list();
2493
2494 if (session_list_ptr->count == 0) {
2495 ret = LTTCOMM_NO_SESSION;
2496 unlock_session_list();
2497 goto error;
2498 }
2499
2500 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) *
2501 session_list_ptr->count);
2502 if (ret < 0) {
2503 unlock_session_list();
2504 goto setup_error;
2505 }
2506
2507 /* Filled the session array */
2508 list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload));
2509
2510 unlock_session_list();
2511
2512 ret = LTTCOMM_OK;
2513 break;
2514 }
2515 case LTTNG_CALIBRATE:
2516 {
2517 /* Setup lttng message with no payload */
2518 ret = setup_lttng_msg(cmd_ctx, 0);
2519 if (ret < 0) {
2520 goto setup_error;
2521 }
2522
2523 switch (cmd_ctx->lsm->domain.type) {
2524 case LTTNG_DOMAIN_KERNEL:
2525 {
2526 struct lttng_kernel_calibrate kcalibrate;
2527
2528 kcalibrate.type = cmd_ctx->lsm->u.calibrate.type;
2529 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2530 if (ret < 0) {
2531 ret = LTTCOMM_KERN_ENABLE_FAIL;
2532 goto error;
2533 }
2534 break;
2535 }
2536 default:
2537 /* TODO: Userspace tracing */
2538 ret = LTTCOMM_NOT_IMPLEMENTED;
2539 goto error;
2540 }
2541 ret = LTTCOMM_OK;
2542 break;
2543 }
2544 case LTTNG_REGISTER_CONSUMER:
2545 {
2546 int sock;
2547
2548 /* Setup lttng message with no payload */
2549 ret = setup_lttng_msg(cmd_ctx, 0);
2550 if (ret < 0) {
2551 goto setup_error;
2552 }
2553
2554 switch (cmd_ctx->lsm->domain.type) {
2555 case LTTNG_DOMAIN_KERNEL:
2556 {
2557 /* Can't register a consumer if there is already one */
2558 if (cmd_ctx->session->kernel_session->consumer_fd != 0) {
2559 ret = LTTCOMM_CONNECT_FAIL;
2560 goto error;
2561 }
2562
2563 sock = lttcomm_connect_unix_sock(cmd_ctx->lsm->u.reg.path);
2564 if (sock < 0) {
2565 ret = LTTCOMM_CONNECT_FAIL;
2566 goto error;
2567 }
2568
2569 cmd_ctx->session->kernel_session->consumer_fd = sock;
2570 break;
2571 }
2572 default:
2573 /* TODO: Userspace tracing */
2574 ret = LTTCOMM_NOT_IMPLEMENTED;
2575 goto error;
2576 }
2577
2578 ret = LTTCOMM_OK;
2579 break;
2580 }
2581
2582 default:
2583 /* Undefined command */
2584 ret = setup_lttng_msg(cmd_ctx, 0);
2585 if (ret < 0) {
2586 goto setup_error;
2587 }
2588
2589 ret = LTTCOMM_UND;
2590 break;
2591 }
2592
2593 /* Set return code */
2594 cmd_ctx->llm->ret_code = ret;
2595
2596 if (cmd_ctx->session) {
2597 unlock_session(cmd_ctx->session);
2598 }
2599
2600 return ret;
2601
2602 error:
2603 if (cmd_ctx->llm == NULL) {
2604 DBG("Missing llm structure. Allocating one.");
2605 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
2606 goto setup_error;
2607 }
2608 }
2609 /* Notify client of error */
2610 cmd_ctx->llm->ret_code = ret;
2611
2612 setup_error:
2613 if (cmd_ctx->session) {
2614 unlock_session(cmd_ctx->session);
2615 }
2616 return ret;
2617 }
2618
2619 /*
2620 * This thread manage all clients request using the unix client socket for
2621 * communication.
2622 */
2623 static void *thread_manage_clients(void *data)
2624 {
2625 int sock = 0, ret;
2626 struct command_ctx *cmd_ctx = NULL;
2627 struct pollfd pollfd[2];
2628
2629 tracepoint(sessiond_th_cli_start);
2630
2631 DBG("[thread] Manage client started");
2632
2633 ret = lttcomm_listen_unix_sock(client_sock);
2634 if (ret < 0) {
2635 goto error;
2636 }
2637
2638 /* First fd is always the quit pipe */
2639 pollfd[0].fd = thread_quit_pipe[0];
2640
2641 /* Apps socket */
2642 pollfd[1].fd = client_sock;
2643 pollfd[1].events = POLLIN;
2644
2645 /* Notify parent pid that we are ready
2646 * to accept command for client side.
2647 */
2648 if (opt_sig_parent) {
2649 kill(ppid, SIGCHLD);
2650 }
2651
2652 while (1) {
2653 DBG("Accepting client command ...");
2654
2655 tracepoint(sessiond_th_cli_poll);
2656
2657 /* Inifinite blocking call, waiting for transmission */
2658 ret = poll(pollfd, 2, -1);
2659 if (ret < 0) {
2660 perror("poll client thread");
2661 goto error;
2662 }
2663
2664 /* Thread quit pipe has been closed. Killing thread. */
2665 if (pollfd[0].revents == POLLNVAL) {
2666 goto error;
2667 } else if (pollfd[1].revents == POLLERR) {
2668 ERR("Client socket poll error");
2669 goto error;
2670 }
2671
2672 sock = lttcomm_accept_unix_sock(client_sock);
2673 if (sock < 0) {
2674 goto error;
2675 }
2676
2677 /* Allocate context command to process the client request */
2678 cmd_ctx = malloc(sizeof(struct command_ctx));
2679
2680 /* Allocate data buffer for reception */
2681 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
2682 cmd_ctx->llm = NULL;
2683 cmd_ctx->session = NULL;
2684
2685 /*
2686 * Data is received from the lttng client. The struct
2687 * lttcomm_session_msg (lsm) contains the command and data request of
2688 * the client.
2689 */
2690 DBG("Receiving data from client ...");
2691 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
2692 if (ret <= 0) {
2693 continue;
2694 }
2695
2696 // TODO: Validate cmd_ctx including sanity check for security purpose.
2697
2698 /*
2699 * This function dispatch the work to the kernel or userspace tracer
2700 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2701 * informations for the client. The command context struct contains
2702 * everything this function may needs.
2703 */
2704 ret = process_client_msg(cmd_ctx);
2705 if (ret < 0) {
2706 /* TODO: Inform client somehow of the fatal error. At this point,
2707 * ret < 0 means that a malloc failed (ENOMEM). */
2708 /* Error detected but still accept command */
2709 clean_command_ctx(&cmd_ctx);
2710 continue;
2711 }
2712
2713 DBG("Sending response (size: %d, retcode: %d)",
2714 cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code);
2715 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
2716 if (ret < 0) {
2717 ERR("Failed to send data back to client");
2718 }
2719
2720 clean_command_ctx(&cmd_ctx);
2721
2722 /* End of transmission */
2723 close(sock);
2724 }
2725
2726 error:
2727 DBG("Client thread dying");
2728 if (client_sock) {
2729 close(client_sock);
2730 }
2731 if (sock) {
2732 close(sock);
2733 }
2734
2735 unlink(client_unix_sock_path);
2736
2737 clean_command_ctx(&cmd_ctx);
2738 return NULL;
2739 }
2740
2741
2742 /*
2743 * usage function on stderr
2744 */
2745 static void usage(void)
2746 {
2747 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
2748 fprintf(stderr, " -h, --help Display this usage.\n");
2749 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
2750 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2751 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2752 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2753 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
2754 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2755 fprintf(stderr, " -V, --version Show version number.\n");
2756 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2757 fprintf(stderr, " -q, --quiet No output at all.\n");
2758 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2759 fprintf(stderr, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2760 }
2761
2762 /*
2763 * daemon argument parsing
2764 */
2765 static int parse_args(int argc, char **argv)
2766 {
2767 int c;
2768
2769 static struct option long_options[] = {
2770 { "client-sock", 1, 0, 'c' },
2771 { "apps-sock", 1, 0, 'a' },
2772 { "kconsumerd-cmd-sock", 1, 0, 0 },
2773 { "kconsumerd-err-sock", 1, 0, 0 },
2774 { "daemonize", 0, 0, 'd' },
2775 { "sig-parent", 0, 0, 'S' },
2776 { "help", 0, 0, 'h' },
2777 { "group", 1, 0, 'g' },
2778 { "version", 0, 0, 'V' },
2779 { "quiet", 0, 0, 'q' },
2780 { "verbose", 0, 0, 'v' },
2781 { "verbose-kconsumerd", 0, 0, 'Z' },
2782 { NULL, 0, 0, 0 }
2783 };
2784
2785 while (1) {
2786 int option_index = 0;
2787 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:Z", long_options, &option_index);
2788 if (c == -1) {
2789 break;
2790 }
2791
2792 switch (c) {
2793 case 0:
2794 fprintf(stderr, "option %s", long_options[option_index].name);
2795 if (optarg) {
2796 fprintf(stderr, " with arg %s\n", optarg);
2797 }
2798 break;
2799 case 'c':
2800 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
2801 break;
2802 case 'a':
2803 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
2804 break;
2805 case 'd':
2806 opt_daemon = 1;
2807 break;
2808 case 'g':
2809 opt_tracing_group = strdup(optarg);
2810 break;
2811 case 'h':
2812 usage();
2813 exit(EXIT_FAILURE);
2814 case 'V':
2815 fprintf(stdout, "%s\n", VERSION);
2816 exit(EXIT_SUCCESS);
2817 case 'S':
2818 opt_sig_parent = 1;
2819 break;
2820 case 'E':
2821 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
2822 break;
2823 case 'C':
2824 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
2825 break;
2826 case 'q':
2827 opt_quiet = 1;
2828 break;
2829 case 'v':
2830 /* Verbose level can increase using multiple -v */
2831 opt_verbose += 1;
2832 break;
2833 case 'Z':
2834 opt_verbose_kconsumerd += 1;
2835 break;
2836 default:
2837 /* Unknown option or other error.
2838 * Error is printed by getopt, just return */
2839 return -1;
2840 }
2841 }
2842
2843 return 0;
2844 }
2845
2846 /*
2847 * Creates the two needed socket by the daemon.
2848 * apps_sock - The communication socket for all UST apps.
2849 * client_sock - The communication of the cli tool (lttng).
2850 */
2851 static int init_daemon_socket(void)
2852 {
2853 int ret = 0;
2854 mode_t old_umask;
2855
2856 old_umask = umask(0);
2857
2858 /* Create client tool unix socket */
2859 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
2860 if (client_sock < 0) {
2861 ERR("Create unix sock failed: %s", client_unix_sock_path);
2862 ret = -1;
2863 goto end;
2864 }
2865
2866 /* File permission MUST be 660 */
2867 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2868 if (ret < 0) {
2869 ERR("Set file permissions failed: %s", client_unix_sock_path);
2870 perror("chmod");
2871 goto end;
2872 }
2873
2874 /* Create the application unix socket */
2875 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
2876 if (apps_sock < 0) {
2877 ERR("Create unix sock failed: %s", apps_unix_sock_path);
2878 ret = -1;
2879 goto end;
2880 }
2881
2882 /* File permission MUST be 666 */
2883 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
2884 if (ret < 0) {
2885 ERR("Set file permissions failed: %s", apps_unix_sock_path);
2886 perror("chmod");
2887 goto end;
2888 }
2889
2890 end:
2891 umask(old_umask);
2892 return ret;
2893 }
2894
2895 /*
2896 * Check if the global socket is available, and if a daemon is answering
2897 * at the other side. If yes, error is returned.
2898 */
2899 static int check_existing_daemon(void)
2900 {
2901 if (access(client_unix_sock_path, F_OK) < 0 &&
2902 access(apps_unix_sock_path, F_OK) < 0) {
2903 return 0;
2904 }
2905 /* Is there anybody out there ? */
2906 if (lttng_session_daemon_alive()) {
2907 return -EEXIST;
2908 } else {
2909 return 0;
2910 }
2911 }
2912
2913 /*
2914 * Set the tracing group gid onto the client socket.
2915 *
2916 * Race window between mkdir and chown is OK because we are going from more
2917 * permissive (root.root) to les permissive (root.tracing).
2918 */
2919 static int set_permissions(void)
2920 {
2921 int ret;
2922 gid_t gid;
2923
2924 gid = allowed_group();
2925 if (gid < 0) {
2926 if (is_root) {
2927 WARN("No tracing group detected");
2928 ret = 0;
2929 } else {
2930 ERR("Missing tracing group. Aborting execution.");
2931 ret = -1;
2932 }
2933 goto end;
2934 }
2935
2936 /* Set lttng run dir */
2937 ret = chown(LTTNG_RUNDIR, 0, gid);
2938 if (ret < 0) {
2939 ERR("Unable to set group on " LTTNG_RUNDIR);
2940 perror("chown");
2941 }
2942
2943 /* lttng client socket path */
2944 ret = chown(client_unix_sock_path, 0, gid);
2945 if (ret < 0) {
2946 ERR("Unable to set group on %s", client_unix_sock_path);
2947 perror("chown");
2948 }
2949
2950 /* kconsumerd error socket path */
2951 ret = chown(kconsumerd_err_unix_sock_path, 0, gid);
2952 if (ret < 0) {
2953 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
2954 perror("chown");
2955 }
2956
2957 DBG("All permissions are set");
2958
2959 end:
2960 return ret;
2961 }
2962
2963 /*
2964 * Create the pipe used to wake up the kernel thread.
2965 */
2966 static int create_kernel_poll_pipe(void)
2967 {
2968 return pipe2(kernel_poll_pipe, O_CLOEXEC);
2969 }
2970
2971 /*
2972 * Create the application command pipe to wake thread_manage_apps.
2973 */
2974 static int create_apps_cmd_pipe(void)
2975 {
2976 return pipe2(apps_cmd_pipe, O_CLOEXEC);
2977 }
2978
2979 /*
2980 * Create the lttng run directory needed for all global sockets and pipe.
2981 */
2982 static int create_lttng_rundir(void)
2983 {
2984 int ret;
2985
2986 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
2987 if (ret < 0) {
2988 if (errno != EEXIST) {
2989 ERR("Unable to create " LTTNG_RUNDIR);
2990 goto error;
2991 } else {
2992 ret = 0;
2993 }
2994 }
2995
2996 error:
2997 return ret;
2998 }
2999
3000 /*
3001 * Setup sockets and directory needed by the kconsumerd communication with the
3002 * session daemon.
3003 */
3004 static int set_kconsumerd_sockets(void)
3005 {
3006 int ret;
3007
3008 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
3009 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
3010 }
3011
3012 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
3013 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
3014 }
3015
3016 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
3017 if (ret < 0) {
3018 if (errno != EEXIST) {
3019 ERR("Failed to create " KCONSUMERD_PATH);
3020 goto error;
3021 }
3022 ret = 0;
3023 }
3024
3025 /* Create the kconsumerd error unix socket */
3026 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
3027 if (kconsumerd_err_sock < 0) {
3028 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
3029 ret = -1;
3030 goto error;
3031 }
3032
3033 /* File permission MUST be 660 */
3034 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3035 if (ret < 0) {
3036 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
3037 perror("chmod");
3038 goto error;
3039 }
3040
3041 error:
3042 return ret;
3043 }
3044
3045 /*
3046 * Signal handler for the daemon
3047 *
3048 * Simply stop all worker threads, leaving main() return gracefully
3049 * after joining all threads and calling cleanup().
3050 */
3051 static void sighandler(int sig)
3052 {
3053 switch (sig) {
3054 case SIGPIPE:
3055 DBG("SIGPIPE catched");
3056 return;
3057 case SIGINT:
3058 DBG("SIGINT catched");
3059 stop_threads();
3060 break;
3061 case SIGTERM:
3062 DBG("SIGTERM catched");
3063 stop_threads();
3064 break;
3065 default:
3066 break;
3067 }
3068 }
3069
3070 /*
3071 * Setup signal handler for :
3072 * SIGINT, SIGTERM, SIGPIPE
3073 */
3074 static int set_signal_handler(void)
3075 {
3076 int ret = 0;
3077 struct sigaction sa;
3078 sigset_t sigset;
3079
3080 if ((ret = sigemptyset(&sigset)) < 0) {
3081 perror("sigemptyset");
3082 return ret;
3083 }
3084
3085 sa.sa_handler = sighandler;
3086 sa.sa_mask = sigset;
3087 sa.sa_flags = 0;
3088 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
3089 perror("sigaction");
3090 return ret;
3091 }
3092
3093 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
3094 perror("sigaction");
3095 return ret;
3096 }
3097
3098 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
3099 perror("sigaction");
3100 return ret;
3101 }
3102
3103 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3104
3105 return ret;
3106 }
3107
3108 /*
3109 * Set open files limit to unlimited. This daemon can open a large number of
3110 * file descriptors in order to consumer multiple kernel traces.
3111 */
3112 static void set_ulimit(void)
3113 {
3114 int ret;
3115 struct rlimit lim;
3116
3117 /* The kernel does not allowed an infinite limit for open files */
3118 lim.rlim_cur = 65535;
3119 lim.rlim_max = 65535;
3120
3121 ret = setrlimit(RLIMIT_NOFILE, &lim);
3122 if (ret < 0) {
3123 perror("failed to set open files limit");
3124 }
3125 }
3126
3127 /*
3128 * main
3129 */
3130 int main(int argc, char **argv)
3131 {
3132 int ret = 0;
3133 void *status;
3134 const char *home_path;
3135
3136 tracepoint(sessiond_boot_start);
3137
3138 /* Create thread quit pipe */
3139 if ((ret = init_thread_quit_pipe()) < 0) {
3140 goto error;
3141 }
3142
3143 /* Parse arguments */
3144 progname = argv[0];
3145 if ((ret = parse_args(argc, argv) < 0)) {
3146 goto error;
3147 }
3148
3149 /* Daemonize */
3150 if (opt_daemon) {
3151 ret = daemon(0, 0);
3152 if (ret < 0) {
3153 perror("daemon");
3154 goto error;
3155 }
3156 }
3157
3158 /* Check if daemon is UID = 0 */
3159 is_root = !getuid();
3160
3161 if (is_root) {
3162 ret = create_lttng_rundir();
3163 if (ret < 0) {
3164 goto error;
3165 }
3166
3167 if (strlen(apps_unix_sock_path) == 0) {
3168 snprintf(apps_unix_sock_path, PATH_MAX,
3169 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
3170 }
3171
3172 if (strlen(client_unix_sock_path) == 0) {
3173 snprintf(client_unix_sock_path, PATH_MAX,
3174 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
3175 }
3176
3177 /* Set global SHM for ust */
3178 if (strlen(wait_shm_path) == 0) {
3179 snprintf(wait_shm_path, PATH_MAX,
3180 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
3181 }
3182 } else {
3183 home_path = get_home_dir();
3184 if (home_path == NULL) {
3185 /* TODO: Add --socket PATH option */
3186 ERR("Can't get HOME directory for sockets creation.");
3187 ret = -EPERM;
3188 goto error;
3189 }
3190
3191 if (strlen(apps_unix_sock_path) == 0) {
3192 snprintf(apps_unix_sock_path, PATH_MAX,
3193 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
3194 }
3195
3196 /* Set the cli tool unix socket path */
3197 if (strlen(client_unix_sock_path) == 0) {
3198 snprintf(client_unix_sock_path, PATH_MAX,
3199 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
3200 }
3201
3202 /* Set global SHM for ust */
3203 if (strlen(wait_shm_path) == 0) {
3204 snprintf(wait_shm_path, PATH_MAX,
3205 DEFAULT_HOME_APPS_WAIT_SHM_PATH, geteuid());
3206 }
3207 }
3208
3209 DBG("Client socket path %s", client_unix_sock_path);
3210 DBG("Application socket path %s", apps_unix_sock_path);
3211
3212 /*
3213 * See if daemon already exist.
3214 */
3215 if ((ret = check_existing_daemon()) < 0) {
3216 ERR("Already running daemon.\n");
3217 /*
3218 * We do not goto exit because we must not cleanup()
3219 * because a daemon is already running.
3220 */
3221 goto error;
3222 }
3223
3224 /* After this point, we can safely call cleanup() so goto error is used */
3225
3226 /*
3227 * These actions must be executed as root. We do that *after* setting up
3228 * the sockets path because we MUST make the check for another daemon using
3229 * those paths *before* trying to set the kernel consumer sockets and init
3230 * kernel tracer.
3231 */
3232 if (is_root) {
3233 ret = set_kconsumerd_sockets();
3234 if (ret < 0) {
3235 goto exit;
3236 }
3237
3238 /* Setup kernel tracer */
3239 init_kernel_tracer();
3240
3241 /* Set ulimit for open files */
3242 set_ulimit();
3243 }
3244
3245 if ((ret = set_signal_handler()) < 0) {
3246 goto exit;
3247 }
3248
3249 /* Setup the needed unix socket */
3250 if ((ret = init_daemon_socket()) < 0) {
3251 goto exit;
3252 }
3253
3254 /* Set credentials to socket */
3255 if (is_root && ((ret = set_permissions()) < 0)) {
3256 goto exit;
3257 }
3258
3259 /* Get parent pid if -S, --sig-parent is specified. */
3260 if (opt_sig_parent) {
3261 ppid = getppid();
3262 }
3263
3264 /* Setup the kernel pipe for waking up the kernel thread */
3265 if ((ret = create_kernel_poll_pipe()) < 0) {
3266 goto exit;
3267 }
3268
3269 /* Setup the thread apps communication pipe. */
3270 if ((ret = create_apps_cmd_pipe()) < 0) {
3271 goto exit;
3272 }
3273
3274 /* Init UST command queue. */
3275 cds_wfq_init(&ust_cmd_queue.queue);
3276
3277 /*
3278 * Get session list pointer. This pointer MUST NOT be free().
3279 * This list is statically declared in session.c
3280 */
3281 session_list_ptr = get_session_list();
3282
3283 /* Create thread to manage the client socket */
3284 ret = pthread_create(&client_thread, NULL,
3285 thread_manage_clients, (void *) NULL);
3286 if (ret != 0) {
3287 perror("pthread_create clients");
3288 goto exit_client;
3289 }
3290
3291 /* Create thread to dispatch registration */
3292 ret = pthread_create(&dispatch_thread, NULL,
3293 thread_dispatch_ust_registration, (void *) NULL);
3294 if (ret != 0) {
3295 perror("pthread_create dispatch");
3296 goto exit_dispatch;
3297 }
3298
3299 /* Create thread to manage application registration. */
3300 ret = pthread_create(&reg_apps_thread, NULL,
3301 thread_registration_apps, (void *) NULL);
3302 if (ret != 0) {
3303 perror("pthread_create registration");
3304 goto exit_reg_apps;
3305 }
3306
3307 /* Create thread to manage application socket */
3308 ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL);
3309 if (ret != 0) {
3310 perror("pthread_create apps");
3311 goto exit_apps;
3312 }
3313
3314 /* Create kernel thread to manage kernel event */
3315 ret = pthread_create(&kernel_thread, NULL, thread_manage_kernel, (void *) NULL);
3316 if (ret != 0) {
3317 perror("pthread_create kernel");
3318 goto exit_kernel;
3319 }
3320
3321 tracepoint(sessiond_boot_end);
3322
3323 ret = pthread_join(kernel_thread, &status);
3324 if (ret != 0) {
3325 perror("pthread_join");
3326 goto error; /* join error, exit without cleanup */
3327 }
3328
3329 exit_kernel:
3330 ret = pthread_join(apps_thread, &status);
3331 if (ret != 0) {
3332 perror("pthread_join");
3333 goto error; /* join error, exit without cleanup */
3334 }
3335
3336 exit_apps:
3337 ret = pthread_join(reg_apps_thread, &status);
3338 if (ret != 0) {
3339 perror("pthread_join");
3340 goto error; /* join error, exit without cleanup */
3341 }
3342
3343 exit_reg_apps:
3344 ret = pthread_join(dispatch_thread, &status);
3345 if (ret != 0) {
3346 perror("pthread_join");
3347 goto error; /* join error, exit without cleanup */
3348 }
3349
3350 exit_dispatch:
3351 ret = pthread_join(client_thread, &status);
3352 if (ret != 0) {
3353 perror("pthread_join");
3354 goto error; /* join error, exit without cleanup */
3355 }
3356
3357 ret = join_kconsumerd_thread();
3358 if (ret != 0) {
3359 perror("join_kconsumerd");
3360 goto error; /* join error, exit without cleanup */
3361 }
3362
3363 exit_client:
3364 exit:
3365 /*
3366 * cleanup() is called when no other thread is running.
3367 */
3368 cleanup();
3369 if (!ret)
3370 exit(EXIT_SUCCESS);
3371 error:
3372 exit(EXIT_FAILURE);
3373 }
This page took 0.096556 seconds and 4 git commands to generate.