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