Debian Codegraph @

mount/mounter_linux.go

raw symbols SCIP
1 package mount
2
3 import (
4 "golang.org/x/sys/unix"
5 )
6
7 const (
8 // ptypes is the set propagation types.
9 ptypes = unix.MS_SHARED | unix.MS_PRIVATE | unix.MS_SLAVE | unix.MS_UNBINDABLE
10
11 // pflags is the full set valid flags for a change propagation call.
12 pflags = ptypes | unix.MS_REC | unix.MS_SILENT
13
14 // broflags is the combination of bind and read only
15 broflags = unix.MS_BIND | unix.MS_RDONLY
16 )
17
18 // isremount returns true if either device name or flags identify a remount request, false otherwise.
19 func isremount(device string, flags uintptr) bool {
20 switch {
21 // We treat device "" and "none" as a remount request to provide compatibility with
22 // requests that don't explicitly set MS_REMOUNT such as those manipulating bind mounts.
23 case flags&unix.MS_REMOUNT != 0, device == "", device == "none":
24 return true
25 default:
26 return false
27 }
28 }
29
30 func mount(device, target, mType string, flags uintptr, data string) error {
31 oflags := flags &^ ptypes
32 if !isremount(device, flags) || data != "" {
33 // Initial call applying all non-propagation flags for mount
34 // or remount with changed data
35 if err := unix.Mount(device, target, mType, oflags, data); err != nil {
36 return &mountError{
37 op: "mount",
38 source: device,
39 target: target,
40 flags: oflags,
41 data: data,
42 err: err,
43 }
44 }
45 }
46
47 if flags&ptypes != 0 {
48 // Change the propagation type.
49 if err := unix.Mount("", target, "", flags&pflags, ""); err != nil {
50 return &mountError{
51 op: "remount",
52 target: target,
53 flags: flags & pflags,
54 err: err,
55 }
56 }
57 }
58
59 if oflags&broflags == broflags {
60 // Remount the bind to apply read only.
61 if err := unix.Mount("", target, "", oflags|unix.MS_REMOUNT, ""); err != nil {
62 return &mountError{
63 op: "remount-ro",
64 target: target,
65 flags: oflags | unix.MS_REMOUNT,
66 err: err,
67 }
68 }
69 }
70
71 return nil
72 }