]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libmount/src/context.c
2 * Copyright (C) 2010,2011,2012 Karel Zak <kzak@redhat.com>
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License.
10 * @title: Library high-level context
11 * @short_description: high-level API to mount/umount devices.
15 * struct libmnt_context *cxt = mnt_new_context();
17 * mnt_context_set_options(cxt, "aaa,bbb,ccc=CCC");
18 * mnt_context_set_mflags(cxt, MS_NOATIME|MS_NOEXEC);
19 * mnt_context_set_target(cxt, "/mnt/foo");
21 * if (!mnt_context_mount(cxt))
22 * printf("successfully mounted\n");
23 * mnt_free_context(cxt);
28 * This code is similar to:
30 * mount -o aaa,bbb,ccc=CCC,noatime,noexec /mnt/foo
41 * Returns: newly allocated mount context
43 struct libmnt_context
*mnt_new_context(void)
45 struct libmnt_context
*cxt
;
48 cxt
= calloc(1, sizeof(*cxt
));
52 INIT_LIST_HEAD(&cxt
->addmounts
);
57 mnt_context_reset_status(cxt
);
61 /* if we're really root and aren't running setuid */
62 cxt
->restricted
= (uid_t
) 0 == ruid
&& ruid
== euid
? 0 : 1;
64 DBG(CXT
, mnt_debug_h(cxt
, "----> allocate %s",
65 cxt
->restricted
? "[RESTRICTED]" : ""));
67 mnt_has_regular_mtab(&cxt
->mtab_path
, &cxt
->mtab_writable
);
69 if (!cxt
->mtab_writable
)
70 /* use /run/mount/utab if /etc/mtab is useless */
71 mnt_has_regular_utab(&cxt
->utab_path
, &cxt
->utab_writable
);
80 * Deallocates context struct.
82 void mnt_free_context(struct libmnt_context
*cxt
)
87 mnt_reset_context(cxt
);
89 free(cxt
->fstype_pattern
);
90 free(cxt
->optstr_pattern
);
92 mnt_unref_table(cxt
->fstab
);
93 mnt_unref_cache(cxt
->cache
);
95 mnt_context_clear_loopdev(cxt
);
96 mnt_free_lock(cxt
->lock
);
97 mnt_free_update(cxt
->update
);
101 DBG(CXT
, mnt_debug_h(cxt
, "<---- free"));
107 * @cxt: mount context
109 * Resets all information in the context that is directly related to
110 * the latest mount (spec, source, target, mount options, ...).
112 * The match patterns, cached fstab, cached canonicalized paths and tags and
113 * [e]uid are not reset. You have to use
115 * mnt_context_set_fstab(cxt, NULL);
116 * mnt_context_set_cache(cxt, NULL);
117 * mnt_context_set_fstype_pattern(cxt, NULL);
118 * mnt_context_set_options_pattern(cxt, NULL);
121 * to reset this stuff.
123 * Returns: 0 on success, negative number in case of error.
125 int mnt_reset_context(struct libmnt_context
*cxt
)
132 DBG(CXT
, mnt_debug_h(cxt
, "<---- reset [status=%d] ---->",
133 mnt_context_get_status(cxt
)));
137 mnt_unref_fs(cxt
->fs
);
138 mnt_unref_table(cxt
->mtab
);
139 mnt_unref_table(cxt
->utab
);
142 free(cxt
->orig_user
);
148 cxt
->orig_user
= NULL
;
150 cxt
->user_mountflags
= 0;
151 cxt
->mountdata
= NULL
;
152 cxt
->flags
= MNT_FL_DEFAULT
;
154 /* free additional mounts list */
155 while (!list_empty(&cxt
->addmounts
)) {
156 struct libmnt_addmount
*ad
= list_entry(cxt
->addmounts
.next
,
157 struct libmnt_addmount
,
159 mnt_free_addmount(ad
);
162 mnt_context_reset_status(cxt
);
164 if (cxt
->table_fltrcb
)
165 mnt_context_set_tabfilter(cxt
, NULL
, NULL
);
167 /* restore non-resettable flags */
168 cxt
->flags
|= (fl
& MNT_FL_NOMTAB
);
169 cxt
->flags
|= (fl
& MNT_FL_FAKE
);
170 cxt
->flags
|= (fl
& MNT_FL_SLOPPY
);
171 cxt
->flags
|= (fl
& MNT_FL_VERBOSE
);
172 cxt
->flags
|= (fl
& MNT_FL_NOHELPERS
);
173 cxt
->flags
|= (fl
& MNT_FL_LOOPDEL
);
174 cxt
->flags
|= (fl
& MNT_FL_LAZY
);
175 cxt
->flags
|= (fl
& MNT_FL_FORK
);
176 cxt
->flags
|= (fl
& MNT_FL_FORCE
);
177 cxt
->flags
|= (fl
& MNT_FL_NOCANONICALIZE
);
178 cxt
->flags
|= (fl
& MNT_FL_RDONLY_UMOUNT
);
179 cxt
->flags
|= (fl
& MNT_FL_NOSWAPMATCH
);
184 * mnt_context_reset_status:
187 * Resets mount(2) and mount.type statuses, so mnt_context_do_mount() or
188 * mnt_context_do_umount() could be again called with the same settings.
190 * BE CAREFUL -- after this soft reset the libmount will NOT parse mount
191 * options, evaluate permissions or apply stuff from fstab.
193 * Returns: 0 on success, negative number in case of error.
195 int mnt_context_reset_status(struct libmnt_context
*cxt
)
201 cxt
->syscall_status
= 1; /* means not called yet */
202 cxt
->helper_exec_status
= 1;
203 cxt
->helper_status
= 0;
207 static int set_flag(struct libmnt_context
*cxt
, int flag
, int enable
)
213 DBG(CXT
, mnt_debug_h(cxt
, "enabling flag %04x", flag
));
216 DBG(CXT
, mnt_debug_h(cxt
, "disabling flag %04x", flag
));
223 * mnt_context_is_restricted:
224 * @cxt: mount context
226 * Returns: 0 for an unrestricted mount (user is root), or 1 for non-root mounts
228 int mnt_context_is_restricted(struct libmnt_context
*cxt
)
230 return cxt
->restricted
;
234 * mnt_context_set_optsmode
235 * @cxt: mount context
236 * @mode: MNT_OMODE_* flags
238 * Controls how to use mount optionssource and target paths from fstab/mtab.
240 * @MNT_OMODE_IGNORE: ignore mtab/fstab options
242 * @MNT_OMODE_APPEND: append mtab/fstab options to existing options
244 * @MNT_OMODE_PREPEND: prepend mtab/fstab options to existing options
246 * @MNT_OMODE_REPLACE: replace existing options with options from mtab/fstab
248 * @MNT_OMODE_FORCE: always read mtab/fstab (although source and target are defined)
250 * @MNT_OMODE_FSTAB: read from fstab
252 * @MNT_OMODE_MTAB: read from mtab if fstab not enabled or failed
254 * @MNT_OMODE_NOTAB: do not read fstab/mtab at all
256 * @MNT_OMODE_AUTO: default mode (MNT_OMODE_PREPEND | MNT_OMODE_FSTAB | MNT_OMODE_MTAB)
258 * @MNT_OMODE_USER: default for non-root users (MNT_OMODE_REPLACE | MNT_OMODE_FORCE | MNT_OMODE_FSTAB)
262 * - MNT_OMODE_USER is always used if mount context is in restricted mode
263 * - MNT_OMODE_AUTO is used if nothing else is defined
264 * - the flags are evaluated in this order: MNT_OMODE_NOTAB, MNT_OMODE_FORCE,
265 * MNT_OMODE_FSTAB, MNT_OMODE_MTAB and then the mount options from fstab/mtab
266 * are set according to MNT_OMODE_{IGNORE,APPEND,PREPAND,REPLACE}
268 * Returns: 0 on success, negative number in case of error.
270 int mnt_context_set_optsmode(struct libmnt_context
*cxt
, int mode
)
275 cxt
->optsmode
= mode
;
280 * mnt_context_get_optsmode
281 * @cxt: mount context
283 * Returns: MNT_OMODE_* mask or zero.
286 int mnt_context_get_optsmode(struct libmnt_context
*cxt
)
289 return cxt
->optsmode
;
293 * mnt_context_disable_canonicalize:
294 * @cxt: mount context
295 * @disable: TRUE or FALSE
297 * Enable/disable paths canonicalization and tags evaluation. The libmount context
298 * canonicalizes paths when searching in fstab and when preparing source and target paths
299 * for mount(2) syscall.
301 * This fuction has an effect on the private (within context) fstab instance only
302 * (see mnt_context_set_fstab()). If you want to use an external fstab then you
303 * need to manage your private struct libmnt_cache (see mnt_table_set_cache(fstab,
306 * Returns: 0 on success, negative number in case of error.
308 int mnt_context_disable_canonicalize(struct libmnt_context
*cxt
, int disable
)
310 return set_flag(cxt
, MNT_FL_NOCANONICALIZE
, disable
);
314 * mnt_context_is_nocanonicalize:
315 * @cxt: mount context
317 * Returns: 1 if no-canonicalize mode is enabled or 0.
319 int mnt_context_is_nocanonicalize(struct libmnt_context
*cxt
)
321 return cxt
&& (cxt
->flags
& MNT_FL_NOCANONICALIZE
) ? 1 : 0;
325 * mnt_context_enable_lazy:
326 * @cxt: mount context
327 * @enable: TRUE or FALSE
329 * Enable/disable lazy umount (see umount(8) man page, option -l).
331 * Returns: 0 on success, negative number in case of error.
333 int mnt_context_enable_lazy(struct libmnt_context
*cxt
, int enable
)
335 return set_flag(cxt
, MNT_FL_LAZY
, enable
);
339 * mnt_context_is_lazy:
340 * @cxt: mount context
342 * Returns: 1 if lazy umount is enabled or 0
344 int mnt_context_is_lazy(struct libmnt_context
*cxt
)
346 return cxt
->flags
& MNT_FL_LAZY
? 1 : 0;
350 * mnt_context_enable_fork:
351 * @cxt: mount context
352 * @enable: TRUE or FALSE
354 * Enable/disable fork(2) call in mnt_context_next_mount() (see mount(8) man
357 * Returns: 0 on success, negative number in case of error.
359 int mnt_context_enable_fork(struct libmnt_context
*cxt
, int enable
)
361 return set_flag(cxt
, MNT_FL_FORK
, enable
);
365 * mnt_context_is_fork:
366 * @cxt: mount context
368 * Returns: 1 if fork (mount -F) is enabled or 0
370 int mnt_context_is_fork(struct libmnt_context
*cxt
)
372 return cxt
->flags
& MNT_FL_FORK
? 1 : 0;
376 * mnt_context_is_parent:
377 * @cxt: mount context
379 * Return: 1 if mount -F enabled and the current context is parent, or 0
381 int mnt_context_is_parent(struct libmnt_context
*cxt
)
383 return mnt_context_is_fork(cxt
) && cxt
->pid
== 0;
387 * mnt_context_is_child:
388 * @cxt: mount context
390 * Return: 1 f the current context is child, or 0
392 int mnt_context_is_child(struct libmnt_context
*cxt
)
394 /* See mnt_fork_context(), the for fork flag is always disabled
395 * for children to avoid recursive forking.
397 return !mnt_context_is_fork(cxt
) && cxt
->pid
;
401 * mnt_context_enable_rdonly_umount:
402 * @cxt: mount context
403 * @enable: TRUE or FALSE
405 * Enable/disable read-only remount on failed umount(2)
406 * (see umount(8) man page, option -r).
408 * Returns: 0 on success, negative number in case of error.
410 int mnt_context_enable_rdonly_umount(struct libmnt_context
*cxt
, int enable
)
412 return set_flag(cxt
, MNT_FL_RDONLY_UMOUNT
, enable
);
416 * mnt_context_is_rdonly_umount
417 * @cxt: mount context
419 * See also mnt_context_enable_rdonly_umount() and umount(8) man page,
422 * Returns: 1 if read-only remount failed umount(2) is enables or 0
424 int mnt_context_is_rdonly_umount(struct libmnt_context
*cxt
)
426 return cxt
->flags
& MNT_FL_RDONLY_UMOUNT
? 1 : 0;
430 * mnt_context_disable_helpers:
431 * @cxt: mount context
432 * @disable: TRUE or FALSE
434 * Enable/disable /sbin/[u]mount.* helpers (see mount(8) man page, option -i).
436 * Returns: 0 on success, negative number in case of error.
438 int mnt_context_disable_helpers(struct libmnt_context
*cxt
, int disable
)
440 return set_flag(cxt
, MNT_FL_NOHELPERS
, disable
);
444 * mnt_context_is_nohelpers
445 * @cxt: mount context
447 * Returns: 1 if helpers are disabled (mount -i) or 0
449 int mnt_context_is_nohelpers(struct libmnt_context
*cxt
)
451 return cxt
->flags
& MNT_FL_NOHELPERS
? 1 : 0;
456 * mnt_context_enable_sloppy:
457 * @cxt: mount context
458 * @enable: TRUE or FALSE
460 * Set/unset sloppy mounting (see mount(8) man page, option -s).
462 * Returns: 0 on success, negative number in case of error.
464 int mnt_context_enable_sloppy(struct libmnt_context
*cxt
, int enable
)
466 return set_flag(cxt
, MNT_FL_SLOPPY
, enable
);
470 * mnt_context_is_sloppy:
471 * @cxt: mount context
473 * Returns: 1 if sloppy flag is enabled or 0
475 int mnt_context_is_sloppy(struct libmnt_context
*cxt
)
477 return cxt
->flags
& MNT_FL_SLOPPY
? 1 : 0;
481 * mnt_context_enable_fake:
482 * @cxt: mount context
483 * @enable: TRUE or FALSE
485 * Enable/disable fake mounting (see mount(8) man page, option -f).
487 * Returns: 0 on success, negative number in case of error.
489 int mnt_context_enable_fake(struct libmnt_context
*cxt
, int enable
)
491 return set_flag(cxt
, MNT_FL_FAKE
, enable
);
495 * mnt_context_is_fake:
496 * @cxt: mount context
498 * Returns: 1 if fake flag is enabled or 0
500 int mnt_context_is_fake(struct libmnt_context
*cxt
)
502 return cxt
->flags
& MNT_FL_FAKE
? 1 : 0;
506 * mnt_context_disable_mtab:
507 * @cxt: mount context
508 * @disable: TRUE or FALSE
510 * Disable/enable mtab update (see mount(8) man page, option -n).
512 * Returns: 0 on success, negative number in case of error.
514 int mnt_context_disable_mtab(struct libmnt_context
*cxt
, int disable
)
516 return set_flag(cxt
, MNT_FL_NOMTAB
, disable
);
520 * mnt_context_is_nomtab:
521 * @cxt: mount context
523 * Returns: 1 if no-mtab is enabled or 0
525 int mnt_context_is_nomtab(struct libmnt_context
*cxt
)
527 return cxt
->flags
& MNT_FL_NOMTAB
? 1 : 0;
531 * mnt_context_disable_swapmatch:
532 * @cxt: mount context
533 * @disable: TRUE or FALSE
535 * Disable/enable swap between source and target for mount(8) if only one path
538 * Returns: 0 on success, negative number in case of error.
540 int mnt_context_disable_swapmatch(struct libmnt_context
*cxt
, int disable
)
542 return set_flag(cxt
, MNT_FL_NOSWAPMATCH
, disable
);
546 * mnt_context_is_swapmatch:
547 * @cxt: mount context
549 * Returns: 1 if swap between source and target is allowed (default is 1) or 0.
551 int mnt_context_is_swapmatch(struct libmnt_context
*cxt
)
553 return cxt
->flags
& MNT_FL_NOSWAPMATCH
? 0 : 1;
557 * mnt_context_enable_force:
558 * @cxt: mount context
559 * @enable: TRUE or FALSE
561 * Enable/disable force umounting (see umount(8) man page, option -f).
563 * Returns: 0 on success, negative number in case of error.
565 int mnt_context_enable_force(struct libmnt_context
*cxt
, int enable
)
567 return set_flag(cxt
, MNT_FL_FORCE
, enable
);
571 * mnt_context_is_force
572 * @cxt: mount context
574 * Returns: 1 if force umounting flag is enabled or 0
576 int mnt_context_is_force(struct libmnt_context
*cxt
)
578 return cxt
->flags
& MNT_FL_FORCE
? 1 : 0;
582 * mnt_context_enable_verbose:
583 * @cxt: mount context
584 * @enable: TRUE or FALSE
586 * Enable/disable verbose output (TODO: not implemented yet)
588 * Returns: 0 on success, negative number in case of error.
590 int mnt_context_enable_verbose(struct libmnt_context
*cxt
, int enable
)
592 return set_flag(cxt
, MNT_FL_VERBOSE
, enable
);
596 * mnt_context_is_verbose
597 * @cxt: mount context
599 * Returns: 1 if verbose flag is enabled or 0
601 int mnt_context_is_verbose(struct libmnt_context
*cxt
)
603 return cxt
->flags
& MNT_FL_VERBOSE
? 1 : 0;
607 * mnt_context_enable_loopdel:
608 * @cxt: mount context
609 * @enable: TRUE or FALSE
611 * Enable/disable the loop delete (destroy) after umount (see umount(8), option -d)
613 * Returns: 0 on success, negative number in case of error.
615 int mnt_context_enable_loopdel(struct libmnt_context
*cxt
, int enable
)
617 return set_flag(cxt
, MNT_FL_LOOPDEL
, enable
);
621 * mnt_context_is_loopdel:
622 * @cxt: mount context
624 * Returns: 1 if loop device should be deleted after umount (umount -d) or 0.
626 int mnt_context_is_loopdel(struct libmnt_context
*cxt
)
628 return cxt
->flags
& MNT_FL_LOOPDEL
? 1 : 0;
632 * mnt_context_set_fs:
633 * @cxt: mount context
634 * @fs: filesystem description
636 * The mount context uses private @fs by default. This function allows to
637 * overwrite the private @fs with an external instance. This function
638 * increments @fs reference counter (and deincrement reference counter of the
641 * The @fs will be modified by mnt_context_set_{source,target,options,fstype}
642 * functions, If the @fs is NULL, then all current FS specific settings (source,
643 * target, etc., exclude spec) are reset.
645 * Returns: 0 on success, negative number in case of error.
647 int mnt_context_set_fs(struct libmnt_context
*cxt
, struct libmnt_fs
*fs
)
652 mnt_ref_fs(fs
); /* new */
653 mnt_unref_fs(cxt
->fs
); /* old */
659 * mnt_context_get_fs:
660 * @cxt: mount context
662 * The FS contains the basic description of mountpoint, fs type and so on.
663 * Note that the FS is modified by mnt_context_set_{source,target,options,fstype}
666 * Returns: pointer to FS description or NULL in case of a calloc() error.
668 struct libmnt_fs
*mnt_context_get_fs(struct libmnt_context
*cxt
)
674 cxt
->fs
= mnt_new_fs();
679 * mnt_context_get_fs_userdata:
680 * @cxt: mount context
682 * Returns: pointer to userdata or NULL.
684 void *mnt_context_get_fs_userdata(struct libmnt_context
*cxt
)
687 return cxt
->fs
? mnt_fs_get_userdata(cxt
->fs
) : NULL
;
691 * mnt_context_get_fstab_userdata:
692 * @cxt: mount context
694 * Returns: pointer to userdata or NULL.
696 void *mnt_context_get_fstab_userdata(struct libmnt_context
*cxt
)
699 return cxt
->fstab
? mnt_table_get_userdata(cxt
->fstab
) : NULL
;
703 * mnt_context_get_mtab_userdata:
704 * @cxt: mount context
706 * Returns: pointer to userdata or NULL.
708 void *mnt_context_get_mtab_userdata(struct libmnt_context
*cxt
)
711 return cxt
->mtab
? mnt_table_get_userdata(cxt
->mtab
) : NULL
;
715 * mnt_context_set_source:
716 * @cxt: mount context
717 * @source: mount source (device, directory, UUID, LABEL, ...)
719 * Returns: 0 on success, negative number in case of error.
721 int mnt_context_set_source(struct libmnt_context
*cxt
, const char *source
)
724 return mnt_fs_set_source(mnt_context_get_fs(cxt
), source
);
728 * mnt_context_get_source:
729 * @cxt: mount context
731 * Returns: returns pointer or NULL in case of error or if not set.
733 const char *mnt_context_get_source(struct libmnt_context
*cxt
)
736 return mnt_fs_get_source(mnt_context_get_fs(cxt
));
740 * mnt_context_set_target:
741 * @cxt: mount context
742 * @target: mountpoint
744 * Returns: 0 on success, negative number in case of error.
746 int mnt_context_set_target(struct libmnt_context
*cxt
, const char *target
)
749 return mnt_fs_set_target(mnt_context_get_fs(cxt
), target
);
753 * mnt_context_get_target:
754 * @cxt: mount context
756 * Returns: returns pointer or NULL in case of error or if not set.
758 const char *mnt_context_get_target(struct libmnt_context
*cxt
)
761 return mnt_fs_get_target(mnt_context_get_fs(cxt
));
765 * mnt_context_set_fstype:
766 * @cxt: mount context
767 * @fstype: filesystem type
769 * Note that the @fstype has to be a FS type. For patterns with
770 * comma-separated list of filesystems or for the "nofs" notation, use
771 * mnt_context_set_fstype_pattern().
773 * Returns: 0 on success, negative number in case of error.
775 int mnt_context_set_fstype(struct libmnt_context
*cxt
, const char *fstype
)
778 return mnt_fs_set_fstype(mnt_context_get_fs(cxt
), fstype
);
782 * mnt_context_get_fstype:
783 * @cxt: mount context
785 * Returns: pointer or NULL in case of error or if not set.
787 const char *mnt_context_get_fstype(struct libmnt_context
*cxt
)
790 return mnt_fs_get_fstype(mnt_context_get_fs(cxt
));
794 * mnt_context_set_options:
795 * @cxt: mount context
796 * @optstr: comma delimited mount options
798 * Returns: 0 on success, negative number in case of error.
800 int mnt_context_set_options(struct libmnt_context
*cxt
, const char *optstr
)
803 return mnt_fs_set_options(mnt_context_get_fs(cxt
), optstr
);
807 * mnt_context_append_options:
808 * @cxt: mount context
809 * @optstr: comma delimited mount options
811 * Returns: 0 on success, negative number in case of error.
813 int mnt_context_append_options(struct libmnt_context
*cxt
, const char *optstr
)
816 return mnt_fs_append_options(mnt_context_get_fs(cxt
), optstr
);
820 * mnt_context_get_options:
821 * @cxt: mount context
823 * This function returns mount options set by mnt_context_set_options() or
824 * mnt_context_append_options().
826 * Note that *after* mnt_context_prepare_mount(), the mount options string
827 * may also include options set by mnt_context_set_mflags() or other options
828 * generated by this library.
830 * Returns: pointer or NULL
832 const char *mnt_context_get_options(struct libmnt_context
*cxt
)
835 return mnt_fs_get_options(mnt_context_get_fs(cxt
));
839 * mnt_context_set_fstype_pattern:
840 * @cxt: mount context
841 * @pattern: FS name pattern (or NULL to reset the current setting)
843 * See mount(8), option -t.
845 * Returns: 0 on success, negative number in case of error.
847 int mnt_context_set_fstype_pattern(struct libmnt_context
*cxt
, const char *pattern
)
859 free(cxt
->fstype_pattern
);
860 cxt
->fstype_pattern
= p
;
865 * mnt_context_set_options_pattern:
866 * @cxt: mount context
867 * @pattern: options pattern (or NULL to reset the current setting)
869 * See mount(8), option -O.
871 * Returns: 0 on success, negative number in case of error.
873 int mnt_context_set_options_pattern(struct libmnt_context
*cxt
, const char *pattern
)
885 free(cxt
->optstr_pattern
);
886 cxt
->optstr_pattern
= p
;
891 * mnt_context_set_fstab:
892 * @cxt: mount context
895 * The mount context reads /etc/fstab to the private struct libmnt_table by default.
896 * This function allows to overwrite the private fstab with an external
899 * This function modify the @tb reference counter. This function does not set
900 * the cache for the @tb. You have to explicitly call mnt_table_set_cache(tb,
901 * mnt_context_get_cache(cxt));
903 * The fstab is used read-only and is not modified, it should be possible to
904 * share the fstab between more mount contexts (TODO: test it.)
906 * If the @tb argument is NULL, then the current private fstab instance is
909 * Returns: 0 on success, negative number in case of error.
911 int mnt_context_set_fstab(struct libmnt_context
*cxt
, struct libmnt_table
*tb
)
917 mnt_ref_table(tb
); /* new */
918 mnt_unref_table(cxt
->fstab
); /* old */
925 * mnt_context_get_fstab:
926 * @cxt: mount context
929 * See also mnt_table_parse_fstab() for more details about fstab.
931 * Returns: 0 on success, negative number in case of error.
933 int mnt_context_get_fstab(struct libmnt_context
*cxt
, struct libmnt_table
**tb
)
941 cxt
->fstab
= mnt_new_table();
944 if (cxt
->table_errcb
)
945 mnt_table_set_parser_errcb(cxt
->fstab
, cxt
->table_errcb
);
946 mnt_table_set_cache(cxt
->fstab
, mnt_context_get_cache(cxt
));
947 rc
= mnt_table_parse_fstab(cxt
->fstab
, NULL
);
958 * mnt_context_get_mtab:
959 * @cxt: mount context
962 * See also mnt_table_parse_mtab() for more details about mtab/mountinfo. The
963 * result will be deallocated by mnt_free_context(@cxt).
965 * Returns: 0 on success, negative number in case of error.
967 int mnt_context_get_mtab(struct libmnt_context
*cxt
, struct libmnt_table
**tb
)
975 cxt
->mtab
= mnt_new_table();
979 if (cxt
->table_errcb
)
980 mnt_table_set_parser_errcb(cxt
->mtab
, cxt
->table_errcb
);
981 if (cxt
->table_fltrcb
)
982 mnt_table_set_parser_fltrcb(cxt
->mtab
,
984 cxt
->table_fltrcb_data
);
986 mnt_table_set_cache(cxt
->mtab
, mnt_context_get_cache(cxt
));
988 /* utab already parsed, don't parse it again */
989 rc
= __mnt_table_parse_mtab(cxt
->mtab
,
990 cxt
->mtab_path
, cxt
->utab
);
992 rc
= mnt_table_parse_mtab(cxt
->mtab
, cxt
->mtab_path
);
1000 DBG(CXT
, mnt_debug_h(cxt
, "mtab requested [nents=%d]",
1001 mnt_table_get_nents(cxt
->mtab
)));
1006 * Allows to specify a filter for tab file entries. The filter is called by
1007 * the table parser. Currently used for mtab and utab only.
1009 int mnt_context_set_tabfilter(struct libmnt_context
*cxt
,
1010 int (*fltr
)(struct libmnt_fs
*, void *),
1017 cxt
->table_fltrcb
= fltr
;
1018 cxt
->table_fltrcb_data
= data
;
1021 mnt_table_set_parser_fltrcb(cxt
->mtab
,
1023 cxt
->table_fltrcb_data
);
1025 DBG(CXT
, mnt_debug_h(cxt
, "tabfilter %s", fltr
? "ENABLED!" : "disabled"));
1030 * mnt_context_get_table:
1031 * @cxt: mount context
1032 * @filename: e.g. /proc/self/mountinfo
1033 * @tb: returns the table
1035 * This function allocates a new table and parses the @file. The parser error
1036 * callback and cache for tags and paths is set according to the @cxt setting.
1037 * See also mnt_table_parse_file().
1039 * It's strongly recommended to use the mnt_context_get_mtab() and
1040 * mnt_context_get_fstab() functions for mtab and fstab files. This function
1041 * does not care about LIBMOUNT_* env.variables and does not merge userspace
1044 * The result will NOT be deallocated by mnt_free_context(@cxt).
1046 * Returns: 0 on success, negative number in case of error.
1048 int mnt_context_get_table(struct libmnt_context
*cxt
,
1049 const char *filename
, struct libmnt_table
**tb
)
1058 *tb
= mnt_new_table();
1062 if (cxt
->table_errcb
)
1063 mnt_table_set_parser_errcb(*tb
, cxt
->table_errcb
);
1065 rc
= mnt_table_parse_file(*tb
, filename
);
1067 mnt_unref_table(*tb
);
1071 mnt_table_set_cache(*tb
, mnt_context_get_cache(cxt
));
1076 * mnt_context_set_tables_errcb
1077 * @cxt: mount context
1078 * @cb: pointer to callback function
1080 * The error callback is used for all tab files (e.g. mtab, fstab)
1081 * parsed within the context.
1083 * See also mnt_context_get_mtab(),
1084 * mnt_context_get_fstab(),
1085 * mnt_table_set_parser_errcb().
1087 * Returns: 0 on success, negative number in case of error.
1089 int mnt_context_set_tables_errcb(struct libmnt_context
*cxt
,
1090 int (*cb
)(struct libmnt_table
*tb
, const char *filename
, int line
))
1097 mnt_table_set_parser_errcb(cxt
->mtab
, cb
);
1099 mnt_table_set_parser_errcb(cxt
->fstab
, cb
);
1101 cxt
->table_errcb
= cb
;
1106 * mnt_context_set_cache:
1107 * @cxt: mount context
1108 * @cache: cache instance or nULL
1110 * The mount context maintains a private struct libmnt_cache by default. This
1111 * function allows to overwrite the private cache with an external instance.
1112 * This function increments cache reference counter.
1114 * If the @cache argument is NULL, then the current cache instance is reset.
1115 * This function apply the cache to fstab and mtab instances (if already
1118 * The old cache instance reference counter is de-incremented.
1120 * Returns: 0 on success, negative number in case of error.
1122 int mnt_context_set_cache(struct libmnt_context
*cxt
, struct libmnt_cache
*cache
)
1127 mnt_ref_cache(cache
); /* new */
1128 mnt_unref_cache(cxt
->cache
); /* old */
1133 mnt_table_set_cache(cxt
->mtab
, cache
);
1135 mnt_table_set_cache(cxt
->fstab
, cache
);
1141 * mnt_context_get_cache
1142 * @cxt: mount context
1144 * See also mnt_context_set_cache().
1146 * Returns: pointer to cache or NULL if canonicalization is disabled.
1148 struct libmnt_cache
*mnt_context_get_cache(struct libmnt_context
*cxt
)
1151 if (!cxt
|| mnt_context_is_nocanonicalize(cxt
))
1155 struct libmnt_cache
*cache
= mnt_new_cache();
1156 mnt_context_set_cache(cxt
, cache
);
1157 mnt_unref_cache(cache
);
1163 * mnt_context_set_passwd_cb:
1164 * @cxt: mount context
1165 * @get: callback to get password
1166 * @release: callback to release (delallocate) password
1168 * Sets callbacks for encryption password (e.g encrypted loopdev). This
1169 * function is deprecated (encrypted loops are no longer supported).
1171 * Returns: 0 on success, negative number in case of error.
1173 int mnt_context_set_passwd_cb(struct libmnt_context
*cxt
,
1174 char *(*get
)(struct libmnt_context
*),
1175 void (*release
)(struct libmnt_context
*, char *))
1180 cxt
->pwd_get_cb
= get
;
1181 cxt
->pwd_release_cb
= release
;
1186 * mnt_context_get_lock:
1187 * @cxt: mount context
1189 * The libmount applications don't have to care about mtab locking, but with a
1190 * small exception: the application has to be able to remove the lock file when
1191 * interrupted by signal or signals have to be ignored when the lock is locked.
1193 * The default behavior is to ignore all signals (except SIGALRM and
1194 * SIGTRAP for mtab udate) when the lock is locked. If this behavior
1195 * is unacceptable, then use:
1197 * lc = mnt_context_get_lock(cxt);
1199 * mnt_lock_block_signals(lc, FALSE);
1201 * and don't forget to call mnt_unlock_file(lc) before exit.
1203 * Returns: pointer to lock struct or NULL.
1205 struct libmnt_lock
*mnt_context_get_lock(struct libmnt_context
*cxt
)
1209 * DON'T call this function within libmount, it will always allocate
1210 * the lock. The mnt_update_* functions are able to allocate the lock
1211 * only when mtab/utab update is really necessary.
1213 if (!cxt
|| mnt_context_is_nomtab(cxt
))
1217 cxt
->lock
= mnt_new_lock(cxt
->mtab_writable
?
1218 cxt
->mtab_path
: cxt
->utab_path
, 0);
1220 mnt_lock_block_signals(cxt
->lock
, TRUE
);
1226 * mnt_context_set_mflags:
1227 * @cxt: mount context
1228 * @flags: mount(2) flags (MS_* flags)
1230 * Sets mount flags (see mount(2) man page).
1232 * Note that mount context allows to define mount options by mount flags. It
1233 * means you can for example use
1235 * mnt_context_set_mflags(cxt, MS_NOEXEC | MS_NOSUID);
1239 * mnt_context_set_options(cxt, "noexec,nosuid");
1241 * both of these calls have the same effect.
1243 * Returns: 0 on success, negative number in case of error.
1245 int mnt_context_set_mflags(struct libmnt_context
*cxt
, unsigned long flags
)
1251 cxt
->mountflags
= flags
;
1253 if ((cxt
->flags
& MNT_FL_MOUNTOPTS_FIXED
) && cxt
->fs
)
1255 * the final mount options are already generated, refresh...
1257 return mnt_optstr_apply_flags(
1258 &cxt
->fs
->vfs_optstr
,
1260 mnt_get_builtin_optmap(MNT_LINUX_MAP
));
1266 * mnt_context_get_mflags:
1267 * @cxt: mount context
1268 * @flags: returns MS_* mount flags
1270 * Converts mount options string to MS_* flags and bitewise-OR the result with
1271 * the already defined flags (see mnt_context_set_mflags()).
1273 * Returns: 0 on success, negative number in case of error.
1275 int mnt_context_get_mflags(struct libmnt_context
*cxt
, unsigned long *flags
)
1278 struct list_head
*p
;
1286 if (!(cxt
->flags
& MNT_FL_MOUNTFLAGS_MERGED
) && cxt
->fs
) {
1287 const char *o
= mnt_fs_get_options(cxt
->fs
);
1289 rc
= mnt_optstr_get_flags(o
, flags
,
1290 mnt_get_builtin_optmap(MNT_LINUX_MAP
));
1293 list_for_each(p
, &cxt
->addmounts
) {
1294 struct libmnt_addmount
*ad
=
1295 list_entry(p
, struct libmnt_addmount
, mounts
);
1297 *flags
|= ad
->mountflags
;
1301 *flags
|= cxt
->mountflags
;
1306 * mnt_context_set_user_mflags:
1307 * @cxt: mount context
1308 * @flags: mount(2) flags (MNT_MS_* flags, e.g. MNT_MS_LOOP)
1310 * Sets userspace mount flags.
1312 * See also notes for mnt_context_set_mflags().
1314 * Returns: 0 on success, negative number in case of error.
1316 int mnt_context_set_user_mflags(struct libmnt_context
*cxt
, unsigned long flags
)
1321 cxt
->user_mountflags
= flags
;
1326 * mnt_context_get_user_mflags:
1327 * @cxt: mount context
1328 * @flags: returns mount flags
1330 * Converts mount options string to MNT_MS_* flags and bitewise-OR the result
1331 * with the already defined flags (see mnt_context_set_user_mflags()).
1333 * Returns: 0 on success, negative number in case of error.
1335 int mnt_context_get_user_mflags(struct libmnt_context
*cxt
, unsigned long *flags
)
1345 if (!(cxt
->flags
& MNT_FL_MOUNTFLAGS_MERGED
) && cxt
->fs
) {
1346 const char *o
= mnt_fs_get_user_options(cxt
->fs
);
1348 rc
= mnt_optstr_get_flags(o
, flags
,
1349 mnt_get_builtin_optmap(MNT_USERSPACE_MAP
));
1352 *flags
|= cxt
->user_mountflags
;
1357 * mnt_context_set_mountdata:
1358 * @cxt: mount context
1359 * @data: mount(2) data
1361 * The mount context generates mountdata from mount options by default. This
1362 * function allows to overwrite this behavior, and @data will be used instead
1365 * The libmount does not deallocate the data by mnt_free_context(). Note that
1366 * NULL is also valid mount data.
1368 * Returns: 0 on success, negative number in case of error.
1370 int mnt_context_set_mountdata(struct libmnt_context
*cxt
, void *data
)
1375 cxt
->mountdata
= data
;
1376 cxt
->flags
|= MNT_FL_MOUNTDATA
;
1381 * Translates LABEL/UUID/path to mountable path
1383 int mnt_context_prepare_srcpath(struct libmnt_context
*cxt
)
1385 const char *path
= NULL
;
1386 struct libmnt_cache
*cache
;
1387 const char *t
, *v
, *src
;
1392 assert((cxt
->flags
& MNT_FL_MOUNTFLAGS_MERGED
));
1394 if (!cxt
|| !cxt
->fs
)
1397 DBG(CXT
, mnt_debug_h(cxt
, "preparing source path"));
1399 src
= mnt_fs_get_source(cxt
->fs
);
1401 if (!src
&& mnt_context_propagation_only(cxt
))
1402 /* mount --make-{shared,private,...} */
1403 return mnt_fs_set_source(cxt
->fs
, "none");
1405 /* ignore filesystems without source or filesystems
1406 * where the source is a quasi-path (//foo/bar)
1408 if (!src
|| mnt_fs_is_netfs(cxt
->fs
))
1411 DBG(CXT
, mnt_debug_h(cxt
, "srcpath '%s'", src
));
1413 cache
= mnt_context_get_cache(cxt
);
1415 if (!mnt_fs_get_tag(cxt
->fs
, &t
, &v
)) {
1417 * Source is TAG (evaluate)
1420 path
= mnt_resolve_tag(t
, v
, cache
);
1422 rc
= path
? mnt_fs_set_source(cxt
->fs
, path
) : -MNT_ERR_NOSOURCE
;
1424 } else if (cache
&& !mnt_fs_is_pseudofs(cxt
->fs
)) {
1426 * Source is PATH (canonicalize)
1428 path
= mnt_resolve_path(src
, cache
);
1429 if (path
&& strcmp(path
, src
))
1430 rc
= mnt_fs_set_source(cxt
->fs
, path
);
1434 DBG(CXT
, mnt_debug_h(cxt
, "failed to prepare srcpath [rc=%d]", rc
));
1441 if ((cxt
->mountflags
& (MS_BIND
| MS_MOVE
| MS_REMOUNT
))
1442 || mnt_fs_is_pseudofs(cxt
->fs
)) {
1443 DBG(CXT
, mnt_debug_h(cxt
, "REMOUNT/BIND/MOVE/pseudo FS source: %s", path
));
1448 * Initialize loop device
1450 if (mnt_context_is_loopdev(cxt
)) {
1451 rc
= mnt_context_setup_loopdev(cxt
);
1456 DBG(CXT
, mnt_debug_h(cxt
, "final srcpath '%s'",
1457 mnt_fs_get_source(cxt
->fs
)));
1461 /* create a mountpoint if x-mount.mkdir[=<mode>] specified */
1462 static int mkdir_target(const char *tgt
, struct libmnt_fs
*fs
)
1473 if (mnt_optstr_get_option(fs
->user_optstr
, "x-mount.mkdir", &mstr
, &mstr_sz
) != 0)
1475 if (stat(tgt
, &st
) == 0)
1478 if (mstr
&& mstr_sz
) {
1482 mode
= strtol(mstr
, &end
, 8);
1484 if (errno
|| !end
|| mstr
+ mstr_sz
!= end
) {
1485 DBG(CXT
, mnt_debug("failed to parse mkdir mode '%s'", mstr
));
1486 return -MNT_ERR_MOUNTOPT
;
1491 mode
= S_IRWXU
| /* 0755 */
1495 rc
= mkdir_p(tgt
, mode
);
1497 DBG(CXT
, mnt_debug("mkdir %s failed: %m", tgt
));
1502 int mnt_context_prepare_target(struct libmnt_context
*cxt
)
1505 struct libmnt_cache
*cache
;
1510 assert((cxt
->flags
& MNT_FL_MOUNTFLAGS_MERGED
));
1512 if (!cxt
|| !cxt
->fs
)
1515 DBG(CXT
, mnt_debug_h(cxt
, "preparing target path"));
1517 tgt
= mnt_fs_get_target(cxt
->fs
);
1522 if (cxt
->action
== MNT_ACT_MOUNT
1523 && !mnt_context_is_restricted(cxt
)
1524 && cxt
->user_mountflags
& MNT_MS_XCOMMENT
) {
1526 rc
= mkdir_target(tgt
, cxt
->fs
);
1528 return rc
; /* mkdir or parse error */
1531 /* canonicalize the path */
1532 cache
= mnt_context_get_cache(cxt
);
1534 char *path
= mnt_resolve_path(tgt
, cache
);
1535 if (path
&& strcmp(path
, tgt
) != 0)
1536 rc
= mnt_fs_set_target(cxt
->fs
, path
);
1540 DBG(CXT
, mnt_debug_h(cxt
, "failed to prepare target '%s'", tgt
));
1542 DBG(CXT
, mnt_debug_h(cxt
, "final target '%s'",
1543 mnt_fs_get_target(cxt
->fs
)));
1548 * It's usually no error when we're not able to detect the filesystem type -- we
1549 * will try to use the types from /{etc,proc}/filesystems.
1551 int mnt_context_guess_fstype(struct libmnt_context
*cxt
)
1559 assert((cxt
->flags
& MNT_FL_MOUNTFLAGS_MERGED
));
1561 if (!cxt
|| !cxt
->fs
)
1564 if ((cxt
->mountflags
& (MS_BIND
| MS_MOVE
))
1565 || mnt_context_propagation_only(cxt
))
1568 type
= (char *) mnt_fs_get_fstype(cxt
->fs
);
1569 if (type
&& !strcmp(type
, "auto")) {
1570 mnt_fs_set_fstype(cxt
->fs
, NULL
);
1576 if (cxt
->flags
& MS_REMOUNT
)
1578 if (cxt
->fstype_pattern
)
1581 dev
= mnt_fs_get_srcpath(cxt
->fs
);
1585 if (access(dev
, F_OK
) == 0) {
1586 struct libmnt_cache
*cache
= mnt_context_get_cache(cxt
);
1589 type
= mnt_get_fstype(dev
, &ambi
, cache
);
1591 rc
= mnt_fs_set_fstype(cxt
->fs
, type
);
1593 free(type
); /* type is not cached */
1596 rc
= -MNT_ERR_AMBIFS
;
1598 DBG(CXT
, mnt_debug_h(cxt
, "access(%s) failed [%m]", dev
));
1599 if (strchr(dev
, ':') != NULL
)
1600 rc
= mnt_fs_set_fstype(cxt
->fs
, "nfs");
1601 else if (!strncmp(dev
, "//", 2))
1602 rc
= mnt_fs_set_fstype(cxt
->fs
, "cifs");
1606 DBG(CXT
, mnt_debug_h(cxt
, "FS type: %s [rc=%d]",
1607 mnt_fs_get_fstype(cxt
->fs
), rc
));
1610 return mnt_fs_set_fstype(cxt
->fs
, "none");
1614 * The default is to use fstype from cxt->fs, this could be overwritten by
1615 * @type. The @act is MNT_ACT_{MOUNT,UMOUNT}.
1617 * Returns: 0 on success or negative number in case of error. Note that success
1618 * does not mean that there is any usable helper, you have to check cxt->helper.
1620 int mnt_context_prepare_helper(struct libmnt_context
*cxt
, const char *name
,
1623 char search_path
[] = FS_SEARCH_PATH
; /* from config.h */
1624 char *p
= NULL
, *path
;
1628 assert((cxt
->flags
& MNT_FL_MOUNTFLAGS_MERGED
));
1631 type
= mnt_fs_get_fstype(cxt
->fs
);
1633 if (type
&& strchr(type
, ','))
1634 return 0; /* type is fstype pattern */
1636 if (mnt_context_is_nohelpers(cxt
)
1638 || !strcmp(type
, "none")
1639 || strstr(type
, "/..") /* don't try to smuggle path */
1640 || mnt_fs_is_swaparea(cxt
->fs
))
1643 path
= strtok_r(search_path
, ":", &p
);
1645 char helper
[PATH_MAX
];
1649 rc
= snprintf(helper
, sizeof(helper
), "%s/%s.%s",
1651 path
= strtok_r(NULL
, ":", &p
);
1653 if (rc
< 0 || (size_t) rc
>= sizeof(helper
))
1656 rc
= stat(helper
, &st
);
1657 if (rc
== -1 && errno
== ENOENT
&& strchr(type
, '.')) {
1658 /* If type ends with ".subtype" try without it */
1659 char *hs
= strrchr(helper
, '.');
1662 rc
= stat(helper
, &st
);
1665 DBG(CXT
, mnt_debug_h(cxt
, "%-25s ... %s", helper
,
1666 rc
? "not found" : "found"));
1671 cxt
->helper
= strdup(helper
);
1680 int mnt_context_merge_mflags(struct libmnt_context
*cxt
)
1682 unsigned long fl
= 0;
1687 DBG(CXT
, mnt_debug_h(cxt
, "merging mount flags"));
1689 rc
= mnt_context_get_mflags(cxt
, &fl
);
1692 cxt
->mountflags
= fl
;
1695 rc
= mnt_context_get_user_mflags(cxt
, &fl
);
1698 cxt
->user_mountflags
= fl
;
1700 DBG(CXT
, mnt_debug_h(cxt
, "final flags: VFS=%08lx user=%08lx",
1701 cxt
->mountflags
, cxt
->user_mountflags
));
1703 cxt
->flags
|= MNT_FL_MOUNTFLAGS_MERGED
;
1708 * Prepare /etc/mtab or /run/mount/utab
1710 int mnt_context_prepare_update(struct libmnt_context
*cxt
)
1717 assert(cxt
->action
);
1718 assert((cxt
->flags
& MNT_FL_MOUNTFLAGS_MERGED
));
1720 DBG(CXT
, mnt_debug_h(cxt
, "prepare update"));
1722 if (mnt_context_propagation_only(cxt
)) {
1723 DBG(CXT
, mnt_debug_h(cxt
, "skip update: only MS_PROPAGATION"));
1727 target
= mnt_fs_get_target(cxt
->fs
);
1729 if (cxt
->action
== MNT_ACT_UMOUNT
&& target
&& !strcmp(target
, "/"))
1730 /* Don't try to touch mtab if umounting root FS */
1731 mnt_context_disable_mtab(cxt
, TRUE
);
1733 if (mnt_context_is_nomtab(cxt
)) {
1734 DBG(CXT
, mnt_debug_h(cxt
, "skip update: NOMTAB flag"));
1737 if (!cxt
->mtab_writable
&& !cxt
->utab_writable
) {
1738 DBG(CXT
, mnt_debug_h(cxt
, "skip update: no writable destination"));
1741 /* 0 = success, 1 = not called yet */
1742 if (cxt
->syscall_status
!= 1 && cxt
->syscall_status
!= 0) {
1743 DBG(CXT
, mnt_debug_h(cxt
,
1744 "skip update: syscall failed [status=%d]",
1745 cxt
->syscall_status
));
1750 const char *name
= cxt
->mtab_writable
? cxt
->mtab_path
: cxt
->utab_path
;
1752 if (cxt
->action
== MNT_ACT_UMOUNT
&& is_file_empty(name
)) {
1753 DBG(CXT
, mnt_debug_h(cxt
,
1754 "skip update: umount, no table"));
1758 cxt
->update
= mnt_new_update();
1762 mnt_update_set_filename(cxt
->update
, name
, !cxt
->mtab_writable
);
1765 if (cxt
->action
== MNT_ACT_UMOUNT
)
1766 rc
= mnt_update_set_fs(cxt
->update
, cxt
->mountflags
,
1767 mnt_context_get_target(cxt
), NULL
);
1769 rc
= mnt_update_set_fs(cxt
->update
, cxt
->mountflags
,
1772 return rc
< 0 ? rc
: 0;
1775 int mnt_context_update_tabs(struct libmnt_context
*cxt
)
1781 if (mnt_context_is_nomtab(cxt
)) {
1782 DBG(CXT
, mnt_debug_h(cxt
, "don't update: NOMTAB flag"));
1785 if (!cxt
->update
|| !mnt_update_is_ready(cxt
->update
)) {
1786 DBG(CXT
, mnt_debug_h(cxt
, "don't update: no update prepared"));
1790 /* check utab update when external helper executed */
1791 if (mnt_context_helper_executed(cxt
)
1792 && mnt_context_get_helper_status(cxt
) == 0
1793 && cxt
->utab_writable
) {
1795 if (mnt_update_already_done(cxt
->update
, cxt
->lock
)) {
1796 DBG(CXT
, mnt_debug_h(cxt
, "don't update: error evaluate or already updated"));
1799 } else if (cxt
->helper
) {
1800 DBG(CXT
, mnt_debug_h(cxt
, "don't update: external helper"));
1804 if (cxt
->syscall_status
!= 0
1805 && !(mnt_context_helper_executed(cxt
) &&
1806 mnt_context_get_helper_status(cxt
) == 0)) {
1808 DBG(CXT
, mnt_debug_h(cxt
, "don't update: syscall/helper failed/not called"));
1812 fl
= mnt_update_get_mflags(cxt
->update
);
1813 if ((cxt
->mountflags
& MS_RDONLY
) != (fl
& MS_RDONLY
))
1815 * fix MS_RDONLY in options
1817 mnt_update_force_rdonly(cxt
->update
,
1818 cxt
->mountflags
& MS_RDONLY
);
1820 return mnt_update_table(cxt
->update
, cxt
->lock
);
1823 static int apply_table(struct libmnt_context
*cxt
, struct libmnt_table
*tb
,
1826 struct libmnt_fs
*fs
= NULL
;
1827 const char *src
= NULL
, *tgt
= NULL
;
1836 src
= mnt_fs_get_source(cxt
->fs
);
1837 tgt
= mnt_fs_get_target(cxt
->fs
);
1840 fs
= mnt_table_find_pair(tb
, src
, tgt
, direction
);
1843 fs
= mnt_table_find_source(tb
, src
, direction
);
1845 fs
= mnt_table_find_target(tb
, tgt
, direction
);
1847 if (!fs
&& mnt_context_is_swapmatch(cxt
)) {
1848 /* swap source and target (if @src is not LABEL/UUID),
1853 * the path could be a mountpoint as well as a source (for
1854 * example bind mount, symlink to a device, ...).
1856 if (src
&& !mnt_fs_get_tag(cxt
->fs
, NULL
, NULL
))
1857 fs
= mnt_table_find_target(tb
, src
, direction
);
1859 fs
= mnt_table_find_source(tb
, tgt
, direction
);
1864 return -MNT_ERR_NOFSTAB
; /* not found */
1866 DBG(CXT
, mnt_debug_h(cxt
, "apply entry:"));
1867 DBG(CXT
, mnt_fs_print_debug(fs
, stderr
));
1869 /* copy from tab to our FS description
1871 rc
= mnt_fs_set_source(cxt
->fs
, mnt_fs_get_source(fs
));
1873 rc
= mnt_fs_set_target(cxt
->fs
, mnt_fs_get_target(fs
));
1875 if (!rc
&& !mnt_fs_get_fstype(cxt
->fs
))
1876 rc
= mnt_fs_set_fstype(cxt
->fs
, mnt_fs_get_fstype(fs
));
1881 if (cxt
->optsmode
& MNT_OMODE_IGNORE
)
1883 else if (cxt
->optsmode
& MNT_OMODE_REPLACE
)
1884 rc
= mnt_fs_set_options(cxt
->fs
, mnt_fs_get_options(fs
));
1886 else if (cxt
->optsmode
& MNT_OMODE_APPEND
)
1887 rc
= mnt_fs_append_options(cxt
->fs
, mnt_fs_get_options(fs
));
1889 else if (cxt
->optsmode
& MNT_OMODE_PREPEND
)
1890 rc
= mnt_fs_prepend_options(cxt
->fs
, mnt_fs_get_options(fs
));
1893 cxt
->flags
|= MNT_FL_TAB_APPLIED
;
1898 * mnt_context_apply_fstab:
1899 * @cxt: mount context
1901 * This function is optional.
1903 * Returns: 0 on success, negative number in case of error.
1905 int mnt_context_apply_fstab(struct libmnt_context
*cxt
)
1908 struct libmnt_table
*tab
= NULL
;
1909 const char *src
= NULL
, *tgt
= NULL
;
1917 if (mnt_context_tab_applied(cxt
)) /* already applied */
1920 if (mnt_context_is_restricted(cxt
)) {
1921 DBG(CXT
, mnt_debug_h(cxt
, "force fstab usage for non-root users!"));
1922 cxt
->optsmode
= MNT_OMODE_USER
;
1923 } else if (cxt
->optsmode
== 0) {
1924 DBG(CXT
, mnt_debug_h(cxt
, "use default optsmode"));
1925 cxt
->optsmode
= MNT_OMODE_AUTO
;
1926 } else if (cxt
->optsmode
& MNT_OMODE_NOTAB
) {
1927 cxt
->optsmode
&= ~MNT_OMODE_FSTAB
;
1928 cxt
->optsmode
&= ~MNT_OMODE_MTAB
;
1929 cxt
->optsmode
&= ~MNT_OMODE_FORCE
;
1933 src
= mnt_fs_get_source(cxt
->fs
);
1934 tgt
= mnt_fs_get_target(cxt
->fs
);
1937 DBG(CXT
, mnt_debug_h(cxt
, "OPTSMODE: ignore=%d, append=%d, prepend=%d, "
1938 "replace=%d, force=%d, fstab=%d, mtab=%d",
1939 cxt
->optsmode
& MNT_OMODE_IGNORE
? 1 : 0,
1940 cxt
->optsmode
& MNT_OMODE_APPEND
? 1 : 0,
1941 cxt
->optsmode
& MNT_OMODE_PREPEND
? 1 : 0,
1942 cxt
->optsmode
& MNT_OMODE_REPLACE
? 1 : 0,
1943 cxt
->optsmode
& MNT_OMODE_FORCE
? 1 : 0,
1944 cxt
->optsmode
& MNT_OMODE_FSTAB
? 1 : 0,
1945 cxt
->optsmode
& MNT_OMODE_MTAB
? 1 : 0));
1947 /* fstab is not required if source and target are specified */
1948 if (src
&& tgt
&& !(cxt
->optsmode
& MNT_OMODE_FORCE
)) {
1949 DBG(CXT
, mnt_debug_h(cxt
, "fstab not required -- skip"));
1954 && !(cxt
->optsmode
& MNT_OMODE_FSTAB
)
1955 && !(cxt
->optsmode
& MNT_OMODE_MTAB
)) {
1956 DBG(CXT
, mnt_debug_h(cxt
, "only target; fstab/mtab not required "
1957 "-- skip, probably MS_PROPAGATION"));
1961 DBG(CXT
, mnt_debug_h(cxt
,
1962 "trying to apply fstab (src=%s, target=%s)", src
, tgt
));
1964 /* let's initialize cxt->fs */
1965 ignore_result( mnt_context_get_fs(cxt
) );
1968 if (cxt
->optsmode
& MNT_OMODE_FSTAB
) {
1969 rc
= mnt_context_get_fstab(cxt
, &tab
);
1971 rc
= apply_table(cxt
, tab
, MNT_ITER_FORWARD
);
1975 if (rc
< 0 && (cxt
->optsmode
& MNT_OMODE_MTAB
)) {
1976 DBG(CXT
, mnt_debug_h(cxt
, "trying to apply from mtab"));
1977 rc
= mnt_context_get_mtab(cxt
, &tab
);
1979 rc
= apply_table(cxt
, tab
, MNT_ITER_BACKWARD
);
1982 DBG(CXT
, mnt_debug_h(cxt
, "failed to find entry in fstab/mtab"));
1987 * mnt_context_tab_applied:
1988 * @cxt: mount context
1990 * Returns: 1 if fstab (or mtab) has been applied to the context, or 0.
1992 int mnt_context_tab_applied(struct libmnt_context
*cxt
)
1995 return cxt
->flags
& MNT_FL_TAB_APPLIED
;
1999 * This is not a public function!
2001 * Returns 1 if *only propagation flags* change is requested.
2003 int mnt_context_propagation_only(struct libmnt_context
*cxt
)
2008 if (cxt
->action
!= MNT_ACT_MOUNT
)
2011 /* has to be called after context_mount.c: fix_opts() */
2012 assert((cxt
->flags
& MNT_FL_MOUNTOPTS_FIXED
));
2014 /* all propagation mounts are in cxt->addmount */
2015 return !list_empty(&cxt
->addmounts
)
2016 && (cxt
->mountflags
== 0 || cxt
->mountflags
== MS_SILENT
)
2018 && (!cxt
->fs
->fstype
|| strcmp(cxt
->fs
->fstype
, "none") == 0)
2019 && (!cxt
->fs
->source
|| strcmp(cxt
->fs
->source
, "none") == 0);
2023 * mnt_context_get_status:
2024 * @cxt: mount context
2026 * Global libmount status.
2028 * The real exit code of the mount.type helper has to be tested by
2029 * mnt_context_get_helper_status(). The mnt_context_get_status() only informs
2030 * that exec() has been successful.
2032 * Returns: 1 if mount.type or mount(2) syscall has been successfully called.
2034 int mnt_context_get_status(struct libmnt_context
*cxt
)
2037 return !cxt
->syscall_status
|| !cxt
->helper_exec_status
;
2041 * mnt_context_helper_executed:
2042 * @cxt: mount context
2044 * Returns: 1 if mount.type helper has been executed, or 0.
2046 int mnt_context_helper_executed(struct libmnt_context
*cxt
)
2049 return cxt
->helper_exec_status
!= 1;
2053 * mnt_context_get_helper_status:
2054 * @cxt: mount context
2056 * Return: mount.type helper exit status, result is reliable only if
2057 * mnt_context_helper_executed() returns 1.
2059 int mnt_context_get_helper_status(struct libmnt_context
*cxt
)
2062 return cxt
->helper_status
;
2066 * mnt_context_syscall_called:
2067 * @cxt: mount context
2069 * Returns: 1 if mount(2) syscall has been called, or 0.
2071 int mnt_context_syscall_called(struct libmnt_context
*cxt
)
2074 return cxt
->syscall_status
!= 1;
2078 * mnt_context_get_syscall_errno:
2079 * @cxt: mount context
2081 * The result from this function is reliable only if
2082 * mnt_context_syscall_called() returns 1.
2084 * Returns: mount(2) errno if the syscall failed or 0.
2086 int mnt_context_get_syscall_errno(struct libmnt_context
*cxt
)
2089 if (cxt
->syscall_status
< 0)
2090 return -cxt
->syscall_status
;
2095 * mnt_context_set_syscall_status:
2096 * @cxt: mount context
2097 * @status: mount(2) status
2099 * The @status should be 0 on success, or negative number on error (-errno).
2101 * This function should only be used if the [u]mount(2) syscall is NOT called by
2104 * Returns: 0 or negative number in case of error.
2106 int mnt_context_set_syscall_status(struct libmnt_context
*cxt
, int status
)
2112 DBG(CXT
, mnt_debug_h(cxt
, "syscall status set to: %d", status
));
2113 cxt
->syscall_status
= status
;
2118 * mnt_context_strerror
2121 * @bufsiz: size of the buffer
2123 * Not implemented yet.
2125 * Returns: 0 or negative number in case of error.
2127 int mnt_context_strerror(struct libmnt_context
*cxt
__attribute__((__unused__
)),
2128 char *buf
__attribute__((__unused__
)),
2129 size_t bufsiz
__attribute__((__unused__
)))
2131 /* TODO: based on cxt->syscall_errno or cxt->helper_status */
2136 * mnt_context_init_helper
2137 * @cxt: mount context
2138 * @action: MNT_ACT_{UMOUNT,MOUNT}
2139 * @flags: not used now
2141 * This function informs libmount that used from [u]mount.type helper.
2143 * The function also calls mnt_context_disable_helpers() to avoid recursive
2144 * mount.type helpers calling. It you really want to call another
2145 * mount.type helper from your helper, then you have to explicitly enable this
2148 * mnt_context_disable_helpers(cxt, FALSE);
2150 * Returns: 0 on success, negative number in case of error.
2152 int mnt_context_init_helper(struct libmnt_context
*cxt
, int action
,
2153 int flags
__attribute__((__unused__
)))
2159 rc
= mnt_context_disable_helpers(cxt
, TRUE
);
2161 rc
= set_flag(cxt
, MNT_FL_HELPER
, 1);
2163 cxt
->action
= action
;
2165 DBG(CXT
, mnt_debug_h(cxt
, "initialized for [u]mount.<type> helper [rc=%d]", rc
));
2170 * mnt_context_helper_setopt:
2172 * @c: getopt() result
2173 * @arg: getopt() optarg
2175 * This function applies the [u]mount.type command line option (for example parsed
2176 * by getopt or getopt_long) to @cxt. All unknown options are ignored and
2177 * then 1 is returned.
2179 * Returns: negative number on error, 1 if @c is unknown option, 0 on success.
2181 int mnt_context_helper_setopt(struct libmnt_context
*cxt
, int c
, char *arg
)
2184 switch(cxt
->action
) {
2186 return mnt_context_mount_setopt(cxt
, c
, arg
);
2187 case MNT_ACT_UMOUNT
:
2188 return mnt_context_umount_setopt(cxt
, c
, arg
);
2195 * mnt_context_is_fs_mounted:
2198 * @mounted: returns 1 for mounted and 0 for non-mounted filesystems
2200 * Please, read the mnt_table_is_fs_mounted() description!
2202 * Returns: 0 on success and negative number in case of error.
2204 int mnt_context_is_fs_mounted(struct libmnt_context
*cxt
,
2205 struct libmnt_fs
*fs
, int *mounted
)
2207 struct libmnt_table
*mtab
;
2211 if (!cxt
|| !fs
|| !mounted
)
2214 rc
= mnt_context_get_mtab(cxt
, &mtab
);
2218 *mounted
= mnt_table_is_fs_mounted(mtab
, fs
);
2222 static int mnt_context_add_child(struct libmnt_context
*cxt
, pid_t pid
)
2230 pids
= realloc(cxt
->children
, sizeof(pid_t
) * cxt
->nchildren
+ 1);
2234 DBG(CXT
, mnt_debug_h(cxt
, "add new child %d", pid
));
2235 cxt
->children
= pids
;
2236 cxt
->children
[cxt
->nchildren
++] = pid
;
2241 int mnt_fork_context(struct libmnt_context
*cxt
)
2247 if (!mnt_context_is_parent(cxt
))
2250 DBG(CXT
, mnt_debug_h(cxt
, "forking context"));
2257 case -1: /* error */
2258 DBG(CXT
, mnt_debug_h(cxt
, "fork failed %m"));
2262 cxt
->pid
= getpid();
2263 mnt_context_enable_fork(cxt
, FALSE
);
2264 DBG(CXT
, mnt_debug_h(cxt
, "child created"));
2268 rc
= mnt_context_add_child(cxt
, pid
);
2275 int mnt_context_wait_for_children(struct libmnt_context
*cxt
,
2276 int *nchildren
, int *nerrs
)
2284 assert(mnt_context_is_parent(cxt
));
2286 for (i
= 0; i
< cxt
->nchildren
; i
++) {
2287 pid_t pid
= cxt
->children
[i
];
2288 int rc
= 0, ret
= 0;
2293 DBG(CXT
, mnt_debug_h(cxt
,
2294 "waiting for child (%d/%d): %d",
2295 i
+ 1, cxt
->nchildren
, pid
));
2297 rc
= waitpid(pid
, &ret
, 0);
2299 } while (rc
== -1 && errno
== EINTR
);
2304 if (rc
!= -1 && nerrs
) {
2306 (*nerrs
) += WEXITSTATUS(ret
) == 0 ? 0 : 1;
2310 cxt
->children
[i
] = 0;
2314 free(cxt
->children
);
2315 cxt
->children
= NULL
;
2323 struct libmnt_lock
*lock
;
2325 static void lock_fallback(void)
2328 mnt_unlock_file(lock
);
2331 int test_mount(struct libmnt_test
*ts
, int argc
, char *argv
[])
2333 int idx
= 1, rc
= 0;
2334 struct libmnt_context
*cxt
;
2339 cxt
= mnt_new_context();
2343 if (!strcmp(argv
[idx
], "-o")) {
2344 mnt_context_set_options(cxt
, argv
[idx
+ 1]);
2347 if (!strcmp(argv
[idx
], "-t")) {
2348 /* TODO: use mnt_context_set_fstype_pattern() */
2349 mnt_context_set_fstype(cxt
, argv
[idx
+ 1]);
2353 if (argc
== idx
+ 1)
2354 /* mount <mountpont>|<device> */
2355 mnt_context_set_target(cxt
, argv
[idx
++]);
2357 else if (argc
== idx
+ 2) {
2358 /* mount <device> <mountpoint> */
2359 mnt_context_set_source(cxt
, argv
[idx
++]);
2360 mnt_context_set_target(cxt
, argv
[idx
++]);
2363 /* this is unnecessary! -- libmount is able to internally
2364 * create and manage the lock
2366 lock
= mnt_context_get_lock(cxt
);
2368 atexit(lock_fallback
);
2370 rc
= mnt_context_mount(cxt
);
2372 warn("failed to mount");
2374 printf("successfully mounted\n");
2376 lock
= NULL
; /* because we use atexit lock_fallback */
2377 mnt_free_context(cxt
);
2381 int test_umount(struct libmnt_test
*ts
, int argc
, char *argv
[])
2383 int idx
= 1, rc
= 0;
2384 struct libmnt_context
*cxt
;
2389 cxt
= mnt_new_context();
2393 if (!strcmp(argv
[idx
], "-t")) {
2394 mnt_context_set_fstype(cxt
, argv
[idx
+ 1]);
2398 if (!strcmp(argv
[idx
], "-f")) {
2399 mnt_context_enable_force(cxt
, TRUE
);
2403 if (!strcmp(argv
[idx
], "-l")) {
2404 mnt_context_enable_lazy(cxt
, TRUE
);
2408 if (!strcmp(argv
[idx
], "-r")) {
2409 mnt_context_enable_rdonly_umount(cxt
, TRUE
);
2413 if (argc
== idx
+ 1) {
2414 /* mount <mountpont>|<device> */
2415 mnt_context_set_target(cxt
, argv
[idx
++]);
2421 lock
= mnt_context_get_lock(cxt
);
2423 atexit(lock_fallback
);
2425 rc
= mnt_context_umount(cxt
);
2427 printf("failed to umount\n");
2429 printf("successfully umounted\n");
2431 lock
= NULL
; /* because we use atexit lock_fallback */
2432 mnt_free_context(cxt
);
2436 int test_flags(struct libmnt_test
*ts
, int argc
, char *argv
[])
2438 int idx
= 1, rc
= 0;
2439 struct libmnt_context
*cxt
;
2440 const char *opt
= NULL
;
2441 unsigned long flags
= 0;
2446 cxt
= mnt_new_context();
2450 if (!strcmp(argv
[idx
], "-o")) {
2451 mnt_context_set_options(cxt
, argv
[idx
+ 1]);
2455 if (argc
== idx
+ 1)
2456 /* mount <mountpont>|<device> */
2457 mnt_context_set_target(cxt
, argv
[idx
++]);
2459 rc
= mnt_context_prepare_mount(cxt
);
2461 printf("failed to prepare mount %s\n", strerror(-rc
));
2463 opt
= mnt_fs_get_options(cxt
->fs
);
2465 fprintf(stdout
, "options: %s\n", opt
);
2467 mnt_context_get_mflags(cxt
, &flags
);
2468 fprintf(stdout
, "flags: %08lx\n", flags
);
2470 mnt_free_context(cxt
);
2474 int test_mountall(struct libmnt_test
*ts
, int argc
, char *argv
[])
2476 struct libmnt_context
*cxt
;
2477 struct libmnt_iter
*itr
;
2478 struct libmnt_fs
*fs
;
2479 int mntrc
, ignored
, idx
= 1;
2481 cxt
= mnt_new_context();
2482 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
2488 if (argv
[idx
] && !strcmp(argv
[idx
], "-O")) {
2489 mnt_context_set_options_pattern(cxt
, argv
[idx
+ 1]);
2492 if (argv
[idx
] && !strcmp(argv
[idx
], "-t")) {
2493 mnt_context_set_fstype_pattern(cxt
, argv
[idx
+ 1]);
2498 while (mnt_context_next_mount(cxt
, itr
, &fs
, &mntrc
, &ignored
) == 0) {
2500 const char *tgt
= mnt_fs_get_target(fs
);
2503 printf("%s: ignored: not match\n", tgt
);
2504 else if (ignored
== 2)
2505 printf("%s: ignored: already mounted\n", tgt
);
2507 else if (!mnt_context_get_status(cxt
)) {
2510 warn("%s: mount failed", tgt
);
2512 warnx("%s: mount failed", tgt
);
2514 printf("%s: successfully mounted\n", tgt
);
2517 mnt_free_context(cxt
);
2521 int main(int argc
, char *argv
[])
2523 struct libmnt_test tss
[] = {
2524 { "--mount", test_mount
, "[-o <opts>] [-t <type>] <spec>|<src> <target>" },
2525 { "--umount", test_umount
, "[-t <type>] [-f][-l][-r] <src>|<target>" },
2526 { "--mount-all", test_mountall
, "[-O <pattern>] [-t <pattern] mount all filesystems from fstab" },
2527 { "--flags", test_flags
, "[-o <opts>] <spec>" },
2530 umask(S_IWGRP
|S_IWOTH
); /* to be compatible with mount(8) */
2532 return mnt_run_test(tss
, argc
, argv
);
2535 #endif /* TEST_PROGRAM */