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