/
context.c
2483 lines (2149 loc) · 56.9 KB
/
context.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2010,2011,2012 Karel Zak <kzak@redhat.com>
*
* This file may be redistributed under the terms of the
* GNU Lesser General Public License.
*/
/**
* SECTION: context
* @title: Library high-level context
* @short_description: high-level API to mount/umount devices.
*
* <informalexample>
* <programlisting>
* struct libmnt_context *cxt = mnt_new_context();
*
* mnt_context_set_options(cxt, "aaa,bbb,ccc=CCC");
* mnt_context_set_mflags(cxt, MS_NOATIME|MS_NOEXEC);
* mnt_context_set_target(cxt, "/mnt/foo");
*
* if (!mnt_context_mount(cxt))
* printf("successfully mounted\n");
* mnt_free_context(cxt);
*
* </programlisting>
* </informalexample>
*
* This code is similar to:
*
* mount -o aaa,bbb,ccc=CCC,noatime,noexec /mnt/foo
*
*/
#include "mountP.h"
#include <sys/wait.h>
/**
* mnt_new_context:
*
* Returns: newly allocated mount context
*/
struct libmnt_context *mnt_new_context(void)
{
struct libmnt_context *cxt;
uid_t ruid, euid;
cxt = calloc(1, sizeof(*cxt));
if (!cxt)
return NULL;
INIT_LIST_HEAD(&cxt->addmounts);
ruid = getuid();
euid = geteuid();
mnt_context_reset_status(cxt);
cxt->loopdev_fd = -1;
/* if we're really root and aren't running setuid */
cxt->restricted = (uid_t) 0 == ruid && ruid == euid ? 0 : 1;
DBG(CXT, mnt_debug_h(cxt, "----> allocate %s",
cxt->restricted ? "[RESTRICTED]" : ""));
mnt_has_regular_mtab(&cxt->mtab_path, &cxt->mtab_writable);
if (!cxt->mtab_writable)
/* use /run/mount/utab if /etc/mtab is useless */
mnt_has_regular_utab(&cxt->utab_path, &cxt->utab_writable);
return cxt;
}
/**
* mnt_free_context:
* @cxt: mount context
*
* Deallocates context struct.
*/
void mnt_free_context(struct libmnt_context *cxt)
{
if (!cxt)
return;
mnt_reset_context(cxt);
free(cxt->fstype_pattern);
free(cxt->optstr_pattern);
if (!(cxt->flags & MNT_FL_EXTERN_FSTAB))
mnt_free_table(cxt->fstab);
if (!(cxt->flags & MNT_FL_EXTERN_CACHE))
mnt_free_cache(cxt->cache);
mnt_context_clear_loopdev(cxt);
mnt_free_lock(cxt->lock);
mnt_free_update(cxt->update);
free(cxt->children);
DBG(CXT, mnt_debug_h(cxt, "<---- free"));
free(cxt);
}
/**
* mnt_reset_context:
* @cxt: mount context
*
* Resets all information in the context that are directly related to
* the latest mount (spec, source, target, mount options, ....)
*
* The match patters, cached fstab, cached canonicalized paths and tags and
* [e]uid are not reseted. You have to use
*
* mnt_context_set_fstab(cxt, NULL);
* mnt_context_set_cache(cxt, NULL);
* mnt_context_set_fstype_pattern(cxt, NULL);
* mnt_context_set_options_pattern(cxt, NULL);
*
*
* to reset these stuff.
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_reset_context(struct libmnt_context *cxt)
{
int fl;
if (!cxt)
return -EINVAL;
DBG(CXT, mnt_debug_h(cxt, "<---- reset [status=%d] ---->",
mnt_context_get_status(cxt)));
fl = cxt->flags;
if (!(cxt->flags & MNT_FL_EXTERN_FS))
mnt_free_fs(cxt->fs);
mnt_free_table(cxt->mtab);
free(cxt->helper);
free(cxt->orig_user);
cxt->fs = NULL;
cxt->mtab = NULL;
cxt->helper = NULL;
cxt->orig_user = NULL;
cxt->mountflags = 0;
cxt->user_mountflags = 0;
cxt->mountdata = NULL;
cxt->flags = MNT_FL_DEFAULT;
/* free additional mounts list */
while (!list_empty(&cxt->addmounts)) {
struct libmnt_addmount *ad = list_entry(cxt->addmounts.next,
struct libmnt_addmount,
mounts);
mnt_free_addmount(ad);
}
mnt_context_reset_status(cxt);
mnt_context_set_tabfilter(cxt, NULL, NULL);
/* restore non-resetable flags */
cxt->flags |= (fl & MNT_FL_EXTERN_FSTAB);
cxt->flags |= (fl & MNT_FL_EXTERN_CACHE);
cxt->flags |= (fl & MNT_FL_NOMTAB);
cxt->flags |= (fl & MNT_FL_FAKE);
cxt->flags |= (fl & MNT_FL_SLOPPY);
cxt->flags |= (fl & MNT_FL_VERBOSE);
cxt->flags |= (fl & MNT_FL_NOHELPERS);
cxt->flags |= (fl & MNT_FL_LOOPDEL);
cxt->flags |= (fl & MNT_FL_LAZY);
cxt->flags |= (fl & MNT_FL_FORK);
cxt->flags |= (fl & MNT_FL_FORCE);
cxt->flags |= (fl & MNT_FL_NOCANONICALIZE);
cxt->flags |= (fl & MNT_FL_RDONLY_UMOUNT);
cxt->flags |= (fl & MNT_FL_NOSWAPMATCH);
return 0;
}
/**
* mnt_context_reset_status:
* @cxt: context
*
* Resets mount(2) and mount.type statuses, so mnt_context_do_mount() or
* mnt_context_do_umount() could be again called with the same settings.
*
* BE CAREFUL -- after this soft reset the libmount will NOT parse mount
* options, evaluate permissions or apply stuff from fstab.
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_reset_status(struct libmnt_context *cxt)
{
assert(cxt);
if (!cxt)
return -EINVAL;
cxt->syscall_status = 1; /* means not called yet */
cxt->helper_exec_status = 1;
cxt->helper_status = 0;
return 0;
}
static int set_flag(struct libmnt_context *cxt, int flag, int enable)
{
assert(cxt);
if (!cxt)
return -EINVAL;
if (enable) {
DBG(CXT, mnt_debug_h(cxt, "enabling flag %04x", flag));
cxt->flags |= flag;
} else {
DBG(CXT, mnt_debug_h(cxt, "disabling flag %04x", flag));
cxt->flags &= ~flag;
}
return 0;
}
/**
* mnt_context_is_restricted:
* @cxt: mount context
*
* Returns: 0 for unrestricted mount (user is root), or 1 for non-root mounts
*/
int mnt_context_is_restricted(struct libmnt_context *cxt)
{
return cxt->restricted;
}
/**
* mnt_context_set_optsmode
* @cxt: mount context
* @mode: MNT_OMASK_* flags
*
* Controls how to use mount optionsmsource and target paths from fstab/mtab.
*
* @MNT_OMODE_IGNORE: ignore mtab/fstab options
*
* @MNT_OMODE_APPEND: append mtab/fstab options to existing options
*
* @MNT_OMODE_PREPEND: prepend mtab/fstab options to existing options
*
* @MNT_OMODE_REPLACE: replace existing options with options from mtab/fstab
*
* @MNT_OMODE_FORCE: always read mtab/fstab (although source and target is defined)
*
* @MNT_OMODE_FSTAB: read from fstab
*
* @MNT_OMODE_MTAB: read from mtab if fstab not enabled or failed
*
* @MNT_OMODE_NOTAB: do not read fstab/mtab at all
*
* @MNT_OMODE_AUTO: default mode (MNT_OMODE_PREPEND | MNT_OMODE_FSTAB | MNT_OMODE_MTAB)
*
* @MNT_OMODE_USER: default for non-root users (MNT_OMODE_REPLACE | MNT_OMODE_FORCE | MNT_OMODE_FSTAB)
*
* Notes:
*
* - MNT_OMODE_USER is always used if mount context is in restricted mode
* - MNT_OMODE_AUTO is used if nothing other is defined
* - the flags are eavaluated in this order: MNT_OMODE_NOTAB, MNT_OMODE_FORCE,
* MNT_OMODE_FSTAB, MNT_OMODE_MTAB and then the mount options from fstab/mtab
* are set according to MNT_OMODE_{IGNORE,APPEND,PREPAND,REPLACE}
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_set_optsmode(struct libmnt_context *cxt, int mode)
{
assert(cxt);
if (!cxt)
return -EINVAL;
cxt->optsmode = mode;
return 0;
}
/**
* mnt_context_get_optsmode
* @cxt: mount context
*
* Returns: MNT_OMASK_* mask or zero.
*/
int mnt_context_get_optsmode(struct libmnt_context *cxt)
{
assert(cxt);
return cxt->optsmode;
}
/**
* mnt_context_disable_canonicalize:
* @cxt: mount context
* @disable: TRUE or FALSE
*
* Enable/disable paths canonicalization and tags evaluation. The libmount context
* canonicalies paths when search in fstab and when prepare source and target paths
* for mount(2) syscall.
*
* This fuction has effect to the private (within context) fstab instance only
* (see mnt_context_set_fstab()). If you want to use an external fstab then you
* need manage your private struct libmnt_cache (see mnt_table_set_cache(fstab,
* NULL).
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_disable_canonicalize(struct libmnt_context *cxt, int disable)
{
return set_flag(cxt, MNT_FL_NOCANONICALIZE, disable);
}
/**
* mnt_context_is_nocanonicalize:
* @cxt: mount context
*
* Returns: 1 if no-canonicalize mode enabled or 0.
*/
int mnt_context_is_nocanonicalize(struct libmnt_context *cxt)
{
return cxt && (cxt->flags & MNT_FL_NOCANONICALIZE) ? 1 : 0;
}
/**
* mnt_context_enable_lazy:
* @cxt: mount context
* @enable: TRUE or FALSE
*
* Enable/disable lazy umount (see umount(8) man page, option -l).
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_enable_lazy(struct libmnt_context *cxt, int enable)
{
return set_flag(cxt, MNT_FL_LAZY, enable);
}
/**
* mnt_context_is_lazy:
* @cxt: mount context
*
* Returns: 1 if lazy umount is enabled or 0
*/
int mnt_context_is_lazy(struct libmnt_context *cxt)
{
return cxt->flags & MNT_FL_LAZY ? 1 : 0;
}
/**
* mnt_context_enable_fork:
* @cxt: mount context
* @enable: TRUE or FALSE
*
* Enable/disable fork(2) call in mnt_context_next_mount() (see mount(8) man
* page, option -F).
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_enable_fork(struct libmnt_context *cxt, int enable)
{
return set_flag(cxt, MNT_FL_FORK, enable);
}
/**
* mnt_context_is_fork:
* @cxt: mount context
*
* Returns: 1 if fork (mount -F) is enabled or 0
*/
int mnt_context_is_fork(struct libmnt_context *cxt)
{
return cxt->flags & MNT_FL_FORK ? 1 : 0;
}
/**
* mnt_context_is_parent:
* @cxt: mount context
*
* Return: 1 if mount -F enabled and the current context is parent, or 0
*/
int mnt_context_is_parent(struct libmnt_context *cxt)
{
return mnt_context_is_fork(cxt) && cxt->pid == 0;
}
/**
* mnt_context_is_child:
* @cxt: mount context
*
* Return: 1 if mount -F enabled and the current context is child, or 0
*/
int mnt_context_is_child(struct libmnt_context *cxt)
{
return !mnt_context_is_fork(cxt) && cxt->pid;
}
/**
* mnt_context_enable_rdonly_umount:
* @cxt: mount context
* @enable: TRUE or FALSE
*
* Enable/disable read-only remount on failed umount(2)
* (see umount(8) man page, option -r).
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_enable_rdonly_umount(struct libmnt_context *cxt, int enable)
{
return set_flag(cxt, MNT_FL_RDONLY_UMOUNT, enable);
}
/**
* mnt_context_is_rdonly_umount
* @cxt: mount context
*
* See also mnt_context_enable_rdonly_umount() and see umount(8) man page,
* option -r.
*
* Returns: 1 if read-only remount failed umount(2) is enables or 0
*/
int mnt_context_is_rdonly_umount(struct libmnt_context *cxt)
{
return cxt->flags & MNT_FL_RDONLY_UMOUNT ? 1 : 0;
}
/**
* mnt_context_disable_helpers:
* @cxt: mount context
* @disable: TRUE or FALSE
*
* Enable/disable /sbin/[u]mount.* helpers (see mount(8) man page, option -i).
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_disable_helpers(struct libmnt_context *cxt, int disable)
{
return set_flag(cxt, MNT_FL_NOHELPERS, disable);
}
/**
* mnt_context_is_nohelpers
* @cxt: mount context
*
* Returns: 1 if helpers are disabled (mount -i) or 0
*/
int mnt_context_is_nohelpers(struct libmnt_context *cxt)
{
return cxt->flags & MNT_FL_NOHELPERS ? 1 : 0;
}
/**
* mnt_context_enable_sloppy:
* @cxt: mount context
* @enable: TRUE or FALSE
*
* Set/unset sloppy mounting (see mount(8) man page, option -s).
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_enable_sloppy(struct libmnt_context *cxt, int enable)
{
return set_flag(cxt, MNT_FL_SLOPPY, enable);
}
/**
* mnt_context_is_sloppy:
* @cxt: mount context
*
* Returns: 1 if sloppy flag is enabled or 0
*/
int mnt_context_is_sloppy(struct libmnt_context *cxt)
{
return cxt->flags & MNT_FL_SLOPPY ? 1 : 0;
}
/**
* mnt_context_enable_fake:
* @cxt: mount context
* @enable: TRUE or FALSE
*
* Enable/disable fake mounting (see mount(8) man page, option -f).
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_enable_fake(struct libmnt_context *cxt, int enable)
{
return set_flag(cxt, MNT_FL_FAKE, enable);
}
/**
* mnt_context_is_fake:
* @cxt: mount context
*
* Returns: 1 if fake flag is enabled or 0
*/
int mnt_context_is_fake(struct libmnt_context *cxt)
{
return cxt->flags & MNT_FL_FAKE ? 1 : 0;
}
/**
* mnt_context_disable_mtab:
* @cxt: mount context
* @disable: TRUE or FALSE
*
* Disable/enable mtab update (see mount(8) man page, option -n).
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_disable_mtab(struct libmnt_context *cxt, int disable)
{
return set_flag(cxt, MNT_FL_NOMTAB, disable);
}
/**
* mnt_context_is_nomtab:
* @cxt: mount context
*
* Returns: 1 if no-mtab is enabled or 0
*/
int mnt_context_is_nomtab(struct libmnt_context *cxt)
{
return cxt->flags & MNT_FL_NOMTAB ? 1 : 0;
}
/**
* mnt_context_disable_swapmatch:
* @cxt: mount context
* @disable: TRUE or FALSE
*
* Disable/enable swap between source and target for mount(8) if only one path
* is specified.
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_disable_swapmatch(struct libmnt_context *cxt, int disable)
{
return set_flag(cxt, MNT_FL_NOSWAPMATCH, disable);
}
/**
* mnt_context_is_swapmatch:
* @cxt: mount context
*
* Returns: 1 if swap between source and target is allowed (default is 1) or 0.
*/
int mnt_context_is_swapmatch(struct libmnt_context *cxt)
{
return cxt->flags & MNT_FL_NOSWAPMATCH ? 0 : 1;
}
/**
* mnt_context_enable_force:
* @cxt: mount context
* @enable: TRUE or FALSE
*
* Enable/disable force umounting (see umount(8) man page, option -f).
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_enable_force(struct libmnt_context *cxt, int enable)
{
return set_flag(cxt, MNT_FL_FORCE, enable);
}
/**
* mnt_context_is_force
* @cxt: mount context
*
* Returns: 1 if force umounting flag is enabled or 0
*/
int mnt_context_is_force(struct libmnt_context *cxt)
{
return cxt->flags & MNT_FL_FORCE ? 1 : 0;
}
/**
* mnt_context_enable_verbose:
* @cxt: mount context
* @enable: TRUE or FALSE
*
* Enable/disable verbose output (TODO: not implemented yet)
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_enable_verbose(struct libmnt_context *cxt, int enable)
{
return set_flag(cxt, MNT_FL_VERBOSE, enable);
}
/**
* mnt_context_is_verbose
* @cxt: mount context
*
* Returns: 1 if verbose flag is enabled or 0
*/
int mnt_context_is_verbose(struct libmnt_context *cxt)
{
return cxt->flags & MNT_FL_VERBOSE ? 1 : 0;
}
/**
* mnt_context_enable_loopdel:
* @cxt: mount context
* @enable: TRUE or FALSE
*
* Enable/disable loop delete (destroy) after umount (see umount(8), option -d)
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_enable_loopdel(struct libmnt_context *cxt, int enable)
{
return set_flag(cxt, MNT_FL_LOOPDEL, enable);
}
/**
* mnt_context_is_loopdel:
* @cxt: mount context
*
* Returns: 1 if loop device should be deleted after umount (umount -d) or 0.
*/
int mnt_context_is_loopdel(struct libmnt_context *cxt)
{
return cxt->flags & MNT_FL_LOOPDEL ? 1 : 0;
}
/**
* mnt_context_set_fs:
* @cxt: mount context
* @fs: filesystem description
*
* The mount context uses private @fs by default. This function allows to
* overwrite the private @fs with an external instance. Note that the external
* @fs instance is not deallocated by mnt_free_context() or mnt_reset_context().
*
* The @fs will be modified by mnt_context_set_{source,target,options,fstype}
* functions, If the @fs is NULL then all current FS specific setting (source,
* target, etc., exclude spec) is reseted.
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_set_fs(struct libmnt_context *cxt, struct libmnt_fs *fs)
{
if (!cxt)
return -EINVAL;
if (!(cxt->flags & MNT_FL_EXTERN_FS))
mnt_free_fs(cxt->fs);
set_flag(cxt, MNT_FL_EXTERN_FS, fs != NULL);
cxt->fs = fs;
return 0;
}
/**
* mnt_context_get_fs:
* @cxt: mount context
*
* The FS contains the basic description of mountpoint, fs type and so on.
* Note that the FS is modified by mnt_context_set_{source,target,options,fstype}
* functions.
*
* Returns: pointer to FS description or NULL in case of calloc() errrr.
*/
struct libmnt_fs *mnt_context_get_fs(struct libmnt_context *cxt)
{
assert(cxt);
if (!cxt)
return NULL;
if (!cxt->fs) {
cxt->fs = mnt_new_fs();
cxt->flags &= ~MNT_FL_EXTERN_FS;
}
return cxt->fs;
}
/**
* mnt_context_set_source:
* @cxt: mount context
* @source: mount source (device, directory, UUID, LABEL, ...)
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_set_source(struct libmnt_context *cxt, const char *source)
{
assert(cxt);
return mnt_fs_set_source(mnt_context_get_fs(cxt), source);
}
/**
* mnt_context_get_source:
* @cxt: mount context
*
* Returns: returns pointer or NULL in case of error pr if not set.
*/
const char *mnt_context_get_source(struct libmnt_context *cxt)
{
assert(cxt);
return mnt_fs_get_source(mnt_context_get_fs(cxt));
}
/**
* mnt_context_set_target:
* @cxt: mount context
* @target: mountpoint
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_set_target(struct libmnt_context *cxt, const char *target)
{
assert(cxt);
return mnt_fs_set_target(mnt_context_get_fs(cxt), target);
}
/**
* mnt_context_get_target:
* @cxt: mount context
*
* Returns: returns pointer or NULL in case of error pr if not set.
*/
const char *mnt_context_get_target(struct libmnt_context *cxt)
{
assert(cxt);
return mnt_fs_get_target(mnt_context_get_fs(cxt));
}
/**
* mnt_context_set_fstype:
* @cxt: mount context
* @fstype: filesystem type
*
* Note that the @fstype has to be the real FS type. For patterns with
* comma-separated list of filesystems or for "nofs" notation use
* mnt_context_set_fstype_pattern().
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_set_fstype(struct libmnt_context *cxt, const char *fstype)
{
assert(cxt);
return mnt_fs_set_fstype(mnt_context_get_fs(cxt), fstype);
}
/**
* mnt_context_get_fstype:
* @cxt: mount context
*
* Returns: pointer or NULL in case of error pr if not set.
*/
const char *mnt_context_get_fstype(struct libmnt_context *cxt)
{
assert(cxt);
return mnt_fs_get_fstype(mnt_context_get_fs(cxt));
}
/**
* mnt_context_set_options:
* @cxt: mount context
* @optstr: comma delimited mount options
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_set_options(struct libmnt_context *cxt, const char *optstr)
{
assert(cxt);
return mnt_fs_set_options(mnt_context_get_fs(cxt), optstr);
}
/**
* mnt_context_append_options:
* @cxt: mount context
* @optstr: comma delimited mount options
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_append_options(struct libmnt_context *cxt, const char *optstr)
{
assert(cxt);
return mnt_fs_append_options(mnt_context_get_fs(cxt), optstr);
}
/**
* mnt_context_get_options:
* @cxt: mount context
*
* This function returns mount options set by mnt_context_set_options() or
* mnt_context_append_options().
*
* Note that *after* mnt_context_prepare_mount() may the mount options string
* also includes options set by mnt_context_set_mflags() or another options
* generated by this library.
*
* Returns: pointer or NULL
*/
const char *mnt_context_get_options(struct libmnt_context *cxt)
{
assert(cxt);
return mnt_fs_get_options(mnt_context_get_fs(cxt));
}
/**
* mnt_context_set_fstype_pattern:
* @cxt: mount context
* @pattern: FS name pattern (or NULL to reset the current setting)
*
* See mount(8), option -t.
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_set_fstype_pattern(struct libmnt_context *cxt, const char *pattern)
{
char *p = NULL;
assert(cxt);
if (!cxt)
return -EINVAL;
if (pattern) {
p = strdup(pattern);
if (!p)
return -ENOMEM;
}
free(cxt->fstype_pattern);
cxt->fstype_pattern = p;
return 0;
}
/**
* mnt_context_set_options_pattern:
* @cxt: mount context
* @pattern: options pattern (or NULL to reset the current setting)
*
* See mount(8), option -O.
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_set_options_pattern(struct libmnt_context *cxt, const char *pattern)
{
char *p = NULL;
assert(cxt);
if (!cxt)
return -EINVAL;
if (pattern) {
p = strdup(pattern);
if (!p)
return -ENOMEM;
}
free(cxt->optstr_pattern);
cxt->optstr_pattern = p;
return 0;
}
/**
* mnt_context_set_fstab:
* @cxt: mount context
* @tb: fstab
*
* The mount context reads /etc/fstab to the private struct libmnt_table by default.
* This function allows to overwrite the private fstab with an external
* instance. Note that the external instance is not deallocated by mnt_free_context().
*
* The fstab is used read-only and is not modified, it should be possible to
* share the fstab between more mount contexts (TODO: tests it.)
*
* If the @tb argument is NULL then the current private fstab instance is
* reseted.
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_set_fstab(struct libmnt_context *cxt, struct libmnt_table *tb)
{
assert(cxt);
if (!cxt)
return -EINVAL;
if (!(cxt->flags & MNT_FL_EXTERN_FSTAB))
mnt_free_table(cxt->fstab);
set_flag(cxt, MNT_FL_EXTERN_FSTAB, tb != NULL);
cxt->fstab = tb;
return 0;
}
/**
* mnt_context_get_fstab:
* @cxt: mount context
* @tb: returns fstab
*
* See also mnt_table_parse_fstab() for more details about fstab.
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_get_fstab(struct libmnt_context *cxt, struct libmnt_table **tb)
{
struct libmnt_cache *cache;
assert(cxt);
if (!cxt)
return -EINVAL;
if (!cxt->fstab) {
int rc;
cxt->fstab = mnt_new_table();
if (!cxt->fstab)
return -ENOMEM;
if (cxt->table_errcb)
mnt_table_set_parser_errcb(cxt->fstab, cxt->table_errcb);
cxt->flags &= ~MNT_FL_EXTERN_FSTAB;
rc = mnt_table_parse_fstab(cxt->fstab, NULL);
if (rc)
return rc;
}
cache = mnt_context_get_cache(cxt);
/* never touch an external fstab */
if (!(cxt->flags & MNT_FL_EXTERN_FSTAB))
mnt_table_set_cache(cxt->fstab, cache);
if (tb)
*tb = cxt->fstab;
return 0;
}
/**
* mnt_context_get_mtab:
* @cxt: mount context
* @tb: returns mtab
*
* See also mnt_table_parse_mtab() for more details about mtab/mountinfo. The
* result will deallocated by mnt_free_context(@cxt).
*
* Returns: 0 on success, negative number in case of error.
*/
int mnt_context_get_mtab(struct libmnt_context *cxt, struct libmnt_table **tb)
{
struct libmnt_cache *cache;
assert(cxt);
if (!cxt)
return -EINVAL;
if (!cxt->mtab) {
int rc;
cxt->mtab = mnt_new_table();
if (!cxt->mtab)
return -ENOMEM;
if (cxt->table_errcb)
mnt_table_set_parser_errcb(cxt->mtab, cxt->table_errcb);
if (cxt->table_fltrcb)
mnt_table_set_parser_fltrcb(cxt->mtab,
cxt->table_fltrcb,
cxt->table_fltrcb_data);
rc = mnt_table_parse_mtab(cxt->mtab, cxt->mtab_path);
if (rc)
return rc;
}
cache = mnt_context_get_cache(cxt);
mnt_table_set_cache(cxt->mtab, cache);
if (tb)
*tb = cxt->mtab;
DBG(CXT, mnt_debug_h(cxt, "mtab requested [nents=%d]",
mnt_table_get_nents(cxt->mtab)));
return 0;
}
/*
* Allows to specify filter for tab file entries. The filter is called by
* table parser. Currently used for mtab and utab only.
*/
int mnt_context_set_tabfilter(struct libmnt_context *cxt,
int (*fltr)(struct libmnt_fs *, void *),
void *data)
{
assert(cxt);
if (!cxt)
return -EINVAL;
cxt->table_fltrcb = fltr;
cxt->table_fltrcb_data = data;
if (cxt->mtab)
mnt_table_set_parser_fltrcb(cxt->mtab,
cxt->table_fltrcb,
cxt->table_fltrcb_data);
DBG(CXT, mnt_debug_h(cxt, "tabfiler %s", fltr ? "ENABLED!" : "disabled"));
return 0;
}
/**
* mnt_context_get_table:
* @cxt: mount context
* @filename: e.g. /proc/self/mountinfo