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