Debian Codegraph @

src/shared/fstab-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include "alloc-util.h"
7 #include "device-nodes.h"
8 #include "errno-util.h"
9 #include "extract-word.h"
10 #include "fstab-util.h"
11 #include "initrd-util.h"
12 #include "libmount-util.h"
13 #include "log.h"
14 #include "nulstr-util.h"
15 #include "parse-util.h"
16 #include "path-util.h"
17 #include "proc-cmdline.h"
18 #include "string-util.h"
19 #include "strv.h"
20
21 bool fstab_enabled_full(int enabled) {
22 static int cached = -1;
23 bool val = true; /* If nothing specified or the check fails, then defaults to true. */
24 int r;
25
26 /* If 'enabled' is non-negative, then update the cache with it. */
27 if (enabled >= 0)
28 cached = enabled;
29
30 if (cached >= 0)
31 return cached;
32
33 r = proc_cmdline_get_bool("fstab", PROC_CMDLINE_STRIP_RD_PREFIX|PROC_CMDLINE_TRUE_WHEN_MISSING, &val);
34 if (r < 0)
35 log_debug_errno(r, "Failed to parse fstab= kernel command line option, ignoring: %m");
36
37 return (cached = val);
38 }
39
40 int fstab_has_fstype(const char *fstype) {
41 #if HAVE_LIBMOUNT
42 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
43 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
44 int r;
45
46 assert(fstype);
47
48 if (!fstab_enabled())
49 return false;
50
51 r = libmount_parse_fstab(&table, &iter);
52 if (r == -ENOENT)
53 return false;
54 if (r < 0)
55 return r;
56
57 for (;;) {
58 struct libmnt_fs *fs;
59
60 r = sym_mnt_table_next_fs(table, iter, &fs);
61 if (r < 0)
62 return r;
63 if (r > 0) /* EOF */
64 return false;
65
66 if (streq_ptr(sym_mnt_fs_get_fstype(fs), fstype))
67 return true;
68 }
69 #else
70 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libmount support not compiled in");
71 #endif
72 }
73
74 bool fstab_is_extrinsic(const char *mount, const char *opts) {
75
76 /* Don't bother with the OS data itself */
77 if (PATH_IN_SET(mount,
78 "/",
79 "/usr",
80 "/etc"))
81 return true;
82
83 if (PATH_STARTSWITH_SET(mount,
84 "/run/initramfs", /* This should stay around from before we boot until after we shutdown */
85 "/run/nextroot", /* Similar (though might be updated from the host) */
86 "/proc", /* All of this is API VFS */
87 "/sys", /* … ditto … */
88 "/dev")) /* … ditto … */
89 return true;
90
91 /* If this is an initrd mount, and we are not in the initrd, then leave
92 * this around forever, too. */
93 if (fstab_test_option(opts, "x-initrd.mount\0") && !in_initrd())
94 return true;
95
96 return false;
97 }
98
99 #if HAVE_LIBMOUNT
100 static int fstab_is_same_node(const char *what_fstab, const char *path) {
101 _cleanup_free_ char *node = NULL;
102
103 assert(what_fstab);
104 assert(path);
105
106 node = fstab_node_to_udev_node(what_fstab);
107 if (!node)
108 return -ENOMEM;
109
110 if (path_equal(node, path))
111 return true;
112
113 if (is_device_path(path) && is_device_path(node))
114 return devnode_same(node, path);
115
116 return false;
117 }
118 #endif
119
120 int fstab_has_mount_point_prefix_strv(char * const *prefixes) {
121 #if HAVE_LIBMOUNT
122 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
123 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
124 int r;
125
126 assert(!strv_isempty(prefixes));
127
128 /* This function returns true if at least one entry in fstab has a mount point that starts with one
129 * of the passed prefixes. */
130
131 if (!fstab_enabled())
132 return false;
133
134 r = libmount_parse_fstab(&table, &iter);
135 if (r == -ENOENT)
136 return false;
137 if (r < 0)
138 return r;
139
140 for (;;) {
141 struct libmnt_fs *fs;
142 const char *path;
143
144 r = sym_mnt_table_next_fs(table, iter, &fs);
145 if (r < 0)
146 return r;
147 if (r > 0) /* EOF */
148 return false;
149
150 path = sym_mnt_fs_get_target(fs);
151 if (!path)
152 continue;
153
154 if (path_startswith_strv(path, prefixes))
155 return true;
156 }
157 #else
158 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libmount support not compiled in");
159 #endif
160 }
161
162 int fstab_is_mount_point_full(const char *where, const char *path) {
163 #if HAVE_LIBMOUNT
164 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
165 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
166 int r;
167
168 assert(where || path);
169
170 if (!fstab_enabled())
171 return false;
172
173 r = libmount_parse_fstab(&table, &iter);
174 if (r == -ENOENT)
175 return false;
176 if (r < 0)
177 return r;
178
179 for (;;) {
180 struct libmnt_fs *fs;
181
182 r = sym_mnt_table_next_fs(table, iter, &fs);
183 if (r < 0)
184 return r;
185 if (r > 0) /* EOF */
186 return false;
187
188 if (where && !path_equal(sym_mnt_fs_get_target(fs), where))
189 continue;
190
191 if (!path)
192 return true;
193
194 r = fstab_is_same_node(sym_mnt_fs_get_source(fs), path);
195 if (r > 0 || (r < 0 && !ERRNO_IS_DEVICE_ABSENT(r)))
196 return r;
197 }
198 #else
199 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libmount support not compiled in");
200 #endif
201 }
202
203 int fstab_filter_options(
204 const char *opts,
205 const char *names,
206 const char **ret_namefound,
207 char **ret_value,
208 char ***ret_values,
209 char **ret_filtered) {
210
211 _cleanup_strv_free_ char **values = NULL;
212 _cleanup_free_ char *value = NULL, *filtered = NULL;
213 const char *namefound = NULL;
214 int r;
215
216 assert(!isempty(names));
217 assert(!(ret_value && ret_values));
218
219 if (!opts)
220 goto finish;
221
222 /* Finds any options matching 'names', and returns:
223 * - the last matching option name in ret_namefound,
224 * - the last matching value in ret_value,
225 * - any matching values in ret_values,
226 * - the rest of the option string in ret_filtered.
227 *
228 * If !ret_value and !ret_values and !ret_filtered, this function is not allowed to fail.
229 *
230 * Returns negative on error, true if any matching options were found, false otherwise. */
231
232 if (ret_value || ret_values || ret_filtered) {
233 _cleanup_strv_free_ char **opts_split = NULL;
234 _cleanup_free_ char **filtered_strv = NULL; /* strings are owned by 'opts_split' */
235
236 /* For backwards compatibility, we need to pass-through escape characters.
237 * The only ones we "consume" are the ones used as "\," or "\\". */
238 r = strv_split_full(&opts_split, opts, ",", EXTRACT_UNESCAPE_SEPARATORS|EXTRACT_UNESCAPE_RELAX);
239 if (r < 0)
240 return r;
241
242 STRV_FOREACH(opt, opts_split) {
243 bool found = false;
244 const char *x;
245
246 NULSTR_FOREACH(name, names) {
247 x = startswith(*opt, name);
248 if (!x)
249 continue;
250
251 /* If ret_values, only accept settings followed by assignment. */
252 if (*x == '=' || (!ret_values && *x == '\0')) {
253 namefound = name;
254 found = true;
255 break;
256 }
257 }
258
259 if (found) {
260 if (ret_value)
261 r = free_and_strdup(&value, *x == '=' ? x + 1 : NULL);
262 else if (ret_values)
263 r = strv_extend(&values, x + 1);
264 else
265 r = 0;
266 } else
267 r = strv_push(&filtered_strv, *opt);
268 if (r < 0)
269 return r;
270 }
271
272 if (ret_filtered) {
273 filtered = strv_join_full(filtered_strv, ",", NULL, /* escape_separator= */ true);
274 if (!filtered)
275 return -ENOMEM;
276 }
277 } else
278 for (const char *word = opts;;) {
279 const char *end = word;
280
281 /* Look for a *non-escaped* comma separator. Only commas and backslashes can be
282 * escaped, so "\," and "\\" are the only valid escape sequences, and we can do a
283 * very simple test here. */
284 for (;;) {
285 end += strcspn(end, ",\\");
286
287 if (IN_SET(*end, ',', '\0'))
288 break;
289 assert(*end == '\\');
290 end++; /* Skip the backslash */
291 if (*end != '\0')
292 end++; /* Skip the escaped char, but watch out for a trailing comma */
293 }
294
295 NULSTR_FOREACH(name, names) {
296 const char *x = startswith(word, name);
297 if (!x || x > end)
298 continue;
299
300 /* We know that the string is NUL terminated, so *x is valid */
301 if (IN_SET(*x, '\0', '=', ',')) {
302 namefound = name;
303 break;
304 }
305 }
306
307 if (*end == '\0')
308 break;
309
310 word = end + 1;
311 }
312
313 finish:
314 if (ret_namefound)
315 *ret_namefound = namefound; /* owned by 'names' (passed-in) */
316 if (ret_value)
317 *ret_value = TAKE_PTR(value);
318 if (ret_values)
319 *ret_values = TAKE_PTR(values);
320 if (ret_filtered)
321 *ret_filtered = TAKE_PTR(filtered);
322
323 return !!namefound;
324 }
325
326 int fstab_find_pri(const char *opts, int *ret) {
327 _cleanup_free_ char *v = NULL;
328 int r, pri;
329
330 assert(ret);
331
332 r = fstab_filter_options(opts, "pri\0", NULL, &v, NULL, NULL);
333 if (r < 0)
334 return r;
335 if (r == 0 || !v)
336 return 0;
337
338 r = safe_atoi(v, &pri);
339 if (r < 0)
340 return r;
341
342 *ret = pri;
343 return 1;
344 }
345
346 static char *unquote(const char *s, const char* quotes) {
347 size_t l;
348 assert(s);
349
350 /* This is rather stupid, simply removes the heading and
351 * trailing quotes if there is one. Doesn't care about
352 * escaping or anything.
353 *
354 * DON'T USE THIS FOR NEW CODE ANYMORE! */
355
356 l = strlen(s);
357 if (l < 2)
358 return strdup(s);
359
360 if (strchr(quotes, s[0]) && s[l-1] == s[0])
361 return strndup(s+1, l-2);
362
363 return strdup(s);
364 }
365
366 static char *tag_to_udev_node(const char *tagvalue, const char *by) {
367 _cleanup_free_ char *t = NULL, *u = NULL;
368 size_t enc_len;
369
370 u = unquote(tagvalue, QUOTES);
371 if (!u)
372 return NULL;
373
374 enc_len = strlen(u) * 4 + 1;
375 t = new(char, enc_len);
376 if (!t)
377 return NULL;
378
379 if (encode_devnode_name(u, t, enc_len) < 0)
380 return NULL;
381
382 return strjoin("/dev/disk/by-", by, "/", t);
383 }
384
385 char* fstab_node_to_udev_node(const char *p) {
386 const char *q;
387
388 assert(p);
389
390 q = startswith(p, "LABEL=");
391 if (q)
392 return tag_to_udev_node(q, "label");
393
394 q = startswith(p, "UUID=");
395 if (q)
396 return tag_to_udev_node(q, "uuid");
397
398 q = startswith(p, "PARTUUID=");
399 if (q)
400 return tag_to_udev_node(q, "partuuid");
401
402 q = startswith(p, "PARTLABEL=");
403 if (q)
404 return tag_to_udev_node(q, "partlabel");
405
406 return strdup(p);
407 }
408
409 const char* fstab_path(void) {
410 return secure_getenv("SYSTEMD_FSTAB") ?: "/etc/fstab";
411 }
412
413 bool fstab_is_bind(const char *options, const char *fstype) {
414
415 if (fstab_test_option(options, "bind\0" "rbind\0"))
416 return true;
417
418 if (fstype && STR_IN_SET(fstype, "bind", "rbind"))
419 return true;
420
421 return false;
422 }