gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/socket/unix/transport/connectioned_state.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 transport
    16  
    17  import "context"
    18  
    19  // saveAcceptedChan is invoked by stateify.
    20  func (e *connectionedEndpoint) saveAcceptedChan() []*connectionedEndpoint {
    21  	// If acceptedChan is nil (i.e. we are not listening) then we will save nil.
    22  	// Otherwise we create a (possibly empty) slice of the values in acceptedChan and
    23  	// save that.
    24  	var acceptedSlice []*connectionedEndpoint
    25  	if e.acceptedChan != nil {
    26  		// Swap out acceptedChan with a new empty channel of the same capacity.
    27  		saveChan := e.acceptedChan
    28  		e.acceptedChan = make(chan *connectionedEndpoint, cap(saveChan))
    29  
    30  		// Create a new slice with the same len and capacity as the channel.
    31  		acceptedSlice = make([]*connectionedEndpoint, len(saveChan), cap(saveChan))
    32  		// Drain acceptedChan into saveSlice, and fill up the new acceptChan at the
    33  		// same time.
    34  		for i := range acceptedSlice {
    35  			ep := <-saveChan
    36  			acceptedSlice[i] = ep
    37  			e.acceptedChan <- ep
    38  		}
    39  		close(saveChan)
    40  	}
    41  	return acceptedSlice
    42  }
    43  
    44  // loadAcceptedChan is invoked by stateify.
    45  func (e *connectionedEndpoint) loadAcceptedChan(_ context.Context, acceptedSlice []*connectionedEndpoint) {
    46  	// If acceptedSlice is nil, then acceptedChan should also be nil.
    47  	if acceptedSlice != nil {
    48  		// Otherwise, create a new channel with the same capacity as acceptedSlice.
    49  		e.acceptedChan = make(chan *connectionedEndpoint, cap(acceptedSlice))
    50  		// Seed the channel with values from acceptedSlice.
    51  		for _, ep := range acceptedSlice {
    52  			e.acceptedChan <- ep
    53  		}
    54  	}
    55  }
    56  
    57  // beforeSave is invoked by stateify.
    58  func (e *connectionedEndpoint) beforeSave() {
    59  	if e.boundSocketFD != nil {
    60  		panic("Cannot save endpoint with bound host socket")
    61  	}
    62  }
    63  
    64  // afterLoad is invoked by stateify.
    65  func (e *connectionedEndpoint) afterLoad(context.Context) {
    66  	e.ops.InitHandler(e, &stackHandler{}, getSendBufferLimits, getReceiveBufferLimits)
    67  }