diff options
| author | Kenny Root <kroot@google.com> | 2010-09-14 14:26:12 -0700 |
|---|---|---|
| committer | Kenny Root <kroot@google.com> | 2010-09-14 14:35:10 -0700 |
| commit | 30abb7234de2a9caa1add4b00a189436f0b24560 (patch) | |
| tree | 7920d9c39658ef8a7dc10e3ba825857597953912 /libsysutils | |
| parent | 40c2b7cbedff612ce8bc7a51589a952cc1d047b6 (diff) | |
| download | system_core-30abb7234de2a9caa1add4b00a189436f0b24560.tar.gz system_core-30abb7234de2a9caa1add4b00a189436f0b24560.tar.bz2 system_core-30abb7234de2a9caa1add4b00a189436f0b24560.zip | |
Fetch peer credentials for local sockets
Fetch the PID, UID, and GID of the remote side of a local socket
connection in case any users of this library class want to check it.
Change-Id: Ia3230e6bc68ab6f93160df9f5996d2bf744b872c
Diffstat (limited to 'libsysutils')
| -rw-r--r-- | libsysutils/src/SocketClient.cpp | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp index e9ae23a9..8e5f1545 100644 --- a/libsysutils/src/SocketClient.cpp +++ b/libsysutils/src/SocketClient.cpp @@ -1,5 +1,6 @@ #include <alloca.h> #include <errno.h> +#include <sys/socket.h> #include <sys/types.h> #include <pthread.h> #include <string.h> @@ -9,9 +10,24 @@ #include <sysutils/SocketClient.h> -SocketClient::SocketClient(int socket) { - mSocket = socket; +SocketClient::SocketClient(int socket) + : mSocket(socket) + , mPid(-1) + , mUid(-1) + , mGid(-1) +{ pthread_mutex_init(&mWriteMutex, NULL); + + struct ucred creds; + socklen_t szCreds = sizeof(creds); + memset(&creds, 0, szCreds); + + int err = getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds); + if (err == 0) { + mPid = creds.pid; + mUid = creds.uid; + mGid = creds.gid; + } } int SocketClient::sendMsg(int code, const char *msg, bool addErrno) { |
