Implement support for lttng-modules syscall filtering
[lttng-tools.git] / src / bin / lttng-sessiond / kernel.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 modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <inttypes.h>
26
27 #include <common/common.h>
28 #include <common/kernel-ctl/kernel-ctl.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
30
31 #include "consumer.h"
32 #include "kernel.h"
33 #include "kernel-consumer.h"
34 #include "kern-modules.h"
35
36 /*
37 * Add context on a kernel channel.
38 */
39 int kernel_add_channel_context(struct ltt_kernel_channel *chan,
40 struct ltt_kernel_context *ctx)
41 {
42 int ret;
43
44 assert(chan);
45 assert(ctx);
46
47 DBG("Adding context to channel %s", chan->channel->name);
48 ret = kernctl_add_context(chan->fd, &ctx->ctx);
49 if (ret < 0) {
50 if (errno != EEXIST) {
51 PERROR("add context ioctl");
52 } else {
53 /* If EEXIST, we just ignore the error */
54 ret = 0;
55 }
56 goto error;
57 }
58
59 cds_list_add_tail(&ctx->list, &chan->ctx_list);
60
61 return 0;
62
63 error:
64 return ret;
65 }
66
67 /*
68 * Create a new kernel session, register it to the kernel tracer and add it to
69 * the session daemon session.
70 */
71 int kernel_create_session(struct ltt_session *session, int tracer_fd)
72 {
73 int ret;
74 struct ltt_kernel_session *lks;
75
76 assert(session);
77
78 /* Allocate data structure */
79 lks = trace_kernel_create_session();
80 if (lks == NULL) {
81 ret = -1;
82 goto error;
83 }
84
85 /* Kernel tracer session creation */
86 ret = kernctl_create_session(tracer_fd);
87 if (ret < 0) {
88 PERROR("ioctl kernel create session");
89 goto error;
90 }
91
92 lks->fd = ret;
93 /* Prevent fd duplication after execlp() */
94 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
95 if (ret < 0) {
96 PERROR("fcntl session fd");
97 }
98
99 lks->id = session->id;
100 lks->consumer_fds_sent = 0;
101 session->kernel_session = lks;
102
103 DBG("Kernel session created (fd: %d)", lks->fd);
104
105 return 0;
106
107 error:
108 if (lks) {
109 trace_kernel_destroy_session(lks);
110 }
111 return ret;
112 }
113
114 /*
115 * Create a kernel channel, register it to the kernel tracer and add it to the
116 * kernel session.
117 */
118 int kernel_create_channel(struct ltt_kernel_session *session,
119 struct lttng_channel *chan)
120 {
121 int ret;
122 struct ltt_kernel_channel *lkc;
123
124 assert(session);
125 assert(chan);
126
127 /* Allocate kernel channel */
128 lkc = trace_kernel_create_channel(chan);
129 if (lkc == NULL) {
130 goto error;
131 }
132
133 DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d",
134 chan->name, lkc->channel->attr.overwrite,
135 lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf,
136 lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval,
137 lkc->channel->attr.live_timer_interval, lkc->channel->attr.output);
138
139 /* Kernel tracer channel creation */
140 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
141 if (ret < 0) {
142 PERROR("ioctl kernel create channel");
143 goto error;
144 }
145
146 /* Setup the channel fd */
147 lkc->fd = ret;
148 /* Prevent fd duplication after execlp() */
149 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
150 if (ret < 0) {
151 PERROR("fcntl session fd");
152 }
153
154 /* Add channel to session */
155 cds_list_add(&lkc->list, &session->channel_list.head);
156 session->channel_count++;
157 lkc->session = session;
158
159 DBG("Kernel channel %s created (fd: %d)", lkc->channel->name, lkc->fd);
160
161 return 0;
162
163 error:
164 if (lkc) {
165 free(lkc->channel);
166 free(lkc);
167 }
168 return -1;
169 }
170
171 /*
172 * Create a kernel event, enable it to the kernel tracer and add it to the
173 * channel event list of the kernel session.
174 */
175 int kernel_create_event(struct lttng_event *ev,
176 struct ltt_kernel_channel *channel)
177 {
178 int ret;
179 struct ltt_kernel_event *event;
180
181 assert(ev);
182 assert(channel);
183
184 event = trace_kernel_create_event(ev);
185 if (event == NULL) {
186 ret = -1;
187 goto error;
188 }
189
190 ret = kernctl_create_event(channel->fd, event->event);
191 if (ret < 0) {
192 switch (errno) {
193 case EEXIST:
194 break;
195 case ENOSYS:
196 WARN("Event type not implemented");
197 break;
198 case ENOENT:
199 WARN("Event %s not found!", ev->name);
200 break;
201 default:
202 PERROR("create event ioctl");
203 }
204 ret = -errno;
205 goto free_event;
206 }
207
208 /*
209 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
210 */
211 if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) {
212 DBG2("Kernel event syscall creation success");
213 /*
214 * We use fd == -1 to ensure that we never trigger a close of fd
215 * 0.
216 */
217 event->fd = -1;
218 goto add_list;
219 }
220
221 event->fd = ret;
222 /* Prevent fd duplication after execlp() */
223 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
224 if (ret < 0) {
225 PERROR("fcntl session fd");
226 }
227
228 add_list:
229 /* Add event to event list */
230 cds_list_add(&event->list, &channel->events_list.head);
231 channel->event_count++;
232
233 DBG("Event %s created (fd: %d)", ev->name, event->fd);
234
235 return 0;
236
237 free_event:
238 free(event);
239 error:
240 return ret;
241 }
242
243 /*
244 * Disable a kernel channel.
245 */
246 int kernel_disable_channel(struct ltt_kernel_channel *chan)
247 {
248 int ret;
249
250 assert(chan);
251
252 ret = kernctl_disable(chan->fd);
253 if (ret < 0) {
254 PERROR("disable chan ioctl");
255 ret = errno;
256 goto error;
257 }
258
259 chan->enabled = 0;
260 DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd);
261
262 return 0;
263
264 error:
265 return ret;
266 }
267
268 /*
269 * Enable a kernel channel.
270 */
271 int kernel_enable_channel(struct ltt_kernel_channel *chan)
272 {
273 int ret;
274
275 assert(chan);
276
277 ret = kernctl_enable(chan->fd);
278 if (ret < 0 && errno != EEXIST) {
279 PERROR("Enable kernel chan");
280 goto error;
281 }
282
283 chan->enabled = 1;
284 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
285
286 return 0;
287
288 error:
289 return ret;
290 }
291
292 /*
293 * Enable a kernel event.
294 */
295 int kernel_enable_event(struct ltt_kernel_event *event)
296 {
297 int ret;
298
299 assert(event);
300
301 ret = kernctl_enable(event->fd);
302 if (ret < 0) {
303 switch (errno) {
304 case EEXIST:
305 ret = LTTNG_ERR_KERN_EVENT_EXIST;
306 break;
307 default:
308 PERROR("enable kernel event");
309 break;
310 }
311 goto error;
312 }
313
314 event->enabled = 1;
315 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
316
317 return 0;
318
319 error:
320 return ret;
321 }
322
323 /*
324 * Disable a kernel event.
325 */
326 int kernel_disable_event(struct ltt_kernel_event *event)
327 {
328 int ret;
329
330 assert(event);
331
332 ret = kernctl_disable(event->fd);
333 if (ret < 0) {
334 switch (errno) {
335 case EEXIST:
336 ret = LTTNG_ERR_KERN_EVENT_EXIST;
337 break;
338 default:
339 PERROR("disable kernel event");
340 break;
341 }
342 goto error;
343 }
344
345 event->enabled = 0;
346 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
347
348 return 0;
349
350 error:
351 return ret;
352 }
353
354 int kernel_enable_syscall(const char *syscall_name,
355 struct ltt_kernel_channel *channel)
356 {
357 return kernctl_enable_syscall(channel->fd, syscall_name);
358 }
359
360 int kernel_disable_syscall(const char *syscall_name,
361 struct ltt_kernel_channel *channel)
362 {
363 return kernctl_disable_syscall(channel->fd, syscall_name);
364 }
365
366 /*
367 * Create kernel metadata, open from the kernel tracer and add it to the
368 * kernel session.
369 */
370 int kernel_open_metadata(struct ltt_kernel_session *session)
371 {
372 int ret;
373 struct ltt_kernel_metadata *lkm = NULL;
374
375 assert(session);
376
377 /* Allocate kernel metadata */
378 lkm = trace_kernel_create_metadata();
379 if (lkm == NULL) {
380 goto error;
381 }
382
383 /* Kernel tracer metadata creation */
384 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
385 if (ret < 0) {
386 goto error_open;
387 }
388
389 lkm->fd = ret;
390 /* Prevent fd duplication after execlp() */
391 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
392 if (ret < 0) {
393 PERROR("fcntl session fd");
394 }
395
396 session->metadata = lkm;
397
398 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
399
400 return 0;
401
402 error_open:
403 trace_kernel_destroy_metadata(lkm);
404 error:
405 return -1;
406 }
407
408 /*
409 * Start tracing session.
410 */
411 int kernel_start_session(struct ltt_kernel_session *session)
412 {
413 int ret;
414
415 assert(session);
416
417 ret = kernctl_start_session(session->fd);
418 if (ret < 0) {
419 PERROR("ioctl start session");
420 goto error;
421 }
422
423 DBG("Kernel session started");
424
425 return 0;
426
427 error:
428 return ret;
429 }
430
431 /*
432 * Make a kernel wait to make sure in-flight probe have completed.
433 */
434 void kernel_wait_quiescent(int fd)
435 {
436 int ret;
437
438 DBG("Kernel quiescent wait on %d", fd);
439
440 ret = kernctl_wait_quiescent(fd);
441 if (ret < 0) {
442 PERROR("wait quiescent ioctl");
443 ERR("Kernel quiescent wait failed");
444 }
445 }
446
447 /*
448 * Kernel calibrate
449 */
450 int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
451 {
452 int ret;
453
454 assert(calibrate);
455
456 ret = kernctl_calibrate(fd, calibrate);
457 if (ret < 0) {
458 PERROR("calibrate ioctl");
459 return -1;
460 }
461
462 return 0;
463 }
464
465
466 /*
467 * Force flush buffer of metadata.
468 */
469 int kernel_metadata_flush_buffer(int fd)
470 {
471 int ret;
472
473 DBG("Kernel flushing metadata buffer on fd %d", fd);
474
475 ret = kernctl_buffer_flush(fd);
476 if (ret < 0) {
477 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
478 }
479
480 return 0;
481 }
482
483 /*
484 * Force flush buffer for channel.
485 */
486 int kernel_flush_buffer(struct ltt_kernel_channel *channel)
487 {
488 int ret;
489 struct ltt_kernel_stream *stream;
490
491 assert(channel);
492
493 DBG("Flush buffer for channel %s", channel->channel->name);
494
495 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
496 DBG("Flushing channel stream %d", stream->fd);
497 ret = kernctl_buffer_flush(stream->fd);
498 if (ret < 0) {
499 PERROR("ioctl");
500 ERR("Fail to flush buffer for stream %d (ret: %d)",
501 stream->fd, ret);
502 }
503 }
504
505 return 0;
506 }
507
508 /*
509 * Stop tracing session.
510 */
511 int kernel_stop_session(struct ltt_kernel_session *session)
512 {
513 int ret;
514
515 assert(session);
516
517 ret = kernctl_stop_session(session->fd);
518 if (ret < 0) {
519 goto error;
520 }
521
522 DBG("Kernel session stopped");
523
524 return 0;
525
526 error:
527 return ret;
528 }
529
530 /*
531 * Open stream of channel, register it to the kernel tracer and add it
532 * to the stream list of the channel.
533 *
534 * Return the number of created stream. Else, a negative value.
535 */
536 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
537 {
538 int ret, count = 0;
539 struct ltt_kernel_stream *lks;
540
541 assert(channel);
542
543 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
544 lks = trace_kernel_create_stream(channel->channel->name, count);
545 if (lks == NULL) {
546 ret = close(ret);
547 if (ret) {
548 PERROR("close");
549 }
550 goto error;
551 }
552
553 lks->fd = ret;
554 /* Prevent fd duplication after execlp() */
555 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
556 if (ret < 0) {
557 PERROR("fcntl session fd");
558 }
559
560 lks->tracefile_size = channel->channel->attr.tracefile_size;
561 lks->tracefile_count = channel->channel->attr.tracefile_count;
562
563 /* Add stream to channe stream list */
564 cds_list_add(&lks->list, &channel->stream_list.head);
565 channel->stream_count++;
566
567 /* Increment counter which represent CPU number. */
568 count++;
569
570 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
571 lks->state);
572 }
573
574 return channel->stream_count;
575
576 error:
577 return -1;
578 }
579
580 /*
581 * Open the metadata stream and set it to the kernel session.
582 */
583 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
584 {
585 int ret;
586
587 assert(session);
588
589 ret = kernctl_create_stream(session->metadata->fd);
590 if (ret < 0) {
591 PERROR("kernel create metadata stream");
592 goto error;
593 }
594
595 DBG("Kernel metadata stream created (fd: %d)", ret);
596 session->metadata_stream_fd = ret;
597 /* Prevent fd duplication after execlp() */
598 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
599 if (ret < 0) {
600 PERROR("fcntl session fd");
601 }
602
603 return 0;
604
605 error:
606 return -1;
607 }
608
609 /*
610 * Get the event list from the kernel tracer and return the number of elements.
611 */
612 ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
613 {
614 int fd, ret;
615 char *event;
616 size_t nbmem, count = 0;
617 FILE *fp;
618 struct lttng_event *elist;
619
620 assert(events);
621
622 fd = kernctl_tracepoint_list(tracer_fd);
623 if (fd < 0) {
624 PERROR("kernel tracepoint list");
625 goto error;
626 }
627
628 fp = fdopen(fd, "r");
629 if (fp == NULL) {
630 PERROR("kernel tracepoint list fdopen");
631 goto error_fp;
632 }
633
634 /*
635 * Init memory size counter
636 * See kernel-ctl.h for explanation of this value
637 */
638 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
639 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
640 if (elist == NULL) {
641 PERROR("alloc list events");
642 count = -ENOMEM;
643 goto end;
644 }
645
646 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
647 if (count >= nbmem) {
648 struct lttng_event *new_elist;
649 size_t new_nbmem;
650
651 new_nbmem = nbmem << 1;
652 DBG("Reallocating event list from %zu to %zu bytes",
653 nbmem, new_nbmem);
654 new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event));
655 if (new_elist == NULL) {
656 PERROR("realloc list events");
657 free(event);
658 free(elist);
659 count = -ENOMEM;
660 goto end;
661 }
662 /* Zero the new memory */
663 memset(new_elist + nbmem, 0,
664 (new_nbmem - nbmem) * sizeof(struct lttng_event));
665 nbmem = new_nbmem;
666 elist = new_elist;
667 }
668 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
669 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
670 elist[count].enabled = -1;
671 count++;
672 free(event);
673 }
674
675 *events = elist;
676 DBG("Kernel list events done (%zu events)", count);
677 end:
678 ret = fclose(fp); /* closes both fp and fd */
679 if (ret) {
680 PERROR("fclose");
681 }
682 return count;
683
684 error_fp:
685 ret = close(fd);
686 if (ret) {
687 PERROR("close");
688 }
689 error:
690 return -1;
691 }
692
693 /*
694 * Get kernel version and validate it.
695 */
696 int kernel_validate_version(int tracer_fd)
697 {
698 int ret;
699 struct lttng_kernel_tracer_version version;
700
701 ret = kernctl_tracer_version(tracer_fd, &version);
702 if (ret < 0) {
703 ERR("Failed at getting the lttng-modules version");
704 goto error;
705 }
706
707 /* Validate version */
708 if (version.major != KERN_MODULES_PRE_MAJOR
709 && version.major != KERN_MODULES_MAJOR) {
710 goto error_version;
711 }
712
713 DBG2("Kernel tracer version validated (major version %d)", version.major);
714 return 0;
715
716 error_version:
717 ERR("Kernel major version %d is not compatible (supporting <= %d)",
718 version.major, KERN_MODULES_MAJOR)
719 ret = -1;
720
721 error:
722 return ret;
723 }
724
725 /*
726 * Kernel work-arounds called at the start of sessiond main().
727 */
728 int init_kernel_workarounds(void)
729 {
730 int ret;
731 FILE *fp;
732
733 /*
734 * boot_id needs to be read once before being used concurrently
735 * to deal with a Linux kernel race. A fix is proposed for
736 * upstream, but the work-around is needed for older kernels.
737 */
738 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
739 if (!fp) {
740 goto end_boot_id;
741 }
742 while (!feof(fp)) {
743 char buf[37] = "";
744
745 ret = fread(buf, 1, sizeof(buf), fp);
746 if (ret < 0) {
747 /* Ignore error, we don't really care */
748 }
749 }
750 ret = fclose(fp);
751 if (ret) {
752 PERROR("fclose");
753 }
754 end_boot_id:
755 return 0;
756 }
757
758 /*
759 * Complete teardown of a kernel session.
760 */
761 void kernel_destroy_session(struct ltt_kernel_session *ksess)
762 {
763 if (ksess == NULL) {
764 DBG3("No kernel session when tearing down session");
765 return;
766 }
767
768 DBG("Tearing down kernel session");
769
770 /*
771 * Destroy channels on the consumer if at least one FD has been sent and we
772 * are in no output mode because the streams are in *no* monitor mode so we
773 * have to send a command to clean them up or else they leaked.
774 */
775 if (!ksess->output_traces && ksess->consumer_fds_sent) {
776 int ret;
777 struct consumer_socket *socket;
778 struct lttng_ht_iter iter;
779
780 /* For each consumer socket. */
781 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
782 socket, node.node) {
783 struct ltt_kernel_channel *chan;
784
785 /* For each channel, ask the consumer to destroy it. */
786 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
787 ret = kernel_consumer_destroy_channel(socket, chan);
788 if (ret < 0) {
789 /* Consumer is probably dead. Use next socket. */
790 continue;
791 }
792 }
793 }
794 }
795
796 /* Close any relayd session */
797 consumer_output_send_destroy_relayd(ksess->consumer);
798
799 trace_kernel_destroy_session(ksess);
800 }
801
802 /*
803 * Destroy a kernel channel object. It does not do anything on the tracer side.
804 */
805 void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
806 {
807 struct ltt_kernel_session *ksess = NULL;
808
809 assert(kchan);
810 assert(kchan->channel);
811
812 DBG3("Kernel destroy channel %s", kchan->channel->name);
813
814 /* Update channel count of associated session. */
815 if (kchan->session) {
816 /* Keep pointer reference so we can update it after the destroy. */
817 ksess = kchan->session;
818 }
819
820 trace_kernel_destroy_channel(kchan);
821
822 /*
823 * At this point the kernel channel is not visible anymore. This is safe
824 * since in order to work on a visible kernel session, the tracing session
825 * lock (ltt_session.lock) MUST be acquired.
826 */
827 if (ksess) {
828 ksess->channel_count--;
829 }
830 }
831
832 /*
833 * Take a snapshot for a given kernel session.
834 *
835 * Return 0 on success or else return a LTTNG_ERR code.
836 */
837 int kernel_snapshot_record(struct ltt_kernel_session *ksess,
838 struct snapshot_output *output, int wait, uint64_t max_size_per_stream)
839 {
840 int err, ret, saved_metadata_fd;
841 struct consumer_socket *socket;
842 struct lttng_ht_iter iter;
843 struct ltt_kernel_metadata *saved_metadata;
844
845 assert(ksess);
846 assert(ksess->consumer);
847 assert(output);
848
849 DBG("Kernel snapshot record started");
850
851 /* Save current metadata since the following calls will change it. */
852 saved_metadata = ksess->metadata;
853 saved_metadata_fd = ksess->metadata_stream_fd;
854
855 rcu_read_lock();
856
857 ret = kernel_open_metadata(ksess);
858 if (ret < 0) {
859 ret = LTTNG_ERR_KERN_META_FAIL;
860 goto error;
861 }
862
863 ret = kernel_open_metadata_stream(ksess);
864 if (ret < 0) {
865 ret = LTTNG_ERR_KERN_META_FAIL;
866 goto error_open_stream;
867 }
868
869 /* Send metadata to consumer and snapshot everything. */
870 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
871 socket, node.node) {
872 struct consumer_output *saved_output;
873 struct ltt_kernel_channel *chan;
874
875 /*
876 * Temporarly switch consumer output for our snapshot output. As long
877 * as the session lock is taken, this is safe.
878 */
879 saved_output = ksess->consumer;
880 ksess->consumer = output->consumer;
881
882 pthread_mutex_lock(socket->lock);
883 /* This stream must not be monitored by the consumer. */
884 ret = kernel_consumer_add_metadata(socket, ksess, 0);
885 pthread_mutex_unlock(socket->lock);
886 /* Put back the saved consumer output into the session. */
887 ksess->consumer = saved_output;
888 if (ret < 0) {
889 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
890 goto error_consumer;
891 }
892
893 /* For each channel, ask the consumer to snapshot it. */
894 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
895 pthread_mutex_lock(socket->lock);
896 ret = consumer_snapshot_channel(socket, chan->fd, output, 0,
897 ksess->uid, ksess->gid,
898 DEFAULT_KERNEL_TRACE_DIR, wait,
899 max_size_per_stream);
900 pthread_mutex_unlock(socket->lock);
901 if (ret < 0) {
902 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
903 (void) kernel_consumer_destroy_metadata(socket,
904 ksess->metadata);
905 goto error_consumer;
906 }
907 }
908
909 /* Snapshot metadata, */
910 pthread_mutex_lock(socket->lock);
911 ret = consumer_snapshot_channel(socket, ksess->metadata->fd, output,
912 1, ksess->uid, ksess->gid,
913 DEFAULT_KERNEL_TRACE_DIR, wait, max_size_per_stream);
914 pthread_mutex_unlock(socket->lock);
915 if (ret < 0) {
916 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
917 goto error_consumer;
918 }
919
920 /*
921 * The metadata snapshot is done, ask the consumer to destroy it since
922 * it's not monitored on the consumer side.
923 */
924 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
925 }
926
927 ret = LTTNG_OK;
928
929 error_consumer:
930 /* Close newly opened metadata stream. It's now on the consumer side. */
931 err = close(ksess->metadata_stream_fd);
932 if (err < 0) {
933 PERROR("close snapshot kernel");
934 }
935
936 error_open_stream:
937 trace_kernel_destroy_metadata(ksess->metadata);
938 error:
939 /* Restore metadata state.*/
940 ksess->metadata = saved_metadata;
941 ksess->metadata_stream_fd = saved_metadata_fd;
942
943 rcu_read_unlock();
944 return ret;
945 }
This page took 0.048817 seconds and 4 git commands to generate.