Fix: add missing refcount of loaded modules
[lttng-tools.git] / src / bin / lttng-sessiond / modprobe.c
1 /*
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
3 * 2014 - Jan Glauber <jan.glauber@gmail.com>
4 *
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.
8 *
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.
13 *
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.
17 */
18
19 /**
20 * @file modprobe.c
21 *
22 * @brief modprobe related functions.
23 *
24 */
25
26 #define _LGPL_SOURCE
27 #include <assert.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/wait.h>
31
32 #include <common/common.h>
33 #include <common/utils.h>
34
35 #include "modprobe.h"
36 #include "kern-modules.h"
37
38 #define LTTNG_MOD_REQUIRED 1
39 #define LTTNG_MOD_OPTIONAL 0
40
41 /* LTTng kernel tracer mandatory core modules list */
42 struct kern_modules_param kern_modules_control_core[] = {
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
51 /* LTTng kernel tracer probe modules list */
52 struct kern_modules_param kern_modules_probes_default[] = {
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" },
60 { "lttng-probe-i2c" },
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" },
92 { "lttng-probe-x86-irq-vectors" },
93 { "lttng-probe-x86-exceptions" },
94 };
95
96 /* dynamic probe modules list */
97 static struct kern_modules_param *probes;
98 static int nr_probes;
99 static int probes_capacity;
100
101 #if HAVE_KMOD
102 #include <libkmod.h>
103
104 /**
105 * @brief Logging function for libkmod integration.
106 */
107 static 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 }
119
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 */
129 static int setup_kmod_ctx(struct kmod_ctx **ctx)
130 {
131 int ret = 0;
132
133 *ctx = kmod_new(NULL, NULL);
134 if (!ctx) {
135 PERROR("Unable to create kmod library context");
136 ret = -ENOMEM;
137 goto error;
138 }
139
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
147 error:
148 return ret;
149 }
150
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 */
164 static 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 }
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
184 ret = kmod_module_probe_insert_module(mod, 0,
185 NULL, NULL, NULL, NULL);
186 if (ret == -EEXIST) {
187 DBG("Module %s is already loaded", modules[i].name);
188 ret = 0;
189 } else if (ret < 0) {
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 }
199 } else {
200 DBG("Modprobe successfully %s", modules[i].name);
201 modules[i].loaded = true;
202 }
203
204 kmod_module_unref(mod);
205 }
206
207 error:
208 if (ctx) {
209 kmod_unref(ctx);
210 }
211 return ret;
212 }
213
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 */
226 static 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
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 */
262 static 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
276 if (!modules[i].loaded) {
277 continue;
278 }
279
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
299 error:
300 if (ctx) {
301 kmod_unref(ctx);
302 }
303 }
304
305 #else /* HAVE_KMOD */
306
307 static int modprobe_lttng(struct kern_modules_param *modules,
308 int entries, int required)
309 {
310 int ret = 0, i;
311 char modprobe[256];
312
313 for (i = 0; i < entries; i++) {
314 ret = snprintf(modprobe, sizeof(modprobe),
315 "/sbin/modprobe %s%s",
316 required ? "" : "-q ",
317 modules[i].name);
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) {
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 }
344 } else {
345 DBG("Modprobe successfully %s", modules[i].name);
346 modules[i].loaded = true;
347 }
348 }
349
350 error:
351 return ret;
352 }
353
354 static 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--) {
361 if (!modules[i].loaded) {
362 continue;
363 }
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
386 #endif /* HAVE_KMOD */
387
388 /*
389 * Remove control kernel module(s) in reverse load order.
390 */
391 void 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
398 static 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 */
416 void 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 */
428 void modprobe_remove_lttng_all(void)
429 {
430 modprobe_remove_lttng_data();
431 modprobe_remove_lttng_control();
432 }
433
434 /*
435 * Load control kernel module(s).
436 */
437 int modprobe_lttng_control(void)
438 {
439 int ret;
440
441 ret = modprobe_lttng(kern_modules_control_core,
442 ARRAY_SIZE(kern_modules_control_core),
443 LTTNG_MOD_REQUIRED);
444 return ret;
445 }
446
447 /**
448 * Grow global list of probes (double capacity or set it to 1 if
449 * currently 0 and copy existing data).
450 */
451 static int grow_probes(void)
452 {
453 int i;
454 struct kern_modules_param *tmp_probes;
455
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;
466 }
467
468 /* Double size. */
469 probes_capacity *= 2;
470
471 tmp_probes = zmalloc(sizeof(*tmp_probes) * probes_capacity);
472 if (!tmp_probes) {
473 PERROR("malloc probe list");
474 return -ENOMEM;
475 }
476
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 */
493 static int append_list_to_probes(const char *list)
494 {
495 char *next;
496 int ret;
497 char *tmp_list, *cur_list;
498
499 assert(list);
500
501 cur_list = tmp_list = strdup(list);
502 if (!tmp_list) {
503 PERROR("strdup temp list");
504 return -ENOMEM;
505 }
506
507 for (;;) {
508 size_t name_len;
509 struct kern_modules_param *cur_mod;
510
511 next = strtok(cur_list, ",");
512 if (!next) {
513 break;
514 }
515 cur_list = NULL;
516
517 /* filter leading spaces */
518 while (*next == ' ') {
519 next++;
520 }
521
522 if (probes_capacity <= nr_probes) {
523 ret = grow_probes();
524 if (ret) {
525 goto error;
526 }
527 }
528
529 /* Length 13 is "lttng-probe-" + \0 */
530 name_len = strlen(next) + 13;
531
532 cur_mod = &probes[nr_probes];
533 cur_mod->name = zmalloc(name_len);
534 if (!cur_mod->name) {
535 PERROR("malloc probe list");
536 ret = -ENOMEM;
537 goto error;
538 }
539
540 ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next);
541 if (ret < 0) {
542 PERROR("snprintf modprobe name");
543 ret = -ENOMEM;
544 goto error;
545 }
546
547 nr_probes++;
548 }
549
550 free(tmp_list);
551 return 0;
552
553 error:
554 free(tmp_list);
555 free_probes();
556 return ret;
557 }
558
559 /*
560 * Load data kernel module(s).
561 */
562 int 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);
580 if (ret) {
581 return ret;
582 }
583 } else {
584 /* Default probes. */
585 int def_len = ARRAY_SIZE(kern_modules_probes_default);
586
587 probes = zmalloc(sizeof(*probes) * def_len);
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");
600 ret = -ENOMEM;
601 goto error;
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) {
620 goto error;
621 }
622 }
623
624 /*
625 * Load probes modules now.
626 */
627 ret = modprobe_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
628 if (ret) {
629 goto error;
630 }
631 return ret;
632
633 error:
634 free_probes();
635 return ret;
636 }
This page took 0.067211 seconds and 4 git commands to generate.