github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/pkg/sentry/fsimpl/host/save_restore.go (about) 1 // Copyright 2020 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 host 16 17 import ( 18 "fmt" 19 "io" 20 21 "github.com/ttpreport/gvisor-ligolo/pkg/fdnotifier" 22 "github.com/ttpreport/gvisor-ligolo/pkg/hostarch" 23 "github.com/ttpreport/gvisor-ligolo/pkg/safemem" 24 "github.com/ttpreport/gvisor-ligolo/pkg/sentry/hostfd" 25 "golang.org/x/sys/unix" 26 ) 27 28 // beforeSave is invoked by stateify. 29 func (i *inode) beforeSave() { 30 if !i.savable { 31 panic("host.inode is not savable") 32 } 33 if i.ftype == unix.S_IFIFO { 34 // If this pipe FD is readable, drain it so that bytes in the pipe can 35 // be read after restore. (This is a legacy VFS1 feature.) We don't 36 // know if the pipe FD is readable, so just try reading and tolerate 37 // EBADF from the read. 38 i.bufMu.Lock() 39 defer i.bufMu.Unlock() 40 var buf [hostarch.PageSize]byte 41 for { 42 n, err := hostfd.Preadv2(int32(i.hostFD), safemem.BlockSeqOf(safemem.BlockFromSafeSlice(buf[:])), -1 /* offset */, 0 /* flags */) 43 if n != 0 { 44 i.buf = append(i.buf, buf[:n]...) 45 } 46 if err != nil { 47 if err == io.EOF || err == unix.EAGAIN || err == unix.EBADF { 48 break 49 } 50 panic(fmt.Errorf("host.inode.beforeSave: buffering from pipe failed: %v", err)) 51 } 52 } 53 if len(i.buf) != 0 { 54 i.haveBuf.Store(1) 55 } 56 } 57 } 58 59 // afterLoad is invoked by stateify. 60 func (i *inode) afterLoad() { 61 if i.epollable { 62 if err := unix.SetNonblock(i.hostFD, true); err != nil { 63 panic(fmt.Sprintf("host.inode.afterLoad: failed to set host FD %d non-blocking: %v", i.hostFD, err)) 64 } 65 if err := fdnotifier.AddFD(int32(i.hostFD), &i.queue); err != nil { 66 panic(fmt.Sprintf("host.inode.afterLoad: fdnotifier.AddFD(%d) failed: %v", i.hostFD, err)) 67 } 68 } 69 }