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