Fix: large audit of close() use in sessiond main.c
[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
DG
44 if (errno != EEXIST) {
45 perror("add context ioctl");
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) {
ba7f0ae5 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) {
78 perror("add context ioctl");
79 goto error;
80 }
81
ba7f0ae5 82 event->ctx = zmalloc(sizeof(struct lttng_kernel_context));
d65106b1 83 if (event->ctx == NULL) {
ba7f0ae5 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) {
54012638 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) {
123 perror("fcntl session fd");
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) {
54012638 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) {
165 perror("fcntl session fd");
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) {
223 perror("fcntl session fd");
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) {
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
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
DG
272 if (ret < 0 && errno != EEXIST) {
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
DG
294 if (ret < 0 && errno != EEXIST) {
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
DG
316 if (ret < 0 && errno != EEXIST) {
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) {
355 perror("fcntl session fd");
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) {
f3ed775e 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) {
400 perror("wait quiescent ioctl");
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) {
414 perror("calibrate ioctl");
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) {
451 perror("ioctl");
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) {
54012638 494 close(ret);
8c0faa1d
DG
495 goto error;
496 }
497
498 lks->fd = ret;
7b395890
DG
499 /* Prevent fd duplication after execlp() */
500 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
501 if (ret < 0) {
502 perror("fcntl session fd");
503 }
504
8e68d1c8
DG
505 ret = asprintf(&lks->pathname, "%s/%s_%d",
506 channel->pathname, channel->channel->name, channel->stream_count);
8c0faa1d
DG
507 if (ret < 0) {
508 perror("asprintf kernel create stream");
509 goto error;
510 }
8c0faa1d 511
54012638 512 /* Add stream to channe stream list */
8c0faa1d
DG
513 cds_list_add(&lks->list, &channel->stream_list.head);
514 channel->stream_count++;
8c0faa1d 515
54012638
DG
516 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
517 channel->stream_count, lks->fd, lks->state, lks->pathname);
518 }
8c0faa1d
DG
519
520 return channel->stream_count;
521
522error:
54012638 523 return -1;
8c0faa1d
DG
524}
525
526/*
050349bb 527 * Open the metadata stream and set it to the kernel session.
8c0faa1d 528 */
f3ed775e 529int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
530{
531 int ret;
532
533 ret = kernctl_create_stream(session->metadata->fd);
534 if (ret < 0) {
535 perror("kernel create metadata stream");
8c0faa1d
DG
536 goto error;
537 }
538
539 DBG("Kernel metadata stream created (fd: %d)", ret);
540 session->metadata_stream_fd = ret;
7b395890
DG
541 /* Prevent fd duplication after execlp() */
542 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
543 if (ret < 0) {
544 perror("fcntl session fd");
545 }
aaf26714
DG
546
547 return 0;
548
549error:
54012638 550 return -1;
aaf26714 551}
2ef84c95
DG
552
553/*
9f19cc17 554 * Get the event list from the kernel tracer and return the number of elements.
2ef84c95 555 */
9f19cc17 556ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
2ef84c95 557{
9f19cc17
DG
558 int fd, pos;
559 char *event;
560 size_t nbmem, count = 0;
2ef84c95
DG
561 ssize_t size;
562 FILE *fp;
9f19cc17 563 struct lttng_event *elist;
2ef84c95
DG
564
565 fd = kernctl_tracepoint_list(tracer_fd);
566 if (fd < 0) {
567 perror("kernel tracepoint list");
568 goto error;
569 }
570
571 fp = fdopen(fd, "r");
572 if (fp == NULL) {
573 perror("kernel tracepoint list fdopen");
61b73b12 574 goto error_fp;
2ef84c95
DG
575 }
576
577 /*
578 * Init memory size counter
579 * See kernel-ctl.h for explanation of this value
580 */
6725fe19 581 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
ba7f0ae5 582 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
2ef84c95 583
9f19cc17 584 while ((size = fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
6725fe19 585 if (count >= nbmem) {
ced2f820 586 DBG("Reallocating event list from %zu to %zu bytes", nbmem,
6725fe19
DG
587 nbmem * 2);
588 /* Double the size */
589 nbmem <<= 1;
2f221590 590 elist = realloc(elist, nbmem * sizeof(struct lttng_event));
9f19cc17 591 if (elist == NULL) {
2ef84c95 592 perror("realloc list events");
61b73b12
MD
593 count = -ENOMEM;
594 goto end;
2ef84c95
DG
595 }
596 }
99497cd0
MD
597 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
598 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
67b9d018 599 elist[count].enabled = -1;
9f19cc17 600 count++;
2ef84c95
DG
601 }
602
9f19cc17 603 *events = elist;
ced2f820 604 DBG("Kernel list events done (%zu events)", count);
61b73b12
MD
605end:
606 fclose(fp); /* closes both fp and fd */
9f19cc17 607 return count;
2ef84c95 608
61b73b12
MD
609error_fp:
610 close(fd);
2ef84c95
DG
611error:
612 return -1;
613}
096102bd
DG
614
615/*
616 * Get kernel version and validate it.
617 */
618int kernel_validate_version(int tracer_fd)
619{
620 int ret;
621 struct lttng_kernel_tracer_version version;
622
623 ret = kernctl_tracer_version(tracer_fd, &version);
624 if (ret < 0) {
625 ERR("Failed at getting the lttng-modules version");
626 goto error;
627 }
628
629 /* Validate version */
a62a6556
MD
630 if (version.major != KERN_MODULES_PRE_MAJOR
631 && version.major != KERN_MODULES_MAJOR) {
096102bd 632 goto error_version;
096102bd
DG
633 }
634
a62a6556 635 DBG2("Kernel tracer version validated (major version %d)", version.major);
096102bd
DG
636 return 0;
637
638error_version:
5df0f285 639 ERR("Kernel major version %d is not compatible (supporting <= %d)",
a62a6556 640 version.major, KERN_MODULES_MAJOR)
096102bd
DG
641 ret = -1;
642
643error:
644 return ret;
645}
335a95b7
MD
646
647/*
648 * Kernel work-arounds called at the start of sessiond main().
649 */
650int init_kernel_workarounds(void)
651{
8936c33a 652 int ret;
335a95b7
MD
653 FILE *fp;
654
655 /*
656 * boot_id needs to be read once before being used concurrently
657 * to deal with a Linux kernel race. A fix is proposed for
658 * upstream, but the work-around is needed for older kernels.
659 */
660 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
661 if (!fp) {
662 goto end_boot_id;
663 }
664 while (!feof(fp)) {
665 char buf[37] = "";
666
8936c33a
DG
667 ret = fread(buf, 1, sizeof(buf), fp);
668 if (ret < 0) {
669 /* Ignore error, we don't really care */
670 }
335a95b7
MD
671 }
672 fclose(fp);
673end_boot_id:
674
675 return 0;
676}
This page took 0.09648 seconds and 4 git commands to generate.