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