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