gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/control/events.go (about)

     1  // Copyright 2021 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 control
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  
    21  	"gvisor.dev/gvisor/pkg/eventchannel"
    22  	"gvisor.dev/gvisor/pkg/urpc"
    23  )
    24  
    25  // EventsOpts are the arguments for eventchannel-related commands.
    26  type EventsOpts struct {
    27  	urpc.FilePayload
    28  }
    29  
    30  // Events is the control server state for eventchannel-related commands.
    31  type Events struct {
    32  	emitter eventchannel.Emitter
    33  }
    34  
    35  // AttachDebugEmitter receives a connected unix domain socket FD from the client
    36  // and establishes it as a new emitter for the sentry eventchannel. Any existing
    37  // emitters are replaced on a subsequent attach.
    38  func (e *Events) AttachDebugEmitter(o *EventsOpts, _ *struct{}) error {
    39  	if len(o.FilePayload.Files) < 1 {
    40  		return errors.New("no output writer provided")
    41  	}
    42  
    43  	sock, err := o.ReleaseFD(0)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	sockFD := sock.Release()
    48  
    49  	// SocketEmitter takes ownership of sockFD.
    50  	emitter, err := eventchannel.SocketEmitter(sockFD)
    51  	if err != nil {
    52  		return fmt.Errorf("failed to create SocketEmitter for FD %d: %v", sockFD, err)
    53  	}
    54  
    55  	// If there is already a debug emitter, close the old one.
    56  	if e.emitter != nil {
    57  		e.emitter.Close()
    58  	}
    59  
    60  	e.emitter = eventchannel.DebugEmitterFrom(emitter)
    61  
    62  	// Register the new stream destination.
    63  	eventchannel.AddEmitter(e.emitter)
    64  	return nil
    65  }