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