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