go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/transport/ipc/ipc_peer_linux.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  // Copyright 2020 The Mangos Authors
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use file except in compliance with the License.
     8  // You may obtain a copy of the license at
     9  //
    10  //    http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package ipc
    19  
    20  import (
    21  	"net"
    22  	"syscall"
    23  
    24  	"go.nanomsg.org/mangos/v3"
    25  	"go.nanomsg.org/mangos/v3/transport"
    26  )
    27  
    28  func getPeer(c *net.UnixConn, pipe transport.ConnPipe) {
    29  	if sc, err := c.SyscallConn(); err == nil {
    30  		sc.Control(func(fd uintptr) {
    31  			uc, err := syscall.GetsockoptUcred(int(fd), syscall.SOL_SOCKET, syscall.SO_PEERCRED)
    32  			if err == nil {
    33  				pipe.SetOption(mangos.OptionPeerPID, int(uc.Pid))
    34  				pipe.SetOption(mangos.OptionPeerUID, int(uc.Uid))
    35  				pipe.SetOption(mangos.OptionPeerGID, int(uc.Gid))
    36  			}
    37  		})
    38  	}
    39  }