Getting PID of socket client

Stefan Ehmann shoesoft at gmx.net
Sun Jul 9 09:47:24 UTC 2017


On 09.07.2017 10:03, Johannes Lundberg wrote:
> Hi
> 
> Without altering the client code (i.e. adding sendmsg(credentials)), is
> there anyway of getting the client PID (or path to binary) using the file
> descriptor returned by accept() on the server side?
> 
> Similar to Linux's getsockopt with SO_PEERCRED option.

You want the LOCAL_CREDS socket option from unix(4).

There's also a FreeBSD sample in the UNIX network programming book. 
Source is available from http://unpbook.com/src.html

With the following two changes it seems to work for me:
unixstrserv02.c: set LOCAL_CREDS
readcred.c: disable CONTROL_LEN check

$ ./unixstrserv02
PID of sender = 1001
real user ID = 1001
real group ID = 1001
effective user ID = 1001
3 groups: 0 5 920


More information about the freebsd-current mailing list