| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #define _LGPL_SOURCE |
| 8 | #include <dlfcn.h> |
| 9 | #include <stdlib.h> |
| 10 | #include "common/logging.h" |
| 11 | #include <urcu/system.h> |
| 12 | #include <urcu/arch.h> |
| 13 | |
| 14 | #include "common/getenv.h" |
| 15 | #include "lib/lttng-ust/getcpu.h" |
| 16 | |
| 17 | /* Function pointer to the current getcpu callback. */ |
| 18 | int (*lttng_ust_get_cpu_sym)(void); |
| 19 | |
| 20 | static |
| 21 | void *getcpu_plugin_handle; |
| 22 | |
| 23 | /* |
| 24 | * Override the user provided getcpu implementation. |
| 25 | */ |
| 26 | int lttng_ust_getcpu_override(int (*getcpu)(void)) |
| 27 | { |
| 28 | CMM_STORE_SHARED(lttng_ust_get_cpu_sym, getcpu); |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | /* |
| 33 | * Initialize the getcpu plugin if it's present. |
| 34 | */ |
| 35 | void lttng_ust_getcpu_plugin_init(void) |
| 36 | { |
| 37 | const char *libname; |
| 38 | void (*getcpu_plugin_init)(void); |
| 39 | |
| 40 | /* If a plugin is already loaded, do nothing. */ |
| 41 | if (getcpu_plugin_handle) |
| 42 | return; |
| 43 | |
| 44 | /* |
| 45 | * If the LTTNG_UST_GETCPU_PLUGIN environment variable is undefined, do |
| 46 | * nothing. |
| 47 | */ |
| 48 | libname = lttng_ust_getenv("LTTNG_UST_GETCPU_PLUGIN"); |
| 49 | if (!libname) |
| 50 | return; |
| 51 | |
| 52 | /* |
| 53 | * Thy to dlopen the getcpu plugin shared object specified in |
| 54 | * LTTNG_UST_GETCPU_PLUGIN. |
| 55 | */ |
| 56 | getcpu_plugin_handle = dlopen(libname, RTLD_NOW); |
| 57 | if (!getcpu_plugin_handle) { |
| 58 | PERROR("Cannot load LTTng UST getcpu override library %s", |
| 59 | libname); |
| 60 | return; |
| 61 | } |
| 62 | dlerror(); |
| 63 | |
| 64 | /* Locate the getcpu plugin init function in the shared object. */ |
| 65 | getcpu_plugin_init = (void (*)(void)) dlsym(getcpu_plugin_handle, |
| 66 | "lttng_ust_getcpu_plugin_init"); |
| 67 | if (!getcpu_plugin_init) { |
| 68 | PERROR("Cannot find LTTng UST getcpu override library %s initialization function lttng_ust_getcpu_plugin_init()", |
| 69 | libname); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | /* Run the user provided getcpu plugin init function. */ |
| 74 | getcpu_plugin_init(); |
| 75 | } |