in dbus-java/src/main/java/org/freedesktop/dbus/connections/FreeBSDHelper.java [36:77]
public static void send_cred(Socket _us) throws java.io.IOException {
POSIX posix = POSIXFactory.getNativePOSIX();
String data = "\0";
byte[] dataBytes = data.getBytes();
MsgHdr outMessage = posix.allocateMsgHdr();
ByteBuffer[] outIov = new ByteBuffer[1];
outIov[0] = ByteBuffer.allocateDirect(dataBytes.length);
outIov[0].put(dataBytes);
outIov[0].flip();
outMessage.setIov(outIov);
CmsgHdr outControl = outMessage.allocateControl(cmsgCredLayout.size());
outControl.setLevel(SocketLevel.SOL_SOCKET.intValue());
outControl.setType(0x03); // 0x03 == SCM_CREDS
ByteBuffer fdBuf = ByteBuffer.allocateDirect(cmsgCredLayout.size());
fdBuf.order(ByteOrder.nativeOrder());
// fdBuf.putInt(i, 0);
outControl.setData(fdBuf);
int fd = ((UnixSocketChannel) ((UnixSocket) _us).getChannel()).getFD();
int sentBytes = -1;
do {
sentBytes = posix.sendmsg(fd, outMessage, 0);
} while (sentBytes < 0 && Errno.valueOf(posix.errno()) == Errno.EINTR);
if (sentBytes < 0) {
long err = posix.errno();
/* This might fail with EINVAL if the socket isn't AF_UNIX */
if (Errno.valueOf(err) == Errno.EINVAL) {
_us.getOutputStream().write(dataBytes);
} else {
throw new IOException("Failed to write credentials byte: " +
LastError.valueOf(err).toString());
}
} else if (sentBytes == 0) {
throw new IOException("wrote zero bytes writing credentials byte");
}
}