Move libkmod ifdef to beginning of file
[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
6c1c0768 19#define _LGPL_SOURCE
c9d42407 20#include <assert.h>
096102bd
DG
21#include <stdio.h>
22#include <stdlib.h>
23#include <sys/wait.h>
24
25#include <common/common.h>
fbb9748b 26#include <common/utils.h>
096102bd
DG
27
28#include "modprobe.h"
29#include "kern-modules.h"
30
ab57d7d3
JG
31#define LTTNG_MOD_REQUIRED 1
32#define LTTNG_MOD_OPTIONAL 0
33
34/* LTTng kernel tracer mandatory core modules list */
35struct kern_modules_param kern_modules_control_core[] = {
ab57d7d3
JG
36 { "lttng-ring-buffer-client-discard" },
37 { "lttng-ring-buffer-client-overwrite" },
38 { "lttng-ring-buffer-metadata-client" },
39 { "lttng-ring-buffer-client-mmap-discard" },
40 { "lttng-ring-buffer-client-mmap-overwrite" },
41 { "lttng-ring-buffer-metadata-mmap-client" },
42};
43
3fa9646c 44/* LTTng kernel tracer probe modules list */
fbb9748b 45struct kern_modules_param kern_modules_probes_default[] = {
ab57d7d3
JG
46 { "lttng-probe-asoc" },
47 { "lttng-probe-block" },
48 { "lttng-probe-btrfs" },
49 { "lttng-probe-compaction" },
50 { "lttng-probe-ext3" },
51 { "lttng-probe-ext4" },
52 { "lttng-probe-gpio" },
53 { "lttng-probe-irq" },
54 { "lttng-probe-jbd" },
55 { "lttng-probe-jbd2" },
56 { "lttng-probe-kmem" },
57 { "lttng-probe-kvm" },
58 { "lttng-probe-kvm-x86" },
59 { "lttng-probe-kvm-x86-mmu" },
60 { "lttng-probe-lock" },
61 { "lttng-probe-module" },
62 { "lttng-probe-napi" },
63 { "lttng-probe-net" },
64 { "lttng-probe-power" },
65 { "lttng-probe-printk" },
66 { "lttng-probe-random" },
67 { "lttng-probe-rcu" },
68 { "lttng-probe-regmap" },
69 { "lttng-probe-regulator" },
70 { "lttng-probe-rpm" },
71 { "lttng-probe-sched" },
72 { "lttng-probe-scsi" },
73 { "lttng-probe-signal" },
74 { "lttng-probe-skb" },
75 { "lttng-probe-sock" },
76 { "lttng-probe-statedump" },
77 { "lttng-probe-sunrpc" },
78 { "lttng-probe-timer" },
79 { "lttng-probe-udp" },
80 { "lttng-probe-vmscan" },
81 { "lttng-probe-v4l2" },
82 { "lttng-probe-workqueue" },
83 { "lttng-probe-writeback" },
c66dfab8 84 { "lttng-probe-x86-irq-vectors" },
037e00be 85 { "lttng-probe-x86-exceptions" },
096102bd
DG
86};
87
fbb9748b
JG
88/* dynamic probe modules list */
89static struct kern_modules_param *probes;
90static int nr_probes;
c9d42407 91static int probes_capacity;
fbb9748b 92
234170ac
UTL
93#if HAVE_KMOD
94#include <libkmod.h>
95static void log_kmod(void *data, int priority, const char *file, int line,
96 const char *fn, const char *format, va_list args)
97{
98 char *str;
99
100 if (vasprintf(&str, format, args) < 0) {
101 return;
102 }
103
104 DBG("libkmod: %s", str);
105 free(str);
106}
107static int modprobe_lttng(struct kern_modules_param *modules,
108 int entries, int required)
109{
110 int ret = 0, i;
111 struct kmod_ctx *ctx;
112
113 ctx = kmod_new(NULL, NULL);
114 if (!ctx) {
115 PERROR("Unable to create kmod library context");
116 ret = -ENOMEM;
117 goto error;
118 }
119
120 kmod_set_log_fn(ctx, log_kmod, NULL);
121 kmod_load_resources(ctx);
122
123 for (i = 0; i < entries; i++) {
124 struct kmod_module *mod = NULL;
125
126 ret = kmod_module_new_from_name(ctx, modules[i].name, &mod);
127 if (ret < 0) {
128 PERROR("Failed to create kmod module for %s", modules[i].name);
129 goto error;
130 }
131
132 ret = kmod_module_probe_insert_module(mod, KMOD_PROBE_IGNORE_LOADED,
133 NULL, NULL, NULL, NULL);
16c2e854
PP
134 if (ret < 0) {
135 if (required) {
136 ERR("Unable to load required module %s",
137 modules[i].name);
138 goto error;
139 } else {
140 DBG("Unable to load optional module %s; continuing",
141 modules[i].name);
142 ret = 0;
143 }
234170ac
UTL
144 } else {
145 DBG("Modprobe successfully %s", modules[i].name);
146 }
147
148 kmod_module_unref(mod);
149 }
150
151error:
152 if (ctx) {
153 kmod_unref(ctx);
154 }
155 return ret;
156}
157
158#else /* HAVE_KMOD */
159
fbb9748b 160static int modprobe_lttng(struct kern_modules_param *modules,
234170ac 161 int entries, int required)
096102bd
DG
162{
163 int ret = 0, i;
164 char modprobe[256];
165
e23b81ed 166 for (i = 0; i < entries; i++) {
096102bd
DG
167 ret = snprintf(modprobe, sizeof(modprobe),
168 "/sbin/modprobe %s%s",
ab57d7d3 169 required ? "" : "-q ",
e23b81ed 170 modules[i].name);
096102bd
DG
171 if (ret < 0) {
172 PERROR("snprintf modprobe");
173 goto error;
174 }
175 modprobe[sizeof(modprobe) - 1] = '\0';
176 ret = system(modprobe);
177 if (ret == -1) {
16c2e854
PP
178 if (required) {
179 ERR("Unable to launch modprobe for required module %s",
180 modules[i].name);
181 goto error;
182 } else {
183 DBG("Unable to launch modprobe for optional module %s; continuing",
184 modules[i].name);
185 ret = 0;
186 }
187 } else if (WEXITSTATUS(ret) != 0) {
188 if (required) {
189 ERR("Unable to load required module %s",
190 modules[i].name);
191 goto error;
192 } else {
193 DBG("Unable to load optional module %s; continuing",
194 modules[i].name);
195 ret = 0;
196 }
096102bd 197 } else {
ab57d7d3 198 DBG("Modprobe successfully %s", modules[i].name);
096102bd
DG
199 }
200 }
201
202error:
203 return ret;
204}
205
234170ac
UTL
206#endif /* HAVE_KMOD */
207
35e090b7
MJ
208static void modprobe_remove_lttng(const struct kern_modules_param *modules,
209 int entries, int required)
210{
211 int ret = 0, i;
212 char modprobe[256];
213
214 for (i = entries - 1; i >= 0; i--) {
215 ret = snprintf(modprobe, sizeof(modprobe),
216 "/sbin/modprobe -r -q %s",
217 modules[i].name);
218 if (ret < 0) {
219 PERROR("snprintf modprobe -r");
220 return;
221 }
222 modprobe[sizeof(modprobe) - 1] = '\0';
223 ret = system(modprobe);
224 if (ret == -1) {
225 ERR("Unable to launch modprobe -r for module %s",
226 modules[i].name);
227 } else if (required && WEXITSTATUS(ret) != 0) {
228 ERR("Unable to remove module %s",
229 modules[i].name);
230 } else {
231 DBG("Modprobe removal successful %s",
232 modules[i].name);
233 }
234 }
235}
236
237/*
238 * Remove control kernel module(s) in reverse load order.
239 */
240void modprobe_remove_lttng_control(void)
241{
242 modprobe_remove_lttng(kern_modules_control_core,
243 ARRAY_SIZE(kern_modules_control_core),
244 LTTNG_MOD_REQUIRED);
245}
246
247static void free_probes(void)
248{
249 int i;
250
251 if (!probes) {
252 return;
253 }
254 for (i = 0; i < nr_probes; ++i) {
255 free(probes[i].name);
256 }
257 free(probes);
258 probes = NULL;
259 nr_probes = 0;
260}
261
262/*
263 * Remove data kernel modules in reverse load order.
264 */
265void modprobe_remove_lttng_data(void)
266{
267 if (!probes) {
268 return;
269 }
270 modprobe_remove_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
271 free_probes();
272}
273
274/*
275 * Remove all kernel modules in reverse order.
276 */
277void modprobe_remove_lttng_all(void)
278{
279 modprobe_remove_lttng_data();
280 modprobe_remove_lttng_control();
281}
282
e23b81ed
JG
283/*
284 * Load control kernel module(s).
285 */
286int modprobe_lttng_control(void)
287{
ab57d7d3
JG
288 int ret;
289
290 ret = modprobe_lttng(kern_modules_control_core,
291 ARRAY_SIZE(kern_modules_control_core),
292 LTTNG_MOD_REQUIRED);
ab57d7d3 293 return ret;
e23b81ed 294}
ab57d7d3 295
c9d42407
PP
296/**
297 * Grow global list of probes (double capacity or set it to 1 if
298 * currently 0 and copy existing data).
096102bd 299 */
c9d42407 300static int grow_probes(void)
096102bd 301{
c9d42407
PP
302 int i;
303 struct kern_modules_param *tmp_probes;
fbb9748b 304
c9d42407
PP
305 /* Initialize capacity to 1 if 0. */
306 if (probes_capacity == 0) {
307 probes = zmalloc(sizeof(*probes));
308 if (!probes) {
309 PERROR("malloc probe list");
310 return -ENOMEM;
311 }
312
313 probes_capacity = 1;
314 return 0;
fbb9748b
JG
315 }
316
c9d42407
PP
317 /* Double size. */
318 probes_capacity *= 2;
319
320 tmp_probes = zmalloc(sizeof(*tmp_probes) * probes_capacity);
321 if (!tmp_probes) {
fbb9748b
JG
322 PERROR("malloc probe list");
323 return -ENOMEM;
324 }
325
c9d42407
PP
326 for (i = 0; i < nr_probes; ++i) {
327 /* Move name pointer. */
328 tmp_probes[i].name = probes[i].name;
329 }
330
331 /* Replace probes with larger copy. */
332 free(probes);
333 probes = tmp_probes;
334
335 return 0;
336}
337
338/*
339 * Appends a comma-separated list of probes to the global list
340 * of probes.
341 */
342static int append_list_to_probes(const char *list)
343{
344 char *next;
d3c04b7c 345 int ret;
44603c80 346 char *tmp_list, *cur_list;
c9d42407
PP
347
348 assert(list);
349
44603c80 350 cur_list = tmp_list = strdup(list);
c9d42407
PP
351 if (!tmp_list) {
352 PERROR("strdup temp list");
353 return -ENOMEM;
354 }
355
356 for (;;) {
fbb9748b 357 size_t name_len;
c9d42407 358 struct kern_modules_param *cur_mod;
fbb9748b 359
44603c80 360 next = strtok(cur_list, ",");
fbb9748b 361 if (!next) {
c9d42407 362 break;
fbb9748b 363 }
44603c80 364 cur_list = NULL;
fbb9748b
JG
365
366 /* filter leading spaces */
367 while (*next == ' ') {
368 next++;
369 }
370
c9d42407
PP
371 if (probes_capacity <= nr_probes) {
372 ret = grow_probes();
373 if (ret) {
398d5459 374 goto error;
c9d42407
PP
375 }
376 }
377
fbb9748b
JG
378 /* Length 13 is "lttng-probe-" + \0 */
379 name_len = strlen(next) + 13;
380
d3c04b7c 381 cur_mod = &probes[nr_probes];
c9d42407
PP
382 cur_mod->name = zmalloc(name_len);
383 if (!cur_mod->name) {
fbb9748b 384 PERROR("malloc probe list");
398d5459
MD
385 ret = -ENOMEM;
386 goto error;
fbb9748b
JG
387 }
388
c9d42407 389 ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next);
fbb9748b
JG
390 if (ret < 0) {
391 PERROR("snprintf modprobe name");
398d5459
MD
392 ret = -ENOMEM;
393 goto error;
fbb9748b 394 }
c9d42407 395
c9d42407 396 nr_probes++;
fbb9748b
JG
397 }
398
c9d42407 399 free(tmp_list);
c9d42407 400 return 0;
398d5459
MD
401
402error:
403 free(tmp_list);
404 free_probes();
405 return ret;
c9d42407
PP
406}
407
408/*
409 * Load data kernel module(s).
410 */
411int modprobe_lttng_data(void)
412{
413 int ret, i;
414 char *list;
415
416 /*
417 * Base probes: either from command line option, environment
418 * variable or default list.
419 */
420 if (kmod_probes_list) {
421 list = kmod_probes_list;
422 } else {
423 list = utils_get_kmod_probes_list();
424 }
425
426 if (list) {
427 /* User-specified probes. */
428 ret = append_list_to_probes(list);
c9d42407
PP
429 if (ret) {
430 return ret;
431 }
432 } else {
433 /* Default probes. */
434 int def_len = ARRAY_SIZE(kern_modules_probes_default);
c9d42407 435
62e0422e 436 probes = zmalloc(sizeof(*probes) * def_len);
c9d42407
PP
437 if (!probes) {
438 PERROR("malloc probe list");
439 return -ENOMEM;
440 }
441
442 nr_probes = probes_capacity = def_len;
443
444 for (i = 0; i < def_len; ++i) {
445 char* name = strdup(kern_modules_probes_default[i].name);
446
447 if (!name) {
448 PERROR("strdup probe item");
398d5459
MD
449 ret = -ENOMEM;
450 goto error;
c9d42407
PP
451 }
452
453 probes[i].name = name;
454 }
455 }
456
457 /*
458 * Extra modules? Append them to current probes list.
459 */
460 if (kmod_extra_probes_list) {
461 list = kmod_extra_probes_list;
462 } else {
463 list = utils_get_extra_kmod_probes_list();
464 }
465
466 if (list) {
467 ret = append_list_to_probes(list);
468 if (ret) {
398d5459 469 goto error;
c9d42407
PP
470 }
471 }
472
473 /*
474 * Load probes modules now.
475 */
398d5459
MD
476 ret = modprobe_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
477 if (ret) {
478 goto error;
479 }
480 return ret;
481
482error:
483 free_probes();
484 return ret;
096102bd 485}
This page took 0.071455 seconds and 5 git commands to generate.