Debian Codegraph @

crt/aws-c-io/source/posix/shared_library.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/io/shared_library.h>
7
8 #include <aws/io/logging.h>
9
10 #include <dlfcn.h>
11
12 static const char *s_null = "<NULL>";
13 static const char *s_unknown_error = "<Unknown>";
14
15 int aws_shared_library_init(struct aws_shared_library *library, const char *library_path) {
16 AWS_ZERO_STRUCT(*library);
17
18 library->library_handle = dlopen(library_path, RTLD_LAZY);
19 if (library->library_handle == NULL) {
20 const char *error = dlerror();
21 AWS_LOGF_ERROR(
22 AWS_LS_IO_SHARED_LIBRARY,
23 "id=%p: Failed to load shared library at path \"%s\" with error: %s",
24 (void *)library,
25 library_path ? library_path : s_null,
26 error ? error : s_unknown_error);
27 return aws_raise_error(AWS_IO_SHARED_LIBRARY_LOAD_FAILURE);
28 }
29
30 return AWS_OP_SUCCESS;
31 }
32
33 void aws_shared_library_clean_up(struct aws_shared_library *library) {
34 if (library && library->library_handle) {
35 dlclose(library->library_handle);
36 library->library_handle = NULL;
37 }
38 }
39
40 int aws_shared_library_find_function(
41 struct aws_shared_library *library,
42 const char *symbol_name,
43 aws_generic_function *function_address) {
44 if (library == NULL || library->library_handle == NULL) {
45 return aws_raise_error(AWS_IO_SHARED_LIBRARY_FIND_SYMBOL_FAILURE);
46 }
47
48 /*
49 * Suggested work around for (undefined behavior) cast from void * to function pointer
50 * in POSIX.1-2003 standard, at least according to dlsym man page code sample.
51 */
52 *(void **)(function_address) = dlsym(library->library_handle, symbol_name);
53
54 if (*function_address == NULL) {
55 const char *error = dlerror();
56 AWS_LOGF_ERROR(
57 AWS_LS_IO_SHARED_LIBRARY,
58 "id=%p: Failed to find shared library symbol \"%s\" with error: %s",
59 (void *)library,
60 symbol_name ? symbol_name : s_null,
61 error ? error : s_unknown_error);
62 return aws_raise_error(AWS_IO_SHARED_LIBRARY_FIND_SYMBOL_FAILURE);
63 }
64
65 return AWS_OP_SUCCESS;
66 }