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