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