github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/sentry/platform/systrap/sysmsg/sysmsg_unsafe.go (about)

     1  // Copyright 2023 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 sysmsg
    16  
    17  import (
    18  	"syscall"
    19  	"unsafe"
    20  
    21  	"golang.org/x/sys/unix"
    22  	"github.com/nicocha30/gvisor-ligolo/pkg/abi/linux"
    23  )
    24  
    25  // SleepOnState makes the caller sleep on the ThreadContext.State futex.
    26  func (c *ThreadContext) SleepOnState(curState ContextState, timeout *unix.Timespec) syscall.Errno {
    27  	_, _, errno := unix.Syscall6(unix.SYS_FUTEX, uintptr(unsafe.Pointer(&c.State)),
    28  		linux.FUTEX_WAIT, uintptr(curState), uintptr(unsafe.Pointer(timeout)), 0, 0)
    29  	if errno == unix.EAGAIN || errno == unix.EINTR {
    30  		errno = 0
    31  	}
    32  	return errno
    33  }
    34  
    35  // WakeSysmsgThread calls futex wake on Sysmsg.State.
    36  func (m *Msg) WakeSysmsgThread() (bool, syscall.Errno) {
    37  	if !m.State.CompareAndSwap(ThreadStateAsleep, ThreadStatePrep) {
    38  		return false, 0
    39  	}
    40  	_, _, e := unix.RawSyscall6(unix.SYS_FUTEX, uintptr(unsafe.Pointer(&m.State)), linux.FUTEX_WAKE, 1, 0, 0, 0)
    41  	return true, e
    42  }