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