| 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 | /** |
| 20 | * @file modprobe.c |
| 21 | * |
| 22 | * @brief modprobe related functions. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #define _LGPL_SOURCE |
| 27 | #include <assert.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <sys/wait.h> |
| 31 | |
| 32 | #include <common/common.h> |
| 33 | #include <common/utils.h> |
| 34 | |
| 35 | #include "modprobe.h" |
| 36 | #include "kern-modules.h" |
| 37 | |
| 38 | #define LTTNG_MOD_REQUIRED 1 |
| 39 | #define LTTNG_MOD_OPTIONAL 0 |
| 40 | |
| 41 | /* LTTng kernel tracer mandatory core modules list */ |
| 42 | struct kern_modules_param kern_modules_control_core[] = { |
| 43 | { "lttng-ring-buffer-client-discard" }, |
| 44 | { "lttng-ring-buffer-client-overwrite" }, |
| 45 | { "lttng-ring-buffer-metadata-client" }, |
| 46 | { "lttng-ring-buffer-client-mmap-discard" }, |
| 47 | { "lttng-ring-buffer-client-mmap-overwrite" }, |
| 48 | { "lttng-ring-buffer-metadata-mmap-client" }, |
| 49 | }; |
| 50 | |
| 51 | /* LTTng kernel tracer probe modules list */ |
| 52 | struct kern_modules_param kern_modules_probes_default[] = { |
| 53 | { "lttng-probe-asoc" }, |
| 54 | { "lttng-probe-block" }, |
| 55 | { "lttng-probe-btrfs" }, |
| 56 | { "lttng-probe-compaction" }, |
| 57 | { "lttng-probe-ext3" }, |
| 58 | { "lttng-probe-ext4" }, |
| 59 | { "lttng-probe-gpio" }, |
| 60 | { "lttng-probe-irq" }, |
| 61 | { "lttng-probe-jbd" }, |
| 62 | { "lttng-probe-jbd2" }, |
| 63 | { "lttng-probe-kmem" }, |
| 64 | { "lttng-probe-kvm" }, |
| 65 | { "lttng-probe-kvm-x86" }, |
| 66 | { "lttng-probe-kvm-x86-mmu" }, |
| 67 | { "lttng-probe-lock" }, |
| 68 | { "lttng-probe-module" }, |
| 69 | { "lttng-probe-napi" }, |
| 70 | { "lttng-probe-net" }, |
| 71 | { "lttng-probe-power" }, |
| 72 | { "lttng-probe-printk" }, |
| 73 | { "lttng-probe-random" }, |
| 74 | { "lttng-probe-rcu" }, |
| 75 | { "lttng-probe-regmap" }, |
| 76 | { "lttng-probe-regulator" }, |
| 77 | { "lttng-probe-rpm" }, |
| 78 | { "lttng-probe-sched" }, |
| 79 | { "lttng-probe-scsi" }, |
| 80 | { "lttng-probe-signal" }, |
| 81 | { "lttng-probe-skb" }, |
| 82 | { "lttng-probe-sock" }, |
| 83 | { "lttng-probe-statedump" }, |
| 84 | { "lttng-probe-sunrpc" }, |
| 85 | { "lttng-probe-timer" }, |
| 86 | { "lttng-probe-udp" }, |
| 87 | { "lttng-probe-vmscan" }, |
| 88 | { "lttng-probe-v4l2" }, |
| 89 | { "lttng-probe-workqueue" }, |
| 90 | { "lttng-probe-writeback" }, |
| 91 | { "lttng-probe-x86-irq-vectors" }, |
| 92 | { "lttng-probe-x86-exceptions" }, |
| 93 | }; |
| 94 | |
| 95 | /* dynamic probe modules list */ |
| 96 | static struct kern_modules_param *probes; |
| 97 | static int nr_probes; |
| 98 | static int probes_capacity; |
| 99 | |
| 100 | #if HAVE_KMOD |
| 101 | #include <libkmod.h> |
| 102 | |
| 103 | /** |
| 104 | * @brief Logging function for libkmod integration. |
| 105 | */ |
| 106 | static void log_kmod(void *data, int priority, const char *file, int line, |
| 107 | const char *fn, const char *format, va_list args) |
| 108 | { |
| 109 | char *str; |
| 110 | |
| 111 | if (vasprintf(&str, format, args) < 0) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | DBG("libkmod: %s", str); |
| 116 | free(str); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @brief Setup the libkmod context. |
| 121 | * |
| 122 | * Create the context, add a custom logging function and preload the |
| 123 | * ressources for faster operation. |
| 124 | * |
| 125 | * @returns \c 0 on success |
| 126 | * \c < 0 on error |
| 127 | */ |
| 128 | static int setup_kmod_ctx(struct kmod_ctx **ctx) |
| 129 | { |
| 130 | int ret = 0; |
| 131 | |
| 132 | *ctx = kmod_new(NULL, NULL); |
| 133 | if (!ctx) { |
| 134 | PERROR("Unable to create kmod library context"); |
| 135 | ret = -ENOMEM; |
| 136 | goto error; |
| 137 | } |
| 138 | |
| 139 | kmod_set_log_fn(*ctx, log_kmod, NULL); |
| 140 | ret = kmod_load_resources(*ctx); |
| 141 | if (ret < 0) { |
| 142 | ERR("Failed to load kmod library resources"); |
| 143 | goto error; |
| 144 | } |
| 145 | |
| 146 | error: |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @brief Loads the kernel modules in \p modules |
| 152 | * |
| 153 | * @param modules List of modules to load |
| 154 | * @param entries Number of modules in the list |
| 155 | * @param required Are the modules required or optionnal |
| 156 | * |
| 157 | * If the modules are required, we will return with error after the |
| 158 | * first failed module load, otherwise we continue loading. |
| 159 | * |
| 160 | * @returns \c 0 on success |
| 161 | * \c < 0 on error |
| 162 | */ |
| 163 | static int modprobe_lttng(struct kern_modules_param *modules, |
| 164 | int entries, int required) |
| 165 | { |
| 166 | int ret = 0, i; |
| 167 | struct kmod_ctx *ctx; |
| 168 | |
| 169 | ret = setup_kmod_ctx(&ctx); |
| 170 | if (ret < 0) { |
| 171 | goto error; |
| 172 | } |
| 173 | |
| 174 | for (i = 0; i < entries; i++) { |
| 175 | struct kmod_module *mod = NULL; |
| 176 | |
| 177 | ret = kmod_module_new_from_name(ctx, modules[i].name, &mod); |
| 178 | if (ret < 0) { |
| 179 | PERROR("Failed to create kmod module for %s", modules[i].name); |
| 180 | goto error; |
| 181 | } |
| 182 | |
| 183 | ret = kmod_module_probe_insert_module(mod, 0, |
| 184 | NULL, NULL, NULL, NULL); |
| 185 | if (ret == -EEXIST) { |
| 186 | DBG("Module %s is already loaded", modules[i].name); |
| 187 | ret = 0; |
| 188 | } else if (ret < 0) { |
| 189 | if (required) { |
| 190 | ERR("Unable to load required module %s", |
| 191 | modules[i].name); |
| 192 | goto error; |
| 193 | } else { |
| 194 | DBG("Unable to load optional module %s; continuing", |
| 195 | modules[i].name); |
| 196 | ret = 0; |
| 197 | } |
| 198 | } else { |
| 199 | DBG("Modprobe successfully %s", modules[i].name); |
| 200 | } |
| 201 | |
| 202 | kmod_module_unref(mod); |
| 203 | } |
| 204 | |
| 205 | error: |
| 206 | if (ctx) { |
| 207 | kmod_unref(ctx); |
| 208 | } |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @brief Recursively unload modules. |
| 214 | * |
| 215 | * This function implements the same modules unloading behavior as |
| 216 | * 'modprobe -r' or rmmod, it will recursevily go trought the \p module |
| 217 | * dependencies and unload modules with a refcount of 0. |
| 218 | * |
| 219 | * @param mod The module to unload |
| 220 | * |
| 221 | * @returns \c 0 on success |
| 222 | * \c < 0 on error |
| 223 | */ |
| 224 | static int rmmod_recurse(struct kmod_module *mod) { |
| 225 | int ret = 0; |
| 226 | struct kmod_list *deps, *itr; |
| 227 | |
| 228 | if (kmod_module_get_initstate(mod) == KMOD_MODULE_BUILTIN) { |
| 229 | DBG("Module %s is builtin", kmod_module_get_name(mod)); |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | ret = kmod_module_remove_module(mod, 0); |
| 234 | |
| 235 | deps = kmod_module_get_dependencies(mod); |
| 236 | if (deps != NULL) { |
| 237 | kmod_list_foreach(itr, deps) { |
| 238 | struct kmod_module *dep = kmod_module_get_module(itr); |
| 239 | if (kmod_module_get_refcnt(dep) == 0) { |
| 240 | DBG("Recursive remove module %s", |
| 241 | kmod_module_get_name(dep)); |
| 242 | rmmod_recurse(dep); |
| 243 | } |
| 244 | kmod_module_unref(dep); |
| 245 | } |
| 246 | kmod_module_unref_list(deps); |
| 247 | } |
| 248 | |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * @brief Unloads the kernel modules in \p modules |
| 254 | * |
| 255 | * @param modules List of modules to unload |
| 256 | * @param entries Number of modules in the list |
| 257 | * @param required Are the modules required or optionnal |
| 258 | * |
| 259 | */ |
| 260 | static void modprobe_remove_lttng(const struct kern_modules_param *modules, |
| 261 | int entries, int required) |
| 262 | { |
| 263 | int ret = 0, i; |
| 264 | struct kmod_ctx *ctx; |
| 265 | |
| 266 | ret = setup_kmod_ctx(&ctx); |
| 267 | if (ret < 0) { |
| 268 | goto error; |
| 269 | } |
| 270 | |
| 271 | for (i = entries - 1; i >= 0; i--) { |
| 272 | struct kmod_module *mod = NULL; |
| 273 | |
| 274 | ret = kmod_module_new_from_name(ctx, modules[i].name, &mod); |
| 275 | if (ret < 0) { |
| 276 | PERROR("Failed to create kmod module for %s", modules[i].name); |
| 277 | goto error; |
| 278 | } |
| 279 | |
| 280 | ret = rmmod_recurse(mod); |
| 281 | if (ret == -EEXIST) { |
| 282 | DBG("Module %s is not in kernel.", modules[i].name); |
| 283 | } else if (required && ret < 0) { |
| 284 | ERR("Unable to remove module %s", modules[i].name); |
| 285 | } else { |
| 286 | DBG("Modprobe removal successful %s", |
| 287 | modules[i].name); |
| 288 | } |
| 289 | |
| 290 | kmod_module_unref(mod); |
| 291 | } |
| 292 | |
| 293 | error: |
| 294 | if (ctx) { |
| 295 | kmod_unref(ctx); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | #else /* HAVE_KMOD */ |
| 300 | |
| 301 | static int modprobe_lttng(struct kern_modules_param *modules, |
| 302 | int entries, int required) |
| 303 | { |
| 304 | int ret = 0, i; |
| 305 | char modprobe[256]; |
| 306 | |
| 307 | for (i = 0; i < entries; i++) { |
| 308 | ret = snprintf(modprobe, sizeof(modprobe), |
| 309 | "/sbin/modprobe %s%s", |
| 310 | required ? "" : "-q ", |
| 311 | modules[i].name); |
| 312 | if (ret < 0) { |
| 313 | PERROR("snprintf modprobe"); |
| 314 | goto error; |
| 315 | } |
| 316 | modprobe[sizeof(modprobe) - 1] = '\0'; |
| 317 | ret = system(modprobe); |
| 318 | if (ret == -1) { |
| 319 | if (required) { |
| 320 | ERR("Unable to launch modprobe for required module %s", |
| 321 | modules[i].name); |
| 322 | goto error; |
| 323 | } else { |
| 324 | DBG("Unable to launch modprobe for optional module %s; continuing", |
| 325 | modules[i].name); |
| 326 | ret = 0; |
| 327 | } |
| 328 | } else if (WEXITSTATUS(ret) != 0) { |
| 329 | if (required) { |
| 330 | ERR("Unable to load required module %s", |
| 331 | modules[i].name); |
| 332 | goto error; |
| 333 | } else { |
| 334 | DBG("Unable to load optional module %s; continuing", |
| 335 | modules[i].name); |
| 336 | ret = 0; |
| 337 | } |
| 338 | } else { |
| 339 | DBG("Modprobe successfully %s", modules[i].name); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | error: |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | static void modprobe_remove_lttng(const struct kern_modules_param *modules, |
| 348 | int entries, int required) |
| 349 | { |
| 350 | int ret = 0, i; |
| 351 | char modprobe[256]; |
| 352 | |
| 353 | for (i = entries - 1; i >= 0; i--) { |
| 354 | ret = snprintf(modprobe, sizeof(modprobe), |
| 355 | "/sbin/modprobe -r -q %s", |
| 356 | modules[i].name); |
| 357 | if (ret < 0) { |
| 358 | PERROR("snprintf modprobe -r"); |
| 359 | return; |
| 360 | } |
| 361 | modprobe[sizeof(modprobe) - 1] = '\0'; |
| 362 | ret = system(modprobe); |
| 363 | if (ret == -1) { |
| 364 | ERR("Unable to launch modprobe -r for module %s", |
| 365 | modules[i].name); |
| 366 | } else if (required && WEXITSTATUS(ret) != 0) { |
| 367 | ERR("Unable to remove module %s", |
| 368 | modules[i].name); |
| 369 | } else { |
| 370 | DBG("Modprobe removal successful %s", |
| 371 | modules[i].name); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | #endif /* HAVE_KMOD */ |
| 377 | |
| 378 | /* |
| 379 | * Remove control kernel module(s) in reverse load order. |
| 380 | */ |
| 381 | void modprobe_remove_lttng_control(void) |
| 382 | { |
| 383 | modprobe_remove_lttng(kern_modules_control_core, |
| 384 | ARRAY_SIZE(kern_modules_control_core), |
| 385 | LTTNG_MOD_REQUIRED); |
| 386 | } |
| 387 | |
| 388 | static void free_probes(void) |
| 389 | { |
| 390 | int i; |
| 391 | |
| 392 | if (!probes) { |
| 393 | return; |
| 394 | } |
| 395 | for (i = 0; i < nr_probes; ++i) { |
| 396 | free(probes[i].name); |
| 397 | } |
| 398 | free(probes); |
| 399 | probes = NULL; |
| 400 | nr_probes = 0; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Remove data kernel modules in reverse load order. |
| 405 | */ |
| 406 | void modprobe_remove_lttng_data(void) |
| 407 | { |
| 408 | if (!probes) { |
| 409 | return; |
| 410 | } |
| 411 | modprobe_remove_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL); |
| 412 | free_probes(); |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * Remove all kernel modules in reverse order. |
| 417 | */ |
| 418 | void modprobe_remove_lttng_all(void) |
| 419 | { |
| 420 | modprobe_remove_lttng_data(); |
| 421 | modprobe_remove_lttng_control(); |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Load control kernel module(s). |
| 426 | */ |
| 427 | int modprobe_lttng_control(void) |
| 428 | { |
| 429 | int ret; |
| 430 | |
| 431 | ret = modprobe_lttng(kern_modules_control_core, |
| 432 | ARRAY_SIZE(kern_modules_control_core), |
| 433 | LTTNG_MOD_REQUIRED); |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Grow global list of probes (double capacity or set it to 1 if |
| 439 | * currently 0 and copy existing data). |
| 440 | */ |
| 441 | static int grow_probes(void) |
| 442 | { |
| 443 | int i; |
| 444 | struct kern_modules_param *tmp_probes; |
| 445 | |
| 446 | /* Initialize capacity to 1 if 0. */ |
| 447 | if (probes_capacity == 0) { |
| 448 | probes = zmalloc(sizeof(*probes)); |
| 449 | if (!probes) { |
| 450 | PERROR("malloc probe list"); |
| 451 | return -ENOMEM; |
| 452 | } |
| 453 | |
| 454 | probes_capacity = 1; |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | /* Double size. */ |
| 459 | probes_capacity *= 2; |
| 460 | |
| 461 | tmp_probes = zmalloc(sizeof(*tmp_probes) * probes_capacity); |
| 462 | if (!tmp_probes) { |
| 463 | PERROR("malloc probe list"); |
| 464 | return -ENOMEM; |
| 465 | } |
| 466 | |
| 467 | for (i = 0; i < nr_probes; ++i) { |
| 468 | /* Move name pointer. */ |
| 469 | tmp_probes[i].name = probes[i].name; |
| 470 | } |
| 471 | |
| 472 | /* Replace probes with larger copy. */ |
| 473 | free(probes); |
| 474 | probes = tmp_probes; |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Appends a comma-separated list of probes to the global list |
| 481 | * of probes. |
| 482 | */ |
| 483 | static int append_list_to_probes(const char *list) |
| 484 | { |
| 485 | char *next; |
| 486 | int ret; |
| 487 | char *tmp_list, *cur_list; |
| 488 | |
| 489 | assert(list); |
| 490 | |
| 491 | cur_list = tmp_list = strdup(list); |
| 492 | if (!tmp_list) { |
| 493 | PERROR("strdup temp list"); |
| 494 | return -ENOMEM; |
| 495 | } |
| 496 | |
| 497 | for (;;) { |
| 498 | size_t name_len; |
| 499 | struct kern_modules_param *cur_mod; |
| 500 | |
| 501 | next = strtok(cur_list, ","); |
| 502 | if (!next) { |
| 503 | break; |
| 504 | } |
| 505 | cur_list = NULL; |
| 506 | |
| 507 | /* filter leading spaces */ |
| 508 | while (*next == ' ') { |
| 509 | next++; |
| 510 | } |
| 511 | |
| 512 | if (probes_capacity <= nr_probes) { |
| 513 | ret = grow_probes(); |
| 514 | if (ret) { |
| 515 | goto error; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /* Length 13 is "lttng-probe-" + \0 */ |
| 520 | name_len = strlen(next) + 13; |
| 521 | |
| 522 | cur_mod = &probes[nr_probes]; |
| 523 | cur_mod->name = zmalloc(name_len); |
| 524 | if (!cur_mod->name) { |
| 525 | PERROR("malloc probe list"); |
| 526 | ret = -ENOMEM; |
| 527 | goto error; |
| 528 | } |
| 529 | |
| 530 | ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next); |
| 531 | if (ret < 0) { |
| 532 | PERROR("snprintf modprobe name"); |
| 533 | ret = -ENOMEM; |
| 534 | goto error; |
| 535 | } |
| 536 | |
| 537 | nr_probes++; |
| 538 | } |
| 539 | |
| 540 | free(tmp_list); |
| 541 | return 0; |
| 542 | |
| 543 | error: |
| 544 | free(tmp_list); |
| 545 | free_probes(); |
| 546 | return ret; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * Load data kernel module(s). |
| 551 | */ |
| 552 | int modprobe_lttng_data(void) |
| 553 | { |
| 554 | int ret, i; |
| 555 | char *list; |
| 556 | |
| 557 | /* |
| 558 | * Base probes: either from command line option, environment |
| 559 | * variable or default list. |
| 560 | */ |
| 561 | if (kmod_probes_list) { |
| 562 | list = kmod_probes_list; |
| 563 | } else { |
| 564 | list = utils_get_kmod_probes_list(); |
| 565 | } |
| 566 | |
| 567 | if (list) { |
| 568 | /* User-specified probes. */ |
| 569 | ret = append_list_to_probes(list); |
| 570 | if (ret) { |
| 571 | return ret; |
| 572 | } |
| 573 | } else { |
| 574 | /* Default probes. */ |
| 575 | int def_len = ARRAY_SIZE(kern_modules_probes_default); |
| 576 | |
| 577 | probes = zmalloc(sizeof(*probes) * def_len); |
| 578 | if (!probes) { |
| 579 | PERROR("malloc probe list"); |
| 580 | return -ENOMEM; |
| 581 | } |
| 582 | |
| 583 | nr_probes = probes_capacity = def_len; |
| 584 | |
| 585 | for (i = 0; i < def_len; ++i) { |
| 586 | char* name = strdup(kern_modules_probes_default[i].name); |
| 587 | |
| 588 | if (!name) { |
| 589 | PERROR("strdup probe item"); |
| 590 | ret = -ENOMEM; |
| 591 | goto error; |
| 592 | } |
| 593 | |
| 594 | probes[i].name = name; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | /* |
| 599 | * Extra modules? Append them to current probes list. |
| 600 | */ |
| 601 | if (kmod_extra_probes_list) { |
| 602 | list = kmod_extra_probes_list; |
| 603 | } else { |
| 604 | list = utils_get_extra_kmod_probes_list(); |
| 605 | } |
| 606 | |
| 607 | if (list) { |
| 608 | ret = append_list_to_probes(list); |
| 609 | if (ret) { |
| 610 | goto error; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * Load probes modules now. |
| 616 | */ |
| 617 | ret = modprobe_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL); |
| 618 | if (ret) { |
| 619 | goto error; |
| 620 | } |
| 621 | return ret; |
| 622 | |
| 623 | error: |
| 624 | free_probes(); |
| 625 | return ret; |
| 626 | } |