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

     1  //go:build solaris && cgo
     2  // +build solaris,cgo
     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  
    23  	"go.nanomsg.org/mangos/v3"
    24  	"go.nanomsg.org/mangos/v3/transport"
    25  )
    26  
    27  // #include <ucred.h>
    28  // #include <stdio.h>
    29  // #include <zone.h>
    30  // typedef struct mycred {
    31  //		pid_t pid;
    32  //		uid_t uid;
    33  //		gid_t gid;
    34  //		zoneid_t zid;
    35  // } mycred_t;
    36  // int getucred(int fd, mycred_t *mc)
    37  // {
    38  //		ucred_t *uc = NULL;
    39  //		if ((getpeerucred(fd, &uc)) != 0) {
    40  //			return (-1);
    41  //		}
    42  //		mc->pid = ucred_getpid(uc);
    43  //		mc->uid = ucred_geteuid(uc);
    44  //		mc->gid = ucred_getegid(uc);
    45  //		mc->zid = ucred_getzoneid(uc);
    46  //		ucred_free(uc);
    47  //
    48  //		return (0);
    49  // }
    50  import "C"
    51  
    52  func getPeer(c *net.UnixConn, pipe transport.ConnPipe) {
    53  	if f, err := c.File(); err == nil {
    54  		mc := &C.mycred_t{}
    55  		if C.getucred(C.int(f.Fd()), mc) == 0 {
    56  			pipe.SetOption(mangos.OptionPeerPID, int(mc.pid))
    57  			pipe.SetOption(mangos.OptionPeerUID, int(mc.uid))
    58  			pipe.SetOption(mangos.OptionPeerGID, int(mc.gid))
    59  			pipe.SetOption(mangos.OptionPeerZone, int(mc.zid))
    60  		}
    61  	}
    62  }
    63  
    64  // getZone exists to support testing.
    65  func getZone() int {
    66  	return int(C.getzoneid())
    67  }