]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | /* | |
3 | * This file is part of libmount from util-linux project. | |
4 | * | |
5 | * Copyright (C) 2010-2018 Karel Zak <kzak@redhat.com> | |
6 | * | |
7 | * libmount is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU Lesser General Public License as published by | |
9 | * the Free Software Foundation; either version 2.1 of the License, or | |
10 | * (at your option) any later version. | |
11 | */ | |
12 | ||
13 | /** | |
14 | * SECTION: context | |
15 | * @title: Library high-level context | |
16 | * @short_description: high-level API to mount/umount devices. | |
17 | * | |
18 | * <informalexample> | |
19 | * <programlisting> | |
20 | * struct libmnt_context *cxt = mnt_new_context(); | |
21 | * | |
22 | * mnt_context_set_options(cxt, "aaa,bbb,ccc=CCC"); | |
23 | * mnt_context_set_mflags(cxt, MS_NOATIME|MS_NOEXEC); | |
24 | * mnt_context_set_target(cxt, "/mnt/foo"); | |
25 | * | |
26 | * if (!mnt_context_mount(cxt)) | |
27 | * printf("successfully mounted\n"); | |
28 | * mnt_free_context(cxt); | |
29 | * | |
30 | * </programlisting> | |
31 | * </informalexample> | |
32 | * | |
33 | * This code is similar to: | |
34 | * | |
35 | * mount -o aaa,bbb,ccc=CCC,noatime,noexec /mnt/foo | |
36 | * | |
37 | */ | |
38 | ||
39 | #include "mountP.h" | |
40 | #include "strutils.h" | |
41 | #include "namespace.h" | |
42 | #include "match.h" | |
43 | ||
44 | #include <stdarg.h> | |
45 | #include <sys/wait.h> | |
46 | ||
47 | #include "mount-api-utils.h" | |
48 | #include "strv.h" | |
49 | ||
50 | /** | |
51 | * mnt_new_context: | |
52 | * | |
53 | * Returns: newly allocated mount context | |
54 | */ | |
55 | struct libmnt_context *mnt_new_context(void) | |
56 | { | |
57 | struct libmnt_context *cxt; | |
58 | uid_t ruid; | |
59 | ||
60 | cxt = calloc(1, sizeof(*cxt)); | |
61 | if (!cxt) | |
62 | return NULL; | |
63 | ||
64 | ruid = getuid(); | |
65 | ||
66 | mnt_context_reset_status(cxt); | |
67 | ||
68 | cxt->ns_orig.fd = -1; | |
69 | cxt->ns_tgt.fd = -1; | |
70 | cxt->ns_cur = &cxt->ns_orig; | |
71 | ||
72 | cxt->map_linux = mnt_get_builtin_optmap(MNT_LINUX_MAP); | |
73 | cxt->map_userspace = mnt_get_builtin_optmap(MNT_USERSPACE_MAP); | |
74 | ||
75 | INIT_LIST_HEAD(&cxt->hooksets_hooks); | |
76 | INIT_LIST_HEAD(&cxt->hooksets_datas); | |
77 | ||
78 | /* if we're really root and aren't running setuid */ | |
79 | cxt->restricted = (uid_t) 0 == ruid && !is_privileged_execution() ? 0 : 1; | |
80 | ||
81 | cxt->noautofs = 0; | |
82 | ||
83 | DBG(CXT, ul_debugobj(cxt, "----> allocate %s", | |
84 | cxt->restricted ? "[RESTRICTED]" : "")); | |
85 | ||
86 | return cxt; | |
87 | } | |
88 | ||
89 | /** | |
90 | * mnt_free_context: | |
91 | * @cxt: mount context | |
92 | * | |
93 | * Deallocates context struct. | |
94 | */ | |
95 | void mnt_free_context(struct libmnt_context *cxt) | |
96 | { | |
97 | if (!cxt) | |
98 | return; | |
99 | ||
100 | mnt_reset_context(cxt); | |
101 | ||
102 | free(cxt->fstype_pattern); | |
103 | free(cxt->optstr_pattern); | |
104 | free(cxt->tgt_prefix); | |
105 | ||
106 | mnt_unref_table(cxt->fstab); | |
107 | mnt_unref_cache(cxt->cache); | |
108 | mnt_unref_fs(cxt->fs); | |
109 | ||
110 | mnt_unref_optlist(cxt->optlist_saved); | |
111 | mnt_unref_optlist(cxt->optlist); | |
112 | ||
113 | mnt_unref_lock(cxt->lock); | |
114 | mnt_free_update(cxt->update); | |
115 | ||
116 | mnt_context_set_target_ns(cxt, NULL); | |
117 | ||
118 | free(cxt->children); | |
119 | ||
120 | DBG(CXT, ul_debugobj(cxt, "free")); | |
121 | free(cxt); | |
122 | } | |
123 | ||
124 | /** | |
125 | * mnt_reset_context: | |
126 | * @cxt: mount context | |
127 | * | |
128 | * Resets all information in the context that is directly related to | |
129 | * the latest mount (spec, source, target, mount options, ...). | |
130 | * | |
131 | * The match patterns, target namespace, prefix, cached fstab, cached canonicalized | |
132 | * paths and tags and [e]uid are not reset. You have to use | |
133 | * | |
134 | * mnt_context_set_fstab(cxt, NULL); | |
135 | * mnt_context_set_cache(cxt, NULL); | |
136 | * mnt_context_set_fstype_pattern(cxt, NULL); | |
137 | * mnt_context_set_options_pattern(cxt, NULL); | |
138 | * mnt_context_set_target_ns(cxt, NULL); | |
139 | * | |
140 | * to reset this stuff. | |
141 | * | |
142 | * Returns: 0 on success, negative number in case of error. | |
143 | */ | |
144 | int mnt_reset_context(struct libmnt_context *cxt) | |
145 | { | |
146 | int fl; | |
147 | ||
148 | if (!cxt) | |
149 | return -EINVAL; | |
150 | ||
151 | DBG(CXT, ul_debugobj(cxt, "<---- reset [status=%d] ---->", | |
152 | mnt_context_get_status(cxt))); | |
153 | ||
154 | fl = cxt->flags; | |
155 | ||
156 | mnt_unref_fs(cxt->fs); | |
157 | mnt_unref_table(cxt->mountinfo); | |
158 | mnt_unref_table(cxt->utab); | |
159 | mnt_unref_optlist(cxt->optlist); | |
160 | ||
161 | free(cxt->helper); | |
162 | ||
163 | cxt->fs = NULL; | |
164 | cxt->mountinfo = NULL; | |
165 | cxt->optlist = NULL; | |
166 | cxt->utab = NULL; | |
167 | cxt->helper = NULL; | |
168 | cxt->mountdata = NULL; | |
169 | cxt->flags = MNT_FL_DEFAULT; | |
170 | cxt->noautofs = 0; | |
171 | cxt->has_selinux_opt = 0; | |
172 | ||
173 | cxt->map_linux = mnt_get_builtin_optmap(MNT_LINUX_MAP); | |
174 | cxt->map_userspace = mnt_get_builtin_optmap(MNT_USERSPACE_MAP); | |
175 | ||
176 | mnt_context_reset_status(cxt); | |
177 | mnt_context_deinit_hooksets(cxt); | |
178 | ||
179 | if (cxt->table_fltrcb) | |
180 | mnt_context_set_tabfilter(cxt, NULL, NULL); | |
181 | ||
182 | /* restore non-resettable flags */ | |
183 | cxt->flags |= (fl & MNT_FL_NOMTAB); | |
184 | cxt->flags |= (fl & MNT_FL_FAKE); | |
185 | cxt->flags |= (fl & MNT_FL_SLOPPY); | |
186 | cxt->flags |= (fl & MNT_FL_VERBOSE); | |
187 | cxt->flags |= (fl & MNT_FL_NOHELPERS); | |
188 | cxt->flags |= (fl & MNT_FL_LOOPDEL); | |
189 | cxt->flags |= (fl & MNT_FL_LAZY); | |
190 | cxt->flags |= (fl & MNT_FL_FORK); | |
191 | cxt->flags |= (fl & MNT_FL_FORCE); | |
192 | cxt->flags |= (fl & MNT_FL_NOCANONICALIZE); | |
193 | cxt->flags |= (fl & MNT_FL_RDONLY_UMOUNT); | |
194 | cxt->flags |= (fl & MNT_FL_RWONLY_MOUNT); | |
195 | cxt->flags |= (fl & MNT_FL_NOSWAPMATCH); | |
196 | cxt->flags |= (fl & MNT_FL_TABPATHS_CHECKED); | |
197 | ||
198 | mnt_context_apply_template(cxt); | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
203 | /* | |
204 | * Saves the current context setting (mount options, etc) to make it usable after | |
205 | * mnt_reset_context() or by mnt_context_apply_template(). This is usable for | |
206 | * example for mnt_context_next_mount() where for the next mount operation we | |
207 | * need to restore to the original context setting. | |
208 | * | |
209 | * Returns: 0 on success, negative number in case of error. | |
210 | */ | |
211 | int mnt_context_save_template(struct libmnt_context *cxt) | |
212 | { | |
213 | if (!cxt) | |
214 | return -EINVAL; | |
215 | ||
216 | DBG(CXT, ul_debugobj(cxt, "saving template")); | |
217 | ||
218 | /* reset old saved data */ | |
219 | mnt_unref_optlist(cxt->optlist_saved); | |
220 | cxt->optlist_saved = NULL; | |
221 | ||
222 | if (cxt->optlist) | |
223 | cxt->optlist_saved = mnt_copy_optlist(cxt->optlist); | |
224 | ||
225 | return 0; | |
226 | } | |
227 | ||
228 | /* | |
229 | * Restores context FS setting from previously saved template (see | |
230 | * mnt_context_save_template()). | |
231 | * | |
232 | * Returns: 0 on success, negative number in case of error. | |
233 | */ | |
234 | int mnt_context_apply_template(struct libmnt_context *cxt) | |
235 | { | |
236 | if (!cxt) | |
237 | return -EINVAL; | |
238 | ||
239 | if (cxt->optlist) { | |
240 | mnt_unref_optlist(cxt->optlist); | |
241 | cxt->optlist = NULL; | |
242 | } | |
243 | ||
244 | if (cxt->optlist_saved) { | |
245 | DBG(CXT, ul_debugobj(cxt, "restoring template")); | |
246 | cxt->optlist = mnt_copy_optlist(cxt->optlist_saved); | |
247 | } | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | int mnt_context_has_template(struct libmnt_context *cxt) | |
253 | { | |
254 | return cxt && cxt->optlist_saved ? 1 : 0; | |
255 | } | |
256 | ||
257 | struct libmnt_context *mnt_copy_context(struct libmnt_context *o) | |
258 | { | |
259 | struct libmnt_context *n; | |
260 | ||
261 | n = mnt_new_context(); | |
262 | if (!n) | |
263 | return NULL; | |
264 | ||
265 | DBG(CXT, ul_debugobj(n, "<---- clone ---->")); | |
266 | ||
267 | n->flags = o->flags; | |
268 | ||
269 | if (o->fs) { | |
270 | n->fs = mnt_copy_fs(NULL, o->fs); | |
271 | if (!n->fs) | |
272 | goto failed; | |
273 | } | |
274 | ||
275 | n->mountinfo = o->mountinfo; | |
276 | mnt_ref_table(n->mountinfo); | |
277 | ||
278 | n->utab = o->utab; | |
279 | mnt_ref_table(n->utab); | |
280 | ||
281 | if (strdup_between_structs(n, o, tgt_prefix)) | |
282 | goto failed; | |
283 | if (strdup_between_structs(n, o, helper)) | |
284 | goto failed; | |
285 | ||
286 | n->map_linux = o->map_linux; | |
287 | n->map_userspace = o->map_userspace; | |
288 | ||
289 | mnt_context_reset_status(n); | |
290 | ||
291 | n->table_fltrcb = o->table_fltrcb; | |
292 | n->table_fltrcb_data = o->table_fltrcb_data; | |
293 | ||
294 | n->noautofs = o->noautofs; | |
295 | n->has_selinux_opt = o->has_selinux_opt; | |
296 | ||
297 | return n; | |
298 | failed: | |
299 | mnt_free_context(n); | |
300 | return NULL; | |
301 | } | |
302 | ||
303 | /** | |
304 | * mnt_context_reset_status: | |
305 | * @cxt: context | |
306 | * | |
307 | * Resets mount(2) and mount.type statuses, so mnt_context_do_mount() or | |
308 | * mnt_context_do_umount() could be again called with the same settings. | |
309 | * | |
310 | * BE CAREFUL -- after this soft reset the libmount will NOT parse mount | |
311 | * options, evaluate permissions or apply stuff from fstab. | |
312 | * | |
313 | * Returns: 0 on success, negative number in case of error. | |
314 | */ | |
315 | int mnt_context_reset_status(struct libmnt_context *cxt) | |
316 | { | |
317 | if (!cxt) | |
318 | return -EINVAL; | |
319 | ||
320 | mnt_context_syscall_reset_status(cxt); | |
321 | ||
322 | cxt->syscall_status = 1; /* means not called yet */ | |
323 | cxt->helper_exec_status = 1; | |
324 | cxt->helper_status = 0; | |
325 | return 0; | |
326 | } | |
327 | ||
328 | static int context_init_paths(struct libmnt_context *cxt, int writable) | |
329 | { | |
330 | struct libmnt_ns *ns_old; | |
331 | ||
332 | assert(cxt); | |
333 | ||
334 | if (!cxt->utab_path) { | |
335 | cxt->utab_path = mnt_get_utab_path(); | |
336 | DBG(CXT, ul_debugobj(cxt, "utab path initialized to: %s", cxt->utab_path)); | |
337 | } | |
338 | ||
339 | if (!writable) | |
340 | return 0; /* only paths wanted */ | |
341 | if (mnt_context_is_nomtab(cxt)) | |
342 | return 0; /* write mode overridden by mount -n */ | |
343 | if (cxt->flags & MNT_FL_TABPATHS_CHECKED) | |
344 | return 0; | |
345 | ||
346 | DBG(CXT, ul_debugobj(cxt, "checking for writable tab files")); | |
347 | ||
348 | ns_old = mnt_context_switch_target_ns(cxt); | |
349 | if (!ns_old) | |
350 | return -MNT_ERR_NAMESPACE; | |
351 | ||
352 | mnt_has_regular_utab(&cxt->utab_path, &cxt->utab_writable); | |
353 | ||
354 | if (!mnt_context_switch_ns(cxt, ns_old)) | |
355 | return -MNT_ERR_NAMESPACE; | |
356 | ||
357 | cxt->flags |= MNT_FL_TABPATHS_CHECKED; | |
358 | return 0; | |
359 | } | |
360 | ||
361 | int mnt_context_utab_writable(struct libmnt_context *cxt) | |
362 | { | |
363 | assert(cxt); | |
364 | ||
365 | context_init_paths(cxt, 1); | |
366 | return cxt->utab_writable == 1; | |
367 | } | |
368 | ||
369 | const char *mnt_context_get_writable_tabpath(struct libmnt_context *cxt) | |
370 | { | |
371 | assert(cxt); | |
372 | ||
373 | return mnt_context_utab_writable(cxt) ? cxt->utab_path : NULL; | |
374 | } | |
375 | ||
376 | ||
377 | static int set_flag(struct libmnt_context *cxt, int flag, int enable) | |
378 | { | |
379 | if (!cxt) | |
380 | return -EINVAL; | |
381 | if (enable) { | |
382 | DBG(CXT, ul_debugobj(cxt, "enabling flag %04x", flag)); | |
383 | cxt->flags |= flag; | |
384 | } else { | |
385 | DBG(CXT, ul_debugobj(cxt, "disabling flag %04x", flag)); | |
386 | cxt->flags &= ~flag; | |
387 | } | |
388 | return 0; | |
389 | } | |
390 | ||
391 | /** | |
392 | * mnt_context_is_restricted: | |
393 | * @cxt: mount context | |
394 | * | |
395 | * Returns: 0 for an unrestricted mount (user is root), or 1 for non-root mounts | |
396 | */ | |
397 | int mnt_context_is_restricted(struct libmnt_context *cxt) | |
398 | { | |
399 | return cxt->restricted; | |
400 | } | |
401 | ||
402 | /** | |
403 | * mnt_context_force_unrestricted: | |
404 | * @cxt: mount context | |
405 | * | |
406 | * This function is DANGEROURS as it disables all security policies in libmount. | |
407 | * Don't use if not sure. It removes "restricted" flag from the context, so | |
408 | * libmount will use the current context as for root user. | |
409 | * | |
410 | * This function is designed for case you have no any suid permissions, so you | |
411 | * can depend on kernel. | |
412 | * | |
413 | * Returns: 0 on success, negative number in case of error. | |
414 | * | |
415 | * Since: 2.35 | |
416 | */ | |
417 | int mnt_context_force_unrestricted(struct libmnt_context *cxt) | |
418 | { | |
419 | if (mnt_context_is_restricted(cxt)) { | |
420 | DBG(CXT, ul_debugobj(cxt, "force UNRESTRICTED")); | |
421 | cxt->restricted = 0; | |
422 | } | |
423 | ||
424 | return 0; | |
425 | } | |
426 | ||
427 | /** | |
428 | * mnt_context_set_optsmode | |
429 | * @cxt: mount context | |
430 | * @mode: MNT_OMODE_* flags | |
431 | * | |
432 | * Controls how to use mount optionssource and target paths from fstab/mountinfo. | |
433 | * | |
434 | * @MNT_OMODE_IGNORE: ignore fstab options | |
435 | * | |
436 | * @MNT_OMODE_APPEND: append fstab options to existing options | |
437 | * | |
438 | * @MNT_OMODE_PREPEND: prepend fstab options to existing options | |
439 | * | |
440 | * @MNT_OMODE_REPLACE: replace existing options with options from fstab | |
441 | * | |
442 | * @MNT_OMODE_FORCE: always read fstab (although source and target are defined) | |
443 | * | |
444 | * @MNT_OMODE_FSTAB: read from fstab | |
445 | * | |
446 | * @MNT_OMODE_MTAB: read from mountinfo if fstab not enabled or failed | |
447 | * | |
448 | * @MNT_OMODE_NOTAB: do not read fstab/mountinfoat all | |
449 | * | |
450 | * @MNT_OMODE_AUTO: default mode (MNT_OMODE_PREPEND | MNT_OMODE_FSTAB | MNT_OMODE_MTAB) | |
451 | * | |
452 | * @MNT_OMODE_USER: default for non-root users (MNT_OMODE_REPLACE | MNT_OMODE_FORCE | MNT_OMODE_FSTAB) | |
453 | * | |
454 | * Notes: | |
455 | * | |
456 | * - MNT_OMODE_USER is always used if mount context is in restricted mode | |
457 | * - MNT_OMODE_AUTO is used if nothing else is defined | |
458 | * - the flags are evaluated in this order: MNT_OMODE_NOTAB, MNT_OMODE_FORCE, | |
459 | * MNT_OMODE_FSTAB, MNT_OMODE_MTAB and then the mount options from fstab/mountinfo | |
460 | * are set according to MNT_OMODE_{IGNORE,APPEND,PREPEND,REPLACE} | |
461 | * | |
462 | * Returns: 0 on success, negative number in case of error. | |
463 | */ | |
464 | int mnt_context_set_optsmode(struct libmnt_context *cxt, int mode) | |
465 | { | |
466 | if (!cxt) | |
467 | return -EINVAL; | |
468 | cxt->optsmode = mode; | |
469 | return 0; | |
470 | } | |
471 | ||
472 | /** | |
473 | * mnt_context_get_optsmode | |
474 | * @cxt: mount context | |
475 | * | |
476 | * Returns: MNT_OMODE_* mask or zero. | |
477 | */ | |
478 | ||
479 | int mnt_context_get_optsmode(struct libmnt_context *cxt) | |
480 | { | |
481 | return cxt->optsmode; | |
482 | } | |
483 | ||
484 | /** | |
485 | * mnt_context_disable_canonicalize: | |
486 | * @cxt: mount context | |
487 | * @disable: TRUE or FALSE | |
488 | * | |
489 | * Enable/disable paths canonicalization and tags evaluation. The libmount context | |
490 | * canonicalizes paths when searching in fstab and when preparing source and target paths | |
491 | * for mount(2) syscall. | |
492 | * | |
493 | * This function has an effect on the private (within context) fstab instance only | |
494 | * (see mnt_context_set_fstab()). If you want to use an external fstab then you | |
495 | * need to manage your private struct libmnt_cache (see mnt_table_set_cache(fstab, | |
496 | * NULL). | |
497 | * | |
498 | * Returns: 0 on success, negative number in case of error. | |
499 | */ | |
500 | int mnt_context_disable_canonicalize(struct libmnt_context *cxt, int disable) | |
501 | { | |
502 | return set_flag(cxt, MNT_FL_NOCANONICALIZE, disable); | |
503 | } | |
504 | ||
505 | /** | |
506 | * mnt_context_is_nocanonicalize: | |
507 | * @cxt: mount context | |
508 | * | |
509 | * Returns: 1 if no-canonicalize mode (on [u]mount command line) is enabled or 0. | |
510 | */ | |
511 | int mnt_context_is_nocanonicalize(struct libmnt_context *cxt) | |
512 | { | |
513 | return cxt->flags & MNT_FL_NOCANONICALIZE ? 1 : 0; | |
514 | } | |
515 | ||
516 | ||
517 | /* | |
518 | * Returns 1 if "x-mount.nocanonicalize[=<type>]" userspace mount option is | |
519 | * specified. The optional arguments 'type' should be "source" or "target". | |
520 | */ | |
521 | int mnt_context_is_xnocanonicalize( | |
522 | struct libmnt_context *cxt, | |
523 | const char *type) | |
524 | { | |
525 | struct libmnt_optlist *ol; | |
526 | struct libmnt_opt *opt; | |
527 | const char *arg; | |
528 | ||
529 | assert(cxt); | |
530 | assert(type); | |
531 | ||
532 | ol = mnt_context_get_optlist(cxt); | |
533 | if (!ol) | |
534 | return 0; | |
535 | opt = mnt_optlist_get_named(ol, "X-mount.nocanonicalize", | |
536 | cxt->map_userspace); | |
537 | if (!opt) | |
538 | return 0; | |
539 | arg = mnt_opt_get_value(opt); | |
540 | if (!arg) | |
541 | return 1; | |
542 | return strcmp(arg, type) == 0; | |
543 | } | |
544 | ||
545 | /** | |
546 | * mnt_context_enable_lazy: | |
547 | * @cxt: mount context | |
548 | * @enable: TRUE or FALSE | |
549 | * | |
550 | * Enable/disable lazy umount (see umount(8) man page, option -l). | |
551 | * | |
552 | * Returns: 0 on success, negative number in case of error. | |
553 | */ | |
554 | int mnt_context_enable_lazy(struct libmnt_context *cxt, int enable) | |
555 | { | |
556 | return set_flag(cxt, MNT_FL_LAZY, enable); | |
557 | } | |
558 | ||
559 | /** | |
560 | * mnt_context_is_lazy: | |
561 | * @cxt: mount context | |
562 | * | |
563 | * Returns: 1 if lazy umount is enabled or 0 | |
564 | */ | |
565 | int mnt_context_is_lazy(struct libmnt_context *cxt) | |
566 | { | |
567 | return cxt->flags & MNT_FL_LAZY ? 1 : 0; | |
568 | } | |
569 | ||
570 | /** | |
571 | * mnt_context_enable_onlyonce: | |
572 | * @cxt: mount context | |
573 | * @enable: TRUE or FALSE | |
574 | * | |
575 | * Enable/disable only-once mount (check if FS is not already mounted). | |
576 | * | |
577 | * Returns: 0 on success, negative number in case of error. | |
578 | */ | |
579 | int mnt_context_enable_onlyonce(struct libmnt_context *cxt, int enable) | |
580 | { | |
581 | return set_flag(cxt, MNT_FL_ONLYONCE, enable); | |
582 | } | |
583 | ||
584 | /** | |
585 | * mnt_context_is_onlyonce: | |
586 | * @cxt: mount context | |
587 | * | |
588 | * Returns: 1 if only-once mount is enabled or 0 | |
589 | */ | |
590 | int mnt_context_is_onlyonce(struct libmnt_context *cxt) | |
591 | { | |
592 | return cxt->flags & MNT_FL_ONLYONCE ? 1 : 0; | |
593 | } | |
594 | ||
595 | /** | |
596 | * mnt_context_enable_fork: | |
597 | * @cxt: mount context | |
598 | * @enable: TRUE or FALSE | |
599 | * | |
600 | * Enable/disable fork(2) call in mnt_context_next_mount() (see mount(8) man | |
601 | * page, option -F). | |
602 | * | |
603 | * Returns: 0 on success, negative number in case of error. | |
604 | */ | |
605 | int mnt_context_enable_fork(struct libmnt_context *cxt, int enable) | |
606 | { | |
607 | return set_flag(cxt, MNT_FL_FORK, enable); | |
608 | } | |
609 | ||
610 | /** | |
611 | * mnt_context_is_fork: | |
612 | * @cxt: mount context | |
613 | * | |
614 | * Returns: 1 if fork (mount -F) is enabled or 0 | |
615 | */ | |
616 | int mnt_context_is_fork(struct libmnt_context *cxt) | |
617 | { | |
618 | return cxt->flags & MNT_FL_FORK ? 1 : 0; | |
619 | } | |
620 | ||
621 | /** | |
622 | * mnt_context_is_parent: | |
623 | * @cxt: mount context | |
624 | * | |
625 | * Return: 1 if mount -F enabled and the current context is parent, or 0 | |
626 | */ | |
627 | int mnt_context_is_parent(struct libmnt_context *cxt) | |
628 | { | |
629 | return mnt_context_is_fork(cxt) && cxt->pid == 0; | |
630 | } | |
631 | ||
632 | /** | |
633 | * mnt_context_is_child: | |
634 | * @cxt: mount context | |
635 | * | |
636 | * Return: 1 f the current context is child, or 0 | |
637 | */ | |
638 | int mnt_context_is_child(struct libmnt_context *cxt) | |
639 | { | |
640 | /* See mnt_fork_context(), the for fork flag is always disabled | |
641 | * for children to avoid recursive forking. | |
642 | */ | |
643 | return !mnt_context_is_fork(cxt) && cxt->pid; | |
644 | } | |
645 | ||
646 | /** | |
647 | * mnt_context_enable_rdonly_umount: | |
648 | * @cxt: mount context | |
649 | * @enable: TRUE or FALSE | |
650 | * | |
651 | * Enable/disable read-only remount on failed umount(2) | |
652 | * (see umount(8) man page, option -r). | |
653 | * | |
654 | * Returns: 0 on success, negative number in case of error. | |
655 | */ | |
656 | int mnt_context_enable_rdonly_umount(struct libmnt_context *cxt, int enable) | |
657 | { | |
658 | return set_flag(cxt, MNT_FL_RDONLY_UMOUNT, enable); | |
659 | } | |
660 | ||
661 | /** | |
662 | * mnt_context_is_rdonly_umount | |
663 | * @cxt: mount context | |
664 | * | |
665 | * See also mnt_context_enable_rdonly_umount() and umount(8) man page, | |
666 | * option -r. | |
667 | * | |
668 | * Returns: 1 if read-only remount failed umount(2) is enables or 0 | |
669 | */ | |
670 | int mnt_context_is_rdonly_umount(struct libmnt_context *cxt) | |
671 | { | |
672 | return cxt->flags & MNT_FL_RDONLY_UMOUNT ? 1 : 0; | |
673 | } | |
674 | ||
675 | /** | |
676 | * mnt_context_enable_rwonly_mount: | |
677 | * @cxt: mount context | |
678 | * @enable: TRUE or FALSE | |
679 | * | |
680 | * Force read-write mount; if enabled libmount will never try MS_RDONLY | |
681 | * after failed mount(2) EROFS. (See mount(8) man page, option -w). | |
682 | * | |
683 | * Since: 2.30 | |
684 | * | |
685 | * Returns: 0 on success, negative number in case of error. | |
686 | */ | |
687 | int mnt_context_enable_rwonly_mount(struct libmnt_context *cxt, int enable) | |
688 | { | |
689 | return set_flag(cxt, MNT_FL_RWONLY_MOUNT, enable); | |
690 | } | |
691 | ||
692 | /** | |
693 | * mnt_context_is_rwonly_mount | |
694 | * @cxt: mount context | |
695 | * | |
696 | * See also mnt_context_enable_rwonly_mount() and mount(8) man page, | |
697 | * option -w. | |
698 | * | |
699 | * Since: 2.30 | |
700 | * | |
701 | * Returns: 1 if only read-write mount is allowed. | |
702 | */ | |
703 | int mnt_context_is_rwonly_mount(struct libmnt_context *cxt) | |
704 | { | |
705 | return cxt->flags & MNT_FL_RWONLY_MOUNT ? 1 : 0; | |
706 | } | |
707 | ||
708 | /** | |
709 | * mnt_context_forced_rdonly: | |
710 | * @cxt: mount context | |
711 | * | |
712 | * See also mnt_context_enable_rwonly_mount(). | |
713 | * | |
714 | * Since: 2.30 | |
715 | * | |
716 | * Returns: 1 if mounted read-only on write-protected device. | |
717 | */ | |
718 | int mnt_context_forced_rdonly(struct libmnt_context *cxt) | |
719 | { | |
720 | return cxt->flags & MNT_FL_FORCED_RDONLY ? 1 : 0; | |
721 | } | |
722 | ||
723 | /** | |
724 | * mnt_context_disable_helpers: | |
725 | * @cxt: mount context | |
726 | * @disable: TRUE or FALSE | |
727 | * | |
728 | * Enable/disable /sbin/[u]mount.* helpers (see mount(8) man page, option -i). | |
729 | * | |
730 | * Returns: 0 on success, negative number in case of error. | |
731 | */ | |
732 | int mnt_context_disable_helpers(struct libmnt_context *cxt, int disable) | |
733 | { | |
734 | return set_flag(cxt, MNT_FL_NOHELPERS, disable); | |
735 | } | |
736 | ||
737 | /** | |
738 | * mnt_context_is_nohelpers | |
739 | * @cxt: mount context | |
740 | * | |
741 | * Returns: 1 if helpers are disabled (mount -i) or 0 | |
742 | */ | |
743 | int mnt_context_is_nohelpers(struct libmnt_context *cxt) | |
744 | { | |
745 | return cxt->flags & MNT_FL_NOHELPERS ? 1 : 0; | |
746 | } | |
747 | ||
748 | ||
749 | /** | |
750 | * mnt_context_enable_sloppy: | |
751 | * @cxt: mount context | |
752 | * @enable: TRUE or FALSE | |
753 | * | |
754 | * Set/unset sloppy mounting (see mount(8) man page, option -s). | |
755 | * | |
756 | * Returns: 0 on success, negative number in case of error. | |
757 | */ | |
758 | int mnt_context_enable_sloppy(struct libmnt_context *cxt, int enable) | |
759 | { | |
760 | return set_flag(cxt, MNT_FL_SLOPPY, enable); | |
761 | } | |
762 | ||
763 | /** | |
764 | * mnt_context_is_sloppy: | |
765 | * @cxt: mount context | |
766 | * | |
767 | * Returns: 1 if sloppy flag is enabled or 0 | |
768 | */ | |
769 | int mnt_context_is_sloppy(struct libmnt_context *cxt) | |
770 | { | |
771 | return cxt->flags & MNT_FL_SLOPPY ? 1 : 0; | |
772 | } | |
773 | ||
774 | /** | |
775 | * mnt_context_enable_fake: | |
776 | * @cxt: mount context | |
777 | * @enable: TRUE or FALSE | |
778 | * | |
779 | * Enable/disable fake mounting (see mount(8) man page, option -f). | |
780 | * | |
781 | * Returns: 0 on success, negative number in case of error. | |
782 | */ | |
783 | int mnt_context_enable_fake(struct libmnt_context *cxt, int enable) | |
784 | { | |
785 | return set_flag(cxt, MNT_FL_FAKE, enable); | |
786 | } | |
787 | ||
788 | /** | |
789 | * mnt_context_is_fake: | |
790 | * @cxt: mount context | |
791 | * | |
792 | * Returns: 1 if fake flag is enabled or 0 | |
793 | */ | |
794 | int mnt_context_is_fake(struct libmnt_context *cxt) | |
795 | { | |
796 | return cxt->flags & MNT_FL_FAKE ? 1 : 0; | |
797 | } | |
798 | ||
799 | /** | |
800 | * mnt_context_disable_mtab: | |
801 | * @cxt: mount context | |
802 | * @disable: TRUE or FALSE | |
803 | * | |
804 | * Disable/enable userspace mount table update (see mount(8) man page, | |
805 | * option -n). Originally /etc/mtab, now /run/mount/utab. | |
806 | * | |
807 | * Returns: 0 on success, negative number in case of error. | |
808 | */ | |
809 | int mnt_context_disable_mtab(struct libmnt_context *cxt, int disable) | |
810 | { | |
811 | return set_flag(cxt, MNT_FL_NOMTAB, disable); | |
812 | } | |
813 | ||
814 | /** | |
815 | * mnt_context_is_nomtab: | |
816 | * @cxt: mount context | |
817 | * | |
818 | * Returns: 1 if no-mtab is enabled or 0 | |
819 | */ | |
820 | int mnt_context_is_nomtab(struct libmnt_context *cxt) | |
821 | { | |
822 | return cxt->flags & MNT_FL_NOMTAB ? 1 : 0; | |
823 | } | |
824 | ||
825 | /** | |
826 | * mnt_context_disable_swapmatch: | |
827 | * @cxt: mount context | |
828 | * @disable: TRUE or FALSE | |
829 | * | |
830 | * Disable/enable swap between source and target for mount(8) if only one path | |
831 | * is specified. | |
832 | * | |
833 | * Returns: 0 on success, negative number in case of error. | |
834 | */ | |
835 | int mnt_context_disable_swapmatch(struct libmnt_context *cxt, int disable) | |
836 | { | |
837 | return set_flag(cxt, MNT_FL_NOSWAPMATCH, disable); | |
838 | } | |
839 | ||
840 | /** | |
841 | * mnt_context_is_swapmatch: | |
842 | * @cxt: mount context | |
843 | * | |
844 | * Returns: 1 if swap between source and target is allowed (default is 1) or 0. | |
845 | */ | |
846 | int mnt_context_is_swapmatch(struct libmnt_context *cxt) | |
847 | { | |
848 | return cxt->flags & MNT_FL_NOSWAPMATCH ? 0 : 1; | |
849 | } | |
850 | ||
851 | /** | |
852 | * mnt_context_enable_force: | |
853 | * @cxt: mount context | |
854 | * @enable: TRUE or FALSE | |
855 | * | |
856 | * Enable/disable force umounting (see umount(8) man page, option -f). | |
857 | * | |
858 | * Returns: 0 on success, negative number in case of error. | |
859 | */ | |
860 | int mnt_context_enable_force(struct libmnt_context *cxt, int enable) | |
861 | { | |
862 | return set_flag(cxt, MNT_FL_FORCE, enable); | |
863 | } | |
864 | ||
865 | /** | |
866 | * mnt_context_is_force | |
867 | * @cxt: mount context | |
868 | * | |
869 | * Returns: 1 if force umounting flag is enabled or 0 | |
870 | */ | |
871 | int mnt_context_is_force(struct libmnt_context *cxt) | |
872 | { | |
873 | return cxt->flags & MNT_FL_FORCE ? 1 : 0; | |
874 | } | |
875 | ||
876 | /** | |
877 | * mnt_context_enable_verbose: | |
878 | * @cxt: mount context | |
879 | * @enable: TRUE or FALSE | |
880 | * | |
881 | * Enable/disable verbose output (TODO: not implemented yet) | |
882 | * | |
883 | * Returns: 0 on success, negative number in case of error. | |
884 | */ | |
885 | int mnt_context_enable_verbose(struct libmnt_context *cxt, int enable) | |
886 | { | |
887 | return set_flag(cxt, MNT_FL_VERBOSE, enable); | |
888 | } | |
889 | ||
890 | /** | |
891 | * mnt_context_is_verbose | |
892 | * @cxt: mount context | |
893 | * | |
894 | * Returns: 1 if verbose flag is enabled or 0 | |
895 | */ | |
896 | int mnt_context_is_verbose(struct libmnt_context *cxt) | |
897 | { | |
898 | return cxt->flags & MNT_FL_VERBOSE ? 1 : 0; | |
899 | } | |
900 | ||
901 | /** | |
902 | * mnt_context_enable_loopdel: | |
903 | * @cxt: mount context | |
904 | * @enable: TRUE or FALSE | |
905 | * | |
906 | * Enable/disable the loop delete (destroy) after umount (see umount(8), option -d) | |
907 | * | |
908 | * Returns: 0 on success, negative number in case of error. | |
909 | */ | |
910 | int mnt_context_enable_loopdel(struct libmnt_context *cxt, int enable) | |
911 | { | |
912 | return set_flag(cxt, MNT_FL_LOOPDEL, enable); | |
913 | } | |
914 | ||
915 | /** | |
916 | * mnt_context_is_loopdel: | |
917 | * @cxt: mount context | |
918 | * | |
919 | * Returns: 1 if loop device should be deleted after umount (umount -d) or 0. | |
920 | */ | |
921 | int mnt_context_is_loopdel(struct libmnt_context *cxt) | |
922 | { | |
923 | return cxt->flags & MNT_FL_LOOPDEL ? 1 : 0; | |
924 | } | |
925 | ||
926 | /** | |
927 | * mnt_context_set_fs: | |
928 | * @cxt: mount context | |
929 | * @fs: filesystem description | |
930 | * | |
931 | * The mount context uses private @fs by default. This function can be used to | |
932 | * overwrite the private @fs with an external instance. This function | |
933 | * increments @fs reference counter (and decrement reference counter of the | |
934 | * old fs). | |
935 | * | |
936 | * The @fs will be modified by mnt_context_set_{source,target,options,fstype} | |
937 | * functions, If the @fs is NULL, then all current FS specific settings (source, | |
938 | * target, etc., exclude spec) are reset. | |
939 | * | |
940 | * Returns: 0 on success, negative number in case of error. | |
941 | */ | |
942 | int mnt_context_set_fs(struct libmnt_context *cxt, struct libmnt_fs *fs) | |
943 | { | |
944 | if (!cxt) | |
945 | return -EINVAL; | |
946 | ||
947 | if (cxt->fs == fs) | |
948 | return 0; | |
949 | ||
950 | DBG(CXT, ul_debugobj(cxt, "setting new FS")); | |
951 | ||
952 | /* new */ | |
953 | if (fs) { | |
954 | struct libmnt_optlist *ol = mnt_context_get_optlist(cxt); | |
955 | ||
956 | if (!ol) | |
957 | return -ENOMEM; | |
958 | ||
959 | mnt_ref_fs(fs); | |
960 | ||
961 | mnt_optlist_set_optstr(ol, mnt_fs_get_options(fs), NULL); | |
962 | mnt_fs_follow_optlist(fs, ol); | |
963 | } | |
964 | ||
965 | /* old */ | |
966 | if (cxt->fs) | |
967 | mnt_fs_follow_optlist(cxt->fs, NULL); | |
968 | mnt_unref_fs(cxt->fs); | |
969 | ||
970 | cxt->fs = fs; | |
971 | return 0; | |
972 | } | |
973 | ||
974 | /** | |
975 | * mnt_context_get_fs: | |
976 | * @cxt: mount context | |
977 | * | |
978 | * The FS contains the basic description of mountpoint, fs type and so on. | |
979 | * Note that the FS is modified by mnt_context_set_{source,target,options,fstype} | |
980 | * functions. | |
981 | * | |
982 | * Returns: pointer to FS description or NULL in case of a calloc() error. | |
983 | */ | |
984 | struct libmnt_fs *mnt_context_get_fs(struct libmnt_context *cxt) | |
985 | { | |
986 | if (!cxt) | |
987 | return NULL; | |
988 | if (!cxt->fs) { | |
989 | struct libmnt_optlist *ol = mnt_context_get_optlist(cxt); | |
990 | ||
991 | if (!ol) | |
992 | return NULL; | |
993 | cxt->fs = mnt_new_fs(); | |
994 | if (!cxt->fs) | |
995 | return NULL; | |
996 | ||
997 | mnt_fs_follow_optlist(cxt->fs, ol); | |
998 | } | |
999 | return cxt->fs; | |
1000 | } | |
1001 | ||
1002 | /** | |
1003 | * mnt_context_get_fs_userdata: | |
1004 | * @cxt: mount context | |
1005 | * | |
1006 | * Returns: pointer to userdata or NULL. | |
1007 | */ | |
1008 | void *mnt_context_get_fs_userdata(struct libmnt_context *cxt) | |
1009 | { | |
1010 | return cxt->fs ? mnt_fs_get_userdata(cxt->fs) : NULL; | |
1011 | } | |
1012 | ||
1013 | /** | |
1014 | * mnt_context_get_fstab_userdata: | |
1015 | * @cxt: mount context | |
1016 | * | |
1017 | * Returns: pointer to userdata or NULL. | |
1018 | */ | |
1019 | void *mnt_context_get_fstab_userdata(struct libmnt_context *cxt) | |
1020 | { | |
1021 | return cxt->fstab ? mnt_table_get_userdata(cxt->fstab) : NULL; | |
1022 | } | |
1023 | ||
1024 | /** | |
1025 | * mnt_context_get_mtab_userdata: | |
1026 | * @cxt: mount context | |
1027 | * | |
1028 | * The file /etc/mtab is no more used, @context points always to mountinfo | |
1029 | * (/proc/self/mountinfo). The function uses "mtab" in the name for backward | |
1030 | * compatibility only. | |
1031 | * | |
1032 | * Returns: pointer to userdata or NULL. | |
1033 | */ | |
1034 | void *mnt_context_get_mtab_userdata(struct libmnt_context *cxt) | |
1035 | { | |
1036 | return cxt->mountinfo ? mnt_table_get_userdata(cxt->mountinfo) : NULL; | |
1037 | } | |
1038 | ||
1039 | /** | |
1040 | * mnt_context_set_source: | |
1041 | * @cxt: mount context | |
1042 | * @source: mount source (device, directory, UUID, LABEL, ...) | |
1043 | * | |
1044 | * Note that libmount does not interpret "nofail" (MNT_MS_NOFAIL) | |
1045 | * mount option. The real return code is always returned, when | |
1046 | * the device does not exist then it's usually MNT_ERR_NOSOURCE | |
1047 | * from libmount or ENOENT, ENOTDIR, ENOTBLK, ENXIO from mount(2). | |
1048 | * | |
1049 | * Returns: 0 on success, negative number in case of error. | |
1050 | */ | |
1051 | int mnt_context_set_source(struct libmnt_context *cxt, const char *source) | |
1052 | { | |
1053 | return mnt_fs_set_source(mnt_context_get_fs(cxt), source); | |
1054 | } | |
1055 | ||
1056 | /** | |
1057 | * mnt_context_get_source: | |
1058 | * @cxt: mount context | |
1059 | * | |
1060 | * Returns: returns pointer or NULL in case of error or if not set. | |
1061 | */ | |
1062 | const char *mnt_context_get_source(struct libmnt_context *cxt) | |
1063 | { | |
1064 | return mnt_fs_get_source(mnt_context_get_fs(cxt)); | |
1065 | } | |
1066 | ||
1067 | /** | |
1068 | * mnt_context_set_target: | |
1069 | * @cxt: mount context | |
1070 | * @target: mountpoint | |
1071 | * | |
1072 | * Returns: 0 on success, negative number in case of error. | |
1073 | */ | |
1074 | int mnt_context_set_target(struct libmnt_context *cxt, const char *target) | |
1075 | { | |
1076 | return mnt_fs_set_target(mnt_context_get_fs(cxt), target); | |
1077 | } | |
1078 | ||
1079 | /** | |
1080 | * mnt_context_get_target: | |
1081 | * @cxt: mount context | |
1082 | * | |
1083 | * Returns: returns pointer or NULL in case of error or if not set. | |
1084 | */ | |
1085 | const char *mnt_context_get_target(struct libmnt_context *cxt) | |
1086 | { | |
1087 | return mnt_fs_get_target(mnt_context_get_fs(cxt)); | |
1088 | } | |
1089 | ||
1090 | /** | |
1091 | * mnt_context_set_target_prefix: | |
1092 | * @cxt: mount context | |
1093 | * @path: mountpoint prefix | |
1094 | * | |
1095 | * Returns: 0 on success, negative number in case of error. | |
1096 | */ | |
1097 | int mnt_context_set_target_prefix(struct libmnt_context *cxt, const char *path) | |
1098 | { | |
1099 | char *p = NULL; | |
1100 | ||
1101 | if (!cxt) | |
1102 | return -EINVAL; | |
1103 | if (path) { | |
1104 | p = strdup(path); | |
1105 | if (!p) | |
1106 | return -ENOMEM; | |
1107 | } | |
1108 | free(cxt->tgt_prefix); | |
1109 | cxt->tgt_prefix = p; | |
1110 | ||
1111 | return 0; | |
1112 | } | |
1113 | ||
1114 | /** | |
1115 | * mnt_context_get_target_prefix: | |
1116 | * @cxt: mount context | |
1117 | * | |
1118 | * Returns: returns pointer or NULL in case of error or if not set. | |
1119 | */ | |
1120 | const char *mnt_context_get_target_prefix(struct libmnt_context *cxt) | |
1121 | { | |
1122 | return cxt ? cxt->tgt_prefix : NULL; | |
1123 | } | |
1124 | ||
1125 | ||
1126 | /** | |
1127 | * mnt_context_set_fstype: | |
1128 | * @cxt: mount context | |
1129 | * @fstype: filesystem type | |
1130 | * | |
1131 | * Note that the @fstype has to be a FS type. For patterns with | |
1132 | * comma-separated list of filesystems or for the "nofs" notation, use | |
1133 | * mnt_context_set_fstype_pattern(). | |
1134 | * | |
1135 | * Returns: 0 on success, negative number in case of error. | |
1136 | */ | |
1137 | int mnt_context_set_fstype(struct libmnt_context *cxt, const char *fstype) | |
1138 | { | |
1139 | return mnt_fs_set_fstype(mnt_context_get_fs(cxt), fstype); | |
1140 | } | |
1141 | ||
1142 | /** | |
1143 | * mnt_context_get_fstype: | |
1144 | * @cxt: mount context | |
1145 | * | |
1146 | * Returns: pointer or NULL in case of error or if not set. | |
1147 | */ | |
1148 | const char *mnt_context_get_fstype(struct libmnt_context *cxt) | |
1149 | { | |
1150 | return mnt_fs_get_fstype(mnt_context_get_fs(cxt)); | |
1151 | } | |
1152 | ||
1153 | struct libmnt_optlist *mnt_context_get_optlist(struct libmnt_context *cxt) | |
1154 | { | |
1155 | if (!cxt) | |
1156 | return NULL; | |
1157 | if (!cxt->optlist) { | |
1158 | cxt->optlist = mnt_new_optlist(); | |
1159 | if (!cxt->optlist) | |
1160 | return NULL; | |
1161 | if (mnt_optlist_register_map(cxt->optlist, cxt->map_linux)) | |
1162 | goto fail; | |
1163 | if (mnt_optlist_register_map(cxt->optlist, cxt->map_userspace)) | |
1164 | goto fail; | |
1165 | } | |
1166 | ||
1167 | return cxt->optlist; | |
1168 | fail: | |
1169 | mnt_unref_optlist(cxt->optlist); | |
1170 | return NULL; | |
1171 | } | |
1172 | ||
1173 | /** | |
1174 | * mnt_context_set_options: | |
1175 | * @cxt: mount context | |
1176 | * @optstr: comma delimited mount options | |
1177 | * | |
1178 | * Please note that MS_MOVE cannot be specified as a "string". The move operation | |
1179 | * cannot be specified in fstab. | |
1180 | * | |
1181 | * Returns: 0 on success, negative number in case of error. | |
1182 | */ | |
1183 | int mnt_context_set_options(struct libmnt_context *cxt, const char *optstr) | |
1184 | { | |
1185 | struct libmnt_optlist *ls = mnt_context_get_optlist(cxt); | |
1186 | ||
1187 | if (!ls) | |
1188 | return -ENOMEM; | |
1189 | return mnt_optlist_set_optstr(ls, optstr, NULL); | |
1190 | } | |
1191 | ||
1192 | /** | |
1193 | * mnt_context_append_options: | |
1194 | * @cxt: mount context | |
1195 | * @optstr: comma delimited mount options | |
1196 | * | |
1197 | * Returns: 0 on success, negative number in case of error. | |
1198 | */ | |
1199 | int mnt_context_append_options(struct libmnt_context *cxt, const char *optstr) | |
1200 | { | |
1201 | struct libmnt_optlist *ls = mnt_context_get_optlist(cxt); | |
1202 | ||
1203 | if (!ls) | |
1204 | return -ENOMEM; | |
1205 | return mnt_optlist_append_optstr(ls, optstr, NULL); | |
1206 | } | |
1207 | ||
1208 | /** | |
1209 | * mnt_context_get_options: | |
1210 | * @cxt: mount context | |
1211 | * | |
1212 | * This function returns mount options set by mnt_context_set_options(), | |
1213 | * mnt_context_append_options() or mnt_context_set_mflags(); | |
1214 | * | |
1215 | * Before v2.39 this function ignored options specified by flags (see | |
1216 | * mnt_context_set_mflags()) before mnt_context_prepare_mount() call. Now this | |
1217 | * function always returns all mount options. | |
1218 | * | |
1219 | * Returns: pointer or NULL | |
1220 | */ | |
1221 | const char *mnt_context_get_options(struct libmnt_context *cxt) | |
1222 | { | |
1223 | const char *str = NULL; | |
1224 | ||
1225 | if (cxt->optlist && !mnt_optlist_is_empty(cxt->optlist)) | |
1226 | mnt_optlist_get_optstr(cxt->optlist, &str, NULL, 0); | |
1227 | return str; | |
1228 | } | |
1229 | ||
1230 | /** | |
1231 | * mnt_context_set_fstype_pattern: | |
1232 | * @cxt: mount context | |
1233 | * @pattern: FS name pattern (or NULL to reset the current setting) | |
1234 | * | |
1235 | * See mount(8), option -t. | |
1236 | * | |
1237 | * Returns: 0 on success, negative number in case of error. | |
1238 | */ | |
1239 | int mnt_context_set_fstype_pattern(struct libmnt_context *cxt, const char *pattern) | |
1240 | { | |
1241 | char *p = NULL; | |
1242 | ||
1243 | if (!cxt) | |
1244 | return -EINVAL; | |
1245 | if (pattern) { | |
1246 | p = strdup(pattern); | |
1247 | if (!p) | |
1248 | return -ENOMEM; | |
1249 | } | |
1250 | free(cxt->fstype_pattern); | |
1251 | cxt->fstype_pattern = p; | |
1252 | return 0; | |
1253 | } | |
1254 | ||
1255 | /** | |
1256 | * mnt_context_set_options_pattern: | |
1257 | * @cxt: mount context | |
1258 | * @pattern: options pattern (or NULL to reset the current setting) | |
1259 | * | |
1260 | * See mount(8), option -O. | |
1261 | * | |
1262 | * Returns: 0 on success, negative number in case of error. | |
1263 | */ | |
1264 | int mnt_context_set_options_pattern(struct libmnt_context *cxt, const char *pattern) | |
1265 | { | |
1266 | char *p = NULL; | |
1267 | ||
1268 | if (!cxt) | |
1269 | return -EINVAL; | |
1270 | if (pattern) { | |
1271 | p = strdup(pattern); | |
1272 | if (!p) | |
1273 | return -ENOMEM; | |
1274 | } | |
1275 | free(cxt->optstr_pattern); | |
1276 | cxt->optstr_pattern = p; | |
1277 | return 0; | |
1278 | } | |
1279 | ||
1280 | /** | |
1281 | * mnt_context_set_fstab: | |
1282 | * @cxt: mount context | |
1283 | * @tb: fstab | |
1284 | * | |
1285 | * The mount context reads /etc/fstab to the private struct libmnt_table by default. | |
1286 | * This function can be used to overwrite the private fstab with an external | |
1287 | * instance. | |
1288 | * | |
1289 | * This function modify the @tb reference counter. This function does not set | |
1290 | * the cache for the @tb. You have to explicitly call mnt_table_set_cache(tb, | |
1291 | * mnt_context_get_cache(cxt)); | |
1292 | * | |
1293 | * The fstab is used read-only and is not modified, it should be possible to | |
1294 | * share the fstab between more mount contexts (TODO: test it.) | |
1295 | * | |
1296 | * If the @tb argument is NULL, then the current private fstab instance is | |
1297 | * reset. | |
1298 | * | |
1299 | * Returns: 0 on success, negative number in case of error. | |
1300 | */ | |
1301 | int mnt_context_set_fstab(struct libmnt_context *cxt, struct libmnt_table *tb) | |
1302 | { | |
1303 | if (!cxt) | |
1304 | return -EINVAL; | |
1305 | ||
1306 | mnt_ref_table(tb); /* new */ | |
1307 | mnt_unref_table(cxt->fstab); /* old */ | |
1308 | ||
1309 | cxt->fstab = tb; | |
1310 | return 0; | |
1311 | } | |
1312 | ||
1313 | /** | |
1314 | * mnt_context_get_fstab: | |
1315 | * @cxt: mount context | |
1316 | * @tb: returns fstab | |
1317 | * | |
1318 | * See also mnt_table_parse_fstab() for more details about fstab. | |
1319 | * | |
1320 | * Returns: 0 on success, negative number in case of error. | |
1321 | */ | |
1322 | int mnt_context_get_fstab(struct libmnt_context *cxt, struct libmnt_table **tb) | |
1323 | { | |
1324 | struct libmnt_ns *ns_old; | |
1325 | ||
1326 | if (!cxt) | |
1327 | return -EINVAL; | |
1328 | if (!cxt->fstab) { | |
1329 | int rc; | |
1330 | ||
1331 | cxt->fstab = mnt_new_table(); | |
1332 | if (!cxt->fstab) | |
1333 | return -ENOMEM; | |
1334 | if (cxt->table_errcb) | |
1335 | mnt_table_set_parser_errcb(cxt->fstab, cxt->table_errcb); | |
1336 | ||
1337 | ns_old = mnt_context_switch_target_ns(cxt); | |
1338 | if (!ns_old) | |
1339 | return -MNT_ERR_NAMESPACE; | |
1340 | ||
1341 | mnt_table_set_cache(cxt->fstab, mnt_context_get_cache(cxt)); | |
1342 | rc = mnt_table_parse_fstab(cxt->fstab, NULL); | |
1343 | ||
1344 | if (!mnt_context_switch_ns(cxt, ns_old)) | |
1345 | return -MNT_ERR_NAMESPACE; | |
1346 | ||
1347 | if (rc) | |
1348 | return rc; | |
1349 | } | |
1350 | ||
1351 | if (tb) | |
1352 | *tb = cxt->fstab; | |
1353 | return 0; | |
1354 | } | |
1355 | ||
1356 | int mnt_context_get_mountinfo(struct libmnt_context *cxt, struct libmnt_table **tb) | |
1357 | { | |
1358 | int rc = 0; | |
1359 | struct libmnt_ns *ns_old = NULL; | |
1360 | ||
1361 | if (!cxt) | |
1362 | return -EINVAL; | |
1363 | if (!cxt->mountinfo) { | |
1364 | ns_old = mnt_context_switch_target_ns(cxt); | |
1365 | if (!ns_old) | |
1366 | return -MNT_ERR_NAMESPACE; | |
1367 | ||
1368 | context_init_paths(cxt, 0); | |
1369 | ||
1370 | cxt->mountinfo = mnt_new_table(); | |
1371 | if (!cxt->mountinfo) { | |
1372 | rc = -ENOMEM; | |
1373 | goto end; | |
1374 | } | |
1375 | ||
1376 | mnt_table_enable_noautofs(cxt->mountinfo, cxt->noautofs); | |
1377 | ||
1378 | if (cxt->table_errcb) | |
1379 | mnt_table_set_parser_errcb(cxt->mountinfo, cxt->table_errcb); | |
1380 | if (cxt->table_fltrcb) | |
1381 | mnt_table_set_parser_fltrcb(cxt->mountinfo, | |
1382 | cxt->table_fltrcb, | |
1383 | cxt->table_fltrcb_data); | |
1384 | ||
1385 | mnt_table_set_cache(cxt->mountinfo, mnt_context_get_cache(cxt)); | |
1386 | } | |
1387 | ||
1388 | /* Read the table; it's empty, because this first mnt_context_get_mountinfo() | |
1389 | * call, or because /proc was not accessible in previous calls */ | |
1390 | if (mnt_table_is_empty(cxt->mountinfo)) { | |
1391 | if (!ns_old) { | |
1392 | ns_old = mnt_context_switch_target_ns(cxt); | |
1393 | if (!ns_old) | |
1394 | return -MNT_ERR_NAMESPACE; | |
1395 | } | |
1396 | ||
1397 | rc = __mnt_table_parse_mountinfo(cxt->mountinfo, NULL, cxt->utab); | |
1398 | if (rc) | |
1399 | goto end; | |
1400 | } | |
1401 | ||
1402 | if (tb) | |
1403 | *tb = cxt->mountinfo; | |
1404 | ||
1405 | DBG(CXT, ul_debugobj(cxt, "mountinfo requested [nents=%d]", | |
1406 | mnt_table_get_nents(cxt->mountinfo))); | |
1407 | ||
1408 | end: | |
1409 | if (ns_old && !mnt_context_switch_ns(cxt, ns_old)) | |
1410 | return -MNT_ERR_NAMESPACE; | |
1411 | ||
1412 | return rc; | |
1413 | } | |
1414 | ||
1415 | /** | |
1416 | * mnt_context_get_mtab: | |
1417 | * @cxt: mount context | |
1418 | * @tb: returns mtab | |
1419 | * | |
1420 | * Parse /proc/self/mountinfo mount table. | |
1421 | * | |
1422 | * The file /etc/mtab is no more used, @context points always to mountinfo | |
1423 | * (/proc/self/mountinfo). The function uses "mtab" in the name for backward | |
1424 | * compatibility only. | |
1425 | * | |
1426 | * See also mnt_table_parse_mtab() for more details about mountinfo. The | |
1427 | * result will be deallocated by mnt_free_context(@cxt). | |
1428 | * | |
1429 | * Returns: 0 on success, negative number in case of error. | |
1430 | */ | |
1431 | int mnt_context_get_mtab(struct libmnt_context *cxt, struct libmnt_table **tb) | |
1432 | { | |
1433 | return mnt_context_get_mountinfo(cxt, tb); | |
1434 | } | |
1435 | ||
1436 | /* | |
1437 | * Called by mountinfo parser to filter out entries, non-zero means that | |
1438 | * an entry has to be filtered out. | |
1439 | */ | |
1440 | static int mountinfo_filter(struct libmnt_fs *fs, void *data) | |
1441 | { | |
1442 | if (!fs || !data) | |
1443 | return 0; | |
1444 | if (mnt_fs_streq_target(fs, data)) | |
1445 | return 0; | |
1446 | if (mnt_fs_streq_srcpath(fs, data)) | |
1447 | return 0; | |
1448 | return 1; | |
1449 | } | |
1450 | ||
1451 | /* | |
1452 | * The same like mnt_context_get_mountinfo(), but does not read all mountinfo | |
1453 | * file, but only entries relevant for @tgt. | |
1454 | */ | |
1455 | int mnt_context_get_mountinfo_for_target(struct libmnt_context *cxt, | |
1456 | struct libmnt_table **mountinfo, | |
1457 | const char *tgt) | |
1458 | { | |
1459 | struct stat st; | |
1460 | struct libmnt_cache *cache = NULL; | |
1461 | char *cn_tgt = NULL; | |
1462 | int rc; | |
1463 | struct libmnt_ns *ns_old; | |
1464 | ||
1465 | ns_old = mnt_context_switch_target_ns(cxt); | |
1466 | if (!ns_old) | |
1467 | return -MNT_ERR_NAMESPACE; | |
1468 | ||
1469 | if (mnt_context_is_nocanonicalize(cxt)) | |
1470 | mnt_context_set_tabfilter(cxt, mountinfo_filter, (void *) tgt); | |
1471 | ||
1472 | else if (mnt_safe_stat(tgt, &st) == 0 && S_ISDIR(st.st_mode)) { | |
1473 | cache = mnt_context_get_cache(cxt); | |
1474 | cn_tgt = mnt_resolve_path(tgt, cache); | |
1475 | if (cn_tgt) | |
1476 | mnt_context_set_tabfilter(cxt, mountinfo_filter, cn_tgt); | |
1477 | } | |
1478 | ||
1479 | rc = mnt_context_get_mountinfo(cxt, mountinfo); | |
1480 | mnt_context_set_tabfilter(cxt, NULL, NULL); | |
1481 | ||
1482 | if (!mnt_context_switch_ns(cxt, ns_old)) | |
1483 | return -MNT_ERR_NAMESPACE; | |
1484 | ||
1485 | if (cn_tgt && !cache) | |
1486 | free(cn_tgt); | |
1487 | ||
1488 | return rc; | |
1489 | } | |
1490 | ||
1491 | /* | |
1492 | * Allows to specify a filter for tab file entries. The filter is called by | |
1493 | * the table parser. Currently used for utab only. | |
1494 | */ | |
1495 | int mnt_context_set_tabfilter(struct libmnt_context *cxt, | |
1496 | int (*fltr)(struct libmnt_fs *, void *), | |
1497 | void *data) | |
1498 | { | |
1499 | if (!cxt) | |
1500 | return -EINVAL; | |
1501 | ||
1502 | cxt->table_fltrcb = fltr; | |
1503 | cxt->table_fltrcb_data = data; | |
1504 | ||
1505 | if (cxt->mountinfo) | |
1506 | mnt_table_set_parser_fltrcb(cxt->mountinfo, | |
1507 | cxt->table_fltrcb, | |
1508 | cxt->table_fltrcb_data); | |
1509 | ||
1510 | DBG(CXT, ul_debugobj(cxt, "tabfilter %s", fltr ? "ENABLED!" : "disabled")); | |
1511 | return 0; | |
1512 | } | |
1513 | ||
1514 | /** | |
1515 | * mnt_context_get_table: | |
1516 | * @cxt: mount context | |
1517 | * @filename: e.g. /proc/self/mountinfo | |
1518 | * @tb: returns the table | |
1519 | * | |
1520 | * This function allocates a new table and parses the @file. The parser error | |
1521 | * callback and cache for tags and paths is set according to the @cxt setting. | |
1522 | * See also mnt_table_parse_file(). | |
1523 | * | |
1524 | * It's strongly recommended to use the mnt_context_get_mtab() and | |
1525 | * mnt_context_get_fstab() functions for mountinfo and fstab files. This function | |
1526 | * does not care about LIBMOUNT_* env.variables and does not merge userspace | |
1527 | * options. | |
1528 | * | |
1529 | * The result will NOT be deallocated by mnt_free_context(@cxt). | |
1530 | * | |
1531 | * Returns: 0 on success, negative number in case of error. | |
1532 | */ | |
1533 | int mnt_context_get_table(struct libmnt_context *cxt, | |
1534 | const char *filename, struct libmnt_table **tb) | |
1535 | { | |
1536 | int rc; | |
1537 | struct libmnt_ns *ns_old; | |
1538 | ||
1539 | if (!cxt || !tb) | |
1540 | return -EINVAL; | |
1541 | ||
1542 | *tb = mnt_new_table(); | |
1543 | if (!*tb) | |
1544 | return -ENOMEM; | |
1545 | ||
1546 | if (cxt->table_errcb) | |
1547 | mnt_table_set_parser_errcb(*tb, cxt->table_errcb); | |
1548 | ||
1549 | ns_old = mnt_context_switch_target_ns(cxt); | |
1550 | if (!ns_old) | |
1551 | return -MNT_ERR_NAMESPACE; | |
1552 | ||
1553 | rc = mnt_table_parse_file(*tb, filename); | |
1554 | ||
1555 | if (rc) { | |
1556 | mnt_unref_table(*tb); | |
1557 | goto end; | |
1558 | } | |
1559 | ||
1560 | mnt_table_set_cache(*tb, mnt_context_get_cache(cxt)); | |
1561 | ||
1562 | end: | |
1563 | if (!mnt_context_switch_ns(cxt, ns_old)) | |
1564 | return -MNT_ERR_NAMESPACE; | |
1565 | ||
1566 | return rc; | |
1567 | } | |
1568 | ||
1569 | /** | |
1570 | * mnt_context_set_tables_errcb | |
1571 | * @cxt: mount context | |
1572 | * @cb: pointer to callback function | |
1573 | * | |
1574 | * The error callback is used for all tab files (e.g. mountinfo, fstab) | |
1575 | * parsed within the context. | |
1576 | * | |
1577 | * See also mnt_context_get_mtab(), | |
1578 | * mnt_context_get_fstab(), | |
1579 | * mnt_table_set_parser_errcb(). | |
1580 | * | |
1581 | * Returns: 0 on success, negative number in case of error. | |
1582 | */ | |
1583 | int mnt_context_set_tables_errcb(struct libmnt_context *cxt, | |
1584 | int (*cb)(struct libmnt_table *tb, const char *filename, int line)) | |
1585 | { | |
1586 | if (!cxt) | |
1587 | return -EINVAL; | |
1588 | ||
1589 | if (cxt->mountinfo) | |
1590 | mnt_table_set_parser_errcb(cxt->mountinfo, cb); | |
1591 | if (cxt->fstab) | |
1592 | mnt_table_set_parser_errcb(cxt->fstab, cb); | |
1593 | ||
1594 | cxt->table_errcb = cb; | |
1595 | return 0; | |
1596 | } | |
1597 | ||
1598 | /** | |
1599 | * mnt_context_set_cache: | |
1600 | * @cxt: mount context | |
1601 | * @cache: cache instance or NULL | |
1602 | * | |
1603 | * The mount context maintains a private struct libmnt_cache by default. This | |
1604 | * function can be used to overwrite the private cache with an external instance. | |
1605 | * This function increments cache reference counter. | |
1606 | * | |
1607 | * If the @cache argument is NULL, then the current cache instance is reset. | |
1608 | * This function apply the cache to fstab and mountinfo instances (if already | |
1609 | * exists). | |
1610 | * | |
1611 | * The old cache instance reference counter is de-incremented. | |
1612 | * | |
1613 | * Returns: 0 on success, negative number in case of error. | |
1614 | */ | |
1615 | int mnt_context_set_cache(struct libmnt_context *cxt, struct libmnt_cache *cache) | |
1616 | { | |
1617 | if (!cxt) | |
1618 | return -EINVAL; | |
1619 | ||
1620 | mnt_ref_cache(cache); /* new */ | |
1621 | mnt_unref_cache(cxt->cache); /* old */ | |
1622 | ||
1623 | cxt->cache = cache; | |
1624 | ||
1625 | if (cxt->mountinfo) | |
1626 | mnt_table_set_cache(cxt->mountinfo, cache); | |
1627 | if (cxt->fstab) | |
1628 | mnt_table_set_cache(cxt->fstab, cache); | |
1629 | ||
1630 | return 0; | |
1631 | } | |
1632 | ||
1633 | /** | |
1634 | * mnt_context_get_cache | |
1635 | * @cxt: mount context | |
1636 | * | |
1637 | * See also mnt_context_set_cache(). | |
1638 | * | |
1639 | * Returns: pointer to cache or NULL if canonicalization is disabled. | |
1640 | */ | |
1641 | struct libmnt_cache *mnt_context_get_cache(struct libmnt_context *cxt) | |
1642 | { | |
1643 | if (!cxt || mnt_context_is_nocanonicalize(cxt)) | |
1644 | return NULL; | |
1645 | ||
1646 | if (!cxt->cache) { | |
1647 | struct libmnt_cache *cache = mnt_new_cache(); | |
1648 | mnt_context_set_cache(cxt, cache); | |
1649 | mnt_unref_cache(cache); | |
1650 | } | |
1651 | return cxt->cache; | |
1652 | } | |
1653 | ||
1654 | /** | |
1655 | * mnt_context_set_passwd_cb: | |
1656 | * @cxt: mount context | |
1657 | * @get: callback to get password | |
1658 | * @release: callback to release (deallocate) password | |
1659 | * | |
1660 | * Sets callbacks for encryption password (e.g encrypted loopdev). This | |
1661 | * function is deprecated (encrypted loops are no longer supported). | |
1662 | * | |
1663 | * Returns: 0 on success, negative number in case of error. | |
1664 | */ | |
1665 | int mnt_context_set_passwd_cb(struct libmnt_context *cxt, | |
1666 | char *(*get)(struct libmnt_context *), | |
1667 | void (*release)(struct libmnt_context *, char *)) | |
1668 | { | |
1669 | if (!cxt) | |
1670 | return -EINVAL; | |
1671 | cxt->pwd_get_cb = get; | |
1672 | cxt->pwd_release_cb = release; | |
1673 | return 0; | |
1674 | } | |
1675 | ||
1676 | /** | |
1677 | * mnt_context_get_lock: | |
1678 | * @cxt: mount context | |
1679 | * | |
1680 | * The libmount applications don't have to care about utab locking, but with a | |
1681 | * small exception: the application has to be able to remove the lock file when | |
1682 | * interrupted by signal or signals have to be ignored when the lock is locked. | |
1683 | * | |
1684 | * The default behavior is to ignore all signals (except SIGALRM and | |
1685 | * SIGTRAP for utab update) when the lock is locked. If this behavior | |
1686 | * is unacceptable, then use: | |
1687 | * | |
1688 | * lc = mnt_context_get_lock(cxt); | |
1689 | * if (lc) | |
1690 | * mnt_lock_block_signals(lc, FALSE); | |
1691 | * | |
1692 | * and don't forget to call mnt_unlock_file(lc) before exit. | |
1693 | * | |
1694 | * Returns: pointer to lock struct or NULL. | |
1695 | */ | |
1696 | struct libmnt_lock *mnt_context_get_lock(struct libmnt_context *cxt) | |
1697 | { | |
1698 | /* | |
1699 | * DON'T call this function within libmount, it will always allocate | |
1700 | * the lock. The mnt_update_* functions are able to allocate the lock | |
1701 | * only when utab update is really necessary. | |
1702 | */ | |
1703 | if (!cxt || mnt_context_is_nomtab(cxt)) | |
1704 | return NULL; | |
1705 | ||
1706 | if (!cxt->lock) { | |
1707 | cxt->lock = mnt_new_lock( | |
1708 | mnt_context_get_writable_tabpath(cxt), 0); | |
1709 | if (cxt->lock) | |
1710 | mnt_lock_block_signals(cxt->lock, TRUE); | |
1711 | } | |
1712 | return cxt->lock; | |
1713 | } | |
1714 | ||
1715 | /** | |
1716 | * mnt_context_set_mflags: | |
1717 | * @cxt: mount context | |
1718 | * @flags: mount(2) flags (MS_* flags) | |
1719 | * | |
1720 | * Sets mount flags (see mount(2) man page). | |
1721 | * | |
1722 | * Note that order of mount options (strings) and flags matter if you mix | |
1723 | * mnt_context_append_options() and mnt_context_set_mflags(). | |
1724 | * | |
1725 | * Be careful if use MS_REC flag -- this is flags is generic for | |
1726 | * all mask. In this case is better to use options string where | |
1727 | * mount options are independent and nothing is applied to all options. | |
1728 | * | |
1729 | * Returns: 0 on success, negative number in case of error. | |
1730 | */ | |
1731 | int mnt_context_set_mflags(struct libmnt_context *cxt, unsigned long flags) | |
1732 | { | |
1733 | struct libmnt_optlist *ls = mnt_context_get_optlist(cxt); | |
1734 | ||
1735 | if (!ls) | |
1736 | return -ENOMEM; | |
1737 | ||
1738 | return mnt_optlist_set_flags(ls, flags, cxt->map_linux); | |
1739 | } | |
1740 | ||
1741 | /** | |
1742 | * mnt_context_get_mflags: | |
1743 | * @cxt: mount context | |
1744 | * @flags: returns MS_* mount flags | |
1745 | * | |
1746 | * Converts mount options string to MS_* flags and bitwise-OR the result with | |
1747 | * the already defined flags (see mnt_context_set_mflags()). | |
1748 | * | |
1749 | * Returns: 0 on success, negative number in case of error. | |
1750 | */ | |
1751 | int mnt_context_get_mflags(struct libmnt_context *cxt, unsigned long *flags) | |
1752 | { | |
1753 | struct libmnt_optlist *ls = mnt_context_get_optlist(cxt); | |
1754 | ||
1755 | if (!ls) | |
1756 | return -ENOMEM; | |
1757 | ||
1758 | return mnt_optlist_get_flags(ls, flags, cxt->map_linux, 0); | |
1759 | } | |
1760 | ||
1761 | /** | |
1762 | * mnt_context_set_user_mflags: | |
1763 | * @cxt: mount context | |
1764 | * @flags: mount(2) flags (MNT_MS_* flags, e.g. MNT_MS_LOOP) | |
1765 | * | |
1766 | * Sets userspace mount flags. | |
1767 | * | |
1768 | * See also notes for mnt_context_set_mflags(). | |
1769 | * | |
1770 | * Returns: 0 on success, negative number in case of error. | |
1771 | */ | |
1772 | int mnt_context_set_user_mflags(struct libmnt_context *cxt, unsigned long flags) | |
1773 | { | |
1774 | struct libmnt_optlist *ls = mnt_context_get_optlist(cxt); | |
1775 | ||
1776 | if (!ls) | |
1777 | return -ENOMEM; | |
1778 | ||
1779 | return mnt_optlist_set_flags(ls, flags, cxt->map_userspace); | |
1780 | } | |
1781 | ||
1782 | /** | |
1783 | * mnt_context_get_user_mflags: | |
1784 | * @cxt: mount context | |
1785 | * @flags: returns mount flags | |
1786 | * | |
1787 | * Converts mount options string to MNT_MS_* flags and bitwise-OR the result | |
1788 | * with the already defined flags (see mnt_context_set_user_mflags()). | |
1789 | * | |
1790 | * Returns: 0 on success, negative number in case of error. | |
1791 | */ | |
1792 | int mnt_context_get_user_mflags(struct libmnt_context *cxt, unsigned long *flags) | |
1793 | { | |
1794 | struct libmnt_optlist *ls = mnt_context_get_optlist(cxt); | |
1795 | ||
1796 | if (!ls) | |
1797 | return -ENOMEM; | |
1798 | ||
1799 | return mnt_optlist_get_flags(ls, flags, cxt->map_userspace, 0); | |
1800 | } | |
1801 | ||
1802 | /** | |
1803 | * mnt_context_set_mountdata: | |
1804 | * @cxt: mount context | |
1805 | * @data: mount(2) data | |
1806 | * | |
1807 | * The mount context generates mountdata from mount options by default. This | |
1808 | * function can be used to overwrite this behavior, and @data will be used instead | |
1809 | * of mount options. | |
1810 | * | |
1811 | * The libmount does not deallocate the data by mnt_free_context(). Note that | |
1812 | * NULL is also valid mount data. | |
1813 | * | |
1814 | * Returns: 0 on success, negative number in case of error. | |
1815 | */ | |
1816 | int mnt_context_set_mountdata(struct libmnt_context *cxt, void *data) | |
1817 | { | |
1818 | if (!cxt) | |
1819 | return -EINVAL; | |
1820 | cxt->mountdata = data; | |
1821 | cxt->flags |= MNT_FL_MOUNTDATA; | |
1822 | return 0; | |
1823 | } | |
1824 | ||
1825 | #ifdef USE_LIBMOUNT_MOUNTFD_SUPPORT | |
1826 | int mnt_context_open_tree(struct libmnt_context *cxt, const char *path, unsigned long mflg) | |
1827 | { | |
1828 | unsigned long oflg = OPEN_TREE_CLOEXEC; | |
1829 | int rc = 0, fd = -1; | |
1830 | ||
1831 | if (mflg == (unsigned long) -1) { | |
1832 | rc = mnt_optlist_get_flags(cxt->optlist, &mflg, cxt->map_linux, 0); | |
1833 | if (rc) | |
1834 | return rc; | |
1835 | } | |
1836 | if (!path) { | |
1837 | path = mnt_fs_get_target(cxt->fs); | |
1838 | if (!path) | |
1839 | return -EINVAL; | |
1840 | } | |
1841 | ||
1842 | /* Classic -oremount,bind,ro is not bind operation, it's just | |
1843 | * VFS flags update only */ | |
1844 | if ((mflg & MS_BIND) && !(mflg & MS_REMOUNT)) { | |
1845 | oflg |= OPEN_TREE_CLONE; | |
1846 | ||
1847 | /* AT_RECURSIVE is only permitted for OPEN_TREE_CLONE (rbind). | |
1848 | * The other recursive operations are handled by | |
1849 | * mount_setattr() and are independent of open_tree() */ | |
1850 | if (mnt_optlist_is_rbind(cxt->optlist)) | |
1851 | oflg |= AT_RECURSIVE; | |
1852 | } | |
1853 | ||
1854 | if (cxt->force_clone) | |
1855 | oflg |= OPEN_TREE_CLONE; | |
1856 | ||
1857 | if (mnt_context_is_xnocanonicalize(cxt, "source")) | |
1858 | oflg |= AT_SYMLINK_NOFOLLOW; | |
1859 | ||
1860 | DBG(CXT, ul_debugobj(cxt, "open_tree(path=%s%s%s)", path, | |
1861 | oflg & OPEN_TREE_CLONE ? " clone" : "", | |
1862 | oflg & AT_RECURSIVE ? " recursive" : "")); | |
1863 | fd = open_tree(AT_FDCWD, path, oflg); | |
1864 | mnt_context_syscall_save_status(cxt, "open_tree", fd >= 0); | |
1865 | ||
1866 | return fd; | |
1867 | } | |
1868 | #endif | |
1869 | ||
1870 | /* | |
1871 | * Translates LABEL/UUID/path to mountable path | |
1872 | */ | |
1873 | int mnt_context_prepare_srcpath(struct libmnt_context *cxt) | |
1874 | { | |
1875 | const char *path = NULL; | |
1876 | struct libmnt_cache *cache; | |
1877 | const char *t, *v, *src, *type; | |
1878 | int rc = 0; | |
1879 | struct libmnt_ns *ns_old; | |
1880 | struct libmnt_optlist *ol; | |
1881 | ||
1882 | assert(cxt); | |
1883 | assert(cxt->fs); | |
1884 | assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); | |
1885 | ||
1886 | DBG(CXT, ul_debugobj(cxt, "--> preparing source path")); | |
1887 | ||
1888 | src = mnt_fs_get_source(cxt->fs); | |
1889 | ||
1890 | if (!src && mnt_context_propagation_only(cxt)) | |
1891 | /* mount --make-{shared,private,...} */ | |
1892 | return mnt_fs_set_source(cxt->fs, "none"); | |
1893 | ||
1894 | /* ignore filesystems without source or filesystems | |
1895 | * where the source is a quasi-path (//foo/bar) | |
1896 | */ | |
1897 | if (!src || mnt_fs_is_netfs(cxt->fs)) | |
1898 | return 0; | |
1899 | ||
1900 | /* ZFS source is always "dataset", not a real path */ | |
1901 | type = mnt_fs_get_fstype(cxt->fs); | |
1902 | if (type && strcmp(type, "zfs") == 0) | |
1903 | return 0; | |
1904 | ||
1905 | DBG(CXT, ul_debugobj(cxt, "srcpath '%s'", src)); | |
1906 | ||
1907 | ns_old = mnt_context_switch_target_ns(cxt); | |
1908 | if (!ns_old) | |
1909 | return -MNT_ERR_NAMESPACE; | |
1910 | ||
1911 | cache = mnt_context_get_cache(cxt); | |
1912 | ||
1913 | if (!mnt_fs_get_tag(cxt->fs, &t, &v)) { | |
1914 | /* | |
1915 | * Source is TAG (evaluate) | |
1916 | */ | |
1917 | if (cache) | |
1918 | path = mnt_resolve_tag(t, v, cache); | |
1919 | ||
1920 | rc = path ? mnt_fs_set_source(cxt->fs, path) : -MNT_ERR_NOSOURCE; | |
1921 | ||
1922 | } else if (cache && !mnt_fs_is_pseudofs(cxt->fs) | |
1923 | && !mnt_context_is_xnocanonicalize(cxt, "source")) { | |
1924 | /* | |
1925 | * Source is PATH (canonicalize) | |
1926 | */ | |
1927 | path = mnt_resolve_path(src, cache); | |
1928 | if (path && strcmp(path, src) != 0) | |
1929 | rc = mnt_fs_set_source(cxt->fs, path); | |
1930 | } | |
1931 | ||
1932 | if (rc) { | |
1933 | DBG(CXT, ul_debugobj(cxt, "failed to prepare srcpath [rc=%d]", rc)); | |
1934 | goto end; | |
1935 | } | |
1936 | ||
1937 | if (!path) | |
1938 | path = src; | |
1939 | ||
1940 | ol = mnt_context_get_optlist(cxt); | |
1941 | if (!ol) | |
1942 | return -ENOMEM; | |
1943 | ||
1944 | if (mnt_optlist_is_bind(ol) | |
1945 | || mnt_optlist_is_move(ol) | |
1946 | || mnt_optlist_is_remount(ol) | |
1947 | || mnt_fs_is_pseudofs(cxt->fs)) { | |
1948 | DBG(CXT, ul_debugobj(cxt, "REMOUNT/BIND/MOVE/pseudo FS source: %s", path)); | |
1949 | goto end; | |
1950 | } | |
1951 | ||
1952 | rc = mnt_context_call_hooks(cxt, MNT_STAGE_PREP_SOURCE); | |
1953 | if (rc) | |
1954 | goto end; | |
1955 | ||
1956 | DBG(CXT, ul_debugobj(cxt, "final srcpath '%s'", | |
1957 | mnt_fs_get_source(cxt->fs))); | |
1958 | ||
1959 | end: | |
1960 | if (!mnt_context_switch_ns(cxt, ns_old)) | |
1961 | return -MNT_ERR_NAMESPACE; | |
1962 | return rc; | |
1963 | } | |
1964 | ||
1965 | /* Guess type, but not set to cxt->fs, always use free() for the result. It's | |
1966 | * no error when we're not able to guess a filesystem type. Note that error | |
1967 | * does not mean that result in @type is NULL. | |
1968 | */ | |
1969 | int mnt_context_guess_srcpath_fstype(struct libmnt_context *cxt, char **type) | |
1970 | { | |
1971 | int rc = 0; | |
1972 | struct libmnt_ns *ns_old; | |
1973 | const char *dev; | |
1974 | ||
1975 | assert(type); | |
1976 | assert(cxt); | |
1977 | ||
1978 | *type = NULL; | |
1979 | ||
1980 | dev = mnt_fs_get_srcpath(cxt->fs); | |
1981 | if (!dev) | |
1982 | return 0; | |
1983 | ||
1984 | ns_old = mnt_context_switch_target_ns(cxt); | |
1985 | if (!ns_old) | |
1986 | return -MNT_ERR_NAMESPACE; | |
1987 | ||
1988 | if (access(dev, F_OK) == 0) { | |
1989 | struct libmnt_cache *cache = mnt_context_get_cache(cxt); | |
1990 | int ambi = 0; | |
1991 | ||
1992 | *type = mnt_get_fstype(dev, &ambi, cache); | |
1993 | if (ambi) | |
1994 | rc = -MNT_ERR_AMBIFS; | |
1995 | ||
1996 | if (cache && *type) { | |
1997 | *type = strdup(*type); | |
1998 | if (!*type) | |
1999 | rc = -ENOMEM; | |
2000 | } | |
2001 | } else { | |
2002 | DBG(CXT, ul_debugobj(cxt, "access(%s) failed [%m]", dev)); | |
2003 | if (strchr(dev, ':') != NULL) { | |
2004 | *type = strdup("nfs"); | |
2005 | if (!*type) | |
2006 | rc = -ENOMEM; | |
2007 | } else if (!strncmp(dev, "//", 2)) { | |
2008 | *type = strdup("cifs"); | |
2009 | if (!*type) | |
2010 | rc = -ENOMEM; | |
2011 | } | |
2012 | } | |
2013 | ||
2014 | if (!mnt_context_switch_ns(cxt, ns_old)) | |
2015 | return -MNT_ERR_NAMESPACE; | |
2016 | ||
2017 | if (rc == 0 && *type) { | |
2018 | struct libmnt_optlist *ol = mnt_context_get_optlist(cxt); | |
2019 | struct libmnt_opt *opt; | |
2020 | const char *allowed; | |
2021 | ||
2022 | if (!ol) { | |
2023 | free(*type); | |
2024 | *type = NULL; | |
2025 | return -ENOMEM; | |
2026 | } | |
2027 | ||
2028 | opt = mnt_optlist_get_named(ol, | |
2029 | "X-mount.auto-fstypes", cxt->map_userspace); | |
2030 | ||
2031 | if (opt | |
2032 | && (allowed = mnt_opt_get_value(opt)) | |
2033 | && !match_fstype(*type, allowed)) { | |
2034 | DBG(CXT, ul_debugobj(cxt, "%s is not allowed by auto-fstypes=%s", | |
2035 | *type, allowed)); | |
2036 | free(*type); | |
2037 | *type = NULL; | |
2038 | rc = -MNT_ERR_NOFSTYPE; | |
2039 | } | |
2040 | } | |
2041 | ||
2042 | return rc; | |
2043 | } | |
2044 | ||
2045 | /* | |
2046 | * It's usually no error when we're not able to detect the filesystem type -- we | |
2047 | * will try to use the types from /{etc,proc}/filesystems. | |
2048 | */ | |
2049 | int mnt_context_guess_fstype(struct libmnt_context *cxt) | |
2050 | { | |
2051 | struct libmnt_optlist *ol; | |
2052 | char *type; | |
2053 | int rc = 0; | |
2054 | ||
2055 | assert(cxt); | |
2056 | assert(cxt->fs); | |
2057 | assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); | |
2058 | ||
2059 | DBG(CXT, ul_debugobj(cxt, "--> preparing fstype")); | |
2060 | ||
2061 | ol = mnt_context_get_optlist(cxt); | |
2062 | if (!ol) | |
2063 | return -ENOMEM; | |
2064 | ||
2065 | if (mnt_optlist_is_bind(ol) | |
2066 | || mnt_optlist_is_move(ol) | |
2067 | || mnt_context_propagation_only(cxt)) | |
2068 | goto none; | |
2069 | ||
2070 | type = (char *) mnt_fs_get_fstype(cxt->fs); | |
2071 | if (type && !strcmp(type, "auto")) { | |
2072 | mnt_fs_set_fstype(cxt->fs, NULL); | |
2073 | type = NULL; | |
2074 | } | |
2075 | ||
2076 | if (type) | |
2077 | goto done; | |
2078 | if (mnt_optlist_is_remount(ol)) | |
2079 | goto none; | |
2080 | if (cxt->fstype_pattern) | |
2081 | goto done; | |
2082 | ||
2083 | rc = mnt_context_guess_srcpath_fstype(cxt, &type); | |
2084 | if (rc == 0 && type) | |
2085 | __mnt_fs_set_fstype_ptr(cxt->fs, type); | |
2086 | else | |
2087 | free(type); | |
2088 | done: | |
2089 | DBG(CXT, ul_debugobj(cxt, "FS type: %s [rc=%d]", | |
2090 | mnt_fs_get_fstype(cxt->fs), rc)); | |
2091 | return rc; | |
2092 | none: | |
2093 | return mnt_fs_set_fstype(cxt->fs, "none"); | |
2094 | } | |
2095 | ||
2096 | /* | |
2097 | * The default is to use fstype from cxt->fs, this could be overwritten by | |
2098 | * @type. The @act is MNT_ACT_{MOUNT,UMOUNT}. | |
2099 | * | |
2100 | * Returns: 0 on success or negative number in case of error. Note that success | |
2101 | * does not mean that there is any usable helper, you have to check cxt->helper. | |
2102 | */ | |
2103 | int mnt_context_prepare_helper(struct libmnt_context *cxt, const char *name, | |
2104 | const char *type) | |
2105 | { | |
2106 | char search_path[] = FS_SEARCH_PATH; /* from config.h */ | |
2107 | char *p = NULL, *path; | |
2108 | struct libmnt_ns *ns_old; | |
2109 | int rc = 0; | |
2110 | ||
2111 | assert(cxt); | |
2112 | assert(cxt->fs); | |
2113 | assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); | |
2114 | ||
2115 | DBG(CXT, ul_debugobj(cxt, "checking for helper")); | |
2116 | ||
2117 | if (cxt->helper) { | |
2118 | free(cxt->helper); | |
2119 | cxt->helper = NULL; | |
2120 | } | |
2121 | ||
2122 | if (!type) | |
2123 | type = mnt_fs_get_fstype(cxt->fs); | |
2124 | ||
2125 | if (type && strchr(type, ',')) | |
2126 | return 0; /* type is fstype pattern */ | |
2127 | ||
2128 | if (mnt_context_is_nohelpers(cxt) | |
2129 | || !type | |
2130 | || !strcmp(type, "none") | |
2131 | || strstr(type, "/..") /* don't try to smuggle path */ | |
2132 | || mnt_fs_is_swaparea(cxt->fs)) | |
2133 | return 0; | |
2134 | ||
2135 | ns_old = mnt_context_switch_origin_ns(cxt); | |
2136 | if (!ns_old) | |
2137 | return -MNT_ERR_NAMESPACE; | |
2138 | ||
2139 | /* Ignore errors when search in $PATH and do not modify @rc | |
2140 | */ | |
2141 | path = strtok_r(search_path, ":", &p); | |
2142 | while (path) { | |
2143 | char helper[PATH_MAX]; | |
2144 | int len, found = 0; | |
2145 | ||
2146 | len = snprintf(helper, sizeof(helper), "%s/%s.%s", | |
2147 | path, name, type); | |
2148 | path = strtok_r(NULL, ":", &p); | |
2149 | ||
2150 | if (len < 0 || (size_t) len >= sizeof(helper)) | |
2151 | continue; | |
2152 | ||
2153 | found = mnt_is_path(helper); | |
2154 | if (!found && strchr(type, '.')) { | |
2155 | /* If type ends with ".subtype" try without it */ | |
2156 | char *hs = strrchr(helper, '.'); | |
2157 | if (hs) | |
2158 | *hs = '\0'; | |
2159 | found = mnt_is_path(helper); | |
2160 | } | |
2161 | ||
2162 | DBG(CXT, ul_debugobj(cxt, "%-25s ... %s", helper, | |
2163 | found ? "found" : "not found")); | |
2164 | if (!found) | |
2165 | continue; | |
2166 | ||
2167 | /* success */ | |
2168 | rc = strdup_to_struct_member(cxt, helper, helper); | |
2169 | break; | |
2170 | } | |
2171 | ||
2172 | if (!mnt_context_switch_ns(cxt, ns_old)) | |
2173 | rc = -MNT_ERR_NAMESPACE; | |
2174 | ||
2175 | /* make sure helper is not set on error */ | |
2176 | if (rc) { | |
2177 | free(cxt->helper); | |
2178 | cxt->helper = NULL; | |
2179 | } | |
2180 | return rc; | |
2181 | } | |
2182 | ||
2183 | /* stop differentiate between options defined by flags and strings */ | |
2184 | int mnt_context_merge_mflags(struct libmnt_context *cxt) | |
2185 | { | |
2186 | struct libmnt_optlist *ls = mnt_context_get_optlist(cxt); | |
2187 | ||
2188 | if (!ls) | |
2189 | return -ENOMEM; | |
2190 | ||
2191 | /* TODO: optlist returns always flags as merged, so | |
2192 | * MNT_FL_MOUNTFLAGS_MERGED is unnecessary anymore | |
2193 | */ | |
2194 | cxt->flags |= MNT_FL_MOUNTFLAGS_MERGED; | |
2195 | return mnt_optlist_merge_opts(ls); | |
2196 | } | |
2197 | ||
2198 | /* | |
2199 | * Prepare /run/mount/utab | |
2200 | */ | |
2201 | int mnt_context_prepare_update(struct libmnt_context *cxt) | |
2202 | { | |
2203 | int rc; | |
2204 | const char *target, *source, *name; | |
2205 | unsigned long flags = 0; | |
2206 | struct libmnt_optlist *ol; | |
2207 | ||
2208 | assert(cxt); | |
2209 | assert(cxt->fs); | |
2210 | assert(cxt->action); | |
2211 | assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); | |
2212 | ||
2213 | DBG(CXT, ul_debugobj(cxt, "--> prepare update")); | |
2214 | ||
2215 | if (mnt_context_propagation_only(cxt)) { | |
2216 | DBG(CXT, ul_debugobj(cxt, "skip update: only MS_PROPAGATION")); | |
2217 | return 0; | |
2218 | } | |
2219 | ||
2220 | target = mnt_fs_get_target(cxt->fs); | |
2221 | source = mnt_fs_get_srcpath(cxt->fs); | |
2222 | ||
2223 | if (cxt->action == MNT_ACT_UMOUNT && target && !strcmp(target, "/")) { | |
2224 | DBG(CXT, ul_debugobj(cxt, "root umount: setting NOMTAB")); | |
2225 | mnt_context_disable_mtab(cxt, TRUE); | |
2226 | } | |
2227 | if (mnt_context_is_nomtab(cxt)) { | |
2228 | DBG(CXT, ul_debugobj(cxt, "skip update: NOMTAB flag")); | |
2229 | return 0; | |
2230 | } | |
2231 | name = mnt_context_get_writable_tabpath(cxt); | |
2232 | if (!name) { | |
2233 | DBG(CXT, ul_debugobj(cxt, "skip update: no writable destination")); | |
2234 | return 0; | |
2235 | } | |
2236 | ||
2237 | /* 0 = success, 1 = not called yet */ | |
2238 | if (cxt->syscall_status != 1 && cxt->syscall_status != 0) { | |
2239 | DBG(CXT, ul_debugobj(cxt, | |
2240 | "skip update: syscall failed [status=%d]", | |
2241 | cxt->syscall_status)); | |
2242 | return 0; | |
2243 | } | |
2244 | ||
2245 | ol = mnt_context_get_optlist(cxt); | |
2246 | if (!ol) | |
2247 | return -ENOMEM; | |
2248 | ||
2249 | if ((cxt->action == MNT_ACT_UMOUNT || mnt_optlist_is_move(ol)) | |
2250 | && is_file_empty(name)) { | |
2251 | DBG(CXT, ul_debugobj(cxt, "skip update: umount/move, empty table")); | |
2252 | return 0; | |
2253 | } | |
2254 | ||
2255 | if (mnt_optlist_is_move(ol) && | |
2256 | ((target && startswithpath(name, target)) | |
2257 | || (source && startswithpath(name, source)))) { | |
2258 | DBG(CXT, ul_debugobj(cxt, "skip update: utab move")); | |
2259 | return 0; | |
2260 | } | |
2261 | ||
2262 | if (!cxt->update) { | |
2263 | cxt->update = mnt_new_update(); | |
2264 | if (!cxt->update) | |
2265 | return -ENOMEM; | |
2266 | ||
2267 | mnt_update_set_filename(cxt->update, name); | |
2268 | } | |
2269 | ||
2270 | mnt_context_get_mflags(cxt, &flags); | |
2271 | ||
2272 | if (cxt->action == MNT_ACT_UMOUNT) | |
2273 | rc = mnt_update_set_fs(cxt->update, flags, | |
2274 | mnt_context_get_target(cxt), NULL); | |
2275 | else | |
2276 | rc = mnt_update_set_fs(cxt->update, flags, | |
2277 | NULL, cxt->fs); | |
2278 | ||
2279 | if (mnt_update_is_ready(cxt->update)) { | |
2280 | DBG(CXT, ul_debugobj(cxt, "update is ready")); | |
2281 | mnt_update_start(cxt->update); | |
2282 | } | |
2283 | return rc < 0 ? rc : 0; | |
2284 | } | |
2285 | ||
2286 | int mnt_context_update_tabs(struct libmnt_context *cxt) | |
2287 | { | |
2288 | int rc = 0; | |
2289 | struct libmnt_ns *ns_old; | |
2290 | ||
2291 | assert(cxt); | |
2292 | ||
2293 | if (mnt_context_is_nomtab(cxt)) { | |
2294 | DBG(CXT, ul_debugobj(cxt, "don't update: NOMTAB flag")); | |
2295 | return 0; | |
2296 | } | |
2297 | if (!cxt->update || !mnt_update_is_ready(cxt->update)) { | |
2298 | DBG(CXT, ul_debugobj(cxt, "don't update: no update prepared")); | |
2299 | return 0; | |
2300 | } | |
2301 | ||
2302 | ns_old = mnt_context_switch_target_ns(cxt); | |
2303 | if (!ns_old) | |
2304 | return -MNT_ERR_NAMESPACE; | |
2305 | ||
2306 | /* check utab update when external helper executed */ | |
2307 | if (mnt_context_helper_executed(cxt) | |
2308 | && mnt_context_get_helper_status(cxt) == 0 | |
2309 | && mnt_context_utab_writable(cxt)) { | |
2310 | ||
2311 | if (mnt_update_already_done(cxt->update)) { | |
2312 | DBG(CXT, ul_debugobj(cxt, "don't update: error evaluate or already updated")); | |
2313 | goto emit; | |
2314 | } | |
2315 | } else if (cxt->helper) { | |
2316 | DBG(CXT, ul_debugobj(cxt, "don't update: external helper")); | |
2317 | goto end; | |
2318 | } | |
2319 | ||
2320 | if (cxt->syscall_status != 0 | |
2321 | && !(mnt_context_helper_executed(cxt) && | |
2322 | mnt_context_get_helper_status(cxt) == 0)) { | |
2323 | ||
2324 | DBG(CXT, ul_debugobj(cxt, "don't update: syscall/helper failed/not called")); | |
2325 | goto end; | |
2326 | } | |
2327 | ||
2328 | rc = mnt_update_table(cxt->update, cxt->lock); | |
2329 | emit: | |
2330 | if (rc == 0 && !mnt_context_within_helper(cxt)) | |
2331 | mnt_update_emit_event(cxt->update); | |
2332 | ||
2333 | end: | |
2334 | mnt_update_end(cxt->update); | |
2335 | ||
2336 | if (!mnt_context_switch_ns(cxt, ns_old)) | |
2337 | return -MNT_ERR_NAMESPACE; | |
2338 | return rc; | |
2339 | } | |
2340 | ||
2341 | /* apply @fs to @cxt; | |
2342 | * | |
2343 | * @mflags are mount flags as specified on command-line -- used only to save | |
2344 | * MS_RDONLY which is allowed for non-root users. | |
2345 | */ | |
2346 | static int apply_fs(struct libmnt_context *cxt, struct libmnt_fs *fs, unsigned long mflags) | |
2347 | { | |
2348 | struct libmnt_optlist *ls; | |
2349 | int rc; | |
2350 | ||
2351 | ||
2352 | ||
2353 | if (!cxt->optsmode) { | |
2354 | if (mnt_context_is_restricted(cxt)) { | |
2355 | DBG(CXT, ul_debugobj(cxt, "force fstab usage for non-root users!")); | |
2356 | cxt->optsmode = MNT_OMODE_USER; | |
2357 | } else { | |
2358 | DBG(CXT, ul_debugobj(cxt, "use default optsmode")); | |
2359 | cxt->optsmode = MNT_OMODE_AUTO; | |
2360 | } | |
2361 | ||
2362 | } | |
2363 | ||
2364 | if (!mnt_context_get_fs(cxt)) | |
2365 | return -ENOMEM; | |
2366 | ||
2367 | DBG(CXT, ul_debugobj(cxt, "apply entry:")); | |
2368 | DBG(CXT, mnt_fs_print_debug(fs, stderr)); | |
2369 | DBG(CXT, ul_debugobj(cxt, "OPTSMODE (opt-part): ignore=%d, append=%d, prepend=%d, replace=%d", | |
2370 | cxt->optsmode & MNT_OMODE_IGNORE ? 1 : 0, | |
2371 | cxt->optsmode & MNT_OMODE_APPEND ? 1 : 0, | |
2372 | cxt->optsmode & MNT_OMODE_PREPEND ? 1 : 0, | |
2373 | cxt->optsmode & MNT_OMODE_REPLACE ? 1 : 0)); | |
2374 | ||
2375 | /* copy from fs to our FS description | |
2376 | */ | |
2377 | rc = mnt_fs_set_source(cxt->fs, mnt_fs_get_source(fs)); | |
2378 | if (!rc) | |
2379 | rc = mnt_fs_set_target(cxt->fs, mnt_fs_get_target(fs)); | |
2380 | ||
2381 | if (!rc && !mnt_fs_get_fstype(cxt->fs)) | |
2382 | rc = mnt_fs_set_fstype(cxt->fs, mnt_fs_get_fstype(fs)); | |
2383 | ||
2384 | if (!rc && !mnt_fs_get_root(cxt->fs) && mnt_fs_get_root(fs)) | |
2385 | rc = mnt_fs_set_root(cxt->fs, mnt_fs_get_root(fs)); | |
2386 | ||
2387 | if (rc) | |
2388 | goto done; | |
2389 | ||
2390 | ls = mnt_context_get_optlist(cxt); | |
2391 | if (!ls) { | |
2392 | rc = -ENOMEM; | |
2393 | goto done; | |
2394 | } | |
2395 | ||
2396 | if (cxt->optsmode & MNT_OMODE_IGNORE) | |
2397 | ; | |
2398 | else if (cxt->optsmode & MNT_OMODE_REPLACE) { | |
2399 | rc = mnt_optlist_set_optstr(ls, mnt_fs_get_options(fs), NULL); | |
2400 | ||
2401 | /* mount --read-only for non-root users is allowed */ | |
2402 | if (rc == 0 && (mflags & MS_RDONLY) | |
2403 | && mnt_context_is_restricted(cxt) | |
2404 | && cxt->optsmode == MNT_OMODE_USER) | |
2405 | rc = mnt_optlist_append_optstr(ls, "ro", NULL); | |
2406 | } | |
2407 | else if (cxt->optsmode & MNT_OMODE_APPEND) | |
2408 | rc = mnt_optlist_append_optstr(ls, mnt_fs_get_options(fs), NULL); | |
2409 | ||
2410 | else if (cxt->optsmode & MNT_OMODE_PREPEND) | |
2411 | rc = mnt_optlist_prepend_optstr(ls, mnt_fs_get_options(fs), NULL); | |
2412 | ||
2413 | if (!rc) | |
2414 | cxt->flags |= MNT_FL_TAB_APPLIED; | |
2415 | ||
2416 | done: | |
2417 | DBG(CXT, ul_debugobj(cxt, "final entry [rc=%d]", rc)); | |
2418 | DBG(CXT, mnt_fs_print_debug(cxt->fs, stderr)); | |
2419 | ||
2420 | return rc; | |
2421 | } | |
2422 | ||
2423 | static int apply_table(struct libmnt_context *cxt, struct libmnt_table *tb, | |
2424 | int direction, unsigned long mflags) | |
2425 | { | |
2426 | struct libmnt_fs *fs = NULL; | |
2427 | const char *src, *tgt; | |
2428 | ||
2429 | assert(cxt); | |
2430 | assert(cxt->fs); | |
2431 | ||
2432 | src = mnt_fs_get_source(cxt->fs); | |
2433 | tgt = mnt_fs_get_target(cxt->fs); | |
2434 | ||
2435 | if (tgt && src) | |
2436 | fs = mnt_table_find_pair(tb, src, tgt, direction); | |
2437 | else { | |
2438 | if (src) | |
2439 | fs = mnt_table_find_source(tb, src, direction); | |
2440 | else if (tgt) | |
2441 | fs = mnt_table_find_target(tb, tgt, direction); | |
2442 | ||
2443 | if (!fs && mnt_context_is_swapmatch(cxt)) { | |
2444 | /* swap source and target (if @src is not LABEL/UUID), | |
2445 | * for example in | |
2446 | * | |
2447 | * mount /foo/bar | |
2448 | * | |
2449 | * the path could be a mountpoint as well as a source (for | |
2450 | * example bind mount, symlink to a device, ...). | |
2451 | */ | |
2452 | if (src && !mnt_fs_get_tag(cxt->fs, NULL, NULL)) | |
2453 | fs = mnt_table_find_target(tb, src, direction); | |
2454 | if (!fs && tgt) | |
2455 | fs = mnt_table_find_source(tb, tgt, direction); | |
2456 | } | |
2457 | } | |
2458 | ||
2459 | if (!fs) | |
2460 | return -MNT_ERR_NOFSTAB; /* not found */ | |
2461 | ||
2462 | return apply_fs(cxt, fs, mflags); | |
2463 | } | |
2464 | ||
2465 | /* apply @fs to @cxt -- use mnt_context_apply_fstab() if not sure | |
2466 | */ | |
2467 | int mnt_context_apply_fs(struct libmnt_context *cxt, struct libmnt_fs *fs) | |
2468 | { | |
2469 | return apply_fs(cxt, fs, 0); | |
2470 | } | |
2471 | ||
2472 | /** | |
2473 | * mnt_context_apply_fstab: | |
2474 | * @cxt: mount context | |
2475 | * | |
2476 | * This function is optional. | |
2477 | * | |
2478 | * Returns: 0 on success, negative number in case of error. | |
2479 | */ | |
2480 | int mnt_context_apply_fstab(struct libmnt_context *cxt) | |
2481 | { | |
2482 | int rc = -1, isremount = 0, iscmdbind = 0; | |
2483 | struct libmnt_ns *ns_old; | |
2484 | struct libmnt_table *tab = NULL; | |
2485 | const char *src = NULL, *tgt = NULL; | |
2486 | unsigned long mflags = 0; | |
2487 | ||
2488 | if (!cxt || !cxt->fs) | |
2489 | return -EINVAL; | |
2490 | ||
2491 | if (mnt_context_tab_applied(cxt)) { /* already applied */ | |
2492 | DBG(CXT, ul_debugobj(cxt, "fstab already applied -- skip")); | |
2493 | return 0; | |
2494 | } | |
2495 | ||
2496 | if (mnt_context_is_restricted(cxt)) { | |
2497 | DBG(CXT, ul_debugobj(cxt, "force fstab usage for non-root users!")); | |
2498 | cxt->optsmode = MNT_OMODE_USER; | |
2499 | } else if (cxt->optsmode == 0) { | |
2500 | DBG(CXT, ul_debugobj(cxt, "use default optsmode")); | |
2501 | cxt->optsmode = MNT_OMODE_AUTO; | |
2502 | } else if (cxt->optsmode & MNT_OMODE_NOTAB) { | |
2503 | cxt->optsmode &= ~MNT_OMODE_FSTAB; | |
2504 | cxt->optsmode &= ~MNT_OMODE_MTAB; | |
2505 | cxt->optsmode &= ~MNT_OMODE_FORCE; | |
2506 | } | |
2507 | ||
2508 | if (mnt_context_get_mflags(cxt, &mflags) == 0) { | |
2509 | isremount = !!(mflags & MS_REMOUNT); | |
2510 | iscmdbind = !!(mflags & MS_BIND); | |
2511 | } | |
2512 | ||
2513 | if (cxt->fs) { | |
2514 | src = mnt_fs_get_source(cxt->fs); | |
2515 | tgt = mnt_fs_get_target(cxt->fs); | |
2516 | } | |
2517 | ||
2518 | DBG(CXT, ul_debugobj(cxt, "OPTSMODE (file-part): force=%d, fstab=%d, mtab=%d", | |
2519 | cxt->optsmode & MNT_OMODE_FORCE ? 1 : 0, | |
2520 | cxt->optsmode & MNT_OMODE_FSTAB ? 1 : 0, | |
2521 | cxt->optsmode & MNT_OMODE_MTAB ? 1 : 0)); | |
2522 | ||
2523 | /* fstab is not required if source and target are specified */ | |
2524 | if (src && tgt && !(cxt->optsmode & MNT_OMODE_FORCE)) { | |
2525 | DBG(CXT, ul_debugobj(cxt, "fstab not required -- skip")); | |
2526 | return 0; | |
2527 | } | |
2528 | ||
2529 | if (!src && tgt | |
2530 | && !(cxt->optsmode & MNT_OMODE_FSTAB) | |
2531 | && !(cxt->optsmode & MNT_OMODE_MTAB)) { | |
2532 | DBG(CXT, ul_debugobj(cxt, "only target; fstab/mtab not required " | |
2533 | "-- skip, probably MS_PROPAGATION")); | |
2534 | return 0; | |
2535 | } | |
2536 | ||
2537 | /* let's initialize cxt->fs */ | |
2538 | ignore_result( mnt_context_get_fs(cxt) ); | |
2539 | ||
2540 | ns_old = mnt_context_switch_target_ns(cxt); | |
2541 | if (!ns_old) | |
2542 | return -MNT_ERR_NAMESPACE; | |
2543 | ||
2544 | /* try fstab */ | |
2545 | if (cxt->optsmode & MNT_OMODE_FSTAB) { | |
2546 | DBG(CXT, ul_debugobj(cxt, "trying to apply fstab (src=%s, target=%s)", src, tgt)); | |
2547 | rc = mnt_context_get_fstab(cxt, &tab); | |
2548 | if (!rc) | |
2549 | rc = apply_table(cxt, tab, MNT_ITER_FORWARD, mflags); | |
2550 | } | |
2551 | ||
2552 | /* try mountinfo */ | |
2553 | if (rc < 0 && (cxt->optsmode & MNT_OMODE_MTAB) | |
2554 | && (isremount || cxt->action == MNT_ACT_UMOUNT)) { | |
2555 | DBG(CXT, ul_debugobj(cxt, "trying to apply mountinfo (src=%s, target=%s)", src, tgt)); | |
2556 | if (tgt) | |
2557 | rc = mnt_context_get_mountinfo_for_target(cxt, &tab, tgt); | |
2558 | else | |
2559 | rc = mnt_context_get_mountinfo(cxt, &tab); | |
2560 | if (!rc) | |
2561 | rc = apply_table(cxt, tab, MNT_ITER_BACKWARD, mflags); | |
2562 | } | |
2563 | ||
2564 | if (!mnt_context_switch_ns(cxt, ns_old)) | |
2565 | return -MNT_ERR_NAMESPACE; | |
2566 | ||
2567 | if (rc) { | |
2568 | if (!mnt_context_is_restricted(cxt) | |
2569 | && tgt && !src | |
2570 | && isremount) { | |
2571 | DBG(CXT, ul_debugobj(cxt, "only target; ignore missing mountinfo entry on remount")); | |
2572 | return 0; | |
2573 | } | |
2574 | ||
2575 | DBG(CXT, ul_debugobj(cxt, "failed to find entry in fstab/mountinfo [rc=%d]: %m", rc)); | |
2576 | ||
2577 | /* force to "not found in fstab/mountinfo" error, the details why | |
2578 | * not found are not so important and may be misinterpreted by | |
2579 | * applications... */ | |
2580 | rc = -MNT_ERR_NOFSTAB; | |
2581 | ||
2582 | ||
2583 | } else if (isremount && !iscmdbind && cxt->optlist) { | |
2584 | ||
2585 | /* ignore "bind" on remount when the flag is read from fstab */ | |
2586 | mnt_optlist_remove_named(cxt->optlist, "bind", NULL); | |
2587 | } | |
2588 | return rc; | |
2589 | } | |
2590 | ||
2591 | /** | |
2592 | * mnt_context_tab_applied: | |
2593 | * @cxt: mount context | |
2594 | * | |
2595 | * Returns: 1 if fstab (or mountinfo) has been applied to the context, or 0. | |
2596 | */ | |
2597 | int mnt_context_tab_applied(struct libmnt_context *cxt) | |
2598 | { | |
2599 | return cxt->flags & MNT_FL_TAB_APPLIED; | |
2600 | } | |
2601 | ||
2602 | /* | |
2603 | * This is not a public function! | |
2604 | * | |
2605 | * Returns 1 if *only propagation flags* change is requested. | |
2606 | */ | |
2607 | int mnt_context_propagation_only(struct libmnt_context *cxt) | |
2608 | { | |
2609 | struct libmnt_optlist *ls; | |
2610 | ||
2611 | if (cxt->action != MNT_ACT_MOUNT) | |
2612 | return 0; | |
2613 | if (cxt->mountdata || cxt->fs == NULL) | |
2614 | return 0; | |
2615 | if (cxt->fs->fstype && strcmp(cxt->fs->fstype, "none") != 0) | |
2616 | return 0; | |
2617 | if (cxt->fs->source && strcmp(cxt->fs->source, "none") != 0) | |
2618 | return 0; | |
2619 | ||
2620 | ls = mnt_context_get_optlist(cxt); | |
2621 | return ls ? mnt_optlist_is_propagation_only(ls) : 0; | |
2622 | } | |
2623 | ||
2624 | /** | |
2625 | * mnt_context_get_status: | |
2626 | * @cxt: mount context | |
2627 | * | |
2628 | * Global libmount status. | |
2629 | * | |
2630 | * The real exit code of the mount.type helper has to be tested by | |
2631 | * mnt_context_get_helper_status(). The mnt_context_get_status() only informs | |
2632 | * that exec() has been successful. | |
2633 | * | |
2634 | * Returns: 1 if mount.type or mount(2) syscall has been successfully called. | |
2635 | */ | |
2636 | int mnt_context_get_status(struct libmnt_context *cxt) | |
2637 | { | |
2638 | return !cxt->syscall_status || !cxt->helper_exec_status; | |
2639 | } | |
2640 | ||
2641 | /** | |
2642 | * mnt_context_helper_executed: | |
2643 | * @cxt: mount context | |
2644 | * | |
2645 | * Returns: 1 if mount.type helper has been executed, or 0. | |
2646 | */ | |
2647 | int mnt_context_helper_executed(struct libmnt_context *cxt) | |
2648 | { | |
2649 | return cxt->helper_exec_status != 1; | |
2650 | } | |
2651 | ||
2652 | /** | |
2653 | * mnt_context_get_helper_status: | |
2654 | * @cxt: mount context | |
2655 | * | |
2656 | * Return: mount.type helper exit status, result is reliable only if | |
2657 | * mnt_context_helper_executed() returns 1. | |
2658 | */ | |
2659 | int mnt_context_get_helper_status(struct libmnt_context *cxt) | |
2660 | { | |
2661 | return cxt->helper_status; | |
2662 | } | |
2663 | ||
2664 | /** | |
2665 | * mnt_context_syscall_called: | |
2666 | * @cxt: mount context | |
2667 | * | |
2668 | * Returns: 1 if mount(2) syscall has been called, or 0. | |
2669 | */ | |
2670 | int mnt_context_syscall_called(struct libmnt_context *cxt) | |
2671 | { | |
2672 | return cxt->syscall_status != 1; | |
2673 | } | |
2674 | ||
2675 | /** | |
2676 | * mnt_context_get_syscall_errno: | |
2677 | * @cxt: mount context | |
2678 | * | |
2679 | * The result from this function is reliable only if | |
2680 | * mnt_context_syscall_called() returns 1. | |
2681 | * | |
2682 | * Returns: mount(2) errno if the syscall failed or 0. | |
2683 | */ | |
2684 | int mnt_context_get_syscall_errno(struct libmnt_context *cxt) | |
2685 | { | |
2686 | if (cxt->syscall_status < 0) | |
2687 | return -cxt->syscall_status; | |
2688 | return 0; | |
2689 | } | |
2690 | ||
2691 | /** | |
2692 | * mnt_context_set_syscall_status: | |
2693 | * @cxt: mount context | |
2694 | * @status: mount(2) status | |
2695 | * | |
2696 | * The @status should be 0 on success, or negative number on error (-errno). | |
2697 | * | |
2698 | * This function is intended for cases where mount/umount is called externally, | |
2699 | * rather than by libmount. | |
2700 | * | |
2701 | * Returns: 0 or negative number in case of error. | |
2702 | */ | |
2703 | int mnt_context_set_syscall_status(struct libmnt_context *cxt, int status) | |
2704 | { | |
2705 | if (!cxt) | |
2706 | return -EINVAL; | |
2707 | ||
2708 | DBG(CXT, ul_debugobj(cxt, "syscall status set to: %d", status)); | |
2709 | cxt->syscall_status = status; | |
2710 | return 0; | |
2711 | } | |
2712 | ||
2713 | /* Use this for syscalls called from libmount */ | |
2714 | void mnt_context_syscall_save_status( struct libmnt_context *cxt, | |
2715 | const char *syscallname, | |
2716 | int success) | |
2717 | { | |
2718 | if (!success) { | |
2719 | DBG(CXT, ul_debug("syscall '%s' [failed: %m]", syscallname)); | |
2720 | cxt->syscall_status = -errno; | |
2721 | cxt->syscall_name = syscallname; | |
2722 | } else { | |
2723 | DBG(CXT, ul_debug("syscall '%s' [success]", syscallname)); | |
2724 | cxt->syscall_status = 0; | |
2725 | } | |
2726 | } | |
2727 | ||
2728 | void mnt_context_syscall_reset_status(struct libmnt_context *cxt) | |
2729 | { | |
2730 | DBG(CXT, ul_debug("reset syscall status")); | |
2731 | cxt->syscall_status = 0; | |
2732 | cxt->syscall_name = NULL; | |
2733 | ||
2734 | mnt_context_reset_mesgs(cxt); | |
2735 | } | |
2736 | ||
2737 | void mnt_context_reset_mesgs(struct libmnt_context *cxt) | |
2738 | { | |
2739 | DBG(CXT, ul_debug("reset messages")); | |
2740 | ul_strv_free(cxt->mesgs); | |
2741 | cxt->mesgs = NULL; | |
2742 | } | |
2743 | ||
2744 | int mnt_context_append_mesg(struct libmnt_context *cxt, const char *msg) | |
2745 | { | |
2746 | if (msg) { | |
2747 | DBG(CXT, ul_debug("mesg: '%s'", msg)); | |
2748 | } | |
2749 | return ul_strv_extend(&cxt->mesgs, msg); | |
2750 | } | |
2751 | ||
2752 | int mnt_context_sprintf_mesg(struct libmnt_context *cxt, const char *msg, ...) | |
2753 | { | |
2754 | int rc; | |
2755 | va_list ap; | |
2756 | ||
2757 | va_start(ap, msg); | |
2758 | rc = ul_strv_extendv(&cxt->mesgs, msg, ap); | |
2759 | va_end(ap); | |
2760 | ||
2761 | return rc; | |
2762 | } | |
2763 | ||
2764 | int mnt_context_read_mesgs(struct libmnt_context *cxt, int fd) | |
2765 | { | |
2766 | uint8_t buf[BUFSIZ]; | |
2767 | ssize_t sz; | |
2768 | size_t count = 0; | |
2769 | int errsv = errno; | |
2770 | ||
2771 | if (fd < 0) | |
2772 | return 0; | |
2773 | ||
2774 | while ((sz = read(fd, buf, sizeof(buf) - 1)) != -1) { | |
2775 | ||
2776 | if (sz <= 0) | |
2777 | continue; | |
2778 | if (buf[sz - 1] == '\n') | |
2779 | buf[--sz] = '\0'; | |
2780 | else | |
2781 | buf[sz] = '\0'; | |
2782 | ||
2783 | if (!*buf) | |
2784 | continue; | |
2785 | ||
2786 | mnt_context_append_mesg(cxt, (char *) buf); | |
2787 | count++;; | |
2788 | } | |
2789 | ||
2790 | errno = errsv; | |
2791 | return count; | |
2792 | } | |
2793 | ||
2794 | /** | |
2795 | * mnt_context_get_nmesgs: | |
2796 | * @cxt: mount context | |
2797 | * @type: type of message (see fsopen() man page) or zero for all types | |
2798 | * | |
2799 | * Returns: number of messages | |
2800 | * | |
2801 | * Since: 2.41 | |
2802 | */ | |
2803 | size_t mnt_context_get_nmesgs(struct libmnt_context *cxt, char type) | |
2804 | { | |
2805 | size_t n; | |
2806 | char **s; | |
2807 | ||
2808 | if (!cxt || !cxt->mesgs) | |
2809 | return 0; | |
2810 | ||
2811 | n = ul_strv_length(cxt->mesgs); | |
2812 | if (n && type) { | |
2813 | n = 0; | |
2814 | UL_STRV_FOREACH(s, cxt->mesgs) { | |
2815 | if (*s && **s == type) | |
2816 | n++; | |
2817 | } | |
2818 | } | |
2819 | ||
2820 | return n; | |
2821 | } | |
2822 | ||
2823 | /** | |
2824 | * mnt_context_get_mesgs: | |
2825 | * @cxt: mount context | |
2826 | * | |
2827 | * Returns: NULL terminated array of messages or NULL | |
2828 | * | |
2829 | * Since: 2.41 | |
2830 | */ | |
2831 | char **mnt_context_get_mesgs(struct libmnt_context *cxt) | |
2832 | { | |
2833 | return cxt ? cxt->mesgs : NULL; | |
2834 | } | |
2835 | ||
2836 | /** | |
2837 | * mnt_context_strerror | |
2838 | * @cxt: context | |
2839 | * @buf: buffer | |
2840 | * @bufsiz: size of the buffer | |
2841 | * | |
2842 | * Not implemented, deprecated in favor or mnt_context_get_excode(). | |
2843 | * | |
2844 | * Returns: 0 or negative number in case of error. | |
2845 | */ | |
2846 | int mnt_context_strerror(struct libmnt_context *cxt __attribute__((__unused__)), | |
2847 | char *buf __attribute__((__unused__)), | |
2848 | size_t bufsiz __attribute__((__unused__))) | |
2849 | { | |
2850 | /* TODO: based on cxt->syscall_errno or cxt->helper_status */ | |
2851 | return 0; | |
2852 | } | |
2853 | ||
2854 | /** | |
2855 | * mnt_context_enable_noautofs: | |
2856 | * @cxt: context | |
2857 | * @ignore: ignore or don't ignore | |
2858 | * | |
2859 | * Enable/disable ignore autofs mount table entries on reading the | |
2860 | * mount table. Only effective if the "ignore" mount option is being | |
2861 | * used for autofs mounts (such as if automount(8) has been configured | |
2862 | * to do so). | |
2863 | */ | |
2864 | int mnt_context_enable_noautofs(struct libmnt_context *cxt, int ignore) | |
2865 | { | |
2866 | if (!cxt) | |
2867 | return -EINVAL; | |
2868 | cxt->noautofs = ignore ? 1 : 0; | |
2869 | return 0; | |
2870 | } | |
2871 | ||
2872 | int mnt_context_get_generic_excode(int rc, char *buf, size_t bufsz, const char *fmt, ...) | |
2873 | { | |
2874 | va_list va; | |
2875 | ||
2876 | if (rc == 0) | |
2877 | return MNT_EX_SUCCESS; | |
2878 | ||
2879 | va_start(va, fmt); | |
2880 | ||
2881 | /* we need to support "%m" */ | |
2882 | errno = rc < 0 ? -rc : rc; | |
2883 | ||
2884 | if (buf && bufsz && vsnprintf(buf, bufsz, fmt, va) < 0) | |
2885 | *buf = '\0'; | |
2886 | ||
2887 | switch (errno) { | |
2888 | case EINVAL: | |
2889 | case EPERM: | |
2890 | rc = MNT_EX_USAGE; | |
2891 | break; | |
2892 | case ENOMEM: | |
2893 | rc = MNT_EX_SYSERR; | |
2894 | break; | |
2895 | default: | |
2896 | rc = MNT_EX_FAIL; | |
2897 | break; | |
2898 | } | |
2899 | va_end(va); | |
2900 | return rc; | |
2901 | } | |
2902 | ||
2903 | /** | |
2904 | * mnt_context_get_excode: | |
2905 | * @cxt: context | |
2906 | * @rc: return code of the previous operation | |
2907 | * @buf: buffer to print error message (optional) | |
2908 | * @bufsz: size of the buffer | |
2909 | * | |
2910 | * This function analyzes context, [u]mount syscall and external helper status | |
2911 | * and @mntrc and generates unified return code (see MNT_EX_*) as expected | |
2912 | * from mount(8) or umount(8). | |
2913 | * | |
2914 | * If the external helper (e.g. /sbin/mount.type) has been executed than it | |
2915 | * returns status from wait() of the helper. It's not libmount fail if helper | |
2916 | * returns some crazy undocumented codes... See mnt_context_helper_executed() | |
2917 | * and mnt_context_get_helper_status(). Note that mount(8) and umount(8) utils | |
2918 | * always return code from helper without extra care about it. | |
2919 | * | |
2920 | * The current implementation does not read error message from external | |
2921 | * helper into @buf. | |
2922 | * | |
2923 | * If the argument @buf is not NULL then error message is generated (if | |
2924 | * anything failed). | |
2925 | * | |
2926 | * The @mntrc is usually return code from mnt_context_mount(), | |
2927 | * mnt_context_umount(), or 'mntrc' as returned by mnt_context_next_mount(). | |
2928 | * | |
2929 | * Since: 2.30 | |
2930 | * | |
2931 | * Returns: MNT_EX_* codes. | |
2932 | */ | |
2933 | int mnt_context_get_excode( | |
2934 | struct libmnt_context *cxt, | |
2935 | int rc, | |
2936 | char *buf, | |
2937 | size_t bufsz) | |
2938 | { | |
2939 | if (buf) { | |
2940 | *buf = '\0'; /* for sure */ | |
2941 | ||
2942 | if (!cxt->enabled_textdomain) { | |
2943 | bindtextdomain(LIBMOUNT_TEXTDOMAIN, LOCALEDIR); | |
2944 | cxt->enabled_textdomain = 1; | |
2945 | } | |
2946 | } | |
2947 | ||
2948 | switch (cxt->action) { | |
2949 | case MNT_ACT_MOUNT: | |
2950 | rc = mnt_context_get_mount_excode(cxt, rc, buf, bufsz); | |
2951 | break; | |
2952 | case MNT_ACT_UMOUNT: | |
2953 | rc = mnt_context_get_umount_excode(cxt, rc, buf, bufsz); | |
2954 | break; | |
2955 | default: | |
2956 | if (rc) | |
2957 | rc = mnt_context_get_generic_excode(rc, buf, bufsz, | |
2958 | _("operation failed: %m")); | |
2959 | else | |
2960 | rc = MNT_EX_SUCCESS; | |
2961 | break; | |
2962 | } | |
2963 | ||
2964 | DBG(CXT, ul_debugobj(cxt, "excode: rc=%d message=\"%s\"", rc, | |
2965 | buf ? buf : "<no-message>")); | |
2966 | return rc; | |
2967 | } | |
2968 | ||
2969 | /** | |
2970 | * mnt_context_init_helper | |
2971 | * @cxt: mount context | |
2972 | * @action: MNT_ACT_{UMOUNT,MOUNT} | |
2973 | * @flags: not used now | |
2974 | * | |
2975 | * This function informs libmount that used from [u]mount.type helper. | |
2976 | * | |
2977 | * The function also calls mnt_context_disable_helpers() to avoid recursive | |
2978 | * mount.type helpers calling. It you really want to call another | |
2979 | * mount.type helper from your helper, then you have to explicitly enable this | |
2980 | * feature by: | |
2981 | * | |
2982 | * mnt_context_disable_helpers(cxt, FALSE); | |
2983 | * | |
2984 | * Returns: 0 on success, negative number in case of error. | |
2985 | */ | |
2986 | int mnt_context_init_helper(struct libmnt_context *cxt, int action, | |
2987 | int flags __attribute__((__unused__))) | |
2988 | { | |
2989 | int rc; | |
2990 | ||
2991 | if (!cxt) | |
2992 | return -EINVAL; | |
2993 | ||
2994 | rc = mnt_context_disable_helpers(cxt, TRUE); | |
2995 | if (!rc) | |
2996 | rc = set_flag(cxt, MNT_FL_HELPER, 1); | |
2997 | if (!rc) | |
2998 | cxt->action = action; | |
2999 | ||
3000 | DBG(CXT, ul_debugobj(cxt, "initialized for [u]mount.<type> helper [rc=%d]", rc)); | |
3001 | return rc; | |
3002 | } | |
3003 | ||
3004 | /* | |
3005 | * libmount used in /sbin/[u]mount.<type> helper | |
3006 | */ | |
3007 | int mnt_context_within_helper(struct libmnt_context *cxt) | |
3008 | { | |
3009 | return cxt && (cxt->flags & MNT_FL_HELPER); | |
3010 | } | |
3011 | ||
3012 | /** | |
3013 | * mnt_context_helper_setopt: | |
3014 | * @cxt: context | |
3015 | * @c: getopt() result | |
3016 | * @arg: getopt() optarg | |
3017 | * | |
3018 | * This function applies the [u]mount.type command line option (for example parsed | |
3019 | * by getopt or getopt_long) to @cxt. All unknown options are ignored and | |
3020 | * then 1 is returned. | |
3021 | * | |
3022 | * Returns: negative number on error, 1 if @c is unknown option, 0 on success. | |
3023 | */ | |
3024 | int mnt_context_helper_setopt(struct libmnt_context *cxt, int c, char *arg) | |
3025 | { | |
3026 | if (cxt) { | |
3027 | switch(cxt->action) { | |
3028 | case MNT_ACT_MOUNT: | |
3029 | return mnt_context_mount_setopt(cxt, c, arg); | |
3030 | case MNT_ACT_UMOUNT: | |
3031 | return mnt_context_umount_setopt(cxt, c, arg); | |
3032 | } | |
3033 | } | |
3034 | return -EINVAL; | |
3035 | } | |
3036 | ||
3037 | /** | |
3038 | * mnt_context_is_fs_mounted: | |
3039 | * @cxt: context | |
3040 | * @fs: filesystem | |
3041 | * @mounted: returns 1 for mounted and 0 for non-mounted filesystems | |
3042 | * | |
3043 | * Please, read the mnt_table_is_fs_mounted() description! | |
3044 | * | |
3045 | * Returns: 0 on success and negative number in case of error. | |
3046 | */ | |
3047 | int mnt_context_is_fs_mounted(struct libmnt_context *cxt, | |
3048 | struct libmnt_fs *fs, int *mounted) | |
3049 | { | |
3050 | struct libmnt_table *mountinfo, *orig; | |
3051 | int rc = 0; | |
3052 | struct libmnt_ns *ns_old; | |
3053 | ||
3054 | if (!cxt || !fs || !mounted) | |
3055 | return -EINVAL; | |
3056 | ||
3057 | ns_old = mnt_context_switch_target_ns(cxt); | |
3058 | if (!ns_old) | |
3059 | return -MNT_ERR_NAMESPACE; | |
3060 | ||
3061 | orig = cxt->mountinfo; | |
3062 | rc = mnt_context_get_mountinfo(cxt, &mountinfo); | |
3063 | if (rc == -ENOENT && mnt_fs_streq_target(fs, "/proc")) { | |
3064 | if (!orig) { | |
3065 | mnt_unref_table(cxt->mountinfo); | |
3066 | cxt->mountinfo = NULL; | |
3067 | } | |
3068 | *mounted = 0; | |
3069 | rc = 0; /* /proc not mounted */ | |
3070 | ||
3071 | } else if (rc == 0) { | |
3072 | *mounted = __mnt_table_is_fs_mounted(mountinfo, fs, | |
3073 | mnt_context_get_target_prefix(cxt)); | |
3074 | ||
3075 | } | |
3076 | ||
3077 | if (!mnt_context_switch_ns(cxt, ns_old)) | |
3078 | return -MNT_ERR_NAMESPACE; | |
3079 | return rc; | |
3080 | } | |
3081 | ||
3082 | static int mnt_context_add_child(struct libmnt_context *cxt, pid_t pid) | |
3083 | { | |
3084 | pid_t *pids; | |
3085 | ||
3086 | if (!cxt) | |
3087 | return -EINVAL; | |
3088 | ||
3089 | pids = reallocarray(cxt->children, cxt->nchildren + 1, sizeof(pid_t)); | |
3090 | if (!pids) | |
3091 | return -ENOMEM; | |
3092 | ||
3093 | DBG(CXT, ul_debugobj(cxt, "add new child %d", pid)); | |
3094 | cxt->children = pids; | |
3095 | cxt->children[cxt->nchildren++] = pid; | |
3096 | ||
3097 | return 0; | |
3098 | } | |
3099 | ||
3100 | int mnt_fork_context(struct libmnt_context *cxt) | |
3101 | { | |
3102 | int rc = 0; | |
3103 | pid_t pid; | |
3104 | ||
3105 | assert(cxt); | |
3106 | if (!mnt_context_is_parent(cxt)) | |
3107 | return -EINVAL; | |
3108 | ||
3109 | DBG(CXT, ul_debugobj(cxt, "forking context")); | |
3110 | ||
3111 | DBG_FLUSH; | |
3112 | ||
3113 | pid = fork(); | |
3114 | ||
3115 | switch (pid) { | |
3116 | case -1: /* error */ | |
3117 | DBG(CXT, ul_debugobj(cxt, "fork failed %m")); | |
3118 | return -errno; | |
3119 | ||
3120 | case 0: /* child */ | |
3121 | cxt->pid = getpid(); | |
3122 | mnt_context_enable_fork(cxt, FALSE); | |
3123 | DBG(CXT, ul_debugobj(cxt, "child created")); | |
3124 | break; | |
3125 | ||
3126 | default: | |
3127 | rc = mnt_context_add_child(cxt, pid); | |
3128 | break; | |
3129 | } | |
3130 | ||
3131 | return rc; | |
3132 | } | |
3133 | ||
3134 | int mnt_context_wait_for_children(struct libmnt_context *cxt, | |
3135 | int *nchildren, int *nerrs) | |
3136 | { | |
3137 | int i; | |
3138 | ||
3139 | if (!cxt) | |
3140 | return -EINVAL; | |
3141 | ||
3142 | assert(mnt_context_is_parent(cxt)); | |
3143 | ||
3144 | for (i = 0; i < cxt->nchildren; i++) { | |
3145 | pid_t pid = cxt->children[i]; | |
3146 | int rc = 0, ret = 0; | |
3147 | ||
3148 | if (!pid) | |
3149 | continue; | |
3150 | do { | |
3151 | DBG(CXT, ul_debugobj(cxt, | |
3152 | "waiting for child (%d/%d): %d", | |
3153 | i + 1, cxt->nchildren, pid)); | |
3154 | errno = 0; | |
3155 | rc = waitpid(pid, &ret, 0); | |
3156 | ||
3157 | } while (rc == -1 && errno == EINTR); | |
3158 | ||
3159 | if (nchildren) | |
3160 | (*nchildren)++; | |
3161 | ||
3162 | if (rc != -1 && nerrs) { | |
3163 | if (WIFEXITED(ret)) | |
3164 | (*nerrs) += WEXITSTATUS(ret) == 0 ? 0 : 1; | |
3165 | else | |
3166 | (*nerrs)++; | |
3167 | } | |
3168 | cxt->children[i] = 0; | |
3169 | } | |
3170 | ||
3171 | cxt->nchildren = 0; | |
3172 | free(cxt->children); | |
3173 | cxt->children = NULL; | |
3174 | return 0; | |
3175 | } | |
3176 | ||
3177 | static void close_ns(struct libmnt_ns *ns) | |
3178 | { | |
3179 | if (ns->fd == -1) | |
3180 | return; | |
3181 | ||
3182 | close(ns->fd); | |
3183 | ns->fd = -1; | |
3184 | ||
3185 | mnt_unref_cache(ns->cache); | |
3186 | ns->cache = NULL; | |
3187 | } | |
3188 | ||
3189 | /** | |
3190 | * mnt_context_set_target_ns: | |
3191 | * @cxt: mount context | |
3192 | * @path: path to target namespace or NULL | |
3193 | * | |
3194 | * Sets target namespace to namespace represented by @path. If @path is NULL, | |
3195 | * target namespace is cleared. | |
3196 | * | |
3197 | * This function sets errno to ENOSYS and returns error if libmount is | |
3198 | * compiled without namespaces support. | |
3199 | * | |
3200 | * Returns: 0 on success, negative number in case of error. | |
3201 | * | |
3202 | * Since: 2.33 | |
3203 | */ | |
3204 | int mnt_context_set_target_ns(struct libmnt_context *cxt, const char *path) | |
3205 | { | |
3206 | if (!cxt) | |
3207 | return -EINVAL; | |
3208 | ||
3209 | DBG(CXT, ul_debugobj(cxt, "Setting %s as target namespace", path)); | |
3210 | ||
3211 | /* cleanup only */ | |
3212 | if (!path) { | |
3213 | close_ns(&cxt->ns_orig); | |
3214 | close_ns(&cxt->ns_tgt); | |
3215 | return 0; | |
3216 | } | |
3217 | ||
3218 | #ifdef USE_LIBMOUNT_SUPPORT_NAMESPACES | |
3219 | int errsv = 0; | |
3220 | int tmp; | |
3221 | ||
3222 | errno = 0; | |
3223 | ||
3224 | /* open original namespace */ | |
3225 | if (cxt->ns_orig.fd == -1) { | |
3226 | cxt->ns_orig.fd = open("/proc/self/ns/mnt", O_RDONLY | O_CLOEXEC); | |
3227 | if (cxt->ns_orig.fd == -1) | |
3228 | return -errno; | |
3229 | cxt->ns_orig.cache = NULL; | |
3230 | } | |
3231 | ||
3232 | /* open target (wanted) namespace */ | |
3233 | tmp = open(path, O_RDONLY | O_CLOEXEC); | |
3234 | if (tmp == -1) | |
3235 | return -errno; | |
3236 | ||
3237 | /* test whether namespace switching works */ | |
3238 | DBG(CXT, ul_debugobj(cxt, "Trying whether namespace is valid")); | |
3239 | if (setns(tmp, CLONE_NEWNS) | |
3240 | || setns(cxt->ns_orig.fd, CLONE_NEWNS)) { | |
3241 | errsv = errno; | |
3242 | DBG(CXT, ul_debugobj(cxt, "setns(2) failed [errno=%d %m]", errno)); | |
3243 | goto err; | |
3244 | } | |
3245 | ||
3246 | close_ns(&cxt->ns_tgt); | |
3247 | ||
3248 | cxt->ns_tgt.fd = tmp; | |
3249 | cxt->ns_tgt.cache = NULL; | |
3250 | ||
3251 | return 0; | |
3252 | err: | |
3253 | close(tmp); | |
3254 | errno = errsv; | |
3255 | ||
3256 | #else /* ! USE_LIBMOUNT_SUPPORT_NAMESPACES */ | |
3257 | errno = ENOSYS; | |
3258 | #endif | |
3259 | return -errno; | |
3260 | } | |
3261 | ||
3262 | /** | |
3263 | * mnt_context_get_target_ns: | |
3264 | * @cxt: mount context | |
3265 | * | |
3266 | * Returns: pointer to target namespace | |
3267 | * | |
3268 | * Since: 2.33 | |
3269 | */ | |
3270 | struct libmnt_ns *mnt_context_get_target_ns(struct libmnt_context *cxt) | |
3271 | { | |
3272 | return &cxt->ns_tgt; | |
3273 | } | |
3274 | ||
3275 | /** | |
3276 | * mnt_context_get_origin_ns: | |
3277 | * @cxt: mount context | |
3278 | * | |
3279 | * Returns: pointer to original namespace | |
3280 | * | |
3281 | * Since: 2.33 | |
3282 | */ | |
3283 | struct libmnt_ns *mnt_context_get_origin_ns(struct libmnt_context *cxt) | |
3284 | { | |
3285 | return &cxt->ns_orig; | |
3286 | } | |
3287 | ||
3288 | ||
3289 | /** | |
3290 | * mnt_context_switch_ns: | |
3291 | * @cxt: mount context | |
3292 | * @ns: namespace to switch to | |
3293 | * | |
3294 | * Switch to namespace specified by ns | |
3295 | * | |
3296 | * Typical usage: | |
3297 | * <informalexample> | |
3298 | * <programlisting> | |
3299 | * struct libmnt_ns *ns_old; | |
3300 | * ns_old = mnt_context_switch_ns(cxt, mnt_context_get_target_ns(cxt)); | |
3301 | * ... code ... | |
3302 | * mnt_context_switch_ns(cxt, ns_old); | |
3303 | * </programlisting> | |
3304 | * </informalexample> | |
3305 | * | |
3306 | * Returns: pointer to previous namespace or NULL on error | |
3307 | * | |
3308 | * Since: 2.33 | |
3309 | */ | |
3310 | struct libmnt_ns *mnt_context_switch_ns(struct libmnt_context *cxt, struct libmnt_ns *ns) | |
3311 | { | |
3312 | struct libmnt_ns *old = NULL; | |
3313 | ||
3314 | if (!cxt || !ns) | |
3315 | return NULL; | |
3316 | ||
3317 | /* | |
3318 | * If mnt_context_set_target_ns() has never been used than @ns file | |
3319 | * descriptor is -1 and this function is noop. | |
3320 | */ | |
3321 | old = cxt->ns_cur; | |
3322 | if (ns == old || ns->fd == -1) | |
3323 | return old; | |
3324 | ||
3325 | #ifdef USE_LIBMOUNT_SUPPORT_NAMESPACES | |
3326 | /* remember the current cache */ | |
3327 | if (old->cache != cxt->cache) { | |
3328 | mnt_unref_cache(old->cache); | |
3329 | old->cache = cxt->cache; | |
3330 | mnt_ref_cache(old->cache); | |
3331 | } | |
3332 | ||
3333 | /* switch */ | |
3334 | DBG(CXT, ul_debugobj(cxt, "Switching to %s namespace", | |
3335 | ns == mnt_context_get_target_ns(cxt) ? "target" : | |
3336 | ns == mnt_context_get_origin_ns(cxt) ? "original" : "other")); | |
3337 | ||
3338 | if (setns(ns->fd, CLONE_NEWNS)) { | |
3339 | int errsv = errno; | |
3340 | ||
3341 | DBG(CXT, ul_debugobj(cxt, "setns(2) failed [errno=%d %m]", errno)); | |
3342 | errno = errsv; | |
3343 | return NULL; | |
3344 | } | |
3345 | ||
3346 | /* update pointer to the current namespace */ | |
3347 | cxt->ns_cur = ns; | |
3348 | ||
3349 | /* update pointer to the cache */ | |
3350 | mnt_unref_cache(cxt->cache); | |
3351 | cxt->cache = ns->cache; | |
3352 | mnt_ref_cache(cxt->cache); | |
3353 | #endif /* USE_LIBMOUNT_SUPPORT_NAMESPACES */ | |
3354 | ||
3355 | return old; | |
3356 | } | |
3357 | ||
3358 | /** | |
3359 | * mnt_context_switch_origin_ns: | |
3360 | * @cxt: mount context | |
3361 | * | |
3362 | * Switch to original namespace | |
3363 | * | |
3364 | * This is shorthand for | |
3365 | * <informalexample> | |
3366 | * <programlisting> | |
3367 | * mnt_context_switch_ns(cxt, mnt_context_get_origin_ns(cxt)); | |
3368 | * </programlisting> | |
3369 | * </informalexample> | |
3370 | * | |
3371 | * Returns: pointer to previous namespace or NULL on error | |
3372 | * | |
3373 | * Since: 2.33 | |
3374 | */ | |
3375 | struct libmnt_ns *mnt_context_switch_origin_ns(struct libmnt_context *cxt) | |
3376 | { | |
3377 | return mnt_context_switch_ns(cxt, mnt_context_get_origin_ns(cxt)); | |
3378 | } | |
3379 | ||
3380 | /** | |
3381 | * mnt_context_switch_target_ns: | |
3382 | * @cxt: mount context | |
3383 | * | |
3384 | * Switch to target namespace | |
3385 | * | |
3386 | * This is shorthand for | |
3387 | * <informalexample> | |
3388 | * <programlisting> | |
3389 | * mnt_context_switch_ns(cxt, mnt_context_get_target_ns(cxt)); | |
3390 | * </programlisting> | |
3391 | * </informalexample> | |
3392 | * | |
3393 | * Returns: pointer to previous namespace or NULL on error | |
3394 | * | |
3395 | * Since: 2.33 | |
3396 | */ | |
3397 | struct libmnt_ns *mnt_context_switch_target_ns(struct libmnt_context *cxt) | |
3398 | { | |
3399 | return mnt_context_switch_ns(cxt, mnt_context_get_target_ns(cxt)); | |
3400 | } | |
3401 | ||
3402 | ||
3403 | #ifdef TEST_PROGRAM | |
3404 | ||
3405 | static int test_search_helper(struct libmnt_test *ts __attribute__((unused)), | |
3406 | int argc, char *argv[]) | |
3407 | { | |
3408 | struct libmnt_context *cxt; | |
3409 | const char *type; | |
3410 | int rc; | |
3411 | ||
3412 | if (argc < 2) | |
3413 | return -EINVAL; | |
3414 | ||
3415 | cxt = mnt_new_context(); | |
3416 | if (!cxt) | |
3417 | return -ENOMEM; | |
3418 | ||
3419 | type = argv[1]; | |
3420 | ||
3421 | mnt_context_get_fs(cxt); /* just to fill cxt->fs */ | |
3422 | cxt->flags |= MNT_FL_MOUNTFLAGS_MERGED; /* fake */ | |
3423 | ||
3424 | rc = mnt_context_prepare_helper(cxt, "mount", type); | |
3425 | printf("helper is: %s\n", cxt->helper ? cxt->helper : "not found"); | |
3426 | ||
3427 | mnt_free_context(cxt); | |
3428 | return rc; | |
3429 | } | |
3430 | ||
3431 | ||
3432 | static struct libmnt_lock *lock; | |
3433 | ||
3434 | static void lock_fallback(void) | |
3435 | { | |
3436 | if (lock) | |
3437 | mnt_unlock_file(lock); | |
3438 | } | |
3439 | ||
3440 | static int test_mount(struct libmnt_test *ts __attribute__((unused)), | |
3441 | int argc, char *argv[]) | |
3442 | { | |
3443 | int idx = 1, rc = 0; | |
3444 | struct libmnt_context *cxt; | |
3445 | ||
3446 | if (argc < 2) | |
3447 | return -EINVAL; | |
3448 | ||
3449 | cxt = mnt_new_context(); | |
3450 | if (!cxt) | |
3451 | return -ENOMEM; | |
3452 | ||
3453 | if (!strcmp(argv[idx], "-o")) { | |
3454 | mnt_context_set_options(cxt, argv[idx + 1]); | |
3455 | idx += 2; | |
3456 | } | |
3457 | if (!strcmp(argv[idx], "-t")) { | |
3458 | /* TODO: use mnt_context_set_fstype_pattern() */ | |
3459 | mnt_context_set_fstype(cxt, argv[idx + 1]); | |
3460 | idx += 2; | |
3461 | } | |
3462 | ||
3463 | if (argc == idx + 1) | |
3464 | /* mount <mountpoint>|<device> */ | |
3465 | mnt_context_set_target(cxt, argv[idx++]); | |
3466 | ||
3467 | else if (argc == idx + 2) { | |
3468 | /* mount <device> <mountpoint> */ | |
3469 | mnt_context_set_source(cxt, argv[idx++]); | |
3470 | mnt_context_set_target(cxt, argv[idx++]); | |
3471 | } | |
3472 | ||
3473 | /* this is unnecessary! -- libmount is able to internally | |
3474 | * create and manage the lock | |
3475 | */ | |
3476 | lock = mnt_context_get_lock(cxt); | |
3477 | if (lock) | |
3478 | atexit(lock_fallback); | |
3479 | ||
3480 | rc = mnt_context_mount(cxt); | |
3481 | if (rc) | |
3482 | warn("failed to mount"); | |
3483 | else | |
3484 | printf("successfully mounted\n"); | |
3485 | ||
3486 | lock = NULL; /* because we use atexit lock_fallback */ | |
3487 | mnt_free_context(cxt); | |
3488 | return rc; | |
3489 | } | |
3490 | ||
3491 | static int test_umount(struct libmnt_test *ts __attribute__((unused)), | |
3492 | int argc, char *argv[]) | |
3493 | { | |
3494 | int idx = 1, rc = 0; | |
3495 | struct libmnt_context *cxt; | |
3496 | ||
3497 | if (argc < 2) | |
3498 | return -EINVAL; | |
3499 | ||
3500 | cxt = mnt_new_context(); | |
3501 | if (!cxt) | |
3502 | return -ENOMEM; | |
3503 | ||
3504 | if (!strcmp(argv[idx], "-t")) { | |
3505 | mnt_context_set_fstype(cxt, argv[idx + 1]); | |
3506 | idx += 2; | |
3507 | } | |
3508 | ||
3509 | if (!strcmp(argv[idx], "-f")) { | |
3510 | mnt_context_enable_force(cxt, TRUE); | |
3511 | idx++; | |
3512 | } | |
3513 | ||
3514 | if (!strcmp(argv[idx], "-l")) { | |
3515 | mnt_context_enable_lazy(cxt, TRUE); | |
3516 | idx++; | |
3517 | } | |
3518 | ||
3519 | if (!strcmp(argv[idx], "-r")) { | |
3520 | mnt_context_enable_rdonly_umount(cxt, TRUE); | |
3521 | idx++; | |
3522 | } | |
3523 | ||
3524 | if (argc == idx + 1) { | |
3525 | /* mount <mountpoint>|<device> */ | |
3526 | mnt_context_set_target(cxt, argv[idx++]); | |
3527 | } else { | |
3528 | rc = -EINVAL; | |
3529 | goto err; | |
3530 | } | |
3531 | ||
3532 | lock = mnt_context_get_lock(cxt); | |
3533 | if (lock) | |
3534 | atexit(lock_fallback); | |
3535 | ||
3536 | rc = mnt_context_umount(cxt); | |
3537 | if (rc) | |
3538 | printf("failed to umount\n"); | |
3539 | else | |
3540 | printf("successfully umounted\n"); | |
3541 | err: | |
3542 | lock = NULL; /* because we use atexit lock_fallback */ | |
3543 | mnt_free_context(cxt); | |
3544 | return rc; | |
3545 | } | |
3546 | ||
3547 | static int test_flags(struct libmnt_test *ts __attribute__((unused)), | |
3548 | int argc, char *argv[]) | |
3549 | { | |
3550 | int idx = 1, rc = 0; | |
3551 | struct libmnt_context *cxt; | |
3552 | const char *opt = NULL; | |
3553 | unsigned long flags = 0; | |
3554 | ||
3555 | if (argc < 2) | |
3556 | return -EINVAL; | |
3557 | ||
3558 | cxt = mnt_new_context(); | |
3559 | if (!cxt) | |
3560 | return -ENOMEM; | |
3561 | ||
3562 | if (!strcmp(argv[idx], "-o")) { | |
3563 | mnt_context_set_options(cxt, argv[idx + 1]); | |
3564 | idx += 2; | |
3565 | } | |
3566 | ||
3567 | if (argc == idx + 1) | |
3568 | /* mount <mountpoint>|<device> */ | |
3569 | mnt_context_set_target(cxt, argv[idx++]); | |
3570 | ||
3571 | rc = mnt_context_prepare_mount(cxt); | |
3572 | if (rc) | |
3573 | printf("failed to prepare mount %s\n", strerror(-rc)); | |
3574 | ||
3575 | opt = mnt_fs_get_options(cxt->fs); | |
3576 | if (opt) | |
3577 | fprintf(stdout, "options: %s\n", opt); | |
3578 | ||
3579 | mnt_context_get_mflags(cxt, &flags); | |
3580 | fprintf(stdout, "flags: %08lx\n", flags); | |
3581 | ||
3582 | mnt_free_context(cxt); | |
3583 | return rc; | |
3584 | } | |
3585 | ||
3586 | static int test_cxtsync(struct libmnt_test *ts __attribute__((unused)), | |
3587 | int argc, char *argv[]) | |
3588 | { | |
3589 | struct libmnt_context *cxt; | |
3590 | struct libmnt_fs *fs; | |
3591 | unsigned long flags = 0; | |
3592 | int rc; | |
3593 | ||
3594 | if (argc != 4) | |
3595 | return -EINVAL; | |
3596 | ||
3597 | fs = mnt_new_fs(); | |
3598 | if (!fs) | |
3599 | return -ENOMEM; | |
3600 | ||
3601 | rc = mnt_fs_set_options(fs, argv[1]); | |
3602 | if (rc) | |
3603 | return rc; | |
3604 | ||
3605 | cxt = mnt_new_context(); | |
3606 | if (!cxt) | |
3607 | return -ENOMEM; | |
3608 | ||
3609 | rc = mnt_context_set_fs(cxt, fs); | |
3610 | if (rc) | |
3611 | return rc; | |
3612 | ||
3613 | rc = mnt_context_append_options(cxt, argv[2]); | |
3614 | if (rc) | |
3615 | return rc; | |
3616 | ||
3617 | rc = mnt_fs_append_options(fs, argv[3]); | |
3618 | if (rc) | |
3619 | return rc; | |
3620 | ||
3621 | mnt_context_get_mflags(cxt, &flags); | |
3622 | ||
3623 | printf(" fs options: %s\n", mnt_fs_get_options(fs)); | |
3624 | printf("context options: %s\n", mnt_context_get_options(cxt)); | |
3625 | printf(" context mflags: %08lx\n", flags); | |
3626 | ||
3627 | mnt_free_context(cxt); | |
3628 | return 0; | |
3629 | } | |
3630 | ||
3631 | static int test_mountall(struct libmnt_test *ts __attribute__((unused)), | |
3632 | int argc, char *argv[]) | |
3633 | { | |
3634 | struct libmnt_context *cxt; | |
3635 | struct libmnt_iter *itr; | |
3636 | struct libmnt_fs *fs; | |
3637 | int mntrc, ignored, idx = 1; | |
3638 | ||
3639 | cxt = mnt_new_context(); | |
3640 | itr = mnt_new_iter(MNT_ITER_FORWARD); | |
3641 | ||
3642 | if (!cxt || !itr) | |
3643 | return -ENOMEM; | |
3644 | ||
3645 | if (argc > 2) { | |
3646 | if (argv[idx] && !strcmp(argv[idx], "-O")) { | |
3647 | mnt_context_set_options_pattern(cxt, argv[idx + 1]); | |
3648 | idx += 2; | |
3649 | } | |
3650 | if (argv[idx] && !strcmp(argv[idx], "-t")) | |
3651 | mnt_context_set_fstype_pattern(cxt, argv[idx + 1]); | |
3652 | } | |
3653 | ||
3654 | while (mnt_context_next_mount(cxt, itr, &fs, &mntrc, &ignored) == 0) { | |
3655 | ||
3656 | const char *tgt = mnt_fs_get_target(fs); | |
3657 | ||
3658 | if (ignored == 1) | |
3659 | printf("%s: ignored: not match\n", tgt); | |
3660 | else if (ignored == 2) | |
3661 | printf("%s: ignored: already mounted\n", tgt); | |
3662 | ||
3663 | else if (!mnt_context_get_status(cxt)) { | |
3664 | if (mntrc > 0) { | |
3665 | errno = mntrc; | |
3666 | warn("%s: mount failed", tgt); | |
3667 | } else | |
3668 | warnx("%s: mount failed", tgt); | |
3669 | } else | |
3670 | printf("%s: successfully mounted\n", tgt); | |
3671 | } | |
3672 | ||
3673 | mnt_free_context(cxt); | |
3674 | return 0; | |
3675 | } | |
3676 | ||
3677 | ||
3678 | ||
3679 | int main(int argc, char *argv[]) | |
3680 | { | |
3681 | struct libmnt_test tss[] = { | |
3682 | { "--mount", test_mount, "[-o <opts>] [-t <type>] <spec>|<src> <target>" }, | |
3683 | { "--umount", test_umount, "[-t <type>] [-f][-l][-r] <src>|<target>" }, | |
3684 | { "--mount-all", test_mountall, "[-O <pattern>] [-t <pattern] mount all filesystems from fstab" }, | |
3685 | { "--flags", test_flags, "[-o <opts>] <spec>" }, | |
3686 | { "--cxtsync", test_cxtsync, "<fsopts> <cxtopts> <fsopts>" }, | |
3687 | { "--search-helper", test_search_helper, "<fstype>" }, | |
3688 | { NULL }}; | |
3689 | ||
3690 | umask(S_IWGRP|S_IWOTH); /* to be compatible with mount(8) */ | |
3691 | ||
3692 | return mnt_run_test(tss, argc, argv); | |
3693 | } | |
3694 | ||
3695 | #endif /* TEST_PROGRAM */ |