Debian Codegraph @

src/shared/libmount-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "libmount-util.h"
4 #include "log.h"
5
6 #if HAVE_LIBMOUNT
7
8 #include <stdio.h>
9
10 #include "sd-dlopen.h"
11
12 #include "fstab-util.h"
13
14 DLSYM_PROTOTYPE(mnt_free_iter) = NULL;
15 DLSYM_PROTOTYPE(mnt_free_table) = NULL;
16 DLSYM_PROTOTYPE(mnt_fs_get_fs_options) = NULL;
17 DLSYM_PROTOTYPE(mnt_fs_get_fstype) = NULL;
18 DLSYM_PROTOTYPE(mnt_fs_get_id) = NULL;
19 DLSYM_PROTOTYPE(mnt_fs_get_option) = NULL;
20 DLSYM_PROTOTYPE(mnt_fs_get_options) = NULL;
21 DLSYM_PROTOTYPE(mnt_fs_get_passno) = NULL;
22 DLSYM_PROTOTYPE(mnt_fs_get_propagation) = NULL;
23 DLSYM_PROTOTYPE(mnt_fs_get_source) = NULL;
24 DLSYM_PROTOTYPE(mnt_fs_get_target) = NULL;
25 DLSYM_PROTOTYPE(mnt_fs_get_vfs_options) = NULL;
26 DLSYM_PROTOTYPE(mnt_get_builtin_optmap) = NULL;
27 DLSYM_PROTOTYPE(mnt_init_debug) = NULL;
28 DLSYM_PROTOTYPE(mnt_monitor_enable_kernel) = NULL;
29 DLSYM_PROTOTYPE(mnt_monitor_enable_userspace) = NULL;
30 DLSYM_PROTOTYPE(mnt_monitor_get_fd) = NULL;
31 DLSYM_PROTOTYPE(mnt_monitor_next_change) = NULL;
32 DLSYM_PROTOTYPE(mnt_new_iter) = NULL;
33 DLSYM_PROTOTYPE(mnt_new_monitor) = NULL;
34 DLSYM_PROTOTYPE(mnt_new_table) = NULL;
35 DLSYM_PROTOTYPE(mnt_optstr_get_flags) = NULL;
36 DLSYM_PROTOTYPE(mnt_table_find_devno) = NULL;
37 DLSYM_PROTOTYPE(mnt_table_find_target) = NULL;
38 DLSYM_PROTOTYPE(mnt_table_next_child_fs) = NULL;
39 DLSYM_PROTOTYPE(mnt_table_next_fs) = NULL;
40 DLSYM_PROTOTYPE(mnt_table_parse_file) = NULL;
41 DLSYM_PROTOTYPE(mnt_table_parse_mtab) = NULL;
42 DLSYM_PROTOTYPE(mnt_table_parse_stream) = NULL;
43 DLSYM_PROTOTYPE(mnt_table_parse_swaps) = NULL;
44 DLSYM_PROTOTYPE(mnt_unref_monitor) = NULL;
45
46 int libmount_parse_full(
47 const char *path,
48 FILE *source,
49 int direction,
50 struct libmnt_table **ret_table,
51 struct libmnt_iter **ret_iter) {
52
53 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
54 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
55 int r;
56
57 /* Older libmount seems to require this. */
58 assert(!source || path);
59 assert(IN_SET(direction, MNT_ITER_FORWARD, MNT_ITER_BACKWARD));
60 assert(ret_table);
61 assert(ret_iter);
62
63 r = dlopen_libmount(LOG_DEBUG);
64 if (r < 0)
65 return r;
66
67 table = sym_mnt_new_table();
68 iter = sym_mnt_new_iter(direction);
69 if (!table || !iter)
70 return -ENOMEM;
71
72 /* If source or path are specified, we use on the functions which ignore utab.
73 * Only if both are empty, we use mnt_table_parse_mtab(). */
74
75 if (source)
76 r = sym_mnt_table_parse_stream(table, source, path);
77 else if (path)
78 r = sym_mnt_table_parse_file(table, path);
79 else
80 r = sym_mnt_table_parse_mtab(table, NULL);
81 if (r < 0)
82 return r;
83
84 *ret_table = TAKE_PTR(table);
85 *ret_iter = TAKE_PTR(iter);
86 return 0;
87 }
88
89 int libmount_parse_fstab(
90 struct libmnt_table **ret_table,
91 struct libmnt_iter **ret_iter) {
92
93 return libmount_parse_full(fstab_path(), NULL, MNT_ITER_FORWARD, ret_table, ret_iter);
94 }
95
96 int libmount_is_leaf(
97 struct libmnt_table *table,
98 struct libmnt_fs *fs) {
99 int r;
100
101 assert(table);
102
103 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter_children = NULL;
104 iter_children = sym_mnt_new_iter(MNT_ITER_FORWARD);
105 if (!iter_children)
106 return log_oom();
107
108 /* We care only whether it exists, it is unused */
109 _unused_ struct libmnt_fs *child;
110 r = sym_mnt_table_next_child_fs(table, iter_children, fs, &child);
111 if (r < 0)
112 return r;
113
114 return r == 1;
115 }
116
117 #endif
118
119 int dlopen_libmount(int log_level) {
120 #if HAVE_LIBMOUNT
121 static void *libmount_dl = NULL;
122
123 LIBMOUNT_NOTE(SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
124
125 return dlopen_many_sym_or_warn(
126 &libmount_dl,
127 "libmount.so.1",
128 log_level,
129 DLSYM_ARG(mnt_free_iter),
130 DLSYM_ARG(mnt_free_table),
131 DLSYM_ARG(mnt_fs_get_fs_options),
132 DLSYM_ARG(mnt_fs_get_fstype),
133 DLSYM_ARG(mnt_fs_get_id),
134 DLSYM_ARG(mnt_fs_get_option),
135 DLSYM_ARG(mnt_fs_get_options),
136 DLSYM_ARG(mnt_fs_get_passno),
137 DLSYM_ARG(mnt_fs_get_propagation),
138 DLSYM_ARG(mnt_fs_get_source),
139 DLSYM_ARG(mnt_fs_get_target),
140 DLSYM_ARG(mnt_fs_get_vfs_options),
141 DLSYM_ARG(mnt_get_builtin_optmap),
142 DLSYM_ARG(mnt_init_debug),
143 DLSYM_ARG(mnt_monitor_enable_kernel),
144 DLSYM_ARG(mnt_monitor_enable_userspace),
145 DLSYM_ARG(mnt_monitor_get_fd),
146 DLSYM_ARG(mnt_monitor_next_change),
147 DLSYM_ARG(mnt_new_iter),
148 DLSYM_ARG(mnt_new_monitor),
149 DLSYM_ARG(mnt_new_table),
150 DLSYM_ARG(mnt_optstr_get_flags),
151 DLSYM_ARG(mnt_table_find_devno),
152 DLSYM_ARG(mnt_table_find_target),
153 DLSYM_ARG(mnt_table_next_child_fs),
154 DLSYM_ARG(mnt_table_next_fs),
155 DLSYM_ARG(mnt_table_parse_file),
156 DLSYM_ARG(mnt_table_parse_mtab),
157 DLSYM_ARG(mnt_table_parse_stream),
158 DLSYM_ARG(mnt_table_parse_swaps),
159 DLSYM_ARG(mnt_unref_monitor));
160 #else
161 return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
162 "libmount support is not compiled in.");
163 #endif
164 }