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