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