Port: Remove _GNU_SOURCE, defined in config.h
[lttng-tools.git] / src / bin / lttng-sessiond / modprobe.c
1 /*
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
3 * 2014 - Jan Glauber <jan.glauber@gmail.com>
4 *
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.
8 *
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.
13 *
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.
17 */
18
19 #define _LGPL_SOURCE
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/wait.h>
24
25 #include <common/common.h>
26 #include <common/utils.h>
27
28 #include "modprobe.h"
29 #include "kern-modules.h"
30
31 #define LTTNG_MOD_REQUIRED 1
32 #define LTTNG_MOD_OPTIONAL 0
33
34 /* LTTng kernel tracer mandatory core modules list */
35 struct 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 */
47 struct kern_modules_param kern_modules_control_opt[] = {
48 { "lttng-types" },
49 { "lttng-ftrace" },
50 { "lttng-kprobes" },
51 { "lttng-kretprobes" },
52 };
53
54 /* LTTng kernel tracer probe modules list */
55 struct kern_modules_param kern_modules_probes_default[] = {
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" },
94 };
95
96 /* dynamic probe modules list */
97 static struct kern_modules_param *probes;
98 static int nr_probes;
99 static int probes_capacity;
100
101 static void modprobe_remove_lttng(const struct kern_modules_param *modules,
102 int entries, int required)
103 {
104 int ret = 0, i;
105 char modprobe[256];
106
107 for (i = entries - 1; i >= 0; i--) {
108 ret = snprintf(modprobe, sizeof(modprobe),
109 "/sbin/modprobe -r -q %s",
110 modules[i].name);
111 if (ret < 0) {
112 PERROR("snprintf modprobe -r");
113 return;
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",
119 modules[i].name);
120 } else if (required && WEXITSTATUS(ret) != 0) {
121 ERR("Unable to remove module %s",
122 modules[i].name);
123 } else {
124 DBG("Modprobe removal successful %s",
125 modules[i].name);
126 }
127 }
128 }
129
130 /*
131 * Remove control kernel module(s) in reverse load order.
132 */
133 void modprobe_remove_lttng_control(void)
134 {
135 modprobe_remove_lttng(kern_modules_control_opt,
136 ARRAY_SIZE(kern_modules_control_opt),
137 LTTNG_MOD_OPTIONAL);
138 modprobe_remove_lttng(kern_modules_control_core,
139 ARRAY_SIZE(kern_modules_control_core),
140 LTTNG_MOD_REQUIRED);
141 }
142
143 static void free_probes(void)
144 {
145 int i;
146
147 if (!probes) {
148 return;
149 }
150 for (i = 0; i < nr_probes; ++i) {
151 free(probes[i].name);
152 }
153 free(probes);
154 probes = NULL;
155 nr_probes = 0;
156 }
157
158 /*
159 * Remove data kernel modules in reverse load order.
160 */
161 void modprobe_remove_lttng_data(void)
162 {
163 if (!probes) {
164 return;
165 }
166 modprobe_remove_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
167 free_probes();
168 }
169
170 /*
171 * Remove all kernel modules in reverse order.
172 */
173 void modprobe_remove_lttng_all(void)
174 {
175 modprobe_remove_lttng_data();
176 modprobe_remove_lttng_control();
177 }
178
179 #if HAVE_KMOD
180 #include <libkmod.h>
181 static void log_kmod(void *data, int priority, const char *file, int line,
182 const char *fn, const char *format, va_list args)
183 {
184 char *str;
185
186 if (vasprintf(&str, format, args) < 0) {
187 return;
188 }
189
190 DBG("libkmod: %s", str);
191 free(str);
192 }
193 static int modprobe_lttng(struct kern_modules_param *modules,
194 int entries, int required)
195 {
196 int ret = 0, i;
197 struct kmod_ctx *ctx;
198
199 ctx = kmod_new(NULL, NULL);
200 if (!ctx) {
201 PERROR("Unable to create kmod library context");
202 ret = -ENOMEM;
203 goto error;
204 }
205
206 kmod_set_log_fn(ctx, log_kmod, NULL);
207 kmod_load_resources(ctx);
208
209 for (i = 0; i < entries; i++) {
210 struct kmod_module *mod = NULL;
211
212 ret = kmod_module_new_from_name(ctx, modules[i].name, &mod);
213 if (ret < 0) {
214 PERROR("Failed to create kmod module for %s", modules[i].name);
215 goto error;
216 }
217
218 ret = kmod_module_probe_insert_module(mod, KMOD_PROBE_IGNORE_LOADED,
219 NULL, NULL, NULL, NULL);
220 if (ret < 0) {
221 if (required) {
222 ERR("Unable to load required module %s",
223 modules[i].name);
224 goto error;
225 } else {
226 DBG("Unable to load optional module %s; continuing",
227 modules[i].name);
228 ret = 0;
229 }
230 } else {
231 DBG("Modprobe successfully %s", modules[i].name);
232 }
233
234 kmod_module_unref(mod);
235 }
236
237 error:
238 if (ctx) {
239 kmod_unref(ctx);
240 }
241 return ret;
242 }
243
244 #else /* HAVE_KMOD */
245
246 static int modprobe_lttng(struct kern_modules_param *modules,
247 int entries, int required)
248 {
249 int ret = 0, i;
250 char modprobe[256];
251
252 for (i = 0; i < entries; i++) {
253 ret = snprintf(modprobe, sizeof(modprobe),
254 "/sbin/modprobe %s%s",
255 required ? "" : "-q ",
256 modules[i].name);
257 if (ret < 0) {
258 PERROR("snprintf modprobe");
259 goto error;
260 }
261 modprobe[sizeof(modprobe) - 1] = '\0';
262 ret = system(modprobe);
263 if (ret == -1) {
264 if (required) {
265 ERR("Unable to launch modprobe for required module %s",
266 modules[i].name);
267 goto error;
268 } else {
269 DBG("Unable to launch modprobe for optional module %s; continuing",
270 modules[i].name);
271 ret = 0;
272 }
273 } else if (WEXITSTATUS(ret) != 0) {
274 if (required) {
275 ERR("Unable to load required module %s",
276 modules[i].name);
277 goto error;
278 } else {
279 DBG("Unable to load optional module %s; continuing",
280 modules[i].name);
281 ret = 0;
282 }
283 } else {
284 DBG("Modprobe successfully %s", modules[i].name);
285 }
286 }
287
288 error:
289 return ret;
290 }
291
292 #endif /* HAVE_KMOD */
293
294 /*
295 * Load control kernel module(s).
296 */
297 int modprobe_lttng_control(void)
298 {
299 int ret;
300
301 ret = modprobe_lttng(kern_modules_control_core,
302 ARRAY_SIZE(kern_modules_control_core),
303 LTTNG_MOD_REQUIRED);
304 if (ret != 0)
305 return ret;
306 ret = modprobe_lttng(kern_modules_control_opt,
307 ARRAY_SIZE(kern_modules_control_opt),
308 LTTNG_MOD_OPTIONAL);
309 return ret;
310 }
311
312 /**
313 * Grow global list of probes (double capacity or set it to 1 if
314 * currently 0 and copy existing data).
315 */
316 static int grow_probes(void)
317 {
318 int i;
319 struct kern_modules_param *tmp_probes;
320
321 /* Initialize capacity to 1 if 0. */
322 if (probes_capacity == 0) {
323 probes = zmalloc(sizeof(*probes));
324 if (!probes) {
325 PERROR("malloc probe list");
326 return -ENOMEM;
327 }
328
329 probes_capacity = 1;
330 return 0;
331 }
332
333 /* Double size. */
334 probes_capacity *= 2;
335
336 tmp_probes = zmalloc(sizeof(*tmp_probes) * probes_capacity);
337 if (!tmp_probes) {
338 PERROR("malloc probe list");
339 return -ENOMEM;
340 }
341
342 for (i = 0; i < nr_probes; ++i) {
343 /* Move name pointer. */
344 tmp_probes[i].name = probes[i].name;
345 }
346
347 /* Replace probes with larger copy. */
348 free(probes);
349 probes = tmp_probes;
350
351 return 0;
352 }
353
354 /*
355 * Appends a comma-separated list of probes to the global list
356 * of probes.
357 */
358 static int append_list_to_probes(const char *list)
359 {
360 char *next;
361 int ret;
362 char *tmp_list, *cur_list;
363
364 assert(list);
365
366 cur_list = tmp_list = strdup(list);
367 if (!tmp_list) {
368 PERROR("strdup temp list");
369 return -ENOMEM;
370 }
371
372 for (;;) {
373 size_t name_len;
374 struct kern_modules_param *cur_mod;
375
376 next = strtok(cur_list, ",");
377 if (!next) {
378 break;
379 }
380 cur_list = NULL;
381
382 /* filter leading spaces */
383 while (*next == ' ') {
384 next++;
385 }
386
387 if (probes_capacity <= nr_probes) {
388 ret = grow_probes();
389 if (ret) {
390 goto error;
391 }
392 }
393
394 /* Length 13 is "lttng-probe-" + \0 */
395 name_len = strlen(next) + 13;
396
397 cur_mod = &probes[nr_probes];
398 cur_mod->name = zmalloc(name_len);
399 if (!cur_mod->name) {
400 PERROR("malloc probe list");
401 ret = -ENOMEM;
402 goto error;
403 }
404
405 ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next);
406 if (ret < 0) {
407 PERROR("snprintf modprobe name");
408 ret = -ENOMEM;
409 goto error;
410 }
411
412 nr_probes++;
413 }
414
415 free(tmp_list);
416 return 0;
417
418 error:
419 free(tmp_list);
420 free_probes();
421 return ret;
422 }
423
424 /*
425 * Load data kernel module(s).
426 */
427 int modprobe_lttng_data(void)
428 {
429 int ret, i;
430 char *list;
431
432 /*
433 * Base probes: either from command line option, environment
434 * variable or default list.
435 */
436 if (kmod_probes_list) {
437 list = kmod_probes_list;
438 } else {
439 list = utils_get_kmod_probes_list();
440 }
441
442 if (list) {
443 /* User-specified probes. */
444 ret = append_list_to_probes(list);
445 if (ret) {
446 return ret;
447 }
448 } else {
449 /* Default probes. */
450 int def_len = ARRAY_SIZE(kern_modules_probes_default);
451
452 probes = zmalloc(sizeof(*probes) * def_len);
453 if (!probes) {
454 PERROR("malloc probe list");
455 return -ENOMEM;
456 }
457
458 nr_probes = probes_capacity = def_len;
459
460 for (i = 0; i < def_len; ++i) {
461 char* name = strdup(kern_modules_probes_default[i].name);
462
463 if (!name) {
464 PERROR("strdup probe item");
465 ret = -ENOMEM;
466 goto error;
467 }
468
469 probes[i].name = name;
470 }
471 }
472
473 /*
474 * Extra modules? Append them to current probes list.
475 */
476 if (kmod_extra_probes_list) {
477 list = kmod_extra_probes_list;
478 } else {
479 list = utils_get_extra_kmod_probes_list();
480 }
481
482 if (list) {
483 ret = append_list_to_probes(list);
484 if (ret) {
485 goto error;
486 }
487 }
488
489 /*
490 * Load probes modules now.
491 */
492 ret = modprobe_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
493 if (ret) {
494 goto error;
495 }
496 return ret;
497
498 error:
499 free_probes();
500 return ret;
501 }
This page took 0.041279 seconds and 6 git commands to generate.