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