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