Fix: kernel memory leak in error path
[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 "kern-modules.h"
34
35 /*
36 * Add context on a kernel channel.
37 */
38 int kernel_add_channel_context(struct ltt_kernel_channel *chan,
39 struct lttng_kernel_context *ctx)
40 {
41 int ret;
42
43 assert(chan);
44 assert(ctx);
45
46 DBG("Adding context to channel %s", chan->channel->name);
47 ret = kernctl_add_context(chan->fd, ctx);
48 if (ret < 0) {
49 if (errno != EEXIST) {
50 PERROR("add context ioctl");
51 } else {
52 /* If EEXIST, we just ignore the error */
53 ret = 0;
54 }
55 goto error;
56 }
57
58 chan->ctx = zmalloc(sizeof(struct lttng_kernel_context));
59 if (chan->ctx == NULL) {
60 PERROR("zmalloc event context");
61 goto error;
62 }
63
64 memcpy(chan->ctx, ctx, sizeof(struct lttng_kernel_context));
65
66 return 0;
67
68 error:
69 return ret;
70 }
71
72 /*
73 * Create a new kernel session, register it to the kernel tracer and add it to
74 * the session daemon session.
75 */
76 int kernel_create_session(struct ltt_session *session, int tracer_fd)
77 {
78 int ret;
79 struct ltt_kernel_session *lks;
80
81 assert(session);
82
83 /* Allocate data structure */
84 lks = trace_kernel_create_session();
85 if (lks == NULL) {
86 ret = -1;
87 goto error;
88 }
89
90 /* Kernel tracer session creation */
91 ret = kernctl_create_session(tracer_fd);
92 if (ret < 0) {
93 PERROR("ioctl kernel create session");
94 goto error;
95 }
96
97 lks->fd = ret;
98 /* Prevent fd duplication after execlp() */
99 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
100 if (ret < 0) {
101 PERROR("fcntl session fd");
102 }
103
104 lks->id = session->id;
105 lks->consumer_fds_sent = 0;
106 session->kernel_session = lks;
107
108 DBG("Kernel session created (fd: %d)", lks->fd);
109
110 return 0;
111
112 error:
113 if (lks) {
114 trace_kernel_destroy_session(lks);
115 }
116 return ret;
117 }
118
119 /*
120 * Create a kernel channel, register it to the kernel tracer and add it to the
121 * kernel session.
122 */
123 int kernel_create_channel(struct ltt_kernel_session *session,
124 struct lttng_channel *chan)
125 {
126 int ret;
127 struct ltt_kernel_channel *lkc;
128
129 assert(session);
130 assert(chan);
131
132 /* Allocate kernel channel */
133 lkc = trace_kernel_create_channel(chan);
134 if (lkc == NULL) {
135 goto error;
136 }
137
138 DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d",
139 chan->name, lkc->channel->attr.overwrite,
140 lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf,
141 lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval,
142 lkc->channel->attr.output);
143
144 /* Kernel tracer channel creation */
145 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
146 if (ret < 0) {
147 PERROR("ioctl kernel create channel");
148 goto error;
149 }
150
151 /* Setup the channel fd */
152 lkc->fd = ret;
153 /* Prevent fd duplication after execlp() */
154 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
155 if (ret < 0) {
156 PERROR("fcntl session fd");
157 }
158
159 /* Add channel to session */
160 cds_list_add(&lkc->list, &session->channel_list.head);
161 session->channel_count++;
162 lkc->session = session;
163
164 DBG("Kernel channel %s created (fd: %d)", lkc->channel->name, lkc->fd);
165
166 return 0;
167
168 error:
169 if (lkc) {
170 free(lkc->channel);
171 free(lkc);
172 }
173 return -1;
174 }
175
176 /*
177 * Create a kernel event, enable it to the kernel tracer and add it to the
178 * channel event list of the kernel session.
179 */
180 int kernel_create_event(struct lttng_event *ev,
181 struct ltt_kernel_channel *channel)
182 {
183 int ret;
184 struct ltt_kernel_event *event;
185
186 assert(ev);
187 assert(channel);
188
189 event = trace_kernel_create_event(ev);
190 if (event == NULL) {
191 ret = -1;
192 goto error;
193 }
194
195 ret = kernctl_create_event(channel->fd, event->event);
196 if (ret < 0) {
197 switch (errno) {
198 case EEXIST:
199 break;
200 case ENOSYS:
201 WARN("Event type not implemented");
202 break;
203 default:
204 PERROR("create event ioctl");
205 }
206 ret = -errno;
207 goto free_event;
208 }
209
210 /*
211 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
212 */
213 if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) {
214 DBG2("Kernel event syscall creation success");
215 /*
216 * We use fd == -1 to ensure that we never trigger a close of fd
217 * 0.
218 */
219 event->fd = -1;
220 goto add_list;
221 }
222
223 event->fd = ret;
224 /* Prevent fd duplication after execlp() */
225 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
226 if (ret < 0) {
227 PERROR("fcntl session fd");
228 }
229
230 add_list:
231 /* Add event to event list */
232 cds_list_add(&event->list, &channel->events_list.head);
233 channel->event_count++;
234
235 DBG("Event %s created (fd: %d)", ev->name, event->fd);
236
237 return 0;
238
239 free_event:
240 free(event);
241 error:
242 return ret;
243 }
244
245 /*
246 * Disable a kernel channel.
247 */
248 int kernel_disable_channel(struct ltt_kernel_channel *chan)
249 {
250 int ret;
251
252 assert(chan);
253
254 ret = kernctl_disable(chan->fd);
255 if (ret < 0) {
256 PERROR("disable chan ioctl");
257 ret = errno;
258 goto error;
259 }
260
261 chan->enabled = 0;
262 DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd);
263
264 return 0;
265
266 error:
267 return ret;
268 }
269
270 /*
271 * Enable a kernel channel.
272 */
273 int kernel_enable_channel(struct ltt_kernel_channel *chan)
274 {
275 int ret;
276
277 assert(chan);
278
279 ret = kernctl_enable(chan->fd);
280 if (ret < 0 && errno != EEXIST) {
281 PERROR("Enable kernel chan");
282 goto error;
283 }
284
285 chan->enabled = 1;
286 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
287
288 return 0;
289
290 error:
291 return ret;
292 }
293
294 /*
295 * Enable a kernel event.
296 */
297 int kernel_enable_event(struct ltt_kernel_event *event)
298 {
299 int ret;
300
301 assert(event);
302
303 ret = kernctl_enable(event->fd);
304 if (ret < 0) {
305 switch (errno) {
306 case EEXIST:
307 ret = LTTNG_ERR_KERN_EVENT_EXIST;
308 break;
309 default:
310 PERROR("enable kernel event");
311 break;
312 }
313 goto error;
314 }
315
316 event->enabled = 1;
317 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
318
319 return 0;
320
321 error:
322 return ret;
323 }
324
325 /*
326 * Disable a kernel event.
327 */
328 int kernel_disable_event(struct ltt_kernel_event *event)
329 {
330 int ret;
331
332 assert(event);
333
334 ret = kernctl_disable(event->fd);
335 if (ret < 0) {
336 switch (errno) {
337 case EEXIST:
338 ret = LTTNG_ERR_KERN_EVENT_EXIST;
339 break;
340 default:
341 PERROR("disable kernel event");
342 break;
343 }
344 goto error;
345 }
346
347 event->enabled = 0;
348 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
349
350 return 0;
351
352 error:
353 return ret;
354 }
355
356 /*
357 * Create kernel metadata, open from the kernel tracer and add it to the
358 * kernel session.
359 */
360 int kernel_open_metadata(struct ltt_kernel_session *session)
361 {
362 int ret;
363 struct ltt_kernel_metadata *lkm = NULL;
364
365 assert(session);
366
367 /* Allocate kernel metadata */
368 lkm = trace_kernel_create_metadata();
369 if (lkm == NULL) {
370 goto error;
371 }
372
373 /* Kernel tracer metadata creation */
374 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
375 if (ret < 0) {
376 goto error_open;
377 }
378
379 lkm->fd = ret;
380 /* Prevent fd duplication after execlp() */
381 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
382 if (ret < 0) {
383 PERROR("fcntl session fd");
384 }
385
386 session->metadata = lkm;
387
388 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
389
390 return 0;
391
392 error_open:
393 trace_kernel_destroy_metadata(lkm);
394 error:
395 return -1;
396 }
397
398 /*
399 * Start tracing session.
400 */
401 int kernel_start_session(struct ltt_kernel_session *session)
402 {
403 int ret;
404
405 assert(session);
406
407 ret = kernctl_start_session(session->fd);
408 if (ret < 0) {
409 PERROR("ioctl start session");
410 goto error;
411 }
412
413 DBG("Kernel session started");
414
415 return 0;
416
417 error:
418 return ret;
419 }
420
421 /*
422 * Make a kernel wait to make sure in-flight probe have completed.
423 */
424 void kernel_wait_quiescent(int fd)
425 {
426 int ret;
427
428 DBG("Kernel quiescent wait on %d", fd);
429
430 ret = kernctl_wait_quiescent(fd);
431 if (ret < 0) {
432 PERROR("wait quiescent ioctl");
433 ERR("Kernel quiescent wait failed");
434 }
435 }
436
437 /*
438 * Kernel calibrate
439 */
440 int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
441 {
442 int ret;
443
444 assert(calibrate);
445
446 ret = kernctl_calibrate(fd, calibrate);
447 if (ret < 0) {
448 PERROR("calibrate ioctl");
449 return -1;
450 }
451
452 return 0;
453 }
454
455
456 /*
457 * Force flush buffer of metadata.
458 */
459 int kernel_metadata_flush_buffer(int fd)
460 {
461 int ret;
462
463 ret = kernctl_buffer_flush(fd);
464 if (ret < 0) {
465 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
466 }
467
468 return 0;
469 }
470
471 /*
472 * Force flush buffer for channel.
473 */
474 int kernel_flush_buffer(struct ltt_kernel_channel *channel)
475 {
476 int ret;
477 struct ltt_kernel_stream *stream;
478
479 assert(channel);
480
481 DBG("Flush buffer for channel %s", channel->channel->name);
482
483 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
484 DBG("Flushing channel stream %d", stream->fd);
485 ret = kernctl_buffer_flush(stream->fd);
486 if (ret < 0) {
487 PERROR("ioctl");
488 ERR("Fail to flush buffer for stream %d (ret: %d)",
489 stream->fd, ret);
490 }
491 }
492
493 return 0;
494 }
495
496 /*
497 * Stop tracing session.
498 */
499 int kernel_stop_session(struct ltt_kernel_session *session)
500 {
501 int ret;
502
503 assert(session);
504
505 ret = kernctl_stop_session(session->fd);
506 if (ret < 0) {
507 goto error;
508 }
509
510 DBG("Kernel session stopped");
511
512 return 0;
513
514 error:
515 return ret;
516 }
517
518 /*
519 * Open stream of channel, register it to the kernel tracer and add it
520 * to the stream list of the channel.
521 *
522 * Return the number of created stream. Else, a negative value.
523 */
524 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
525 {
526 int ret, count = 0;
527 struct ltt_kernel_stream *lks;
528
529 assert(channel);
530
531 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
532 lks = trace_kernel_create_stream(channel->channel->name, count);
533 if (lks == NULL) {
534 ret = close(ret);
535 if (ret) {
536 PERROR("close");
537 }
538 goto error;
539 }
540
541 lks->fd = ret;
542 /* Prevent fd duplication after execlp() */
543 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
544 if (ret < 0) {
545 PERROR("fcntl session fd");
546 }
547
548 lks->tracefile_size = channel->channel->attr.tracefile_size;
549 lks->tracefile_count = channel->channel->attr.tracefile_count;
550
551 /* Add stream to channe stream list */
552 cds_list_add(&lks->list, &channel->stream_list.head);
553 channel->stream_count++;
554
555 /* Increment counter which represent CPU number. */
556 count++;
557
558 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
559 lks->state);
560 }
561
562 return channel->stream_count;
563
564 error:
565 return -1;
566 }
567
568 /*
569 * Open the metadata stream and set it to the kernel session.
570 */
571 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
572 {
573 int ret;
574
575 assert(session);
576
577 ret = kernctl_create_stream(session->metadata->fd);
578 if (ret < 0) {
579 PERROR("kernel create metadata stream");
580 goto error;
581 }
582
583 DBG("Kernel metadata stream created (fd: %d)", ret);
584 session->metadata_stream_fd = ret;
585 /* Prevent fd duplication after execlp() */
586 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
587 if (ret < 0) {
588 PERROR("fcntl session fd");
589 }
590
591 return 0;
592
593 error:
594 return -1;
595 }
596
597 /*
598 * Get the event list from the kernel tracer and return the number of elements.
599 */
600 ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
601 {
602 int fd, pos, ret;
603 char *event;
604 size_t nbmem, count = 0;
605 FILE *fp;
606 struct lttng_event *elist;
607
608 assert(events);
609
610 fd = kernctl_tracepoint_list(tracer_fd);
611 if (fd < 0) {
612 PERROR("kernel tracepoint list");
613 goto error;
614 }
615
616 fp = fdopen(fd, "r");
617 if (fp == NULL) {
618 PERROR("kernel tracepoint list fdopen");
619 goto error_fp;
620 }
621
622 /*
623 * Init memory size counter
624 * See kernel-ctl.h for explanation of this value
625 */
626 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
627 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
628 if (elist == NULL) {
629 PERROR("alloc list events");
630 count = -ENOMEM;
631 goto end;
632 }
633
634 while (fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos) == 1) {
635 if (count >= nbmem) {
636 struct lttng_event *new_elist;
637
638 DBG("Reallocating event list from %zu to %zu bytes", nbmem,
639 nbmem * 2);
640 /* Double the size */
641 nbmem <<= 1;
642 new_elist = realloc(elist, nbmem * sizeof(struct lttng_event));
643 if (new_elist == NULL) {
644 PERROR("realloc list events");
645 free(event);
646 free(elist);
647 count = -ENOMEM;
648 goto end;
649 }
650 elist = new_elist;
651 }
652 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
653 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
654 elist[count].enabled = -1;
655 count++;
656 free(event);
657 }
658
659 *events = elist;
660 DBG("Kernel list events done (%zu events)", count);
661 end:
662 ret = fclose(fp); /* closes both fp and fd */
663 if (ret) {
664 PERROR("fclose");
665 }
666 return count;
667
668 error_fp:
669 ret = close(fd);
670 if (ret) {
671 PERROR("close");
672 }
673 error:
674 return -1;
675 }
676
677 /*
678 * Get kernel version and validate it.
679 */
680 int kernel_validate_version(int tracer_fd)
681 {
682 int ret;
683 struct lttng_kernel_tracer_version version;
684
685 ret = kernctl_tracer_version(tracer_fd, &version);
686 if (ret < 0) {
687 ERR("Failed at getting the lttng-modules version");
688 goto error;
689 }
690
691 /* Validate version */
692 if (version.major != KERN_MODULES_PRE_MAJOR
693 && version.major != KERN_MODULES_MAJOR) {
694 goto error_version;
695 }
696
697 DBG2("Kernel tracer version validated (major version %d)", version.major);
698 return 0;
699
700 error_version:
701 ERR("Kernel major version %d is not compatible (supporting <= %d)",
702 version.major, KERN_MODULES_MAJOR)
703 ret = -1;
704
705 error:
706 return ret;
707 }
708
709 /*
710 * Kernel work-arounds called at the start of sessiond main().
711 */
712 int init_kernel_workarounds(void)
713 {
714 int ret;
715 FILE *fp;
716
717 /*
718 * boot_id needs to be read once before being used concurrently
719 * to deal with a Linux kernel race. A fix is proposed for
720 * upstream, but the work-around is needed for older kernels.
721 */
722 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
723 if (!fp) {
724 goto end_boot_id;
725 }
726 while (!feof(fp)) {
727 char buf[37] = "";
728
729 ret = fread(buf, 1, sizeof(buf), fp);
730 if (ret < 0) {
731 /* Ignore error, we don't really care */
732 }
733 }
734 ret = fclose(fp);
735 if (ret) {
736 PERROR("fclose");
737 }
738 end_boot_id:
739 return 0;
740 }
741
742 /*
743 * Complete teardown of a kernel session.
744 */
745 void kernel_destroy_session(struct ltt_kernel_session *ksess)
746 {
747 if (ksess == NULL) {
748 DBG3("No kernel session when tearing down session");
749 return;
750 }
751
752 DBG("Tearing down kernel session");
753
754 /* Close any relayd session */
755 consumer_output_send_destroy_relayd(ksess->consumer);
756
757 trace_kernel_destroy_session(ksess);
758 }
759
760 /*
761 * Destroy a kernel channel object. It does not do anything on the tracer side.
762 */
763 void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
764 {
765 struct ltt_kernel_session *ksess = NULL;
766
767 assert(kchan);
768 assert(kchan->channel);
769
770 DBG3("Kernel destroy channel %s", kchan->channel->name);
771
772 /* Update channel count of associated session. */
773 if (kchan->session) {
774 /* Keep pointer reference so we can update it after the destroy. */
775 ksess = kchan->session;
776 }
777
778 trace_kernel_destroy_channel(kchan);
779
780 /*
781 * At this point the kernel channel is not visible anymore. This is safe
782 * since in order to work on a visible kernel session, the tracing session
783 * lock (ltt_session.lock) MUST be acquired.
784 */
785 if (ksess) {
786 ksess->channel_count--;
787 }
788 }
This page took 0.044859 seconds and 5 git commands to generate.