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