relayd: send sessiond uuid and session id as part of create session
[lttng-tools.git] / src / bin / lttng-sessiond / modprobe.c
CommitLineData
096102bd
DG
1/*
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
fbb9748b 3 * 2014 - Jan Glauber <jan.glauber@gmail.com>
096102bd 4 *
d14d33bf
AM
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.
096102bd 8 *
d14d33bf
AM
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.
096102bd 13 *
d14d33bf
AM
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.
096102bd
DG
17 */
18
d11b2027
MJ
19/**
20 * @file modprobe.c
21 *
22 * @brief modprobe related functions.
23 *
24 */
25
6c1c0768 26#define _LGPL_SOURCE
c9d42407 27#include <assert.h>
096102bd
DG
28#include <stdio.h>
29#include <stdlib.h>
30#include <sys/wait.h>
31
32#include <common/common.h>
fbb9748b 33#include <common/utils.h>
096102bd
DG
34
35#include "modprobe.h"
36#include "kern-modules.h"
e6142f2e 37#include "lttng-sessiond.h"
096102bd 38
ab57d7d3
JG
39#define LTTNG_MOD_REQUIRED 1
40#define LTTNG_MOD_OPTIONAL 0
41
42/* LTTng kernel tracer mandatory core modules list */
43struct kern_modules_param kern_modules_control_core[] = {
ab57d7d3
JG
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
3fa9646c 52/* LTTng kernel tracer probe modules list */
fbb9748b 53struct kern_modules_param kern_modules_probes_default[] = {
ab57d7d3
JG
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" },
0b1c5ad5 61 { "lttng-probe-i2c" },
ab57d7d3
JG
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" },
bc6bc7c5 74 { "lttng-probe-preemptirq" },
ab57d7d3
JG
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" },
c66dfab8 94 { "lttng-probe-x86-irq-vectors" },
037e00be 95 { "lttng-probe-x86-exceptions" },
096102bd
DG
96};
97
fbb9748b
JG
98/* dynamic probe modules list */
99static struct kern_modules_param *probes;
100static int nr_probes;
c9d42407 101static int probes_capacity;
fbb9748b 102
234170ac
UTL
103#if HAVE_KMOD
104#include <libkmod.h>
866c17ce 105
d11b2027
MJ
106/**
107 * @brief Logging function for libkmod integration.
108 */
234170ac
UTL
109static 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}
866c17ce 121
d11b2027
MJ
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 */
866c17ce 131static int setup_kmod_ctx(struct kmod_ctx **ctx)
234170ac 132{
866c17ce 133 int ret = 0;
234170ac 134
866c17ce 135 *ctx = kmod_new(NULL, NULL);
234170ac
UTL
136 if (!ctx) {
137 PERROR("Unable to create kmod library context");
138 ret = -ENOMEM;
139 goto error;
140 }
141
866c17ce
MJ
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
149error:
150 return ret;
151}
152
d11b2027
MJ
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 */
866c17ce
MJ
166static 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 }
234170ac
UTL
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
0b1e16b8 186 ret = kmod_module_probe_insert_module(mod, 0,
234170ac 187 NULL, NULL, NULL, NULL);
0b1e16b8
MJ
188 if (ret == -EEXIST) {
189 DBG("Module %s is already loaded", modules[i].name);
190 ret = 0;
191 } else if (ret < 0) {
16c2e854
PP
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 }
234170ac
UTL
201 } else {
202 DBG("Modprobe successfully %s", modules[i].name);
4ad664a0 203 modules[i].loaded = true;
234170ac
UTL
204 }
205
206 kmod_module_unref(mod);
207 }
208
209error:
210 if (ctx) {
211 kmod_unref(ctx);
212 }
213 return ret;
214}
215
d11b2027
MJ
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 */
866c17ce
MJ
228static 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
d11b2027
MJ
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 */
866c17ce
MJ
264static 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
4ad664a0
JG
278 if (!modules[i].loaded) {
279 continue;
280 }
281
866c17ce
MJ
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
301error:
302 if (ctx) {
303 kmod_unref(ctx);
304 }
305}
306
234170ac
UTL
307#else /* HAVE_KMOD */
308
fbb9748b 309static int modprobe_lttng(struct kern_modules_param *modules,
234170ac 310 int entries, int required)
096102bd
DG
311{
312 int ret = 0, i;
313 char modprobe[256];
314
e23b81ed 315 for (i = 0; i < entries; i++) {
096102bd
DG
316 ret = snprintf(modprobe, sizeof(modprobe),
317 "/sbin/modprobe %s%s",
ab57d7d3 318 required ? "" : "-q ",
e23b81ed 319 modules[i].name);
096102bd
DG
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) {
16c2e854
PP
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 }
096102bd 346 } else {
ab57d7d3 347 DBG("Modprobe successfully %s", modules[i].name);
355d2778 348 modules[i].loaded = true;
096102bd
DG
349 }
350 }
351
352error:
353 return ret;
354}
355
35e090b7
MJ
356static 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--) {
4ad664a0
JG
363 if (!modules[i].loaded) {
364 continue;
365 }
35e090b7
MJ
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
866c17ce
MJ
388#endif /* HAVE_KMOD */
389
35e090b7
MJ
390/*
391 * Remove control kernel module(s) in reverse load order.
392 */
393void 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
400static 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 */
418void 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 */
430void modprobe_remove_lttng_all(void)
431{
432 modprobe_remove_lttng_data();
433 modprobe_remove_lttng_control();
434}
435
e23b81ed
JG
436/*
437 * Load control kernel module(s).
438 */
439int modprobe_lttng_control(void)
440{
ab57d7d3
JG
441 int ret;
442
443 ret = modprobe_lttng(kern_modules_control_core,
444 ARRAY_SIZE(kern_modules_control_core),
445 LTTNG_MOD_REQUIRED);
ab57d7d3 446 return ret;
e23b81ed 447}
ab57d7d3 448
c9d42407
PP
449/**
450 * Grow global list of probes (double capacity or set it to 1 if
451 * currently 0 and copy existing data).
096102bd 452 */
c9d42407 453static int grow_probes(void)
096102bd 454{
c9d42407
PP
455 int i;
456 struct kern_modules_param *tmp_probes;
fbb9748b 457
c9d42407
PP
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;
fbb9748b
JG
468 }
469
c9d42407
PP
470 /* Double size. */
471 probes_capacity *= 2;
472
473 tmp_probes = zmalloc(sizeof(*tmp_probes) * probes_capacity);
474 if (!tmp_probes) {
fbb9748b
JG
475 PERROR("malloc probe list");
476 return -ENOMEM;
477 }
478
c9d42407
PP
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 */
495static int append_list_to_probes(const char *list)
496{
497 char *next;
d3c04b7c 498 int ret;
44603c80 499 char *tmp_list, *cur_list;
c9d42407
PP
500
501 assert(list);
502
44603c80 503 cur_list = tmp_list = strdup(list);
c9d42407
PP
504 if (!tmp_list) {
505 PERROR("strdup temp list");
506 return -ENOMEM;
507 }
508
509 for (;;) {
fbb9748b 510 size_t name_len;
c9d42407 511 struct kern_modules_param *cur_mod;
fbb9748b 512
44603c80 513 next = strtok(cur_list, ",");
fbb9748b 514 if (!next) {
c9d42407 515 break;
fbb9748b 516 }
44603c80 517 cur_list = NULL;
fbb9748b
JG
518
519 /* filter leading spaces */
520 while (*next == ' ') {
521 next++;
522 }
523
c9d42407
PP
524 if (probes_capacity <= nr_probes) {
525 ret = grow_probes();
526 if (ret) {
398d5459 527 goto error;
c9d42407
PP
528 }
529 }
530
fbb9748b
JG
531 /* Length 13 is "lttng-probe-" + \0 */
532 name_len = strlen(next) + 13;
533
d3c04b7c 534 cur_mod = &probes[nr_probes];
c9d42407
PP
535 cur_mod->name = zmalloc(name_len);
536 if (!cur_mod->name) {
fbb9748b 537 PERROR("malloc probe list");
398d5459
MD
538 ret = -ENOMEM;
539 goto error;
fbb9748b
JG
540 }
541
c9d42407 542 ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next);
fbb9748b
JG
543 if (ret < 0) {
544 PERROR("snprintf modprobe name");
398d5459
MD
545 ret = -ENOMEM;
546 goto error;
fbb9748b 547 }
c9d42407 548
c9d42407 549 nr_probes++;
fbb9748b
JG
550 }
551
c9d42407 552 free(tmp_list);
c9d42407 553 return 0;
398d5459
MD
554
555error:
556 free(tmp_list);
557 free_probes();
558 return ret;
c9d42407
PP
559}
560
561/*
562 * Load data kernel module(s).
563 */
564int 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 */
e6142f2e 573 list = config.kmod_probes_list.value;
c9d42407
PP
574 if (list) {
575 /* User-specified probes. */
576 ret = append_list_to_probes(list);
c9d42407
PP
577 if (ret) {
578 return ret;
579 }
580 } else {
581 /* Default probes. */
582 int def_len = ARRAY_SIZE(kern_modules_probes_default);
c9d42407 583
62e0422e 584 probes = zmalloc(sizeof(*probes) * def_len);
c9d42407
PP
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");
398d5459
MD
597 ret = -ENOMEM;
598 goto error;
c9d42407
PP
599 }
600
601 probes[i].name = name;
602 }
603 }
604
605 /*
606 * Extra modules? Append them to current probes list.
607 */
e6142f2e 608 list = config.kmod_extra_probes_list.value;
c9d42407
PP
609 if (list) {
610 ret = append_list_to_probes(list);
611 if (ret) {
398d5459 612 goto error;
c9d42407
PP
613 }
614 }
615
616 /*
617 * Load probes modules now.
618 */
398d5459
MD
619 ret = modprobe_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
620 if (ret) {
621 goto error;
622 }
623 return ret;
624
625error:
626 free_probes();
627 return ret;
096102bd 628}
This page took 0.145061 seconds and 5 git commands to generate.