syscall_linux.go raw

   1  /*
   2   *
   3   * Copyright 2018 gRPC authors.
   4   *
   5   * Licensed under the Apache License, Version 2.0 (the "License");
   6   * you may not use this file except in compliance with the License.
   7   * You may obtain a copy of the License at
   8   *
   9   *     http://www.apache.org/licenses/LICENSE-2.0
  10   *
  11   * Unless required by applicable law or agreed to in writing, software
  12   * distributed under the License is distributed on an "AS IS" BASIS,
  13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14   * See the License for the specific language governing permissions and
  15   * limitations under the License.
  16   *
  17   */
  18  
  19  package channelz
  20  
  21  import (
  22  	"syscall"
  23  
  24  	"golang.org/x/sys/unix"
  25  )
  26  
  27  // SocketOptionData defines the struct to hold socket option data, and related
  28  // getter function to obtain info from fd.
  29  type SocketOptionData struct {
  30  	Linger      *unix.Linger
  31  	RecvTimeout *unix.Timeval
  32  	SendTimeout *unix.Timeval
  33  	TCPInfo     *unix.TCPInfo
  34  }
  35  
  36  // Getsockopt defines the function to get socket options requested by channelz.
  37  // It is to be passed to syscall.RawConn.Control().
  38  func (s *SocketOptionData) Getsockopt(fd uintptr) {
  39  	if v, err := unix.GetsockoptLinger(int(fd), syscall.SOL_SOCKET, syscall.SO_LINGER); err == nil {
  40  		s.Linger = v
  41  	}
  42  	if v, err := unix.GetsockoptTimeval(int(fd), syscall.SOL_SOCKET, syscall.SO_RCVTIMEO); err == nil {
  43  		s.RecvTimeout = v
  44  	}
  45  	if v, err := unix.GetsockoptTimeval(int(fd), syscall.SOL_SOCKET, syscall.SO_SNDTIMEO); err == nil {
  46  		s.SendTimeout = v
  47  	}
  48  	if v, err := unix.GetsockoptTCPInfo(int(fd), syscall.SOL_TCP, syscall.TCP_INFO); err == nil {
  49  		s.TCPInfo = v
  50  	}
  51  }
  52  
  53  // GetSocketOption gets the socket option info of the conn.
  54  func GetSocketOption(socket any) *SocketOptionData {
  55  	c, ok := socket.(syscall.Conn)
  56  	if !ok {
  57  		return nil
  58  	}
  59  	data := &SocketOptionData{}
  60  	if rawConn, err := c.SyscallConn(); err == nil {
  61  		rawConn.Control(data.Getsockopt)
  62  		return data
  63  	}
  64  	return nil
  65  }
  66