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