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