---
name: "getpeereid"
section: "3"
platform: "linux"
package: "libbsd-dev"
version: "0.8.3-1"
release: "Debian 9 (Stretch)"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/getpeereid"
---

# getpeereid(3)

get the effective credentials of a UNIX-domain peer

## NAME

**getpeereid** – get the effective credentials of a UNIX-domain peer

## LIBRARY

library “libbsd”

## SYNOPSIS

**#include \<sys/types.h\>**

**#include \<bsd/unistd.h\>**

*int*\

**getpeereid**(*int s*, *uid\_t \*euid*, *gid\_t \*egid*);

## DESCRIPTION

The **getpeereid**() function returns the effective user and group IDs of the peer connected to a UNIX-domain socket. The argument *s* must be a UNIX-domain socket ([unix(4)](/unix/4.md)) of type SOCK\_STREAM on which either [connect(2)](/connect/2.md) or [listen(2)](/listen/2.md) have been called. The effective used ID is placed in *euid*, and the effective group ID in *egid*.

The credentials returned to the [listen(2)](/listen/2.md) caller are those of its peer at the time it called [connect(2)](/connect/2.md); the credentials returned to the [connect(2)](/connect/2.md) caller are those of its peer at the time it called [listen(2)](/listen/2.md). This mechanism is reliable; there is no way for either side to influence the credentials returned to its peer except by calling the appropriate system call (i.e., either [connect(2)](/connect/2.md) or [listen(2)](/listen/2.md)) under different effective credentials.

One common use of this routine is for a UNIX-domain server to verify the credentials of its client. Likewise, the client can verify the credentials of the server.

## IMPLEMENTATION NOTES

On FreeBSD, **getpeereid**() is implemented in terms of the LOCAL\_PEERCRED [unix(4)](/unix/4.md) socket option.

## RETURN VALUES

The **getpeereid()** function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

## ERRORS

The **getpeereid**() function fails if:

- \[EBADF\]

  The argument *s* is not a valid descriptor.

- \[ENOTSOCK\]

  The argument *s* is a file, not a socket.

- \[ENOTCONN\]

  The argument *s* does not refer to a socket on which [connect(2)](/connect/2.md) or [listen(2)](/listen/2.md) have been called.

- \[EINVAL\]

  The argument *s* does not refer to a socket of type SOCK\_STREAM, or the kernel returned invalid data.

## SEE ALSO

[connect(2)](/connect/2.md), [getpeername(2)](/getpeername/2.md), [getsockname(2)](/getsockname/2.md), [getsockopt(2)](/getsockopt/2.md), [listen(2)](/listen/2.md), [unix(4)](/unix/4.md)

## HISTORY

The **getpeereid**() function appeared in FreeBSD 4.6.
