lib/libc/net/minix/getpeereid.c
C | 36 lines | 23 code | 4 blank | 9 comment | 7 complexity | cef4a41faa31bca032c162e6cc5c85a8 MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/socket.h>
- /*
- * get the effective user ID and effective group ID of a peer
- * connected through a Unix domain socket.
- */
- int getpeereid(int sd, uid_t *euid, gid_t *egid) {
- int rc;
- struct ucred cred;
- socklen_t ucred_length;
- /* Initialize Data Structures */
- ucred_length = sizeof(struct ucred);
- memset(&cred, '\0', ucred_length);
- /* Validate Input Parameters */
- if (euid == NULL || egid == NULL) {
- errno = EFAULT;
- return -1;
- } /* getsockopt will handle validating 'sd' */
- /* Get the credentials of the peer at the other end of 'sd' */
- rc = getsockopt(sd, SOL_SOCKET, SO_PEERCRED, &cred, &ucred_length);
- if (rc == 0) {
- /* Success - return the results */
- *euid = cred.uid;
- *egid = cred.gid;
- return 0;
- } else {
- /* Failure - getsockopt takes care of setting errno */
- return -1;
- }
- }