Cleanup: sessiond modprobe.c: coding style
[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
144/*
145 * Remove data kernel modules in reverse load order.
146 */
147void modprobe_remove_lttng_data(void)
148{
c9d42407
PP
149 int i;
150
fbb9748b
JG
151 if (probes) {
152 modprobe_remove_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
c9d42407
PP
153
154 for (i = 0; i < nr_probes; ++i) {
155 free(probes[i].name);
156 }
157
fbb9748b
JG
158 free(probes);
159 probes = NULL;
c9d42407 160 }
096102bd
DG
161}
162
163/*
164 * Remove all kernel modules in reverse order.
165 */
166void modprobe_remove_lttng_all(void)
167{
168 modprobe_remove_lttng_data();
169 modprobe_remove_lttng_control();
170}
171
234170ac
UTL
172#if HAVE_KMOD
173#include <libkmod.h>
174static 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}
186static 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);
16c2e854
PP
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 }
234170ac
UTL
223 } else {
224 DBG("Modprobe successfully %s", modules[i].name);
225 }
226
227 kmod_module_unref(mod);
228 }
229
230error:
231 if (ctx) {
232 kmod_unref(ctx);
233 }
234 return ret;
235}
236
237#else /* HAVE_KMOD */
238
fbb9748b 239static int modprobe_lttng(struct kern_modules_param *modules,
234170ac 240 int entries, int required)
096102bd
DG
241{
242 int ret = 0, i;
243 char modprobe[256];
244
e23b81ed 245 for (i = 0; i < entries; i++) {
096102bd
DG
246 ret = snprintf(modprobe, sizeof(modprobe),
247 "/sbin/modprobe %s%s",
ab57d7d3 248 required ? "" : "-q ",
e23b81ed 249 modules[i].name);
096102bd
DG
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) {
16c2e854
PP
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 }
096102bd 276 } else {
ab57d7d3 277 DBG("Modprobe successfully %s", modules[i].name);
096102bd
DG
278 }
279 }
280
281error:
282 return ret;
283}
284
234170ac
UTL
285#endif /* HAVE_KMOD */
286
e23b81ed
JG
287/*
288 * Load control kernel module(s).
289 */
290int modprobe_lttng_control(void)
291{
ab57d7d3
JG
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,
fbb9748b
JG
300 ARRAY_SIZE(kern_modules_control_opt),
301 LTTNG_MOD_OPTIONAL);
ab57d7d3 302 return ret;
e23b81ed 303}
ab57d7d3 304
c9d42407
PP
305/**
306 * Grow global list of probes (double capacity or set it to 1 if
307 * currently 0 and copy existing data).
096102bd 308 */
c9d42407 309static int grow_probes(void)
096102bd 310{
c9d42407
PP
311 int i;
312 struct kern_modules_param *tmp_probes;
fbb9748b 313
c9d42407
PP
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;
fbb9748b
JG
324 }
325
c9d42407
PP
326 /* Double size. */
327 probes_capacity *= 2;
328
329 tmp_probes = zmalloc(sizeof(*tmp_probes) * probes_capacity);
330 if (!tmp_probes) {
fbb9748b
JG
331 PERROR("malloc probe list");
332 return -ENOMEM;
333 }
334
c9d42407
PP
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 */
351static int append_list_to_probes(const char *list)
352{
353 char *next;
354 int index = nr_probes, ret;
62e0422e 355 char *tmp_list;
c9d42407
PP
356
357 assert(list);
358
62e0422e 359 tmp_list = strdup(list);
c9d42407
PP
360 if (!tmp_list) {
361 PERROR("strdup temp list");
362 return -ENOMEM;
363 }
364
365 for (;;) {
fbb9748b 366 size_t name_len;
c9d42407 367 struct kern_modules_param *cur_mod;
fbb9748b 368
c9d42407 369 next = strtok(tmp_list, ",");
fbb9748b 370 if (!next) {
c9d42407 371 break;
fbb9748b 372 }
c9d42407 373 tmp_list = NULL;
fbb9748b
JG
374
375 /* filter leading spaces */
376 while (*next == ' ') {
377 next++;
378 }
379
c9d42407
PP
380 if (probes_capacity <= nr_probes) {
381 ret = grow_probes();
382 if (ret) {
383 return ret;
384 }
385 }
386
fbb9748b
JG
387 /* Length 13 is "lttng-probe-" + \0 */
388 name_len = strlen(next) + 13;
389
c9d42407
PP
390 cur_mod = &probes[index];
391 cur_mod->name = zmalloc(name_len);
392 if (!cur_mod->name) {
fbb9748b
JG
393 PERROR("malloc probe list");
394 return -ENOMEM;
395 }
396
c9d42407 397 ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next);
fbb9748b
JG
398 if (ret < 0) {
399 PERROR("snprintf modprobe name");
c9d42407 400 return -ENOMEM;
fbb9748b 401 }
c9d42407
PP
402
403 cur_mod++;
404 nr_probes++;
fbb9748b
JG
405 }
406
c9d42407
PP
407 free(tmp_list);
408
409 return 0;
410}
411
412/*
413 * Load data kernel module(s).
414 */
415int modprobe_lttng_data(void)
416{
417 int ret, i;
418 char *list;
419
420 /*
421 * Base probes: either from command line option, environment
422 * variable or default list.
423 */
424 if (kmod_probes_list) {
425 list = kmod_probes_list;
426 } else {
427 list = utils_get_kmod_probes_list();
428 }
429
430 if (list) {
431 /* User-specified probes. */
432 ret = append_list_to_probes(list);
433
434 if (ret) {
435 return ret;
436 }
437 } else {
438 /* Default probes. */
439 int def_len = ARRAY_SIZE(kern_modules_probes_default);
c9d42407 440
62e0422e 441 probes = zmalloc(sizeof(*probes) * def_len);
c9d42407
PP
442 if (!probes) {
443 PERROR("malloc probe list");
444 return -ENOMEM;
445 }
446
447 nr_probes = probes_capacity = def_len;
448
449 for (i = 0; i < def_len; ++i) {
450 char* name = strdup(kern_modules_probes_default[i].name);
451
452 if (!name) {
453 PERROR("strdup probe item");
454 return -ENOMEM;
455 }
456
457 probes[i].name = name;
458 }
459 }
460
461 /*
462 * Extra modules? Append them to current probes list.
463 */
464 if (kmod_extra_probes_list) {
465 list = kmod_extra_probes_list;
466 } else {
467 list = utils_get_extra_kmod_probes_list();
468 }
469
470 if (list) {
471 ret = append_list_to_probes(list);
472 if (ret) {
473 return ret;
474 }
475 }
476
477 /*
478 * Load probes modules now.
479 */
fbb9748b 480 return modprobe_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL);
096102bd 481}
This page took 0.054488 seconds and 4 git commands to generate.