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