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