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