github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/pkg/sentry/strace/poll.go (about)

     1  // Copyright 2019 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/ttpreport/gvisor-ligolo/pkg/abi"
    22  	"github.com/ttpreport/gvisor-ligolo/pkg/abi/linux"
    23  	"github.com/ttpreport/gvisor-ligolo/pkg/sentry/kernel"
    24  	slinux "github.com/ttpreport/gvisor-ligolo/pkg/sentry/syscalls/linux"
    25  
    26  	"github.com/ttpreport/gvisor-ligolo/pkg/hostarch"
    27  )
    28  
    29  // PollEventSet is the set of poll(2) event flags.
    30  var PollEventSet = abi.FlagSet{
    31  	{Flag: linux.POLLIN, Name: "POLLIN"},
    32  	{Flag: linux.POLLPRI, Name: "POLLPRI"},
    33  	{Flag: linux.POLLOUT, Name: "POLLOUT"},
    34  	{Flag: linux.POLLERR, Name: "POLLERR"},
    35  	{Flag: linux.POLLHUP, Name: "POLLHUP"},
    36  	{Flag: linux.POLLNVAL, Name: "POLLNVAL"},
    37  	{Flag: linux.POLLRDNORM, Name: "POLLRDNORM"},
    38  	{Flag: linux.POLLRDBAND, Name: "POLLRDBAND"},
    39  	{Flag: linux.POLLWRNORM, Name: "POLLWRNORM"},
    40  	{Flag: linux.POLLWRBAND, Name: "POLLWRBAND"},
    41  	{Flag: linux.POLLMSG, Name: "POLLMSG"},
    42  	{Flag: linux.POLLREMOVE, Name: "POLLREMOVE"},
    43  	{Flag: linux.POLLRDHUP, Name: "POLLRDHUP"},
    44  	{Flag: linux.POLLFREE, Name: "POLLFREE"},
    45  	{Flag: linux.POLL_BUSY_LOOP, Name: "POLL_BUSY_LOOP"},
    46  }
    47  
    48  func pollFD(t *kernel.Task, pfd *linux.PollFD, post bool) string {
    49  	revents := "..."
    50  	if post {
    51  		revents = PollEventSet.Parse(uint64(pfd.REvents))
    52  	}
    53  	return fmt.Sprintf("{FD: %s, Events: %s, REvents: %s}", fd(t, pfd.FD), PollEventSet.Parse(uint64(pfd.Events)), revents)
    54  }
    55  
    56  func pollFDs(t *kernel.Task, addr hostarch.Addr, nfds uint, post bool) string {
    57  	if addr == 0 {
    58  		return "null"
    59  	}
    60  
    61  	pfds, err := slinux.CopyInPollFDs(t, addr, nfds)
    62  	if err != nil {
    63  		return fmt.Sprintf("%#x (error decoding pollfds: %s)", addr, err)
    64  	}
    65  
    66  	s := make([]string, 0, len(pfds))
    67  	for i := range pfds {
    68  		s = append(s, pollFD(t, &pfds[i], post))
    69  	}
    70  
    71  	return fmt.Sprintf("%#x [%s]", addr, strings.Join(s, ", "))
    72  }