github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/sentry/strace/epoll.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 strace
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/MerlinKodo/gvisor/pkg/abi"
    22  	"github.com/MerlinKodo/gvisor/pkg/abi/linux"
    23  	"github.com/MerlinKodo/gvisor/pkg/sentry/kernel"
    24  
    25  	"github.com/MerlinKodo/gvisor/pkg/hostarch"
    26  )
    27  
    28  func epollEvent(t *kernel.Task, eventAddr hostarch.Addr) string {
    29  	var e linux.EpollEvent
    30  	if _, err := e.CopyIn(t, eventAddr); err != nil {
    31  		return fmt.Sprintf("%#x {error reading event: %v}", eventAddr, err)
    32  	}
    33  	var sb strings.Builder
    34  	fmt.Fprintf(&sb, "%#x ", eventAddr)
    35  	writeEpollEvent(&sb, e)
    36  	return sb.String()
    37  }
    38  
    39  func epollEvents(t *kernel.Task, eventsAddr hostarch.Addr, numEvents, maxBytes uint64) string {
    40  	var sb strings.Builder
    41  	fmt.Fprintf(&sb, "%#x {", eventsAddr)
    42  	addr := eventsAddr
    43  	for i := uint64(0); i < numEvents; i++ {
    44  		var e linux.EpollEvent
    45  		if _, err := e.CopyIn(t, addr); err != nil {
    46  			fmt.Fprintf(&sb, "{error reading event at %#x: %v}", addr, err)
    47  			continue
    48  		}
    49  		writeEpollEvent(&sb, e)
    50  		if uint64(sb.Len()) >= maxBytes {
    51  			sb.WriteString("...")
    52  			break
    53  		}
    54  		// Allowing addr to overflow is consistent with Linux, and harmless; if
    55  		// this isn't the last iteration of the loop, the next call to CopyIn
    56  		// will just fail with EFAULT.
    57  		addr, _ = addr.AddLength(uint64(linux.SizeOfEpollEvent))
    58  	}
    59  	sb.WriteString("}")
    60  	return sb.String()
    61  }
    62  
    63  func writeEpollEvent(sb *strings.Builder, e linux.EpollEvent) {
    64  	events := epollEventEvents.Parse(uint64(e.Events))
    65  	fmt.Fprintf(sb, "{events=%s data=[%#x, %#x]}", events, e.Data[0], e.Data[1])
    66  }
    67  
    68  var epollCtlOps = abi.ValueSet{
    69  	linux.EPOLL_CTL_ADD: "EPOLL_CTL_ADD",
    70  	linux.EPOLL_CTL_DEL: "EPOLL_CTL_DEL",
    71  	linux.EPOLL_CTL_MOD: "EPOLL_CTL_MOD",
    72  }
    73  
    74  var epollEventEvents = abi.FlagSet{
    75  	{Flag: linux.EPOLLIN, Name: "EPOLLIN"},
    76  	{Flag: linux.EPOLLPRI, Name: "EPOLLPRI"},
    77  	{Flag: linux.EPOLLOUT, Name: "EPOLLOUT"},
    78  	{Flag: linux.EPOLLERR, Name: "EPOLLERR"},
    79  	{Flag: linux.EPOLLHUP, Name: "EPOLLHUP"},
    80  	{Flag: linux.EPOLLRDNORM, Name: "EPOLLRDNORM"},
    81  	{Flag: linux.EPOLLRDBAND, Name: "EPOLLRDBAND"},
    82  	{Flag: linux.EPOLLWRNORM, Name: "EPOLLWRNORM"},
    83  	{Flag: linux.EPOLLWRBAND, Name: "EPOLLWRBAND"},
    84  	{Flag: linux.EPOLLMSG, Name: "EPOLLMSG"},
    85  	{Flag: linux.EPOLLRDHUP, Name: "EPOLLRDHUP"},
    86  	{Flag: linux.EPOLLEXCLUSIVE, Name: "EPOLLEXCLUSIVE"},
    87  	{Flag: linux.EPOLLWAKEUP, Name: "EPOLLWAKEUP"},
    88  	{Flag: linux.EPOLLONESHOT, Name: "EPOLLONESHOT"},
    89  	{Flag: linux.EPOLLET, Name: "EPOLLET"},
    90  }