| 1 | /* |
| 2 | * Copyright (C) 2011 David Goulet <dgoulet@efficios.com> |
| 3 | * Copyright (C) 2014 Jan Glauber <jan.glauber@gmail.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * @file modprobe.c |
| 11 | * |
| 12 | * @brief modprobe related functions. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #define _LGPL_SOURCE |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/wait.h> |
| 20 | |
| 21 | #include <common/common.hpp> |
| 22 | #include <common/utils.hpp> |
| 23 | |
| 24 | #include "modprobe.hpp" |
| 25 | #include "kern-modules.hpp" |
| 26 | #include "lttng-sessiond.hpp" |
| 27 | |
| 28 | /* LTTng kernel tracer mandatory core modules list */ |
| 29 | struct kern_modules_param kern_modules_control_core[] = { |
| 30 | { |
| 31 | .name = (char *) "lttng-ring-buffer-client-discard", |
| 32 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED, |
| 33 | .loaded = false, |
| 34 | }, |
| 35 | { |
| 36 | .name = (char *) "lttng-ring-buffer-client-overwrite", |
| 37 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED, |
| 38 | .loaded = false, |
| 39 | }, |
| 40 | { |
| 41 | .name = (char *) "lttng-ring-buffer-metadata-client", |
| 42 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED, |
| 43 | .loaded = false, |
| 44 | }, |
| 45 | { |
| 46 | .name = (char *) "lttng-ring-buffer-client-mmap-discard", |
| 47 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED, |
| 48 | .loaded = false, |
| 49 | }, |
| 50 | { |
| 51 | .name = (char *) "lttng-ring-buffer-client-mmap-overwrite", |
| 52 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED, |
| 53 | .loaded = false, |
| 54 | }, |
| 55 | { |
| 56 | .name = (char *) "lttng-ring-buffer-metadata-mmap-client", |
| 57 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED, |
| 58 | .loaded = false, |
| 59 | }, |
| 60 | { |
| 61 | .name = (char *) "lttng-ring-buffer-event_notifier-client", |
| 62 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 63 | .loaded = false, |
| 64 | }, |
| 65 | { |
| 66 | .name = (char *) "lttng-counter-client-percpu-64-modular", |
| 67 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 68 | .loaded = false, |
| 69 | }, |
| 70 | { |
| 71 | .name = (char *) "lttng-counter-client-percpu-32-modular", |
| 72 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 73 | .loaded = false, |
| 74 | }, |
| 75 | }; |
| 76 | |
| 77 | /* LTTng kerneltracer probe modules list */ |
| 78 | struct kern_modules_param kern_modules_probes_default[] = { |
| 79 | { |
| 80 | .name = (char *) "lttng-probe-asoc", |
| 81 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 82 | .loaded = false, |
| 83 | }, |
| 84 | { |
| 85 | .name = (char *) "lttng-probe-block", |
| 86 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 87 | .loaded = false, |
| 88 | }, |
| 89 | { |
| 90 | .name = (char *) "lttng-probe-btrfs", |
| 91 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 92 | .loaded = false, |
| 93 | }, |
| 94 | { |
| 95 | .name = (char *) "lttng-probe-compaction", |
| 96 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 97 | .loaded = false, |
| 98 | }, |
| 99 | { |
| 100 | .name = (char *) "lttng-probe-ext3", |
| 101 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 102 | .loaded = false, |
| 103 | }, |
| 104 | { |
| 105 | .name = (char *) "lttng-probe-ext4", |
| 106 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 107 | .loaded = false, |
| 108 | }, |
| 109 | { |
| 110 | .name = (char *) "lttng-probe-gpio", |
| 111 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 112 | .loaded = false, |
| 113 | }, |
| 114 | { |
| 115 | .name = (char *) "lttng-probe-i2c", |
| 116 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 117 | .loaded = false, |
| 118 | }, |
| 119 | { |
| 120 | .name = (char *) "lttng-probe-irq", |
| 121 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 122 | .loaded = false, |
| 123 | }, |
| 124 | { |
| 125 | .name = (char *) "lttng-probe-jbd", |
| 126 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 127 | .loaded = false, |
| 128 | }, |
| 129 | { |
| 130 | .name = (char *) "lttng-probe-jbd2", |
| 131 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 132 | .loaded = false, |
| 133 | }, |
| 134 | { |
| 135 | .name = (char *) "lttng-probe-kmem", |
| 136 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 137 | .loaded = false, |
| 138 | }, |
| 139 | { |
| 140 | .name = (char *) "lttng-probe-kvm", |
| 141 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 142 | .loaded = false, |
| 143 | }, |
| 144 | { |
| 145 | .name = (char *) "lttng-probe-kvm-x86", |
| 146 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 147 | .loaded = false, |
| 148 | }, |
| 149 | { |
| 150 | .name = (char *) "lttng-probe-kvm-x86-mmu", |
| 151 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 152 | .loaded = false, |
| 153 | }, |
| 154 | { |
| 155 | .name = (char *) "lttng-probe-lock", |
| 156 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 157 | .loaded = false, |
| 158 | }, |
| 159 | { |
| 160 | .name = (char *) "lttng-probe-module", |
| 161 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 162 | .loaded = false, |
| 163 | }, |
| 164 | { |
| 165 | .name = (char *) "lttng-probe-napi", |
| 166 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 167 | .loaded = false, |
| 168 | }, |
| 169 | { |
| 170 | .name = (char *) "lttng-probe-net", |
| 171 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 172 | .loaded = false, |
| 173 | }, |
| 174 | { |
| 175 | .name = (char *) "lttng-probe-power", |
| 176 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 177 | .loaded = false, |
| 178 | }, |
| 179 | { |
| 180 | .name = (char *) "lttng-probe-preemptirq", |
| 181 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 182 | .loaded = false, |
| 183 | }, |
| 184 | { |
| 185 | .name = (char *) "lttng-probe-printk", |
| 186 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 187 | .loaded = false, |
| 188 | }, |
| 189 | { |
| 190 | .name = (char *) "lttng-probe-random", |
| 191 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 192 | .loaded = false, |
| 193 | }, |
| 194 | { |
| 195 | .name = (char *) "lttng-probe-rcu", |
| 196 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 197 | .loaded = false, |
| 198 | }, |
| 199 | { |
| 200 | .name = (char *) "lttng-probe-regmap", |
| 201 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 202 | .loaded = false, |
| 203 | }, |
| 204 | { |
| 205 | .name = (char *) "lttng-probe-regulator", |
| 206 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 207 | .loaded = false, |
| 208 | }, |
| 209 | { |
| 210 | .name = (char *) "lttng-probe-rpm", |
| 211 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 212 | .loaded = false, |
| 213 | }, |
| 214 | { |
| 215 | .name = (char *) "lttng-probe-sched", |
| 216 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 217 | .loaded = false, |
| 218 | }, |
| 219 | { |
| 220 | .name = (char *) "lttng-probe-scsi", |
| 221 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 222 | .loaded = false, |
| 223 | }, |
| 224 | { |
| 225 | .name = (char *) "lttng-probe-signal", |
| 226 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 227 | .loaded = false, |
| 228 | }, |
| 229 | { |
| 230 | .name = (char *) "lttng-probe-skb", |
| 231 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 232 | .loaded = false, |
| 233 | }, |
| 234 | { |
| 235 | .name = (char *) "lttng-probe-sock", |
| 236 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 237 | .loaded = false, |
| 238 | }, |
| 239 | { |
| 240 | .name = (char *) "lttng-probe-statedump", |
| 241 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 242 | .loaded = false, |
| 243 | }, |
| 244 | { |
| 245 | .name = (char *) "lttng-probe-sunrpc", |
| 246 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 247 | .loaded = false, |
| 248 | }, |
| 249 | { |
| 250 | .name = (char *) "lttng-probe-timer", |
| 251 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 252 | .loaded = false, |
| 253 | }, |
| 254 | { |
| 255 | .name = (char *) "lttng-probe-udp", |
| 256 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 257 | .loaded = false, |
| 258 | }, |
| 259 | { |
| 260 | .name = (char *) "lttng-probe-vmscan", |
| 261 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 262 | .loaded = false, |
| 263 | }, |
| 264 | { |
| 265 | .name = (char *) "lttng-probe-v4l2", |
| 266 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 267 | .loaded = false, |
| 268 | }, |
| 269 | { |
| 270 | .name = (char *) "lttng-probe-workqueue", |
| 271 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 272 | .loaded = false, |
| 273 | }, |
| 274 | { |
| 275 | .name = (char *) "lttng-probe-writeback", |
| 276 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 277 | .loaded = false, |
| 278 | }, |
| 279 | { |
| 280 | .name = (char *) "lttng-probe-x86-irq-vectors", |
| 281 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 282 | .loaded = false, |
| 283 | }, |
| 284 | { |
| 285 | .name = (char *) "lttng-probe-x86-exceptions", |
| 286 | .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL, |
| 287 | .loaded = false, |
| 288 | }, |
| 289 | }; |
| 290 | |
| 291 | /* dynamic probe modules list */ |
| 292 | static struct kern_modules_param *probes; |
| 293 | static int nr_probes; |
| 294 | static int probes_capacity; |
| 295 | |
| 296 | #ifdef HAVE_KMOD |
| 297 | #include <libkmod.h> |
| 298 | |
| 299 | /** |
| 300 | * @brief Logging function for libkmod integration. |
| 301 | */ |
| 302 | static ATTR_FORMAT_PRINTF(6, 0) |
| 303 | void log_kmod(void *data __attribute__((unused)), |
| 304 | int priority __attribute__((unused)), |
| 305 | const char *file __attribute__((unused)), |
| 306 | int line __attribute__((unused)), |
| 307 | const char *fn __attribute__((unused)), |
| 308 | const char *format, va_list args) |
| 309 | { |
| 310 | char *str; |
| 311 | |
| 312 | if (vasprintf(&str, format, args) < 0) { |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | DBG("libkmod: %s", str); |
| 317 | free(str); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * @brief Setup the libkmod context. |
| 322 | * |
| 323 | * Create the context, add a custom logging function and preload the |
| 324 | * ressources for faster operation. |
| 325 | * |
| 326 | * @returns \c 0 on success |
| 327 | * \c < 0 on error |
| 328 | */ |
| 329 | static int setup_kmod_ctx(struct kmod_ctx **ctx) |
| 330 | { |
| 331 | int ret = 0; |
| 332 | |
| 333 | *ctx = kmod_new(NULL, NULL); |
| 334 | if (!ctx) { |
| 335 | PERROR("Unable to create kmod library context"); |
| 336 | ret = -ENOMEM; |
| 337 | goto error; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Parameter 2 of kmod_set_log_fn generates a |
| 342 | * -Wsuggest-attribute=formatkmod_set_log_fn warning that we can't fix, |
| 343 | * ignore it. |
| 344 | */ |
| 345 | DIAGNOSTIC_PUSH |
| 346 | DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT |
| 347 | kmod_set_log_fn(*ctx, log_kmod, NULL); |
| 348 | DIAGNOSTIC_POP |
| 349 | ret = kmod_load_resources(*ctx); |
| 350 | if (ret < 0) { |
| 351 | ERR("Failed to load kmod library resources"); |
| 352 | goto error; |
| 353 | } |
| 354 | |
| 355 | error: |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * @brief Loads the kernel modules in \p modules |
| 361 | * |
| 362 | * @param modules List of modules to load |
| 363 | * @param entries Number of modules in the list |
| 364 | * |
| 365 | * If the modules are required, we will return with error after the |
| 366 | * first failed module load, otherwise we continue loading. |
| 367 | * |
| 368 | * @returns \c 0 on success |
| 369 | * \c < 0 on error |
| 370 | */ |
| 371 | static int modprobe_lttng(struct kern_modules_param *modules, |
| 372 | int entries) |
| 373 | { |
| 374 | int ret = 0, i; |
| 375 | struct kmod_ctx *ctx; |
| 376 | |
| 377 | ret = setup_kmod_ctx(&ctx); |
| 378 | if (ret < 0) { |
| 379 | goto error; |
| 380 | } |
| 381 | |
| 382 | for (i = 0; i < entries; i++) { |
| 383 | struct kmod_module *mod = NULL; |
| 384 | |
| 385 | ret = kmod_module_new_from_name(ctx, modules[i].name, &mod); |
| 386 | if (ret < 0) { |
| 387 | PERROR("Failed to create kmod module for %s", modules[i].name); |
| 388 | goto error; |
| 389 | } |
| 390 | |
| 391 | ret = kmod_module_probe_insert_module(mod, 0, |
| 392 | NULL, NULL, NULL, NULL); |
| 393 | if (ret == -EEXIST) { |
| 394 | DBG("Module %s is already loaded", modules[i].name); |
| 395 | ret = 0; |
| 396 | } else if (ret < 0) { |
| 397 | if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) { |
| 398 | ERR("Unable to load required module %s", |
| 399 | modules[i].name); |
| 400 | goto error; |
| 401 | } else { |
| 402 | DBG("Unable to load optional module %s; continuing", |
| 403 | modules[i].name); |
| 404 | ret = 0; |
| 405 | } |
| 406 | } else { |
| 407 | DBG("Modprobe successfully %s", modules[i].name); |
| 408 | modules[i].loaded = true; |
| 409 | } |
| 410 | |
| 411 | kmod_module_unref(mod); |
| 412 | } |
| 413 | |
| 414 | error: |
| 415 | if (ctx) { |
| 416 | kmod_unref(ctx); |
| 417 | } |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * @brief Recursively unload modules. |
| 423 | * |
| 424 | * This function implements the same modules unloading behavior as |
| 425 | * 'modprobe -r' or rmmod, it will recursevily go trought the \p module |
| 426 | * dependencies and unload modules with a refcount of 0. |
| 427 | * |
| 428 | * @param mod The module to unload |
| 429 | * |
| 430 | * @returns \c 0 on success |
| 431 | * \c < 0 on error |
| 432 | */ |
| 433 | static int rmmod_recurse(struct kmod_module *mod) { |
| 434 | int ret = 0; |
| 435 | struct kmod_list *deps, *itr; |
| 436 | |
| 437 | if (kmod_module_get_initstate(mod) == KMOD_MODULE_BUILTIN) { |
| 438 | DBG("Module %s is builtin", kmod_module_get_name(mod)); |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | ret = kmod_module_remove_module(mod, 0); |
| 443 | |
| 444 | deps = kmod_module_get_dependencies(mod); |
| 445 | if (deps != NULL) { |
| 446 | kmod_list_foreach(itr, deps) { |
| 447 | struct kmod_module *dep = kmod_module_get_module(itr); |
| 448 | if (kmod_module_get_refcnt(dep) == 0) { |
| 449 | DBG("Recursive remove module %s", |
| 450 | kmod_module_get_name(dep)); |
| 451 | rmmod_recurse(dep); |
| 452 | } |
| 453 | kmod_module_unref(dep); |
| 454 | } |
| 455 | kmod_module_unref_list(deps); |
| 456 | } |
| 457 | |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * @brief Unloads the kernel modules in \p modules |
| 463 | * |
| 464 | * @param modules List of modules to unload |
| 465 | * @param entries Number of modules in the list |
| 466 | * |
| 467 | */ |
| 468 | static void modprobe_remove_lttng(const struct kern_modules_param *modules, |
| 469 | int entries) |
| 470 | { |
| 471 | int ret = 0, i; |
| 472 | struct kmod_ctx *ctx; |
| 473 | |
| 474 | ret = setup_kmod_ctx(&ctx); |
| 475 | if (ret < 0) { |
| 476 | goto error; |
| 477 | } |
| 478 | |
| 479 | for (i = entries - 1; i >= 0; i--) { |
| 480 | struct kmod_module *mod = NULL; |
| 481 | |
| 482 | if (!modules[i].loaded) { |
| 483 | continue; |
| 484 | } |
| 485 | |
| 486 | ret = kmod_module_new_from_name(ctx, modules[i].name, &mod); |
| 487 | if (ret < 0) { |
| 488 | PERROR("Failed to create kmod module for %s", modules[i].name); |
| 489 | goto error; |
| 490 | } |
| 491 | |
| 492 | ret = rmmod_recurse(mod); |
| 493 | if (ret == -EEXIST) { |
| 494 | DBG("Module %s is not in kernel.", modules[i].name); |
| 495 | } else if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED && ret < 0) { |
| 496 | ERR("Unable to remove module %s", modules[i].name); |
| 497 | } else { |
| 498 | DBG("Modprobe removal successful %s", |
| 499 | modules[i].name); |
| 500 | } |
| 501 | |
| 502 | kmod_module_unref(mod); |
| 503 | } |
| 504 | |
| 505 | error: |
| 506 | if (ctx) { |
| 507 | kmod_unref(ctx); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | #else /* HAVE_KMOD */ |
| 512 | |
| 513 | static int modprobe_lttng(struct kern_modules_param *modules, |
| 514 | int entries) |
| 515 | { |
| 516 | int ret = 0, i; |
| 517 | char modprobe[256]; |
| 518 | |
| 519 | for (i = 0; i < entries; i++) { |
| 520 | ret = snprintf(modprobe, sizeof(modprobe), |
| 521 | "/sbin/modprobe %s%s", |
| 522 | modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED ? "" : "-q ", |
| 523 | modules[i].name); |
| 524 | if (ret < 0) { |
| 525 | PERROR("snprintf modprobe"); |
| 526 | goto error; |
| 527 | } |
| 528 | modprobe[sizeof(modprobe) - 1] = '\0'; |
| 529 | ret = system(modprobe); |
| 530 | if (ret == -1) { |
| 531 | if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) { |
| 532 | ERR("Unable to launch modprobe for required module %s", |
| 533 | modules[i].name); |
| 534 | goto error; |
| 535 | } else { |
| 536 | DBG("Unable to launch modprobe for optional module %s; continuing", |
| 537 | modules[i].name); |
| 538 | ret = 0; |
| 539 | } |
| 540 | } else if (WEXITSTATUS(ret) != 0) { |
| 541 | if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) { |
| 542 | ERR("Unable to load required module %s", |
| 543 | modules[i].name); |
| 544 | goto error; |
| 545 | } else { |
| 546 | DBG("Unable to load optional module %s; continuing", |
| 547 | modules[i].name); |
| 548 | ret = 0; |
| 549 | } |
| 550 | } else { |
| 551 | DBG("Modprobe successfully %s", modules[i].name); |
| 552 | modules[i].loaded = true; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | error: |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | static void modprobe_remove_lttng(const struct kern_modules_param *modules, |
| 561 | int entries) |
| 562 | { |
| 563 | int ret = 0, i; |
| 564 | char modprobe[256]; |
| 565 | |
| 566 | for (i = entries - 1; i >= 0; i--) { |
| 567 | if (!modules[i].loaded) { |
| 568 | continue; |
| 569 | } |
| 570 | ret = snprintf(modprobe, sizeof(modprobe), |
| 571 | "/sbin/modprobe -r -q %s", |
| 572 | modules[i].name); |
| 573 | if (ret < 0) { |
| 574 | PERROR("snprintf modprobe -r"); |
| 575 | return; |
| 576 | } |
| 577 | modprobe[sizeof(modprobe) - 1] = '\0'; |
| 578 | ret = system(modprobe); |
| 579 | if (ret == -1) { |
| 580 | if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) { |
| 581 | ERR("Unable to launch modprobe -r for required module %s", |
| 582 | modules[i].name); |
| 583 | } else { |
| 584 | DBG("Unable to launch modprobe -r for optional module %s", |
| 585 | modules[i].name); |
| 586 | } |
| 587 | } else if (WEXITSTATUS(ret) != 0) { |
| 588 | if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) { |
| 589 | ERR("Unable to remove required module %s", |
| 590 | modules[i].name); |
| 591 | } else { |
| 592 | DBG("Unable to remove optional module %s", |
| 593 | modules[i].name); |
| 594 | } |
| 595 | } else { |
| 596 | DBG("Modprobe removal successful %s", modules[i].name); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | #endif /* HAVE_KMOD */ |
| 602 | |
| 603 | /* |
| 604 | * Remove control kernel module(s) in reverse load order. |
| 605 | */ |
| 606 | void modprobe_remove_lttng_control(void) |
| 607 | { |
| 608 | modprobe_remove_lttng(kern_modules_control_core, |
| 609 | ARRAY_SIZE(kern_modules_control_core)); |
| 610 | } |
| 611 | |
| 612 | static void free_probes(void) |
| 613 | { |
| 614 | int i; |
| 615 | |
| 616 | if (!probes) { |
| 617 | return; |
| 618 | } |
| 619 | for (i = 0; i < nr_probes; ++i) { |
| 620 | free(probes[i].name); |
| 621 | } |
| 622 | free(probes); |
| 623 | probes = NULL; |
| 624 | nr_probes = 0; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Remove data kernel modules in reverse load order. |
| 629 | */ |
| 630 | void modprobe_remove_lttng_data(void) |
| 631 | { |
| 632 | if (!probes) { |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | modprobe_remove_lttng(probes, nr_probes); |
| 637 | free_probes(); |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | * Remove all kernel modules in reverse order. |
| 642 | */ |
| 643 | void modprobe_remove_lttng_all(void) |
| 644 | { |
| 645 | modprobe_remove_lttng_data(); |
| 646 | modprobe_remove_lttng_control(); |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * Load control kernel module(s). |
| 651 | */ |
| 652 | int modprobe_lttng_control(void) |
| 653 | { |
| 654 | return modprobe_lttng(kern_modules_control_core, |
| 655 | ARRAY_SIZE(kern_modules_control_core)); |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * Grow global list of probes (double capacity or set it to 1 if |
| 660 | * currently 0 and copy existing data). |
| 661 | */ |
| 662 | static int grow_probes(void) |
| 663 | { |
| 664 | int i; |
| 665 | struct kern_modules_param *tmp_probes; |
| 666 | |
| 667 | /* Initialize capacity to 1 if 0. */ |
| 668 | if (probes_capacity == 0) { |
| 669 | probes = zmalloc<kern_modules_param>(); |
| 670 | if (!probes) { |
| 671 | PERROR("malloc probe list"); |
| 672 | return -ENOMEM; |
| 673 | } |
| 674 | |
| 675 | probes_capacity = 1; |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | /* Double size. */ |
| 680 | probes_capacity *= 2; |
| 681 | |
| 682 | tmp_probes = calloc<kern_modules_param>(probes_capacity); |
| 683 | if (!tmp_probes) { |
| 684 | PERROR("malloc probe list"); |
| 685 | return -ENOMEM; |
| 686 | } |
| 687 | |
| 688 | for (i = 0; i < nr_probes; ++i) { |
| 689 | /* Ownership of 'name' field is transferred. */ |
| 690 | tmp_probes[i] = probes[i]; |
| 691 | } |
| 692 | |
| 693 | /* Replace probes with larger copy. */ |
| 694 | free(probes); |
| 695 | probes = tmp_probes; |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * Appends a comma-separated list of probes to the global list |
| 702 | * of probes. |
| 703 | */ |
| 704 | static int append_list_to_probes(const char *list) |
| 705 | { |
| 706 | char *next; |
| 707 | int ret; |
| 708 | char *tmp_list, *cur_list, *saveptr; |
| 709 | |
| 710 | LTTNG_ASSERT(list); |
| 711 | |
| 712 | cur_list = tmp_list = strdup(list); |
| 713 | if (!tmp_list) { |
| 714 | PERROR("strdup temp list"); |
| 715 | return -ENOMEM; |
| 716 | } |
| 717 | |
| 718 | for (;;) { |
| 719 | size_t name_len; |
| 720 | struct kern_modules_param *cur_mod; |
| 721 | |
| 722 | next = strtok_r(cur_list, ",", &saveptr); |
| 723 | if (!next) { |
| 724 | break; |
| 725 | } |
| 726 | cur_list = NULL; |
| 727 | |
| 728 | /* filter leading spaces */ |
| 729 | while (*next == ' ') { |
| 730 | next++; |
| 731 | } |
| 732 | |
| 733 | if (probes_capacity <= nr_probes) { |
| 734 | ret = grow_probes(); |
| 735 | if (ret) { |
| 736 | goto error; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | /* Length 13 is "lttng-probe-" + \0 */ |
| 741 | name_len = strlen(next) + 13; |
| 742 | |
| 743 | cur_mod = &probes[nr_probes]; |
| 744 | cur_mod->name = calloc<char>(name_len); |
| 745 | if (!cur_mod->name) { |
| 746 | PERROR("malloc probe list"); |
| 747 | ret = -ENOMEM; |
| 748 | goto error; |
| 749 | } |
| 750 | |
| 751 | ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next); |
| 752 | if (ret < 0) { |
| 753 | PERROR("snprintf modprobe name"); |
| 754 | ret = -ENOMEM; |
| 755 | goto error; |
| 756 | } |
| 757 | |
| 758 | cur_mod->load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL; |
| 759 | |
| 760 | nr_probes++; |
| 761 | } |
| 762 | |
| 763 | free(tmp_list); |
| 764 | return 0; |
| 765 | |
| 766 | error: |
| 767 | free(tmp_list); |
| 768 | free_probes(); |
| 769 | return ret; |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * Load data kernel module(s). |
| 774 | */ |
| 775 | int modprobe_lttng_data(void) |
| 776 | { |
| 777 | int ret, i; |
| 778 | char *list; |
| 779 | |
| 780 | /* |
| 781 | * Base probes: either from command line option, environment |
| 782 | * variable or default list. |
| 783 | */ |
| 784 | list = the_config.kmod_probes_list.value; |
| 785 | if (list) { |
| 786 | /* User-specified probes. */ |
| 787 | ret = append_list_to_probes(list); |
| 788 | if (ret) { |
| 789 | return ret; |
| 790 | } |
| 791 | } else { |
| 792 | /* Default probes. */ |
| 793 | int def_len = ARRAY_SIZE(kern_modules_probes_default); |
| 794 | |
| 795 | probes = calloc<kern_modules_param>(def_len); |
| 796 | if (!probes) { |
| 797 | PERROR("malloc probe list"); |
| 798 | return -ENOMEM; |
| 799 | } |
| 800 | |
| 801 | nr_probes = probes_capacity = def_len; |
| 802 | |
| 803 | for (i = 0; i < def_len; ++i) { |
| 804 | char* name = strdup(kern_modules_probes_default[i].name); |
| 805 | |
| 806 | if (!name) { |
| 807 | PERROR("strdup probe item"); |
| 808 | ret = -ENOMEM; |
| 809 | goto error; |
| 810 | } |
| 811 | |
| 812 | probes[i].name = name; |
| 813 | probes[i].load_policy = kern_modules_probes_default[i].load_policy; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | * Extra modules? Append them to current probes list. |
| 819 | */ |
| 820 | list = the_config.kmod_extra_probes_list.value; |
| 821 | if (list) { |
| 822 | ret = append_list_to_probes(list); |
| 823 | if (ret) { |
| 824 | goto error; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | * Load probes modules now. |
| 830 | */ |
| 831 | ret = modprobe_lttng(probes, nr_probes); |
| 832 | if (ret) { |
| 833 | goto error; |
| 834 | } |
| 835 | return ret; |
| 836 | |
| 837 | error: |
| 838 | free_probes(); |
| 839 | return ret; |
| 840 | } |