Get the effective credentials of a UNIX-domain peer
#include <sys/types.h> #include <unistd.h> int getpeereid( int s, uid_t *euid, gid_t *egid );
- A UNIX-domain socket (see the UNIX protocol) of type SOCK_STREAM on which either you've called connect() , or one returned from accept() after you've called bind() and listen() .
- NULL, or a pointer to a location where the function can store the effective user ID.
- NULL, or a pointer to a location where the function can store the effective group ID.
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The getpeereid() function gets the effective user and group IDs of the peer connected to a UNIX-domain socket. If euid and egid are non-NULL, the function stores the IDs in the locations they point to.
If you got the socket by calling connect(), the credentials are those of your peer when you called bind(). If the socket was one returned from accept(), the credentials are those of your peer when you called connect(). 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() or bind()) under different effective credentials.
UNIX-domain servers and clients commonly use this function to verify each other's credentials.
- An error occurred; errno is set.
- The argument s isn't a valid descriptor.
- The argument s is a file, not a socket.
- The argument s doesn't refer to a socket on which you've called connect(), or isn't one returned by listen().
- The argument s doesn't refer to a socket of type SOCK_STREAM, or the system returned invalid data.