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