lttng-sessiond: auto-load lttng-probe-i2c module
[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 }
202
203 kmod_module_unref(mod);
204 }
205
206 error:
207 if (ctx) {
208 kmod_unref(ctx);
209 }
210 return ret;
211 }
212
213 /**
214 * @brief Recursively unload modules.
215 *
216 * This function implements the same modules unloading behavior as
217 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
218 * dependencies and unload modules with a refcount of 0.
219 *
220 * @param mod The module to unload
221 *
222 * @returns \c 0 on success
223 * \c < 0 on error
224 */
225 static int rmmod_recurse(struct kmod_module *mod) {
226 int ret = 0;
227 struct kmod_list *deps, *itr;
228
229 if (kmod_module_get_initstate(mod) == KMOD_MODULE_BUILTIN) {
230 DBG("Module %s is builtin", kmod_module_get_name(mod));
231 return ret;
232 }
233
234 ret = kmod_module_remove_module(mod, 0);
235
236 deps = kmod_module_get_dependencies(mod);
237 if (deps != NULL) {
238 kmod_list_foreach(itr, deps) {
239 struct kmod_module *dep = kmod_module_get_module(itr);
240 if (kmod_module_get_refcnt(dep) == 0) {
241 DBG("Recursive remove module %s",
242 kmod_module_get_name(dep));
243 rmmod_recurse(dep);
244 }
245 kmod_module_unref(dep);
246 }
247 kmod_module_unref_list(deps);
248 }
249
250 return ret;
251 }
252
253 /**
254 * @brief Unloads the kernel modules in \p modules
255 *
256 * @param modules List of modules to unload
257 * @param entries Number of modules in the list
258 * @param required Are the modules required or optionnal
259 *
260 */
261 static void modprobe_remove_lttng(const struct kern_modules_param *modules,
262 int entries, int required)
263 {
264 int ret = 0, i;
265 struct kmod_ctx *ctx;
266
267 ret = setup_kmod_ctx(&ctx);
268 if (ret < 0) {
269 goto error;
270 }
271
272 for (i = entries - 1; i >= 0; i--) {
273 struct kmod_module *mod = NULL;
274
275 ret = kmod_module_new_from_name(ctx, modules[i].name, &mod);
276 if (ret < 0) {
277 PERROR("Failed to create kmod module for %s", modules[i].name);
278 goto error;
279 }
280
281 ret = rmmod_recurse(mod);
282 if (ret == -EEXIST) {
283 DBG("Module %s is not in kernel.", modules[i].name);
284 } else if (required && ret < 0) {
285 ERR("Unable to remove module %s", modules[i].name);
286 } else {
287 DBG("Modprobe removal successful %s",
288 modules[i].name);
289 }
290
291 kmod_module_unref(mod);
292 }
293
294 error:
295 if (ctx) {
296 kmod_unref(ctx);
297 }
298 }
299
300 #else /* HAVE_KMOD */
301
302 static int modprobe_lttng(struct kern_modules_param *modules,
303 int entries, int required)
304 {
305 int ret = 0, i;
306 char modprobe[256];
307
308 for (i = 0; i < entries; i++) {
309 ret = snprintf(modprobe, sizeof(modprobe),
310 "/sbin/modprobe %s%s",
311 required ? "" : "-q ",
312 modules[i].name);
313 if (ret < 0) {
314 PERROR("snprintf modprobe");
315 goto error;
316 }
317 modprobe[sizeof(modprobe) - 1] = '\0';
318 ret = system(modprobe);
319 if (ret == -1) {
320 if (required) {
321 ERR("Unable to launch modprobe for required module %s",
322 modules[i].name);
323 goto error;
324 } else {
325 DBG("Unable to launch modprobe for optional module %s; continuing",
326 modules[i].name);
327 ret = 0;
328 }
329 } else if (WEXITSTATUS(ret) != 0) {
330 if (required) {
331 ERR("Unable to load required module %s",
332 modules[i].name);
333 goto error;
334 } else {
335 DBG("Unable to load optional module %s; continuing",
336 modules[i].name);
337 ret = 0;
338 }
339 } else {
340 DBG("Modprobe successfully %s", modules[i].name);
341 }
342 }
343
344 error:
345 return ret;
346 }
347
348 static void modprobe_remove_lttng(const struct kern_modules_param *modules,
349 int entries, int required)
350 {
351 int ret = 0, i;
352 char modprobe[256];
353
354 for (i = entries - 1; i >= 0; i--) {
355 ret = snprintf(modprobe, sizeof(modprobe),
356 "/sbin/modprobe -r -q %s",
357 modules[i].name);
358 if (ret < 0) {
359 PERROR("snprintf modprobe -r");
360 return;
361 }
362 modprobe[sizeof(modprobe) - 1] = '\0';
363 ret = system(modprobe);
364 if (ret == -1) {
365 ERR("Unable to launch modprobe -r for module %s",
366 modules[i].name);
367 } else if (required && WEXITSTATUS(ret) != 0) {
368 ERR("Unable to remove module %s",
369 modules[i].name);
370 } else {
371 DBG("Modprobe removal successful %s",
372 modules[i].name);
373 }
374 }
375 }
376
377 #endif /* HAVE_KMOD */
378
379 /*
380 * Remove control kernel module(s) in reverse load order.
381 */
382 void modprobe_remove_lttng_control(void)
383 {
384 modprobe_remove_lttng(kern_modules_control_core,
385 ARRAY_SIZE(kern_modules_control_core),
386 LTTNG_MOD_REQUIRED);
387 }
388
389 static void free_probes(void)
390 {
391 int i;
392
393 if (!probes) {
394 return;
395 }
396 for (i = 0; i < nr_probes; ++i) {
397 free(probes[i].name);
398 }
399 free(probes);
400 probes = NULL;
401 nr_probes = 0;
402 }
403
404 /*
405 * Remove data kernel modules in reverse load order.
406 */
407 void modprobe_remove_lttng_data(void)
408 {
409 if (!probes) {
410 return;
411 }
412 modprobe_remove_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
413 free_probes();
414 }
415
416 /*
417 * Remove all kernel modules in reverse order.
418 */
419 void modprobe_remove_lttng_all(void)
420 {
421 modprobe_remove_lttng_data();
422 modprobe_remove_lttng_control();
423 }
424
425 /*
426 * Load control kernel module(s).
427 */
428 int modprobe_lttng_control(void)
429 {
430 int ret;
431
432 ret = modprobe_lttng(kern_modules_control_core,
433 ARRAY_SIZE(kern_modules_control_core),
434 LTTNG_MOD_REQUIRED);
435 return ret;
436 }
437
438 /**
439 * Grow global list of probes (double capacity or set it to 1 if
440 * currently 0 and copy existing data).
441 */
442 static int grow_probes(void)
443 {
444 int i;
445 struct kern_modules_param *tmp_probes;
446
447 /* Initialize capacity to 1 if 0. */
448 if (probes_capacity == 0) {
449 probes = zmalloc(sizeof(*probes));
450 if (!probes) {
451 PERROR("malloc probe list");
452 return -ENOMEM;
453 }
454
455 probes_capacity = 1;
456 return 0;
457 }
458
459 /* Double size. */
460 probes_capacity *= 2;
461
462 tmp_probes = zmalloc(sizeof(*tmp_probes) * probes_capacity);
463 if (!tmp_probes) {
464 PERROR("malloc probe list");
465 return -ENOMEM;
466 }
467
468 for (i = 0; i < nr_probes; ++i) {
469 /* Move name pointer. */
470 tmp_probes[i].name = probes[i].name;
471 }
472
473 /* Replace probes with larger copy. */
474 free(probes);
475 probes = tmp_probes;
476
477 return 0;
478 }
479
480 /*
481 * Appends a comma-separated list of probes to the global list
482 * of probes.
483 */
484 static int append_list_to_probes(const char *list)
485 {
486 char *next;
487 int ret;
488 char *tmp_list, *cur_list;
489
490 assert(list);
491
492 cur_list = tmp_list = strdup(list);
493 if (!tmp_list) {
494 PERROR("strdup temp list");
495 return -ENOMEM;
496 }
497
498 for (;;) {
499 size_t name_len;
500 struct kern_modules_param *cur_mod;
501
502 next = strtok(cur_list, ",");
503 if (!next) {
504 break;
505 }
506 cur_list = NULL;
507
508 /* filter leading spaces */
509 while (*next == ' ') {
510 next++;
511 }
512
513 if (probes_capacity <= nr_probes) {
514 ret = grow_probes();
515 if (ret) {
516 goto error;
517 }
518 }
519
520 /* Length 13 is "lttng-probe-" + \0 */
521 name_len = strlen(next) + 13;
522
523 cur_mod = &probes[nr_probes];
524 cur_mod->name = zmalloc(name_len);
525 if (!cur_mod->name) {
526 PERROR("malloc probe list");
527 ret = -ENOMEM;
528 goto error;
529 }
530
531 ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next);
532 if (ret < 0) {
533 PERROR("snprintf modprobe name");
534 ret = -ENOMEM;
535 goto error;
536 }
537
538 nr_probes++;
539 }
540
541 free(tmp_list);
542 return 0;
543
544 error:
545 free(tmp_list);
546 free_probes();
547 return ret;
548 }
549
550 /*
551 * Load data kernel module(s).
552 */
553 int modprobe_lttng_data(void)
554 {
555 int ret, i;
556 char *list;
557
558 /*
559 * Base probes: either from command line option, environment
560 * variable or default list.
561 */
562 if (kmod_probes_list) {
563 list = kmod_probes_list;
564 } else {
565 list = utils_get_kmod_probes_list();
566 }
567
568 if (list) {
569 /* User-specified probes. */
570 ret = append_list_to_probes(list);
571 if (ret) {
572 return ret;
573 }
574 } else {
575 /* Default probes. */
576 int def_len = ARRAY_SIZE(kern_modules_probes_default);
577
578 probes = zmalloc(sizeof(*probes) * def_len);
579 if (!probes) {
580 PERROR("malloc probe list");
581 return -ENOMEM;
582 }
583
584 nr_probes = probes_capacity = def_len;
585
586 for (i = 0; i < def_len; ++i) {
587 char* name = strdup(kern_modules_probes_default[i].name);
588
589 if (!name) {
590 PERROR("strdup probe item");
591 ret = -ENOMEM;
592 goto error;
593 }
594
595 probes[i].name = name;
596 }
597 }
598
599 /*
600 * Extra modules? Append them to current probes list.
601 */
602 if (kmod_extra_probes_list) {
603 list = kmod_extra_probes_list;
604 } else {
605 list = utils_get_extra_kmod_probes_list();
606 }
607
608 if (list) {
609 ret = append_list_to_probes(list);
610 if (ret) {
611 goto error;
612 }
613 }
614
615 /*
616 * Load probes modules now.
617 */
618 ret = modprobe_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
619 if (ret) {
620 goto error;
621 }
622 return ret;
623
624 error:
625 free_probes();
626 return ret;
627 }
This page took 0.042088 seconds and 5 git commands to generate.