github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/syscalls/linux/sys_eventfd.go (about)

     1  // Copyright 2018 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 linux
    16  
    17  import (
    18  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    19  	"github.com/SagerNet/gvisor/pkg/errors/linuxerr"
    20  	"github.com/SagerNet/gvisor/pkg/sentry/arch"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/fs"
    22  	"github.com/SagerNet/gvisor/pkg/sentry/kernel"
    23  	"github.com/SagerNet/gvisor/pkg/sentry/kernel/eventfd"
    24  )
    25  
    26  // Eventfd2 implements linux syscall eventfd2(2).
    27  func Eventfd2(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    28  	initVal := args[0].Int()
    29  	flags := uint(args[1].Uint())
    30  	allOps := uint(linux.EFD_SEMAPHORE | linux.EFD_NONBLOCK | linux.EFD_CLOEXEC)
    31  
    32  	if flags & ^allOps != 0 {
    33  		return 0, nil, linuxerr.EINVAL
    34  	}
    35  
    36  	event := eventfd.New(t, uint64(initVal), flags&linux.EFD_SEMAPHORE != 0)
    37  	event.SetFlags(fs.SettableFileFlags{
    38  		NonBlocking: flags&linux.EFD_NONBLOCK != 0,
    39  	})
    40  	defer event.DecRef(t)
    41  
    42  	fd, err := t.NewFDFrom(0, event, kernel.FDFlags{
    43  		CloseOnExec: flags&linux.EFD_CLOEXEC != 0,
    44  	})
    45  	if err != nil {
    46  		return 0, nil, err
    47  	}
    48  
    49  	return uintptr(fd), nil, nil
    50  }
    51  
    52  // Eventfd implements linux syscall eventfd(2).
    53  func Eventfd(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    54  	args[1].Value = 0
    55  	return Eventfd2(t, args)
    56  }