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