Commit | Line | Data |
---|---|---|
096102bd | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2011 David Goulet <dgoulet@efficios.com> |
3 | * Copyright (C) 2014 Jan Glauber <jan.glauber@gmail.com> | |
096102bd | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
096102bd | 6 | * |
096102bd DG |
7 | */ |
8 | ||
d11b2027 MJ |
9 | /** |
10 | * @file modprobe.c | |
11 | * | |
12 | * @brief modprobe related functions. | |
13 | * | |
14 | */ | |
15 | ||
6c1c0768 | 16 | #define _LGPL_SOURCE |
096102bd DG |
17 | #include <stdio.h> |
18 | #include <stdlib.h> | |
19 | #include <sys/wait.h> | |
20 | ||
21 | #include <common/common.h> | |
fbb9748b | 22 | #include <common/utils.h> |
096102bd DG |
23 | |
24 | #include "modprobe.h" | |
25 | #include "kern-modules.h" | |
e6142f2e | 26 | #include "lttng-sessiond.h" |
096102bd | 27 | |
ab57d7d3 JG |
28 | /* LTTng kernel tracer mandatory core modules list */ |
29 | struct kern_modules_param kern_modules_control_core[] = { | |
adfe4f5e JG |
30 | { |
31 | .name = (char *) "lttng-ring-buffer-client-discard", | |
32 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED, | |
33 | }, | |
34 | { | |
35 | .name = (char *) "lttng-ring-buffer-client-overwrite", | |
36 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED, | |
37 | }, | |
38 | { | |
39 | .name = (char *) "lttng-ring-buffer-metadata-client", | |
40 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED, | |
41 | }, | |
42 | { | |
43 | .name = (char *) "lttng-ring-buffer-client-mmap-discard", | |
44 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED, | |
45 | }, | |
46 | { | |
47 | .name = (char *) "lttng-ring-buffer-client-mmap-overwrite", | |
48 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED, | |
49 | }, | |
50 | { | |
51 | .name = (char *) "lttng-ring-buffer-metadata-mmap-client", | |
52 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED, | |
53 | }, | |
87a15032 JR |
54 | { |
55 | .name = (char *) "lttng-ring-buffer-event_notifier-client", | |
56 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
57 | }, | |
90aa04a1 FD |
58 | { |
59 | .name = (char *) "lttng-counter-client-percpu-64-modular", | |
60 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
61 | }, | |
62 | { | |
63 | .name = (char *) "lttng-counter-client-percpu-32-modular", | |
64 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
65 | }, | |
ab57d7d3 JG |
66 | }; |
67 | ||
adfe4f5e | 68 | /* LTTng kerneltracer probe modules list */ |
fbb9748b | 69 | struct kern_modules_param kern_modules_probes_default[] = { |
adfe4f5e JG |
70 | { |
71 | .name = (char *) "lttng-probe-asoc", | |
72 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
73 | }, | |
74 | { | |
75 | .name = (char *) "lttng-probe-block", | |
76 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
77 | }, | |
78 | { | |
79 | .name = (char *) "lttng-probe-btrfs", | |
80 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
81 | }, | |
82 | { | |
83 | .name = (char *) "lttng-probe-compaction", | |
84 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
85 | }, | |
86 | { | |
87 | .name = (char *) "lttng-probe-ext3", | |
88 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
89 | }, | |
90 | { | |
91 | .name = (char *) "lttng-probe-ext4", | |
92 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
93 | }, | |
94 | { | |
95 | .name = (char *) "lttng-probe-gpio", | |
96 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
97 | }, | |
98 | { | |
99 | .name = (char *) "lttng-probe-i2c", | |
100 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
101 | }, | |
102 | { | |
103 | .name = (char *) "lttng-probe-irq", | |
104 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
105 | }, | |
106 | { | |
107 | .name = (char *) "lttng-probe-jbd", | |
108 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
109 | }, | |
110 | { | |
111 | .name = (char *) "lttng-probe-jbd2", | |
112 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
113 | }, | |
114 | { | |
115 | .name = (char *) "lttng-probe-kmem", | |
116 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
117 | }, | |
118 | { | |
119 | .name = (char *) "lttng-probe-kvm", | |
120 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
121 | }, | |
122 | { | |
123 | .name = (char *) "lttng-probe-kvm-x86", | |
124 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
125 | }, | |
126 | { | |
127 | .name = (char *) "lttng-probe-kvm-x86-mmu", | |
128 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
129 | }, | |
130 | { | |
131 | .name = (char *) "lttng-probe-lock", | |
132 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
133 | }, | |
134 | { | |
135 | .name = (char *) "lttng-probe-module", | |
136 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
137 | }, | |
138 | { | |
139 | .name = (char *) "lttng-probe-napi", | |
140 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
141 | }, | |
142 | { | |
143 | .name = (char *) "lttng-probe-net", | |
144 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
145 | }, | |
146 | { | |
147 | .name = (char *) "lttng-probe-power", | |
148 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
149 | }, | |
150 | { | |
151 | .name = (char *) "lttng-probe-preemptirq", | |
152 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
153 | }, | |
154 | { | |
155 | .name = (char *) "lttng-probe-printk", | |
156 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
157 | }, | |
158 | { | |
159 | .name = (char *) "lttng-probe-random", | |
160 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
161 | }, | |
162 | { | |
163 | .name = (char *) "lttng-probe-rcu", | |
164 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
165 | }, | |
166 | { | |
167 | .name = (char *) "lttng-probe-regmap", | |
168 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
169 | }, | |
170 | { | |
171 | .name = (char *) "lttng-probe-regulator", | |
172 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
173 | }, | |
174 | { | |
175 | .name = (char *) "lttng-probe-rpm", | |
176 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
177 | }, | |
178 | { | |
179 | .name = (char *) "lttng-probe-sched", | |
180 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
181 | }, | |
182 | { | |
183 | .name = (char *) "lttng-probe-scsi", | |
184 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
185 | }, | |
186 | { | |
187 | .name = (char *) "lttng-probe-signal", | |
188 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
189 | }, | |
190 | { | |
191 | .name = (char *) "lttng-probe-skb", | |
192 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
193 | }, | |
194 | { | |
195 | .name = (char *) "lttng-probe-sock", | |
196 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
197 | }, | |
198 | { | |
199 | .name = (char *) "lttng-probe-statedump", | |
200 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
201 | }, | |
202 | { | |
203 | .name = (char *) "lttng-probe-sunrpc", | |
204 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
205 | }, | |
206 | { | |
207 | .name = (char *) "lttng-probe-timer", | |
208 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
209 | }, | |
210 | { | |
211 | .name = (char *) "lttng-probe-udp", | |
212 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
213 | }, | |
214 | { | |
215 | .name = (char *) "lttng-probe-vmscan", | |
216 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
217 | }, | |
218 | { | |
219 | .name = (char *) "lttng-probe-v4l2", | |
220 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
221 | }, | |
222 | { | |
223 | .name = (char *) "lttng-probe-workqueue", | |
224 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
225 | }, | |
226 | { | |
227 | .name = (char *) "lttng-probe-writeback", | |
228 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
229 | }, | |
230 | { | |
231 | .name = (char *) "lttng-probe-x86-irq-vectors", | |
232 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
233 | }, | |
234 | { | |
235 | .name = (char *) "lttng-probe-x86-exceptions", | |
236 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, | |
237 | }, | |
096102bd DG |
238 | }; |
239 | ||
fbb9748b JG |
240 | /* dynamic probe modules list */ |
241 | static struct kern_modules_param *probes; | |
242 | static int nr_probes; | |
c9d42407 | 243 | static int probes_capacity; |
fbb9748b | 244 | |
234170ac UTL |
245 | #if HAVE_KMOD |
246 | #include <libkmod.h> | |
866c17ce | 247 | |
d11b2027 MJ |
248 | /** |
249 | * @brief Logging function for libkmod integration. | |
250 | */ | |
d22ad5f8 SM |
251 | static ATTR_FORMAT_PRINTF(6, 0) |
252 | void log_kmod(void *data, int priority, const char *file, int line, | |
234170ac UTL |
253 | const char *fn, const char *format, va_list args) |
254 | { | |
255 | char *str; | |
256 | ||
257 | if (vasprintf(&str, format, args) < 0) { | |
258 | return; | |
259 | } | |
260 | ||
261 | DBG("libkmod: %s", str); | |
262 | free(str); | |
263 | } | |
866c17ce | 264 | |
d11b2027 MJ |
265 | /** |
266 | * @brief Setup the libkmod context. | |
267 | * | |
268 | * Create the context, add a custom logging function and preload the | |
269 | * ressources for faster operation. | |
270 | * | |
271 | * @returns \c 0 on success | |
272 | * \c < 0 on error | |
273 | */ | |
866c17ce | 274 | static int setup_kmod_ctx(struct kmod_ctx **ctx) |
234170ac | 275 | { |
866c17ce | 276 | int ret = 0; |
234170ac | 277 | |
866c17ce | 278 | *ctx = kmod_new(NULL, NULL); |
234170ac UTL |
279 | if (!ctx) { |
280 | PERROR("Unable to create kmod library context"); | |
281 | ret = -ENOMEM; | |
282 | goto error; | |
283 | } | |
284 | ||
d22ad5f8 SM |
285 | /* |
286 | * Parameter 2 of kmod_set_log_fn generates a | |
287 | * -Wsuggest-attribute=formatkmod_set_log_fn warning that we can't fix, | |
288 | * ignore it. | |
289 | */ | |
290 | DIAGNOSTIC_PUSH | |
291 | DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT | |
866c17ce | 292 | kmod_set_log_fn(*ctx, log_kmod, NULL); |
d22ad5f8 | 293 | DIAGNOSTIC_POP |
866c17ce MJ |
294 | ret = kmod_load_resources(*ctx); |
295 | if (ret < 0) { | |
296 | ERR("Failed to load kmod library resources"); | |
297 | goto error; | |
298 | } | |
299 | ||
300 | error: | |
301 | return ret; | |
302 | } | |
303 | ||
d11b2027 MJ |
304 | /** |
305 | * @brief Loads the kernel modules in \p modules | |
306 | * | |
307 | * @param modules List of modules to load | |
308 | * @param entries Number of modules in the list | |
d11b2027 MJ |
309 | * |
310 | * If the modules are required, we will return with error after the | |
311 | * first failed module load, otherwise we continue loading. | |
312 | * | |
313 | * @returns \c 0 on success | |
314 | * \c < 0 on error | |
315 | */ | |
866c17ce | 316 | static int modprobe_lttng(struct kern_modules_param *modules, |
adfe4f5e | 317 | int entries) |
866c17ce MJ |
318 | { |
319 | int ret = 0, i; | |
320 | struct kmod_ctx *ctx; | |
321 | ||
322 | ret = setup_kmod_ctx(&ctx); | |
323 | if (ret < 0) { | |
324 | goto error; | |
325 | } | |
234170ac UTL |
326 | |
327 | for (i = 0; i < entries; i++) { | |
328 | struct kmod_module *mod = NULL; | |
329 | ||
330 | ret = kmod_module_new_from_name(ctx, modules[i].name, &mod); | |
331 | if (ret < 0) { | |
332 | PERROR("Failed to create kmod module for %s", modules[i].name); | |
333 | goto error; | |
334 | } | |
335 | ||
0b1e16b8 | 336 | ret = kmod_module_probe_insert_module(mod, 0, |
234170ac | 337 | NULL, NULL, NULL, NULL); |
0b1e16b8 MJ |
338 | if (ret == -EEXIST) { |
339 | DBG("Module %s is already loaded", modules[i].name); | |
340 | ret = 0; | |
341 | } else if (ret < 0) { | |
adfe4f5e | 342 | if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) { |
16c2e854 PP |
343 | ERR("Unable to load required module %s", |
344 | modules[i].name); | |
345 | goto error; | |
346 | } else { | |
347 | DBG("Unable to load optional module %s; continuing", | |
348 | modules[i].name); | |
349 | ret = 0; | |
350 | } | |
234170ac UTL |
351 | } else { |
352 | DBG("Modprobe successfully %s", modules[i].name); | |
4ad664a0 | 353 | modules[i].loaded = true; |
234170ac UTL |
354 | } |
355 | ||
356 | kmod_module_unref(mod); | |
357 | } | |
358 | ||
359 | error: | |
360 | if (ctx) { | |
361 | kmod_unref(ctx); | |
362 | } | |
363 | return ret; | |
364 | } | |
365 | ||
d11b2027 MJ |
366 | /** |
367 | * @brief Recursively unload modules. | |
368 | * | |
369 | * This function implements the same modules unloading behavior as | |
370 | * 'modprobe -r' or rmmod, it will recursevily go trought the \p module | |
371 | * dependencies and unload modules with a refcount of 0. | |
372 | * | |
373 | * @param mod The module to unload | |
374 | * | |
375 | * @returns \c 0 on success | |
376 | * \c < 0 on error | |
377 | */ | |
866c17ce MJ |
378 | static int rmmod_recurse(struct kmod_module *mod) { |
379 | int ret = 0; | |
380 | struct kmod_list *deps, *itr; | |
381 | ||
382 | if (kmod_module_get_initstate(mod) == KMOD_MODULE_BUILTIN) { | |
383 | DBG("Module %s is builtin", kmod_module_get_name(mod)); | |
384 | return ret; | |
385 | } | |
386 | ||
387 | ret = kmod_module_remove_module(mod, 0); | |
388 | ||
389 | deps = kmod_module_get_dependencies(mod); | |
390 | if (deps != NULL) { | |
391 | kmod_list_foreach(itr, deps) { | |
392 | struct kmod_module *dep = kmod_module_get_module(itr); | |
393 | if (kmod_module_get_refcnt(dep) == 0) { | |
394 | DBG("Recursive remove module %s", | |
395 | kmod_module_get_name(dep)); | |
396 | rmmod_recurse(dep); | |
397 | } | |
398 | kmod_module_unref(dep); | |
399 | } | |
400 | kmod_module_unref_list(deps); | |
401 | } | |
402 | ||
403 | return ret; | |
404 | } | |
405 | ||
d11b2027 MJ |
406 | /** |
407 | * @brief Unloads the kernel modules in \p modules | |
408 | * | |
409 | * @param modules List of modules to unload | |
410 | * @param entries Number of modules in the list | |
d11b2027 MJ |
411 | * |
412 | */ | |
866c17ce | 413 | static void modprobe_remove_lttng(const struct kern_modules_param *modules, |
adfe4f5e | 414 | int entries) |
866c17ce MJ |
415 | { |
416 | int ret = 0, i; | |
417 | struct kmod_ctx *ctx; | |
418 | ||
419 | ret = setup_kmod_ctx(&ctx); | |
420 | if (ret < 0) { | |
421 | goto error; | |
422 | } | |
423 | ||
424 | for (i = entries - 1; i >= 0; i--) { | |
425 | struct kmod_module *mod = NULL; | |
426 | ||
4ad664a0 JG |
427 | if (!modules[i].loaded) { |
428 | continue; | |
429 | } | |
430 | ||
866c17ce MJ |
431 | ret = kmod_module_new_from_name(ctx, modules[i].name, &mod); |
432 | if (ret < 0) { | |
433 | PERROR("Failed to create kmod module for %s", modules[i].name); | |
434 | goto error; | |
435 | } | |
436 | ||
437 | ret = rmmod_recurse(mod); | |
438 | if (ret == -EEXIST) { | |
439 | DBG("Module %s is not in kernel.", modules[i].name); | |
adfe4f5e | 440 | } else if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED && ret < 0) { |
866c17ce MJ |
441 | ERR("Unable to remove module %s", modules[i].name); |
442 | } else { | |
443 | DBG("Modprobe removal successful %s", | |
444 | modules[i].name); | |
445 | } | |
446 | ||
447 | kmod_module_unref(mod); | |
448 | } | |
449 | ||
450 | error: | |
451 | if (ctx) { | |
452 | kmod_unref(ctx); | |
453 | } | |
454 | } | |
455 | ||
234170ac UTL |
456 | #else /* HAVE_KMOD */ |
457 | ||
fbb9748b | 458 | static int modprobe_lttng(struct kern_modules_param *modules, |
adfe4f5e | 459 | int entries) |
096102bd DG |
460 | { |
461 | int ret = 0, i; | |
462 | char modprobe[256]; | |
463 | ||
e23b81ed | 464 | for (i = 0; i < entries; i++) { |
096102bd DG |
465 | ret = snprintf(modprobe, sizeof(modprobe), |
466 | "/sbin/modprobe %s%s", | |
adfe4f5e | 467 | modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED ? "" : "-q ", |
e23b81ed | 468 | modules[i].name); |
096102bd DG |
469 | if (ret < 0) { |
470 | PERROR("snprintf modprobe"); | |
471 | goto error; | |
472 | } | |
473 | modprobe[sizeof(modprobe) - 1] = '\0'; | |
474 | ret = system(modprobe); | |
475 | if (ret == -1) { | |
adfe4f5e | 476 | if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) { |
16c2e854 PP |
477 | ERR("Unable to launch modprobe for required module %s", |
478 | modules[i].name); | |
479 | goto error; | |
480 | } else { | |
481 | DBG("Unable to launch modprobe for optional module %s; continuing", | |
482 | modules[i].name); | |
483 | ret = 0; | |
484 | } | |
485 | } else if (WEXITSTATUS(ret) != 0) { | |
adfe4f5e | 486 | if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) { |
16c2e854 PP |
487 | ERR("Unable to load required module %s", |
488 | modules[i].name); | |
489 | goto error; | |
490 | } else { | |
491 | DBG("Unable to load optional module %s; continuing", | |
492 | modules[i].name); | |
493 | ret = 0; | |
494 | } | |
096102bd | 495 | } else { |
ab57d7d3 | 496 | DBG("Modprobe successfully %s", modules[i].name); |
355d2778 | 497 | modules[i].loaded = true; |
096102bd DG |
498 | } |
499 | } | |
500 | ||
501 | error: | |
502 | return ret; | |
503 | } | |
504 | ||
35e090b7 | 505 | static void modprobe_remove_lttng(const struct kern_modules_param *modules, |
adfe4f5e | 506 | int entries) |
35e090b7 MJ |
507 | { |
508 | int ret = 0, i; | |
509 | char modprobe[256]; | |
510 | ||
511 | for (i = entries - 1; i >= 0; i--) { | |
4ad664a0 JG |
512 | if (!modules[i].loaded) { |
513 | continue; | |
514 | } | |
35e090b7 MJ |
515 | ret = snprintf(modprobe, sizeof(modprobe), |
516 | "/sbin/modprobe -r -q %s", | |
517 | modules[i].name); | |
518 | if (ret < 0) { | |
519 | PERROR("snprintf modprobe -r"); | |
520 | return; | |
521 | } | |
522 | modprobe[sizeof(modprobe) - 1] = '\0'; | |
523 | ret = system(modprobe); | |
524 | if (ret == -1) { | |
1d25334c FD |
525 | if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) { |
526 | ERR("Unable to launch modprobe -r for required module %s", | |
527 | modules[i].name); | |
528 | } else { | |
529 | DBG("Unable to launch modprobe -r for optional module %s", | |
530 | modules[i].name); | |
531 | } | |
532 | } else if (WEXITSTATUS(ret) != 0) { | |
533 | if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) { | |
534 | ERR("Unable to remove required module %s", | |
535 | modules[i].name); | |
536 | } else { | |
537 | DBG("Unable to remove optional module %s", | |
538 | modules[i].name); | |
539 | } | |
35e090b7 | 540 | } else { |
1d25334c | 541 | DBG("Modprobe removal successful %s", modules[i].name); |
35e090b7 MJ |
542 | } |
543 | } | |
544 | } | |
545 | ||
866c17ce MJ |
546 | #endif /* HAVE_KMOD */ |
547 | ||
35e090b7 MJ |
548 | /* |
549 | * Remove control kernel module(s) in reverse load order. | |
550 | */ | |
551 | void modprobe_remove_lttng_control(void) | |
552 | { | |
553 | modprobe_remove_lttng(kern_modules_control_core, | |
adfe4f5e | 554 | ARRAY_SIZE(kern_modules_control_core)); |
35e090b7 MJ |
555 | } |
556 | ||
557 | static void free_probes(void) | |
558 | { | |
559 | int i; | |
560 | ||
561 | if (!probes) { | |
562 | return; | |
563 | } | |
564 | for (i = 0; i < nr_probes; ++i) { | |
565 | free(probes[i].name); | |
566 | } | |
567 | free(probes); | |
568 | probes = NULL; | |
569 | nr_probes = 0; | |
570 | } | |
571 | ||
572 | /* | |
573 | * Remove data kernel modules in reverse load order. | |
574 | */ | |
575 | void modprobe_remove_lttng_data(void) | |
576 | { | |
577 | if (!probes) { | |
578 | return; | |
579 | } | |
adfe4f5e JG |
580 | |
581 | modprobe_remove_lttng(probes, nr_probes); | |
35e090b7 MJ |
582 | free_probes(); |
583 | } | |
584 | ||
585 | /* | |
586 | * Remove all kernel modules in reverse order. | |
587 | */ | |
588 | void modprobe_remove_lttng_all(void) | |
589 | { | |
590 | modprobe_remove_lttng_data(); | |
591 | modprobe_remove_lttng_control(); | |
592 | } | |
593 | ||
e23b81ed JG |
594 | /* |
595 | * Load control kernel module(s). | |
596 | */ | |
597 | int modprobe_lttng_control(void) | |
598 | { | |
adfe4f5e JG |
599 | return modprobe_lttng(kern_modules_control_core, |
600 | ARRAY_SIZE(kern_modules_control_core)); | |
e23b81ed | 601 | } |
ab57d7d3 | 602 | |
c9d42407 PP |
603 | /** |
604 | * Grow global list of probes (double capacity or set it to 1 if | |
605 | * currently 0 and copy existing data). | |
096102bd | 606 | */ |
c9d42407 | 607 | static int grow_probes(void) |
096102bd | 608 | { |
c9d42407 PP |
609 | int i; |
610 | struct kern_modules_param *tmp_probes; | |
fbb9748b | 611 | |
c9d42407 PP |
612 | /* Initialize capacity to 1 if 0. */ |
613 | if (probes_capacity == 0) { | |
7966af57 | 614 | probes = (kern_modules_param *) zmalloc(sizeof(*probes)); |
c9d42407 PP |
615 | if (!probes) { |
616 | PERROR("malloc probe list"); | |
617 | return -ENOMEM; | |
618 | } | |
619 | ||
620 | probes_capacity = 1; | |
621 | return 0; | |
fbb9748b JG |
622 | } |
623 | ||
c9d42407 PP |
624 | /* Double size. */ |
625 | probes_capacity *= 2; | |
626 | ||
7966af57 | 627 | tmp_probes = (kern_modules_param *) zmalloc(sizeof(*tmp_probes) * probes_capacity); |
c9d42407 | 628 | if (!tmp_probes) { |
fbb9748b JG |
629 | PERROR("malloc probe list"); |
630 | return -ENOMEM; | |
631 | } | |
632 | ||
c9d42407 | 633 | for (i = 0; i < nr_probes; ++i) { |
adfe4f5e JG |
634 | /* Ownership of 'name' field is transferred. */ |
635 | tmp_probes[i] = probes[i]; | |
c9d42407 PP |
636 | } |
637 | ||
638 | /* Replace probes with larger copy. */ | |
639 | free(probes); | |
640 | probes = tmp_probes; | |
641 | ||
642 | return 0; | |
643 | } | |
644 | ||
645 | /* | |
646 | * Appends a comma-separated list of probes to the global list | |
647 | * of probes. | |
648 | */ | |
649 | static int append_list_to_probes(const char *list) | |
650 | { | |
651 | char *next; | |
d3c04b7c | 652 | int ret; |
50e0fa5f | 653 | char *tmp_list, *cur_list, *saveptr; |
c9d42407 | 654 | |
a0377dfe | 655 | LTTNG_ASSERT(list); |
c9d42407 | 656 | |
44603c80 | 657 | cur_list = tmp_list = strdup(list); |
c9d42407 PP |
658 | if (!tmp_list) { |
659 | PERROR("strdup temp list"); | |
660 | return -ENOMEM; | |
661 | } | |
662 | ||
663 | for (;;) { | |
fbb9748b | 664 | size_t name_len; |
c9d42407 | 665 | struct kern_modules_param *cur_mod; |
fbb9748b | 666 | |
50e0fa5f | 667 | next = strtok_r(cur_list, ",", &saveptr); |
fbb9748b | 668 | if (!next) { |
c9d42407 | 669 | break; |
fbb9748b | 670 | } |
44603c80 | 671 | cur_list = NULL; |
fbb9748b JG |
672 | |
673 | /* filter leading spaces */ | |
674 | while (*next == ' ') { | |
675 | next++; | |
676 | } | |
677 | ||
c9d42407 PP |
678 | if (probes_capacity <= nr_probes) { |
679 | ret = grow_probes(); | |
680 | if (ret) { | |
398d5459 | 681 | goto error; |
c9d42407 PP |
682 | } |
683 | } | |
684 | ||
fbb9748b JG |
685 | /* Length 13 is "lttng-probe-" + \0 */ |
686 | name_len = strlen(next) + 13; | |
687 | ||
d3c04b7c | 688 | cur_mod = &probes[nr_probes]; |
7966af57 | 689 | cur_mod->name = (char *) zmalloc(name_len); |
c9d42407 | 690 | if (!cur_mod->name) { |
fbb9748b | 691 | PERROR("malloc probe list"); |
398d5459 MD |
692 | ret = -ENOMEM; |
693 | goto error; | |
fbb9748b JG |
694 | } |
695 | ||
c9d42407 | 696 | ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next); |
fbb9748b JG |
697 | if (ret < 0) { |
698 | PERROR("snprintf modprobe name"); | |
398d5459 MD |
699 | ret = -ENOMEM; |
700 | goto error; | |
fbb9748b | 701 | } |
c9d42407 | 702 | |
adfe4f5e JG |
703 | cur_mod->load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL; |
704 | ||
c9d42407 | 705 | nr_probes++; |
fbb9748b JG |
706 | } |
707 | ||
c9d42407 | 708 | free(tmp_list); |
c9d42407 | 709 | return 0; |
398d5459 MD |
710 | |
711 | error: | |
712 | free(tmp_list); | |
713 | free_probes(); | |
714 | return ret; | |
c9d42407 PP |
715 | } |
716 | ||
717 | /* | |
718 | * Load data kernel module(s). | |
719 | */ | |
720 | int modprobe_lttng_data(void) | |
721 | { | |
722 | int ret, i; | |
723 | char *list; | |
724 | ||
725 | /* | |
726 | * Base probes: either from command line option, environment | |
727 | * variable or default list. | |
728 | */ | |
412d7227 | 729 | list = the_config.kmod_probes_list.value; |
c9d42407 PP |
730 | if (list) { |
731 | /* User-specified probes. */ | |
732 | ret = append_list_to_probes(list); | |
c9d42407 PP |
733 | if (ret) { |
734 | return ret; | |
735 | } | |
736 | } else { | |
737 | /* Default probes. */ | |
738 | int def_len = ARRAY_SIZE(kern_modules_probes_default); | |
c9d42407 | 739 | |
7966af57 | 740 | probes = (kern_modules_param *) zmalloc(sizeof(*probes) * def_len); |
c9d42407 PP |
741 | if (!probes) { |
742 | PERROR("malloc probe list"); | |
743 | return -ENOMEM; | |
744 | } | |
745 | ||
746 | nr_probes = probes_capacity = def_len; | |
747 | ||
748 | for (i = 0; i < def_len; ++i) { | |
749 | char* name = strdup(kern_modules_probes_default[i].name); | |
750 | ||
751 | if (!name) { | |
752 | PERROR("strdup probe item"); | |
398d5459 MD |
753 | ret = -ENOMEM; |
754 | goto error; | |
c9d42407 PP |
755 | } |
756 | ||
757 | probes[i].name = name; | |
adfe4f5e | 758 | probes[i].load_policy = kern_modules_probes_default[i].load_policy; |
c9d42407 PP |
759 | } |
760 | } | |
761 | ||
762 | /* | |
763 | * Extra modules? Append them to current probes list. | |
764 | */ | |
412d7227 | 765 | list = the_config.kmod_extra_probes_list.value; |
c9d42407 PP |
766 | if (list) { |
767 | ret = append_list_to_probes(list); | |
768 | if (ret) { | |
398d5459 | 769 | goto error; |
c9d42407 PP |
770 | } |
771 | } | |
772 | ||
773 | /* | |
774 | * Load probes modules now. | |
775 | */ | |
adfe4f5e | 776 | ret = modprobe_lttng(probes, nr_probes); |
398d5459 MD |
777 | if (ret) { |
778 | goto error; | |
779 | } | |
780 | return ret; | |
781 | ||
782 | error: | |
783 | free_probes(); | |
784 | return ret; | |
096102bd | 785 | } |