public static long recv_cred()

in dbus-java/src/main/java/org/freedesktop/dbus/connections/FreeBSDHelper.java [79:111]


    public static long recv_cred(Socket _us) {
        NativePOSIX posix = (NativePOSIX) POSIXFactory.getNativePOSIX();
        MsgHdr inMessage = posix.allocateMsgHdr();
        ByteBuffer[] inIov = new ByteBuffer[1];
        inIov[0] = ByteBuffer.allocateDirect(1);
        inMessage.setIov(inIov);
        // CmsgHdr inControl = inMessage.allocateControl(cmsgCredLayout.size());

        int fd = ((UnixSocketChannel) ((UnixSocket) _us).getChannel()).getFD();
        int recvBytes = -1;
        do {
            recvBytes = posix.recvmsg(fd, inMessage, 0);
        }

        while (recvBytes < 0 && Errno.valueOf(posix.errno()) == Errno.EINTR);

        if (recvBytes > 0 && inIov[0].get(0) == 0) {
            for (CmsgHdr cmsg : inMessage.getControls()) {
                if (cmsg.getType() == 0x03 && // 0x03 == SCM_CREDS
                        cmsg.getLevel() == SocketLevel.SOL_SOCKET.intValue() &&
                        cmsg.getLen() >= posix.socketMacros().CMSG_LEN(84)) {
                    ByteBuffer data = cmsg.getData();
                    final jnr.ffi.Pointer memory = jnr.ffi.Runtime.getSystemRuntime().getMemoryManager()
                            .allocateTemporary(cmsgCredLayout.size(), true);
                    for (int i = 0; i < cmsgCredLayout.size(); ++i) {
                        memory.putByte(i, data.get(i));
                    }
                    return cmsgCredLayout.cmcred_euid.get(memory);
                }
            }
        }
        return -1;
    }