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