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