81ce8105a220be9568618ac07ceb032811432127
[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-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
44 /* LTTng kernel tracer probe modules list */
45 struct kern_modules_param kern_modules_probes_default[] = {
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" },
84 { "lttng-probe-x86-irq-vectors" },
85 { "lttng-probe-x86-exceptions" },
86 };
87
88 /* dynamic probe modules list */
89 static struct kern_modules_param *probes;
90 static int nr_probes;
91 static int probes_capacity;
92
93 static void modprobe_remove_lttng(const struct kern_modules_param *modules,
94 int entries, int required)
95 {
96 int ret = 0, i;
97 char modprobe[256];
98
99 for (i = entries - 1; i >= 0; i--) {
100 ret = snprintf(modprobe, sizeof(modprobe),
101 "/sbin/modprobe -r -q %s",
102 modules[i].name);
103 if (ret < 0) {
104 PERROR("snprintf modprobe -r");
105 return;
106 }
107 modprobe[sizeof(modprobe) - 1] = '\0';
108 ret = system(modprobe);
109 if (ret == -1) {
110 ERR("Unable to launch modprobe -r for module %s",
111 modules[i].name);
112 } else if (required && WEXITSTATUS(ret) != 0) {
113 ERR("Unable to remove module %s",
114 modules[i].name);
115 } else {
116 DBG("Modprobe removal successful %s",
117 modules[i].name);
118 }
119 }
120 }
121
122 /*
123 * Remove control kernel module(s) in reverse load order.
124 */
125 void modprobe_remove_lttng_control(void)
126 {
127 modprobe_remove_lttng(kern_modules_control_core,
128 ARRAY_SIZE(kern_modules_control_core),
129 LTTNG_MOD_REQUIRED);
130 }
131
132 static void free_probes(void)
133 {
134 int i;
135
136 if (!probes) {
137 return;
138 }
139 for (i = 0; i < nr_probes; ++i) {
140 free(probes[i].name);
141 }
142 free(probes);
143 probes = NULL;
144 nr_probes = 0;
145 }
146
147 /*
148 * Remove data kernel modules in reverse load order.
149 */
150 void modprobe_remove_lttng_data(void)
151 {
152 if (!probes) {
153 return;
154 }
155 modprobe_remove_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
156 free_probes();
157 }
158
159 /*
160 * Remove all kernel modules in reverse order.
161 */
162 void modprobe_remove_lttng_all(void)
163 {
164 modprobe_remove_lttng_data();
165 modprobe_remove_lttng_control();
166 }
167
168 #if HAVE_KMOD
169 #include <libkmod.h>
170 static void log_kmod(void *data, int priority, const char *file, int line,
171 const char *fn, const char *format, va_list args)
172 {
173 char *str;
174
175 if (vasprintf(&str, format, args) < 0) {
176 return;
177 }
178
179 DBG("libkmod: %s", str);
180 free(str);
181 }
182 static int modprobe_lttng(struct kern_modules_param *modules,
183 int entries, int required)
184 {
185 int ret = 0, i;
186 struct kmod_ctx *ctx;
187
188 ctx = kmod_new(NULL, NULL);
189 if (!ctx) {
190 PERROR("Unable to create kmod library context");
191 ret = -ENOMEM;
192 goto error;
193 }
194
195 kmod_set_log_fn(ctx, log_kmod, NULL);
196 kmod_load_resources(ctx);
197
198 for (i = 0; i < entries; i++) {
199 struct kmod_module *mod = NULL;
200
201 ret = kmod_module_new_from_name(ctx, modules[i].name, &mod);
202 if (ret < 0) {
203 PERROR("Failed to create kmod module for %s", modules[i].name);
204 goto error;
205 }
206
207 ret = kmod_module_probe_insert_module(mod, KMOD_PROBE_IGNORE_LOADED,
208 NULL, NULL, NULL, NULL);
209 if (ret < 0) {
210 if (required) {
211 ERR("Unable to load required module %s",
212 modules[i].name);
213 goto error;
214 } else {
215 DBG("Unable to load optional module %s; continuing",
216 modules[i].name);
217 ret = 0;
218 }
219 } else {
220 DBG("Modprobe successfully %s", modules[i].name);
221 }
222
223 kmod_module_unref(mod);
224 }
225
226 error:
227 if (ctx) {
228 kmod_unref(ctx);
229 }
230 return ret;
231 }
232
233 #else /* HAVE_KMOD */
234
235 static int modprobe_lttng(struct kern_modules_param *modules,
236 int entries, int required)
237 {
238 int ret = 0, i;
239 char modprobe[256];
240
241 for (i = 0; i < entries; i++) {
242 ret = snprintf(modprobe, sizeof(modprobe),
243 "/sbin/modprobe %s%s",
244 required ? "" : "-q ",
245 modules[i].name);
246 if (ret < 0) {
247 PERROR("snprintf modprobe");
248 goto error;
249 }
250 modprobe[sizeof(modprobe) - 1] = '\0';
251 ret = system(modprobe);
252 if (ret == -1) {
253 if (required) {
254 ERR("Unable to launch modprobe for required module %s",
255 modules[i].name);
256 goto error;
257 } else {
258 DBG("Unable to launch modprobe for optional module %s; continuing",
259 modules[i].name);
260 ret = 0;
261 }
262 } else if (WEXITSTATUS(ret) != 0) {
263 if (required) {
264 ERR("Unable to load required module %s",
265 modules[i].name);
266 goto error;
267 } else {
268 DBG("Unable to load optional module %s; continuing",
269 modules[i].name);
270 ret = 0;
271 }
272 } else {
273 DBG("Modprobe successfully %s", modules[i].name);
274 }
275 }
276
277 error:
278 return ret;
279 }
280
281 #endif /* HAVE_KMOD */
282
283 /*
284 * Load control kernel module(s).
285 */
286 int modprobe_lttng_control(void)
287 {
288 int ret;
289
290 ret = modprobe_lttng(kern_modules_control_core,
291 ARRAY_SIZE(kern_modules_control_core),
292 LTTNG_MOD_REQUIRED);
293 return ret;
294 }
295
296 /**
297 * Grow global list of probes (double capacity or set it to 1 if
298 * currently 0 and copy existing data).
299 */
300 static int grow_probes(void)
301 {
302 int i;
303 struct kern_modules_param *tmp_probes;
304
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;
315 }
316
317 /* Double size. */
318 probes_capacity *= 2;
319
320 tmp_probes = zmalloc(sizeof(*tmp_probes) * probes_capacity);
321 if (!tmp_probes) {
322 PERROR("malloc probe list");
323 return -ENOMEM;
324 }
325
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 */
342 static int append_list_to_probes(const char *list)
343 {
344 char *next;
345 int ret;
346 char *tmp_list, *cur_list;
347
348 assert(list);
349
350 cur_list = tmp_list = strdup(list);
351 if (!tmp_list) {
352 PERROR("strdup temp list");
353 return -ENOMEM;
354 }
355
356 for (;;) {
357 size_t name_len;
358 struct kern_modules_param *cur_mod;
359
360 next = strtok(cur_list, ",");
361 if (!next) {
362 break;
363 }
364 cur_list = NULL;
365
366 /* filter leading spaces */
367 while (*next == ' ') {
368 next++;
369 }
370
371 if (probes_capacity <= nr_probes) {
372 ret = grow_probes();
373 if (ret) {
374 goto error;
375 }
376 }
377
378 /* Length 13 is "lttng-probe-" + \0 */
379 name_len = strlen(next) + 13;
380
381 cur_mod = &probes[nr_probes];
382 cur_mod->name = zmalloc(name_len);
383 if (!cur_mod->name) {
384 PERROR("malloc probe list");
385 ret = -ENOMEM;
386 goto error;
387 }
388
389 ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next);
390 if (ret < 0) {
391 PERROR("snprintf modprobe name");
392 ret = -ENOMEM;
393 goto error;
394 }
395
396 nr_probes++;
397 }
398
399 free(tmp_list);
400 return 0;
401
402 error:
403 free(tmp_list);
404 free_probes();
405 return ret;
406 }
407
408 /*
409 * Load data kernel module(s).
410 */
411 int 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);
429 if (ret) {
430 return ret;
431 }
432 } else {
433 /* Default probes. */
434 int def_len = ARRAY_SIZE(kern_modules_probes_default);
435
436 probes = zmalloc(sizeof(*probes) * def_len);
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");
449 ret = -ENOMEM;
450 goto error;
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) {
469 goto error;
470 }
471 }
472
473 /*
474 * Load probes modules now.
475 */
476 ret = modprobe_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
477 if (ret) {
478 goto error;
479 }
480 return ret;
481
482 error:
483 free_probes();
484 return ret;
485 }
This page took 0.037889 seconds and 3 git commands to generate.