]>
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
, ul_debugobj(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
, ul_debugobj(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
, ul_debugobj(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
, ul_debugobj(cxt
, "enabling flag %04x", flag
));
216 DBG(CXT
, ul_debugobj(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 * Note that libmount does not interpret "nofail" (MNT_MS_NOFAIL)
720 * mount option. The real return code is always returned, when
721 * the device does not exist then it's usually MNT_ERR_NOSOURCE
722 * from libmount or ENOENT, ENOTDIR, ENOTBLK, ENXIO from moun(2).
724 * Returns: 0 on success, negative number in case of error.
726 int mnt_context_set_source(struct libmnt_context
*cxt
, const char *source
)
729 return mnt_fs_set_source(mnt_context_get_fs(cxt
), source
);
733 * mnt_context_get_source:
734 * @cxt: mount context
736 * Returns: returns pointer or NULL in case of error or if not set.
738 const char *mnt_context_get_source(struct libmnt_context
*cxt
)
741 return mnt_fs_get_source(mnt_context_get_fs(cxt
));
745 * mnt_context_set_target:
746 * @cxt: mount context
747 * @target: mountpoint
749 * Returns: 0 on success, negative number in case of error.
751 int mnt_context_set_target(struct libmnt_context
*cxt
, const char *target
)
754 return mnt_fs_set_target(mnt_context_get_fs(cxt
), target
);
758 * mnt_context_get_target:
759 * @cxt: mount context
761 * Returns: returns pointer or NULL in case of error or if not set.
763 const char *mnt_context_get_target(struct libmnt_context
*cxt
)
766 return mnt_fs_get_target(mnt_context_get_fs(cxt
));
770 * mnt_context_set_fstype:
771 * @cxt: mount context
772 * @fstype: filesystem type
774 * Note that the @fstype has to be a FS type. For patterns with
775 * comma-separated list of filesystems or for the "nofs" notation, use
776 * mnt_context_set_fstype_pattern().
778 * Returns: 0 on success, negative number in case of error.
780 int mnt_context_set_fstype(struct libmnt_context
*cxt
, const char *fstype
)
783 return mnt_fs_set_fstype(mnt_context_get_fs(cxt
), fstype
);
787 * mnt_context_get_fstype:
788 * @cxt: mount context
790 * Returns: pointer or NULL in case of error or if not set.
792 const char *mnt_context_get_fstype(struct libmnt_context
*cxt
)
795 return mnt_fs_get_fstype(mnt_context_get_fs(cxt
));
799 * mnt_context_set_options:
800 * @cxt: mount context
801 * @optstr: comma delimited mount options
803 * Returns: 0 on success, negative number in case of error.
805 int mnt_context_set_options(struct libmnt_context
*cxt
, const char *optstr
)
808 return mnt_fs_set_options(mnt_context_get_fs(cxt
), optstr
);
812 * mnt_context_append_options:
813 * @cxt: mount context
814 * @optstr: comma delimited mount options
816 * Returns: 0 on success, negative number in case of error.
818 int mnt_context_append_options(struct libmnt_context
*cxt
, const char *optstr
)
821 return mnt_fs_append_options(mnt_context_get_fs(cxt
), optstr
);
825 * mnt_context_get_options:
826 * @cxt: mount context
828 * This function returns mount options set by mnt_context_set_options() or
829 * mnt_context_append_options().
831 * Note that *after* mnt_context_prepare_mount(), the mount options string
832 * may also include options set by mnt_context_set_mflags() or other options
833 * generated by this library.
835 * Returns: pointer or NULL
837 const char *mnt_context_get_options(struct libmnt_context
*cxt
)
840 return mnt_fs_get_options(mnt_context_get_fs(cxt
));
844 * mnt_context_set_fstype_pattern:
845 * @cxt: mount context
846 * @pattern: FS name pattern (or NULL to reset the current setting)
848 * See mount(8), option -t.
850 * Returns: 0 on success, negative number in case of error.
852 int mnt_context_set_fstype_pattern(struct libmnt_context
*cxt
, const char *pattern
)
864 free(cxt
->fstype_pattern
);
865 cxt
->fstype_pattern
= p
;
870 * mnt_context_set_options_pattern:
871 * @cxt: mount context
872 * @pattern: options pattern (or NULL to reset the current setting)
874 * See mount(8), option -O.
876 * Returns: 0 on success, negative number in case of error.
878 int mnt_context_set_options_pattern(struct libmnt_context
*cxt
, const char *pattern
)
890 free(cxt
->optstr_pattern
);
891 cxt
->optstr_pattern
= p
;
896 * mnt_context_set_fstab:
897 * @cxt: mount context
900 * The mount context reads /etc/fstab to the private struct libmnt_table by default.
901 * This function allows to overwrite the private fstab with an external
904 * This function modify the @tb reference counter. This function does not set
905 * the cache for the @tb. You have to explicitly call mnt_table_set_cache(tb,
906 * mnt_context_get_cache(cxt));
908 * The fstab is used read-only and is not modified, it should be possible to
909 * share the fstab between more mount contexts (TODO: test it.)
911 * If the @tb argument is NULL, then the current private fstab instance is
914 * Returns: 0 on success, negative number in case of error.
916 int mnt_context_set_fstab(struct libmnt_context
*cxt
, struct libmnt_table
*tb
)
922 mnt_ref_table(tb
); /* new */
923 mnt_unref_table(cxt
->fstab
); /* old */
930 * mnt_context_get_fstab:
931 * @cxt: mount context
934 * See also mnt_table_parse_fstab() for more details about fstab.
936 * Returns: 0 on success, negative number in case of error.
938 int mnt_context_get_fstab(struct libmnt_context
*cxt
, struct libmnt_table
**tb
)
946 cxt
->fstab
= mnt_new_table();
949 if (cxt
->table_errcb
)
950 mnt_table_set_parser_errcb(cxt
->fstab
, cxt
->table_errcb
);
951 mnt_table_set_cache(cxt
->fstab
, mnt_context_get_cache(cxt
));
952 rc
= mnt_table_parse_fstab(cxt
->fstab
, NULL
);
963 * mnt_context_get_mtab:
964 * @cxt: mount context
967 * See also mnt_table_parse_mtab() for more details about mtab/mountinfo. The
968 * result will be deallocated by mnt_free_context(@cxt).
970 * Returns: 0 on success, negative number in case of error.
972 int mnt_context_get_mtab(struct libmnt_context
*cxt
, struct libmnt_table
**tb
)
980 cxt
->mtab
= mnt_new_table();
984 if (cxt
->table_errcb
)
985 mnt_table_set_parser_errcb(cxt
->mtab
, cxt
->table_errcb
);
986 if (cxt
->table_fltrcb
)
987 mnt_table_set_parser_fltrcb(cxt
->mtab
,
989 cxt
->table_fltrcb_data
);
991 mnt_table_set_cache(cxt
->mtab
, mnt_context_get_cache(cxt
));
993 /* utab already parsed, don't parse it again */
994 rc
= __mnt_table_parse_mtab(cxt
->mtab
,
995 cxt
->mtab_path
, cxt
->utab
);
997 rc
= mnt_table_parse_mtab(cxt
->mtab
, cxt
->mtab_path
);
1005 DBG(CXT
, ul_debugobj(cxt
, "mtab requested [nents=%d]",
1006 mnt_table_get_nents(cxt
->mtab
)));
1011 * Allows to specify a filter for tab file entries. The filter is called by
1012 * the table parser. Currently used for mtab and utab only.
1014 int mnt_context_set_tabfilter(struct libmnt_context
*cxt
,
1015 int (*fltr
)(struct libmnt_fs
*, void *),
1022 cxt
->table_fltrcb
= fltr
;
1023 cxt
->table_fltrcb_data
= data
;
1026 mnt_table_set_parser_fltrcb(cxt
->mtab
,
1028 cxt
->table_fltrcb_data
);
1030 DBG(CXT
, ul_debugobj(cxt
, "tabfilter %s", fltr
? "ENABLED!" : "disabled"));
1035 * mnt_context_get_table:
1036 * @cxt: mount context
1037 * @filename: e.g. /proc/self/mountinfo
1038 * @tb: returns the table
1040 * This function allocates a new table and parses the @file. The parser error
1041 * callback and cache for tags and paths is set according to the @cxt setting.
1042 * See also mnt_table_parse_file().
1044 * It's strongly recommended to use the mnt_context_get_mtab() and
1045 * mnt_context_get_fstab() functions for mtab and fstab files. This function
1046 * does not care about LIBMOUNT_* env.variables and does not merge userspace
1049 * The result will NOT be deallocated by mnt_free_context(@cxt).
1051 * Returns: 0 on success, negative number in case of error.
1053 int mnt_context_get_table(struct libmnt_context
*cxt
,
1054 const char *filename
, struct libmnt_table
**tb
)
1063 *tb
= mnt_new_table();
1067 if (cxt
->table_errcb
)
1068 mnt_table_set_parser_errcb(*tb
, cxt
->table_errcb
);
1070 rc
= mnt_table_parse_file(*tb
, filename
);
1072 mnt_unref_table(*tb
);
1076 mnt_table_set_cache(*tb
, mnt_context_get_cache(cxt
));
1081 * mnt_context_set_tables_errcb
1082 * @cxt: mount context
1083 * @cb: pointer to callback function
1085 * The error callback is used for all tab files (e.g. mtab, fstab)
1086 * parsed within the context.
1088 * See also mnt_context_get_mtab(),
1089 * mnt_context_get_fstab(),
1090 * mnt_table_set_parser_errcb().
1092 * Returns: 0 on success, negative number in case of error.
1094 int mnt_context_set_tables_errcb(struct libmnt_context
*cxt
,
1095 int (*cb
)(struct libmnt_table
*tb
, const char *filename
, int line
))
1102 mnt_table_set_parser_errcb(cxt
->mtab
, cb
);
1104 mnt_table_set_parser_errcb(cxt
->fstab
, cb
);
1106 cxt
->table_errcb
= cb
;
1111 * mnt_context_set_cache:
1112 * @cxt: mount context
1113 * @cache: cache instance or nULL
1115 * The mount context maintains a private struct libmnt_cache by default. This
1116 * function allows to overwrite the private cache with an external instance.
1117 * This function increments cache reference counter.
1119 * If the @cache argument is NULL, then the current cache instance is reset.
1120 * This function apply the cache to fstab and mtab instances (if already
1123 * The old cache instance reference counter is de-incremented.
1125 * Returns: 0 on success, negative number in case of error.
1127 int mnt_context_set_cache(struct libmnt_context
*cxt
, struct libmnt_cache
*cache
)
1132 mnt_ref_cache(cache
); /* new */
1133 mnt_unref_cache(cxt
->cache
); /* old */
1138 mnt_table_set_cache(cxt
->mtab
, cache
);
1140 mnt_table_set_cache(cxt
->fstab
, cache
);
1146 * mnt_context_get_cache
1147 * @cxt: mount context
1149 * See also mnt_context_set_cache().
1151 * Returns: pointer to cache or NULL if canonicalization is disabled.
1153 struct libmnt_cache
*mnt_context_get_cache(struct libmnt_context
*cxt
)
1156 if (!cxt
|| mnt_context_is_nocanonicalize(cxt
))
1160 struct libmnt_cache
*cache
= mnt_new_cache();
1161 mnt_context_set_cache(cxt
, cache
);
1162 mnt_unref_cache(cache
);
1168 * mnt_context_set_passwd_cb:
1169 * @cxt: mount context
1170 * @get: callback to get password
1171 * @release: callback to release (delallocate) password
1173 * Sets callbacks for encryption password (e.g encrypted loopdev). This
1174 * function is deprecated (encrypted loops are no longer supported).
1176 * Returns: 0 on success, negative number in case of error.
1178 int mnt_context_set_passwd_cb(struct libmnt_context
*cxt
,
1179 char *(*get
)(struct libmnt_context
*),
1180 void (*release
)(struct libmnt_context
*, char *))
1185 cxt
->pwd_get_cb
= get
;
1186 cxt
->pwd_release_cb
= release
;
1191 * mnt_context_get_lock:
1192 * @cxt: mount context
1194 * The libmount applications don't have to care about mtab locking, but with a
1195 * small exception: the application has to be able to remove the lock file when
1196 * interrupted by signal or signals have to be ignored when the lock is locked.
1198 * The default behavior is to ignore all signals (except SIGALRM and
1199 * SIGTRAP for mtab udate) when the lock is locked. If this behavior
1200 * is unacceptable, then use:
1202 * lc = mnt_context_get_lock(cxt);
1204 * mnt_lock_block_signals(lc, FALSE);
1206 * and don't forget to call mnt_unlock_file(lc) before exit.
1208 * Returns: pointer to lock struct or NULL.
1210 struct libmnt_lock
*mnt_context_get_lock(struct libmnt_context
*cxt
)
1214 * DON'T call this function within libmount, it will always allocate
1215 * the lock. The mnt_update_* functions are able to allocate the lock
1216 * only when mtab/utab update is really necessary.
1218 if (!cxt
|| mnt_context_is_nomtab(cxt
))
1222 cxt
->lock
= mnt_new_lock(cxt
->mtab_writable
?
1223 cxt
->mtab_path
: cxt
->utab_path
, 0);
1225 mnt_lock_block_signals(cxt
->lock
, TRUE
);
1231 * mnt_context_set_mflags:
1232 * @cxt: mount context
1233 * @flags: mount(2) flags (MS_* flags)
1235 * Sets mount flags (see mount(2) man page).
1237 * Note that mount context allows to define mount options by mount flags. It
1238 * means you can for example use
1240 * mnt_context_set_mflags(cxt, MS_NOEXEC | MS_NOSUID);
1244 * mnt_context_set_options(cxt, "noexec,nosuid");
1246 * both of these calls have the same effect.
1248 * Returns: 0 on success, negative number in case of error.
1250 int mnt_context_set_mflags(struct libmnt_context
*cxt
, unsigned long flags
)
1256 cxt
->mountflags
= flags
;
1258 if ((cxt
->flags
& MNT_FL_MOUNTOPTS_FIXED
) && cxt
->fs
)
1260 * the final mount options are already generated, refresh...
1262 return mnt_optstr_apply_flags(
1263 &cxt
->fs
->vfs_optstr
,
1265 mnt_get_builtin_optmap(MNT_LINUX_MAP
));
1271 * mnt_context_get_mflags:
1272 * @cxt: mount context
1273 * @flags: returns MS_* mount flags
1275 * Converts mount options string to MS_* flags and bitewise-OR the result with
1276 * the already defined flags (see mnt_context_set_mflags()).
1278 * Returns: 0 on success, negative number in case of error.
1280 int mnt_context_get_mflags(struct libmnt_context
*cxt
, unsigned long *flags
)
1283 struct list_head
*p
;
1291 if (!(cxt
->flags
& MNT_FL_MOUNTFLAGS_MERGED
) && cxt
->fs
) {
1292 const char *o
= mnt_fs_get_options(cxt
->fs
);
1294 rc
= mnt_optstr_get_flags(o
, flags
,
1295 mnt_get_builtin_optmap(MNT_LINUX_MAP
));
1298 list_for_each(p
, &cxt
->addmounts
) {
1299 struct libmnt_addmount
*ad
=
1300 list_entry(p
, struct libmnt_addmount
, mounts
);
1302 *flags
|= ad
->mountflags
;
1306 *flags
|= cxt
->mountflags
;
1311 * mnt_context_set_user_mflags:
1312 * @cxt: mount context
1313 * @flags: mount(2) flags (MNT_MS_* flags, e.g. MNT_MS_LOOP)
1315 * Sets userspace mount flags.
1317 * See also notes for mnt_context_set_mflags().
1319 * Returns: 0 on success, negative number in case of error.
1321 int mnt_context_set_user_mflags(struct libmnt_context
*cxt
, unsigned long flags
)
1326 cxt
->user_mountflags
= flags
;
1331 * mnt_context_get_user_mflags:
1332 * @cxt: mount context
1333 * @flags: returns mount flags
1335 * Converts mount options string to MNT_MS_* flags and bitewise-OR the result
1336 * with the already defined flags (see mnt_context_set_user_mflags()).
1338 * Returns: 0 on success, negative number in case of error.
1340 int mnt_context_get_user_mflags(struct libmnt_context
*cxt
, unsigned long *flags
)
1350 if (!(cxt
->flags
& MNT_FL_MOUNTFLAGS_MERGED
) && cxt
->fs
) {
1351 const char *o
= mnt_fs_get_user_options(cxt
->fs
);
1353 rc
= mnt_optstr_get_flags(o
, flags
,
1354 mnt_get_builtin_optmap(MNT_USERSPACE_MAP
));
1357 *flags
|= cxt
->user_mountflags
;
1362 * mnt_context_set_mountdata:
1363 * @cxt: mount context
1364 * @data: mount(2) data
1366 * The mount context generates mountdata from mount options by default. This
1367 * function allows to overwrite this behavior, and @data will be used instead
1370 * The libmount does not deallocate the data by mnt_free_context(). Note that
1371 * NULL is also valid mount data.
1373 * Returns: 0 on success, negative number in case of error.
1375 int mnt_context_set_mountdata(struct libmnt_context
*cxt
, void *data
)
1380 cxt
->mountdata
= data
;
1381 cxt
->flags
|= MNT_FL_MOUNTDATA
;
1386 * Translates LABEL/UUID/path to mountable path
1388 int mnt_context_prepare_srcpath(struct libmnt_context
*cxt
)
1390 const char *path
= NULL
;
1391 struct libmnt_cache
*cache
;
1392 const char *t
, *v
, *src
;
1397 assert((cxt
->flags
& MNT_FL_MOUNTFLAGS_MERGED
));
1399 if (!cxt
|| !cxt
->fs
)
1402 DBG(CXT
, ul_debugobj(cxt
, "preparing source path"));
1404 src
= mnt_fs_get_source(cxt
->fs
);
1406 if (!src
&& mnt_context_propagation_only(cxt
))
1407 /* mount --make-{shared,private,...} */
1408 return mnt_fs_set_source(cxt
->fs
, "none");
1410 /* ignore filesystems without source or filesystems
1411 * where the source is a quasi-path (//foo/bar)
1413 if (!src
|| mnt_fs_is_netfs(cxt
->fs
))
1416 DBG(CXT
, ul_debugobj(cxt
, "srcpath '%s'", src
));
1418 cache
= mnt_context_get_cache(cxt
);
1420 if (!mnt_fs_get_tag(cxt
->fs
, &t
, &v
)) {
1422 * Source is TAG (evaluate)
1425 path
= mnt_resolve_tag(t
, v
, cache
);
1427 rc
= path
? mnt_fs_set_source(cxt
->fs
, path
) : -MNT_ERR_NOSOURCE
;
1429 } else if (cache
&& !mnt_fs_is_pseudofs(cxt
->fs
)) {
1431 * Source is PATH (canonicalize)
1433 path
= mnt_resolve_path(src
, cache
);
1434 if (path
&& strcmp(path
, src
))
1435 rc
= mnt_fs_set_source(cxt
->fs
, path
);
1439 DBG(CXT
, ul_debugobj(cxt
, "failed to prepare srcpath [rc=%d]", rc
));
1446 if ((cxt
->mountflags
& (MS_BIND
| MS_MOVE
| MS_REMOUNT
))
1447 || mnt_fs_is_pseudofs(cxt
->fs
)) {
1448 DBG(CXT
, ul_debugobj(cxt
, "REMOUNT/BIND/MOVE/pseudo FS source: %s", path
));
1453 * Initialize loop device
1455 if (mnt_context_is_loopdev(cxt
)) {
1456 rc
= mnt_context_setup_loopdev(cxt
);
1461 DBG(CXT
, ul_debugobj(cxt
, "final srcpath '%s'",
1462 mnt_fs_get_source(cxt
->fs
)));
1466 /* create a mountpoint if x-mount.mkdir[=<mode>] specified */
1467 static int mkdir_target(const char *tgt
, struct libmnt_fs
*fs
)
1478 if (mnt_optstr_get_option(fs
->user_optstr
, "x-mount.mkdir", &mstr
, &mstr_sz
) != 0)
1480 if (stat(tgt
, &st
) == 0)
1483 if (mstr
&& mstr_sz
) {
1487 mode
= strtol(mstr
, &end
, 8);
1489 if (errno
|| !end
|| mstr
+ mstr_sz
!= end
) {
1490 DBG(CXT
, ul_debug("failed to parse mkdir mode '%s'", mstr
));
1491 return -MNT_ERR_MOUNTOPT
;
1496 mode
= S_IRWXU
| /* 0755 */
1500 rc
= mkdir_p(tgt
, mode
);
1502 DBG(CXT
, ul_debug("mkdir %s failed: %m", tgt
));
1507 int mnt_context_prepare_target(struct libmnt_context
*cxt
)
1510 struct libmnt_cache
*cache
;
1515 assert((cxt
->flags
& MNT_FL_MOUNTFLAGS_MERGED
));
1517 if (!cxt
|| !cxt
->fs
)
1520 DBG(CXT
, ul_debugobj(cxt
, "preparing target path"));
1522 tgt
= mnt_fs_get_target(cxt
->fs
);
1527 if (cxt
->action
== MNT_ACT_MOUNT
1528 && !mnt_context_is_restricted(cxt
)
1529 && cxt
->user_mountflags
& MNT_MS_XCOMMENT
) {
1531 rc
= mkdir_target(tgt
, cxt
->fs
);
1533 return rc
; /* mkdir or parse error */
1536 /* canonicalize the path */
1537 cache
= mnt_context_get_cache(cxt
);
1539 char *path
= mnt_resolve_path(tgt
, cache
);
1540 if (path
&& strcmp(path
, tgt
) != 0)
1541 rc
= mnt_fs_set_target(cxt
->fs
, path
);
1545 DBG(CXT
, ul_debugobj(cxt
, "failed to prepare target '%s'", tgt
));
1547 DBG(CXT
, ul_debugobj(cxt
, "final target '%s'",
1548 mnt_fs_get_target(cxt
->fs
)));
1553 * It's usually no error when we're not able to detect the filesystem type -- we
1554 * will try to use the types from /{etc,proc}/filesystems.
1556 int mnt_context_guess_fstype(struct libmnt_context
*cxt
)
1564 assert((cxt
->flags
& MNT_FL_MOUNTFLAGS_MERGED
));
1566 if (!cxt
|| !cxt
->fs
)
1569 if ((cxt
->mountflags
& (MS_BIND
| MS_MOVE
))
1570 || mnt_context_propagation_only(cxt
))
1573 type
= (char *) mnt_fs_get_fstype(cxt
->fs
);
1574 if (type
&& !strcmp(type
, "auto")) {
1575 mnt_fs_set_fstype(cxt
->fs
, NULL
);
1581 if (cxt
->flags
& MS_REMOUNT
)
1583 if (cxt
->fstype_pattern
)
1586 dev
= mnt_fs_get_srcpath(cxt
->fs
);
1590 if (access(dev
, F_OK
) == 0) {
1591 struct libmnt_cache
*cache
= mnt_context_get_cache(cxt
);
1594 type
= mnt_get_fstype(dev
, &ambi
, cache
);
1596 rc
= mnt_fs_set_fstype(cxt
->fs
, type
);
1598 free(type
); /* type is not cached */
1601 rc
= -MNT_ERR_AMBIFS
;
1603 DBG(CXT
, ul_debugobj(cxt
, "access(%s) failed [%m]", dev
));
1604 if (strchr(dev
, ':') != NULL
)
1605 rc
= mnt_fs_set_fstype(cxt
->fs
, "nfs");
1606 else if (!strncmp(dev
, "//", 2))
1607 rc
= mnt_fs_set_fstype(cxt
->fs
, "cifs");
1611 DBG(CXT
, ul_debugobj(cxt
, "FS type: %s [rc=%d]",
1612 mnt_fs_get_fstype(cxt
->fs
), rc
));
1615 return mnt_fs_set_fstype(cxt
->fs
, "none");
1619 * The default is to use fstype from cxt->fs, this could be overwritten by
1620 * @type. The @act is MNT_ACT_{MOUNT,UMOUNT}.
1622 * Returns: 0 on success or negative number in case of error. Note that success
1623 * does not mean that there is any usable helper, you have to check cxt->helper.
1625 int mnt_context_prepare_helper(struct libmnt_context
*cxt
, const char *name
,
1628 char search_path
[] = FS_SEARCH_PATH
; /* from config.h */
1629 char *p
= NULL
, *path
;
1633 assert((cxt
->flags
& MNT_FL_MOUNTFLAGS_MERGED
));
1636 type
= mnt_fs_get_fstype(cxt
->fs
);
1638 if (type
&& strchr(type
, ','))
1639 return 0; /* type is fstype pattern */
1641 if (mnt_context_is_nohelpers(cxt
)
1643 || !strcmp(type
, "none")
1644 || strstr(type
, "/..") /* don't try to smuggle path */
1645 || mnt_fs_is_swaparea(cxt
->fs
))
1648 path
= strtok_r(search_path
, ":", &p
);
1650 char helper
[PATH_MAX
];
1654 rc
= snprintf(helper
, sizeof(helper
), "%s/%s.%s",
1656 path
= strtok_r(NULL
, ":", &p
);
1658 if (rc
< 0 || (size_t) rc
>= sizeof(helper
))
1661 rc
= stat(helper
, &st
);
1662 if (rc
== -1 && errno
== ENOENT
&& strchr(type
, '.')) {
1663 /* If type ends with ".subtype" try without it */
1664 char *hs
= strrchr(helper
, '.');
1667 rc
= stat(helper
, &st
);
1670 DBG(CXT
, ul_debugobj(cxt
, "%-25s ... %s", helper
,
1671 rc
? "not found" : "found"));
1676 cxt
->helper
= strdup(helper
);
1685 int mnt_context_merge_mflags(struct libmnt_context
*cxt
)
1687 unsigned long fl
= 0;
1692 DBG(CXT
, ul_debugobj(cxt
, "merging mount flags"));
1694 rc
= mnt_context_get_mflags(cxt
, &fl
);
1697 cxt
->mountflags
= fl
;
1700 rc
= mnt_context_get_user_mflags(cxt
, &fl
);
1703 cxt
->user_mountflags
= fl
;
1705 DBG(CXT
, ul_debugobj(cxt
, "final flags: VFS=%08lx user=%08lx",
1706 cxt
->mountflags
, cxt
->user_mountflags
));
1708 cxt
->flags
|= MNT_FL_MOUNTFLAGS_MERGED
;
1713 * Prepare /etc/mtab or /run/mount/utab
1715 int mnt_context_prepare_update(struct libmnt_context
*cxt
)
1722 assert(cxt
->action
);
1723 assert((cxt
->flags
& MNT_FL_MOUNTFLAGS_MERGED
));
1725 DBG(CXT
, ul_debugobj(cxt
, "prepare update"));
1727 if (mnt_context_propagation_only(cxt
)) {
1728 DBG(CXT
, ul_debugobj(cxt
, "skip update: only MS_PROPAGATION"));
1732 target
= mnt_fs_get_target(cxt
->fs
);
1734 if (cxt
->action
== MNT_ACT_UMOUNT
&& target
&& !strcmp(target
, "/"))
1735 /* Don't try to touch mtab if umounting root FS */
1736 mnt_context_disable_mtab(cxt
, TRUE
);
1738 if (mnt_context_is_nomtab(cxt
)) {
1739 DBG(CXT
, ul_debugobj(cxt
, "skip update: NOMTAB flag"));
1742 if (!cxt
->mtab_writable
&& !cxt
->utab_writable
) {
1743 DBG(CXT
, ul_debugobj(cxt
, "skip update: no writable destination"));
1746 /* 0 = success, 1 = not called yet */
1747 if (cxt
->syscall_status
!= 1 && cxt
->syscall_status
!= 0) {
1748 DBG(CXT
, ul_debugobj(cxt
,
1749 "skip update: syscall failed [status=%d]",
1750 cxt
->syscall_status
));
1755 const char *name
= cxt
->mtab_writable
? cxt
->mtab_path
: cxt
->utab_path
;
1757 if (cxt
->action
== MNT_ACT_UMOUNT
&& is_file_empty(name
)) {
1758 DBG(CXT
, ul_debugobj(cxt
,
1759 "skip update: umount, no table"));
1763 cxt
->update
= mnt_new_update();
1767 mnt_update_set_filename(cxt
->update
, name
, !cxt
->mtab_writable
);
1770 if (cxt
->action
== MNT_ACT_UMOUNT
)
1771 rc
= mnt_update_set_fs(cxt
->update
, cxt
->mountflags
,
1772 mnt_context_get_target(cxt
), NULL
);
1774 rc
= mnt_update_set_fs(cxt
->update
, cxt
->mountflags
,
1777 return rc
< 0 ? rc
: 0;
1780 int mnt_context_update_tabs(struct libmnt_context
*cxt
)
1786 if (mnt_context_is_nomtab(cxt
)) {
1787 DBG(CXT
, ul_debugobj(cxt
, "don't update: NOMTAB flag"));
1790 if (!cxt
->update
|| !mnt_update_is_ready(cxt
->update
)) {
1791 DBG(CXT
, ul_debugobj(cxt
, "don't update: no update prepared"));
1795 /* check utab update when external helper executed */
1796 if (mnt_context_helper_executed(cxt
)
1797 && mnt_context_get_helper_status(cxt
) == 0
1798 && cxt
->utab_writable
) {
1800 if (mnt_update_already_done(cxt
->update
, cxt
->lock
)) {
1801 DBG(CXT
, ul_debugobj(cxt
, "don't update: error evaluate or already updated"));
1804 } else if (cxt
->helper
) {
1805 DBG(CXT
, ul_debugobj(cxt
, "don't update: external helper"));
1809 if (cxt
->syscall_status
!= 0
1810 && !(mnt_context_helper_executed(cxt
) &&
1811 mnt_context_get_helper_status(cxt
) == 0)) {
1813 DBG(CXT
, ul_debugobj(cxt
, "don't update: syscall/helper failed/not called"));
1817 fl
= mnt_update_get_mflags(cxt
->update
);
1818 if ((cxt
->mountflags
& MS_RDONLY
) != (fl
& MS_RDONLY
))
1820 * fix MS_RDONLY in options
1822 mnt_update_force_rdonly(cxt
->update
,
1823 cxt
->mountflags
& MS_RDONLY
);
1825 return mnt_update_table(cxt
->update
, cxt
->lock
);
1828 static int apply_table(struct libmnt_context
*cxt
, struct libmnt_table
*tb
,
1831 struct libmnt_fs
*fs
= NULL
;
1832 const char *src
= NULL
, *tgt
= NULL
;
1841 src
= mnt_fs_get_source(cxt
->fs
);
1842 tgt
= mnt_fs_get_target(cxt
->fs
);
1845 fs
= mnt_table_find_pair(tb
, src
, tgt
, direction
);
1848 fs
= mnt_table_find_source(tb
, src
, direction
);
1850 fs
= mnt_table_find_target(tb
, tgt
, direction
);
1852 if (!fs
&& mnt_context_is_swapmatch(cxt
)) {
1853 /* swap source and target (if @src is not LABEL/UUID),
1858 * the path could be a mountpoint as well as a source (for
1859 * example bind mount, symlink to a device, ...).
1861 if (src
&& !mnt_fs_get_tag(cxt
->fs
, NULL
, NULL
))
1862 fs
= mnt_table_find_target(tb
, src
, direction
);
1864 fs
= mnt_table_find_source(tb
, tgt
, direction
);
1869 return -MNT_ERR_NOFSTAB
; /* not found */
1871 DBG(CXT
, ul_debugobj(cxt
, "apply entry:"));
1872 DBG(CXT
, mnt_fs_print_debug(fs
, stderr
));
1874 /* copy from tab to our FS description
1876 rc
= mnt_fs_set_source(cxt
->fs
, mnt_fs_get_source(fs
));
1878 rc
= mnt_fs_set_target(cxt
->fs
, mnt_fs_get_target(fs
));
1880 if (!rc
&& !mnt_fs_get_fstype(cxt
->fs
))
1881 rc
= mnt_fs_set_fstype(cxt
->fs
, mnt_fs_get_fstype(fs
));
1886 if (cxt
->optsmode
& MNT_OMODE_IGNORE
)
1888 else if (cxt
->optsmode
& MNT_OMODE_REPLACE
)
1889 rc
= mnt_fs_set_options(cxt
->fs
, mnt_fs_get_options(fs
));
1891 else if (cxt
->optsmode
& MNT_OMODE_APPEND
)
1892 rc
= mnt_fs_append_options(cxt
->fs
, mnt_fs_get_options(fs
));
1894 else if (cxt
->optsmode
& MNT_OMODE_PREPEND
)
1895 rc
= mnt_fs_prepend_options(cxt
->fs
, mnt_fs_get_options(fs
));
1898 cxt
->flags
|= MNT_FL_TAB_APPLIED
;
1903 * mnt_context_apply_fstab:
1904 * @cxt: mount context
1906 * This function is optional.
1908 * Returns: 0 on success, negative number in case of error.
1910 int mnt_context_apply_fstab(struct libmnt_context
*cxt
)
1913 struct libmnt_table
*tab
= NULL
;
1914 const char *src
= NULL
, *tgt
= NULL
;
1922 if (mnt_context_tab_applied(cxt
)) /* already applied */
1925 if (mnt_context_is_restricted(cxt
)) {
1926 DBG(CXT
, ul_debugobj(cxt
, "force fstab usage for non-root users!"));
1927 cxt
->optsmode
= MNT_OMODE_USER
;
1928 } else if (cxt
->optsmode
== 0) {
1929 DBG(CXT
, ul_debugobj(cxt
, "use default optsmode"));
1930 cxt
->optsmode
= MNT_OMODE_AUTO
;
1931 } else if (cxt
->optsmode
& MNT_OMODE_NOTAB
) {
1932 cxt
->optsmode
&= ~MNT_OMODE_FSTAB
;
1933 cxt
->optsmode
&= ~MNT_OMODE_MTAB
;
1934 cxt
->optsmode
&= ~MNT_OMODE_FORCE
;
1938 src
= mnt_fs_get_source(cxt
->fs
);
1939 tgt
= mnt_fs_get_target(cxt
->fs
);
1942 DBG(CXT
, ul_debugobj(cxt
, "OPTSMODE: ignore=%d, append=%d, prepend=%d, "
1943 "replace=%d, force=%d, fstab=%d, mtab=%d",
1944 cxt
->optsmode
& MNT_OMODE_IGNORE
? 1 : 0,
1945 cxt
->optsmode
& MNT_OMODE_APPEND
? 1 : 0,
1946 cxt
->optsmode
& MNT_OMODE_PREPEND
? 1 : 0,
1947 cxt
->optsmode
& MNT_OMODE_REPLACE
? 1 : 0,
1948 cxt
->optsmode
& MNT_OMODE_FORCE
? 1 : 0,
1949 cxt
->optsmode
& MNT_OMODE_FSTAB
? 1 : 0,
1950 cxt
->optsmode
& MNT_OMODE_MTAB
? 1 : 0));
1952 /* fstab is not required if source and target are specified */
1953 if (src
&& tgt
&& !(cxt
->optsmode
& MNT_OMODE_FORCE
)) {
1954 DBG(CXT
, ul_debugobj(cxt
, "fstab not required -- skip"));
1959 && !(cxt
->optsmode
& MNT_OMODE_FSTAB
)
1960 && !(cxt
->optsmode
& MNT_OMODE_MTAB
)) {
1961 DBG(CXT
, ul_debugobj(cxt
, "only target; fstab/mtab not required "
1962 "-- skip, probably MS_PROPAGATION"));
1966 DBG(CXT
, ul_debugobj(cxt
,
1967 "trying to apply fstab (src=%s, target=%s)", src
, tgt
));
1969 /* let's initialize cxt->fs */
1970 ignore_result( mnt_context_get_fs(cxt
) );
1973 if (cxt
->optsmode
& MNT_OMODE_FSTAB
) {
1974 rc
= mnt_context_get_fstab(cxt
, &tab
);
1976 rc
= apply_table(cxt
, tab
, MNT_ITER_FORWARD
);
1980 if (rc
< 0 && (cxt
->optsmode
& MNT_OMODE_MTAB
)) {
1981 DBG(CXT
, ul_debugobj(cxt
, "trying to apply from mtab"));
1982 rc
= mnt_context_get_mtab(cxt
, &tab
);
1984 rc
= apply_table(cxt
, tab
, MNT_ITER_BACKWARD
);
1987 DBG(CXT
, ul_debugobj(cxt
, "failed to find entry in fstab/mtab"));
1992 * mnt_context_tab_applied:
1993 * @cxt: mount context
1995 * Returns: 1 if fstab (or mtab) has been applied to the context, or 0.
1997 int mnt_context_tab_applied(struct libmnt_context
*cxt
)
2000 return cxt
->flags
& MNT_FL_TAB_APPLIED
;
2004 * This is not a public function!
2006 * Returns 1 if *only propagation flags* change is requested.
2008 int mnt_context_propagation_only(struct libmnt_context
*cxt
)
2013 if (cxt
->action
!= MNT_ACT_MOUNT
)
2016 /* has to be called after context_mount.c: fix_opts() */
2017 assert((cxt
->flags
& MNT_FL_MOUNTOPTS_FIXED
));
2019 /* all propagation mounts are in cxt->addmount */
2020 return !list_empty(&cxt
->addmounts
)
2021 && (cxt
->mountflags
== 0 || cxt
->mountflags
== MS_SILENT
)
2023 && (!cxt
->fs
->fstype
|| strcmp(cxt
->fs
->fstype
, "none") == 0)
2024 && (!cxt
->fs
->source
|| strcmp(cxt
->fs
->source
, "none") == 0);
2028 * mnt_context_get_status:
2029 * @cxt: mount context
2031 * Global libmount status.
2033 * The real exit code of the mount.type helper has to be tested by
2034 * mnt_context_get_helper_status(). The mnt_context_get_status() only informs
2035 * that exec() has been successful.
2037 * Returns: 1 if mount.type or mount(2) syscall has been successfully called.
2039 int mnt_context_get_status(struct libmnt_context
*cxt
)
2042 return !cxt
->syscall_status
|| !cxt
->helper_exec_status
;
2046 * mnt_context_helper_executed:
2047 * @cxt: mount context
2049 * Returns: 1 if mount.type helper has been executed, or 0.
2051 int mnt_context_helper_executed(struct libmnt_context
*cxt
)
2054 return cxt
->helper_exec_status
!= 1;
2058 * mnt_context_get_helper_status:
2059 * @cxt: mount context
2061 * Return: mount.type helper exit status, result is reliable only if
2062 * mnt_context_helper_executed() returns 1.
2064 int mnt_context_get_helper_status(struct libmnt_context
*cxt
)
2067 return cxt
->helper_status
;
2071 * mnt_context_syscall_called:
2072 * @cxt: mount context
2074 * Returns: 1 if mount(2) syscall has been called, or 0.
2076 int mnt_context_syscall_called(struct libmnt_context
*cxt
)
2079 return cxt
->syscall_status
!= 1;
2083 * mnt_context_get_syscall_errno:
2084 * @cxt: mount context
2086 * The result from this function is reliable only if
2087 * mnt_context_syscall_called() returns 1.
2089 * Returns: mount(2) errno if the syscall failed or 0.
2091 int mnt_context_get_syscall_errno(struct libmnt_context
*cxt
)
2094 if (cxt
->syscall_status
< 0)
2095 return -cxt
->syscall_status
;
2100 * mnt_context_set_syscall_status:
2101 * @cxt: mount context
2102 * @status: mount(2) status
2104 * The @status should be 0 on success, or negative number on error (-errno).
2106 * This function should only be used if the [u]mount(2) syscall is NOT called by
2109 * Returns: 0 or negative number in case of error.
2111 int mnt_context_set_syscall_status(struct libmnt_context
*cxt
, int status
)
2117 DBG(CXT
, ul_debugobj(cxt
, "syscall status set to: %d", status
));
2118 cxt
->syscall_status
= status
;
2123 * mnt_context_strerror
2126 * @bufsiz: size of the buffer
2128 * Not implemented yet.
2130 * Returns: 0 or negative number in case of error.
2132 int mnt_context_strerror(struct libmnt_context
*cxt
__attribute__((__unused__
)),
2133 char *buf
__attribute__((__unused__
)),
2134 size_t bufsiz
__attribute__((__unused__
)))
2136 /* TODO: based on cxt->syscall_errno or cxt->helper_status */
2141 * mnt_context_init_helper
2142 * @cxt: mount context
2143 * @action: MNT_ACT_{UMOUNT,MOUNT}
2144 * @flags: not used now
2146 * This function informs libmount that used from [u]mount.type helper.
2148 * The function also calls mnt_context_disable_helpers() to avoid recursive
2149 * mount.type helpers calling. It you really want to call another
2150 * mount.type helper from your helper, then you have to explicitly enable this
2153 * mnt_context_disable_helpers(cxt, FALSE);
2155 * Returns: 0 on success, negative number in case of error.
2157 int mnt_context_init_helper(struct libmnt_context
*cxt
, int action
,
2158 int flags
__attribute__((__unused__
)))
2164 rc
= mnt_context_disable_helpers(cxt
, TRUE
);
2166 rc
= set_flag(cxt
, MNT_FL_HELPER
, 1);
2168 cxt
->action
= action
;
2170 DBG(CXT
, ul_debugobj(cxt
, "initialized for [u]mount.<type> helper [rc=%d]", rc
));
2175 * mnt_context_helper_setopt:
2177 * @c: getopt() result
2178 * @arg: getopt() optarg
2180 * This function applies the [u]mount.type command line option (for example parsed
2181 * by getopt or getopt_long) to @cxt. All unknown options are ignored and
2182 * then 1 is returned.
2184 * Returns: negative number on error, 1 if @c is unknown option, 0 on success.
2186 int mnt_context_helper_setopt(struct libmnt_context
*cxt
, int c
, char *arg
)
2189 switch(cxt
->action
) {
2191 return mnt_context_mount_setopt(cxt
, c
, arg
);
2192 case MNT_ACT_UMOUNT
:
2193 return mnt_context_umount_setopt(cxt
, c
, arg
);
2200 * mnt_context_is_fs_mounted:
2203 * @mounted: returns 1 for mounted and 0 for non-mounted filesystems
2205 * Please, read the mnt_table_is_fs_mounted() description!
2207 * Returns: 0 on success and negative number in case of error.
2209 int mnt_context_is_fs_mounted(struct libmnt_context
*cxt
,
2210 struct libmnt_fs
*fs
, int *mounted
)
2212 struct libmnt_table
*mtab
;
2216 if (!cxt
|| !fs
|| !mounted
)
2219 rc
= mnt_context_get_mtab(cxt
, &mtab
);
2223 *mounted
= mnt_table_is_fs_mounted(mtab
, fs
);
2227 static int mnt_context_add_child(struct libmnt_context
*cxt
, pid_t pid
)
2235 pids
= realloc(cxt
->children
, sizeof(pid_t
) * cxt
->nchildren
+ 1);
2239 DBG(CXT
, ul_debugobj(cxt
, "add new child %d", pid
));
2240 cxt
->children
= pids
;
2241 cxt
->children
[cxt
->nchildren
++] = pid
;
2246 int mnt_fork_context(struct libmnt_context
*cxt
)
2252 if (!mnt_context_is_parent(cxt
))
2255 DBG(CXT
, ul_debugobj(cxt
, "forking context"));
2262 case -1: /* error */
2263 DBG(CXT
, ul_debugobj(cxt
, "fork failed %m"));
2267 cxt
->pid
= getpid();
2268 mnt_context_enable_fork(cxt
, FALSE
);
2269 DBG(CXT
, ul_debugobj(cxt
, "child created"));
2273 rc
= mnt_context_add_child(cxt
, pid
);
2280 int mnt_context_wait_for_children(struct libmnt_context
*cxt
,
2281 int *nchildren
, int *nerrs
)
2289 assert(mnt_context_is_parent(cxt
));
2291 for (i
= 0; i
< cxt
->nchildren
; i
++) {
2292 pid_t pid
= cxt
->children
[i
];
2293 int rc
= 0, ret
= 0;
2298 DBG(CXT
, ul_debugobj(cxt
,
2299 "waiting for child (%d/%d): %d",
2300 i
+ 1, cxt
->nchildren
, pid
));
2302 rc
= waitpid(pid
, &ret
, 0);
2304 } while (rc
== -1 && errno
== EINTR
);
2309 if (rc
!= -1 && nerrs
) {
2311 (*nerrs
) += WEXITSTATUS(ret
) == 0 ? 0 : 1;
2315 cxt
->children
[i
] = 0;
2319 free(cxt
->children
);
2320 cxt
->children
= NULL
;
2328 struct libmnt_lock
*lock
;
2330 static void lock_fallback(void)
2333 mnt_unlock_file(lock
);
2336 int test_mount(struct libmnt_test
*ts
, int argc
, char *argv
[])
2338 int idx
= 1, rc
= 0;
2339 struct libmnt_context
*cxt
;
2344 cxt
= mnt_new_context();
2348 if (!strcmp(argv
[idx
], "-o")) {
2349 mnt_context_set_options(cxt
, argv
[idx
+ 1]);
2352 if (!strcmp(argv
[idx
], "-t")) {
2353 /* TODO: use mnt_context_set_fstype_pattern() */
2354 mnt_context_set_fstype(cxt
, argv
[idx
+ 1]);
2358 if (argc
== idx
+ 1)
2359 /* mount <mountpont>|<device> */
2360 mnt_context_set_target(cxt
, argv
[idx
++]);
2362 else if (argc
== idx
+ 2) {
2363 /* mount <device> <mountpoint> */
2364 mnt_context_set_source(cxt
, argv
[idx
++]);
2365 mnt_context_set_target(cxt
, argv
[idx
++]);
2368 /* this is unnecessary! -- libmount is able to internally
2369 * create and manage the lock
2371 lock
= mnt_context_get_lock(cxt
);
2373 atexit(lock_fallback
);
2375 rc
= mnt_context_mount(cxt
);
2377 warn("failed to mount");
2379 printf("successfully mounted\n");
2381 lock
= NULL
; /* because we use atexit lock_fallback */
2382 mnt_free_context(cxt
);
2386 int test_umount(struct libmnt_test
*ts
, int argc
, char *argv
[])
2388 int idx
= 1, rc
= 0;
2389 struct libmnt_context
*cxt
;
2394 cxt
= mnt_new_context();
2398 if (!strcmp(argv
[idx
], "-t")) {
2399 mnt_context_set_fstype(cxt
, argv
[idx
+ 1]);
2403 if (!strcmp(argv
[idx
], "-f")) {
2404 mnt_context_enable_force(cxt
, TRUE
);
2408 if (!strcmp(argv
[idx
], "-l")) {
2409 mnt_context_enable_lazy(cxt
, TRUE
);
2413 if (!strcmp(argv
[idx
], "-r")) {
2414 mnt_context_enable_rdonly_umount(cxt
, TRUE
);
2418 if (argc
== idx
+ 1) {
2419 /* mount <mountpont>|<device> */
2420 mnt_context_set_target(cxt
, argv
[idx
++]);
2426 lock
= mnt_context_get_lock(cxt
);
2428 atexit(lock_fallback
);
2430 rc
= mnt_context_umount(cxt
);
2432 printf("failed to umount\n");
2434 printf("successfully umounted\n");
2436 lock
= NULL
; /* because we use atexit lock_fallback */
2437 mnt_free_context(cxt
);
2441 int test_flags(struct libmnt_test
*ts
, int argc
, char *argv
[])
2443 int idx
= 1, rc
= 0;
2444 struct libmnt_context
*cxt
;
2445 const char *opt
= NULL
;
2446 unsigned long flags
= 0;
2451 cxt
= mnt_new_context();
2455 if (!strcmp(argv
[idx
], "-o")) {
2456 mnt_context_set_options(cxt
, argv
[idx
+ 1]);
2460 if (argc
== idx
+ 1)
2461 /* mount <mountpont>|<device> */
2462 mnt_context_set_target(cxt
, argv
[idx
++]);
2464 rc
= mnt_context_prepare_mount(cxt
);
2466 printf("failed to prepare mount %s\n", strerror(-rc
));
2468 opt
= mnt_fs_get_options(cxt
->fs
);
2470 fprintf(stdout
, "options: %s\n", opt
);
2472 mnt_context_get_mflags(cxt
, &flags
);
2473 fprintf(stdout
, "flags: %08lx\n", flags
);
2475 mnt_free_context(cxt
);
2479 int test_mountall(struct libmnt_test
*ts
, int argc
, char *argv
[])
2481 struct libmnt_context
*cxt
;
2482 struct libmnt_iter
*itr
;
2483 struct libmnt_fs
*fs
;
2484 int mntrc
, ignored
, idx
= 1;
2486 cxt
= mnt_new_context();
2487 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
2493 if (argv
[idx
] && !strcmp(argv
[idx
], "-O")) {
2494 mnt_context_set_options_pattern(cxt
, argv
[idx
+ 1]);
2497 if (argv
[idx
] && !strcmp(argv
[idx
], "-t")) {
2498 mnt_context_set_fstype_pattern(cxt
, argv
[idx
+ 1]);
2503 while (mnt_context_next_mount(cxt
, itr
, &fs
, &mntrc
, &ignored
) == 0) {
2505 const char *tgt
= mnt_fs_get_target(fs
);
2508 printf("%s: ignored: not match\n", tgt
);
2509 else if (ignored
== 2)
2510 printf("%s: ignored: already mounted\n", tgt
);
2512 else if (!mnt_context_get_status(cxt
)) {
2515 warn("%s: mount failed", tgt
);
2517 warnx("%s: mount failed", tgt
);
2519 printf("%s: successfully mounted\n", tgt
);
2522 mnt_free_context(cxt
);
2526 int main(int argc
, char *argv
[])
2528 struct libmnt_test tss
[] = {
2529 { "--mount", test_mount
, "[-o <opts>] [-t <type>] <spec>|<src> <target>" },
2530 { "--umount", test_umount
, "[-t <type>] [-f][-l][-r] <src>|<target>" },
2531 { "--mount-all", test_mountall
, "[-O <pattern>] [-t <pattern] mount all filesystems from fstab" },
2532 { "--flags", test_flags
, "[-o <opts>] <spec>" },
2535 umask(S_IWGRP
|S_IWOTH
); /* to be compatible with mount(8) */
2537 return mnt_run_test(tss
, argc
, argv
);
2540 #endif /* TEST_PROGRAM */