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