Add kernel module version validation
[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
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
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
19 #define _GNU_SOURCE
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <common/common.h>
28 #include <common/kernel-ctl/kernel-ctl.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. However
208 * this FD must not be added to the event list.
209 */
210 if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) {
211 DBG2("Kernel event syscall creation success");
212 goto end;
213 }
214
215 event->fd = ret;
216 /* Prevent fd duplication after execlp() */
217 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
218 if (ret < 0) {
219 perror("fcntl session fd");
220 }
221
222 /* Add event to event list */
223 cds_list_add(&event->list, &channel->events_list.head);
224 channel->event_count++;
225
226 DBG("Event %s created (fd: %d)", ev->name, event->fd);
227
228 end:
229 return 0;
230
231 free_event:
232 free(event);
233 error:
234 return ret;
235 }
236
237 /*
238 * Disable a kernel channel.
239 */
240 int kernel_disable_channel(struct ltt_kernel_channel *chan)
241 {
242 int ret;
243
244 ret = kernctl_disable(chan->fd);
245 if (ret < 0) {
246 perror("disable chan ioctl");
247 ret = errno;
248 goto error;
249 }
250
251 chan->enabled = 0;
252 DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd);
253
254 return 0;
255
256 error:
257 return ret;
258 }
259
260 /*
261 * Enable a kernel channel.
262 */
263 int kernel_enable_channel(struct ltt_kernel_channel *chan)
264 {
265 int ret;
266
267 ret = kernctl_enable(chan->fd);
268 if (ret < 0 && errno != EEXIST) {
269 perror("Enable kernel chan");
270 goto error;
271 }
272
273 chan->enabled = 1;
274 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
275
276 return 0;
277
278 error:
279 return ret;
280 }
281
282 /*
283 * Enable a kernel event.
284 */
285 int kernel_enable_event(struct ltt_kernel_event *event)
286 {
287 int ret;
288
289 ret = kernctl_enable(event->fd);
290 if (ret < 0 && errno != EEXIST) {
291 perror("enable kernel event");
292 goto error;
293 }
294
295 event->enabled = 1;
296 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
297
298 return 0;
299
300 error:
301 return ret;
302 }
303
304 /*
305 * Disable a kernel event.
306 */
307 int kernel_disable_event(struct ltt_kernel_event *event)
308 {
309 int ret;
310
311 ret = kernctl_disable(event->fd);
312 if (ret < 0 && errno != EEXIST) {
313 perror("disable kernel event");
314 goto error;
315 }
316
317 event->enabled = 0;
318 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
319
320 return 0;
321
322 error:
323 return ret;
324 }
325
326 /*
327 * Create kernel metadata, open from the kernel tracer and add it to the
328 * kernel session.
329 */
330 int kernel_open_metadata(struct ltt_kernel_session *session, char *path)
331 {
332 int ret;
333 struct ltt_kernel_metadata *lkm;
334
335 /* Allocate kernel metadata */
336 lkm = trace_kernel_create_metadata(path);
337 if (lkm == NULL) {
338 goto error;
339 }
340
341 /* Kernel tracer metadata creation */
342 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
343 if (ret < 0) {
344 goto error;
345 }
346
347 lkm->fd = ret;
348 /* Prevent fd duplication after execlp() */
349 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
350 if (ret < 0) {
351 perror("fcntl session fd");
352 }
353
354 session->metadata = lkm;
355
356 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname);
357
358 return 0;
359
360 error:
361 return -1;
362 }
363
364 /*
365 * Start tracing session.
366 */
367 int kernel_start_session(struct ltt_kernel_session *session)
368 {
369 int ret;
370
371 ret = kernctl_start_session(session->fd);
372 if (ret < 0) {
373 perror("ioctl start session");
374 goto error;
375 }
376
377 DBG("Kernel session started");
378
379 return 0;
380
381 error:
382 return ret;
383 }
384
385 /*
386 * Make a kernel wait to make sure in-flight probe have completed.
387 */
388 void kernel_wait_quiescent(int fd)
389 {
390 int ret;
391
392 DBG("Kernel quiescent wait on %d", fd);
393
394 ret = kernctl_wait_quiescent(fd);
395 if (ret < 0) {
396 perror("wait quiescent ioctl");
397 ERR("Kernel quiescent wait failed");
398 }
399 }
400
401 /*
402 * Kernel calibrate
403 */
404 int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
405 {
406 int ret;
407
408 ret = kernctl_calibrate(fd, calibrate);
409 if (ret < 0) {
410 perror("calibrate ioctl");
411 return -1;
412 }
413
414 return 0;
415 }
416
417
418 /*
419 * Force flush buffer of metadata.
420 */
421 int kernel_metadata_flush_buffer(int fd)
422 {
423 int ret;
424
425 ret = kernctl_buffer_flush(fd);
426 if (ret < 0) {
427 ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret);
428 }
429
430 return 0;
431 }
432
433 /*
434 * Force flush buffer for channel.
435 */
436 int kernel_flush_buffer(struct ltt_kernel_channel *channel)
437 {
438 int ret;
439 struct ltt_kernel_stream *stream;
440
441 DBG("Flush buffer for channel %s", channel->channel->name);
442
443 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
444 DBG("Flushing channel stream %d", stream->fd);
445 ret = kernctl_buffer_flush(stream->fd);
446 if (ret < 0) {
447 perror("ioctl");
448 ERR("Fail to flush buffer for stream %d (ret: %d)",
449 stream->fd, ret);
450 }
451 }
452
453 return 0;
454 }
455
456 /*
457 * Stop tracing session.
458 */
459 int kernel_stop_session(struct ltt_kernel_session *session)
460 {
461 int ret;
462
463 ret = kernctl_stop_session(session->fd);
464 if (ret < 0) {
465 goto error;
466 }
467
468 DBG("Kernel session stopped");
469
470 return 0;
471
472 error:
473 return ret;
474 }
475
476 /*
477 * Open stream of channel, register it to the kernel tracer and add it
478 * to the stream list of the channel.
479 *
480 * Return the number of created stream. Else, a negative value.
481 */
482 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
483 {
484 int ret;
485 struct ltt_kernel_stream *lks;
486
487 while ((ret = kernctl_create_stream(channel->fd)) > 0) {
488 lks = trace_kernel_create_stream();
489 if (lks == NULL) {
490 close(ret);
491 goto error;
492 }
493
494 lks->fd = ret;
495 /* Prevent fd duplication after execlp() */
496 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
497 if (ret < 0) {
498 perror("fcntl session fd");
499 }
500
501 ret = asprintf(&lks->pathname, "%s/%s_%d",
502 channel->pathname, channel->channel->name, channel->stream_count);
503 if (ret < 0) {
504 perror("asprintf kernel create stream");
505 goto error;
506 }
507
508 /* Add stream to channe stream list */
509 cds_list_add(&lks->list, &channel->stream_list.head);
510 channel->stream_count++;
511
512 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
513 channel->stream_count, lks->fd, lks->state, lks->pathname);
514 }
515
516 return channel->stream_count;
517
518 error:
519 return -1;
520 }
521
522 /*
523 * Open the metadata stream and set it to the kernel session.
524 */
525 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
526 {
527 int ret;
528
529 ret = kernctl_create_stream(session->metadata->fd);
530 if (ret < 0) {
531 perror("kernel create metadata stream");
532 goto error;
533 }
534
535 DBG("Kernel metadata stream created (fd: %d)", ret);
536 session->metadata_stream_fd = ret;
537 /* Prevent fd duplication after execlp() */
538 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
539 if (ret < 0) {
540 perror("fcntl session fd");
541 }
542
543 return 0;
544
545 error:
546 return -1;
547 }
548
549 /*
550 * Get the event list from the kernel tracer and return the number of elements.
551 */
552 ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
553 {
554 int fd, pos;
555 char *event;
556 size_t nbmem, count = 0;
557 ssize_t size;
558 FILE *fp;
559 struct lttng_event *elist;
560
561 fd = kernctl_tracepoint_list(tracer_fd);
562 if (fd < 0) {
563 perror("kernel tracepoint list");
564 goto error;
565 }
566
567 fp = fdopen(fd, "r");
568 if (fp == NULL) {
569 perror("kernel tracepoint list fdopen");
570 goto error_fp;
571 }
572
573 /*
574 * Init memory size counter
575 * See kernel-ctl.h for explanation of this value
576 */
577 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
578 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
579
580 while ((size = fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
581 if (count >= nbmem) {
582 DBG("Reallocating event list from %zu to %zu bytes", nbmem,
583 nbmem * 2);
584 /* Double the size */
585 nbmem <<= 1;
586 elist = realloc(elist, nbmem * sizeof(struct lttng_event));
587 if (elist == NULL) {
588 perror("realloc list events");
589 count = -ENOMEM;
590 goto end;
591 }
592 }
593 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
594 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
595 elist[count].enabled = -1;
596 count++;
597 }
598
599 *events = elist;
600 DBG("Kernel list events done (%zu events)", count);
601 end:
602 fclose(fp); /* closes both fp and fd */
603 return count;
604
605 error_fp:
606 close(fd);
607 error:
608 return -1;
609 }
610
611 /*
612 * Get kernel version and validate it.
613 */
614 int kernel_validate_version(int tracer_fd)
615 {
616 int ret;
617 struct lttng_kernel_tracer_version version;
618
619 ret = kernctl_tracer_version(tracer_fd, &version);
620 if (ret < 0) {
621 ERR("Failed at getting the lttng-modules version");
622 goto error;
623 }
624
625 /* Validate version */
626 if (version.version > KERN_MODULES_VERSION) {
627 goto error_version;
628 } else {
629 if (version.patchlevel > KERN_MODULES_PATCHLEVEL) {
630 goto error_version;
631 }
632 else {
633 if (version.sublevel > KERN_MODULES_SUBLEVEL) {
634 goto error_version;
635 }
636 }
637 }
638
639 DBG2("Kernel tracer version validated (%d.%d.%d)", version.version,
640 version.patchlevel, version.sublevel);
641
642 return 0;
643
644 error_version:
645 ERR("Kernel version is not compatible %d.%d.%d (supporting <= %d.%d.%d)",
646 version.version, version.patchlevel, version.sublevel,
647 KERN_MODULES_VERSION, KERN_MODULES_PATCHLEVEL,
648 KERN_MODULES_SUBLEVEL);
649 ret = -1;
650
651 error:
652 return ret;
653 }
This page took 0.047548 seconds and 4 git commands to generate.