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