Clean-up append_list_to_probes()
[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 static 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 static 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
159 /*
160 * Remove data kernel modules in reverse load order.
161 */
162 void modprobe_remove_lttng_data(void)
163 {
164 if (!probes) {
165 return;
166 }
167 modprobe_remove_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
168 free_probes();
169 }
170
171 /*
172 * Remove all kernel modules in reverse order.
173 */
174 void modprobe_remove_lttng_all(void)
175 {
176 modprobe_remove_lttng_data();
177 modprobe_remove_lttng_control();
178 }
179
180 #if HAVE_KMOD
181 #include <libkmod.h>
182 static 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 }
194 static 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);
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 }
231 } else {
232 DBG("Modprobe successfully %s", modules[i].name);
233 }
234
235 kmod_module_unref(mod);
236 }
237
238 error:
239 if (ctx) {
240 kmod_unref(ctx);
241 }
242 return ret;
243 }
244
245 #else /* HAVE_KMOD */
246
247 static int modprobe_lttng(struct kern_modules_param *modules,
248 int entries, int required)
249 {
250 int ret = 0, i;
251 char modprobe[256];
252
253 for (i = 0; i < entries; i++) {
254 ret = snprintf(modprobe, sizeof(modprobe),
255 "/sbin/modprobe %s%s",
256 required ? "" : "-q ",
257 modules[i].name);
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) {
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 }
284 } else {
285 DBG("Modprobe successfully %s", modules[i].name);
286 }
287 }
288
289 error:
290 return ret;
291 }
292
293 #endif /* HAVE_KMOD */
294
295 /*
296 * Load control kernel module(s).
297 */
298 int modprobe_lttng_control(void)
299 {
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,
308 ARRAY_SIZE(kern_modules_control_opt),
309 LTTNG_MOD_OPTIONAL);
310 return ret;
311 }
312
313 /**
314 * Grow global list of probes (double capacity or set it to 1 if
315 * currently 0 and copy existing data).
316 */
317 static int grow_probes(void)
318 {
319 int i;
320 struct kern_modules_param *tmp_probes;
321
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;
332 }
333
334 /* Double size. */
335 probes_capacity *= 2;
336
337 tmp_probes = zmalloc(sizeof(*tmp_probes) * probes_capacity);
338 if (!tmp_probes) {
339 PERROR("malloc probe list");
340 return -ENOMEM;
341 }
342
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 */
359 static int append_list_to_probes(const char *list)
360 {
361 char *next;
362 int ret;
363 char *tmp_list, *cur_list;
364
365 assert(list);
366
367 cur_list = tmp_list = strdup(list);
368 if (!tmp_list) {
369 PERROR("strdup temp list");
370 return -ENOMEM;
371 }
372
373 for (;;) {
374 size_t name_len;
375 struct kern_modules_param *cur_mod;
376
377 next = strtok(cur_list, ",");
378 if (!next) {
379 break;
380 }
381 cur_list = NULL;
382
383 /* filter leading spaces */
384 while (*next == ' ') {
385 next++;
386 }
387
388 if (probes_capacity <= nr_probes) {
389 ret = grow_probes();
390 if (ret) {
391 goto error;
392 }
393 }
394
395 /* Length 13 is "lttng-probe-" + \0 */
396 name_len = strlen(next) + 13;
397
398 cur_mod = &probes[nr_probes];
399 cur_mod->name = zmalloc(name_len);
400 if (!cur_mod->name) {
401 PERROR("malloc probe list");
402 ret = -ENOMEM;
403 goto error;
404 }
405
406 ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next);
407 if (ret < 0) {
408 PERROR("snprintf modprobe name");
409 ret = -ENOMEM;
410 goto error;
411 }
412
413 nr_probes++;
414 }
415
416 free(tmp_list);
417 return 0;
418
419 error:
420 free(tmp_list);
421 free_probes();
422 return ret;
423 }
424
425 /*
426 * Load data kernel module(s).
427 */
428 int modprobe_lttng_data(void)
429 {
430 int ret, i;
431 char *list;
432
433 /*
434 * Base probes: either from command line option, environment
435 * variable or default list.
436 */
437 if (kmod_probes_list) {
438 list = kmod_probes_list;
439 } else {
440 list = utils_get_kmod_probes_list();
441 }
442
443 if (list) {
444 /* User-specified probes. */
445 ret = append_list_to_probes(list);
446 if (ret) {
447 return ret;
448 }
449 } else {
450 /* Default probes. */
451 int def_len = ARRAY_SIZE(kern_modules_probes_default);
452
453 probes = zmalloc(sizeof(*probes) * def_len);
454 if (!probes) {
455 PERROR("malloc probe list");
456 return -ENOMEM;
457 }
458
459 nr_probes = probes_capacity = def_len;
460
461 for (i = 0; i < def_len; ++i) {
462 char* name = strdup(kern_modules_probes_default[i].name);
463
464 if (!name) {
465 PERROR("strdup probe item");
466 ret = -ENOMEM;
467 goto error;
468 }
469
470 probes[i].name = name;
471 }
472 }
473
474 /*
475 * Extra modules? Append them to current probes list.
476 */
477 if (kmod_extra_probes_list) {
478 list = kmod_extra_probes_list;
479 } else {
480 list = utils_get_extra_kmod_probes_list();
481 }
482
483 if (list) {
484 ret = append_list_to_probes(list);
485 if (ret) {
486 goto error;
487 }
488 }
489
490 /*
491 * Load probes modules now.
492 */
493 ret = modprobe_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
494 if (ret) {
495 goto error;
496 }
497 return ret;
498
499 error:
500 free_probes();
501 return ret;
502 }
This page took 0.040661 seconds and 6 git commands to generate.