I have a program that is listening to a Unix Domain Socket.
When a client connects to the socket I\'d like to find out which program connected and then decide if I allow
I searched for this quite a bit, so I will show you this example on how to use SO_PEERCRED
on a socket sock
to get the pid/uid/gid of the peer of a socket:
int len;
struct ucred ucred;
len = sizeof(struct ucred);
if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) {
//getsockopt failed
}
printf("Credentials from SO_PEERCRED: pid=%ld, euid=%ld, egid=%ld\n",
(long) ucred.pid, (long) ucred.uid, (long) ucred.gid);