1 /* 2 * Copyright 2016 The Netty Project 3 * 4 * The Netty Project licenses this file to you under the Apache License, 5 * version 2.0 (the "License"); you may not use this file except in compliance 6 * with the License. You may obtain a copy of the License at: 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations 14 * under the License. 15 */ 16 17 package io.netty5.channel.unix; 18 19 import io.netty5.util.internal.UnstableApi; 20 21 import static io.netty5.util.internal.EmptyArrays.EMPTY_INTS; 22 23 /** 24 * User credentials discovered for the peer unix domain socket. 25 * 26 * The PID, UID and GID of the user connected on the other side of the unix domain socket 27 * For details see: 28 * <a href=https://man7.org/linux/man-pages/man7/socket.7.html>SO_PEERCRED</a> 29 */ 30 @UnstableApi 31 public final class PeerCredentials { 32 private final int pid; 33 private final int uid; 34 private final int[] gids; 35 36 // These values are set by JNI via Socket.peerCredentials() 37 PeerCredentials(int p, int u, int... gids) { 38 pid = p; 39 uid = u; 40 this.gids = gids == null ? EMPTY_INTS : gids; 41 } 42 43 /** 44 * Get the PID of the peer process. 45 * <p> 46 * This is currently not populated on MacOS and BSD based systems. 47 * @return The PID of the peer process. 48 */ 49 public int pid() { 50 return pid; 51 } 52 53 public int uid() { 54 return uid; 55 } 56 57 public int[] gids() { 58 return gids.clone(); 59 } 60 61 @Override 62 public String toString() { 63 StringBuilder sb = new StringBuilder(128); 64 sb.append("UserCredentials[pid=").append(pid).append("; uid=").append(uid).append("; gids=["); 65 if (gids.length > 0) { 66 sb.append(gids[0]); 67 for (int i = 1; i < gids.length; ++i) { 68 sb.append(", ").append(gids[i]); 69 } 70 } 71 sb.append(']'); 72 return sb.toString(); 73 } 74 }