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