diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7d90d2c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +example/abi-query-dlopen diff --git a/example/Makefile b/example/Makefile new file mode 100644 index 0000000..d16dc24 --- /dev/null +++ b/example/Makefile @@ -0,0 +1,22 @@ +# +# Copyright (c) 2022 IBM Corporation. All rights reserved. +# $COPYRIGHT$ +# +# Additional copyrights may follow +# +# $HEADER$ +# +PROGS=abi-query-dlopen + +all: $(PROGS) + +PMIX_ABI_HEADERS=$(PWD)/../ + +abi-query-dlopen: abi-query-dlopen.c + gcc abi-query-dlopen.c -O0 -g -ldl -I$(PMIX_ABI_HEADERS) -o abi-query-dlopen + +run: + @echo $(PMIX_ROOT) + +clean: + rm -f $(PROGS) core.* diff --git a/example/abi-query-dlopen.c b/example/abi-query-dlopen.c new file mode 100644 index 0000000..04198ca --- /dev/null +++ b/example/abi-query-dlopen.c @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2022 IBM Corporation. All rights reserved. + * $COPYRIGHT$ + * + * Additional copyrights may follow + * + * $HEADER$ + * + * Test PMIx_Query_info outside of init/finalize by asking for a + * key (PMIX_QUERY_ABI_VERSION) that is allowed to be accessed + * outside of the init/finalize region. Use dlopen to pick up + * the PMIx library. + */ + +#include +#include +#include +#include + +// This is the dlopen friendly header from the pmix-abi +#include + +// Temporary until it is added to the PMIx Standard +#define PMIX_QUERY_ABI_VERSION "pmix.qry.abiversion" + + +int main(int argc, char **argv) { + int rc, i; + size_t ninfo, nqueries; + pmix_info_t *info = NULL; + pmix_query_t *query = NULL; + char *pmix_root = NULL; + char *pmix_lib = NULL; + + pmix_root = getenv("PMIX_ROOT"); + if( NULL == pmix_root ) { + fprintf(stderr, "Error: The envar PMIX_ROOT must be set to a PMIx implementation installation.\n"); + exit(1); + } + + // append "/lib/libpmix.so\0" = 16 characters + pmix_lib = (char*)malloc(sizeof(char) * (strlen(pmix_root) + 17)); + pmix_lib[0] = '\0'; + strcat(pmix_lib, pmix_root); + strcat(pmix_lib, "/lib/libpmix.so"); + printf("Using: PMIX_ROOT: \"%s\"\n", pmix_root); + printf("Using: PMIX_LIB : \"%s\"\n", pmix_lib); + + /* + * dlopen the PMIx library using the PMIX_ROOT as the location + */ + void *lib_handle; + lib_handle = dlopen(pmix_lib, RTLD_LAZY | RTLD_LOCAL); + if( NULL == lib_handle ) { + fprintf(stderr, "Error: Failed to open the library. %s\n", dlerror()); + exit(1); + } + + /* + * Find the PMIx_Query_info symbol so we can check the ABI level + */ + pmix_query_info_fn_t query_fn; + query_fn = dlsym(lib_handle, "PMIx_Query_info"); + if( NULL == query_fn ) { + fprintf(stderr, "Error: Failed to find the PMIx_Query_info symbol. %s\n", dlerror()); + exit(1); + } + + /* + * Perform the query + */ + nqueries = 1; + + PMIX_QUERY_CREATE(query, nqueries); + PMIX_ARGV_APPEND(rc, query[0].keys, PMIX_QUERY_ABI_VERSION); + + //rc = PMIx_Query_info(query, nqueries, &info, &ninfo); + rc = query_fn(query, nqueries, &info, &ninfo); + if (PMIX_SUCCESS != rc ) { + fprintf(stderr, "Error: PMIx_Query_info failed: %d\n", rc); + return rc; + } + + printf("--> Query returned (ninfo %d)\n", ninfo); + for(i = 0; i < ninfo; ++i) { + printf("--> KEY: %s\n", info[i].key); + if (PMIX_CHECK_KEY(&info[i], PMIX_QUERY_ABI_VERSION)) { + printf("----> ABI: String: %s\n", + info[i].value.data.string); + } + } + + PMIX_INFO_FREE(info, ninfo); + PMIX_QUERY_FREE(query, nqueries); + + return 0; +}