| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <syslog.h> |
| 4 |
|
| 5 |
#include "sd-dlopen.h" |
| 6 |
|
| 7 |
#include "libarchive-util.h" |
| 8 |
#include "log.h" /* IWYU pragma: keep */ |
| 9 |
#include "user-util.h" /* IWYU pragma: keep */ |
| 10 |
|
| 11 |
#if HAVE_LIBARCHIVE |
| 12 |
DLSYM_PROTOTYPE(archive_entry_acl_add_entry) = NULL; |
| 13 |
DLSYM_PROTOTYPE(archive_entry_acl_next) = NULL; |
| 14 |
DLSYM_PROTOTYPE(archive_entry_acl_reset) = NULL; |
| 15 |
DLSYM_PROTOTYPE(archive_entry_fflags) = NULL; |
| 16 |
DLSYM_PROTOTYPE(archive_entry_filetype) = NULL; |
| 17 |
DLSYM_PROTOTYPE(archive_entry_free) = NULL; |
| 18 |
DLSYM_PROTOTYPE(archive_entry_gid) = NULL; |
| 19 |
static int missing_archive_entry_gid_is_set(struct archive_entry *e) { |
| 20 |
return gid_is_valid(sym_archive_entry_gid(e)); |
| 21 |
} |
| 22 |
DLSYM_PROTOTYPE(archive_entry_gid_is_set) = missing_archive_entry_gid_is_set; |
| 23 |
DLSYM_PROTOTYPE(archive_entry_hardlink) = NULL; |
| 24 |
static int missing_archive_entry_hardlink_is_set(struct archive_entry *e) { |
| 25 |
return !!sym_archive_entry_hardlink(e); |
| 26 |
} |
| 27 |
DLSYM_PROTOTYPE(archive_entry_hardlink_is_set) = missing_archive_entry_hardlink_is_set; |
| 28 |
DLSYM_PROTOTYPE(archive_entry_mode) = NULL; |
| 29 |
DLSYM_PROTOTYPE(archive_entry_mtime) = NULL; |
| 30 |
DLSYM_PROTOTYPE(archive_entry_mtime_is_set) = NULL; |
| 31 |
DLSYM_PROTOTYPE(archive_entry_mtime_nsec) = NULL; |
| 32 |
DLSYM_PROTOTYPE(archive_entry_new) = NULL; |
| 33 |
DLSYM_PROTOTYPE(archive_entry_pathname) = NULL; |
| 34 |
DLSYM_PROTOTYPE(archive_entry_rdevmajor) = NULL; |
| 35 |
DLSYM_PROTOTYPE(archive_entry_rdevminor) = NULL; |
| 36 |
DLSYM_PROTOTYPE(archive_entry_set_ctime) = NULL; |
| 37 |
DLSYM_PROTOTYPE(archive_entry_set_fflags) = NULL; |
| 38 |
DLSYM_PROTOTYPE(archive_entry_set_filetype) = NULL; |
| 39 |
DLSYM_PROTOTYPE(archive_entry_set_gid) = NULL; |
| 40 |
DLSYM_PROTOTYPE(archive_entry_set_hardlink) = NULL; |
| 41 |
DLSYM_PROTOTYPE(archive_entry_set_mtime) = NULL; |
| 42 |
DLSYM_PROTOTYPE(archive_entry_set_pathname) = NULL; |
| 43 |
DLSYM_PROTOTYPE(archive_entry_set_perm) = NULL; |
| 44 |
DLSYM_PROTOTYPE(archive_entry_set_rdevmajor) = NULL; |
| 45 |
DLSYM_PROTOTYPE(archive_entry_set_rdevminor) = NULL; |
| 46 |
DLSYM_PROTOTYPE(archive_entry_set_size) = NULL; |
| 47 |
DLSYM_PROTOTYPE(archive_entry_set_symlink) = NULL; |
| 48 |
DLSYM_PROTOTYPE(archive_entry_set_uid) = NULL; |
| 49 |
DLSYM_PROTOTYPE(archive_entry_sparse_add_entry) = NULL; |
| 50 |
DLSYM_PROTOTYPE(archive_entry_symlink) = NULL; |
| 51 |
DLSYM_PROTOTYPE(archive_entry_uid) = NULL; |
| 52 |
static int missing_archive_entry_uid_is_set(struct archive_entry *e) { |
| 53 |
return uid_is_valid(sym_archive_entry_uid(e)); |
| 54 |
} |
| 55 |
DLSYM_PROTOTYPE(archive_entry_uid_is_set) = missing_archive_entry_uid_is_set; |
| 56 |
DLSYM_PROTOTYPE(archive_entry_xattr_add_entry) = NULL; |
| 57 |
DLSYM_PROTOTYPE(archive_entry_xattr_next) = NULL; |
| 58 |
DLSYM_PROTOTYPE(archive_entry_xattr_reset) = NULL; |
| 59 |
DLSYM_PROTOTYPE(archive_error_string) = NULL; |
| 60 |
DLSYM_PROTOTYPE(archive_read_data_into_fd) = NULL; |
| 61 |
DLSYM_PROTOTYPE(archive_read_free) = NULL; |
| 62 |
DLSYM_PROTOTYPE(archive_read_new) = NULL; |
| 63 |
DLSYM_PROTOTYPE(archive_read_next_header) = NULL; |
| 64 |
DLSYM_PROTOTYPE(archive_read_open_fd) = NULL; |
| 65 |
DLSYM_PROTOTYPE(archive_read_support_format_cpio) = NULL; |
| 66 |
DLSYM_PROTOTYPE(archive_read_support_format_tar) = NULL; |
| 67 |
DLSYM_PROTOTYPE(archive_write_close) = NULL; |
| 68 |
DLSYM_PROTOTYPE(archive_write_data) = NULL; |
| 69 |
DLSYM_PROTOTYPE(archive_write_free) = NULL; |
| 70 |
DLSYM_PROTOTYPE(archive_write_header) = NULL; |
| 71 |
DLSYM_PROTOTYPE(archive_write_new) = NULL; |
| 72 |
DLSYM_PROTOTYPE(archive_write_open_FILE) = NULL; |
| 73 |
DLSYM_PROTOTYPE(archive_write_open_fd) = NULL; |
| 74 |
DLSYM_PROTOTYPE(archive_write_set_format_filter_by_ext) = NULL; |
| 75 |
DLSYM_PROTOTYPE(archive_write_set_format_pax) = NULL; |
| 76 |
#endif |
| 77 |
|
| 78 |
int dlopen_libarchive(int log_level) { |
| 79 |
#if HAVE_LIBARCHIVE |
| 80 |
static void *libarchive_dl = NULL; |
| 81 |
int r; |
| 82 |
|
| 83 |
LIBARCHIVE_NOTE(SD_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED); |
| 84 |
|
| 85 |
r = dlopen_many_sym_or_warn( |
| 86 |
&libarchive_dl, |
| 87 |
"libarchive.so.13", |
| 88 |
log_level, |
| 89 |
DLSYM_ARG(archive_entry_acl_add_entry), |
| 90 |
DLSYM_ARG(archive_entry_acl_next), |
| 91 |
DLSYM_ARG(archive_entry_acl_reset), |
| 92 |
DLSYM_ARG(archive_entry_fflags), |
| 93 |
DLSYM_ARG(archive_entry_filetype), |
| 94 |
DLSYM_ARG(archive_entry_free), |
| 95 |
DLSYM_ARG(archive_entry_gid), |
| 96 |
DLSYM_ARG(archive_entry_hardlink), |
| 97 |
DLSYM_ARG(archive_entry_mode), |
| 98 |
DLSYM_ARG(archive_entry_mtime), |
| 99 |
DLSYM_ARG(archive_entry_mtime_is_set), |
| 100 |
DLSYM_ARG(archive_entry_mtime_nsec), |
| 101 |
DLSYM_ARG(archive_entry_new), |
| 102 |
DLSYM_ARG(archive_entry_pathname), |
| 103 |
DLSYM_ARG(archive_entry_rdevmajor), |
| 104 |
DLSYM_ARG(archive_entry_rdevminor), |
| 105 |
DLSYM_ARG(archive_entry_set_ctime), |
| 106 |
DLSYM_ARG(archive_entry_set_fflags), |
| 107 |
DLSYM_ARG(archive_entry_set_filetype), |
| 108 |
DLSYM_ARG(archive_entry_set_gid), |
| 109 |
DLSYM_ARG(archive_entry_set_hardlink), |
| 110 |
DLSYM_ARG(archive_entry_set_mtime), |
| 111 |
DLSYM_ARG(archive_entry_set_pathname), |
| 112 |
DLSYM_ARG(archive_entry_set_perm), |
| 113 |
DLSYM_ARG(archive_entry_set_rdevmajor), |
| 114 |
DLSYM_ARG(archive_entry_set_rdevminor), |
| 115 |
DLSYM_ARG(archive_entry_set_size), |
| 116 |
DLSYM_ARG(archive_entry_set_symlink), |
| 117 |
DLSYM_ARG(archive_entry_set_uid), |
| 118 |
DLSYM_ARG(archive_entry_sparse_add_entry), |
| 119 |
DLSYM_ARG(archive_entry_symlink), |
| 120 |
DLSYM_ARG(archive_entry_uid), |
| 121 |
DLSYM_ARG(archive_entry_xattr_add_entry), |
| 122 |
DLSYM_ARG(archive_entry_xattr_next), |
| 123 |
DLSYM_ARG(archive_entry_xattr_reset), |
| 124 |
DLSYM_ARG(archive_error_string), |
| 125 |
DLSYM_ARG(archive_read_data_into_fd), |
| 126 |
DLSYM_ARG(archive_read_free), |
| 127 |
DLSYM_ARG(archive_read_new), |
| 128 |
DLSYM_ARG(archive_read_next_header), |
| 129 |
DLSYM_ARG(archive_read_open_fd), |
| 130 |
DLSYM_ARG(archive_read_support_format_cpio), |
| 131 |
DLSYM_ARG(archive_read_support_format_tar), |
| 132 |
DLSYM_ARG(archive_write_close), |
| 133 |
DLSYM_ARG(archive_write_data), |
| 134 |
DLSYM_ARG(archive_write_free), |
| 135 |
DLSYM_ARG(archive_write_header), |
| 136 |
DLSYM_ARG(archive_write_new), |
| 137 |
DLSYM_ARG(archive_write_open_FILE), |
| 138 |
DLSYM_ARG(archive_write_open_fd), |
| 139 |
DLSYM_ARG(archive_write_set_format_filter_by_ext), |
| 140 |
DLSYM_ARG(archive_write_set_format_pax)); |
| 141 |
if (r <= 0) |
| 142 |
return r; |
| 143 |
|
| 144 |
/* Optional symbols: archive_entry_gid_is_set and archive_entry_uid_is_set exist only in libarchive |
| 145 |
* 3.7.3+, archive_entry_hardlink_is_set exists only in 3.7.5+. If missing, sym_X keeps its |
| 146 |
* fallback-function initializer (see above). */ |
| 147 |
DLSYM_OPTIONAL(libarchive_dl, archive_entry_gid_is_set); |
| 148 |
DLSYM_OPTIONAL(libarchive_dl, archive_entry_uid_is_set); |
| 149 |
DLSYM_OPTIONAL(libarchive_dl, archive_entry_hardlink_is_set); |
| 150 |
|
| 151 |
return 1; |
| 152 |
#else |
| 153 |
return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 154 |
"libarchive support is not compiled in."); |
| 155 |
#endif |
| 156 |
} |
| 157 |
|
| 158 |
#if HAVE_LIBARCHIVE |
| 159 |
/* libarchive uses its own file type macros. They happen to be defined the same way as the Linux ones, and |
| 160 |
* we'd like to rely on it. Let's verify this first though. */ |
| 161 |
assert_cc(S_IFDIR == AE_IFDIR); |
| 162 |
assert_cc(S_IFREG == AE_IFREG); |
| 163 |
assert_cc(S_IFLNK == AE_IFLNK); |
| 164 |
assert_cc(S_IFBLK == AE_IFBLK); |
| 165 |
assert_cc(S_IFCHR == AE_IFCHR); |
| 166 |
assert_cc(S_IFIFO == AE_IFIFO); |
| 167 |
assert_cc(S_IFSOCK == AE_IFSOCK); |
| 168 |
|
| 169 |
#endif |