github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/runsc/cmd/umount_unsafe.go (about) 1 // Copyright 2022 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cmd 16 17 import ( 18 "context" 19 "os" 20 "syscall" 21 "unsafe" 22 23 "github.com/MerlinKodo/gvisor/pkg/abi/linux" 24 "github.com/MerlinKodo/gvisor/runsc/cmd/util" 25 "github.com/MerlinKodo/gvisor/runsc/flag" 26 "github.com/google/subcommands" 27 "golang.org/x/sys/unix" 28 ) 29 30 // Umount implements subcommands.Command for the "umount" command. 31 type Umount struct { 32 syncFD int 33 } 34 35 // Name implements subcommands.Command.Name. 36 func (*Umount) Name() string { 37 return "umount" 38 } 39 40 // Synopsis implements subcommands.Command.Synopsis. 41 func (*Umount) Synopsis() string { 42 return "umount the specified directory lazily when one byte is read from sync-fd" 43 } 44 45 // Usage implements subcommands.Command.Usage. 46 func (*Umount) Usage() string { 47 return `umount --sync-fd=FD <directory path>` 48 } 49 50 // SetFlags implements subcommands.Command.SetFlags. 51 func (u *Umount) SetFlags(f *flag.FlagSet) { 52 f.IntVar(&u.syncFD, "sync-fd", -1, "") 53 } 54 55 // Execute implements subcommands.Command.Execute. 56 func (u *Umount) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus { 57 if f.NArg() == 0 || f.NArg() > 1 { 58 f.Usage() 59 return subcommands.ExitUsageError 60 } 61 62 dirPath := f.Arg(0) 63 64 syncFile := os.NewFile(uintptr(u.syncFD), "sync file") 65 defer syncFile.Close() 66 67 buf := make([]byte, 1) 68 if l, err := syncFile.Read(buf); err != nil || l != 1 { 69 util.Fatalf("unable to read from the sync descriptor: %v, error %v", l, err) 70 } 71 72 if _, _, errno := unix.RawSyscall( 73 unix.SYS_UMOUNT2, 74 uintptr(unsafe.Pointer(syscall.StringBytePtr(dirPath))), 75 uintptr(linux.MNT_DETACH), 0); errno != 0 { 76 util.Fatalf("Unable to umount %s: errno %v", dirPath, errno) 77 } 78 79 return subcommands.ExitSuccess 80 }