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