Re: Getting PID of socket client

From: Stefan Ehmann <shoesoft_at_gmx.net>
Date: Sun, 9 Jul 2017 11:47:14 +0200
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
Received on Sun Jul 09 2017 - 07:47:24 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:41:12 UTC