Install lttng/lttng-sessiond-comm.h into system
[lttng-tools.git] / ltt-sessiond / main.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
8 * of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #define _GNU_SOURCE
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <grp.h>
24 #include <limits.h>
25 #include <poll.h>
26 #include <pthread.h>
27 #include <semaphore.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/ipc.h>
33 #include <sys/mount.h>
34 #include <sys/shm.h>
35 #include <sys/socket.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <sys/time.h>
39 #include <sys/resource.h>
40 #include <unistd.h>
41
42 #include <urcu/list.h> /* URCU list library (-lurcu) */
43 #include <lttng/lttng.h>
44 #include <lttng/lttng-sessiond-comm.h>
45
46 #include "context.h"
47 #include "ltt-sessiond.h"
48 #include "lttngerr.h"
49 #include "kernel-ctl.h"
50 #include "ust-ctl.h"
51 #include "session.h"
52 #include "traceable-app.h"
53 #include "lttng-kconsumerd.h"
54 #include "utils.h"
55
56 /* Const values */
57 const char default_home_dir[] = DEFAULT_HOME_DIR;
58 const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP;
59 const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
60 const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
61
62 /* Variables */
63 int opt_verbose; /* Not static for lttngerr.h */
64 int opt_verbose_kconsumerd; /* Not static for lttngerr.h */
65 int opt_quiet; /* Not static for lttngerr.h */
66
67 const char *progname;
68 const char *opt_tracing_group;
69 static int opt_sig_parent;
70 static int opt_daemon;
71 static int is_root; /* Set to 1 if the daemon is running as root */
72 static pid_t ppid; /* Parent PID for --sig-parent option */
73 static pid_t kconsumerd_pid;
74 static struct pollfd *kernel_pollfd;
75
76 static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */
77 static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */
78 static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */
79 static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */
80
81 /* Sockets and FDs */
82 static int client_sock;
83 static int apps_sock;
84 static int kconsumerd_err_sock;
85 static int kconsumerd_cmd_sock;
86 static int kernel_tracer_fd;
87 static int kernel_poll_pipe[2];
88
89 /*
90 * Quit pipe for all threads. This permits a single cancellation point
91 * for all threads when receiving an event on the pipe.
92 */
93 static int thread_quit_pipe[2];
94
95 /* Pthread, Mutexes and Semaphores */
96 static pthread_t kconsumerd_thread;
97 static pthread_t apps_thread;
98 static pthread_t client_thread;
99 static pthread_t kernel_thread;
100 static sem_t kconsumerd_sem;
101
102 static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */
103
104 static int modprobe_remove_kernel_modules(void);
105
106 /*
107 * Pointer initialized before thread creation.
108 *
109 * This points to the tracing session list containing the session count and a
110 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
111 * MUST NOT be taken if you call a public function in session.c.
112 *
113 * The lock is nested inside the structure: session_list_ptr->lock. Please use
114 * lock_session_list and unlock_session_list for lock acquisition.
115 */
116 static struct ltt_session_list *session_list_ptr;
117
118 static gid_t allowed_group(void)
119 {
120 struct group *grp;
121
122 if (opt_tracing_group) {
123 grp = getgrnam(opt_tracing_group);
124 } else {
125 grp = getgrnam(default_tracing_group);
126 }
127 if (!grp) {
128 return -1;
129 } else {
130 return grp->gr_gid;
131 }
132 }
133
134 /*
135 * Init quit pipe.
136 *
137 * Return -1 on error or 0 if all pipes are created.
138 */
139 static int init_thread_quit_pipe(void)
140 {
141 int ret;
142
143 ret = pipe2(thread_quit_pipe, O_CLOEXEC);
144 if (ret < 0) {
145 perror("thread quit pipe");
146 goto error;
147 }
148
149 error:
150 return ret;
151 }
152
153 /*
154 * Complete teardown of a kernel session. This free all data structure related
155 * to a kernel session and update counter.
156 */
157 static void teardown_kernel_session(struct ltt_session *session)
158 {
159 if (session->kernel_session != NULL) {
160 DBG("Tearing down kernel session");
161 trace_destroy_kernel_session(session->kernel_session);
162 /* Extra precaution */
163 session->kernel_session = NULL;
164 }
165 }
166
167 static void stop_threads(void)
168 {
169 /* Stopping all threads */
170 DBG("Terminating all threads");
171 close(thread_quit_pipe[0]);
172 close(thread_quit_pipe[1]);
173 }
174
175 /*
176 * Cleanup the daemon
177 */
178 static void cleanup(void)
179 {
180 int ret;
181 char *cmd;
182 struct ltt_session *sess, *stmp;
183
184 DBG("Cleaning up");
185
186 /* <fun> */
187 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
188 "Matthew, BEET driven development works!%c[%dm",
189 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
190 /* </fun> */
191
192 DBG("Removing %s directory", LTTNG_RUNDIR);
193 ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR);
194 if (ret < 0) {
195 ERR("asprintf failed. Something is really wrong!");
196 }
197
198 /* Remove lttng run directory */
199 ret = system(cmd);
200 if (ret < 0) {
201 ERR("Unable to clean " LTTNG_RUNDIR);
202 }
203
204 DBG("Cleaning up all session");
205
206 /* Destroy session list mutex */
207 if (session_list_ptr != NULL) {
208 pthread_mutex_destroy(&session_list_ptr->lock);
209
210 /* Cleanup ALL session */
211 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
212 teardown_kernel_session(sess);
213 // TODO complete session cleanup (including UST)
214 }
215 }
216
217 pthread_mutex_destroy(&kconsumerd_pid_mutex);
218
219 DBG("Closing kernel fd");
220 close(kernel_tracer_fd);
221
222 DBG("Unloading kernel modules");
223 modprobe_remove_kernel_modules();
224 }
225
226 /*
227 * Send data on a unix socket using the liblttsessiondcomm API.
228 *
229 * Return lttcomm error code.
230 */
231 static int send_unix_sock(int sock, void *buf, size_t len)
232 {
233 /* Check valid length */
234 if (len <= 0) {
235 return -1;
236 }
237
238 return lttcomm_send_unix_sock(sock, buf, len);
239 }
240
241 /*
242 * Free memory of a command context structure.
243 */
244 static void clean_command_ctx(struct command_ctx **cmd_ctx)
245 {
246 DBG("Clean command context structure");
247 if (*cmd_ctx) {
248 if ((*cmd_ctx)->llm) {
249 free((*cmd_ctx)->llm);
250 }
251 if ((*cmd_ctx)->lsm) {
252 free((*cmd_ctx)->lsm);
253 }
254 free(*cmd_ctx);
255 *cmd_ctx = NULL;
256 }
257 }
258
259 /*
260 * Send all stream fds of kernel channel to the consumer.
261 */
262 static int send_kconsumerd_channel_fds(int sock, struct ltt_kernel_channel *channel)
263 {
264 int ret;
265 size_t nb_fd;
266 struct ltt_kernel_stream *stream;
267 struct lttcomm_kconsumerd_header lkh;
268 struct lttcomm_kconsumerd_msg lkm;
269
270 DBG("Sending fds of channel %s to kernel consumer", channel->channel->name);
271
272 nb_fd = channel->stream_count;
273
274 /* Setup header */
275 lkh.payload_size = nb_fd * sizeof(struct lttcomm_kconsumerd_msg);
276 lkh.cmd_type = ADD_STREAM;
277
278 DBG("Sending kconsumerd header");
279
280 ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
281 if (ret < 0) {
282 perror("send kconsumerd header");
283 goto error;
284 }
285
286 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
287 if (stream->fd != 0) {
288 lkm.fd = stream->fd;
289 lkm.state = stream->state;
290 lkm.max_sb_size = channel->channel->attr.subbuf_size;
291 strncpy(lkm.path_name, stream->pathname, PATH_MAX);
292
293 DBG("Sending fd %d to kconsumerd", lkm.fd);
294
295 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
296 if (ret < 0) {
297 perror("send kconsumerd fd");
298 goto error;
299 }
300 }
301 }
302
303 DBG("Kconsumerd channel fds sent");
304
305 return 0;
306
307 error:
308 return ret;
309 }
310
311 /*
312 * Send all stream fds of the kernel session to the consumer.
313 */
314 static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session)
315 {
316 int ret;
317 struct ltt_kernel_channel *chan;
318 struct lttcomm_kconsumerd_header lkh;
319 struct lttcomm_kconsumerd_msg lkm;
320
321 /* Setup header */
322 lkh.payload_size = sizeof(struct lttcomm_kconsumerd_msg);
323 lkh.cmd_type = ADD_STREAM;
324
325 DBG("Sending kconsumerd header for metadata");
326
327 ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
328 if (ret < 0) {
329 perror("send kconsumerd header");
330 goto error;
331 }
332
333 DBG("Sending metadata stream fd");
334
335 if (session->metadata_stream_fd != 0) {
336 /* Send metadata stream fd first */
337 lkm.fd = session->metadata_stream_fd;
338 lkm.state = ACTIVE_FD;
339 lkm.max_sb_size = session->metadata->conf->attr.subbuf_size;
340 strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX);
341
342 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
343 if (ret < 0) {
344 perror("send kconsumerd fd");
345 goto error;
346 }
347 }
348
349 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
350 ret = send_kconsumerd_channel_fds(sock, chan);
351 if (ret < 0) {
352 goto error;
353 }
354 }
355
356 DBG("Kconsumerd fds (metadata and channel streams) sent");
357
358 return 0;
359
360 error:
361 return ret;
362 }
363
364 #ifdef DISABLED
365 /*
366 * Return a socket connected to the libust communication socket of the
367 * application identified by the pid.
368 *
369 * If the pid is not found in the traceable list, return -1 to indicate error.
370 */
371 static int ust_connect_app(pid_t pid)
372 {
373 int sock;
374 struct ltt_traceable_app *lta;
375
376 DBG("Connect to application pid %d", pid);
377
378 lta = find_app_by_pid(pid);
379 if (lta == NULL) {
380 /* App not found */
381 DBG("Application pid %d not found", pid);
382 return -1;
383 }
384
385 sock = ustctl_connect_pid(lta->pid);
386 if (sock < 0) {
387 ERR("Fail connecting to the PID %d", pid);
388 }
389
390 return sock;
391 }
392 #endif /* DISABLED */
393
394 /*
395 * Notify apps by writing 42 to a named pipe using name. Every applications
396 * waiting for a ltt-sessiond will be notified and re-register automatically to
397 * the session daemon.
398 *
399 * Return open or write error value.
400 */
401 static int notify_apps(const char *name)
402 {
403 int fd;
404 int ret = -1;
405
406 DBG("Notify the global application pipe");
407
408 /* Try opening the global pipe */
409 fd = open(name, O_WRONLY);
410 if (fd < 0) {
411 goto error;
412 }
413
414 /* Notify by writing on the pipe */
415 ret = write(fd, "42", 2);
416 if (ret < 0) {
417 perror("write");
418 }
419
420 error:
421 return ret;
422 }
423
424 /*
425 * Setup the outgoing data buffer for the response (llm) by allocating the
426 * right amount of memory and copying the original information from the lsm
427 * structure.
428 *
429 * Return total size of the buffer pointed by buf.
430 */
431 static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
432 {
433 int ret, buf_size;
434
435 buf_size = size;
436
437 cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
438 if (cmd_ctx->llm == NULL) {
439 perror("malloc");
440 ret = -ENOMEM;
441 goto error;
442 }
443
444 /* Copy common data */
445 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
446 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
447
448 cmd_ctx->llm->data_size = size;
449 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
450
451 return buf_size;
452
453 error:
454 return ret;
455 }
456
457 /*
458 * Update the kernel pollfd set of all channel fd available over all tracing
459 * session. Add the wakeup pipe at the end of the set.
460 */
461 static int update_kernel_pollfd(void)
462 {
463 int i = 0;
464 /*
465 * The wakup pipe and the quit pipe are needed so the number of fds starts
466 * at 2 for those pipes.
467 */
468 unsigned int nb_fd = 2;
469 struct ltt_session *session;
470 struct ltt_kernel_channel *channel;
471
472 DBG("Updating kernel_pollfd");
473
474 /* Get the number of channel of all kernel session */
475 lock_session_list();
476 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
477 lock_session(session);
478 if (session->kernel_session == NULL) {
479 unlock_session(session);
480 continue;
481 }
482 nb_fd += session->kernel_session->channel_count;
483 unlock_session(session);
484 }
485
486 DBG("Resizing kernel_pollfd to size %d", nb_fd);
487
488 kernel_pollfd = realloc(kernel_pollfd, nb_fd * sizeof(struct pollfd));
489 if (kernel_pollfd == NULL) {
490 perror("malloc kernel_pollfd");
491 goto error;
492 }
493
494 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
495 lock_session(session);
496 if (session->kernel_session == NULL) {
497 unlock_session(session);
498 continue;
499 }
500 if (i >= nb_fd) {
501 ERR("To much channel for kernel_pollfd size");
502 unlock_session(session);
503 break;
504 }
505 cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
506 kernel_pollfd[i].fd = channel->fd;
507 kernel_pollfd[i].events = POLLIN | POLLRDNORM;
508 i++;
509 }
510 unlock_session(session);
511 }
512 unlock_session_list();
513
514 /* Adding wake up pipe */
515 kernel_pollfd[nb_fd - 2].fd = kernel_poll_pipe[0];
516 kernel_pollfd[nb_fd - 2].events = POLLIN;
517
518 /* Adding the quit pipe */
519 kernel_pollfd[nb_fd - 1].fd = thread_quit_pipe[0];
520
521 return nb_fd;
522
523 error:
524 unlock_session_list();
525 return -1;
526 }
527
528 /*
529 * Find the channel fd from 'fd' over all tracing session. When found, check
530 * for new channel stream and send those stream fds to the kernel consumer.
531 *
532 * Useful for CPU hotplug feature.
533 */
534 static int update_kernel_stream(int fd)
535 {
536 int ret = 0;
537 struct ltt_session *session;
538 struct ltt_kernel_channel *channel;
539
540 DBG("Updating kernel streams for channel fd %d", fd);
541
542 lock_session_list();
543 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
544 lock_session(session);
545 if (session->kernel_session == NULL) {
546 unlock_session(session);
547 continue;
548 }
549 cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
550 if (channel->fd == fd) {
551 DBG("Channel found, updating kernel streams");
552 ret = kernel_open_channel_stream(channel);
553 if (ret < 0) {
554 goto end;
555 }
556 /*
557 * Have we already sent fds to the consumer? If yes, it means that
558 * tracing is started so it is safe to send our updated stream fds.
559 */
560 if (session->kernel_session->kconsumer_fds_sent == 1) {
561 ret = send_kconsumerd_channel_fds(kconsumerd_cmd_sock, channel);
562 if (ret < 0) {
563 goto end;
564 }
565 }
566 goto end;
567 }
568 }
569 unlock_session(session);
570 }
571
572 end:
573 unlock_session_list();
574 if (session) {
575 unlock_session(session);
576 }
577 return ret;
578 }
579
580 /*
581 * This thread manage event coming from the kernel.
582 *
583 * Features supported in this thread:
584 * -) CPU Hotplug
585 */
586 static void *thread_manage_kernel(void *data)
587 {
588 int ret, i, nb_fd = 0;
589 char tmp;
590 int update_poll_flag = 1;
591
592 DBG("Thread manage kernel started");
593
594 while (1) {
595 if (update_poll_flag == 1) {
596 nb_fd = update_kernel_pollfd();
597 if (nb_fd < 0) {
598 goto error;
599 }
600 update_poll_flag = 0;
601 }
602
603 DBG("Polling on %d fds", nb_fd);
604
605 /* Poll infinite value of time */
606 ret = poll(kernel_pollfd, nb_fd, -1);
607 if (ret < 0) {
608 perror("poll kernel thread");
609 goto error;
610 } else if (ret == 0) {
611 /* Should not happen since timeout is infinite */
612 continue;
613 }
614
615 /* Thread quit pipe has been closed. Killing thread. */
616 if (kernel_pollfd[nb_fd - 1].revents == POLLNVAL) {
617 goto error;
618 }
619
620 DBG("Kernel poll event triggered");
621
622 /*
623 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
624 * must be updated.
625 */
626 switch (kernel_pollfd[nb_fd - 2].revents) {
627 case POLLIN:
628 ret = read(kernel_poll_pipe[0], &tmp, 1);
629 update_poll_flag = 1;
630 continue;
631 case POLLERR:
632 goto error;
633 default:
634 break;
635 }
636
637 for (i = 0; i < nb_fd; i++) {
638 switch (kernel_pollfd[i].revents) {
639 /*
640 * New CPU detected by the kernel. Adding kernel stream to kernel
641 * session and updating the kernel consumer
642 */
643 case POLLIN | POLLRDNORM:
644 ret = update_kernel_stream(kernel_pollfd[i].fd);
645 if (ret < 0) {
646 continue;
647 }
648 break;
649 }
650 }
651 }
652
653 error:
654 DBG("Kernel thread dying");
655 if (kernel_pollfd) {
656 free(kernel_pollfd);
657 }
658
659 close(kernel_poll_pipe[0]);
660 close(kernel_poll_pipe[1]);
661 return NULL;
662 }
663
664 /*
665 * This thread manage the kconsumerd error sent back to the session daemon.
666 */
667 static void *thread_manage_kconsumerd(void *data)
668 {
669 int sock = 0, ret;
670 enum lttcomm_return_code code;
671 struct pollfd pollfd[2];
672
673 DBG("[thread] Manage kconsumerd started");
674
675 ret = lttcomm_listen_unix_sock(kconsumerd_err_sock);
676 if (ret < 0) {
677 goto error;
678 }
679
680 /* First fd is always the quit pipe */
681 pollfd[0].fd = thread_quit_pipe[0];
682
683 /* Apps socket */
684 pollfd[1].fd = kconsumerd_err_sock;
685 pollfd[1].events = POLLIN;
686
687 /* Inifinite blocking call, waiting for transmission */
688 ret = poll(pollfd, 2, -1);
689 if (ret < 0) {
690 perror("poll kconsumerd thread");
691 goto error;
692 }
693
694 /* Thread quit pipe has been closed. Killing thread. */
695 if (pollfd[0].revents == POLLNVAL) {
696 goto error;
697 } else if (pollfd[1].revents == POLLERR) {
698 ERR("Kconsumerd err socket poll error");
699 goto error;
700 }
701
702 sock = lttcomm_accept_unix_sock(kconsumerd_err_sock);
703 if (sock < 0) {
704 goto error;
705 }
706
707 /* Getting status code from kconsumerd */
708 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
709 if (ret <= 0) {
710 goto error;
711 }
712
713 if (code == KCONSUMERD_COMMAND_SOCK_READY) {
714 kconsumerd_cmd_sock = lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path);
715 if (kconsumerd_cmd_sock < 0) {
716 sem_post(&kconsumerd_sem);
717 perror("kconsumerd connect");
718 goto error;
719 }
720 /* Signal condition to tell that the kconsumerd is ready */
721 sem_post(&kconsumerd_sem);
722 DBG("Kconsumerd command socket ready");
723 } else {
724 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
725 lttcomm_get_readable_code(-code));
726 goto error;
727 }
728
729 /* Wait for any kconsumerd error */
730 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
731 if (ret <= 0) {
732 ERR("Kconsumerd closed the command socket");
733 goto error;
734 }
735
736 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code));
737
738 error:
739 DBG("Kconsumerd thread dying");
740 if (kconsumerd_err_sock) {
741 close(kconsumerd_err_sock);
742 }
743 if (kconsumerd_cmd_sock) {
744 close(kconsumerd_cmd_sock);
745 }
746 if (sock) {
747 close(sock);
748 }
749
750 unlink(kconsumerd_err_unix_sock_path);
751 unlink(kconsumerd_cmd_unix_sock_path);
752
753 kconsumerd_pid = 0;
754 return NULL;
755 }
756
757 /*
758 * This thread manage the application socket communication
759 */
760 static void *thread_manage_apps(void *data)
761 {
762 int sock = 0, ret;
763 struct pollfd pollfd[2];
764
765 /* TODO: Something more elegant is needed but fine for now */
766 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
767 * for 32-bit vs 64-bit compat processes. */
768 /* replicate in ust with version number */
769 struct {
770 int reg; /* 1:register, 0:unregister */
771 pid_t pid;
772 uid_t uid;
773 } reg_msg;
774
775 DBG("[thread] Manage apps started");
776
777 ret = lttcomm_listen_unix_sock(apps_sock);
778 if (ret < 0) {
779 goto error;
780 }
781
782 /* First fd is always the quit pipe */
783 pollfd[0].fd = thread_quit_pipe[0];
784
785 /* Apps socket */
786 pollfd[1].fd = apps_sock;
787 pollfd[1].events = POLLIN;
788
789 /* Notify all applications to register */
790 notify_apps(default_global_apps_pipe);
791
792 while (1) {
793 DBG("Accepting application registration");
794
795 /* Inifinite blocking call, waiting for transmission */
796 ret = poll(pollfd, 2, -1);
797 if (ret < 0) {
798 perror("poll apps thread");
799 goto error;
800 }
801
802 /* Thread quit pipe has been closed. Killing thread. */
803 if (pollfd[0].revents == POLLNVAL) {
804 goto error;
805 } else if (pollfd[1].revents == POLLERR) {
806 ERR("Apps socket poll error");
807 goto error;
808 }
809
810 sock = lttcomm_accept_unix_sock(apps_sock);
811 if (sock < 0) {
812 goto error;
813 }
814
815 /*
816 * Basic recv here to handle the very simple data
817 * that the libust send to register (reg_msg).
818 */
819 ret = recv(sock, &reg_msg, sizeof(reg_msg), 0);
820 if (ret < 0) {
821 perror("recv");
822 continue;
823 }
824
825 /* Add application to the global traceable list */
826 if (reg_msg.reg == 1) {
827 /* Registering */
828 ret = register_traceable_app(reg_msg.pid, reg_msg.uid);
829 if (ret < 0) {
830 /* register_traceable_app only return an error with
831 * ENOMEM. At this point, we better stop everything.
832 */
833 goto error;
834 }
835 } else {
836 /* Unregistering */
837 unregister_traceable_app(reg_msg.pid);
838 }
839 }
840
841 error:
842 DBG("Apps thread dying");
843 if (apps_sock) {
844 close(apps_sock);
845 }
846 if (sock) {
847 close(sock);
848 }
849
850 unlink(apps_unix_sock_path);
851 return NULL;
852 }
853
854 /*
855 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
856 * exec or it will fails.
857 */
858 static int spawn_kconsumerd_thread(void)
859 {
860 int ret;
861
862 /* Setup semaphore */
863 sem_init(&kconsumerd_sem, 0, 0);
864
865 ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL);
866 if (ret != 0) {
867 perror("pthread_create kconsumerd");
868 goto error;
869 }
870
871 /* Wait for the kconsumerd thread to be ready */
872 sem_wait(&kconsumerd_sem);
873
874 if (kconsumerd_pid == 0) {
875 ERR("Kconsumerd did not start");
876 goto error;
877 }
878
879 return 0;
880
881 error:
882 ret = LTTCOMM_KERN_CONSUMER_FAIL;
883 return ret;
884 }
885
886 static int join_kconsumerd_thread(void)
887 {
888 void *status;
889 int ret;
890
891 if (kconsumerd_pid != 0) {
892 ret = kill(kconsumerd_pid, SIGTERM);
893 if (ret) {
894 ERR("Error killing kconsumerd");
895 return ret;
896 }
897 return pthread_join(kconsumerd_thread, &status);
898 } else {
899 return 0;
900 }
901 }
902
903 /*
904 * Fork and exec a kernel consumer daemon (kconsumerd).
905 *
906 * Return pid if successful else -1.
907 */
908 static pid_t spawn_kconsumerd(void)
909 {
910 int ret;
911 pid_t pid;
912 const char *verbosity;
913
914 DBG("Spawning kconsumerd");
915
916 pid = fork();
917 if (pid == 0) {
918 /*
919 * Exec kconsumerd.
920 */
921 if (opt_verbose > 1 || opt_verbose_kconsumerd) {
922 verbosity = "--verbose";
923 } else {
924 verbosity = "--quiet";
925 }
926 execl(INSTALL_BIN_PATH "/ltt-kconsumerd", "ltt-kconsumerd", verbosity, NULL);
927 if (errno != 0) {
928 perror("kernel start consumer exec");
929 }
930 exit(EXIT_FAILURE);
931 } else if (pid > 0) {
932 ret = pid;
933 goto error;
934 } else {
935 perror("kernel start consumer fork");
936 ret = -errno;
937 goto error;
938 }
939
940 error:
941 return ret;
942 }
943
944 /*
945 * Spawn the kconsumerd daemon and session daemon thread.
946 */
947 static int start_kconsumerd(void)
948 {
949 int ret;
950
951 pthread_mutex_lock(&kconsumerd_pid_mutex);
952 if (kconsumerd_pid != 0) {
953 pthread_mutex_unlock(&kconsumerd_pid_mutex);
954 goto end;
955 }
956
957 ret = spawn_kconsumerd();
958 if (ret < 0) {
959 ERR("Spawning kconsumerd failed");
960 ret = LTTCOMM_KERN_CONSUMER_FAIL;
961 pthread_mutex_unlock(&kconsumerd_pid_mutex);
962 goto error;
963 }
964
965 /* Setting up the global kconsumerd_pid */
966 kconsumerd_pid = ret;
967 pthread_mutex_unlock(&kconsumerd_pid_mutex);
968
969 DBG("Kconsumerd pid %d", ret);
970
971 DBG("Spawning kconsumerd thread");
972 ret = spawn_kconsumerd_thread();
973 if (ret < 0) {
974 ERR("Fatal error spawning kconsumerd thread");
975 goto error;
976 }
977
978 end:
979 return 0;
980
981 error:
982 return ret;
983 }
984
985 /*
986 * modprobe_kernel_modules
987 */
988 static int modprobe_kernel_modules(void)
989 {
990 int ret = 0, i;
991 char modprobe[256];
992
993 for (i = 0; i < ARRAY_SIZE(kernel_modules_list); i++) {
994 ret = snprintf(modprobe, sizeof(modprobe),
995 "/sbin/modprobe %s%s",
996 kernel_modules_list[i].required ? "" : "--quiet ",
997 kernel_modules_list[i].name);
998 if (ret < 0) {
999 perror("snprintf modprobe");
1000 goto error;
1001 }
1002 modprobe[sizeof(modprobe) - 1] = '\0';
1003 ret = system(modprobe);
1004 if (ret == -1) {
1005 ERR("Unable to launch modprobe for module %s",
1006 kernel_modules_list[i].name);
1007 } else if (kernel_modules_list[i].required
1008 && WEXITSTATUS(ret) != 0) {
1009 ERR("Unable to load module %s",
1010 kernel_modules_list[i].name);
1011 } else {
1012 DBG("Modprobe successfully %s",
1013 kernel_modules_list[i].name);
1014 }
1015 }
1016
1017 error:
1018 return ret;
1019 }
1020
1021 /*
1022 * modprobe_remove_kernel_modules
1023 * Remove modules in reverse load order.
1024 */
1025 static int modprobe_remove_kernel_modules(void)
1026 {
1027 int ret = 0, i;
1028 char modprobe[256];
1029
1030 for (i = ARRAY_SIZE(kernel_modules_list) - 1; i >= 0; i--) {
1031 ret = snprintf(modprobe, sizeof(modprobe),
1032 "/sbin/modprobe --remove --quiet %s",
1033 kernel_modules_list[i].name);
1034 if (ret < 0) {
1035 perror("snprintf modprobe --remove");
1036 goto error;
1037 }
1038 modprobe[sizeof(modprobe) - 1] = '\0';
1039 ret = system(modprobe);
1040 if (ret == -1) {
1041 ERR("Unable to launch modprobe --remove for module %s",
1042 kernel_modules_list[i].name);
1043 } else if (kernel_modules_list[i].required
1044 && WEXITSTATUS(ret) != 0) {
1045 ERR("Unable to remove module %s",
1046 kernel_modules_list[i].name);
1047 } else {
1048 DBG("Modprobe removal successful %s",
1049 kernel_modules_list[i].name);
1050 }
1051 }
1052
1053 error:
1054 return ret;
1055 }
1056
1057 /*
1058 * mount_debugfs
1059 */
1060 static int mount_debugfs(char *path)
1061 {
1062 int ret;
1063 char *type = "debugfs";
1064
1065 ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid());
1066 if (ret < 0) {
1067 goto error;
1068 }
1069
1070 ret = mount(type, path, type, 0, NULL);
1071 if (ret < 0) {
1072 perror("mount debugfs");
1073 goto error;
1074 }
1075
1076 DBG("Mounted debugfs successfully at %s", path);
1077
1078 error:
1079 return ret;
1080 }
1081
1082 /*
1083 * Setup necessary data for kernel tracer action.
1084 */
1085 static void init_kernel_tracer(void)
1086 {
1087 int ret;
1088 char *proc_mounts = "/proc/mounts";
1089 char line[256];
1090 char *debugfs_path = NULL, *lttng_path;
1091 FILE *fp;
1092
1093 /* Detect debugfs */
1094 fp = fopen(proc_mounts, "r");
1095 if (fp == NULL) {
1096 ERR("Unable to probe %s", proc_mounts);
1097 goto error;
1098 }
1099
1100 while (fgets(line, sizeof(line), fp) != NULL) {
1101 if (strstr(line, "debugfs") != NULL) {
1102 /* Remove first string */
1103 strtok(line, " ");
1104 /* Dup string here so we can reuse line later on */
1105 debugfs_path = strdup(strtok(NULL, " "));
1106 DBG("Got debugfs path : %s", debugfs_path);
1107 break;
1108 }
1109 }
1110
1111 fclose(fp);
1112
1113 /* Mount debugfs if needded */
1114 if (debugfs_path == NULL) {
1115 ret = asprintf(&debugfs_path, "/mnt/debugfs");
1116 if (ret < 0) {
1117 perror("asprintf debugfs path");
1118 goto error;
1119 }
1120 ret = mount_debugfs(debugfs_path);
1121 if (ret < 0) {
1122 goto error;
1123 }
1124 }
1125
1126 /* Modprobe lttng kernel modules */
1127 ret = modprobe_kernel_modules();
1128 if (ret < 0) {
1129 goto error;
1130 }
1131
1132 /* Setup lttng kernel path */
1133 ret = asprintf(&lttng_path, "%s/lttng", debugfs_path);
1134 if (ret < 0) {
1135 perror("asprintf lttng path");
1136 goto error;
1137 }
1138
1139 /* Open debugfs lttng */
1140 kernel_tracer_fd = open(lttng_path, O_RDWR);
1141 if (kernel_tracer_fd < 0) {
1142 DBG("Failed to open %s", lttng_path);
1143 goto error;
1144 }
1145
1146 free(lttng_path);
1147 free(debugfs_path);
1148 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1149 return;
1150
1151 error:
1152 if (lttng_path) {
1153 free(lttng_path);
1154 }
1155 if (debugfs_path) {
1156 free(debugfs_path);
1157 }
1158 WARN("No kernel tracer available");
1159 kernel_tracer_fd = 0;
1160 return;
1161 }
1162
1163 /*
1164 * Start tracing by creating trace directory and sending FDs to the kernel
1165 * consumer.
1166 */
1167 static int start_kernel_trace(struct ltt_kernel_session *session)
1168 {
1169 int ret = 0;
1170
1171 if (session->kconsumer_fds_sent == 0) {
1172 ret = send_kconsumerd_fds(kconsumerd_cmd_sock, session);
1173 if (ret < 0) {
1174 ERR("Send kconsumerd fds failed");
1175 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1176 goto error;
1177 }
1178
1179 session->kconsumer_fds_sent = 1;
1180 }
1181
1182 error:
1183 return ret;
1184 }
1185
1186 /*
1187 * Notify kernel thread to update it's pollfd.
1188 */
1189 static int notify_kernel_pollfd(void)
1190 {
1191 int ret;
1192
1193 /* Inform kernel thread of the new kernel channel */
1194 ret = write(kernel_poll_pipe[1], "!", 1);
1195 if (ret < 0) {
1196 perror("write kernel poll pipe");
1197 }
1198
1199 return ret;
1200 }
1201
1202 /*
1203 * Allocate a channel structure and fill it.
1204 */
1205 static struct lttng_channel *init_default_channel(char *name)
1206 {
1207 struct lttng_channel *chan;
1208
1209 chan = malloc(sizeof(struct lttng_channel));
1210 if (chan == NULL) {
1211 perror("init channel malloc");
1212 goto error;
1213 }
1214
1215 if (snprintf(chan->name, NAME_MAX, "%s", name) < 0) {
1216 perror("snprintf channel name");
1217 return NULL;
1218 }
1219
1220 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
1221 chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
1222 chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
1223 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1224 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1225 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1226
1227 error:
1228 return chan;
1229 }
1230
1231 /*
1232 * Create a kernel tracer session then create the default channel.
1233 */
1234 static int create_kernel_session(struct ltt_session *session)
1235 {
1236 int ret;
1237
1238 DBG("Creating kernel session");
1239
1240 ret = kernel_create_session(session, kernel_tracer_fd);
1241 if (ret < 0) {
1242 ret = LTTCOMM_KERN_SESS_FAIL;
1243 goto error;
1244 }
1245
1246 ret = mkdir_recursive(session->path, S_IRWXU | S_IRWXG,
1247 geteuid(), allowed_group());
1248 if (ret < 0) {
1249 if (ret != -EEXIST) {
1250 ERR("Trace directory creation error");
1251 goto error;
1252 }
1253 }
1254
1255 error:
1256 return ret;
1257 }
1258
1259 /*
1260 * Using the session list, filled a lttng_session array to send back to the
1261 * client for session listing.
1262 *
1263 * The session list lock MUST be acquired before calling this function. Use
1264 * lock_session_list() and unlock_session_list().
1265 */
1266 static void list_lttng_sessions(struct lttng_session *sessions)
1267 {
1268 int i = 0;
1269 struct ltt_session *session;
1270
1271 DBG("Getting all available session");
1272 /*
1273 * Iterate over session list and append data after the control struct in
1274 * the buffer.
1275 */
1276 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
1277 strncpy(sessions[i].path, session->path, PATH_MAX);
1278 strncpy(sessions[i].name, session->name, NAME_MAX);
1279 i++;
1280 }
1281 }
1282
1283 /*
1284 * Fill lttng_channel array of all channels.
1285 */
1286 static void list_lttng_channels(struct ltt_session *session,
1287 struct lttng_channel *channels)
1288 {
1289 int i = 0;
1290 struct ltt_kernel_channel *kchan;
1291
1292 DBG("Listing channels for session %s", session->name);
1293
1294 /* Kernel channels */
1295 if (session->kernel_session != NULL) {
1296 cds_list_for_each_entry(kchan, &session->kernel_session->channel_list.head, list) {
1297 /* Copy lttng_channel struct to array */
1298 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
1299 channels[i].enabled = kchan->enabled;
1300 i++;
1301 }
1302 }
1303
1304 /* TODO: Missing UST listing */
1305 }
1306
1307 /*
1308 * Fill lttng_event array of all events in the channel.
1309 */
1310 static void list_lttng_events(struct ltt_kernel_channel *kchan,
1311 struct lttng_event *events)
1312 {
1313 /*
1314 * TODO: This is ONLY kernel. Need UST support.
1315 */
1316 int i = 0;
1317 struct ltt_kernel_event *event;
1318
1319 DBG("Listing events for channel %s", kchan->channel->name);
1320
1321 /* Kernel channels */
1322 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
1323 strncpy(events[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
1324 events[i].enabled = event->enabled;
1325 switch (event->event->instrumentation) {
1326 case LTTNG_KERNEL_TRACEPOINT:
1327 events[i].type = LTTNG_EVENT_TRACEPOINT;
1328 break;
1329 case LTTNG_KERNEL_KPROBE:
1330 case LTTNG_KERNEL_KRETPROBE:
1331 events[i].type = LTTNG_EVENT_PROBE;
1332 memcpy(&events[i].attr.probe, &event->event->u.kprobe,
1333 sizeof(struct lttng_kernel_kprobe));
1334 break;
1335 case LTTNG_KERNEL_FUNCTION:
1336 events[i].type = LTTNG_EVENT_FUNCTION;
1337 memcpy(&events[i].attr.ftrace, &event->event->u.ftrace,
1338 sizeof(struct lttng_kernel_function));
1339 break;
1340 }
1341 i++;
1342 }
1343 }
1344
1345 /*
1346 * Process the command requested by the lttng client within the command
1347 * context structure. This function make sure that the return structure (llm)
1348 * is set and ready for transmission before returning.
1349 *
1350 * Return any error encountered or 0 for success.
1351 */
1352 static int process_client_msg(struct command_ctx *cmd_ctx)
1353 {
1354 int ret = LTTCOMM_OK;
1355
1356 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
1357
1358 /* Listing commands don't need a session */
1359 switch (cmd_ctx->lsm->cmd_type) {
1360 case LTTNG_CREATE_SESSION:
1361 case LTTNG_LIST_SESSIONS:
1362 case LTTNG_LIST_TRACEPOINTS:
1363 case LTTNG_CALIBRATE:
1364 break;
1365 default:
1366 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
1367 cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session.name);
1368 if (cmd_ctx->session == NULL) {
1369 /* If session name not found */
1370 if (cmd_ctx->lsm->session.name != NULL) {
1371 ret = LTTCOMM_SESS_NOT_FOUND;
1372 } else { /* If no session name specified */
1373 ret = LTTCOMM_SELECT_SESS;
1374 }
1375 goto error;
1376 } else {
1377 /* Acquire lock for the session */
1378 lock_session(cmd_ctx->session);
1379 }
1380 break;
1381 }
1382
1383 /*
1384 * Check domain type for specific "pre-action".
1385 */
1386 switch (cmd_ctx->lsm->domain.type) {
1387 case LTTNG_DOMAIN_KERNEL:
1388 /* Kernel tracer check */
1389 if (kernel_tracer_fd == 0) {
1390 init_kernel_tracer();
1391 if (kernel_tracer_fd == 0) {
1392 ret = LTTCOMM_KERN_NA;
1393 goto error;
1394 }
1395 }
1396
1397 /* Need a session for kernel command */
1398 switch (cmd_ctx->lsm->cmd_type) {
1399 case LTTNG_CREATE_SESSION:
1400 case LTTNG_LIST_SESSIONS:
1401 case LTTNG_LIST_TRACEPOINTS:
1402 case LTTNG_CALIBRATE:
1403 break;
1404 default:
1405 if (cmd_ctx->session->kernel_session == NULL) {
1406 ret = create_kernel_session(cmd_ctx->session);
1407 if (ret < 0) {
1408 ret = LTTCOMM_KERN_SESS_FAIL;
1409 goto error;
1410 }
1411
1412 /* Start the kernel consumer daemon */
1413 if (kconsumerd_pid == 0) {
1414 ret = start_kconsumerd();
1415 if (ret < 0) {
1416 goto error;
1417 }
1418 }
1419 }
1420 }
1421 break;
1422 default:
1423 break;
1424 }
1425
1426 /* Process by command type */
1427 switch (cmd_ctx->lsm->cmd_type) {
1428 case LTTNG_ADD_CONTEXT:
1429 {
1430 struct lttng_kernel_context kctx;
1431
1432 /* Setup lttng message with no payload */
1433 ret = setup_lttng_msg(cmd_ctx, 0);
1434 if (ret < 0) {
1435 goto setup_error;
1436 }
1437
1438 switch (cmd_ctx->lsm->domain.type) {
1439 case LTTNG_DOMAIN_KERNEL:
1440 /* Create Kernel context */
1441 kctx.ctx = cmd_ctx->lsm->u.context.ctx.ctx;
1442 kctx.u.perf_counter.type = cmd_ctx->lsm->u.context.ctx.u.perf_counter.type;
1443 kctx.u.perf_counter.config = cmd_ctx->lsm->u.context.ctx.u.perf_counter.config;
1444 strncpy(kctx.u.perf_counter.name,
1445 cmd_ctx->lsm->u.context.ctx.u.perf_counter.name,
1446 LTTNG_SYMBOL_NAME_LEN);
1447
1448 /* Add kernel context to kernel tracer. See context.c */
1449 ret = add_kernel_context(cmd_ctx->session->kernel_session, &kctx,
1450 cmd_ctx->lsm->u.context.event_name,
1451 cmd_ctx->lsm->u.context.channel_name);
1452 if (ret != LTTCOMM_OK) {
1453 goto error;
1454 }
1455 break;
1456 default:
1457 /* TODO: Userspace tracing */
1458 ret = LTTCOMM_NOT_IMPLEMENTED;
1459 goto error;
1460 }
1461
1462 ret = LTTCOMM_OK;
1463 break;
1464 }
1465 case LTTNG_DISABLE_CHANNEL:
1466 {
1467 struct ltt_kernel_channel *kchan;
1468
1469 /* Setup lttng message with no payload */
1470 ret = setup_lttng_msg(cmd_ctx, 0);
1471 if (ret < 0) {
1472 goto setup_error;
1473 }
1474
1475 switch (cmd_ctx->lsm->domain.type) {
1476 case LTTNG_DOMAIN_KERNEL:
1477 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1478 cmd_ctx->session->kernel_session);
1479 if (kchan == NULL) {
1480 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1481 goto error;
1482 } else if (kchan->enabled == 1) {
1483 ret = kernel_disable_channel(kchan);
1484 if (ret < 0) {
1485 if (ret != EEXIST) {
1486 ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL;
1487 }
1488 goto error;
1489 }
1490 }
1491 kernel_wait_quiescent(kernel_tracer_fd);
1492 break;
1493 default:
1494 /* TODO: Userspace tracing */
1495 ret = LTTCOMM_NOT_IMPLEMENTED;
1496 goto error;
1497 }
1498
1499 ret = LTTCOMM_OK;
1500 break;
1501 }
1502 case LTTNG_DISABLE_EVENT:
1503 {
1504 struct ltt_kernel_channel *kchan;
1505 struct ltt_kernel_event *kevent;
1506
1507 /* Setup lttng message with no payload */
1508 ret = setup_lttng_msg(cmd_ctx, 0);
1509 if (ret < 0) {
1510 goto setup_error;
1511 }
1512
1513 switch (cmd_ctx->lsm->domain.type) {
1514 case LTTNG_DOMAIN_KERNEL:
1515 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1516 cmd_ctx->session->kernel_session);
1517 if (kchan == NULL) {
1518 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1519 goto error;
1520 }
1521
1522 kevent = get_kernel_event_by_name(cmd_ctx->lsm->u.disable.name, kchan);
1523 if (kevent != NULL) {
1524 DBG("Disabling kernel event %s for channel %s.", kevent->event->name,
1525 kchan->channel->name);
1526 ret = kernel_disable_event(kevent);
1527 if (ret < 0) {
1528 ret = LTTCOMM_KERN_ENABLE_FAIL;
1529 goto error;
1530 }
1531 }
1532
1533 kernel_wait_quiescent(kernel_tracer_fd);
1534 break;
1535 default:
1536 /* TODO: Userspace tracing */
1537 ret = LTTCOMM_NOT_IMPLEMENTED;
1538 goto error;
1539 }
1540
1541 ret = LTTCOMM_OK;
1542 break;
1543 }
1544 case LTTNG_DISABLE_ALL_EVENT:
1545 {
1546 struct ltt_kernel_channel *kchan;
1547 struct ltt_kernel_event *kevent;
1548
1549 /* Setup lttng message with no payload */
1550 ret = setup_lttng_msg(cmd_ctx, 0);
1551 if (ret < 0) {
1552 goto setup_error;
1553 }
1554
1555 switch (cmd_ctx->lsm->domain.type) {
1556 case LTTNG_DOMAIN_KERNEL:
1557 DBG("Disabling all enabled kernel events");
1558 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1559 cmd_ctx->session->kernel_session);
1560 if (kchan == NULL) {
1561 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1562 goto error;
1563 }
1564
1565 /* For each event in the kernel session */
1566 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
1567 DBG("Disabling kernel event %s for channel %s.",
1568 kevent->event->name, kchan->channel->name);
1569 ret = kernel_disable_event(kevent);
1570 if (ret < 0) {
1571 continue;
1572 }
1573 }
1574
1575 /* Quiescent wait after event disable */
1576 kernel_wait_quiescent(kernel_tracer_fd);
1577 break;
1578 default:
1579 /* TODO: Userspace tracing */
1580 ret = LTTCOMM_NOT_IMPLEMENTED;
1581 goto error;
1582 }
1583
1584 ret = LTTCOMM_OK;
1585 break;
1586 }
1587 case LTTNG_ENABLE_CHANNEL:
1588 {
1589 struct ltt_kernel_channel *kchan;
1590
1591 /* Setup lttng message with no payload */
1592 ret = setup_lttng_msg(cmd_ctx, 0);
1593 if (ret < 0) {
1594 goto setup_error;
1595 }
1596
1597 switch (cmd_ctx->lsm->domain.type) {
1598 case LTTNG_DOMAIN_KERNEL:
1599 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1600 cmd_ctx->session->kernel_session);
1601 if (kchan == NULL) {
1602 /* Channel not found, creating it */
1603 DBG("Creating kernel channel");
1604
1605 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
1606 &cmd_ctx->lsm->u.channel.chan, cmd_ctx->session->path);
1607 if (ret < 0) {
1608 ret = LTTCOMM_KERN_CHAN_FAIL;
1609 goto error;
1610 }
1611
1612 /* Notify kernel thread that there is a new channel */
1613 ret = notify_kernel_pollfd();
1614 if (ret < 0) {
1615 ret = LTTCOMM_FATAL;
1616 goto error;
1617 }
1618 } else if (kchan->enabled == 0) {
1619 ret = kernel_enable_channel(kchan);
1620 if (ret < 0) {
1621 if (ret != EEXIST) {
1622 ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL;
1623 }
1624 goto error;
1625 }
1626 }
1627
1628 kernel_wait_quiescent(kernel_tracer_fd);
1629 break;
1630 default:
1631 /* TODO: Userspace tracing */
1632 ret = LTTCOMM_NOT_IMPLEMENTED;
1633 goto error;
1634 }
1635
1636 ret = LTTCOMM_OK;
1637 break;
1638 }
1639 case LTTNG_ENABLE_EVENT:
1640 {
1641 char *channel_name;
1642 struct ltt_kernel_channel *kchan;
1643 struct ltt_kernel_event *kevent;
1644 struct lttng_channel *chan;
1645
1646 /* Setup lttng message with no payload */
1647 ret = setup_lttng_msg(cmd_ctx, 0);
1648 if (ret < 0) {
1649 goto setup_error;
1650 }
1651
1652 channel_name = cmd_ctx->lsm->u.enable.channel_name;
1653
1654 switch (cmd_ctx->lsm->domain.type) {
1655 case LTTNG_DOMAIN_KERNEL:
1656 do {
1657 kchan = get_kernel_channel_by_name(channel_name,
1658 cmd_ctx->session->kernel_session);
1659 if (kchan == NULL) {
1660 DBG("Channel not found. Creating channel %s", channel_name);
1661
1662 chan = init_default_channel(channel_name);
1663 if (chan == NULL) {
1664 ret = LTTCOMM_FATAL;
1665 goto error;
1666 }
1667
1668 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
1669 chan, cmd_ctx->session->path);
1670 if (ret < 0) {
1671 ret = LTTCOMM_KERN_CHAN_FAIL;
1672 goto error;
1673 }
1674 }
1675 } while (kchan == NULL);
1676
1677 kevent = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, kchan);
1678 if (kevent == NULL) {
1679 DBG("Creating kernel event %s for channel %s.",
1680 cmd_ctx->lsm->u.enable.event.name, channel_name);
1681 ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, kchan);
1682 } else {
1683 DBG("Enabling kernel event %s for channel %s.",
1684 kevent->event->name, channel_name);
1685 ret = kernel_enable_event(kevent);
1686 if (ret == -EEXIST) {
1687 ret = LTTCOMM_KERN_EVENT_EXIST;
1688 goto error;
1689 }
1690 }
1691
1692 if (ret < 0) {
1693 ret = LTTCOMM_KERN_ENABLE_FAIL;
1694 goto error;
1695 }
1696
1697 kernel_wait_quiescent(kernel_tracer_fd);
1698 break;
1699 default:
1700 /* TODO: Userspace tracing */
1701 ret = LTTCOMM_NOT_IMPLEMENTED;
1702 goto error;
1703 }
1704 ret = LTTCOMM_OK;
1705 break;
1706 }
1707 case LTTNG_ENABLE_ALL_EVENT:
1708 {
1709 int size, i;
1710 char *channel_name;
1711 struct ltt_kernel_channel *kchan;
1712 struct ltt_kernel_event *kevent;
1713 struct lttng_event *event_list;
1714 struct lttng_channel *chan;
1715
1716 /* Setup lttng message with no payload */
1717 ret = setup_lttng_msg(cmd_ctx, 0);
1718 if (ret < 0) {
1719 goto setup_error;
1720 }
1721
1722 DBG("Enabling all kernel event");
1723
1724 channel_name = cmd_ctx->lsm->u.enable.channel_name;
1725
1726 switch (cmd_ctx->lsm->domain.type) {
1727 case LTTNG_DOMAIN_KERNEL:
1728 do {
1729 kchan = get_kernel_channel_by_name(channel_name,
1730 cmd_ctx->session->kernel_session);
1731 if (kchan == NULL) {
1732 DBG("Channel not found. Creating channel %s", channel_name);
1733
1734 chan = init_default_channel(channel_name);
1735 if (chan == NULL) {
1736 ret = LTTCOMM_FATAL;
1737 goto error;
1738 }
1739
1740 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
1741 chan, cmd_ctx->session->path);
1742 if (ret < 0) {
1743 ret = LTTCOMM_KERN_CHAN_FAIL;
1744 goto error;
1745 }
1746 }
1747 } while (kchan == NULL);
1748
1749 /* For each event in the kernel session */
1750 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
1751 DBG("Enabling kernel event %s for channel %s.",
1752 kevent->event->name, channel_name);
1753 ret = kernel_enable_event(kevent);
1754 if (ret < 0) {
1755 continue;
1756 }
1757 }
1758
1759 size = kernel_list_events(kernel_tracer_fd, &event_list);
1760 if (size < 0) {
1761 ret = LTTCOMM_KERN_LIST_FAIL;
1762 goto error;
1763 }
1764
1765 for (i = 0; i < size; i++) {
1766 kevent = get_kernel_event_by_name(event_list[i].name, kchan);
1767 if (kevent == NULL) {
1768 /* Default event type for enable all */
1769 event_list[i].type = LTTNG_EVENT_TRACEPOINT;
1770 /* Enable each single tracepoint event */
1771 ret = kernel_create_event(&event_list[i], kchan);
1772 if (ret < 0) {
1773 /* Ignore error here and continue */
1774 }
1775 }
1776 }
1777
1778 free(event_list);
1779
1780 /* Quiescent wait after event enable */
1781 kernel_wait_quiescent(kernel_tracer_fd);
1782 break;
1783 default:
1784 /* TODO: Userspace tracing */
1785 ret = LTTCOMM_NOT_IMPLEMENTED;
1786 goto error;
1787 }
1788
1789 ret = LTTCOMM_OK;
1790 break;
1791 }
1792 case LTTNG_LIST_TRACEPOINTS:
1793 {
1794 struct lttng_event *events;
1795 ssize_t nb_events = 0;
1796
1797 switch (cmd_ctx->lsm->domain.type) {
1798 case LTTNG_DOMAIN_KERNEL:
1799 DBG("Listing kernel events");
1800 nb_events = kernel_list_events(kernel_tracer_fd, &events);
1801 if (nb_events < 0) {
1802 ret = LTTCOMM_KERN_LIST_FAIL;
1803 goto error;
1804 }
1805 break;
1806 default:
1807 /* TODO: Userspace listing */
1808 ret = LTTCOMM_NOT_IMPLEMENTED;
1809 break;
1810 }
1811
1812 /*
1813 * Setup lttng message with payload size set to the event list size in
1814 * bytes and then copy list into the llm payload.
1815 */
1816 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
1817 if (ret < 0) {
1818 free(events);
1819 goto setup_error;
1820 }
1821
1822 /* Copy event list into message payload */
1823 memcpy(cmd_ctx->llm->payload, events,
1824 sizeof(struct lttng_event) * nb_events);
1825
1826 free(events);
1827
1828 ret = LTTCOMM_OK;
1829 break;
1830 }
1831 case LTTNG_START_TRACE:
1832 {
1833 struct ltt_kernel_channel *chan;
1834
1835 /* Setup lttng message with no payload */
1836 ret = setup_lttng_msg(cmd_ctx, 0);
1837 if (ret < 0) {
1838 goto setup_error;
1839 }
1840
1841 /* Kernel tracing */
1842 if (cmd_ctx->session->kernel_session != NULL) {
1843 if (cmd_ctx->session->kernel_session->metadata == NULL) {
1844 DBG("Open kernel metadata");
1845 ret = kernel_open_metadata(cmd_ctx->session->kernel_session,
1846 cmd_ctx->session->path);
1847 if (ret < 0) {
1848 ret = LTTCOMM_KERN_META_FAIL;
1849 goto error;
1850 }
1851 }
1852
1853 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1854 DBG("Opening kernel metadata stream");
1855 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1856 ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session);
1857 if (ret < 0) {
1858 ERR("Kernel create metadata stream failed");
1859 ret = LTTCOMM_KERN_STREAM_FAIL;
1860 goto error;
1861 }
1862 }
1863 }
1864
1865 /* For each channel */
1866 cds_list_for_each_entry(chan,
1867 &cmd_ctx->session->kernel_session->channel_list.head, list) {
1868 if (chan->stream_count == 0) {
1869 ret = kernel_open_channel_stream(chan);
1870 if (ret < 0) {
1871 ERR("Kernel create channel stream failed");
1872 ret = LTTCOMM_KERN_STREAM_FAIL;
1873 goto error;
1874 }
1875 /* Update the stream global counter */
1876 cmd_ctx->session->kernel_session->stream_count_global += ret;
1877 }
1878 }
1879
1880 DBG("Start kernel tracing");
1881 ret = kernel_start_session(cmd_ctx->session->kernel_session);
1882 if (ret < 0) {
1883 ERR("Kernel start session failed");
1884 ret = LTTCOMM_KERN_START_FAIL;
1885 goto error;
1886 }
1887
1888 ret = start_kernel_trace(cmd_ctx->session->kernel_session);
1889 if (ret < 0) {
1890 ret = LTTCOMM_KERN_START_FAIL;
1891 goto error;
1892 }
1893
1894 /* Quiescent wait after starting trace */
1895 kernel_wait_quiescent(kernel_tracer_fd);
1896 }
1897
1898 /* TODO: Start all UST traces */
1899
1900 ret = LTTCOMM_OK;
1901 break;
1902 }
1903 case LTTNG_STOP_TRACE:
1904 {
1905 struct ltt_kernel_channel *chan;
1906 /* Setup lttng message with no payload */
1907 ret = setup_lttng_msg(cmd_ctx, 0);
1908 if (ret < 0) {
1909 goto setup_error;
1910 }
1911
1912 /* Kernel tracer */
1913 if (cmd_ctx->session->kernel_session != NULL) {
1914 DBG("Stop kernel tracing");
1915
1916 ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd);
1917 if (ret < 0) {
1918 ERR("Kernel metadata flush failed");
1919 }
1920
1921 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
1922 ret = kernel_flush_buffer(chan);
1923 if (ret < 0) {
1924 ERR("Kernel flush buffer error");
1925 }
1926 }
1927
1928 ret = kernel_stop_session(cmd_ctx->session->kernel_session);
1929 if (ret < 0) {
1930 ERR("Kernel stop session failed");
1931 ret = LTTCOMM_KERN_STOP_FAIL;
1932 goto error;
1933 }
1934
1935 /* Quiescent wait after stopping trace */
1936 kernel_wait_quiescent(kernel_tracer_fd);
1937 }
1938
1939 /* TODO : User-space tracer */
1940
1941 ret = LTTCOMM_OK;
1942 break;
1943 }
1944 case LTTNG_CREATE_SESSION:
1945 {
1946 /* Setup lttng message with no payload */
1947 ret = setup_lttng_msg(cmd_ctx, 0);
1948 if (ret < 0) {
1949 goto setup_error;
1950 }
1951
1952 ret = create_session(cmd_ctx->lsm->session.name, cmd_ctx->lsm->session.path);
1953 if (ret < 0) {
1954 if (ret == -EEXIST) {
1955 ret = LTTCOMM_EXIST_SESS;
1956 } else {
1957 ret = LTTCOMM_FATAL;
1958 }
1959 goto error;
1960 }
1961
1962 ret = LTTCOMM_OK;
1963 break;
1964 }
1965 case LTTNG_DESTROY_SESSION:
1966 {
1967 /* Setup lttng message with no payload */
1968 ret = setup_lttng_msg(cmd_ctx, 0);
1969 if (ret < 0) {
1970 goto setup_error;
1971 }
1972
1973 /* Clean kernel session teardown */
1974 teardown_kernel_session(cmd_ctx->session);
1975
1976 ret = destroy_session(cmd_ctx->lsm->session.name);
1977 if (ret < 0) {
1978 ret = LTTCOMM_FATAL;
1979 goto error;
1980 }
1981
1982 /*
1983 * Must notify the kernel thread here to update it's pollfd in order to
1984 * remove the channel(s)' fd just destroyed.
1985 */
1986 ret = notify_kernel_pollfd();
1987 if (ret < 0) {
1988 ret = LTTCOMM_FATAL;
1989 goto error;
1990 }
1991
1992 ret = LTTCOMM_OK;
1993 break;
1994 }
1995 case LTTNG_LIST_DOMAINS:
1996 {
1997 size_t nb_dom = 0;
1998
1999 if (cmd_ctx->session->kernel_session != NULL) {
2000 nb_dom++;
2001 }
2002
2003 nb_dom += cmd_ctx->session->ust_trace_count;
2004
2005 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_domain) * nb_dom);
2006 if (ret < 0) {
2007 goto setup_error;
2008 }
2009
2010 ((struct lttng_domain *)(cmd_ctx->llm->payload))[0].type =
2011 LTTNG_DOMAIN_KERNEL;
2012
2013 /* TODO: User-space tracer domain support */
2014 ret = LTTCOMM_OK;
2015 break;
2016 }
2017 case LTTNG_LIST_CHANNELS:
2018 {
2019 /*
2020 * TODO: Only kernel channels are listed here. UST listing
2021 * is needed on lttng-ust 2.0 release.
2022 */
2023 size_t nb_chan = 0;
2024 if (cmd_ctx->session->kernel_session != NULL) {
2025 nb_chan += cmd_ctx->session->kernel_session->channel_count;
2026 }
2027
2028 ret = setup_lttng_msg(cmd_ctx,
2029 sizeof(struct lttng_channel) * nb_chan);
2030 if (ret < 0) {
2031 goto setup_error;
2032 }
2033
2034 list_lttng_channels(cmd_ctx->session,
2035 (struct lttng_channel *)(cmd_ctx->llm->payload));
2036
2037 ret = LTTCOMM_OK;
2038 break;
2039 }
2040 case LTTNG_LIST_EVENTS:
2041 {
2042 /*
2043 * TODO: Only kernel events are listed here. UST listing
2044 * is needed on lttng-ust 2.0 release.
2045 */
2046 size_t nb_event = 0;
2047 struct ltt_kernel_channel *kchan = NULL;
2048
2049 if (cmd_ctx->session->kernel_session != NULL) {
2050 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.list.channel_name,
2051 cmd_ctx->session->kernel_session);
2052 if (kchan == NULL) {
2053 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2054 goto error;
2055 }
2056 nb_event += kchan->event_count;
2057 }
2058
2059 ret = setup_lttng_msg(cmd_ctx,
2060 sizeof(struct lttng_event) * nb_event);
2061 if (ret < 0) {
2062 goto setup_error;
2063 }
2064
2065 DBG("Listing events (%zu events)", nb_event);
2066
2067 list_lttng_events(kchan,
2068 (struct lttng_event *)(cmd_ctx->llm->payload));
2069
2070 ret = LTTCOMM_OK;
2071 break;
2072 }
2073 case LTTNG_LIST_SESSIONS:
2074 {
2075 lock_session_list();
2076
2077 if (session_list_ptr->count == 0) {
2078 ret = LTTCOMM_NO_SESSION;
2079 unlock_session_list();
2080 goto error;
2081 }
2082
2083 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) *
2084 session_list_ptr->count);
2085 if (ret < 0) {
2086 unlock_session_list();
2087 goto setup_error;
2088 }
2089
2090 /* Filled the session array */
2091 list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload));
2092
2093 unlock_session_list();
2094
2095 ret = LTTCOMM_OK;
2096 break;
2097 }
2098
2099 case LTTNG_CALIBRATE:
2100 {
2101 /* Setup lttng message with no payload */
2102 ret = setup_lttng_msg(cmd_ctx, 0);
2103 if (ret < 0) {
2104 goto setup_error;
2105 }
2106
2107 switch (cmd_ctx->lsm->domain.type) {
2108 case LTTNG_DOMAIN_KERNEL:
2109 {
2110 struct lttng_kernel_calibrate kcalibrate;
2111
2112 kcalibrate.type = cmd_ctx->lsm->u.calibrate.type;
2113 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2114 if (ret < 0) {
2115 ret = LTTCOMM_KERN_ENABLE_FAIL;
2116 goto error;
2117 }
2118 break;
2119 }
2120 default:
2121 /* TODO: Userspace tracing */
2122 ret = LTTCOMM_NOT_IMPLEMENTED;
2123 goto error;
2124 }
2125 ret = LTTCOMM_OK;
2126 break;
2127 }
2128
2129 default:
2130 /* Undefined command */
2131 ret = setup_lttng_msg(cmd_ctx, 0);
2132 if (ret < 0) {
2133 goto setup_error;
2134 }
2135
2136 ret = LTTCOMM_UND;
2137 break;
2138 }
2139
2140 /* Set return code */
2141 cmd_ctx->llm->ret_code = ret;
2142
2143 if (cmd_ctx->session) {
2144 unlock_session(cmd_ctx->session);
2145 }
2146
2147 return ret;
2148
2149 error:
2150 if (cmd_ctx->llm == NULL) {
2151 DBG("Missing llm structure. Allocating one.");
2152 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
2153 goto setup_error;
2154 }
2155 }
2156 /* Notify client of error */
2157 cmd_ctx->llm->ret_code = ret;
2158
2159 setup_error:
2160 if (cmd_ctx->session) {
2161 unlock_session(cmd_ctx->session);
2162 }
2163 return ret;
2164 }
2165
2166 /*
2167 * This thread manage all clients request using the unix client socket for
2168 * communication.
2169 */
2170 static void *thread_manage_clients(void *data)
2171 {
2172 int sock = 0, ret;
2173 struct command_ctx *cmd_ctx = NULL;
2174 struct pollfd pollfd[2];
2175
2176 DBG("[thread] Manage client started");
2177
2178 ret = lttcomm_listen_unix_sock(client_sock);
2179 if (ret < 0) {
2180 goto error;
2181 }
2182
2183 /* First fd is always the quit pipe */
2184 pollfd[0].fd = thread_quit_pipe[0];
2185
2186 /* Apps socket */
2187 pollfd[1].fd = client_sock;
2188 pollfd[1].events = POLLIN;
2189
2190 /* Notify parent pid that we are ready
2191 * to accept command for client side.
2192 */
2193 if (opt_sig_parent) {
2194 kill(ppid, SIGCHLD);
2195 }
2196
2197 while (1) {
2198 DBG("Accepting client command ...");
2199
2200 /* Inifinite blocking call, waiting for transmission */
2201 ret = poll(pollfd, 2, -1);
2202 if (ret < 0) {
2203 perror("poll client thread");
2204 goto error;
2205 }
2206
2207 /* Thread quit pipe has been closed. Killing thread. */
2208 if (pollfd[0].revents == POLLNVAL) {
2209 goto error;
2210 } else if (pollfd[1].revents == POLLERR) {
2211 ERR("Client socket poll error");
2212 goto error;
2213 }
2214
2215 sock = lttcomm_accept_unix_sock(client_sock);
2216 if (sock < 0) {
2217 goto error;
2218 }
2219
2220 /* Allocate context command to process the client request */
2221 cmd_ctx = malloc(sizeof(struct command_ctx));
2222
2223 /* Allocate data buffer for reception */
2224 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
2225 cmd_ctx->llm = NULL;
2226 cmd_ctx->session = NULL;
2227
2228 /*
2229 * Data is received from the lttng client. The struct
2230 * lttcomm_session_msg (lsm) contains the command and data request of
2231 * the client.
2232 */
2233 DBG("Receiving data from client ...");
2234 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
2235 if (ret <= 0) {
2236 continue;
2237 }
2238
2239 // TODO: Validate cmd_ctx including sanity check for security purpose.
2240
2241 /*
2242 * This function dispatch the work to the kernel or userspace tracer
2243 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2244 * informations for the client. The command context struct contains
2245 * everything this function may needs.
2246 */
2247 ret = process_client_msg(cmd_ctx);
2248 if (ret < 0) {
2249 /* TODO: Inform client somehow of the fatal error. At this point,
2250 * ret < 0 means that a malloc failed (ENOMEM). */
2251 /* Error detected but still accept command */
2252 clean_command_ctx(&cmd_ctx);
2253 continue;
2254 }
2255
2256 DBG("Sending response (size: %d, retcode: %d)",
2257 cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code);
2258 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
2259 if (ret < 0) {
2260 ERR("Failed to send data back to client");
2261 }
2262
2263 clean_command_ctx(&cmd_ctx);
2264
2265 /* End of transmission */
2266 close(sock);
2267 }
2268
2269 error:
2270 DBG("Client thread dying");
2271 if (client_sock) {
2272 close(client_sock);
2273 }
2274 if (sock) {
2275 close(sock);
2276 }
2277
2278 unlink(client_unix_sock_path);
2279
2280 clean_command_ctx(&cmd_ctx);
2281 return NULL;
2282 }
2283
2284
2285 /*
2286 * usage function on stderr
2287 */
2288 static void usage(void)
2289 {
2290 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
2291 fprintf(stderr, " -h, --help Display this usage.\n");
2292 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
2293 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2294 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2295 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2296 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
2297 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2298 fprintf(stderr, " -V, --version Show version number.\n");
2299 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2300 fprintf(stderr, " -q, --quiet No output at all.\n");
2301 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2302 fprintf(stderr, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2303 }
2304
2305 /*
2306 * daemon argument parsing
2307 */
2308 static int parse_args(int argc, char **argv)
2309 {
2310 int c;
2311
2312 static struct option long_options[] = {
2313 { "client-sock", 1, 0, 'c' },
2314 { "apps-sock", 1, 0, 'a' },
2315 { "kconsumerd-cmd-sock", 1, 0, 0 },
2316 { "kconsumerd-err-sock", 1, 0, 0 },
2317 { "daemonize", 0, 0, 'd' },
2318 { "sig-parent", 0, 0, 'S' },
2319 { "help", 0, 0, 'h' },
2320 { "group", 1, 0, 'g' },
2321 { "version", 0, 0, 'V' },
2322 { "quiet", 0, 0, 'q' },
2323 { "verbose", 0, 0, 'v' },
2324 { "verbose-kconsumerd", 0, 0, 'Z' },
2325 { NULL, 0, 0, 0 }
2326 };
2327
2328 while (1) {
2329 int option_index = 0;
2330 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:Z", long_options, &option_index);
2331 if (c == -1) {
2332 break;
2333 }
2334
2335 switch (c) {
2336 case 0:
2337 fprintf(stderr, "option %s", long_options[option_index].name);
2338 if (optarg) {
2339 fprintf(stderr, " with arg %s\n", optarg);
2340 }
2341 break;
2342 case 'c':
2343 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
2344 break;
2345 case 'a':
2346 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
2347 break;
2348 case 'd':
2349 opt_daemon = 1;
2350 break;
2351 case 'g':
2352 opt_tracing_group = strdup(optarg);
2353 break;
2354 case 'h':
2355 usage();
2356 exit(EXIT_FAILURE);
2357 case 'V':
2358 fprintf(stdout, "%s\n", VERSION);
2359 exit(EXIT_SUCCESS);
2360 case 'S':
2361 opt_sig_parent = 1;
2362 break;
2363 case 'E':
2364 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
2365 break;
2366 case 'C':
2367 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
2368 break;
2369 case 'q':
2370 opt_quiet = 1;
2371 break;
2372 case 'v':
2373 /* Verbose level can increase using multiple -v */
2374 opt_verbose += 1;
2375 break;
2376 case 'Z':
2377 opt_verbose_kconsumerd += 1;
2378 break;
2379 default:
2380 /* Unknown option or other error.
2381 * Error is printed by getopt, just return */
2382 return -1;
2383 }
2384 }
2385
2386 return 0;
2387 }
2388
2389 /*
2390 * Creates the two needed socket by the daemon.
2391 * apps_sock - The communication socket for all UST apps.
2392 * client_sock - The communication of the cli tool (lttng).
2393 */
2394 static int init_daemon_socket(void)
2395 {
2396 int ret = 0;
2397 mode_t old_umask;
2398
2399 old_umask = umask(0);
2400
2401 /* Create client tool unix socket */
2402 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
2403 if (client_sock < 0) {
2404 ERR("Create unix sock failed: %s", client_unix_sock_path);
2405 ret = -1;
2406 goto end;
2407 }
2408
2409 /* File permission MUST be 660 */
2410 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2411 if (ret < 0) {
2412 ERR("Set file permissions failed: %s", client_unix_sock_path);
2413 perror("chmod");
2414 goto end;
2415 }
2416
2417 /* Create the application unix socket */
2418 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
2419 if (apps_sock < 0) {
2420 ERR("Create unix sock failed: %s", apps_unix_sock_path);
2421 ret = -1;
2422 goto end;
2423 }
2424
2425 /* File permission MUST be 666 */
2426 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
2427 if (ret < 0) {
2428 ERR("Set file permissions failed: %s", apps_unix_sock_path);
2429 perror("chmod");
2430 goto end;
2431 }
2432
2433 end:
2434 umask(old_umask);
2435 return ret;
2436 }
2437
2438 /*
2439 * Check if the global socket is available, and if a daemon is answering
2440 * at the other side. If yes, error is returned.
2441 */
2442 static int check_existing_daemon(void)
2443 {
2444 if (access(client_unix_sock_path, F_OK) < 0 &&
2445 access(apps_unix_sock_path, F_OK) < 0)
2446 return 0;
2447 /* Is there anybody out there ? */
2448 if (lttng_session_daemon_alive())
2449 return -EEXIST;
2450 else
2451 return 0;
2452 }
2453
2454 /*
2455 * Set the tracing group gid onto the client socket.
2456 *
2457 * Race window between mkdir and chown is OK because we are going from more
2458 * permissive (root.root) to les permissive (root.tracing).
2459 */
2460 static int set_permissions(void)
2461 {
2462 int ret;
2463 gid_t gid;
2464
2465 gid = allowed_group();
2466 if (gid < 0) {
2467 if (is_root) {
2468 WARN("No tracing group detected");
2469 ret = 0;
2470 } else {
2471 ERR("Missing tracing group. Aborting execution.");
2472 ret = -1;
2473 }
2474 goto end;
2475 }
2476
2477 /* Set lttng run dir */
2478 ret = chown(LTTNG_RUNDIR, 0, gid);
2479 if (ret < 0) {
2480 ERR("Unable to set group on " LTTNG_RUNDIR);
2481 perror("chown");
2482 }
2483
2484 /* lttng client socket path */
2485 ret = chown(client_unix_sock_path, 0, gid);
2486 if (ret < 0) {
2487 ERR("Unable to set group on %s", client_unix_sock_path);
2488 perror("chown");
2489 }
2490
2491 /* kconsumerd error socket path */
2492 ret = chown(kconsumerd_err_unix_sock_path, 0, gid);
2493 if (ret < 0) {
2494 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
2495 perror("chown");
2496 }
2497
2498 DBG("All permissions are set");
2499
2500 end:
2501 return ret;
2502 }
2503
2504 /*
2505 * Create the pipe used to wake up the kernel thread.
2506 */
2507 static int create_kernel_poll_pipe(void)
2508 {
2509 return pipe2(kernel_poll_pipe, O_CLOEXEC);
2510 }
2511
2512 /*
2513 * Create the lttng run directory needed for all global sockets and pipe.
2514 */
2515 static int create_lttng_rundir(void)
2516 {
2517 int ret;
2518
2519 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
2520 if (ret < 0) {
2521 if (errno != EEXIST) {
2522 ERR("Unable to create " LTTNG_RUNDIR);
2523 goto error;
2524 } else {
2525 ret = 0;
2526 }
2527 }
2528
2529 error:
2530 return ret;
2531 }
2532
2533 /*
2534 * Setup sockets and directory needed by the kconsumerd communication with the
2535 * session daemon.
2536 */
2537 static int set_kconsumerd_sockets(void)
2538 {
2539 int ret;
2540
2541 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
2542 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
2543 }
2544
2545 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
2546 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
2547 }
2548
2549 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
2550 if (ret < 0) {
2551 if (errno != EEXIST) {
2552 ERR("Failed to create " KCONSUMERD_PATH);
2553 goto error;
2554 }
2555 ret = 0;
2556 }
2557
2558 /* Create the kconsumerd error unix socket */
2559 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
2560 if (kconsumerd_err_sock < 0) {
2561 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
2562 ret = -1;
2563 goto error;
2564 }
2565
2566 /* File permission MUST be 660 */
2567 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2568 if (ret < 0) {
2569 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
2570 perror("chmod");
2571 goto error;
2572 }
2573
2574 error:
2575 return ret;
2576 }
2577
2578 /*
2579 * Signal handler for the daemon
2580 *
2581 * Simply stop all worker threads, leaving main() return gracefully
2582 * after joining all threads and calling cleanup().
2583 */
2584 static void sighandler(int sig)
2585 {
2586 switch (sig) {
2587 case SIGPIPE:
2588 DBG("SIGPIPE catched");
2589 return;
2590 case SIGINT:
2591 DBG("SIGINT catched");
2592 stop_threads();
2593 break;
2594 case SIGTERM:
2595 DBG("SIGTERM catched");
2596 stop_threads();
2597 break;
2598 default:
2599 break;
2600 }
2601 }
2602
2603 /*
2604 * Setup signal handler for :
2605 * SIGINT, SIGTERM, SIGPIPE
2606 */
2607 static int set_signal_handler(void)
2608 {
2609 int ret = 0;
2610 struct sigaction sa;
2611 sigset_t sigset;
2612
2613 if ((ret = sigemptyset(&sigset)) < 0) {
2614 perror("sigemptyset");
2615 return ret;
2616 }
2617
2618 sa.sa_handler = sighandler;
2619 sa.sa_mask = sigset;
2620 sa.sa_flags = 0;
2621 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
2622 perror("sigaction");
2623 return ret;
2624 }
2625
2626 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
2627 perror("sigaction");
2628 return ret;
2629 }
2630
2631 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
2632 perror("sigaction");
2633 return ret;
2634 }
2635
2636 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2637
2638 return ret;
2639 }
2640
2641 /*
2642 * Set open files limit to unlimited. This daemon can open a large number of
2643 * file descriptors in order to consumer multiple kernel traces.
2644 */
2645 static void set_ulimit(void)
2646 {
2647 int ret;
2648 struct rlimit lim;
2649
2650 /* The kernel does not allowed an infinite limit for open files */
2651 lim.rlim_cur = 65535;
2652 lim.rlim_max = 65535;
2653
2654 ret = setrlimit(RLIMIT_NOFILE, &lim);
2655 if (ret < 0) {
2656 perror("failed to set open files limit");
2657 }
2658 }
2659
2660 /*
2661 * main
2662 */
2663 int main(int argc, char **argv)
2664 {
2665 int ret = 0;
2666 void *status;
2667 const char *home_path;
2668
2669 /* Create thread quit pipe */
2670 if ((ret = init_thread_quit_pipe()) < 0) {
2671 goto error;
2672 }
2673
2674 /* Parse arguments */
2675 progname = argv[0];
2676 if ((ret = parse_args(argc, argv) < 0)) {
2677 goto error;
2678 }
2679
2680 /* Daemonize */
2681 if (opt_daemon) {
2682 ret = daemon(0, 0);
2683 if (ret < 0) {
2684 perror("daemon");
2685 goto error;
2686 }
2687 }
2688
2689 /* Check if daemon is UID = 0 */
2690 is_root = !getuid();
2691
2692 if (is_root) {
2693 ret = create_lttng_rundir();
2694 if (ret < 0) {
2695 goto error;
2696 }
2697
2698 if (strlen(apps_unix_sock_path) == 0) {
2699 snprintf(apps_unix_sock_path, PATH_MAX,
2700 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
2701 }
2702
2703 if (strlen(client_unix_sock_path) == 0) {
2704 snprintf(client_unix_sock_path, PATH_MAX,
2705 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
2706 }
2707 } else {
2708 home_path = get_home_dir();
2709 if (home_path == NULL) {
2710 /* TODO: Add --socket PATH option */
2711 ERR("Can't get HOME directory for sockets creation.");
2712 ret = -EPERM;
2713 goto error;
2714 }
2715
2716 if (strlen(apps_unix_sock_path) == 0) {
2717 snprintf(apps_unix_sock_path, PATH_MAX,
2718 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
2719 }
2720
2721 /* Set the cli tool unix socket path */
2722 if (strlen(client_unix_sock_path) == 0) {
2723 snprintf(client_unix_sock_path, PATH_MAX,
2724 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
2725 }
2726 }
2727
2728 DBG("Client socket path %s", client_unix_sock_path);
2729 DBG("Application socket path %s", apps_unix_sock_path);
2730
2731 /*
2732 * See if daemon already exist.
2733 */
2734 if ((ret = check_existing_daemon()) < 0) {
2735 ERR("Already running daemon.\n");
2736 /*
2737 * We do not goto exit because we must not cleanup()
2738 * because a daemon is already running.
2739 */
2740 goto error;
2741 }
2742
2743 /* After this point, we can safely call cleanup() so goto error is used */
2744
2745 /*
2746 * These actions must be executed as root. We do that *after* setting up
2747 * the sockets path because we MUST make the check for another daemon using
2748 * those paths *before* trying to set the kernel consumer sockets and init
2749 * kernel tracer.
2750 */
2751 if (is_root) {
2752 ret = set_kconsumerd_sockets();
2753 if (ret < 0) {
2754 goto exit;
2755 }
2756
2757 /* Setup kernel tracer */
2758 init_kernel_tracer();
2759
2760 /* Set ulimit for open files */
2761 set_ulimit();
2762 }
2763
2764 if ((ret = set_signal_handler()) < 0) {
2765 goto exit;
2766 }
2767
2768 /* Setup the needed unix socket */
2769 if ((ret = init_daemon_socket()) < 0) {
2770 goto exit;
2771 }
2772
2773 /* Set credentials to socket */
2774 if (is_root && ((ret = set_permissions()) < 0)) {
2775 goto exit;
2776 }
2777
2778 /* Get parent pid if -S, --sig-parent is specified. */
2779 if (opt_sig_parent) {
2780 ppid = getppid();
2781 }
2782
2783 /* Setup the kernel pipe for waking up the kernel thread */
2784 if ((ret = create_kernel_poll_pipe()) < 0) {
2785 goto exit;
2786 }
2787
2788 /*
2789 * Get session list pointer. This pointer MUST NOT be free().
2790 * This list is statically declared in session.c
2791 */
2792 session_list_ptr = get_session_list();
2793
2794 /* Create thread to manage the client socket */
2795 ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL);
2796 if (ret != 0) {
2797 perror("pthread_create");
2798 goto exit_client;
2799 }
2800
2801 /* Create thread to manage application socket */
2802 ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL);
2803 if (ret != 0) {
2804 perror("pthread_create");
2805 goto exit_apps;
2806 }
2807
2808 /* Create kernel thread to manage kernel event */
2809 ret = pthread_create(&kernel_thread, NULL, thread_manage_kernel, (void *) NULL);
2810 if (ret != 0) {
2811 perror("pthread_create");
2812 goto exit_kernel;
2813 }
2814
2815 ret = pthread_join(kernel_thread, &status);
2816 if (ret != 0) {
2817 perror("pthread_join");
2818 goto error; /* join error, exit without cleanup */
2819 }
2820
2821 exit_kernel:
2822 ret = pthread_join(apps_thread, &status);
2823 if (ret != 0) {
2824 perror("pthread_join");
2825 goto error; /* join error, exit without cleanup */
2826 }
2827
2828 exit_apps:
2829 ret = pthread_join(client_thread, &status);
2830 if (ret != 0) {
2831 perror("pthread_join");
2832 goto error; /* join error, exit without cleanup */
2833 }
2834
2835 ret = join_kconsumerd_thread();
2836 if (ret != 0) {
2837 perror("join_kconsumerd");
2838 goto error; /* join error, exit without cleanup */
2839 }
2840
2841 exit_client:
2842 exit:
2843 /*
2844 * cleanup() is called when no other thread is running.
2845 */
2846 cleanup();
2847 if (!ret)
2848 exit(EXIT_SUCCESS);
2849 error:
2850 exit(EXIT_FAILURE);
2851 }
This page took 0.138674 seconds and 5 git commands to generate.