]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libmount/src/context.c
Merge branch 'master' of https://github.com/rudimeier/util-linux
[thirdparty/util-linux.git] / libmount / src / context.c
1 /*
2 * Copyright (C) 2010,2011,2012 Karel Zak <kzak@redhat.com>
3 *
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License.
6 */
7
8 /**
9 * SECTION: context
10 * @title: Library high-level context
11 * @short_description: high-level API to mount/umount devices.
12 *
13 * <informalexample>
14 * <programlisting>
15 * struct libmnt_context *cxt = mnt_new_context();
16 *
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");
20 *
21 * if (!mnt_context_mount(cxt))
22 * printf("successfully mounted\n");
23 * mnt_free_context(cxt);
24 *
25 * </programlisting>
26 * </informalexample>
27 *
28 * This code is similar to:
29 *
30 * mount -o aaa,bbb,ccc=CCC,noatime,noexec /mnt/foo
31 *
32 */
33
34 #include "mountP.h"
35
36 #include <sys/wait.h>
37
38 /**
39 * mnt_new_context:
40 *
41 * Returns: newly allocated mount context
42 */
43 struct libmnt_context *mnt_new_context(void)
44 {
45 struct libmnt_context *cxt;
46 uid_t ruid, euid;
47
48 cxt = calloc(1, sizeof(*cxt));
49 if (!cxt)
50 return NULL;
51
52 INIT_LIST_HEAD(&cxt->addmounts);
53
54 ruid = getuid();
55 euid = geteuid();
56
57 mnt_context_reset_status(cxt);
58
59 cxt->loopdev_fd = -1;
60
61 /* if we're really root and aren't running setuid */
62 cxt->restricted = (uid_t) 0 == ruid && ruid == euid ? 0 : 1;
63
64 DBG(CXT, mnt_debug_h(cxt, "----> allocate %s",
65 cxt->restricted ? "[RESTRICTED]" : ""));
66
67 mnt_has_regular_mtab(&cxt->mtab_path, &cxt->mtab_writable);
68
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);
72
73 return cxt;
74 }
75
76 /**
77 * mnt_free_context:
78 * @cxt: mount context
79 *
80 * Deallocates context struct.
81 */
82 void mnt_free_context(struct libmnt_context *cxt)
83 {
84 if (!cxt)
85 return;
86
87 mnt_reset_context(cxt);
88
89 free(cxt->fstype_pattern);
90 free(cxt->optstr_pattern);
91
92 mnt_unref_table(cxt->fstab);
93 mnt_unref_cache(cxt->cache);
94
95 mnt_context_clear_loopdev(cxt);
96 mnt_free_lock(cxt->lock);
97 mnt_free_update(cxt->update);
98
99 free(cxt->children);
100
101 DBG(CXT, mnt_debug_h(cxt, "<---- free"));
102 free(cxt);
103 }
104
105 /**
106 * mnt_reset_context:
107 * @cxt: mount context
108 *
109 * Resets all information in the context that is directly related to
110 * the latest mount (spec, source, target, mount options, ...).
111 *
112 * The match patterns, cached fstab, cached canonicalized paths and tags and
113 * [e]uid are not reset. You have to use
114 *
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);
119 *
120 *
121 * to reset this stuff.
122 *
123 * Returns: 0 on success, negative number in case of error.
124 */
125 int mnt_reset_context(struct libmnt_context *cxt)
126 {
127 int fl;
128
129 if (!cxt)
130 return -EINVAL;
131
132 DBG(CXT, mnt_debug_h(cxt, "<---- reset [status=%d] ---->",
133 mnt_context_get_status(cxt)));
134
135 fl = cxt->flags;
136
137 mnt_unref_fs(cxt->fs);
138 mnt_unref_table(cxt->mtab);
139 mnt_unref_table(cxt->utab);
140
141 free(cxt->helper);
142 free(cxt->orig_user);
143
144 cxt->fs = NULL;
145 cxt->mtab = NULL;
146 cxt->utab = NULL;
147 cxt->helper = NULL;
148 cxt->orig_user = NULL;
149 cxt->mountflags = 0;
150 cxt->user_mountflags = 0;
151 cxt->mountdata = NULL;
152 cxt->flags = MNT_FL_DEFAULT;
153
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,
158 mounts);
159 mnt_free_addmount(ad);
160 }
161
162 mnt_context_reset_status(cxt);
163
164 if (cxt->table_fltrcb)
165 mnt_context_set_tabfilter(cxt, NULL, NULL);
166
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);
180 return 0;
181 }
182
183 /**
184 * mnt_context_reset_status:
185 * @cxt: context
186 *
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.
189 *
190 * BE CAREFUL -- after this soft reset the libmount will NOT parse mount
191 * options, evaluate permissions or apply stuff from fstab.
192 *
193 * Returns: 0 on success, negative number in case of error.
194 */
195 int mnt_context_reset_status(struct libmnt_context *cxt)
196 {
197 assert(cxt);
198 if (!cxt)
199 return -EINVAL;
200
201 cxt->syscall_status = 1; /* means not called yet */
202 cxt->helper_exec_status = 1;
203 cxt->helper_status = 0;
204 return 0;
205 }
206
207 static int set_flag(struct libmnt_context *cxt, int flag, int enable)
208 {
209 assert(cxt);
210 if (!cxt)
211 return -EINVAL;
212 if (enable) {
213 DBG(CXT, mnt_debug_h(cxt, "enabling flag %04x", flag));
214 cxt->flags |= flag;
215 } else {
216 DBG(CXT, mnt_debug_h(cxt, "disabling flag %04x", flag));
217 cxt->flags &= ~flag;
218 }
219 return 0;
220 }
221
222 /**
223 * mnt_context_is_restricted:
224 * @cxt: mount context
225 *
226 * Returns: 0 for an unrestricted mount (user is root), or 1 for non-root mounts
227 */
228 int mnt_context_is_restricted(struct libmnt_context *cxt)
229 {
230 return cxt->restricted;
231 }
232
233 /**
234 * mnt_context_set_optsmode
235 * @cxt: mount context
236 * @mode: MNT_OMODE_* flags
237 *
238 * Controls how to use mount optionssource and target paths from fstab/mtab.
239 *
240 * @MNT_OMODE_IGNORE: ignore mtab/fstab options
241 *
242 * @MNT_OMODE_APPEND: append mtab/fstab options to existing options
243 *
244 * @MNT_OMODE_PREPEND: prepend mtab/fstab options to existing options
245 *
246 * @MNT_OMODE_REPLACE: replace existing options with options from mtab/fstab
247 *
248 * @MNT_OMODE_FORCE: always read mtab/fstab (although source and target are defined)
249 *
250 * @MNT_OMODE_FSTAB: read from fstab
251 *
252 * @MNT_OMODE_MTAB: read from mtab if fstab not enabled or failed
253 *
254 * @MNT_OMODE_NOTAB: do not read fstab/mtab at all
255 *
256 * @MNT_OMODE_AUTO: default mode (MNT_OMODE_PREPEND | MNT_OMODE_FSTAB | MNT_OMODE_MTAB)
257 *
258 * @MNT_OMODE_USER: default for non-root users (MNT_OMODE_REPLACE | MNT_OMODE_FORCE | MNT_OMODE_FSTAB)
259 *
260 * Notes:
261 *
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}
267 *
268 * Returns: 0 on success, negative number in case of error.
269 */
270 int mnt_context_set_optsmode(struct libmnt_context *cxt, int mode)
271 {
272 assert(cxt);
273 if (!cxt)
274 return -EINVAL;
275 cxt->optsmode = mode;
276 return 0;
277 }
278
279 /**
280 * mnt_context_get_optsmode
281 * @cxt: mount context
282 *
283 * Returns: MNT_OMODE_* mask or zero.
284 */
285
286 int mnt_context_get_optsmode(struct libmnt_context *cxt)
287 {
288 assert(cxt);
289 return cxt->optsmode;
290 }
291
292 /**
293 * mnt_context_disable_canonicalize:
294 * @cxt: mount context
295 * @disable: TRUE or FALSE
296 *
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.
300 *
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,
304 * NULL).
305 *
306 * Returns: 0 on success, negative number in case of error.
307 */
308 int mnt_context_disable_canonicalize(struct libmnt_context *cxt, int disable)
309 {
310 return set_flag(cxt, MNT_FL_NOCANONICALIZE, disable);
311 }
312
313 /**
314 * mnt_context_is_nocanonicalize:
315 * @cxt: mount context
316 *
317 * Returns: 1 if no-canonicalize mode is enabled or 0.
318 */
319 int mnt_context_is_nocanonicalize(struct libmnt_context *cxt)
320 {
321 return cxt && (cxt->flags & MNT_FL_NOCANONICALIZE) ? 1 : 0;
322 }
323
324 /**
325 * mnt_context_enable_lazy:
326 * @cxt: mount context
327 * @enable: TRUE or FALSE
328 *
329 * Enable/disable lazy umount (see umount(8) man page, option -l).
330 *
331 * Returns: 0 on success, negative number in case of error.
332 */
333 int mnt_context_enable_lazy(struct libmnt_context *cxt, int enable)
334 {
335 return set_flag(cxt, MNT_FL_LAZY, enable);
336 }
337
338 /**
339 * mnt_context_is_lazy:
340 * @cxt: mount context
341 *
342 * Returns: 1 if lazy umount is enabled or 0
343 */
344 int mnt_context_is_lazy(struct libmnt_context *cxt)
345 {
346 return cxt->flags & MNT_FL_LAZY ? 1 : 0;
347 }
348
349 /**
350 * mnt_context_enable_fork:
351 * @cxt: mount context
352 * @enable: TRUE or FALSE
353 *
354 * Enable/disable fork(2) call in mnt_context_next_mount() (see mount(8) man
355 * page, option -F).
356 *
357 * Returns: 0 on success, negative number in case of error.
358 */
359 int mnt_context_enable_fork(struct libmnt_context *cxt, int enable)
360 {
361 return set_flag(cxt, MNT_FL_FORK, enable);
362 }
363
364 /**
365 * mnt_context_is_fork:
366 * @cxt: mount context
367 *
368 * Returns: 1 if fork (mount -F) is enabled or 0
369 */
370 int mnt_context_is_fork(struct libmnt_context *cxt)
371 {
372 return cxt->flags & MNT_FL_FORK ? 1 : 0;
373 }
374
375 /**
376 * mnt_context_is_parent:
377 * @cxt: mount context
378 *
379 * Return: 1 if mount -F enabled and the current context is parent, or 0
380 */
381 int mnt_context_is_parent(struct libmnt_context *cxt)
382 {
383 return mnt_context_is_fork(cxt) && cxt->pid == 0;
384 }
385
386 /**
387 * mnt_context_is_child:
388 * @cxt: mount context
389 *
390 * Return: 1 f the current context is child, or 0
391 */
392 int mnt_context_is_child(struct libmnt_context *cxt)
393 {
394 /* See mnt_fork_context(), the for fork flag is always disabled
395 * for children to avoid recursive forking.
396 */
397 return !mnt_context_is_fork(cxt) && cxt->pid;
398 }
399
400 /**
401 * mnt_context_enable_rdonly_umount:
402 * @cxt: mount context
403 * @enable: TRUE or FALSE
404 *
405 * Enable/disable read-only remount on failed umount(2)
406 * (see umount(8) man page, option -r).
407 *
408 * Returns: 0 on success, negative number in case of error.
409 */
410 int mnt_context_enable_rdonly_umount(struct libmnt_context *cxt, int enable)
411 {
412 return set_flag(cxt, MNT_FL_RDONLY_UMOUNT, enable);
413 }
414
415 /**
416 * mnt_context_is_rdonly_umount
417 * @cxt: mount context
418 *
419 * See also mnt_context_enable_rdonly_umount() and umount(8) man page,
420 * option -r.
421 *
422 * Returns: 1 if read-only remount failed umount(2) is enables or 0
423 */
424 int mnt_context_is_rdonly_umount(struct libmnt_context *cxt)
425 {
426 return cxt->flags & MNT_FL_RDONLY_UMOUNT ? 1 : 0;
427 }
428
429 /**
430 * mnt_context_disable_helpers:
431 * @cxt: mount context
432 * @disable: TRUE or FALSE
433 *
434 * Enable/disable /sbin/[u]mount.* helpers (see mount(8) man page, option -i).
435 *
436 * Returns: 0 on success, negative number in case of error.
437 */
438 int mnt_context_disable_helpers(struct libmnt_context *cxt, int disable)
439 {
440 return set_flag(cxt, MNT_FL_NOHELPERS, disable);
441 }
442
443 /**
444 * mnt_context_is_nohelpers
445 * @cxt: mount context
446 *
447 * Returns: 1 if helpers are disabled (mount -i) or 0
448 */
449 int mnt_context_is_nohelpers(struct libmnt_context *cxt)
450 {
451 return cxt->flags & MNT_FL_NOHELPERS ? 1 : 0;
452 }
453
454
455 /**
456 * mnt_context_enable_sloppy:
457 * @cxt: mount context
458 * @enable: TRUE or FALSE
459 *
460 * Set/unset sloppy mounting (see mount(8) man page, option -s).
461 *
462 * Returns: 0 on success, negative number in case of error.
463 */
464 int mnt_context_enable_sloppy(struct libmnt_context *cxt, int enable)
465 {
466 return set_flag(cxt, MNT_FL_SLOPPY, enable);
467 }
468
469 /**
470 * mnt_context_is_sloppy:
471 * @cxt: mount context
472 *
473 * Returns: 1 if sloppy flag is enabled or 0
474 */
475 int mnt_context_is_sloppy(struct libmnt_context *cxt)
476 {
477 return cxt->flags & MNT_FL_SLOPPY ? 1 : 0;
478 }
479
480 /**
481 * mnt_context_enable_fake:
482 * @cxt: mount context
483 * @enable: TRUE or FALSE
484 *
485 * Enable/disable fake mounting (see mount(8) man page, option -f).
486 *
487 * Returns: 0 on success, negative number in case of error.
488 */
489 int mnt_context_enable_fake(struct libmnt_context *cxt, int enable)
490 {
491 return set_flag(cxt, MNT_FL_FAKE, enable);
492 }
493
494 /**
495 * mnt_context_is_fake:
496 * @cxt: mount context
497 *
498 * Returns: 1 if fake flag is enabled or 0
499 */
500 int mnt_context_is_fake(struct libmnt_context *cxt)
501 {
502 return cxt->flags & MNT_FL_FAKE ? 1 : 0;
503 }
504
505 /**
506 * mnt_context_disable_mtab:
507 * @cxt: mount context
508 * @disable: TRUE or FALSE
509 *
510 * Disable/enable mtab update (see mount(8) man page, option -n).
511 *
512 * Returns: 0 on success, negative number in case of error.
513 */
514 int mnt_context_disable_mtab(struct libmnt_context *cxt, int disable)
515 {
516 return set_flag(cxt, MNT_FL_NOMTAB, disable);
517 }
518
519 /**
520 * mnt_context_is_nomtab:
521 * @cxt: mount context
522 *
523 * Returns: 1 if no-mtab is enabled or 0
524 */
525 int mnt_context_is_nomtab(struct libmnt_context *cxt)
526 {
527 return cxt->flags & MNT_FL_NOMTAB ? 1 : 0;
528 }
529
530 /**
531 * mnt_context_disable_swapmatch:
532 * @cxt: mount context
533 * @disable: TRUE or FALSE
534 *
535 * Disable/enable swap between source and target for mount(8) if only one path
536 * is specified.
537 *
538 * Returns: 0 on success, negative number in case of error.
539 */
540 int mnt_context_disable_swapmatch(struct libmnt_context *cxt, int disable)
541 {
542 return set_flag(cxt, MNT_FL_NOSWAPMATCH, disable);
543 }
544
545 /**
546 * mnt_context_is_swapmatch:
547 * @cxt: mount context
548 *
549 * Returns: 1 if swap between source and target is allowed (default is 1) or 0.
550 */
551 int mnt_context_is_swapmatch(struct libmnt_context *cxt)
552 {
553 return cxt->flags & MNT_FL_NOSWAPMATCH ? 0 : 1;
554 }
555
556 /**
557 * mnt_context_enable_force:
558 * @cxt: mount context
559 * @enable: TRUE or FALSE
560 *
561 * Enable/disable force umounting (see umount(8) man page, option -f).
562 *
563 * Returns: 0 on success, negative number in case of error.
564 */
565 int mnt_context_enable_force(struct libmnt_context *cxt, int enable)
566 {
567 return set_flag(cxt, MNT_FL_FORCE, enable);
568 }
569
570 /**
571 * mnt_context_is_force
572 * @cxt: mount context
573 *
574 * Returns: 1 if force umounting flag is enabled or 0
575 */
576 int mnt_context_is_force(struct libmnt_context *cxt)
577 {
578 return cxt->flags & MNT_FL_FORCE ? 1 : 0;
579 }
580
581 /**
582 * mnt_context_enable_verbose:
583 * @cxt: mount context
584 * @enable: TRUE or FALSE
585 *
586 * Enable/disable verbose output (TODO: not implemented yet)
587 *
588 * Returns: 0 on success, negative number in case of error.
589 */
590 int mnt_context_enable_verbose(struct libmnt_context *cxt, int enable)
591 {
592 return set_flag(cxt, MNT_FL_VERBOSE, enable);
593 }
594
595 /**
596 * mnt_context_is_verbose
597 * @cxt: mount context
598 *
599 * Returns: 1 if verbose flag is enabled or 0
600 */
601 int mnt_context_is_verbose(struct libmnt_context *cxt)
602 {
603 return cxt->flags & MNT_FL_VERBOSE ? 1 : 0;
604 }
605
606 /**
607 * mnt_context_enable_loopdel:
608 * @cxt: mount context
609 * @enable: TRUE or FALSE
610 *
611 * Enable/disable the loop delete (destroy) after umount (see umount(8), option -d)
612 *
613 * Returns: 0 on success, negative number in case of error.
614 */
615 int mnt_context_enable_loopdel(struct libmnt_context *cxt, int enable)
616 {
617 return set_flag(cxt, MNT_FL_LOOPDEL, enable);
618 }
619
620 /**
621 * mnt_context_is_loopdel:
622 * @cxt: mount context
623 *
624 * Returns: 1 if loop device should be deleted after umount (umount -d) or 0.
625 */
626 int mnt_context_is_loopdel(struct libmnt_context *cxt)
627 {
628 return cxt->flags & MNT_FL_LOOPDEL ? 1 : 0;
629 }
630
631 /**
632 * mnt_context_set_fs:
633 * @cxt: mount context
634 * @fs: filesystem description
635 *
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
639 * old fs).
640 *
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.
644 *
645 * Returns: 0 on success, negative number in case of error.
646 */
647 int mnt_context_set_fs(struct libmnt_context *cxt, struct libmnt_fs *fs)
648 {
649 if (!cxt)
650 return -EINVAL;
651
652 mnt_ref_fs(fs); /* new */
653 mnt_unref_fs(cxt->fs); /* old */
654 cxt->fs = fs;
655 return 0;
656 }
657
658 /**
659 * mnt_context_get_fs:
660 * @cxt: mount context
661 *
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}
664 * functions.
665 *
666 * Returns: pointer to FS description or NULL in case of a calloc() error.
667 */
668 struct libmnt_fs *mnt_context_get_fs(struct libmnt_context *cxt)
669 {
670 assert(cxt);
671 if (!cxt)
672 return NULL;
673 if (!cxt->fs)
674 cxt->fs = mnt_new_fs();
675 return cxt->fs;
676 }
677
678 /**
679 * mnt_context_get_fs_userdata:
680 * @cxt: mount context
681 *
682 * Returns: pointer to userdata or NULL.
683 */
684 void *mnt_context_get_fs_userdata(struct libmnt_context *cxt)
685 {
686 assert(cxt);
687 return cxt->fs ? mnt_fs_get_userdata(cxt->fs) : NULL;
688 }
689
690 /**
691 * mnt_context_get_fstab_userdata:
692 * @cxt: mount context
693 *
694 * Returns: pointer to userdata or NULL.
695 */
696 void *mnt_context_get_fstab_userdata(struct libmnt_context *cxt)
697 {
698 assert(cxt);
699 return cxt->fstab ? mnt_table_get_userdata(cxt->fstab) : NULL;
700 }
701
702 /**
703 * mnt_context_get_mtab_userdata:
704 * @cxt: mount context
705 *
706 * Returns: pointer to userdata or NULL.
707 */
708 void *mnt_context_get_mtab_userdata(struct libmnt_context *cxt)
709 {
710 assert(cxt);
711 return cxt->mtab ? mnt_table_get_userdata(cxt->mtab) : NULL;
712 }
713
714 /**
715 * mnt_context_set_source:
716 * @cxt: mount context
717 * @source: mount source (device, directory, UUID, LABEL, ...)
718 *
719 * Returns: 0 on success, negative number in case of error.
720 */
721 int mnt_context_set_source(struct libmnt_context *cxt, const char *source)
722 {
723 assert(cxt);
724 return mnt_fs_set_source(mnt_context_get_fs(cxt), source);
725 }
726
727 /**
728 * mnt_context_get_source:
729 * @cxt: mount context
730 *
731 * Returns: returns pointer or NULL in case of error or if not set.
732 */
733 const char *mnt_context_get_source(struct libmnt_context *cxt)
734 {
735 assert(cxt);
736 return mnt_fs_get_source(mnt_context_get_fs(cxt));
737 }
738
739 /**
740 * mnt_context_set_target:
741 * @cxt: mount context
742 * @target: mountpoint
743 *
744 * Returns: 0 on success, negative number in case of error.
745 */
746 int mnt_context_set_target(struct libmnt_context *cxt, const char *target)
747 {
748 assert(cxt);
749 return mnt_fs_set_target(mnt_context_get_fs(cxt), target);
750 }
751
752 /**
753 * mnt_context_get_target:
754 * @cxt: mount context
755 *
756 * Returns: returns pointer or NULL in case of error or if not set.
757 */
758 const char *mnt_context_get_target(struct libmnt_context *cxt)
759 {
760 assert(cxt);
761 return mnt_fs_get_target(mnt_context_get_fs(cxt));
762 }
763
764 /**
765 * mnt_context_set_fstype:
766 * @cxt: mount context
767 * @fstype: filesystem type
768 *
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().
772 *
773 * Returns: 0 on success, negative number in case of error.
774 */
775 int mnt_context_set_fstype(struct libmnt_context *cxt, const char *fstype)
776 {
777 assert(cxt);
778 return mnt_fs_set_fstype(mnt_context_get_fs(cxt), fstype);
779 }
780
781 /**
782 * mnt_context_get_fstype:
783 * @cxt: mount context
784 *
785 * Returns: pointer or NULL in case of error or if not set.
786 */
787 const char *mnt_context_get_fstype(struct libmnt_context *cxt)
788 {
789 assert(cxt);
790 return mnt_fs_get_fstype(mnt_context_get_fs(cxt));
791 }
792
793 /**
794 * mnt_context_set_options:
795 * @cxt: mount context
796 * @optstr: comma delimited mount options
797 *
798 * Returns: 0 on success, negative number in case of error.
799 */
800 int mnt_context_set_options(struct libmnt_context *cxt, const char *optstr)
801 {
802 assert(cxt);
803 return mnt_fs_set_options(mnt_context_get_fs(cxt), optstr);
804 }
805
806 /**
807 * mnt_context_append_options:
808 * @cxt: mount context
809 * @optstr: comma delimited mount options
810 *
811 * Returns: 0 on success, negative number in case of error.
812 */
813 int mnt_context_append_options(struct libmnt_context *cxt, const char *optstr)
814 {
815 assert(cxt);
816 return mnt_fs_append_options(mnt_context_get_fs(cxt), optstr);
817 }
818
819 /**
820 * mnt_context_get_options:
821 * @cxt: mount context
822 *
823 * This function returns mount options set by mnt_context_set_options() or
824 * mnt_context_append_options().
825 *
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.
829 *
830 * Returns: pointer or NULL
831 */
832 const char *mnt_context_get_options(struct libmnt_context *cxt)
833 {
834 assert(cxt);
835 return mnt_fs_get_options(mnt_context_get_fs(cxt));
836 }
837
838 /**
839 * mnt_context_set_fstype_pattern:
840 * @cxt: mount context
841 * @pattern: FS name pattern (or NULL to reset the current setting)
842 *
843 * See mount(8), option -t.
844 *
845 * Returns: 0 on success, negative number in case of error.
846 */
847 int mnt_context_set_fstype_pattern(struct libmnt_context *cxt, const char *pattern)
848 {
849 char *p = NULL;
850
851 assert(cxt);
852 if (!cxt)
853 return -EINVAL;
854 if (pattern) {
855 p = strdup(pattern);
856 if (!p)
857 return -ENOMEM;
858 }
859 free(cxt->fstype_pattern);
860 cxt->fstype_pattern = p;
861 return 0;
862 }
863
864 /**
865 * mnt_context_set_options_pattern:
866 * @cxt: mount context
867 * @pattern: options pattern (or NULL to reset the current setting)
868 *
869 * See mount(8), option -O.
870 *
871 * Returns: 0 on success, negative number in case of error.
872 */
873 int mnt_context_set_options_pattern(struct libmnt_context *cxt, const char *pattern)
874 {
875 char *p = NULL;
876
877 assert(cxt);
878 if (!cxt)
879 return -EINVAL;
880 if (pattern) {
881 p = strdup(pattern);
882 if (!p)
883 return -ENOMEM;
884 }
885 free(cxt->optstr_pattern);
886 cxt->optstr_pattern = p;
887 return 0;
888 }
889
890 /**
891 * mnt_context_set_fstab:
892 * @cxt: mount context
893 * @tb: fstab
894 *
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
897 * instance.
898 *
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));
902 *
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.)
905 *
906 * If the @tb argument is NULL, then the current private fstab instance is
907 * reset.
908 *
909 * Returns: 0 on success, negative number in case of error.
910 */
911 int mnt_context_set_fstab(struct libmnt_context *cxt, struct libmnt_table *tb)
912 {
913 assert(cxt);
914 if (!cxt)
915 return -EINVAL;
916
917 mnt_ref_table(tb); /* new */
918 mnt_unref_table(cxt->fstab); /* old */
919
920 cxt->fstab = tb;
921 return 0;
922 }
923
924 /**
925 * mnt_context_get_fstab:
926 * @cxt: mount context
927 * @tb: returns fstab
928 *
929 * See also mnt_table_parse_fstab() for more details about fstab.
930 *
931 * Returns: 0 on success, negative number in case of error.
932 */
933 int mnt_context_get_fstab(struct libmnt_context *cxt, struct libmnt_table **tb)
934 {
935 assert(cxt);
936 if (!cxt)
937 return -EINVAL;
938 if (!cxt->fstab) {
939 int rc;
940
941 cxt->fstab = mnt_new_table();
942 if (!cxt->fstab)
943 return -ENOMEM;
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);
948 if (rc)
949 return rc;
950 }
951
952 if (tb)
953 *tb = cxt->fstab;
954 return 0;
955 }
956
957 /**
958 * mnt_context_get_mtab:
959 * @cxt: mount context
960 * @tb: returns mtab
961 *
962 * See also mnt_table_parse_mtab() for more details about mtab/mountinfo. The
963 * result will be deallocated by mnt_free_context(@cxt).
964 *
965 * Returns: 0 on success, negative number in case of error.
966 */
967 int mnt_context_get_mtab(struct libmnt_context *cxt, struct libmnt_table **tb)
968 {
969 assert(cxt);
970 if (!cxt)
971 return -EINVAL;
972 if (!cxt->mtab) {
973 int rc;
974
975 cxt->mtab = mnt_new_table();
976 if (!cxt->mtab)
977 return -ENOMEM;
978
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,
983 cxt->table_fltrcb,
984 cxt->table_fltrcb_data);
985
986 mnt_table_set_cache(cxt->mtab, mnt_context_get_cache(cxt));
987 if (cxt->utab)
988 /* utab already parsed, don't parse it again */
989 rc = __mnt_table_parse_mtab(cxt->mtab,
990 cxt->mtab_path, cxt->utab);
991 else
992 rc = mnt_table_parse_mtab(cxt->mtab, cxt->mtab_path);
993 if (rc)
994 return rc;
995 }
996
997 if (tb)
998 *tb = cxt->mtab;
999
1000 DBG(CXT, mnt_debug_h(cxt, "mtab requested [nents=%d]",
1001 mnt_table_get_nents(cxt->mtab)));
1002 return 0;
1003 }
1004
1005 /*
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.
1008 */
1009 int mnt_context_set_tabfilter(struct libmnt_context *cxt,
1010 int (*fltr)(struct libmnt_fs *, void *),
1011 void *data)
1012 {
1013 assert(cxt);
1014 if (!cxt)
1015 return -EINVAL;
1016
1017 cxt->table_fltrcb = fltr;
1018 cxt->table_fltrcb_data = data;
1019
1020 if (cxt->mtab)
1021 mnt_table_set_parser_fltrcb(cxt->mtab,
1022 cxt->table_fltrcb,
1023 cxt->table_fltrcb_data);
1024
1025 DBG(CXT, mnt_debug_h(cxt, "tabfilter %s", fltr ? "ENABLED!" : "disabled"));
1026 return 0;
1027 }
1028
1029 /**
1030 * mnt_context_get_table:
1031 * @cxt: mount context
1032 * @filename: e.g. /proc/self/mountinfo
1033 * @tb: returns the table
1034 *
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().
1038 *
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
1042 * options.
1043 *
1044 * The result will NOT be deallocated by mnt_free_context(@cxt).
1045 *
1046 * Returns: 0 on success, negative number in case of error.
1047 */
1048 int mnt_context_get_table(struct libmnt_context *cxt,
1049 const char *filename, struct libmnt_table **tb)
1050 {
1051 int rc;
1052
1053 assert(cxt);
1054 assert(tb);
1055 if (!cxt || !tb)
1056 return -EINVAL;
1057
1058 *tb = mnt_new_table();
1059 if (!*tb)
1060 return -ENOMEM;
1061
1062 if (cxt->table_errcb)
1063 mnt_table_set_parser_errcb(*tb, cxt->table_errcb);
1064
1065 rc = mnt_table_parse_file(*tb, filename);
1066 if (rc) {
1067 mnt_unref_table(*tb);
1068 return rc;
1069 }
1070
1071 mnt_table_set_cache(*tb, mnt_context_get_cache(cxt));
1072 return 0;
1073 }
1074
1075 /**
1076 * mnt_context_set_tables_errcb
1077 * @cxt: mount context
1078 * @cb: pointer to callback function
1079 *
1080 * The error callback is used for all tab files (e.g. mtab, fstab)
1081 * parsed within the context.
1082 *
1083 * See also mnt_context_get_mtab(),
1084 * mnt_context_get_fstab(),
1085 * mnt_table_set_parser_errcb().
1086 *
1087 * Returns: 0 on success, negative number in case of error.
1088 */
1089 int mnt_context_set_tables_errcb(struct libmnt_context *cxt,
1090 int (*cb)(struct libmnt_table *tb, const char *filename, int line))
1091 {
1092 assert(cxt);
1093 if (!cxt)
1094 return -EINVAL;
1095
1096 if (cxt->mtab)
1097 mnt_table_set_parser_errcb(cxt->mtab, cb);
1098 if (cxt->fstab)
1099 mnt_table_set_parser_errcb(cxt->fstab, cb);
1100
1101 cxt->table_errcb = cb;
1102 return 0;
1103 }
1104
1105 /**
1106 * mnt_context_set_cache:
1107 * @cxt: mount context
1108 * @cache: cache instance or nULL
1109 *
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.
1113 *
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
1116 * exists).
1117 *
1118 * The old cache instance reference counter is de-incremented.
1119 *
1120 * Returns: 0 on success, negative number in case of error.
1121 */
1122 int mnt_context_set_cache(struct libmnt_context *cxt, struct libmnt_cache *cache)
1123 {
1124 if (!cxt)
1125 return -EINVAL;
1126
1127 mnt_ref_cache(cache); /* new */
1128 mnt_unref_cache(cxt->cache); /* old */
1129
1130 cxt->cache = cache;
1131
1132 if (cxt->mtab)
1133 mnt_table_set_cache(cxt->mtab, cache);
1134 if (cxt->fstab)
1135 mnt_table_set_cache(cxt->fstab, cache);
1136
1137 return 0;
1138 }
1139
1140 /**
1141 * mnt_context_get_cache
1142 * @cxt: mount context
1143 *
1144 * See also mnt_context_set_cache().
1145 *
1146 * Returns: pointer to cache or NULL if canonicalization is disabled.
1147 */
1148 struct libmnt_cache *mnt_context_get_cache(struct libmnt_context *cxt)
1149 {
1150 assert(cxt);
1151 if (!cxt || mnt_context_is_nocanonicalize(cxt))
1152 return NULL;
1153
1154 if (!cxt->cache) {
1155 struct libmnt_cache *cache = mnt_new_cache();
1156 mnt_context_set_cache(cxt, cache);
1157 mnt_unref_cache(cache);
1158 }
1159 return cxt->cache;
1160 }
1161
1162 /**
1163 * mnt_context_set_passwd_cb:
1164 * @cxt: mount context
1165 * @get: callback to get password
1166 * @release: callback to release (delallocate) password
1167 *
1168 * Sets callbacks for encryption password (e.g encrypted loopdev). This
1169 * function is deprecated (encrypted loops are no longer supported).
1170 *
1171 * Returns: 0 on success, negative number in case of error.
1172 */
1173 int mnt_context_set_passwd_cb(struct libmnt_context *cxt,
1174 char *(*get)(struct libmnt_context *),
1175 void (*release)(struct libmnt_context *, char *))
1176 {
1177 assert(cxt);
1178 if (!cxt)
1179 return -EINVAL;
1180 cxt->pwd_get_cb = get;
1181 cxt->pwd_release_cb = release;
1182 return 0;
1183 }
1184
1185 /**
1186 * mnt_context_get_lock:
1187 * @cxt: mount context
1188 *
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.
1192 *
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:
1196 *
1197 * lc = mnt_context_get_lock(cxt);
1198 * if (lc)
1199 * mnt_lock_block_signals(lc, FALSE);
1200 *
1201 * and don't forget to call mnt_unlock_file(lc) before exit.
1202 *
1203 * Returns: pointer to lock struct or NULL.
1204 */
1205 struct libmnt_lock *mnt_context_get_lock(struct libmnt_context *cxt)
1206 {
1207 assert(cxt);
1208 /*
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.
1212 */
1213 if (!cxt || mnt_context_is_nomtab(cxt))
1214 return NULL;
1215
1216 if (!cxt->lock) {
1217 cxt->lock = mnt_new_lock(cxt->mtab_writable ?
1218 cxt->mtab_path : cxt->utab_path, 0);
1219 if (cxt->lock)
1220 mnt_lock_block_signals(cxt->lock, TRUE);
1221 }
1222 return cxt->lock;
1223 }
1224
1225 /**
1226 * mnt_context_set_mflags:
1227 * @cxt: mount context
1228 * @flags: mount(2) flags (MS_* flags)
1229 *
1230 * Sets mount flags (see mount(2) man page).
1231 *
1232 * Note that mount context allows to define mount options by mount flags. It
1233 * means you can for example use
1234 *
1235 * mnt_context_set_mflags(cxt, MS_NOEXEC | MS_NOSUID);
1236 *
1237 * rather than
1238 *
1239 * mnt_context_set_options(cxt, "noexec,nosuid");
1240 *
1241 * both of these calls have the same effect.
1242 *
1243 * Returns: 0 on success, negative number in case of error.
1244 */
1245 int mnt_context_set_mflags(struct libmnt_context *cxt, unsigned long flags)
1246 {
1247 assert(cxt);
1248 if (!cxt)
1249 return -EINVAL;
1250
1251 cxt->mountflags = flags;
1252
1253 if ((cxt->flags & MNT_FL_MOUNTOPTS_FIXED) && cxt->fs)
1254 /*
1255 * the final mount options are already generated, refresh...
1256 */
1257 return mnt_optstr_apply_flags(
1258 &cxt->fs->vfs_optstr,
1259 cxt->mountflags,
1260 mnt_get_builtin_optmap(MNT_LINUX_MAP));
1261
1262 return 0;
1263 }
1264
1265 /**
1266 * mnt_context_get_mflags:
1267 * @cxt: mount context
1268 * @flags: returns MS_* mount flags
1269 *
1270 * Converts mount options string to MS_* flags and bitewise-OR the result with
1271 * the already defined flags (see mnt_context_set_mflags()).
1272 *
1273 * Returns: 0 on success, negative number in case of error.
1274 */
1275 int mnt_context_get_mflags(struct libmnt_context *cxt, unsigned long *flags)
1276 {
1277 int rc = 0;
1278 struct list_head *p;
1279
1280 assert(cxt);
1281 assert(flags);
1282 if (!cxt || !flags)
1283 return -EINVAL;
1284
1285 *flags = 0;
1286 if (!(cxt->flags & MNT_FL_MOUNTFLAGS_MERGED) && cxt->fs) {
1287 const char *o = mnt_fs_get_options(cxt->fs);
1288 if (o)
1289 rc = mnt_optstr_get_flags(o, flags,
1290 mnt_get_builtin_optmap(MNT_LINUX_MAP));
1291 }
1292
1293 list_for_each(p, &cxt->addmounts) {
1294 struct libmnt_addmount *ad =
1295 list_entry(p, struct libmnt_addmount, mounts);
1296
1297 *flags |= ad->mountflags;
1298 }
1299
1300 if (!rc)
1301 *flags |= cxt->mountflags;
1302 return rc;
1303 }
1304
1305 /**
1306 * mnt_context_set_user_mflags:
1307 * @cxt: mount context
1308 * @flags: mount(2) flags (MNT_MS_* flags, e.g. MNT_MS_LOOP)
1309 *
1310 * Sets userspace mount flags.
1311 *
1312 * See also notes for mnt_context_set_mflags().
1313 *
1314 * Returns: 0 on success, negative number in case of error.
1315 */
1316 int mnt_context_set_user_mflags(struct libmnt_context *cxt, unsigned long flags)
1317 {
1318 assert(cxt);
1319 if (!cxt)
1320 return -EINVAL;
1321 cxt->user_mountflags = flags;
1322 return 0;
1323 }
1324
1325 /**
1326 * mnt_context_get_user_mflags:
1327 * @cxt: mount context
1328 * @flags: returns mount flags
1329 *
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()).
1332 *
1333 * Returns: 0 on success, negative number in case of error.
1334 */
1335 int mnt_context_get_user_mflags(struct libmnt_context *cxt, unsigned long *flags)
1336 {
1337 int rc = 0;
1338
1339 assert(cxt);
1340 assert(flags);
1341 if (!cxt || !flags)
1342 return -EINVAL;
1343
1344 *flags = 0;
1345 if (!(cxt->flags & MNT_FL_MOUNTFLAGS_MERGED) && cxt->fs) {
1346 const char *o = mnt_fs_get_user_options(cxt->fs);
1347 if (o)
1348 rc = mnt_optstr_get_flags(o, flags,
1349 mnt_get_builtin_optmap(MNT_USERSPACE_MAP));
1350 }
1351 if (!rc)
1352 *flags |= cxt->user_mountflags;
1353 return rc;
1354 }
1355
1356 /**
1357 * mnt_context_set_mountdata:
1358 * @cxt: mount context
1359 * @data: mount(2) data
1360 *
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
1363 * of mount options.
1364 *
1365 * The libmount does not deallocate the data by mnt_free_context(). Note that
1366 * NULL is also valid mount data.
1367 *
1368 * Returns: 0 on success, negative number in case of error.
1369 */
1370 int mnt_context_set_mountdata(struct libmnt_context *cxt, void *data)
1371 {
1372 assert(cxt);
1373 if (!cxt)
1374 return -EINVAL;
1375 cxt->mountdata = data;
1376 cxt->flags |= MNT_FL_MOUNTDATA;
1377 return 0;
1378 }
1379
1380 /*
1381 * Translates LABEL/UUID/path to mountable path
1382 */
1383 int mnt_context_prepare_srcpath(struct libmnt_context *cxt)
1384 {
1385 const char *path = NULL;
1386 struct libmnt_cache *cache;
1387 const char *t, *v, *src;
1388 int rc = 0;
1389
1390 assert(cxt);
1391 assert(cxt->fs);
1392 assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED));
1393
1394 if (!cxt || !cxt->fs)
1395 return -EINVAL;
1396
1397 DBG(CXT, mnt_debug_h(cxt, "preparing source path"));
1398
1399 src = mnt_fs_get_source(cxt->fs);
1400
1401 if (!src && mnt_context_propagation_only(cxt))
1402 /* mount --make-{shared,private,...} */
1403 return mnt_fs_set_source(cxt->fs, "none");
1404
1405 /* ignore filesystems without source or filesystems
1406 * where the source is a quasi-path (//foo/bar)
1407 */
1408 if (!src || mnt_fs_is_netfs(cxt->fs))
1409 return 0;
1410
1411 DBG(CXT, mnt_debug_h(cxt, "srcpath '%s'", src));
1412
1413 cache = mnt_context_get_cache(cxt);
1414
1415 if (!mnt_fs_get_tag(cxt->fs, &t, &v)) {
1416 /*
1417 * Source is TAG (evaluate)
1418 */
1419 if (cache)
1420 path = mnt_resolve_tag(t, v, cache);
1421
1422 rc = path ? mnt_fs_set_source(cxt->fs, path) : -MNT_ERR_NOSOURCE;
1423
1424 } else if (cache && !mnt_fs_is_pseudofs(cxt->fs)) {
1425 /*
1426 * Source is PATH (canonicalize)
1427 */
1428 path = mnt_resolve_path(src, cache);
1429 if (path && strcmp(path, src))
1430 rc = mnt_fs_set_source(cxt->fs, path);
1431 }
1432
1433 if (rc) {
1434 DBG(CXT, mnt_debug_h(cxt, "failed to prepare srcpath [rc=%d]", rc));
1435 return rc;
1436 }
1437
1438 if (!path)
1439 path = src;
1440
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));
1444 return rc;
1445 }
1446
1447 /*
1448 * Initialize loop device
1449 */
1450 if (mnt_context_is_loopdev(cxt)) {
1451 rc = mnt_context_setup_loopdev(cxt);
1452 if (rc)
1453 return rc;
1454 }
1455
1456 DBG(CXT, mnt_debug_h(cxt, "final srcpath '%s'",
1457 mnt_fs_get_source(cxt->fs)));
1458 return 0;
1459 }
1460
1461 /* create a mountpoint if x-mount.mkdir[=<mode>] specified */
1462 static int mkdir_target(const char *tgt, struct libmnt_fs *fs)
1463 {
1464 char *mstr = NULL;
1465 size_t mstr_sz = 0;
1466 mode_t mode = 0;
1467 struct stat st;
1468 int rc;
1469
1470 assert(tgt);
1471 assert(fs);
1472
1473 if (mnt_optstr_get_option(fs->user_optstr, "x-mount.mkdir", &mstr, &mstr_sz) != 0)
1474 return 0;
1475 if (stat(tgt, &st) == 0)
1476 return 0;
1477
1478 if (mstr && mstr_sz) {
1479 char *end = NULL;
1480
1481 errno = 0;
1482 mode = strtol(mstr, &end, 8);
1483
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;
1487 }
1488 }
1489
1490 if (!mode)
1491 mode = S_IRWXU | /* 0755 */
1492 S_IRGRP | S_IXGRP |
1493 S_IROTH | S_IXOTH;
1494
1495 rc = mkdir_p(tgt, mode);
1496 if (rc)
1497 DBG(CXT, mnt_debug("mkdir %s failed: %m", tgt));
1498
1499 return rc;
1500 }
1501
1502 int mnt_context_prepare_target(struct libmnt_context *cxt)
1503 {
1504 const char *tgt;
1505 struct libmnt_cache *cache;
1506 int rc = 0;
1507
1508 assert(cxt);
1509 assert(cxt->fs);
1510 assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED));
1511
1512 if (!cxt || !cxt->fs)
1513 return -EINVAL;
1514
1515 DBG(CXT, mnt_debug_h(cxt, "preparing target path"));
1516
1517 tgt = mnt_fs_get_target(cxt->fs);
1518 if (!tgt)
1519 return 0;
1520
1521 /* mkdir target */
1522 if (cxt->action == MNT_ACT_MOUNT
1523 && !mnt_context_is_restricted(cxt)
1524 && cxt->user_mountflags & MNT_MS_XCOMMENT) {
1525
1526 rc = mkdir_target(tgt, cxt->fs);
1527 if (rc)
1528 return rc; /* mkdir or parse error */
1529 }
1530
1531 /* canonicalize the path */
1532 cache = mnt_context_get_cache(cxt);
1533 if (cache) {
1534 char *path = mnt_resolve_path(tgt, cache);
1535 if (path && strcmp(path, tgt) != 0)
1536 rc = mnt_fs_set_target(cxt->fs, path);
1537 }
1538
1539 if (rc)
1540 DBG(CXT, mnt_debug_h(cxt, "failed to prepare target '%s'", tgt));
1541 else
1542 DBG(CXT, mnt_debug_h(cxt, "final target '%s'",
1543 mnt_fs_get_target(cxt->fs)));
1544 return 0;
1545 }
1546
1547 /*
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.
1550 */
1551 int mnt_context_guess_fstype(struct libmnt_context *cxt)
1552 {
1553 char *type;
1554 const char *dev;
1555 int rc = 0;
1556
1557 assert(cxt);
1558 assert(cxt->fs);
1559 assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED));
1560
1561 if (!cxt || !cxt->fs)
1562 return -EINVAL;
1563
1564 if ((cxt->mountflags & (MS_BIND | MS_MOVE))
1565 || mnt_context_propagation_only(cxt))
1566 goto none;
1567
1568 type = (char *) mnt_fs_get_fstype(cxt->fs);
1569 if (type && !strcmp(type, "auto")) {
1570 mnt_fs_set_fstype(cxt->fs, NULL);
1571 type = NULL;
1572 }
1573
1574 if (type)
1575 goto done;
1576 if (cxt->flags & MS_REMOUNT)
1577 goto none;
1578 if (cxt->fstype_pattern)
1579 goto done;
1580
1581 dev = mnt_fs_get_srcpath(cxt->fs);
1582 if (!dev)
1583 goto done;
1584
1585 if (access(dev, F_OK) == 0) {
1586 struct libmnt_cache *cache = mnt_context_get_cache(cxt);
1587 int ambi = 0;
1588
1589 type = mnt_get_fstype(dev, &ambi, cache);
1590 if (type) {
1591 rc = mnt_fs_set_fstype(cxt->fs, type);
1592 if (!cache)
1593 free(type); /* type is not cached */
1594 }
1595 if (ambi)
1596 rc = -MNT_ERR_AMBIFS;
1597 } else {
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");
1603 }
1604
1605 done:
1606 DBG(CXT, mnt_debug_h(cxt, "FS type: %s [rc=%d]",
1607 mnt_fs_get_fstype(cxt->fs), rc));
1608 return rc;
1609 none:
1610 return mnt_fs_set_fstype(cxt->fs, "none");
1611 }
1612
1613 /*
1614 * The default is to use fstype from cxt->fs, this could be overwritten by
1615 * @type. The @act is MNT_ACT_{MOUNT,UMOUNT}.
1616 *
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.
1619 */
1620 int mnt_context_prepare_helper(struct libmnt_context *cxt, const char *name,
1621 const char *type)
1622 {
1623 char search_path[] = FS_SEARCH_PATH; /* from config.h */
1624 char *p = NULL, *path;
1625
1626 assert(cxt);
1627 assert(cxt->fs);
1628 assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED));
1629
1630 if (!type)
1631 type = mnt_fs_get_fstype(cxt->fs);
1632
1633 if (type && strchr(type, ','))
1634 return 0; /* type is fstype pattern */
1635
1636 if (mnt_context_is_nohelpers(cxt)
1637 || !type
1638 || !strcmp(type, "none")
1639 || strstr(type, "/..") /* don't try to smuggle path */
1640 || mnt_fs_is_swaparea(cxt->fs))
1641 return 0;
1642
1643 path = strtok_r(search_path, ":", &p);
1644 while (path) {
1645 char helper[PATH_MAX];
1646 struct stat st;
1647 int rc;
1648
1649 rc = snprintf(helper, sizeof(helper), "%s/%s.%s",
1650 path, name, type);
1651 path = strtok_r(NULL, ":", &p);
1652
1653 if (rc < 0 || (size_t) rc >= sizeof(helper))
1654 continue;
1655
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, '.');
1660 if (hs)
1661 *hs = '\0';
1662 rc = stat(helper, &st);
1663 }
1664
1665 DBG(CXT, mnt_debug_h(cxt, "%-25s ... %s", helper,
1666 rc ? "not found" : "found"));
1667 if (rc)
1668 continue;
1669
1670 free(cxt->helper);
1671 cxt->helper = strdup(helper);
1672 if (!cxt->helper)
1673 return -ENOMEM;
1674 return 0;
1675 }
1676
1677 return 0;
1678 }
1679
1680 int mnt_context_merge_mflags(struct libmnt_context *cxt)
1681 {
1682 unsigned long fl = 0;
1683 int rc;
1684
1685 assert(cxt);
1686
1687 DBG(CXT, mnt_debug_h(cxt, "merging mount flags"));
1688
1689 rc = mnt_context_get_mflags(cxt, &fl);
1690 if (rc)
1691 return rc;
1692 cxt->mountflags = fl;
1693
1694 fl = 0;
1695 rc = mnt_context_get_user_mflags(cxt, &fl);
1696 if (rc)
1697 return rc;
1698 cxt->user_mountflags = fl;
1699
1700 DBG(CXT, mnt_debug_h(cxt, "final flags: VFS=%08lx user=%08lx",
1701 cxt->mountflags, cxt->user_mountflags));
1702
1703 cxt->flags |= MNT_FL_MOUNTFLAGS_MERGED;
1704 return 0;
1705 }
1706
1707 /*
1708 * Prepare /etc/mtab or /run/mount/utab
1709 */
1710 int mnt_context_prepare_update(struct libmnt_context *cxt)
1711 {
1712 int rc;
1713 const char *target;
1714
1715 assert(cxt);
1716 assert(cxt->fs);
1717 assert(cxt->action);
1718 assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED));
1719
1720 DBG(CXT, mnt_debug_h(cxt, "prepare update"));
1721
1722 if (mnt_context_propagation_only(cxt)) {
1723 DBG(CXT, mnt_debug_h(cxt, "skip update: only MS_PROPAGATION"));
1724 return 0;
1725 }
1726
1727 target = mnt_fs_get_target(cxt->fs);
1728
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);
1732
1733 if (mnt_context_is_nomtab(cxt)) {
1734 DBG(CXT, mnt_debug_h(cxt, "skip update: NOMTAB flag"));
1735 return 0;
1736 }
1737 if (!cxt->mtab_writable && !cxt->utab_writable) {
1738 DBG(CXT, mnt_debug_h(cxt, "skip update: no writable destination"));
1739 return 0;
1740 }
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));
1746 return 0;
1747 }
1748
1749 if (!cxt->update) {
1750 const char *name = cxt->mtab_writable ? cxt->mtab_path : cxt->utab_path;
1751
1752 if (cxt->action == MNT_ACT_UMOUNT && is_file_empty(name)) {
1753 DBG(CXT, mnt_debug_h(cxt,
1754 "skip update: umount, no table"));
1755 return 0;
1756 }
1757
1758 cxt->update = mnt_new_update();
1759 if (!cxt->update)
1760 return -ENOMEM;
1761
1762 mnt_update_set_filename(cxt->update, name, !cxt->mtab_writable);
1763 }
1764
1765 if (cxt->action == MNT_ACT_UMOUNT)
1766 rc = mnt_update_set_fs(cxt->update, cxt->mountflags,
1767 mnt_context_get_target(cxt), NULL);
1768 else
1769 rc = mnt_update_set_fs(cxt->update, cxt->mountflags,
1770 NULL, cxt->fs);
1771
1772 return rc < 0 ? rc : 0;
1773 }
1774
1775 int mnt_context_update_tabs(struct libmnt_context *cxt)
1776 {
1777 unsigned long fl;
1778
1779 assert(cxt);
1780
1781 if (mnt_context_is_nomtab(cxt)) {
1782 DBG(CXT, mnt_debug_h(cxt, "don't update: NOMTAB flag"));
1783 return 0;
1784 }
1785 if (!cxt->update || !mnt_update_is_ready(cxt->update)) {
1786 DBG(CXT, mnt_debug_h(cxt, "don't update: no update prepared"));
1787 return 0;
1788 }
1789
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) {
1794
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"));
1797 return 0;
1798 }
1799 } else if (cxt->helper) {
1800 DBG(CXT, mnt_debug_h(cxt, "don't update: external helper"));
1801 return 0;
1802 }
1803
1804 if (cxt->syscall_status != 0
1805 && !(mnt_context_helper_executed(cxt) &&
1806 mnt_context_get_helper_status(cxt) == 0)) {
1807
1808 DBG(CXT, mnt_debug_h(cxt, "don't update: syscall/helper failed/not called"));
1809 return 0;
1810 }
1811
1812 fl = mnt_update_get_mflags(cxt->update);
1813 if ((cxt->mountflags & MS_RDONLY) != (fl & MS_RDONLY))
1814 /*
1815 * fix MS_RDONLY in options
1816 */
1817 mnt_update_force_rdonly(cxt->update,
1818 cxt->mountflags & MS_RDONLY);
1819
1820 return mnt_update_table(cxt->update, cxt->lock);
1821 }
1822
1823 static int apply_table(struct libmnt_context *cxt, struct libmnt_table *tb,
1824 int direction)
1825 {
1826 struct libmnt_fs *fs = NULL;
1827 const char *src = NULL, *tgt = NULL;
1828 int rc;
1829
1830 assert(cxt);
1831 assert(cxt->fs);
1832
1833 if (!cxt->fs)
1834 return -EINVAL;
1835
1836 src = mnt_fs_get_source(cxt->fs);
1837 tgt = mnt_fs_get_target(cxt->fs);
1838
1839 if (tgt && src)
1840 fs = mnt_table_find_pair(tb, src, tgt, direction);
1841 else {
1842 if (src)
1843 fs = mnt_table_find_source(tb, src, direction);
1844 else if (tgt)
1845 fs = mnt_table_find_target(tb, tgt, direction);
1846
1847 if (!fs && mnt_context_is_swapmatch(cxt)) {
1848 /* swap source and target (if @src is not LABEL/UUID),
1849 * for example in
1850 *
1851 * mount /foo/bar
1852 *
1853 * the path could be a mountpoint as well as a source (for
1854 * example bind mount, symlink to a device, ...).
1855 */
1856 if (src && !mnt_fs_get_tag(cxt->fs, NULL, NULL))
1857 fs = mnt_table_find_target(tb, src, direction);
1858 if (!fs && tgt)
1859 fs = mnt_table_find_source(tb, tgt, direction);
1860 }
1861 }
1862
1863 if (!fs)
1864 return -MNT_ERR_NOFSTAB; /* not found */
1865
1866 DBG(CXT, mnt_debug_h(cxt, "apply entry:"));
1867 DBG(CXT, mnt_fs_print_debug(fs, stderr));
1868
1869 /* copy from tab to our FS description
1870 */
1871 rc = mnt_fs_set_source(cxt->fs, mnt_fs_get_source(fs));
1872 if (!rc)
1873 rc = mnt_fs_set_target(cxt->fs, mnt_fs_get_target(fs));
1874
1875 if (!rc && !mnt_fs_get_fstype(cxt->fs))
1876 rc = mnt_fs_set_fstype(cxt->fs, mnt_fs_get_fstype(fs));
1877
1878 if (rc)
1879 return rc;
1880
1881 if (cxt->optsmode & MNT_OMODE_IGNORE)
1882 ;
1883 else if (cxt->optsmode & MNT_OMODE_REPLACE)
1884 rc = mnt_fs_set_options(cxt->fs, mnt_fs_get_options(fs));
1885
1886 else if (cxt->optsmode & MNT_OMODE_APPEND)
1887 rc = mnt_fs_append_options(cxt->fs, mnt_fs_get_options(fs));
1888
1889 else if (cxt->optsmode & MNT_OMODE_PREPEND)
1890 rc = mnt_fs_prepend_options(cxt->fs, mnt_fs_get_options(fs));
1891
1892 if (!rc)
1893 cxt->flags |= MNT_FL_TAB_APPLIED;
1894 return rc;
1895 }
1896
1897 /**
1898 * mnt_context_apply_fstab:
1899 * @cxt: mount context
1900 *
1901 * This function is optional.
1902 *
1903 * Returns: 0 on success, negative number in case of error.
1904 */
1905 int mnt_context_apply_fstab(struct libmnt_context *cxt)
1906 {
1907 int rc = -1;
1908 struct libmnt_table *tab = NULL;
1909 const char *src = NULL, *tgt = NULL;
1910
1911 assert(cxt);
1912 assert(cxt->fs);
1913
1914 if (!cxt)
1915 return -EINVAL;
1916
1917 if (mnt_context_tab_applied(cxt)) /* already applied */
1918 return 0;
1919
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;
1930 }
1931
1932 if (cxt->fs) {
1933 src = mnt_fs_get_source(cxt->fs);
1934 tgt = mnt_fs_get_target(cxt->fs);
1935 }
1936
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));
1946
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"));
1950 return 0;
1951 }
1952
1953 if (!src && tgt
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"));
1958 return 0;
1959 }
1960
1961 DBG(CXT, mnt_debug_h(cxt,
1962 "trying to apply fstab (src=%s, target=%s)", src, tgt));
1963
1964 /* let's initialize cxt->fs */
1965 ignore_result( mnt_context_get_fs(cxt) );
1966
1967 /* try fstab */
1968 if (cxt->optsmode & MNT_OMODE_FSTAB) {
1969 rc = mnt_context_get_fstab(cxt, &tab);
1970 if (!rc)
1971 rc = apply_table(cxt, tab, MNT_ITER_FORWARD);
1972 }
1973
1974 /* try mtab */
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);
1978 if (!rc)
1979 rc = apply_table(cxt, tab, MNT_ITER_BACKWARD);
1980 }
1981 if (rc)
1982 DBG(CXT, mnt_debug_h(cxt, "failed to find entry in fstab/mtab"));
1983 return rc;
1984 }
1985
1986 /**
1987 * mnt_context_tab_applied:
1988 * @cxt: mount context
1989 *
1990 * Returns: 1 if fstab (or mtab) has been applied to the context, or 0.
1991 */
1992 int mnt_context_tab_applied(struct libmnt_context *cxt)
1993 {
1994 assert(cxt);
1995 return cxt->flags & MNT_FL_TAB_APPLIED;
1996 }
1997
1998 /*
1999 * This is not a public function!
2000 *
2001 * Returns 1 if *only propagation flags* change is requested.
2002 */
2003 int mnt_context_propagation_only(struct libmnt_context *cxt)
2004 {
2005 assert(cxt);
2006 assert(cxt->fs);
2007
2008 if (cxt->action != MNT_ACT_MOUNT)
2009 return 0;
2010
2011 /* has to be called after context_mount.c: fix_opts() */
2012 assert((cxt->flags & MNT_FL_MOUNTOPTS_FIXED));
2013
2014 /* all propagation mounts are in cxt->addmount */
2015 return !list_empty(&cxt->addmounts)
2016 && (cxt->mountflags == 0 || cxt->mountflags == MS_SILENT)
2017 && cxt->fs
2018 && (!cxt->fs->fstype || strcmp(cxt->fs->fstype, "none") == 0)
2019 && (!cxt->fs->source || strcmp(cxt->fs->source, "none") == 0);
2020 }
2021
2022 /**
2023 * mnt_context_get_status:
2024 * @cxt: mount context
2025 *
2026 * Global libmount status.
2027 *
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.
2031 *
2032 * Returns: 1 if mount.type or mount(2) syscall has been successfully called.
2033 */
2034 int mnt_context_get_status(struct libmnt_context *cxt)
2035 {
2036 assert(cxt);
2037 return !cxt->syscall_status || !cxt->helper_exec_status;
2038 }
2039
2040 /**
2041 * mnt_context_helper_executed:
2042 * @cxt: mount context
2043 *
2044 * Returns: 1 if mount.type helper has been executed, or 0.
2045 */
2046 int mnt_context_helper_executed(struct libmnt_context *cxt)
2047 {
2048 assert(cxt);
2049 return cxt->helper_exec_status != 1;
2050 }
2051
2052 /**
2053 * mnt_context_get_helper_status:
2054 * @cxt: mount context
2055 *
2056 * Return: mount.type helper exit status, result is reliable only if
2057 * mnt_context_helper_executed() returns 1.
2058 */
2059 int mnt_context_get_helper_status(struct libmnt_context *cxt)
2060 {
2061 assert(cxt);
2062 return cxt->helper_status;
2063 }
2064
2065 /**
2066 * mnt_context_syscall_called:
2067 * @cxt: mount context
2068 *
2069 * Returns: 1 if mount(2) syscall has been called, or 0.
2070 */
2071 int mnt_context_syscall_called(struct libmnt_context *cxt)
2072 {
2073 assert(cxt);
2074 return cxt->syscall_status != 1;
2075 }
2076
2077 /**
2078 * mnt_context_get_syscall_errno:
2079 * @cxt: mount context
2080 *
2081 * The result from this function is reliable only if
2082 * mnt_context_syscall_called() returns 1.
2083 *
2084 * Returns: mount(2) errno if the syscall failed or 0.
2085 */
2086 int mnt_context_get_syscall_errno(struct libmnt_context *cxt)
2087 {
2088 assert(cxt);
2089 if (cxt->syscall_status < 0)
2090 return -cxt->syscall_status;
2091 return 0;
2092 }
2093
2094 /**
2095 * mnt_context_set_syscall_status:
2096 * @cxt: mount context
2097 * @status: mount(2) status
2098 *
2099 * The @status should be 0 on success, or negative number on error (-errno).
2100 *
2101 * This function should only be used if the [u]mount(2) syscall is NOT called by
2102 * libmount code.
2103 *
2104 * Returns: 0 or negative number in case of error.
2105 */
2106 int mnt_context_set_syscall_status(struct libmnt_context *cxt, int status)
2107 {
2108 assert(cxt);
2109 if (!cxt)
2110 return -EINVAL;
2111
2112 DBG(CXT, mnt_debug_h(cxt, "syscall status set to: %d", status));
2113 cxt->syscall_status = status;
2114 return 0;
2115 }
2116
2117 /**
2118 * mnt_context_strerror
2119 * @cxt: context
2120 * @buf: buffer
2121 * @bufsiz: size of the buffer
2122 *
2123 * Not implemented yet.
2124 *
2125 * Returns: 0 or negative number in case of error.
2126 */
2127 int mnt_context_strerror(struct libmnt_context *cxt __attribute__((__unused__)),
2128 char *buf __attribute__((__unused__)),
2129 size_t bufsiz __attribute__((__unused__)))
2130 {
2131 /* TODO: based on cxt->syscall_errno or cxt->helper_status */
2132 return 0;
2133 }
2134
2135 /**
2136 * mnt_context_init_helper
2137 * @cxt: mount context
2138 * @action: MNT_ACT_{UMOUNT,MOUNT}
2139 * @flags: not used now
2140 *
2141 * This function informs libmount that used from [u]mount.type helper.
2142 *
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
2146 * feature by:
2147 *
2148 * mnt_context_disable_helpers(cxt, FALSE);
2149 *
2150 * Returns: 0 on success, negative number in case of error.
2151 */
2152 int mnt_context_init_helper(struct libmnt_context *cxt, int action,
2153 int flags __attribute__((__unused__)))
2154 {
2155 int rc;
2156
2157 assert(cxt);
2158
2159 rc = mnt_context_disable_helpers(cxt, TRUE);
2160 if (!rc)
2161 rc = set_flag(cxt, MNT_FL_HELPER, 1);
2162 if (!rc)
2163 cxt->action = action;
2164
2165 DBG(CXT, mnt_debug_h(cxt, "initialized for [u]mount.<type> helper [rc=%d]", rc));
2166 return rc;
2167 }
2168
2169 /**
2170 * mnt_context_helper_setopt:
2171 * @cxt: context
2172 * @c: getopt() result
2173 * @arg: getopt() optarg
2174 *
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.
2178 *
2179 * Returns: negative number on error, 1 if @c is unknown option, 0 on success.
2180 */
2181 int mnt_context_helper_setopt(struct libmnt_context *cxt, int c, char *arg)
2182 {
2183 if (cxt) {
2184 switch(cxt->action) {
2185 case MNT_ACT_MOUNT:
2186 return mnt_context_mount_setopt(cxt, c, arg);
2187 case MNT_ACT_UMOUNT:
2188 return mnt_context_umount_setopt(cxt, c, arg);
2189 }
2190 }
2191 return -EINVAL;
2192 }
2193
2194 /**
2195 * mnt_context_is_fs_mounted:
2196 * @cxt: context
2197 * @fs: filesystem
2198 * @mounted: returns 1 for mounted and 0 for non-mounted filesystems
2199 *
2200 * Please, read the mnt_table_is_fs_mounted() description!
2201 *
2202 * Returns: 0 on success and negative number in case of error.
2203 */
2204 int mnt_context_is_fs_mounted(struct libmnt_context *cxt,
2205 struct libmnt_fs *fs, int *mounted)
2206 {
2207 struct libmnt_table *mtab;
2208 int rc;
2209
2210 assert(cxt);
2211 if (!cxt || !fs || !mounted)
2212 return -EINVAL;
2213
2214 rc = mnt_context_get_mtab(cxt, &mtab);
2215 if (rc)
2216 return rc;
2217
2218 *mounted = mnt_table_is_fs_mounted(mtab, fs);
2219 return 0;
2220 }
2221
2222 static int mnt_context_add_child(struct libmnt_context *cxt, pid_t pid)
2223 {
2224 pid_t *pids;
2225
2226 assert(cxt);
2227 if (!cxt)
2228 return -EINVAL;
2229
2230 pids = realloc(cxt->children, sizeof(pid_t) * cxt->nchildren + 1);
2231 if (!pids)
2232 return -ENOMEM;
2233
2234 DBG(CXT, mnt_debug_h(cxt, "add new child %d", pid));
2235 cxt->children = pids;
2236 cxt->children[cxt->nchildren++] = pid;
2237
2238 return 0;
2239 }
2240
2241 int mnt_fork_context(struct libmnt_context *cxt)
2242 {
2243 int rc = 0;
2244 pid_t pid;
2245
2246 assert(cxt);
2247 if (!mnt_context_is_parent(cxt))
2248 return -EINVAL;
2249
2250 DBG(CXT, mnt_debug_h(cxt, "forking context"));
2251
2252 DBG_FLUSH;
2253
2254 pid = fork();
2255
2256 switch (pid) {
2257 case -1: /* error */
2258 DBG(CXT, mnt_debug_h(cxt, "fork failed %m"));
2259 return -errno;
2260
2261 case 0: /* child */
2262 cxt->pid = getpid();
2263 mnt_context_enable_fork(cxt, FALSE);
2264 DBG(CXT, mnt_debug_h(cxt, "child created"));
2265 break;
2266
2267 default:
2268 rc = mnt_context_add_child(cxt, pid);
2269 break;
2270 }
2271
2272 return rc;
2273 }
2274
2275 int mnt_context_wait_for_children(struct libmnt_context *cxt,
2276 int *nchildren, int *nerrs)
2277 {
2278 int i;
2279
2280 assert(cxt);
2281 if (!cxt)
2282 return -EINVAL;
2283
2284 assert(mnt_context_is_parent(cxt));
2285
2286 for (i = 0; i < cxt->nchildren; i++) {
2287 pid_t pid = cxt->children[i];
2288 int rc = 0, ret = 0;
2289
2290 if (!pid)
2291 continue;
2292 do {
2293 DBG(CXT, mnt_debug_h(cxt,
2294 "waiting for child (%d/%d): %d",
2295 i + 1, cxt->nchildren, pid));
2296 errno = 0;
2297 rc = waitpid(pid, &ret, 0);
2298
2299 } while (rc == -1 && errno == EINTR);
2300
2301 if (nchildren)
2302 (*nchildren)++;
2303
2304 if (rc != -1 && nerrs) {
2305 if (WIFEXITED(ret))
2306 (*nerrs) += WEXITSTATUS(ret) == 0 ? 0 : 1;
2307 else
2308 (*nerrs)++;
2309 }
2310 cxt->children[i] = 0;
2311 }
2312
2313 cxt->nchildren = 0;
2314 free(cxt->children);
2315 cxt->children = NULL;
2316 return 0;
2317 }
2318
2319
2320
2321 #ifdef TEST_PROGRAM
2322
2323 struct libmnt_lock *lock;
2324
2325 static void lock_fallback(void)
2326 {
2327 if (lock)
2328 mnt_unlock_file(lock);
2329 }
2330
2331 int test_mount(struct libmnt_test *ts, int argc, char *argv[])
2332 {
2333 int idx = 1, rc = 0;
2334 struct libmnt_context *cxt;
2335
2336 if (argc < 2)
2337 return -EINVAL;
2338
2339 cxt = mnt_new_context();
2340 if (!cxt)
2341 return -ENOMEM;
2342
2343 if (!strcmp(argv[idx], "-o")) {
2344 mnt_context_set_options(cxt, argv[idx + 1]);
2345 idx += 2;
2346 }
2347 if (!strcmp(argv[idx], "-t")) {
2348 /* TODO: use mnt_context_set_fstype_pattern() */
2349 mnt_context_set_fstype(cxt, argv[idx + 1]);
2350 idx += 2;
2351 }
2352
2353 if (argc == idx + 1)
2354 /* mount <mountpont>|<device> */
2355 mnt_context_set_target(cxt, argv[idx++]);
2356
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++]);
2361 }
2362
2363 /* this is unnecessary! -- libmount is able to internally
2364 * create and manage the lock
2365 */
2366 lock = mnt_context_get_lock(cxt);
2367 if (lock)
2368 atexit(lock_fallback);
2369
2370 rc = mnt_context_mount(cxt);
2371 if (rc)
2372 warn("failed to mount");
2373 else
2374 printf("successfully mounted\n");
2375
2376 lock = NULL; /* because we use atexit lock_fallback */
2377 mnt_free_context(cxt);
2378 return rc;
2379 }
2380
2381 int test_umount(struct libmnt_test *ts, int argc, char *argv[])
2382 {
2383 int idx = 1, rc = 0;
2384 struct libmnt_context *cxt;
2385
2386 if (argc < 2)
2387 return -EINVAL;
2388
2389 cxt = mnt_new_context();
2390 if (!cxt)
2391 return -ENOMEM;
2392
2393 if (!strcmp(argv[idx], "-t")) {
2394 mnt_context_set_fstype(cxt, argv[idx + 1]);
2395 idx += 2;
2396 }
2397
2398 if (!strcmp(argv[idx], "-f")) {
2399 mnt_context_enable_force(cxt, TRUE);
2400 idx++;
2401 }
2402
2403 if (!strcmp(argv[idx], "-l")) {
2404 mnt_context_enable_lazy(cxt, TRUE);
2405 idx++;
2406 }
2407
2408 if (!strcmp(argv[idx], "-r")) {
2409 mnt_context_enable_rdonly_umount(cxt, TRUE);
2410 idx++;
2411 }
2412
2413 if (argc == idx + 1) {
2414 /* mount <mountpont>|<device> */
2415 mnt_context_set_target(cxt, argv[idx++]);
2416 } else {
2417 rc = -EINVAL;
2418 goto err;
2419 }
2420
2421 lock = mnt_context_get_lock(cxt);
2422 if (lock)
2423 atexit(lock_fallback);
2424
2425 rc = mnt_context_umount(cxt);
2426 if (rc)
2427 printf("failed to umount\n");
2428 else
2429 printf("successfully umounted\n");
2430 err:
2431 lock = NULL; /* because we use atexit lock_fallback */
2432 mnt_free_context(cxt);
2433 return rc;
2434 }
2435
2436 int test_flags(struct libmnt_test *ts, int argc, char *argv[])
2437 {
2438 int idx = 1, rc = 0;
2439 struct libmnt_context *cxt;
2440 const char *opt = NULL;
2441 unsigned long flags = 0;
2442
2443 if (argc < 2)
2444 return -EINVAL;
2445
2446 cxt = mnt_new_context();
2447 if (!cxt)
2448 return -ENOMEM;
2449
2450 if (!strcmp(argv[idx], "-o")) {
2451 mnt_context_set_options(cxt, argv[idx + 1]);
2452 idx += 2;
2453 }
2454
2455 if (argc == idx + 1)
2456 /* mount <mountpont>|<device> */
2457 mnt_context_set_target(cxt, argv[idx++]);
2458
2459 rc = mnt_context_prepare_mount(cxt);
2460 if (rc)
2461 printf("failed to prepare mount %s\n", strerror(-rc));
2462
2463 opt = mnt_fs_get_options(cxt->fs);
2464 if (opt)
2465 fprintf(stdout, "options: %s\n", opt);
2466
2467 mnt_context_get_mflags(cxt, &flags);
2468 fprintf(stdout, "flags: %08lx\n", flags);
2469
2470 mnt_free_context(cxt);
2471 return rc;
2472 }
2473
2474 int test_mountall(struct libmnt_test *ts, int argc, char *argv[])
2475 {
2476 struct libmnt_context *cxt;
2477 struct libmnt_iter *itr;
2478 struct libmnt_fs *fs;
2479 int mntrc, ignored, idx = 1;
2480
2481 cxt = mnt_new_context();
2482 itr = mnt_new_iter(MNT_ITER_FORWARD);
2483
2484 if (!cxt || !itr)
2485 return -ENOMEM;
2486
2487 if (argc > 2) {
2488 if (argv[idx] && !strcmp(argv[idx], "-O")) {
2489 mnt_context_set_options_pattern(cxt, argv[idx + 1]);
2490 idx += 2;
2491 }
2492 if (argv[idx] && !strcmp(argv[idx], "-t")) {
2493 mnt_context_set_fstype_pattern(cxt, argv[idx + 1]);
2494 idx += 2;
2495 }
2496 }
2497
2498 while (mnt_context_next_mount(cxt, itr, &fs, &mntrc, &ignored) == 0) {
2499
2500 const char *tgt = mnt_fs_get_target(fs);
2501
2502 if (ignored == 1)
2503 printf("%s: ignored: not match\n", tgt);
2504 else if (ignored == 2)
2505 printf("%s: ignored: already mounted\n", tgt);
2506
2507 else if (!mnt_context_get_status(cxt)) {
2508 if (mntrc > 0) {
2509 errno = mntrc;
2510 warn("%s: mount failed", tgt);
2511 } else
2512 warnx("%s: mount failed", tgt);
2513 } else
2514 printf("%s: successfully mounted\n", tgt);
2515 }
2516
2517 mnt_free_context(cxt);
2518 return 0;
2519 }
2520
2521 int main(int argc, char *argv[])
2522 {
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>" },
2528 { NULL }};
2529
2530 umask(S_IWGRP|S_IWOTH); /* to be compatible with mount(8) */
2531
2532 return mnt_run_test(tss, argc, argv);
2533 }
2534
2535 #endif /* TEST_PROGRAM */