Fix: sessiond: crash when sending data_pending to an active session
[lttng-tools.git] / src / bin / lttng-sessiond / modprobe.cpp
1 /*
2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2014 Jan Glauber <jan.glauber@gmail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 /**
10 * @file modprobe.c
11 *
12 * @brief modprobe related functions.
13 *
14 */
15
16 #define _LGPL_SOURCE
17 #include "kern-modules.hpp"
18 #include "lttng-sessiond.hpp"
19 #include "modprobe.hpp"
20
21 #include <common/common.hpp>
22 #include <common/utils.hpp>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/wait.h>
27
28 /* LTTng kernel tracer mandatory core modules list */
29 struct kern_modules_param kern_modules_control_core[] = {
30 {
31 .name = (char *) "lttng-ring-buffer-client-discard",
32 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED,
33 .loaded = false,
34 },
35 {
36 .name = (char *) "lttng-ring-buffer-client-overwrite",
37 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED,
38 .loaded = false,
39 },
40 {
41 .name = (char *) "lttng-ring-buffer-metadata-client",
42 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED,
43 .loaded = false,
44 },
45 {
46 .name = (char *) "lttng-ring-buffer-client-mmap-discard",
47 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED,
48 .loaded = false,
49 },
50 {
51 .name = (char *) "lttng-ring-buffer-client-mmap-overwrite",
52 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED,
53 .loaded = false,
54 },
55 {
56 .name = (char *) "lttng-ring-buffer-metadata-mmap-client",
57 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED,
58 .loaded = false,
59 },
60 {
61 .name = (char *) "lttng-ring-buffer-event_notifier-client",
62 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
63 .loaded = false,
64 },
65 {
66 .name = (char *) "lttng-counter-client-percpu-64-modular",
67 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
68 .loaded = false,
69 },
70 {
71 .name = (char *) "lttng-counter-client-percpu-32-modular",
72 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
73 .loaded = false,
74 },
75 };
76
77 /* LTTng kerneltracer probe modules list */
78 struct kern_modules_param kern_modules_probes_default[] = {
79 {
80 .name = (char *) "lttng-probe-asoc",
81 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
82 .loaded = false,
83 },
84 {
85 .name = (char *) "lttng-probe-block",
86 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
87 .loaded = false,
88 },
89 {
90 .name = (char *) "lttng-probe-btrfs",
91 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
92 .loaded = false,
93 },
94 {
95 .name = (char *) "lttng-probe-compaction",
96 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
97 .loaded = false,
98 },
99 {
100 .name = (char *) "lttng-probe-ext3",
101 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
102 .loaded = false,
103 },
104 {
105 .name = (char *) "lttng-probe-ext4",
106 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
107 .loaded = false,
108 },
109 {
110 .name = (char *) "lttng-probe-gpio",
111 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
112 .loaded = false,
113 },
114 {
115 .name = (char *) "lttng-probe-i2c",
116 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
117 .loaded = false,
118 },
119 {
120 .name = (char *) "lttng-probe-irq",
121 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
122 .loaded = false,
123 },
124 {
125 .name = (char *) "lttng-probe-jbd",
126 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
127 .loaded = false,
128 },
129 {
130 .name = (char *) "lttng-probe-jbd2",
131 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
132 .loaded = false,
133 },
134 {
135 .name = (char *) "lttng-probe-kmem",
136 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
137 .loaded = false,
138 },
139 {
140 .name = (char *) "lttng-probe-kvm",
141 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
142 .loaded = false,
143 },
144 {
145 .name = (char *) "lttng-probe-kvm-x86",
146 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
147 .loaded = false,
148 },
149 {
150 .name = (char *) "lttng-probe-kvm-x86-mmu",
151 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
152 .loaded = false,
153 },
154 {
155 .name = (char *) "lttng-probe-lock",
156 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
157 .loaded = false,
158 },
159 {
160 .name = (char *) "lttng-probe-module",
161 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
162 .loaded = false,
163 },
164 {
165 .name = (char *) "lttng-probe-napi",
166 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
167 .loaded = false,
168 },
169 {
170 .name = (char *) "lttng-probe-net",
171 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
172 .loaded = false,
173 },
174 {
175 .name = (char *) "lttng-probe-power",
176 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
177 .loaded = false,
178 },
179 {
180 .name = (char *) "lttng-probe-preemptirq",
181 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
182 .loaded = false,
183 },
184 {
185 .name = (char *) "lttng-probe-printk",
186 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
187 .loaded = false,
188 },
189 {
190 .name = (char *) "lttng-probe-random",
191 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
192 .loaded = false,
193 },
194 {
195 .name = (char *) "lttng-probe-rcu",
196 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
197 .loaded = false,
198 },
199 {
200 .name = (char *) "lttng-probe-regmap",
201 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
202 .loaded = false,
203 },
204 {
205 .name = (char *) "lttng-probe-regulator",
206 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
207 .loaded = false,
208 },
209 {
210 .name = (char *) "lttng-probe-rpm",
211 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
212 .loaded = false,
213 },
214 {
215 .name = (char *) "lttng-probe-sched",
216 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
217 .loaded = false,
218 },
219 {
220 .name = (char *) "lttng-probe-scsi",
221 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
222 .loaded = false,
223 },
224 {
225 .name = (char *) "lttng-probe-signal",
226 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
227 .loaded = false,
228 },
229 {
230 .name = (char *) "lttng-probe-skb",
231 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
232 .loaded = false,
233 },
234 {
235 .name = (char *) "lttng-probe-sock",
236 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
237 .loaded = false,
238 },
239 {
240 .name = (char *) "lttng-probe-statedump",
241 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
242 .loaded = false,
243 },
244 {
245 .name = (char *) "lttng-probe-sunrpc",
246 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
247 .loaded = false,
248 },
249 {
250 .name = (char *) "lttng-probe-timer",
251 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
252 .loaded = false,
253 },
254 {
255 .name = (char *) "lttng-probe-udp",
256 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
257 .loaded = false,
258 },
259 {
260 .name = (char *) "lttng-probe-vmscan",
261 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
262 .loaded = false,
263 },
264 {
265 .name = (char *) "lttng-probe-v4l2",
266 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
267 .loaded = false,
268 },
269 {
270 .name = (char *) "lttng-probe-workqueue",
271 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
272 .loaded = false,
273 },
274 {
275 .name = (char *) "lttng-probe-writeback",
276 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
277 .loaded = false,
278 },
279 {
280 .name = (char *) "lttng-probe-x86-irq-vectors",
281 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
282 .loaded = false,
283 },
284 {
285 .name = (char *) "lttng-probe-x86-exceptions",
286 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
287 .loaded = false,
288 },
289 };
290
291 /* dynamic probe modules list */
292 static struct kern_modules_param *probes;
293 static int nr_probes;
294 static int probes_capacity;
295
296 #ifdef HAVE_KMOD
297 #include <libkmod.h>
298
299 /**
300 * @brief Logging function for libkmod integration.
301 */
302 static ATTR_FORMAT_PRINTF(6, 0) void log_kmod(void *data __attribute__((unused)),
303 int priority __attribute__((unused)),
304 const char *file __attribute__((unused)),
305 int line __attribute__((unused)),
306 const char *fn __attribute__((unused)),
307 const char *format,
308 va_list args)
309 {
310 char *str;
311
312 if (vasprintf(&str, format, args) < 0) {
313 return;
314 }
315
316 DBG("libkmod: %s", str);
317 free(str);
318 }
319
320 /**
321 * @brief Setup the libkmod context.
322 *
323 * Create the context, add a custom logging function and preload the
324 * ressources for faster operation.
325 *
326 * @returns \c 0 on success
327 * \c < 0 on error
328 */
329 static int setup_kmod_ctx(struct kmod_ctx **ctx)
330 {
331 int ret = 0;
332
333 *ctx = kmod_new(nullptr, nullptr);
334 if (!ctx) {
335 PERROR("Unable to create kmod library context");
336 ret = -ENOMEM;
337 goto error;
338 }
339
340 /*
341 * Parameter 2 of kmod_set_log_fn generates a
342 * -Wsuggest-attribute=formatkmod_set_log_fn warning that we can't fix,
343 * ignore it.
344 */
345 DIAGNOSTIC_PUSH
346 DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
347 kmod_set_log_fn(*ctx, log_kmod, nullptr);
348 DIAGNOSTIC_POP
349 ret = kmod_load_resources(*ctx);
350 if (ret < 0) {
351 ERR("Failed to load kmod library resources");
352 goto error;
353 }
354
355 error:
356 return ret;
357 }
358
359 /**
360 * @brief Loads the kernel modules in \p modules
361 *
362 * @param modules List of modules to load
363 * @param entries Number of modules in the list
364 *
365 * If the modules are required, we will return with error after the
366 * first failed module load, otherwise we continue loading.
367 *
368 * @returns \c 0 on success
369 * \c < 0 on error
370 */
371 static int modprobe_lttng(struct kern_modules_param *modules, int entries)
372 {
373 int ret = 0, i;
374 struct kmod_ctx *ctx;
375
376 ret = setup_kmod_ctx(&ctx);
377 if (ret < 0) {
378 goto error;
379 }
380
381 for (i = 0; i < entries; i++) {
382 struct kmod_module *mod = nullptr;
383
384 ret = kmod_module_new_from_name(ctx, modules[i].name, &mod);
385 if (ret < 0) {
386 PERROR("Failed to create kmod module for %s", modules[i].name);
387 goto error;
388 }
389
390 ret = kmod_module_probe_insert_module(mod, 0, nullptr, nullptr, nullptr, nullptr);
391 if (ret == -EEXIST) {
392 DBG("Module %s is already loaded", modules[i].name);
393 ret = 0;
394 } else if (ret < 0) {
395 if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) {
396 ERR("Unable to load required module %s", modules[i].name);
397 goto error;
398 } else {
399 DBG("Unable to load optional module %s; continuing",
400 modules[i].name);
401 ret = 0;
402 }
403 } else {
404 DBG("Modprobe successfully %s", modules[i].name);
405 modules[i].loaded = true;
406 }
407
408 kmod_module_unref(mod);
409 }
410
411 error:
412 if (ctx) {
413 kmod_unref(ctx);
414 }
415 return ret;
416 }
417
418 /**
419 * @brief Recursively unload modules.
420 *
421 * This function implements the same modules unloading behavior as
422 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
423 * dependencies and unload modules with a refcount of 0.
424 *
425 * @param mod The module to unload
426 *
427 * @returns \c 0 on success
428 * \c < 0 on error
429 */
430 static int rmmod_recurse(struct kmod_module *mod)
431 {
432 int ret = 0;
433 struct kmod_list *deps, *itr;
434
435 if (kmod_module_get_initstate(mod) == KMOD_MODULE_BUILTIN) {
436 DBG("Module %s is builtin", kmod_module_get_name(mod));
437 return ret;
438 }
439
440 ret = kmod_module_remove_module(mod, 0);
441
442 deps = kmod_module_get_dependencies(mod);
443 if (deps != nullptr) {
444 kmod_list_foreach(itr, deps)
445 {
446 struct kmod_module *dep = kmod_module_get_module(itr);
447 if (kmod_module_get_refcnt(dep) == 0) {
448 DBG("Recursive remove module %s", kmod_module_get_name(dep));
449 rmmod_recurse(dep);
450 }
451 kmod_module_unref(dep);
452 }
453 kmod_module_unref_list(deps);
454 }
455
456 return ret;
457 }
458
459 /**
460 * @brief Unloads the kernel modules in \p modules
461 *
462 * @param modules List of modules to unload
463 * @param entries Number of modules in the list
464 *
465 */
466 static void modprobe_remove_lttng(const struct kern_modules_param *modules, int entries)
467 {
468 int ret = 0, i;
469 struct kmod_ctx *ctx;
470
471 ret = setup_kmod_ctx(&ctx);
472 if (ret < 0) {
473 goto error;
474 }
475
476 for (i = entries - 1; i >= 0; i--) {
477 struct kmod_module *mod = nullptr;
478
479 if (!modules[i].loaded) {
480 continue;
481 }
482
483 ret = kmod_module_new_from_name(ctx, modules[i].name, &mod);
484 if (ret < 0) {
485 PERROR("Failed to create kmod module for %s", modules[i].name);
486 goto error;
487 }
488
489 ret = rmmod_recurse(mod);
490 if (ret == -EEXIST) {
491 DBG("Module %s is not in kernel.", modules[i].name);
492 } else if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED &&
493 ret < 0) {
494 ERR("Unable to remove module %s", modules[i].name);
495 } else {
496 DBG("Modprobe removal successful %s", modules[i].name);
497 }
498
499 kmod_module_unref(mod);
500 }
501
502 error:
503 if (ctx) {
504 kmod_unref(ctx);
505 }
506 }
507
508 #else /* HAVE_KMOD */
509
510 static int modprobe_lttng(struct kern_modules_param *modules, int entries)
511 {
512 int ret = 0, i;
513 char modprobe[256];
514
515 for (i = 0; i < entries; i++) {
516 ret = snprintf(modprobe,
517 sizeof(modprobe),
518 "/sbin/modprobe %s%s",
519 modules[i].load_policy ==
520 KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED ?
521 "" :
522 "-q ",
523 modules[i].name);
524 if (ret < 0) {
525 PERROR("snprintf modprobe");
526 goto error;
527 }
528 modprobe[sizeof(modprobe) - 1] = '\0';
529 ret = system(modprobe);
530 if (ret == -1) {
531 if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) {
532 ERR("Unable to launch modprobe for required module %s",
533 modules[i].name);
534 goto error;
535 } else {
536 DBG("Unable to launch modprobe for optional module %s; continuing",
537 modules[i].name);
538 ret = 0;
539 }
540 } else if (WEXITSTATUS(ret) != 0) {
541 if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) {
542 ERR("Unable to load required module %s", modules[i].name);
543 goto error;
544 } else {
545 DBG("Unable to load optional module %s; continuing",
546 modules[i].name);
547 ret = 0;
548 }
549 } else {
550 DBG("Modprobe successfully %s", modules[i].name);
551 modules[i].loaded = true;
552 }
553 }
554
555 error:
556 return ret;
557 }
558
559 static void modprobe_remove_lttng(const struct kern_modules_param *modules, int entries)
560 {
561 int ret = 0, i;
562 char modprobe[256];
563
564 for (i = entries - 1; i >= 0; i--) {
565 if (!modules[i].loaded) {
566 continue;
567 }
568 ret = snprintf(
569 modprobe, sizeof(modprobe), "/sbin/modprobe -r -q %s", modules[i].name);
570 if (ret < 0) {
571 PERROR("snprintf modprobe -r");
572 return;
573 }
574 modprobe[sizeof(modprobe) - 1] = '\0';
575 ret = system(modprobe);
576 if (ret == -1) {
577 if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) {
578 ERR("Unable to launch modprobe -r for required module %s",
579 modules[i].name);
580 } else {
581 DBG("Unable to launch modprobe -r for optional module %s",
582 modules[i].name);
583 }
584 } else if (WEXITSTATUS(ret) != 0) {
585 if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) {
586 ERR("Unable to remove required module %s", modules[i].name);
587 } else {
588 DBG("Unable to remove optional module %s", modules[i].name);
589 }
590 } else {
591 DBG("Modprobe removal successful %s", modules[i].name);
592 }
593 }
594 }
595
596 #endif /* HAVE_KMOD */
597
598 /*
599 * Remove control kernel module(s) in reverse load order.
600 */
601 void modprobe_remove_lttng_control()
602 {
603 modprobe_remove_lttng(kern_modules_control_core, ARRAY_SIZE(kern_modules_control_core));
604 }
605
606 static void free_probes()
607 {
608 int i;
609
610 if (!probes) {
611 return;
612 }
613 for (i = 0; i < nr_probes; ++i) {
614 free(probes[i].name);
615 }
616 free(probes);
617 probes = nullptr;
618 nr_probes = 0;
619 }
620
621 /*
622 * Remove data kernel modules in reverse load order.
623 */
624 void modprobe_remove_lttng_data()
625 {
626 if (!probes) {
627 return;
628 }
629
630 modprobe_remove_lttng(probes, nr_probes);
631 free_probes();
632 }
633
634 /*
635 * Remove all kernel modules in reverse order.
636 */
637 void modprobe_remove_lttng_all()
638 {
639 modprobe_remove_lttng_data();
640 modprobe_remove_lttng_control();
641 }
642
643 /*
644 * Load control kernel module(s).
645 */
646 int modprobe_lttng_control()
647 {
648 return modprobe_lttng(kern_modules_control_core, ARRAY_SIZE(kern_modules_control_core));
649 }
650
651 /**
652 * Grow global list of probes (double capacity or set it to 1 if
653 * currently 0 and copy existing data).
654 */
655 static int grow_probes()
656 {
657 int i;
658 struct kern_modules_param *tmp_probes;
659
660 /* Initialize capacity to 1 if 0. */
661 if (probes_capacity == 0) {
662 probes = zmalloc<kern_modules_param>();
663 if (!probes) {
664 PERROR("malloc probe list");
665 return -ENOMEM;
666 }
667
668 probes_capacity = 1;
669 return 0;
670 }
671
672 /* Double size. */
673 probes_capacity *= 2;
674
675 tmp_probes = calloc<kern_modules_param>(probes_capacity);
676 if (!tmp_probes) {
677 PERROR("malloc probe list");
678 return -ENOMEM;
679 }
680
681 for (i = 0; i < nr_probes; ++i) {
682 /* Ownership of 'name' field is transferred. */
683 tmp_probes[i] = probes[i];
684 }
685
686 /* Replace probes with larger copy. */
687 free(probes);
688 probes = tmp_probes;
689
690 return 0;
691 }
692
693 /*
694 * Appends a comma-separated list of probes to the global list
695 * of probes.
696 */
697 static int append_list_to_probes(const char *list)
698 {
699 char *next;
700 int ret;
701 char *tmp_list, *cur_list, *saveptr;
702
703 LTTNG_ASSERT(list);
704
705 cur_list = tmp_list = strdup(list);
706 if (!tmp_list) {
707 PERROR("strdup temp list");
708 return -ENOMEM;
709 }
710
711 for (;;) {
712 size_t name_len;
713 struct kern_modules_param *cur_mod;
714
715 next = strtok_r(cur_list, ",", &saveptr);
716 if (!next) {
717 break;
718 }
719 cur_list = nullptr;
720
721 /* filter leading spaces */
722 while (*next == ' ') {
723 next++;
724 }
725
726 if (probes_capacity <= nr_probes) {
727 ret = grow_probes();
728 if (ret) {
729 goto error;
730 }
731 }
732
733 /* Length 13 is "lttng-probe-" + \0 */
734 name_len = strlen(next) + 13;
735
736 cur_mod = &probes[nr_probes];
737 cur_mod->name = calloc<char>(name_len);
738 if (!cur_mod->name) {
739 PERROR("malloc probe list");
740 ret = -ENOMEM;
741 goto error;
742 }
743
744 ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next);
745 if (ret < 0) {
746 PERROR("snprintf modprobe name");
747 ret = -ENOMEM;
748 goto error;
749 }
750
751 cur_mod->load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL;
752
753 nr_probes++;
754 }
755
756 free(tmp_list);
757 return 0;
758
759 error:
760 free(tmp_list);
761 free_probes();
762 return ret;
763 }
764
765 /*
766 * Load data kernel module(s).
767 */
768 int modprobe_lttng_data()
769 {
770 int ret, i;
771 char *list;
772
773 /*
774 * Base probes: either from command line option, environment
775 * variable or default list.
776 */
777 list = the_config.kmod_probes_list.value;
778 if (list) {
779 /* User-specified probes. */
780 ret = append_list_to_probes(list);
781 if (ret) {
782 return ret;
783 }
784 } else {
785 /* Default probes. */
786 int def_len = ARRAY_SIZE(kern_modules_probes_default);
787
788 probes = calloc<kern_modules_param>(def_len);
789 if (!probes) {
790 PERROR("malloc probe list");
791 return -ENOMEM;
792 }
793
794 nr_probes = probes_capacity = def_len;
795
796 for (i = 0; i < def_len; ++i) {
797 char *name = strdup(kern_modules_probes_default[i].name);
798
799 if (!name) {
800 PERROR("strdup probe item");
801 ret = -ENOMEM;
802 goto error;
803 }
804
805 probes[i].name = name;
806 probes[i].load_policy = kern_modules_probes_default[i].load_policy;
807 }
808 }
809
810 /*
811 * Extra modules? Append them to current probes list.
812 */
813 list = the_config.kmod_extra_probes_list.value;
814 if (list) {
815 ret = append_list_to_probes(list);
816 if (ret) {
817 goto error;
818 }
819 }
820
821 /*
822 * Load probes modules now.
823 */
824 ret = modprobe_lttng(probes, nr_probes);
825 if (ret) {
826 goto error;
827 }
828 return ret;
829
830 error:
831 free_probes();
832 return ret;
833 }
This page took 0.047292 seconds and 5 git commands to generate.